summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/latex/ocg-p/ocg-p.sty
blob: 9354753ab0cae7b5128dcd9098d9dd5a0353adb2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
% Copyright (C) 2012 by Werner Moshammer
% Parts of this code are Copyright (C) 2007 by Michael Ritzert <michael.ritz...@gmail.com>
% REPLACEMENT FOR THE OLD OCG.STY
% FOR PDFLATEX AND XELATEX (the old ocg.sys works only with pdflatex)
% This file may be distributed and/or modified under the LaTeX Project Public License

\NeedsTeXFormat{LaTeX2e}
\def\ocgpversion{0.3}
\ProvidesPackage{ocg-p}[2012/12/01 v\ocgpversion\space Optional Content Group in a PDF document]

\RequirePackage{eso-pic}
\RequirePackage{ifpdf}
\RequirePackage{ifxetex}

\DeclareOption*{}\ProcessOptions*\relax %allow anything as option for the moment


\newif\if@ocgp@ifps

\ifpdf
  \ifnum\pdftexversion<120
    \PackageError{ocg-p}{%
      pdfeTeX, version >= 1.20, required%
    }{%
       Install a newer version!%
    }%
  \fi
\else
  \ifxetex
     %already ok 
  \else
    % dvips
    \@ocgp@ifpstrue
    \PackageWarningNoLine{ocg-p}{%
      Only xelatex and pdflatex are supported%
    }%
  \fi
\fi

\def\@ocgp@ocgHandle{\@auxout}

\newif\if@ocgp@isnestedB\@ocgp@isnestedBfalse % nested OCG begin
\newif\if@ocgp@isnestedE\@ocgp@isnestedEfalse % nested OCG end

\def\@ocgp@nestedB{%
  \xdef\@ocgp@ocgorderlist{\@ocgp@ocgorderlist\space[}
}
\def\@ocgp@nestedE{%
  \xdef\@ocgp@ocgorderlist{\@ocgp@ocgorderlist\space]}
}


