%D \module
%D   [       file=enco-pfr,
%D        version=2000.12.10,
%D          title=\CONTEXT\ Encoding Macros,
%D       subtitle=PDF Font Resource Inclusion,
%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.

%D This is an experimental module in which we implement
%D font resource inclusion in \PDF. One reason to include
%D font resources is that it enables a search engine to
%D perform a search (I'm told). This feature ws requested by 
%D Petr Ferdus from Czech.  

%D A simple test file may look like this (watch how we first
%D load the encoding and then the font; previous font 
%D definitions are left untouched.) 
%D 
%D \starttypen
%D % output=pdftex interface=en 
%D 
%D \useencoding[pfr]
%D \setupbodyfont[csr]
%D 
%D \starttext 
%D   test \`z \'z \bf test \sl test \bs quite funny \`z \page
%D   test \`z \'z \bf test \sl test \bs quite funny \`z \page
%D \stoptext 
%D \stoptypen 
%D
%D We do our best to include a (often large) font resources 
%D only once. The current implementation is not that 
%D general which is also due to the fact that \type 
%D {\pdffontattr} is expanded instantly and persistent. A 
%D more versatile (but also slower) approach is to keep track 
%D of the fonts and either flush the information at shipout 
%D time, or at the end of the document. 

\unprotect

%D \macros 
%D   {ifincludepdffontresources}
%D
%D You can turn of this feature using the following switch. 

\newif\ifincludepdffontresources \includepdffontresourcestrue

%D The name of the resource is stored in a macro, as is its
%D object reference. A resource is only processed once. When
%D done, the resource name is erased, and we use this fact to
%D prevent redefinition as well as well as reloading. So, a
%D macro defined with \type {\pdffontfileresource} can have
%D three states: 
%D 
%D \startopsomming[opelkaar]
%D \som undefined: not yet loaded, and not yet included
%D \som some value: loaded, but not yet included
%D \som empty: loaded, and already included
%D \stopopsomming

\def\pdffontresource%
  {\strippedcsname\pdffontresource\currentencoding}

\def\pdffontfileresource%
  {pdfr:\pdffontresource}

%D A resource is defined in a file prefixed by \type {pdfr-}.
%D The following \PDF\ code is compsed by Ondrej Koala Vacha (i
%D probably mispelled this name). 
%D 
%D \starttypen
%D \startpdffontresource[il2] 
%D /CIDInit /ProcSet findresource begin
%D 12 dict begin
%D   begincmap
%D     /CIDSystemInfo
%D       << /Registry (Adobe) 
%D          /Ordering (T1UV)  
%D          /Supplement 0 
%D       >> def
%D     /CMapName /Adobe-Identity-UCS def
%D     /CMapType 1 def
%D     1 begincodespacerange
%D       <00> <FF>
%D     endcodespacerange
%D     %%FontSpecificEncoding
%D     191 beginbfrange
%D       <20> <20> <0020> % space         dec: 32 oct:040 hex:20
%D       .... .... ...... . ........      .... .. ....... ......
%D       <ff> <ff> <00ff> % dotaccent     dec:255 oct:377 hex:ff
%D     endbfrange
%D   endcmap
%D CMapName currentdict /CMap defineresource pop end
%D end
%D \stoppdffontresource
%D \stoptypen
%D
%D We don't preload such huge definitions, and process them 
%D run||time to save memory. Therefore, in the encoding 
%D vector, we only add an entry like:
%D 
%D \starttypen
%D \startencoding [il2]
%D   \usepdffontresource il2
%D \stopencoding
%D \stoptypen
%D
%D This macro is defined as follows. 

\def\usepdffontresource #1 %
  {\doifundefinedelse{\pdffontfileresource} % okay, undefined, so either
     {\setxvalue{\pdffontfileresource}{#1}} % band new, or not yet loaded
     {\doifvaluesomething{\pdffontfileresource}  % only if not loaded in which
        {\setxvalue{\pdffontfileresource}{#1}}}} % case it's made empty

%D Watch how we check for duplicated loading. The resource 
%D itself, when asked for, is included immediately, after which 
%D we save its reference. Normally a document will have one 
%D such a resource. 

\long\def\startpdffontresource[#1]#2\stoppdffontresource%
  {\donefalse % we use boolean due to \par
   \doifundefined{\pdffontresource}% should be \long
     {\doif{#1}{\currentencoding}{\donetrue}}%
   \ifdone
     \immediate\pdfobj stream {#2}%
     \setxvalue{\pdffontresource}{\the\pdflastobj}%
   \fi}

%D The reference to such a vector is to be handled at font 
%D definition time, which is why we hook it into the font 
%D loading routine. A little bit of indirectness speeds up 
%D the process when this feature is disabled and keeps the 
%D macros readable. 

\appendtoksonce \includepdffontresource \to \everyfont

\def\includepdffontresource%
  {\ifincludepdffontresources
     \ifx\pdffontattr\undefined
       % we're not using (a recent version of) pdftex
     \else\ifcase\pdfoutput
       % we're not in pdf mode
     \else
       \doincludepdffontresource
     \fi\fi
   \fi}

\def\doincludepdffontresource%
  {% does this font has an special encoding entry
   \doifdefined{\purefontname\font\s!encoding}
     {% does this encoding entry has a value
      \doifvaluesomething{\purefontname\font\s!encoding}
        {\bgroup
         % quick and dirty switch
         \edef\currentencoding{\getvalue{\purefontname\font\s!encoding}}% okay?
         % is there a pdf font encoding resource file defined
         \doifdefined{\pdffontfileresource}
           {% load the pdf font resource
            \doifvaluesomething{\pdffontfileresource}
              {% but load it only once
               \startreadingfile
               \readsysfile{pdfr-\getvalue{\pdffontfileresource}}{}{}% messages
               \stopreadingfile
               % but do that only once, so forget the flag, empty==loaded
               \global\letvalue{\pdffontfileresource}\empty}}%
         % is there a resource indeed, i.e. an object reference
         \doifdefined{\pdffontresource}
           {% if so, create a reference to the object
            \expanded{\pdffontattr\font % current font
              {/ToUnicode \getvalue{\pdffontresource}\space0 R}}}%
         \egroup}}}

%D For the moment, we keep this definition here, if only 
%D because \type {\usepdffontencoding} is not defined in the 
%D core. In the end, this will go to enco-il2.

\startencoding [il2]
  \usepdffontresource il2
\stopencoding

\protect \endinput
