% hele base laden

\setXMLfallbackmode3

% icon        : preview
% caption     : \figurebasevariable{caption}
% background  : rgb cmyk gray name
% dimensions  : width height offset (l,r,t,b) area
% alternative : tag label / evt list
%
% movie
% sound
% applet
% application

%D \module
%D   [      file=x-fig-04,
%D        version=2001.03.21,
%D          title=\CONTEXT\ Style File,
%D       subtitle=Figure Base Loading,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA / Hans Hagen \& Ton Otten}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

\usemodule[res-00] % basic element definitions 

%D This module implements an interface to a figure database
%D and file. The database is formatted in \XML\ conforming
%D the following \DTD:
%D
%D \typefile{x-fig-00.dtd}
%D
%D A figure base coded this way looks like:
%D
%D \starttypen
%D <!-- texexec --pdf --use=fig-01 figtest.xml -->
%D
%D <figurelibrary language="nl">
%D
%D   <description>
%D     <organization>PRAGMA Advanced Document Engineering</organization>
%D     <project>Manuals</project>
%D     <product>Beginners Manual</product>
%D     <comment>A bunch of figures.</comment>
%D   </description>
%D
%D   <figure>
%D     <file>koe.pdf</file>
%D     <label>a dutch cow</label>
%D     <copyright>Corel Draw Suite</copyright>
%D     <comment>I bet that you've seen this cow before.</comment>
%D   </figure>
%D
%D   <figure>
%D     <dummy width="4cm" height="3cm">non existent</dummy>
%D     <label>a european cow</label>
%D     <copyright>Nobody</copyright>
%D     <comment>When will we talk about European cows?</comment>
%D   </figure>
%D
%D </figurelibrary>
%D \stoptypen
%D
%D You can convert this base into a \PDF\ file using
%D \TEXEXEC\ and another module in this suite.
%D
%D \starttypen
%D texexec --pdf --use=fig-make yourfile.xml
%D \stoptypen
%D
%D You can now select a graphic from this file using the
%D
%D \starttypen
%D \externalfigure[a dutch cow][width=4cm]
%D \stoptypen
%D
%D This module overloads this command so that a figure is
%D it first searched in the list of databases.
%D
%D \starttypen
%D \usemodule     [fig-base]
%D \usefigurebases[yourfile]
%D \stoptypen
%D
%D The special keyword \type {reset} can be used to reset
%D this list.

\startcommands      dutch                 english
                    german                czech
                    italian               romanian

     usefigurebase: gebruikfiguurbestand  usefigurebase
                    usefigurebase         usefigurebase
                    usefigurebase         usefigurebase

\stopcommands

\unprotect

% 0 = no loading
% 1 = selective loading
% 2 = full loading

\chardef\figurebasemode=1 % 2

\newcounter\figurefilepage

% loading a complete figure base 

\startXMLmapping[rl:load]

\defineXMLenvironmentsave [rl:figure]
  {\bgroup}
  {\XMLflush{rl:figure}
   \doglobal\increment\figurefilepage
   \figbase@savedata{\XMLflush{rl:label}}\figurefilepage
   \egroup}

\stopXMLmapping 

