summaryrefslogtreecommitdiff
path: root/graphics/asymptote/doc/ocg.sty
blob: 87da828a8d22df77f326a14f86397b5a92c8bf7f (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
%% Copyright (C) 2007 by Michael Ritzert <michael.ritz...@gmail.com>
%% Spurious spaces removed by John Bowman [2009/06/01].
%% Global macros to find the number of a PDF OCG object from its LaTeX
%% reference contributed by Paul Gaborit [2012/09/13].

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{ocg}[2012/09/13]

\RequirePackage{ifpdf}
\ifpdf
\else
   \PackageWarningNoLine{ocg}{%
      Loading aborted, because pdfTeX is not running in PDF mode%
   }%
   \expandafter\endinput
\fi

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

%testing for correct pdfTeX version
%TODO: find out minimum required version!
\ifnum\pdftexversion<120
   \PackageError{ocg}{%
      pdfeTeX, version >= 1.20, required%
   }{%
      Install a newer version!%
   }%
\fi

% Next OCG id -- TODO: autogenerate. but keep possibility to reopen an OCG.
\newcount\@ocg@num\@ocg@num=0

\gdef\@ocg@layersnames{}

% called from the aux file
\def\@ocg@makeknown#1#2#3{% #1: OCG name, #2: OC id, #3: on/off
   \@ifundefined{OCG#2}{%
      \message{OCG#2}
      \expandafter\gdef\csname OCG#2\endcsname{#1}%
      \immediate\pdfobj{<< /Type /OCG /Name (#1) >>}% new ocg
      \xdef\@ocg@curocg{\the\pdflastobj\space 0 R}% reference to id
      \expandafter\xdef\csname OCGpdfobj#2\endcsname{\@ocg@curocg}
      \xdef\@ocg@ocgs{\@ocg@ocgs\space\@ocg@curocg}% list of all OCGs in "first defined" order
      \ifnum#3=1 %on
         \xdef\@ocg@ocgson{\@ocg@ocgson\space\@ocg@curocg}% list of all default-on OCGs
      \else%
         \xdef\@ocg@ocgsoff{\@ocg@ocgsoff\space\@ocg@curocg}% list of all default-off OCGs
      \fi%
      \xdef\@ocg@layersnames{%
         \@ocg@layersnames\space/OC#2\space\@ocg@curocg% name-to-id mapping
      }%
   }{%
      \message{OCG#2 reopened}
      % layer reopened
   }
}

\AtBeginDocument{%
   % the auxfile has been read if available. register the OCGs in the page resources.
   \@ocg@addresource
   \let\@ocg@makeknown\@gobble
}

% set page resources to include the layers defined in the aux file
\def\@ocg@addresource{%
   \immediate\pdfobj{<<\@ocg@layersnames\space>>}%
   \xdef\@ocg@namesobj{\the\pdflastobj\space 0 R}%
   % append to pageresources
   \begingroup
   \edef\x{\endgroup
      \pdfpageresources{%
         \the\pdfpageresources
         /Properties \@ocg@namesobj%
      }%
   }%
   \x
}

\newcount\@ocg@@ocgs
\pdfobj reserveobjnum
\@ocg@@ocgs=\pdflastobj

\newcount\@ocg@@layersconfig
\pdfobj reserveobjnum
\@ocg@@layersconfig=\pdflastobj

\pdfcatalog{%
   /OCProperties <<
      /OCGs \the\@ocg@@ocgs\space0 R\space
      /D \the\@ocg@@layersconfig\space0 R\space
   >>%
}
\def\@ocg@ocgs{}
\def\@ocg@ocgson{}
\def\@ocg@ocgsoff{}
\AtEndDocument{%
   \immediate\pdfobj useobjnum \@ocg@@ocgs {%
      [\@ocg@ocgs\space]%
   }%
   \immediate\pdfobj useobjnum \@ocg@@layersconfig {%
      <<
         /Order [\@ocg@ocgs\space]
         /ON    [\@ocg@ocgson\space]
         /OFF   [\@ocg@ocgsoff\space]
      >>%
   }%
}%

% schedule a OCG for creation on the next pdflatex run (via the auxfile)
\def\@ocg@newocg#1#2#3{% #1:name, #2:num, #3:on
    \if@filesw%
      \immediate\write\@auxout{%
        \string\@ocg@makeknown{#1}{#2}{#3}%
      }%
    \fi%
}

% TODO: Are nested OCGs allowed?
\newenvironment{ocg}[3]{%
   \@ocg@newocg{#1}{#2}{#3}%
   \gdef\@ocg@curnum{#2}%
   \pdfliteral{/OC /OC\@ocg@curnum\space BDC}%
   \message{/OC\@ocg@curnum}%
   \ignorespaces
}{%
   \pdfliteral{EMC}%
   %\unskip%
   %\endgroup%
   \ignorespacesafterend
}