\providecommand\ocg[3]{} % if running with ocgtools
\renewenvironment{ocg}[4][]{%
  \if@ocgp@isnestedB% begin of nested ocg detected
		\immediate\write\@ocgp@ocgHandle{%
      \string\@ocgp@nestedB{}%
    }
	\fi
	\global\@ocgp@isnestedBtrue % ocg begin
	\global\@ocgp@isnestedEfalse % ocg end
	
  \if@filesw%
    \immediate\write\@ocgp@ocgHandle{%
      \string\@ocgp@newocg{#2}{#3}{#4}{#1}%
    }
  \fi
  \gdef\@ocgp@curnum{#3}%
  \ifpdf
    \pdfliteral{/OC /OC\@ocgp@curnum\space BDC}%
  \else
    \if@ocgp@ifps % soon (not implemented yet) POSTSCRIPT
      \special{ps: mark /OC /OC\@ocgp@curnum\space /BDC pdfmark}%
    \else
      \special{pdf: content /OC /OC\@ocgp@curnum\space BDC}%
    \fi
  \fi
  \message{/OC\@ocgp@curnum}%
  \ignorespaces
}{%
  \ifpdf
    \pdfliteral{EMC}%
  \else
    \if@ocgp@ifps % soon (not implemented yet) POSTSCRIPT
      \special{ps: mark /EMC pdfmark}%
    \else
      \special{pdf: content EMC}%
    \fi
  \fi
	\if@ocgp@isnestedE% end of nested ocg detected
    \immediate\write\@ocgp@ocgHandle{%
      \string\@ocgp@nestedE{}%
    }
  \fi
  \global\@ocgp@isnestedEtrue % ocg end
  \global\@ocgp@isnestedBfalse % ocg begin
  \ignorespacesafterend
}


\def\@ocgp@ocglist{}
\def\@ocgp@ocgofflist{}
\def\@ocgp@ocgviewlist{} % to switch ocg off in layer toolbar of the viewer
\gdef\@ocgp@ocgmaplist{}
\def\@ocgp@ocgorderlist{} % ocgs in first-defined order + hierarchy

% FUTURE PARAMETERS
% notprintable                                 -> PrintState=OFF
% alwaysprinted                                -> Printstate=ON
% notinlayertoolbar (visiblity not changeable) -> not listed in /Order
% notexportable                                -> ExportState=OFF
% alwaysexported                               -> Exportstate=ON
% visible (same as bool-argument visible)      -> ViewState=ON
% not visible  (same as bool-argument visible) -> ViewState=ON

% ->
% not visible AND alwaysprinted                -> only on print

\newcount\@ocgp@num\@ocgp@num=0

\def\@ocgp@newocg#1#2#3#4{% #1: OCG name, #2: OC id num, #3: visiblity on/off   
  \expandafter\ifx\csname OCG#2\endcsname\relax
    \expandafter\gdef\csname OCG#2\endcsname{#1}%
    \global\advance\@ocgp@num by 1
    \ifpdf% PDFLATEX
      \immediate\pdfobj{<< /Type /OCG /Name (#1) /Usage <<%
          %/Print<</PrintState /ON>> %
          %/View<</ViewState /OFF>> %
          % /Export<</ExportState /OFF>>%
        >> >>}% new ocg
      \xdef\@ocgp@curocg{\the\pdflastobj\space 0 R}% reference to current ocg id
    \else
      \if@ocgp@ifps % soon (not implemented yet) POSTSCRIPT
        \xdef\@ocgp@curocg{{@ocg\the\@ocgp@num}}% reference to current ocg id
        \special{ps: mark /_objdef \@ocgp@curocg /type/dict /OBJ pdfmark}  
        \special{ps: mark \@ocgp@curocg << /Type /OCG /Name (#1)
          >> /PUT pdfmark}% new ocg
      \else % XELATEX
        \xdef\@ocgp@curocg{@ocg\the\@ocgp@num}% reference to current ocg id
        \special{pdf:obj \@ocgp@curocg\space <</Type/OCG /Name (#1) /Usage <<
            %/Print<</PrintState /ON>> %
            %/View<</ViewState /OFF>> %
            %/Export<</ExportState /OFF>>%
          >> >>}% new ocg
      \fi
    \fi  
    \expandafter\xdef\csname OCGpdfobj#2\endcsname{\@ocgp@curocg} % for ogcx-package
    \xdef\@ocgp@ocglist{\@ocgp@ocglist\space\@ocgp@curocg}% list of all OCGs in "first defined" order  
		\xdef\@ocgp@ocgorderlist{\@ocgp@ocgorderlist\space\@ocgp@curocg} % all OCGs in "first defined" order + hierarchy
    \xdef\@ocgp@ocgmaplist{\@ocgp@ocgmaplist\space/OC#2\space\@ocgp@curocg\space ^^J} % name-to-id mapping
    \ifnum#3=1 %on
      % no list of all default-on OCGs needed, because of basestate on
    \else%
      \xdef\@ocgp@ocgofflist{\@ocgp@ocgofflist\space\@ocgp@curocg}% list of all default-off OCGs
    \fi%
  \else
    % layer reopened
  \fi
}


 \AtEndDocument{%
   %\message{... \@ocgp@ocgorderlist ...}
   \ifpdf % PDFLATEX
     \pdfcatalog{%
       /OCProperties <<
         /OCGs [\@ocgp@ocglist]
         /D <</BaseState/ON /Order [\@ocgp@ocgorderlist] /OFF [\@ocgp@ocgofflist] /AS [%
           <</Event/View /OCGs [\@ocgp@ocglist] /Category[/View]>>%
           <</Event/Print /OCGs [\@ocgp@ocglist] /Category[/Print]>>%
           <</Event/Export /OCGs [\@ocgp@ocglist] /Category[/Export]>>%
         ]>>%
       >>%
     }
   \else 
     \if@ocgp@ifps % soon (not implemented yet) POSTSCRIPT
       %\special{ps: mark {Catalog} <<
       %  /OCProperties <<
       %    /OCGs [\@ocgp@ocglist]
       %    /D <</BaseState/ON /Order [\@ocgp@ocgorderlist] /OFF [\@ocgp@ocgofflist]>>
       %  >>
       %>> /PUT pdfmark}%
		 \else % XELATEX
       \special{pdf:put @catalog <<
         /OCProperties <<
           /OCGs [\@ocgp@ocglist]
           /D <</BaseState/ON /Order [\@ocgp@ocgorderlist] /OFF [\@ocgp@ocgofflist] /AS [% 
             <</Event/View /OCGs [\@ocgp@ocglist] /Category[/View]>>%
             <</Event/Print /OCGs [\@ocgp@ocglist] /Category[/Print]>>%
             <</Event/Export /OCGs [\@ocgp@ocglist] /Category[/Export]>>%
           ]>>%
         >>%
       >>}%
     \fi
   \fi   
 }

\AtBeginDocument{%
  \ifpdf % PDFLATEX
    \immediate\pdfobj{<<\@ocgp@ocgmaplist\space>>}%
     \xdef\@ocgp@namesobj{\the\pdflastobj\space 0 R}%
     % append to pageresources
     \begingroup
     \edef\x{\endgroup
        \pdfpageresources{%
           \the\pdfpageresources
           /Properties \@ocgp@namesobj%
        }%
     }%
     \x
  \else
    \if@ocgp@ifps % soon (not implemented yet) POSTSCRIPT
      %\AddToShipoutPicture{
        %\special{ps: mark {Resources} <<
        %  /Properties << ^^J%
        %  \@ocgp@ocgmaplist
        %>>
        %>> /PUT pdfmark}}%
    \else  % XELATEX
      \AddToShipoutPicture{
        \special{pdf: put @resources <<
          /Properties << ^^J%
          \@ocgp@ocgmaplist
        >>
        >>}}%
    \fi
  \fi
}

% parsing a space-delimited ocgid-list to a space-delimited list of ocg-objects
\makeatletter
\def\@ocgp@parseSpaceSeperatedList#1{ \@ocgp@doparseSpaceSeperatedList#1 \@empty }
\def\@ocgp@doparseSpaceSeperatedList#1 #2{%
  \ifcsname OCGpdfobj#1\endcsname
    \xdef\@ocgp@ocgobjlist{\@ocgp@ocgobjlist\space\csname OCGpdfobj#1\endcsname}
  \fi
  \ifx #2\@empty
    %\@ocgp@ocgobjlist % only for debugging reasons
  \else
    \expandafter\@ocgp@doparseSpaceSeperatedList
  \fi
  #2%
}
\makeatother

% link to toggle a layer in a document without using the layer toolbar of the viewer
\makeatletter
\long\def\toggleocg#1#2{%
  \xdef\@ocgp@ocgobjlist{}%
  \@ocgp@parseSpaceSeperatedList{#1}
  \leavevmode%
  \ifpdf
    \pdfstartlink user {%
      /Subtype /Link
      /A << /S/SetOCGState /State [/Toggle \@ocgp@ocgobjlist] >>
      %/A << /Type /Action /S /GoTo /D [1 0 R] >>
      /Border [0 0 0]% no border
    }%
    #2\pdfendlink%
  \else
    \special{pdf: bann
      << /Type /Annot
        /Subtype /Link
        /Border [0 0 0]% no border
        /A <</S/SetOCGState /State [/Toggle \@ocgp@ocgobjlist] >>%
      >>}#2% 
    \special{pdf:eann}%
  \fi
}
\makeatother