summaryrefslogtreecommitdiff
path: root/tex/context/base/mkiv/pack-obj.mkiv
blob: d08c664944fa72240b3e9ab493e0dff3343fa8ee (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
%D \module
%D   [       file=pack-obj,
%D        version=1998.01.15,
%D          title=\CONTEXT\ Packaging Macros,
%D       subtitle=Objects,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
%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 Packaging Macros / Objects}

\unprotect

\registerctxluafile{pack-obj}{}

%D \macros
%D   {setobject,getobject,ifinobject}
%D
%D Boxes can be considered reuable objects. Traditionally once passed
%D to the \DVI\ file, such objects cannot be reused. In \PDF\ however,
%D reusing is possible and sometimes even a necessity. Therefore,
%D \CONTEXT\ supports reusable objects and \LUATEX\ has native support
%D for so called box resources.
%D
%D The first application of objects in \CONTEXT\ concerned \METAPOST\
%D graphics and fill||in form fields. Reusing resources can save lots
%D of bytes and sometimes also runtime.
%D
%D \starttyping
%D \setobject{class}{name}\somebox{}
%D \getobject{class}{name}
%D \stoptyping
%D
%D Here \type {\somebox} can be whatever box specification suits \TEX.
%D Although the implementation in \MKIV\ is somewhat different the
%D principles have not changed.

\installcorenamespace {objects}

\newif   \ifinobject
\newbox  \objectbox

\def\objectoffset{1cm}

\newdimen\objectoff  \def\objectmargin{\the\objectoff}
\newdimen\objectwd   \def\objectwidth {\the\objectwd}
\newdimen\objectht   \def\objectheight{\the\objectht}
\newdimen\objectdp   \def\objectdepth {\the\objectdp}

%D If I have time I will use the unreferenced variant for e.g. mp reuse. This can be
%D rewritten in \LUA\ anyway.

\unexpanded\def\setreferencedobject       #1#2{\begingroup\synctexpause\objectoff\objectoffset\inobjecttrue\dowithnextbox{\pack_objects_set_yes{#1}{#2}}}
\unexpanded\def\settightreferencedobject  #1#2{\begingroup\synctexpause\objectoff\zeropoint   \inobjecttrue\dowithnextbox{\pack_objects_set_yes{#1}{#2}}}
\unexpanded\def\setunreferencedobject     #1#2{\begingroup\synctexpause\objectoff\objectoffset\inobjecttrue\dowithnextbox{\pack_objects_set_nop{#1}{#2}}}
\unexpanded\def\settightunreferencedobject#1#2{\begingroup\synctexpause\objectoff\zeropoint   \inobjecttrue\dowithnextbox{\pack_objects_set_nop{#1}{#2}}}

\let\setobject     \setreferencedobject
\let\settightobject\settightreferencedobject

%D We can get subtle differences in boundingboxes but both methods work ok.

\newconstant\c_pack_objects_offset_mode % 0=tex 1=box

\unexpanded\def\pack_objects_set_yes#1#2%
  {\ifcase\c_pack_objects_offset_mode
     \ifzeropt\objectoff
       \pack_objects_package_nop\nextbox
     \else
       \pack_objects_package_yes\nextbox
     \fi
   \else
     \pack_objects_package_nop\nextbox
   \fi
   \clf_registerreferencedobject{#1}{#2}\objectbox\objectoff\c_pack_objects_offset_mode
   \synctexresume
   \endgroup}

\unexpanded\def\pack_objects_set_nop#1#2%
  {\ifcase\c_pack_objects_offset_mode
     \ifzeropt\objectoff
       \pack_objects_package_nop\nextbox
     \else
       \pack_objects_package_yes\nextbox
     \fi
   \else
     \pack_objects_package_nop\nextbox
   \fi
   \clf_registerobject{#1}{#2}\objectbox\objectoff\c_pack_objects_offset_mode
   \synctexresume
   \endgroup}

\def\pack_objects_package_nop#1% we pack because otherwise \ruledhbox is still tight
  {\setbox\objectbox\hpack{\box#1}}

\def\pack_objects_package_yes#1%
  {\objectwd\dimexpr\wd#1+2\objectoff\relax
   \objectht\dimexpr\ht#1+ \objectoff\relax
   \objectdp\dimexpr\dp#1+ \objectoff\relax
   \setbox\objectbox\hpack
     {\hskip\objectoff
      \box#1}%
   \wd\objectbox\objectwd
   \ht\objectbox\objectht
   \dp\objectbox\objectdp}

\unexpanded\def\getobject#1#2%
  {\begingroup
   \synctexpause
   \clf_restoreobject{#1}{#2}%
   \ifzeropt\objectoff\else
     \objectoff-\objectoff
     \pack_objects_package_yes\objectbox
   \fi
   \box\objectbox
   \synctexresume
   \endgroup}

%D If needed one can ask for the dimensions of an object with:
%D
%D \starttyping
%D \getobjectdimensions{class}{name}
%D \stoptyping
%D
%D The results are reported in \type {\objectwidth}, \type {\objectheight} and \type
%D {\objectdepth} as well as \type {\objectoffset}.

\unexpanded\def\getobjectdimensions#1#2%
  {\clf_getobjectdimensions{#1}{#2}}

%D \macros
%D   {doifobjectfoundelse,doifobjectreferencefoundelse}
%D
%D To prevent redundant definition of objects, one can use the next tests:
%D
%D \starttyping
%D \doifobjectfoundelse{class}{object}{do then}{do else}
%D \doifobjectreferencefoundelse{class}{object}{do then}{do else}
%D \stoptyping

\def\defaultobjectreference{0}
\def\defaultobjectpage     {\realfolio}

\unexpanded\def\dogetobjectreference        {\clf_getobjectreference}
\unexpanded\def\dogetobjectreferencepage    {\clf_getobjectreferencepage}

\unexpanded\def\doifelseobjectfound         {\clf_doifelseobject}
\unexpanded\def\doifelseobjectreferencefound{\clf_doifelseobjectreference}

\let\doifobjectfoundelse         \doifelseobjectfound
\let\doifobjectreferencefoundelse\doifelseobjectreferencefound

%D For the moment here:

\let\lastpredefinedsymbol\empty % some day we can do more at the lua end

\unexpanded\def\predefinesymbol[#1]%
  {\begingroup
   \xdef\lastpredefinedsymbol{#1}%
   \settightobject{SYM}{#1}\hbox{\symbol[#1]}% to be checked ... maybe only fitting
   \dogetobjectreference{SYM}{#1}\m_back_object_reference
   \clf_registerbackendsymbol{#1}\m_back_object_reference\relax
   \endgroup}

\protect \endinput