%D \module
%D   [       file=attr-ini,
%D        version=2007.06.06,
%D          title=\CONTEXT\ Attribute Macros,
%D       subtitle=Initialization,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright=PRAGMA-ADE]
%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 Attribute Macros / Initialization}

%D Although it's still somewhat experimental, here we introduce code
%D related to attributes.

\unprotect

\registerctxluafile{attr-ini}{1.001}

\definesystemattribute[state]
\definesystemattribute[skip]
\definesystemattribute[penalty]
\definesystemattribute[colormodel][global]                     \chardef\colormodelattribute     \dogetattributeid{colormodel}
\definesystemattribute[color]                                  \chardef\colorattribute          \dogetattributeid{color}
\definesystemattribute[transparency]                           \chardef\transparencyattribute   \dogetattributeid{transparency}
\definesystemattribute[background]                             \chardef\backgroundattribute     \dogetattributeid{background}
\definesystemattribute[overprint]
\definesystemattribute[negative]
\definesystemattribute[effect]
\definesystemattribute[viewerlayer]                            \chardef\viewerlayerattribute    \dogetattributeid{viewerlayer}
\definesystemattribute[layoutcomponent]                        \chardef\layoutcomponentattribute\dogetattributeid{layoutcomponent}
\definesystemattribute[reference]                              \chardef\referenceattribute      \dogetattributeid{reference}
\definesystemattribute[destination]                            \chardef\destinationattribute    \dogetattributeid{destination}
\definesystemattribute[graphicvadjust]                         \chardef\graphicvadjustattribute \dogetattributeid{graphicvadjust}

% \definesystemattribute[ignore]
%
% \edef\startignorecontent{\dosetattribute{ignore}\plusone}
% \edef\stopignorecontent {\doresetattribute{ignore}}

% todo: no need for 'color' argument, we can set that once at startup; currently
% a bit inconsistent

% 1=off  2=gray  3=spot  4=rgb  5=cmyk  6=cmy % only 1/2/4/5 are supported
%
% We could combine this in one attribute but this is not faster and also
% less flexible because sometimes we want to freeze the attribute bit.
%
% Watch out: real color support will be implemented later.

\newcount\currentcolormodel

\def\setcolormodel#1%
  {\currentcolormodel\ctxlua{tex.print(colors.setmodel('#1'))}%
   \dosetattribute{colormodel}{\the\currentcolormodel}}

\setcolormodel{all}

\appendtoks
    \setcolormodel{all}% redundant?
\to \everyjob

\def\registerrgbcolor   #1#2#3#4{\ctxlua{colors.register('#1','rgb' ,#2,#3,#4)}}
\def\registercmykcolor#1#2#3#4#5{\ctxlua{colors.register('#1','cmyk',#2,#3,#4,#5)}}
\def\registergraycolor      #1#2{\ctxlua{colors.register('#1','gray',#2)}}

% transparency

\def\registertransparency#1#2#3%
  {\setevalue{(ts:#1)}{\dosetattribute{transparency}{\ctxlua{tex.print(transparencies.register(#2,#3))}}}}

\def\sometransparencyswitch#1{\csname(ts:#1)\endcsname}

\def\sometransparencyswitch
  {\ctxlua{transparencies.enabled=true}%
   \gdef\sometransparencyswitch##1{\csname(ts:##1)\endcsname}%
   \sometransparencyswitch}

% \registertransparency {one} {1} {.5}
% \registertransparency {two} {1} {.6}

% overprint

\def\registeroverprint#1#2%
  {\setevalue{(os:#1)}{\dosetattribute{overprint}{\ctxlua{tex.print(overprints.register('#2'))}}}}

\def\dotriggeroverprint
  {\ctxlua{overprints.enabled=true}%
   \gdef\dotriggeroverprint##1{\csname(os:##1)\endcsname}%
   \dotriggeroverprint}

\registeroverprint{knockout} {knockout}
\registeroverprint{overprint}{overprint}

% negative

\def\registernegative#1#2%
  {\setevalue{(ns:#1)}{\dosetattribute{negative}{\ctxlua{tex.print(negatives.register('#2'))}}}}

\def\dotriggernegative
  {\ctxlua{negatives.enabled=true}%
   \gdef\dotriggernegative##1{\csname(ns:##1)\endcsname}%
   \dotriggernegative}

\registernegative{positive}{positive}
\registernegative{negative}{negative}

% effect

\def\registereffect#1#2#3% #2=stretch #3=rulethickness
  {\setxvalue{(es:#1:#2:\number\dimexpr#3\relax)}%
     {\dosetattribute{effect}{\ctxlua{tex.print(effects.register('#1',#2,\number\dimexpr#3\relax))}}}}

\def\dotriggereffect
  {\ctxlua{effects.enabled=true}%
   \gdef\dotriggereffect##1##2##3%
     {\ifcsname(es:##1:##2:\number\dimexpr##3\relax)\endcsname\else\registereffect{##1}{##2}{##3}\fi
      \csname(es:##1:##2:\number\dimexpr##3\relax)\endcsname}%
   \dotriggereffect}

% \registereffect{normal}
% \registereffect{inner}
% \registereffect{outer}
% \registereffect{both}
% \registereffect{hidden}

% viewerlayers (will probably change a bit)

% \def\registerviewerlayer#1#2% global !
%   {\setxvalue{(vl:#1)}{\dosetattribute{viewerlayer}{\ctxlua{tex.print(viewerlayers.register('#2'))}}}}
%
% \setevalue{(vl:)}{\doresetattribute{viewerlayer}}
%
% needs to work over stopitemize grouping etc

\def\registerviewerlayer#1#2% global !
  {\setxvalue{(vl:#1)}{\global\dosetattribute{viewerlayer}{\ctxlua{tex.print(viewerlayers.register('#2'))}}}}

\setevalue{(vl:)}{\global\doresetattribute{viewerlayer}}

\def\dotriggerviewerlayer
  {\ctxlua{viewerlayers.enabled=true}%
   \gdef\dotriggerviewerlayer##1{\csname(vl:##1)\endcsname}%
   \dotriggerviewerlayer}

\protect \endinput

% test case
%
% {\green \hbox to \hsize{\leaders\hrule  \hfill a}\par}
% {\red   \hbox to \hsize{\leaders\hbox{x}\hfill a}\par}
