%D \module
%D   [       file=scrn-hlt,
%D        version=2024.09.12,
%D          title=\CONTEXT\ Core Macros,
%D       subtitle=PDF Highlights,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

\writestatus{loading}{ConTeXt Interaction Macros / Highlights}

%D This is a fragile feature implemented in abit hackish way, that is: we run on top
%D of links so thatwe need very little code. Viewers implement the quad points
%D different from the standard (probably a side effect of acrobat doing it that way:
%D swapping the third and fourth coordinate).
%D
%D We use the PDF namespace so that it's clear that we have some hack. It's mainly
%D meant for Hraban.
%D
%D \starttyping
%D \enabledirectives[backend.pdf.fixhighlight]
%D
%D \definecolor[pdfhighlight:Rhaban][r=.8,g=1,b=1]
%D \definecolor[pdfhighlight:Hans]  [r=1,g=.8,b=1]
%D \definecolor[pdfhighlight:Ton]   [r=1,g=1,b=.8]
%D
%D test \PDFhighlight[Rhaban][Who also wants content.]{\samplefile{tufte}} test \blank
%D test \PDFhighlight[Hans]{what a mess}          test \page
%D test \PDFhighlight[Ton]{\samplefile{tufte}}    test \page
%D \stoptyping
%D
%D Currently the directive is needed for Acrobat Reader, Sumatra PDF, Okular, and
%D maybe more viewers.

%D Tested September 14, 2024 by Mikael & Hans:
%D
%D \starttabulate
%D \NC Firefox    \NC follows standard but also works with swapping \NC \NR
%D \NC EBookDroid \NC follows standard but does something when swapping \NC \NR
%D \NC Google     \NC show nothing \NC \NR
%D \NC okular     \NC swap 3 and 4 \NC \NR
%D \NC evince     \NC swap 3 and 4 \NC \NR
%D \NC zathura    \NC swap 3 and 4 \NC \NR
%D \NC qpdf       \NC swap 3 and 4 \NC \NR
%D \NC acrobat    \NC swap 3 and 4 \NC \NR
%D \NC sumatra    \NC swap 3 and 4, more curved \NC \NR
%D \stoptabulate
%D
%D So, it looks like firefox is doing some analysis on probably the x
%D coordinates and then accepts a rotated rectangle to misbehave but no
%D one uses that anyway (the spec could have been 4 numbers plus angle to
%D save space).

\unprotect

\aliased\let\stopPDFhighlight\relax

\def\strc_references_faked_goto#1[#2]% no test for valid references
  {\dontleavehmode
   \begingroup
   \c_attr_reference\attributeunsetvalue
   \global\lastsavedreferenceattribute\attributeunsetvalue
   \iflocation
     \clf_injectreference
       %{\referenceprefix}%
        {}%
        {#2}%
        {%
             height .9\strutht
             depth  .9\strutdp
            %\extrareferencearguments
        }%
     \relax
    %\setlocationattributes
    %\setstrut % can be option
     \global\lastsavedreferenceattribute\lastreferenceattribute
     \c_attr_reference\lastreferenceattribute
    %\ifexporting\dostarttaggednodetail\t!link\fi
     #1%
    %\ifexporting\dostoptagged\fi
   \else
     #1%
   \fi
   \endgroup}

\permanent\tolerant\protected\def\startPDFhighlight[#1]#*[#2]#:#3\stopPDFhighlight
  {\strc_references_faked_goto{#3}[highlight(#1{#2})]}

\permanent\tolerant\protected\def\PDFhighlight[#1]#*[#2]#:#3%
  {\strc_references_faked_goto{#3}[highlight(#1{#2})]}

\protect
