%D \module
%D   [       file=back-imp-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. After a decade it is time to remove the \type
%D {\pdf*} ones completely. For the moment I keep them commented but even that will
%D go away.

% semi-automatic:
%
% profile=,intent=default => profile={sRGB-v4.icc,CGATS001Compat-v2-micro.icc,sGrey-v4.icc}
% profile=,intent=rgb     => profile={sRGB-v4.icc,CGATS001Compat-v2-micro.icc,sGrey-v4.icc}
% profile=,intent=cmyk    => profile={sRGB-v4.icc,CGATS001Compat-v2-micro.icc}
%
% manual:
%
% profile={sRGB-v4.icc,CGATS001Compat-v2-micro.icc,sGrey-v4.icc},
%
% or:
%
% profile={sRGB-v4.icc,CGATS001Compat-v2-micro.icc,sGrey-v4.icc},
% intent={sRGB-v4.icc},

%D Todo: a few delayed binding left:

% ./lpdf-ini.lmt : reserve | immediate | positions | matrix | pos | fontobject
% ./lpdf-lmt.lmt : fontname | object | boxresources
% ./lpdf-rul.lmt : mp stuff
% ./back-res.lmt : boxresources

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

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

\registerctxluafile{lpdf-ini}{autosuffix}
\registerctxluafile{lpdf-lmt}{autosuffix}
\registerctxluafile{lpdf-sig}{autosuffix,optional}
\registerctxluafile{lpdf-col}{autosuffix}
\registerctxluafile{lpdf-xmp}{autosuffix}
\registerctxluafile{lpdf-ano}{autosuffix}
\registerctxluafile{lpdf-mis}{autosuffix}
\registerctxluafile{lpdf-ren}{autosuffix}
\registerctxluafile{lpdf-grp}{autosuffix}
\registerctxluafile{lpdf-wid}{autosuffix}
\registerctxluafile{lpdf-fld}{autosuffix}
\registerctxluafile{lpdf-tag}{autosuffix}
\registerctxluafile{lpdf-fmt}{autosuffix}
\registerctxluafile{lpdf-pde}{autosuffix}
\registerctxluafile{lpdf-img}{autosuffix}
\registerctxluafile{lpdf-epa}{autosuffix}
\registerctxluafile{lpdf-emb}{autosuffix}
\registerctxluafile{lpdf-fix}{autosuffix}
\registerctxluafile{lpdf-rul}{autosuffix}

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

\registerctxluafile{lpdf-fix-imp-fonts}   {autosuffix} % some code will move to lpdf-*
\registerctxluafile{lpdf-fix-imp-contents}{autosuffix} % idem

\registerctxluafile{lxml-mms}{autosuffix}

% \registerctxluafile{lpdf-u3d}{autosuffix}

\loadmkxlfile{back-imp-u3d}

\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 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. We fake them as primitives which is why they are
%D defined as untraced.

% \pdfextension % defined in lua
% \pdffeedback  % idem
% \pdfvariable  % idem
% \pdfliteral   % idem
% \pdfobj       % idem
% \pdflastobj   % idem
% \pdfrefobj    % idem

\untraced\permanent\protected\def\pdfrestore  {\pdfextension restore}
\untraced\permanent\protected\def\pdfsave     {\pdfextension save}
\untraced\permanent\protected\def\pdfsetmatrix{\pdfextension setmatrix}

\untraced\permanent\def\pdfxform              {\saveboxresource}
\untraced\permanent\def\pdflastxform          {\lastsavedboxresourceindex}
\untraced\permanent\def\pdfrefxform           {\useboxresource}

%D This is also needed for tikz:

\ifdefined\pdfsavepos\else
    \untraced\permanent\def\pdfsavepos {\savepos }
    \untraced\permanent\def\pdflastxpos{\lastxpos}
    \untraced\permanent\def\pdflastypos{\lastypos}
\fi

%D Here are quick and dirty compression flippers, mostly used when testing something
%D as one can best stick to the defaults that also adapt to specific standards.

% level objectlevel

\untraced\permanent\protected\def\nopdfcompression        {\clf_setpdfcompression\zerocount\zerocount}
\untraced\permanent\protected\def\nopdfobjectcompression  {\clf_setpdfcompression\plusthree\zerocount}
\untraced\permanent\protected\def\onlypdfobjectcompression{\clf_setpdfcompression\zerocount\plusthree}
\untraced\permanent\protected\def\maximumpdfcompression   {\clf_setpdfcompression\plusnine \plusnine }
\untraced\permanent\protected\def\normalpdfcompression    {\clf_setpdfcompression\plusthree\plusthree}

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

\setupbackend
  [xmpfile=,
   profile=,
   intent=default]

\appendtoks
    \edef\p_file{\backendparameter{xmpfile}}%
    \ifempty\p_file\else
      \clf_setxmpfile{\p_file}%
    \fi
\to \everysetupbackend

%D This will change:

\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}% profile database
    \relax
\to \everysetupbackend

%D These are the only official methods to add stuff to the resources. If more is
%D needed for third party low level code, it can be added.

% \pdfbackendsetcatalog       #1#2 % defined in lua
% \pdfbackendsetinfo          #1#2 % idem
% \pdfbackendsetname          #1#2 % idem
%
% \pdfbackendsetpageattribute #1#2 % idem
% \pdfbackendsetpagesattribute#1#2 % idem
% \pdfbackendsetpageresource  #1#2 % idem
%
% \pdfbackendsetextgstate     #1#2 % idem
% \pdfbackendsetcolorspace    #1#2 % idem
% \pdfbackendsetpattern       #1#2 % idem
% \pdfbackendsetshade         #1#2 % idem
%
% \pdfbackendcurrentresources      %  idem, expandable
% \pdfcolor                     #1 %  idem, expandable

%D This is a temporary hack that will be removed, improved or somehow can become
%D default.

\permanent\def\TransparencyHack{\setupcolors[\c!pagecolormodel=\v!auto]}

%D Just in case one needs this \unknown:
%D
%D \starttyping
%D text \pdfbackendactualtext{Meier}{Müller} text
%D \stoptyping

\untraced\permanent\protected\def\pdfbackendactualtext#1#2% not interfaced
  {\clf_startactualtext{#2}%
   #1%
   \clf_stopactualtext}

\aliased\let\pdfactualtext\pdfbackendactualtext

\untraced\permanent\protected\def\pdfbackendalternativetext#1#2% not interfaced
  {\clf_startalternativetext{#2}%
   #1%
   \clf_stopalternativetext}

\aliased\let\pdfalternativetext\pdfbackendalternativetext

\permanent\protected\def\startPDFalternativetext{\clf_startalternativetext}
\permanent\protected\def\stopPDFalternativetext {\clf_stopalternativetext}

\protect \endinput