%D \module
%D   [       file=back-pdf,
%D        version=2009.04.15,
%D          title=\CONTEXT\ Backend Macros,
%D       subtitle=\PDF,
%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.

%D The less there is here, the better.

\writestatus{loading}{ConTeXt Backend Macros / PDF}

\registerctxluafile{lpdf-ini}{optimize}
\registerctxluafile{lpdf-nod}{}
\registerctxluafile{lpdf-col}{}
\registerctxluafile{lpdf-xmp}{}
\registerctxluafile{lpdf-ano}{}
\registerctxluafile{lpdf-res}{}
\registerctxluafile{lpdf-mis}{}
\registerctxluafile{lpdf-ren}{}
\registerctxluafile{lpdf-grp}{}
\registerctxluafile{lpdf-wid}{}
\registerctxluafile{lpdf-fld}{}
\registerctxluafile{lpdf-mov}{}
\registerctxluafile{lpdf-u3d}{} % this will become a module
\registerctxluafile{lpdf-swf}{} % this will become a module
\registerctxluafile{lpdf-tag}{}
\registerctxluafile{lpdf-fmt}{}
\registerctxluafile{lpdf-pde}{}

\ifcase\contextlmtxmode\else
    \registerctxluafile{lpdf-img}{optimize}
\fi

\registerctxluafile{lpdf-epa}{}

\ifcase\contextlmtxmode\else
    \registerctxluafile{lpdf-emb}{optimize}
\fi

\registerctxluafile{back-pdp}{}

\registerctxluafile{lpdf-fnt}{}

\registerctxluafile{back-pdf}{} % some code will move to lpdf-*

\loadmarkfile{back-u3d} % this will become a module
\loadmarkfile{back-swf} % this will become a module

\unprotect

%D We will minimize the number of calls to \PDF\ specific primitives and delegate
%D all management and injection of code to the backend.
%D
%D Here we initialize some internal quantities. We also protect them.

\ifdefined\outputmode
    \outputmode\plusone
    \let\outputmode\relax
    \newcount\outputmode
    \outputmode\plusone
    \let\normaloutputmode\outputmode
\fi

%D Because we do a lot in \LUA\ and don't want interferences, we nil most of the
%D \PDFTEX\ primitives. Of course one can always use the \type {\pdfvariable},
%D \type {\pdfextension} and \type {\pdffeedback} primitives but it will probably
%D have bad side effects.

%D For the moment we put these here as they are pdf related but they might move to
%D a better place. We overload the primitives with our own but use a bit of indirection
%D for the purpose of tracing.

\unexpanded\def\saveboxresource            {\clf_saveboxresource}
\unexpanded\def\lastsavedboxresourceindex  {\numexpr\clf_lastsavedboxresourceindex\relax}
\unexpanded\def\useboxresource             {\clf_useboxresource}

\unexpanded\def\saveimageresource          {\clf_saveimageresource}
\unexpanded\def\lastsavedimageresourceindex{\numexpr\clf_lastsavedimageresourceindex\relax}
\unexpanded\def\lastsavedimageresourcepages{\numexpr\clf_lastsavedimageresourcepages\relax}
\unexpanded\def\useimageresource           {\clf_useimageresource}

\unexpanded\def\savepos                    {\clf_savepos}
           \def\lastxpos                   {\clf_lastxpos}
           \def\lastypos                   {\clf_lastypos}

\unexpanded\def\pdfextension               {\clf_pdfextension}
           \def\pdffeedback                {\clf_pdffeedback}

%D These are no-ops and don't even intercept what comes next. Maybe some day
%D I'll write a parser that maps onto \CONTEXT.

\unexpanded\def\unsupportedpdfprimitive#1%
  {\writestatus{fatal error}{the primitive \string#1\space is not supported}%
   \directlua{os.exit()}}

\unexpanded\def\pdfcolorstack    {\unsupportedpdfprimitive\pdfcolorstack}
\unexpanded\def\pdfcolorstackinit{\unsupportedpdfprimitive\pdfcolorstackinit}
%               pdfannot
\unexpanded\def\pdfstartlink     {\unsupportedpdfprimitive\pdfstartlink}
\unexpanded\def\pdfendlink       {\unsupportedpdfprimitive\pdfendlink}
\unexpanded\def\pdfoutline       {\unsupportedpdfprimitive\pdfoutline}
%               pdfdest
\unexpanded\def\pdfthread        {\unsupportedpdfprimitive\pdfthread}
\unexpanded\def\pdfstartthread   {\unsupportedpdfprimitive\pdfstartthread}
\unexpanded\def\pdfendthread     {\unsupportedpdfprimitive\pdfendthread}
\unexpanded\def\pdffontattr      {\unsupportedpdfprimitive\pdffontattr}
\unexpanded\def\pdfglyphtounicode{\unsupportedpdfprimitive\pdfglyphtounicode}

\unexpanded\def\pdfannot         {\clf_pdfannot}
\unexpanded\def\pdfdest          {\clf_pdfdest}

%D Here we do intercept (silently) what gets passed. One should use the \CONTEXT\
%D interfaces instead.

\let\pdfcatalog       \relax  \newtoks\pdfcatalog
\let\pdfinfo          \relax  \newtoks\pdfinfo
\let\pdfnames         \relax  \newtoks\pdfnames
\let\pdftrailer       \relax  \newtoks\pdftrailer
\let\pdfpageresources \relax  \newtoks\pdfpageresources
\let\pdfpageattr      \relax  \newtoks\pdfpageattr
\let\pdfpagesattr     \relax  \newtoks\pdfpagesattr
\let\pdfxformattr     \relax  \newtoks\pdfxformattr
\let\pdfxformresources\relax  \newtoks\pdfxformresources

%D We use the \LUA\ interface (which then permits more control over possible pdf/x
%D extensions).

\let\pdfcompresslevel   \relax  \newcount\pdfcompresslevel
\let\pdfobjcompresslevel\relax  \newcount\pdfobjcompresslevel

%D But we still provide:

\unexpanded\def\nopdfcompression        {\clf_setpdfcompression\zerocount\zerocount}
\unexpanded\def\onlypdfobjectcompression{\clf_setpdfcompression\zerocount\plusthree}
\unexpanded\def\maximumpdfcompression   {\clf_setpdfcompression\plusnine \plusnine }
\unexpanded\def\normalpdfcompression    {\clf_setpdfcompression\plusthree\plusthree}

%D These might even become no-ops as we don't need them in \CONTEXT:

\unexpanded\def\pdfmapfile#1{} % obsolete
\unexpanded\def\pdfmapline#1{} % obsolete

%D We don't support these directives, at least not this way. If they are needed
%D by third party modules we can provide some interface.

%pdfcreationdate
\let\pdfdecimaldigits          \relax \newcount\pdfdecimaldigits
\let\pdfdestmargin             \relax \newdimen\pdfdestmargin
%    pdffontname
%    pdffontobjnum
\let\pdffontsize               \relax \newcount\pdffontsize
\let\pdfgamma                  \relax \newcount\pdfgamma
\let\pdfgentounicode           \relax \newcount\pdfgentounicode
\let\pdfhorigin                \relax \newcount\pdfhorigin
\let\pdfignoreunknownimages    \relax \newcount\pdfignoreunknownimages
\let\pdfimageaddfilename       \relax \newcount\pdfimageaddfilename
\let\pdfimageapplygamma        \relax \newcount\pdfimageapplygamma
\let\pdfimagegamma             \relax \newcount\pdfimagegamma
\let\pdfimagehicolor           \relax \newcount\pdfimagehicolor
\let\pdfimageresolution        \relax \newcount\pdfimageresolution
\let\pdfincludechars           \relax \newtoks \pdfincludechars
\let\pdfinclusioncopyfonts     \relax \newcount\pdfinclusioncopyfonts
\let\pdfinclusionerrorlevel    \relax \newcount\pdfinclusionerrorlevel
\let\pdfinfoomitdate           \relax \newcount\pdfinfoomitdate
%    pdflastannot
%    pdflastlink
\let\pdflinkmargin             \relax \newdimen\pdflinkmargin
\let\pdfmajorversion           \relax \newcount\pdfmajorversion
\let\pdfminorversion           \relax \newcount\pdfminorversion
\let\pdfpagebox                \relax \newcount\pdfpagebox
%    pdfpageref
\let\pdfpkfixeddpi             \relax \newcount\pdfpkfixeddpi
\let\pdfpkmode                 \relax \newtoks \pdfpkmode
\let\pdfpkresolution           \relax \newcount\pdfpkresolution
%    pdfretval
\let\pdfsuppressoptionalinfo   \relax \newcount\pdfsuppressoptionalinfo
\let\pdfsuppressptexinfo       \relax \newcount\pdfsuppressptexinfo
%    pdftexrevision
%    pdftexversion
\let\pdfthreadmargin           \relax \newdimen\pdfthreadmargin
\let\pdftrailerid              \relax \newtoks \pdftrailerid
\let\pdfuniqueresname          \relax \newcount\pdfuniqueresname
\let\pdfvorigin                \relax \newcount\pdfvorigin
\let\pdfxformmargin            \relax \newdimen\pdfxformmargin
%   \pdfxformname

%D These are still accepted but are normally not needed.

\let\pdfxform                  \saveboxresource
\let\pdfximage                 \saveimageresource

\let\pdflastxform              \lastsavedboxresourceindex
\let\pdflastximage             \lastsavedimageresourceindex

\let\pdfrefxform               \useboxresource
\let\pdfrefximage              \useimageresource

\let\pdflastximagepages        \lastsavedimageresourcepages

\let\pdfsavepos                \savepos
\let\pdflastxpos               \lastxpos
\let\pdflastypos               \lastypos

%D For the moment we keep these as they are but they will become \LUA\ calls
%D eventually, after which we will nil the three \type {\pdf} interface primitives.

\normalprotected\def\pdfliteral  {\clf_pdfliteral}%

\normalprotected\def\pdfobj      {\clf_pdfobj}%
\normalprotected\def\pdflastobj  {\numexpr\clf_pdflastobj\relax}%

\normalprotected\def\pdfrefobj   {\pdfextension refobj }
\normalprotected\def\pdfrestore  {\clf_restore}
\normalprotected\def\pdfsave     {\clf_save}
\normalprotected\def\pdfsetmatrix{\clf_setmatrix}

%D This one can be consulted by users although the suffix is also a system mode.

\back_job_set_suffix{pdf}

%D PDF/X (maybe combine the two lua calls)

\setupbackend
  [xmpfile=]

\appendtoks
    \doifsomething{\backendparameter{xmpfile}}
      {\clf_setxmpfile{\backendparameter{xmpfile}}}%
\to \everysetupbackend

% \doifsomething{\backendparameter\c!format} .. at the lua end

\appendtoks
    \clf_setformat
         format  {\backendparameter\c!format}%
         level   {\backendparameter\c!level}%
         option  {\backendparameter\c!option}%
         profile {\backendparameter\c!profile}%
         intent  {\backendparameter\c!intent}%
         file    {\backendparameter\c!file}%
    \relax
\to \everysetupbackend

%D For the moment we keep these.

%newtoks \pdfbackendeveryximage
\newtoks \pdfbackendeveryxform

%D These are the only official methods to add stuff to the resources.

\unexpanded\def\pdfbackendsetcatalog       #1#2{\clf_lpdf_addtocatalog        {#1}{#2}}
\unexpanded\def\pdfbackendsetinfo          #1#2{\clf_lpdf_addtoinfo           {#1}{#2}}
\unexpanded\def\pdfbackendsetname          #1#2{\clf_lpdf_addtonames          {#1}{#2}}

\unexpanded\def\pdfbackendsetpageattribute #1#2{\clf_lpdf_addtopageattributes  {#1}{#2}}
\unexpanded\def\pdfbackendsetpagesattribute#1#2{\clf_lpdf_addtopagesattributes {#1}{#2}}
\unexpanded\def\pdfbackendsetpageresource  #1#2{\clf_lpdf_addtopageresources   {#1}{#2}}

\unexpanded\def\pdfbackendsetextgstate     #1#2{\clf_lpdf_adddocumentextgstate {#1}{#2}}
\unexpanded\def\pdfbackendsetcolorspace    #1#2{\clf_lpdf_adddocumentcolorspace{#1}{#2}}
\unexpanded\def\pdfbackendsetpattern       #1#2{\clf_lpdf_adddocumentpattern   {#1}{#2}}
\unexpanded\def\pdfbackendsetshade         #1#2{\clf_lpdf_adddocumentshade     {#1}{#2}}

           \def\pdfbackendcurrentresources     {\clf_lpdf_collectedresources}
           \def\pdfcolor                     #1{\clf_lpdf_color\numexpr\thecolorattribute{#1}\relax}
           \let\PDFcolor\pdfcolor

% clipping

\unexpanded\def\dostartclipping#1#2#3% we can move this to lua and only set a box here
  {\forcecolorhack
   \edef\width {\tobigpoints#2}%
   \edef\height{\tobigpoints#3}%
   \meta_grab_clip_path{#1}\width\height{%
     0            0             m %
     \width\space 0             l %
     \width\space \height\space l %
     0            \height\space l%
   }%
   \pdfliteral{q 0 w \MPclippath\space W n}}

\unexpanded\def\dostopclipping
  {\pdfliteral{Q}}

%D Temporary hack, will be removed or improved or default.

% attr {/Group << /S /Transparency /I false /K true >>}

%def\TransparencyHack{\ctxlua{backends.codeinjections.addtransparencygroup()}}
\def\TransparencyHack{\setupcolors[\c!pagecolormodel=\v!auto]}

% Still here:

%D \macros
%D  {back_ovalbox}
%D
%D Drawing frames with round corners is inherited from the main module. For drawing
%D ovals we use quite raw \PDF\ code. The next implementation does not differ that
%D much from the one implemented in the \POSTSCRIPT\ driver. This code is somewhat
%D obsolete as we now have metapost embedded.

\unexpanded\def\back_ovalbox#1#2#3#4#5#6#7#8%
  {\forcecolorhack
   \bgroup
   \edef\stroke{\tobigpoints\dimexpr#4\relax\space}%
   \edef\radius{\tobigpoints\dimexpr#5\relax\space}%
   \scratchdimen\dimexpr#4/\plustwo\relax
   \edef\xmin {\tobigpoints            \scratchdimen      \space}%
   \edef\xmax {\tobigpoints\dimexpr #1-\scratchdimen\relax\space}%
   \edef\ymax {\tobigpoints\dimexpr #2-\scratchdimen\relax\space}%
   \edef\ymin {\tobigpoints\dimexpr-#3+\scratchdimen\relax\space}%
   \advance\scratchdimen by #5\relax
   \edef\xxmin{\tobigpoints            \scratchdimen      \space}%
   \edef\xxmax{\tobigpoints\dimexpr #1-\scratchdimen\relax\space}%
   \edef\yymax{\tobigpoints\dimexpr #2-\scratchdimen\relax\space}%
   \edef\yymin{\tobigpoints\dimexpr-#3+\scratchdimen\relax\space}%
   %
   \edef\dostroke{\number#6}%
   \edef\dofill  {\number#7}%
   \scratchcounter#8\relax
   \setbox\scratchbox\naturalhpack
     {\ifnum\dostroke\dofill>\zerocount
        \pdfliteral
          {q
           \stroke w
           \ifcase\scratchcounter
             \xxmin \ymin  m
             \xxmax \ymin  l
             \xmax  \ymin  \xmax  \yymin y
             \xmax  \yymax l
             \xmax  \ymax  \xxmax \ymax  y
             \xxmin \ymax  l
             \xmin  \ymax  \xmin  \yymax y
             \xmin  \yymin l
             \xmin  \ymin  \xxmin \ymin  y
             h
           \or % 1
             \xxmin \ymin  m
             \xxmax \ymin  l
             \xmax  \ymin  \xmax  \yymin y
             \xmax  \ymax  l
             \xmin  \ymax  l
             \xmin  \yymin l
             \xmin  \ymin  \xxmin \ymin  y
             h
           \or % 2
             \xxmin \ymin  m
             \xmax  \ymin  l
             \xmax  \ymax  l
             \xxmin \ymax  l
             \xmin  \ymax  \xmin  \yymax y
             \xmin  \yymin l
             \xmin  \ymin  \xxmin \ymin  y
             h
           \or % 3
             \xmin  \ymin  m
             \xmax  \ymin  l
             \xmax  \yymax l
             \xmax  \ymax  \xxmax \ymax  y
             \xxmin \ymax  l
             \xmin  \ymax  \xmin  \yymax y
             \xmin  \ymin  l
             h
           \or % 4
             \xmin  \ymin  m
             \xxmax \ymin  l
             \xmax  \ymin  \xmax  \yymin y
             \xmax  \yymax l
             \xmax  \ymax  \xxmax \ymax  y
             \xmin  \ymax  l
             \xmin  \ymin  l
             h
           \or % 5
             \xmin  \ymin  m
             \xmax  \ymin  l
             \xmax  \yymax l
             \xmax  \ymax  \xxmax \ymax  y
             \xmin  \ymax  l
             \xmin  \ymin  l
             h
           \or % 6
             \xmin  \ymin  m
             \xxmax \ymin  l
             \xmax  \ymin  \xmax  \yymin y
             \xmax  \ymax  l
             \xmin  \ymax  l
             \xmin  \ymin  l
             h
           \or % 7
             \xxmin \ymin  m
             \xmax  \ymin  l
             \xmax  \ymax  l
             \xmin  \ymax  l
             \xmin  \yymin l
             \xmin  \ymin  \xxmin \ymin  y
             h
           \or % 8
             \xmin  \ymin  m
             \xmax  \ymin  l
             \xmax  \ymax  l
             \xxmin \ymax  l
             \xmin  \ymax  \xmin  \yymax y
             \xmin  \ymin  l
             h
           \or % 9 top open
             \xmin  \ymax  m
             \xmin  \yymin l
             \xmin  \ymin  \xxmin \ymin  y
             \xxmax \ymin  l
             \xmax  \ymin  \xmax  \yymin y
             \xmax  \ymax  l
           \or % 10 right open
             \xmax  \ymax  m
             \xxmin \ymax  l
             \xmin  \ymax  \xmin  \yymax y
             \xmin  \yymin l
             \xmin  \ymin  \xxmin \ymin  y
             \xmax  \ymin  l
           \or % 11 bottom open
             \xmax  \ymin  m
             \xmax  \yymax l
             \xmax  \ymax  \xxmax  \ymax y
             \xxmin \ymax  l
             \xmin  \ymax  \xmin  \yymax y
             \xmin  \ymin  l
           \or % 12 left open
             \xmin  \ymax  m
             \xxmax \ymax  l
             \xmax  \ymax  \xmax  \yymax y
             \xmax  \yymin l
             \xmax  \ymin  \xxmax \ymin  y
             \xmin  \ymin  l
           \or % 13
             \xmin  \ymax  m
             \xxmax \ymax  l
             \xmax  \ymax  \xmax  \yymax y
             \xmax  \ymin  l
           \or % 14
             \xmax  \ymax  m
             \xmax  \yymin l
             \xmax  \ymin  \xxmax \ymin  y
             \xmin  \ymin  l
           \or % 15
             \xmax  \ymin  m
             \xxmin \ymin  l
             \xmin  \ymin  \xmin  \yymin y
             \xmin  \ymax  l
           \or % 16
             \xmin  \ymin  m
             \xmin  \yymax l
             \xmin  \ymax  \xxmin \ymax  y
             \xmax  \ymax  l
           \or % 17
             \xxmax \ymax  m
             \xmax  \ymax  \xmax  \yymax y
           \or % 18
             \xmax  \yymin m
             \xmax  \ymin  \xxmax \ymin  y
           \or % 19
             \xxmin \ymin  m
             \xmin  \ymin  \xmin  \yymin y
           \or % 20
             \xmin  \yymax m
             \xmin  \ymax  \xxmin \ymax  y
           \or % 21
             \xxmax \ymax  m
             \xmax  \ymax  \xmax  \yymax y
             \xmin  \yymax m
             \xmin  \ymax  \xxmin \ymax  y
           \or % 22
             \xxmax \ymax  m
             \xmax  \ymax  \xmax  \yymax y
             \xmax  \yymin m
             \xmax  \ymin  \xxmax \ymin  y
           \or % 23
             \xmax  \yymin m
             \xmax  \ymin  \xxmax \ymin  y
             \xxmin \ymin  m
             \xmin  \ymin  \xmin  \yymin y
           \or % 24
             \xxmin \ymin  m
             \xmin  \ymin  \xmin  \yymin y
             \xmin  \yymax m
             \xmin  \ymax  \xxmin \ymax  y
           \or % 25
             \xxmax \ymax  m
             \xmax  \ymax  \xmax  \yymax y
             \xmax  \yymin m
             \xmax  \ymin  \xxmax \ymin  y
             \xxmin \ymin  m
             \xmin  \ymin  \xmin  \yymin y
             \xmin  \yymax m
             \xmin  \ymax  \xxmin \ymax  y
           \or % 26
             \xmax  \yymin m
             \xmax  \ymin  \xxmax \ymin  y
             \xmin  \yymax m
             \xmin  \ymax  \xxmin \ymax  y
           \or % 27
             \xxmax \ymax  m
             \xmax  \ymax  \xmax  \yymax y
             \xxmin \ymin  m
             \xmin  \ymin  \xmin  \yymin y
           \or % 28
           \fi
           \ifnum\scratchcounter>\pluseight
                                       S
           \else
             \ifnum\dostroke=\plusone  S \fi
             \ifnum\dofill  =\plusone  f \fi
           \fi
           Q}%
      \fi}%
   \wd\scratchbox#1%
   \ht\scratchbox#2%
   \dp\scratchbox#3%
   \box\scratchbox
   \egroup}

\unexpanded\def\pdfbackendactualtext#1#2% not interfaced
  {\clf_startactualtext{#2}%
   #1%
   \clf_stopactualtext}

\let\pdfactualtext\pdfbackendactualtext

% \starttext
%     text \pdfbackendactualtext{Meier}{Müller} text
% \stoptext

\protect \endinput