\def\loadfigurebase#1%
  {\doifnotflagged{rl:#1}
     {\writestatus{figbase}{loading #1 into memory}%
      \startnointerference
        \autoXMLnamespace[rl]
        \startXMLmapping[rl:load]
          \doglobal\newcounter\figurefilepage
          \processXMLfilegrouped{#1.xml}
        \stopXMLmapping
        \doglobal\setflag{rl:#1} 
      \stopnointerference}}

\def\figbase@savedata#1#2%
  {%\writestatus{figbase}{data of #1 loaded}%
   \doglobal\saveXMLdatastructure{rl:#1}{record}{page="#2"}{}{rl:figure}{}}

% locating and if needed loading one figure record 

\startXMLmapping[rl:find]

\defineXMLenvironment[rl:instance]
  {\bgroup}
  {\doif\askedlabel{\XMLflush{rl:label}}
     {\doglobal\saveXMLdata{rl:g:manipulation}{rl:manipulation}%
      \doifXMLdata{rl:original}
        {\xdef\askedlabel{\XMLflush{rl:original}}}}%
   \egroup}

\defineXMLenvironmentsave [rl:figure]
  {\bgroup}
  {\XMLflush{rl:figure}
   \doglobal\increment\figurefilepage
   \doif\askedlabel{\XMLflush{rl:label}}
     {\ifnum\figurebasemode=\plusone % load used ones 
        \figbase@savedata\askedlabel\figurefilepage
      \fi
      \doglobal\saveXMLdata{rl:l:manipulation}{rl:manipulation}%
      \xdef\figurefilelabel   {\XMLflush{rl:label}}%
      \xdef\figurefilefile    {\XMLflush{rl:file}}%
      \xdef\figurefileoriginal{\XMLflush{rl:original}}%
      \xdef\figurefilename    {\XMLflush{rl:file}} 
      \endinput}%
   \egroup}

\defineXMLenvironment [rl:record]
  {}
  {\xdef\figurefilename{\XMLpop{rl:file}}
   \xdef\figurefilepage{\XMLop {page}}}

\stopXMLmapping 

\def\getfigurefilename#1#2%
  {\ifnum\figurebasemode=2 \loadfigurebase{#1} \fi
   \startnointerference
     \traceXMLelementsfalse
     \autoXMLnamespace[rl]
     \startXMLmapping[rl:find]
       \resetfigurefilebase
       \xdef\figurefilebase{#1}
       \doglobal\newcounter\figurefilepage
       \def\askedlabel{#2}
       \doifelseXMLelement{rl:\askedlabel}
         {\enableXMLelements\flushXMLelement{rl:\askedlabel}}
         {\processcommacommand[\figurepathlist]\dogetfigurefilename}%
     \stopXMLmapping
   \stopnointerference}

% todo: niet toegekendede naam doorgeven aan calculate en pad 
% in padstring 

\def\dogetfigurefilename#1%
  {\ifx\figurefilename\empty
     \bgroup
     \globalletempty\figurefilelabel
     \globalletempty\figurefileoriginal
     \globalletempty\figurefilefile
     \xdef\figurefilebasepath{#1}%
     \assignfullfilename{#1}\figurefilebase\to\filename
     \expanded{\processXMLfilegrouped{\filename.xml}}%
     \ifx\figurefilename\empty \else
       \global\let\figurefilebase\figurefilebase
     \fi
     \egroup
   \fi}

\newtoks\figurebaseresets

\appendtoks
  \globalletempty\figurefilebase
  \globalletempty\figurefilename
  \globalletempty\figurefilebasepath
  \globalletempty\figurefilepage
  \globalletempty\figurefilelabel
 %\globalletempty\figurefileoriginal
 %\globalletempty\figurefilefile
\to\figurebaseresets

\def\resetfigurefilebase
  {\the\figurebaseresets}

\let\normalcalculateexternalfigure\calculateexternalfigure

\def\calculateexternalfigure[#1][#2][#3][#4][#5][#6]%
  {\resetfigurefilebase
   \ifx\figurebaselist\empty
     \normalcalculateexternalfigure
       [#1][#2][#3][#4][#5][#6]%
   \else
     \def\docommando##1%
       {\getfigurefilename{##1}{#3}%
        \ifx\figurefilename\empty\else
          \gdef\figurefilelabel{#3}% 
          \quitcommalist
        \fi}%
     \processcommacommand[\figurebaselist]\docommando
     \ifx\figurefilename\empty
       \stripspaces\from#3\to\figurefilename % to be sure
       \normalcalculateexternalfigure
         [#1][#2][\figurefilename][#4][#5][#6]%
     \else
       \doiffileelse{\figurefilebase.pdf}
         {\let\figurepathlist\figurefilebasepath
          \normalcalculateexternalfigure
            [#1][#2][\figurefilebase.pdf]%
            [\c!pagina=\figurefilepage,#4][#5][#6]}
         {\@EA\stripspaces\@EA\from\figurefilename\to\figurefilename 
          \normalcalculateexternalfigure
            [#1][#2][\figurefilename][#4][#5][#6]}%
     \fi
   \fi}

% management 

% will become \useresourcelibrary 

\def\usefigurebase[#1]%
  {\doifelse{#1}\v!reset
     {\let\figurebaselist\empty}
     {\addtocommalist{#1}\figurebaselist}}

\let\figurebaselist\empty

\resetfigurefilebase

% manipulations / todo: fixed order 

\defineXMLsave [rl:manipulation] 

\defineXMLsingular [rl:background] [r=0,g=0,b=0,s=0,c=0,m=0,y=0,k=0] 
  {\global\setbox\foundexternalfigure\vbox
     {\definecolor
        [XMLRLcolor]
        [r=\XMLop{r},g=\XMLop{g},b=\XMLop{b},s=\XMLop{s},%
         c=\XMLop{c},m=\XMLop{m},y=\XMLop{y},k=\XMLop{k}]%
       \framed
         [\c!kader=\v!uit,\c!offset=\v!overlay,
          \c!achtergrond=\v!kleur,\c!achtergrondkleur=XMLRLcolor]
         {\box\foundexternalfigure}}}

\defineXMLsingular [rl:viewport] [\??cp] % []
  {\global\setbox\foundexternalfigure\vbox
     {\expandXMLta \getXMLta 
      \clip
        [\XMLta]
        {\box\foundexternalfigure}}%
   \global\setbox\foundexternalfigure\vbox
     {\scale
        [\c!hoogte=\figurewidth,\c!hoogte=\figureheight]
        {\box\foundexternalfigure}}}

\defineXMLsingular [rl:dimensions] [width=,height=]
  {}

\defineXMLsingular [rl:position] [offset=,width=,height=,hoffset=,voffset=]
  {}

\appendtoks
  \doglobal\eraseXMLelement{rl:l:manipulation}%
  \doglobal\eraseXMLelement{rl:g:manipulation}%
\to \figurebaseresets

\appendtoks
  \startnointerference
    \processXMLelement{rl:l:manipulation}%
    \processXMLelement{rl:g:manipulation}%
  \stopnointerference
\to \externalfigurepostprocessors

\protect \doifnotmode{demo}{\endinput}

\starttext

\setupcolors[state=start]

\usefigurebase[d-fig-01]

\externalfigure[part of a dutch cow][width=3cm,frame=on]
\externalfigure[a simple dutch cow][width=5cm,frame=on]
\externalfigure[another simple dutch cow][width=5cm,frame=on]

\stoptext
