grph-exp.mkxl /size: 5935 b    last modification: 2025-02-21 11:03
1%D \module
2%D   [       file=grph-exp,
3%D        version=2024.07.05,
4%D          title=\CONTEXT\ Graphic Macros,
5%D       subtitle=Figure Export,
6%D         author=Hans Hagen,
7%D           date=\currentdate,
8%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
9%C
10%C This module is part of the \CONTEXT\ macro||package and is
11%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
12%C details.
13
14\writestatus{loading}{ConTeXt Graphic Macros / Figure Export}
15
16\unprotect
17
18%D Assuming \type {test.tex}:
19%D
20%D \starttyping
21%D \startmode [images]
22%D      \collectexported
23%D \stopmode
24%D
25%D \usemodule[article-basic]
26%D
27%D \setupbackend
28%D   [export=yes]
29%D
30%D \setupinteraction
31%D   [title={\documentvariable{title}}]
32%D
33%D \startdocument[title=Runtime graphics]
34%D
35%D \samplefile{tufte}
36%D
37%D \starttextdisplay
38%D     \startimage
39%D         \startMPcode
40%D             fill fullcircle scaled 3cm withcolor darkred ;
41%D         \stopMPcode
42%D     \stopimage
43%D \stoptextdisplay
44%D
45%D \samplefile{tufte}
46%D
47%D \stopdocument
48%D \stoptyping
49%D
50%D This should do the job:
51%D
52%D \starttyping
53%D mtxrun test
54%D mtxrun --script epub --images test
55%D \stoptyping
56
57\newinteger\nofexportedboxes
58
59\newconditional\c_grph_exported
60
61\definesystemconstant{exported}
62
63\def\grph_exported_discard#1%
64  {\ctxlua{backends.codeinjections.discardpages(\ifcase#1\else true\fi)}}
65
66\permanent\protected\def\collectexported
67  {\grph_exported_discard\plusone
68   \c_grph_exported\conditionaltrue
69   \appendtoks
70     \grph_exported_flush
71   \to \everystoptext}
72
73\aliased\let\dotagexported\relax
74
75\appendtoks
76   \enforced\let\dotagexported\grph_exported_tag
77\to \everyenableelements
78
79\permanent\protected\def\grph_exported_tag
80  {\enforced\def \figurefileoriginal{\jobname-\s!exported-\the\nofexportedboxes.svg}%
81   \enforced\let \figurefullname    \figurefileoriginal
82   \enforced\let \figurefilepage    \!!plusone
83   \enforced\let \figurelabel       \empty
84   \enforced\edef\figurewidth       {\the\wd\b_grph_image}%
85   \enforced\edef\figureheight      {\the\ht\b_grph_image}%
86   \enforced\edef\figuredepth       {\the\dp\b_grph_image}%
87   \dotagfigure}
88
89\permanent\protected\def\grph_exported_flush
90  {\page
91   % todo block writing to the tuc file
92   \grph_exported_discard\zerocount
93   \dorecurse \nofexportedboxes {%
94     \startTEXpage
95       \dontleavehmode\directboxfromcache\s!exported{##1}%
96     \stopTEXpage}%
97   \writestatus\m!system\empty
98   \writestatus\m!system{if needed run: mtxrun --script epub --images \jobname}%
99   \writestatus\m!system\empty}
100
101\newbox\b_grph_image
102
103\permanent\protected\def\grph_start_image_tagged
104  {\begingroup
105   \global\advanceby\nofexportedboxes\plusone
106   \dostarttagged\t!image{\the\nofexportedboxes}%
107   \setbox\b_grph_image\hpack\bgroup
108   \ignorepars
109   \ignorespaces}
110
111\permanent\protected\def\grph_stop_image_tagged
112  {\removeunwantedspaces
113   \egroup
114   \dotagexported
115   \dostoptagged
116   \ifconditional\c_grph_exported
117     \setbox\scratchbox\box\b_grph_image
118     \putboxincache\s!exported{\the\nofexportedboxes}\scratchbox
119   \fi
120   \box\b_grph_image
121   \endgroup}
122
123%D We want to get compatible packaging (beware of spaces)
124
125\permanent\protected\def\grph_start_image_normal
126  {\hpack\bgroup
127   \ignorepars
128   \ignorespaces}
129
130\permanent\protected\def\grph_stop_image_normal
131  {\removeunwantedspaces
132   \egroup}
133
134\enforced\let\startimage\grph_start_image_normal
135\enforced\let\stopimage \grph_stop_image_normal
136
137\appendtoks
138    \enforced\let\startimage\grph_start_image_tagged
139    \enforced\let\stopimage \grph_stop_image_tagged
140\to \everyenableelements
141
142%D Maybe more hooks are needed. We need for instance to deal with
143%D reuse and also need to be able to bypass background graphics. It is
144%D anyway better to be explicit and just wrap in an image.
145
146% \aliased\let\meta_process_graphic_start_normal\meta_process_graphic_start
147% \aliased\let\meta_process_graphic_stop_normal \meta_process_graphic_stop
148%
149% \def\meta_process_graphic_start_tagged
150%   {\startimage
151%    \meta_process_graphic_start_normal}
152%
153% \def\meta_process_graphic_stop_tagged
154%   {\meta_process_graphic_stop_normal
155%    \stopimage}
156%
157% \appendtoks
158%     \let\meta_process_graphic_start\meta_process_graphic_start_tagged
159%     \let\meta_process_graphic_stop \meta_process_graphic_stop_tagged
160% \to \everyenableelements
161
162%D So we go for explicit wrapping plus an extra MP command:
163
164\permanent\protected\def\startMPimage#1\stopMPimage
165  {\startimage
166   \startMPcode#1\stopMPcode
167   \stopimage}
168
169\permanent\protected\lettonothing\stopMPimage
170
171% bonus
172
173\newdimension\justimageoffset
174\newdimension\justimagetopoffset
175\newdimension\justimagebottomoffset
176\newdimension\justimageleftoffset
177\newdimension\justimagerightoffset
178
179\permanent\protected\def\grph_start_image_only
180  {\begingroup
181   \setbox\b_grph_image\hpack\bgroup
182   \ignorepars
183   \ignorespaces}
184
185\permanent\protected\def\grph_stop_image_only
186  {\removeunwantedspaces
187   \egroup
188   \global\advanceby\nofexportedboxes\plusone
189   \ifzeropt{%
190          \justimageoffset
191         +\justimageleftoffset
192         +\justimagerightoffset
193         +\justimagetopoffset
194         +\justimagebottomoffset
195       }%
196     \setbox\scratchbox\box\b_grph_image
197   \else
198     \setbox\scratchbox\hpack\bgroup
199       \kern{\justimageoffset+\justimageleftoffset}%
200       \box\b_grph_image
201       \kern{\justimageoffset+\justimagerightoffset}%
202     \egroup
203     \ht\scratchbox{\ht\scratchbox+\justimageoffset+\justimagetopoffset}%
204     \dp\scratchbox{\dp\scratchbox+\justimageoffset+\justimagebottomoffset}%
205   \fi
206   \putboxincache\s!exported{\the\nofexportedboxes}\scratchbox
207   \box\b_grph_image
208   \endgroup}
209
210\permanent\protected\def\justimages
211  {\exportingtrue
212   \ctxlua{backends.codeinjections.discardpages(true)}%
213   \appendtoks
214     \grph_exported_flush
215   \to \everystoptext
216   \enforced\let\startimage\grph_start_image_only
217   \enforced\let\stopimage \grph_stop_image_only}
218
219\protect \endinput
220