grph-inc.mkxl /size: 36 Kb    last modification: 2021-10-28 13:51
1%D \module
2%D   [       file=grph-inc, % moved from core-fig
3%D        version=2006.08.26, % overhaul of 1997.03.31
4%D          title=\CONTEXT\ Graphic Macros,
5%D       subtitle=Figure Inclusion,
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% \enabledirectives[graphics.conversion.eps.cleanup.ai]
15
16% \setupexternalfigures[directory=dirfile://./test/**]
17% \externalfigure[crappname(2).eps][frame=on]
18
19% todo: messages
20
21\writestatus{loading}{ConTeXt Graphic Macros / Figure Inclusion}
22
23\registerctxluafile{grph-img}{}
24\registerctxluafile{grph-inc}{autosuffix}
25\registerctxluafile{grph-bmp}{}
26\registerctxluafile{grph-chk}{}
27\registerctxluafile{grph-con}{}
28\registerctxluafile{grph-fil}{}
29%registerctxluafile{grph-mem}{} % obsolete
30\registerctxluafile{grph-u3d}{} % this will become a module
31%registerctxluafile{grph-swf}{} % this will become a module
32
33\unprotect
34
35%D Including graphics is complicated by the fact that  we need to locate them first,
36%D optionally manipulate them and scale then next. Lookups are to be done as efficient
37%D as possible and inclusion of the data might happens only once. In \MKIV\ much of this
38%D is delegated to the \LUA\ end. There is not so much less code as in \MKII\ but it's
39%D more powerful, flexible, pluggable and some of the extended functionality has been
40%D moved from modules to the core. The overall functionality is rather stable and has
41%D not changed much over the years.
42
43\ifdefined\dotagfigure \else \aliased\let\dotagfigure\relax \fi
44
45\installcorenamespace{externalfigure}
46\installcorenamespace{externalfigureinstance}
47\installcorenamespace{externalfigurecollection}
48
49\installframedcommandhandler \??externalfigure {externalfigure} \??externalfigure
50
51\aliased\let\setupexternalfigures\setupexternalfigure
52
53\setupexternalfigures[% we really need the defaults
54   \c!method         =,
55   \c!label          =,
56   \c!size           =,
57   \c!conversion     =,
58   \c!resolution     =,
59   \c!prefix         =,
60   \c!cache          =,
61   \c!page           =\zerocount,
62   \c!file           =,
63   \c!display        =,
64   \c!mask           =,
65   \c!preset         =\v!yes,
66   \c!split          =,
67   \c!color          =,
68   \c!arguments      =,
69   \c!symbol         =\v!no,
70   \c!controls       =\v!no,
71   \c!resources      =,
72   \c!preview        =\v!no,
73   \c!repeat         =\v!no,
74   \c!foregroundcolor=,
75   \c!interaction    =\v!none,
76   \c!hfactor        =,
77   \c!wfactor        =,
78   \c!factor         =,
79   \c!defaultwidth   =8\lineheight,
80   \c!defaultheight  =6\lineheight,
81   \c!maxwidth       =\externalfigureparameter\c!width,
82   \c!maxheight      =\externalfigureparameter\c!height,
83   \c!xscale         =,
84   \c!yscale         =,
85   \c!scale          =,
86   \c!sx             =\externalfigureparameter\c!s,
87   \c!sy             =\externalfigureparameter\c!s,
88   \c!s              =1,
89   \c!width          =,
90   \c!height         =,
91   \c!lines          =,
92   \c!grid           =,
93   \c!bodyfont       =\bodyfontsize,
94   \c!object         =\v!yes,
95   \c!corner         =\v!rectangular,
96   \c!frame          =\v!off,
97   \c!option         =,
98   \c!reset          =\v!no,
99   \c!directory      =,
100   \c!radius         =.5\bodyfontsize,
101   \c!background     =,
102   \c!splitcolor     =\s!white,
103   \c!order          =,
104   \c!equalwidth     =,
105   \c!equalheight    =,
106   \c!location       ={\v!local,\v!global},
107   \c!frames         =\v!off,
108   \c!ymax           =24,
109   \c!xmax           =,
110   \c!align          =\v!none, % New, for Tacos extremely large graphics.
111   \c!crossreference =\v!no,
112   \c!transform      =\v!auto,
113   \c!userpassword   =,
114   \c!ownerpassword  =,
115   \c!compact        =,
116   \c!cmyk           =,
117   \c!crop           =\v!yes,
118  ]
119
120%D Defining figures.
121
122\newcount\c_grph_include_nesting
123
124\newtoks \everyexternalfigureresets % for the moment still public
125\newtoks \everyexternalfigurechecks % for the moment still public
126
127% \useexternalfigure[alpha][cow]
128% \useexternalfigure[beta] [cow]       [width=1cm]
129% \useexternalfigure[gamma][cow][alpha]
130% \useexternalfigure[delta][cow][alpha][width=2cm]
131%
132% full width : \externalfigure[cow]               \par
133% 3cm width  : \externalfigure[cow]  [width=3cm]  \par
134% full width : \externalfigure[alpha]             \par
135% 1cm width  : \externalfigure[beta]              \par
136% full width : \externalfigure[gamma]             \par
137% 2cm width  : \externalfigure[delta]             \par
138% 4cm width  : \externalfigure[beta] [width=4cm]  \par
139% 5cm width  : \externalfigure[gamma][width=5cm]  \par
140%
141% \defineexternalfigure[a][width=10cm]
142% \defineexternalfigure[b][width=5cm]
143% \externalfigure[cow][a]
144% \externalfigure[cow][b][height=8cm]
145%
146% \useexternalfigure[x][cow][width=10cm,height=1cm]
147% \externalfigure[x]
148% \externalfigure[x][width=3cm]
149%
150% [label] [filename]
151% [label] [filename] [parent]
152% [label] [filename] [parent] [settings]
153% [label] [filename]          [settings]
154%
155% new: more convenient/efficient than
156%
157%   \use..[a][a][setting] \externalfigure[b][a]
158%
159% is equivalent to:
160%
161%   \def..[a][setting]    \externalfigure[b][a]
162%
163% see x-res modules for usage:
164%
165% \defineexternalfigure[name][settings]
166
167%D Defining is persistent, i.e.\ when you redefine an instance, the already set
168%D parameters need to be set again or otherwise the old values will be used.
169%D
170%D New: \type {method=auto}: strips suffix and uses \quote {order} which is handy in
171%D some of four workflows where sources are used for web and print and where the web
172%D tools need a suffix (like gif) which we don't want as we want a high quality
173%D format.
174
175\newconditional\c_grph_include_trace_inheritance
176
177\installtextracker
178  {graphics.inheritance}
179  {\settrue \c_grph_include_trace_inheritance}
180  {\setfalse\c_grph_include_trace_inheritance}
181
182\installcorenamespace{externalfiguredefinition}
183
184\aliased\let\defineexternalfigures\defineexternalfigure
185
186% label file parent settings
187% label file settings
188% label file parent
189
190\permanent\tolerant\protected\def\useexternalfigure[#1]#*[#2]#*[#3]#*[#4]%
191  {\iftok{#1}\emptytoks
192     \iftok{#2}\emptytoks\else
193       \ifhastok={#3}%
194         \grph_include_use_indeed{#2}{#2}{#3}{#4}%
195       \else
196         \grph_include_use_indeed{#2}{#2}\empty{#4}%
197       \fi
198     \fi
199   \orelse\iftok{#2}\emptytoks
200     \ifhastok={#3}%
201       \grph_include_use_indeed{#1}{#1}\empty{#3}%
202     \else
203       \grph_include_use_indeed{#1}{#1}{#3}{#4}%
204     \fi
205   \else
206     \ifhastok={#3}%
207       \grph_include_use_indeed{#1}{#2}\empty{#3}
208     \else
209       \grph_include_use_indeed{#1}{#2}{#3}{#4}%
210     \fi
211   \fi}
212
213\def\grph_include_use_indeed#1#2#3#4%
214  {\dodoglobal\setvalue{\??externalfigureinstance#1}{\grph_include_setup{#2}{#3}{#4}}%
215   \grph_include_analyze_collection[#2][#4]}
216
217% inclusion
218
219\permanent\tolerant\protected\def\externalfigure[#1]#,[#2]#,[#3]%
220  {\ifhastok={#2}%
221     \grph_include_place[#1][][#2]%
222   \else
223     \grph_include_place[#1][#2][#3]%
224   \fi}
225
226% todo: chain them
227
228\def\grph_include_setup#1#2#3% name parent settings
229  {\edef\m_grph_include_name  {#1}%
230   \edef\m_grph_include_parent{#2}%
231   \ifempty\m_grph_include_name \else
232     \let\p_grph_include_name\m_grph_include_name
233   \fi
234   \ifempty\m_grph_include_parent \else
235     \grph_include_inherit_from_parent\m_grph_include_parent
236   \fi
237   \setupcurrentexternalfigure[#3]}
238
239\def\grph_include_inherit_from_parent#1%
240  {\ifcsname\??externalfigure#1:\s!parent\endcsname
241     \let\currentexternalfigure#1%
242   \fi
243   \ifcsname\??externalfigureinstance#1\endcsname
244     \ifconditional\c_grph_include_trace_inheritance\writestatus\m!figures{inheriting from instance: #1}\fi
245     \csname\??externalfigureinstance#1\endcsname
246   \fi}
247
248\newtoks\t_grph_include_local_settings
249
250\appendtoks
251   \enforced\let\textunderscore\letterunderscore % {\string _} % space needed as _ is now letter in unprotected mode (probably no longer needed)
252   %
253   \dontcomplain
254   \restorecatcodes
255   \forgetall
256\to \t_grph_include_local_settings
257
258\def\grph_include_place_inherit
259  {\ifconditional\c_grph_include_trace_inheritance
260     \writestatus\m!figures{label: \p_grph_include_label, name: \p_grph_include_name, parent: \p_grph_include_parent}%
261   \fi
262   \ifempty\p_grph_include_parent
263     % nothing to be done
264   \orelse\ifx\p_grph_include_parent\p_grph_include_label
265     % redundant
266   \else
267     \grph_include_inherit_from_parent\p_grph_include_parent
268   \fi
269   \ifempty\p_grph_include_label
270     % nothing to be done
271   \else
272     \grph_include_inherit_from_parent\p_grph_include_label
273   \fi}
274
275\def\grph_include_place[#1][#2][#3]% [label][file][settings] | [file][settings] | [file][parent][settings]
276  {\bgroup
277   \advance\c_grph_include_nesting\plusone
278   \edef\currentexternalfigure{\the\c_grph_include_nesting}%
279   \checkexternalfigureparent % each inherits from the root
280   %
281   \the\everyexternalfigureresets
282   %
283   \edef\p_grph_include_label{#1}%
284   \let\p_grph_include_name\p_grph_include_label
285   \ifhastok={#2}%
286     % [label] [settings]
287     \let\p_grph_include_parent\p_grph_include_label
288     \grph_include_place_inherit
289     \setupcurrentexternalfigure[#2]%
290   \else
291     % [label] [parent] [settings]
292     \edef\p_grph_include_parent{#2}%
293     \ifempty\p_grph_include_parent
294        \let\p_grph_include_parent\p_grph_include_label
295     \fi
296     \grph_include_place_inherit
297     \setupcurrentexternalfigure[#3]%
298   \fi
299   %
300   \the\everyexternalfigurechecks
301   %
302   \the\t_grph_include_local_settings
303   %
304   \edef\p_width    {\externalfigureparameter\c!width}%
305   \edef\p_height   {\externalfigureparameter\c!height}%
306   \edef\p_label    {\externalfigureparameter\c!label}%
307   \edef\p_reference{\externalfigureparameter\c!reference}%
308   %
309   \dostarttagged\t!image\empty
310   \clf_figure_push {
311        name         {\p_grph_include_name}%
312        label        {\ifempty\p_label\p_grph_include_label\else\p_label\fi}%
313        page         {\externalfigureparameter\c!page}%
314        file         {\externalfigureparameter\c!file}%
315        size         {\externalfigureparameter\c!size}%
316        object       {\externalfigureparameter\c!object}%
317        prefix       {\externalfigureparameter\c!prefix}%
318        cache        {\externalfigureparameter\c!cache}%
319        format       {\externalfigureparameter\c!method}%
320        preset       {\externalfigureparameter\c!prefix}%
321        controls     {\externalfigureparameter\c!controls}%
322        resources    {\externalfigureparameter\c!resources}%
323        preview      {\externalfigureparameter\c!preview}%
324        display      {\externalfigureparameter\c!display}%
325        mask         {\externalfigureparameter\c!mask}%
326        conversion   {\externalfigureparameter\c!conversion}%
327        resolution   {\externalfigureparameter\c!resolution}%
328        color        {\externalfigureparameter\c!color}% unprocessed raw key
329        cmyk         {\externalfigureparameter\c!cmyk}% kind of special
330        arguments    {\externalfigureparameter\c!arguments}% used for converters
331        repeat       {\externalfigureparameter\c!repeat}%
332        transform    {\externalfigureparameter\c!transform}%
333        compact      {\externalfigureparameter\c!compact}% experiment, share fonts
334        userpassword {\externalfigureparameter\c!userpassword}%
335        ownerpassword{\externalfigureparameter\c!ownerpassword}%
336        crop         {\externalfigureparameter\c!crop}%
337    \ifempty\p_width \else
338        width  \dimexpr\p_width\relax
339    \fi
340    \ifempty\p_height \else
341        height \dimexpr\p_height\relax
342    \fi
343   }%\relax
344   \clf_figure_identify
345   \relax
346   \ifconditional\c_grph_include_test_only
347      \ifcase\figurestatus \else
348        \clf_figure_check
349        \clf_figure_dummy
350        \clf_figure_scale
351        \clf_figure_done
352      \fi
353      \grph_include_set_mode
354   \else
355      \ifcase\figurestatus
356        \clf_figure_dummy
357        \clf_figure_scale
358      \else
359        \clf_figure_check
360        \clf_figure_include
361        \clf_figure_scale
362      \fi
363      \clf_figure_done
364      \grph_include_set_mode
365      \grph_include_finalize
366   \fi
367   \clf_figure_pop
368   \dotagfigure
369   %
370   \scratchcounter\zerocount
371   \edef\p_crossreference{\externalfigureparameter\c!crossreference}%
372   \iflocation\iftrialtypesetting\else
373     \ifempty\p_crossreference
374        % nothing
375     \orelse\ifx\p_crossreference\v!no
376        % nothing
377     \orelse\ifx\p_crossreference\v!yes
378       \ifhastok={#2}%
379         \scratchcounter\clf_figure_register_page{#1}{}{#2}\relax
380       \else
381         \scratchcounter\clf_figure_register_page{#1}{#2}{#3}\relax
382       \fi
383     \else
384       \scratchcounter-\p_crossreference % passed by repeater
385     \fi
386   \fi\fi
387   \naturalvpack
388     attr \imageattribute \plustwo
389   \ifnum\scratchcounter>\zerocount
390     {\strc_references_cross_forward {ex:\number \scratchcounter}{\box\foundexternalfigure}}%
391   \orelse\ifnum\scratchcounter<\zerocount
392     {\strc_references_cross_backward{ex:\number-\scratchcounter}{\box\foundexternalfigure}}%
393   \else
394     {\box\foundexternalfigure}%
395   \fi
396   %
397 % \naturalvpack attr \imageattribute \plustwo \bgroup
398 %   \ifcmpnum\scratchcounter\zerocount
399 %     \strc_references_cross_backward{ex:\number-\scratchcounter}{\box\foundexternalfigure}%
400 %   \or % equal
401 %     \box\foundexternalfigure%
402 %   \or % more
403 %     \strc_references_cross_forward {ex:\number \scratchcounter}{\box\foundexternalfigure}%
404 %   \fi
405 % \egroup
406   %
407   \dostoptagged
408   \egroup}
409
410%D Next we provide a cross referenced scale-up mechanism:
411
412\def\strc_references_cross_forward#1#2%
413  {\begingroup
414   \strc_references_set_simple_internal_reference{cross:b:#1}%
415   \naturalhpack
416      attr \destinationattribute\number\lastdestinationattribute
417      {\naturalhpack{\strc_references_flush_destination_nodes\directgotodumbbox{#2}[cross:f:#1]}}%
418   \endgroup}
419
420\def\strc_references_cross_backward#1#2%
421  {\begingroup
422   \strc_references_set_simple_internal_reference{cross:f:#1}%
423   \naturalhpack
424      attr \destinationattribute\number\lastdestinationattribute
425      {\naturalhpack{\strc_references_flush_destination_nodes\directgotodumbbox{#2}[cross:b:#1]}}%
426   \endgroup}
427
428\aliased\let\doexternalfigurerepeat\gobblefourarguments % called from lua end
429
430\permanent\tolerant\protected\def\placeregisteredexternalfigures[#1]%
431  {\page
432   \begingroup
433   \enforced\protected\def\doexternalfigurerepeat{\grph_include_place_registered_indeed{#1}}%
434   \dorecurse\clf_figure_nof_registered_pages
435    {\page
436     \clf_figure_flush_registered_pages\recurselevel
437     \page}%
438   \endgroup
439   \page}
440
441\protected\def\grph_include_place_registered_indeed#1#2#3#4#5% called from lua end
442  {\grph_include_place[#2][#3][#4,%
443     \c!hfactor=,\c!wfactor=,\c!factor=,%
444     \c!maxwidth=,\c!maxheight=,%
445     \c!xscale=,\c!yscale=,\c!scale=,%
446     \c!sx=,\c!sy=,\c!s=,%
447     \c!width=,\c!height=,%
448     \c!crossreference=#5,%
449     #1]}
450
451%D Scaling:
452
453\mutable\let\dowithfigure\relax % name might change (into a proper hook)
454
455\permanent\protected\def\doscalefigure % used at lua end
456  {\global\setbox\foundexternalfigure\vpack{\scale[\v!figure]{\dowithfigure{\box\foundexternalfigure}}}}
457
458\definescale % some day we will inherit
459  [\v!figure]
460  [\c!hfactor    =\externalfigureparameter\c!hfactor,
461   \c!wfactor    =\externalfigureparameter\c!wfactor,
462   \c!factor     =\externalfigureparameter\c!factor,
463   \c!maxwidth   =\externalfigureparameter\c!maxwidth,
464   \c!maxheight  =\externalfigureparameter\c!maxheight,
465   \c!equalwidth =\externalfigureparameter\c!equalwidth,
466   \c!equalheight=\externalfigureparameter\c!equalheight,
467   \c!xscale     =\externalfigureparameter\c!xscale,
468   \c!yscale     =\externalfigureparameter\c!yscale,
469   \c!scale      =\externalfigureparameter\c!scale,
470   \c!sx         =\externalfigureparameter\c!sx,
471   \c!sy         =\externalfigureparameter\c!sy,
472   \c!s          =\externalfigureparameter\c!s,
473   \c!width      =\externalfigureparameter\c!width,
474   \c!height     =\externalfigureparameter\c!height,
475   \c!lines      =\externalfigureparameter\c!lines]
476
477%D You can register additional suffixes with the following command:
478%D
479%D \starttyping
480%D \definegraphictypesynonym[jbig] [jb2]
481%D \definegraphictypesynonym[jbig2][jb2]
482%D \definegraphictypesynonym[jbg]  [jb2]
483%D \stoptyping
484
485\permanent\tolerant\protected\def\definegraphictypesynonym[#1]#*[#2]%
486  {\clf_registerfiguresuffix{#1}{#2}}
487
488%D Additional paths can be installed with the regular setup command. The next
489%D macro picks up the list.
490
491\permanent\protected\def\setfigurepathlist
492  {\clf_setfigurepaths{\externalfigureparameter\c!location}{\externalfigureparameter\c!directory}}
493
494%D Variables:
495
496\newbox \foundexternalfigure
497\newtoks\externalfigurepostprocessors
498
499\permanent\def\figurestatus        {\numexpr\clf_figurestatus{status}{0}\relax} % number: 0 = not found
500\permanent\def\figurewidth         {\clf_figurestatus{width}{0}sp}
501\permanent\def\figureheight        {\clf_figurestatus{height}{0}sp}
502\permanent\def\figurexscale        {\clf_figurestatus{xscale}{1}}
503\permanent\def\figureyscale        {\clf_figurestatus{yscale}{1}}
504\permanent\def\figurereference     {\clf_figurestatus{reference}{}}
505
506\permanent\def\figuresize          {\clf_figurerequest{size}{}}
507\permanent\def\figurelabel         {\clf_figurerequest{label}{}}
508\permanent\def\figurefileoriginal  {\clf_figurerequest{name}{}}
509\permanent\def\figurefilepage      {\clf_figurerequest{page}{1}}
510\permanent\def\figurefileoptions   {\clf_figurerequest{options}{}}
511\permanent\def\figurefileconversion{\clf_figurerequest{conversion}{}}
512\permanent\def\figurefileresolution{\clf_figurerequest{resolution}{}}
513\permanent\def\figurefilecolor     {\clf_figurerequest{color}{}}
514\permanent\def\figurefilearguments {\clf_figurerequest{arguments}{}}
515\permanent\def\figurefilecache     {\clf_figurerequest{cache}{}}
516\permanent\def\figurefileprefix    {\clf_figurerequest{prefix}{}}
517
518\permanent\def\figurenaturalwidth  {\clf_figureused{width}{\number\dimexpr\externalfigureparameter\c!defaultwidth\relax}sp}
519\permanent\def\figurenaturalheight {\clf_figureused{height}{\number\dimexpr\externalfigureparameter\c!defaultheight\relax}sp}
520\permanent\def\figurexresolution   {\clf_figureused{xresolution}{0}}
521\permanent\def\figureyresolution   {\clf_figureused{yresolution}{0}}
522\permanent\def\figureorientation   {\clf_figureused{orientation}{1}}
523\permanent\def\figurerotation      {\clf_figureused{rotation}{0}}
524\permanent\def\figurexsize         {\clf_figureused{xsize}{0}}
525\permanent\def\figureysize         {\clf_figureused{ysize}{0}}
526\permanent\def\figurecolordepth    {\clf_figureused{colordepth}{0}}
527\permanent\def\figuredepth         {\clf_figureused{depth}{0}}
528
529\permanent\def\figurefullname      {\clf_figureused{fullname}{}}
530\permanent\def\noffigurepages      {\clf_figureused{pages}{0}}
531
532
533% \permanent\def\figurefilepath      {\clf_figurefilepath} % public in lua
534% \permanent\def\figurefilename      {\clf_figurefilename} % public in lua
535% \permanent\def\figurefiletype      {\clf_figurefiletype} % public in lua
536
537\permanent\let\naturalfigurewidth  \figurenaturalwidth
538\permanent\let\naturalfigureheight \figurenaturalheight
539
540\aliased\let\figurescalewidth    \figurewidth
541\aliased\let\figurescaleheight   \figureheight
542\aliased\let\figurescalexscale   \figurexscale
543\aliased\let\figurescaleyscale   \figureyscale
544
545%D Abuse:
546%D
547%D \starttyping
548%D \externalfigure[rubish.pdf] \ifcase\figurestatus\relax \ctxlua{os.exit(999)} \fi
549%D \stoptyping
550
551%D Calculating:
552
553% \enabletrackers[figures.conversion]
554% \externalfigure[demo.svg]
555% \externalfigure[demo.svg][conversion=png]
556
557%D The following registers are used (if only to be downward compatible).
558
559\newconditional\c_grph_include_skip
560\newconditional\c_grph_include_test_only
561\newconditional\c_grph_include_level      \setfalse\c_grph_include_level  % true=background false=normal
562\newconditional\c_grph_include_flush      \settrue \c_grph_include_flush  % true=place      false=ignore
563
564\newsystemmode\v!figure
565
566\def\grph_include_set_mode
567  {\ifcase\figurestatus
568     \globalresetsystemmode\v!figure % todo, also: \v!resource
569   \else
570     \globalsetsystemmode  \v!figure % todo, also: \v!resource
571   \fi}
572
573\appendtoks
574    \clf_setfigurepaths
575      {\externalfigureparameter\c!location}%
576      {\externalfigureparameter\c!directory}%
577    \clf_figure_reset
578      \foundexternalfigure
579      \externalfigureparameter\c!defaultwidth
580      \externalfigureparameter\c!defaultheight
581    \relax
582\to \everyexternalfigureresets
583
584\appendtoks
585    \edef\p_option{\externalfigureparameter\c!option}%
586    \ifx\p_option\v!frame
587      \setfalse\c_grph_include_skip
588      \letexternalfigureparameter\c!frame\v!on
589    \orelse\ifx\p_option\v!empty
590      \settrue\c_grph_include_skip
591      \letexternalfigureparameter\c!frame\v!off
592    \else
593      \setfalse\c_grph_include_skip
594    \fi
595    % fake color in gray bitmaps, assumes that
596    % a transparent color is used
597    \edef\p_foregroundcolor{\externalfigureparameter\c!foregroundcolor}%
598    \ifempty\p_foregroundcolor \else
599        \setexternalfigureparameter\c!background{\v!foreground,\v!color}%
600        \letexternalfigureparameter\c!backgroundcolor\p_foregroundcolor
601    \fi
602\to \everyexternalfigurechecks
603
604%D Internal graphics are handled at the \TEX\ end:
605
606% strut=none also avoids the \noindent! otherwise we're in hmode when
607% blank happens and we get a lineskip
608
609\def\grph_include_process_tex#1%
610  {\framed
611     [\c!strut=\v!none,\c!align=\v!normal,\c!frame=\v!off,
612      \c!offset=\v!overlay,\c!width=\v!fit,\c!height=\v!fit]
613     {\blank[\v!disable]%
614      #1\endgraf
615      \removelastskip}} % disable should stay here!
616
617
618% used al lua end:
619
620\permanent\protected\def\docheckfigurebuffer  #1{\global\setbox\foundexternalfigure\vpack{\grph_include_process_tex{\getbuffer[#1]}}}
621\permanent\protected\def\docheckfiguretex     #1{\global\setbox\foundexternalfigure\vpack{\grph_include_process_tex{\input{#1}}}}
622\permanent\protected\def\docheckfigurecld     #1{\global\setbox\foundexternalfigure\vbox {\cldprocessfile{#1}}}
623\permanent\protected\def\docheckfiguremps     #1{\global\setbox\foundexternalfigure\vpack{\convertMPtoPDF{#1}11}}
624\permanent\protected\def\docheckfiguremprun #1#2{\global\setbox\foundexternalfigure\vpack{\useMPrun{#1}{#2}}}
625
626\permanent\protected\def\relocateexternalfigure
627  {\global\setbox\foundexternalfigure\vpack to \ht\foundexternalfigure\bgroup
628     %
629     % The \vss can (!) introduce 1 sp excess visible in xform which in itself
630     % is not that important but some don't like these cosmetic side effects, for
631     % instance we can get:
632     %
633     % vss   : \vbox(845.1575+0.0)x597.23125, glue set 845.15747fil, direction TLT
634     % vskip : \vbox(845.1575+0.0)x597.23125, direction TLT
635     %
636     % or
637     %
638     % 1 0 0 1 0 0.00003 cm
639     % 1 0 0 1 0 0       cm
640     %
641     % This is a known property of using glue and can even depend on the architecture
642     % (float implementation). Anyway, let's for now use a skip. Of course this can
643     % shift the issue elsewhere, as vss is used a lot elsewhere.
644     %
645   % \vss
646     \vkern\ht\foundexternalfigure
647     %
648     \ht\foundexternalfigure\zeropoint
649     \dp\foundexternalfigure\zeropoint
650     \hpack to \wd\foundexternalfigure\bgroup
651        \box\foundexternalfigure
652        \hss
653     \egroup
654   \egroup}
655
656\permanent\protected\def\startfoundexternalfigure#1#2% ht wd
657  {\global\setbox\foundexternalfigure\vbox to #2\bgroup\vss\hbox to #1\bgroup}
658
659\permanent\protected\def\stopfoundexternalfigure
660  {\hss\egroup\egroup}
661
662\permanent\protected\def\emptyfoundexternalfigure % sort of obsolete
663  {\startfoundexternalfigure{\externalfigureparameter\c!defaultwidth}{\externalfigureparameter\c!defaultheight}%
664   \stopfoundexternalfigure}
665
666% \doifmodeelse{*\v!last}
667%   {\settrue \c_grph_include_flush}
668%   {\setfalse\c_grph_include_flush}%
669
670\def\grph_include_finalize
671  {\global\setbox\foundexternalfigure\vbox
672     {\ifcase\figurestatus
673        \letexternalfigureparameter\c!frame\v!on
674      \fi
675      \ifconditional\c_grph_include_flush
676        \ifconditional\c_grph_include_level % probably background
677          \ifconditional\c_grph_include_skip
678            % nothing
679            \fakebox\foundexternalfigure
680          \orelse\ifcase\figurestatus
681            % nothing
682          \else
683            \the\externalfigurepostprocessors
684            \box\foundexternalfigure
685          \fi
686        \else
687          \iftrialtypesetting \else \feedbackexternalfigure \fi
688          \settrue\c_grph_include_level
689          \ifconditional\c_grph_include_skip
690            \ifcase\figurestatus
691              \grph_include_replacement\figurelabel\figurefileoriginal{unknown}%
692            \else
693              \grph_include_replacement\figurelabel\figurefullname{skipped}%
694            \fi
695          \orelse\ifcase\figurestatus
696            \grph_include_replacement\figurelabel\figurefileoriginal{unknown}%
697          \else
698            \the\externalfigurepostprocessors
699            \edef\p_reset{\externalfigureparameter\c!reset}%
700            \ifx\p_reset\v!yes
701              \wd\foundexternalfigure\figurewidth
702              \ht\foundexternalfigure\figureheight
703              \dp\foundexternalfigure\zeropoint
704              \box\foundexternalfigure
705            \else
706              \letexternalfigureparameter\c!offset\v!overlay
707              \letexternalfigureparameter\c!width \figurewidth
708              \letexternalfigureparameter\c!height\figureheight
709              \inheritedexternalfigureframed{\box\foundexternalfigure}%
710            \fi
711          \fi
712        \fi
713      \else
714        % maybe also \the\externalfigurepostprocessors
715        \iftrialtypesetting \else \feedbackexternalfigure \fi
716      \fi}}
717
718\mutable\let\feedbackexternalfigure\relax % hook
719
720\permanent\protected\def\getfiguredimensions
721  {\dodoubleempty\grph_include_get_dimensions}
722
723\def\grph_include_get_dimensions[#1][#2]%
724  {\startnointerference
725     \settrue\c_grph_include_test_only
726     \externalfigure[#1][#2,\c!display=,\c!mask=,\c!object=\v!no]%
727   \stopnointerference}
728
729\permanent\protected\def\doifelsefigure#1%
730  {\getfiguredimensions[#1]% so data is available !
731   \ifcase\figurestatus
732     \expandafter\secondoftwoarguments
733   \else
734     \expandafter\firstoftwoarguments
735   \fi}
736
737\aliased\let\doiffigureelse\doifelsefigure
738
739% No placement, handy for preprocessing:
740
741\permanent\tolerant\protected\def\registerexternalfigure[#1]#*[#2]#*[#3]%
742  {\startnointerference
743     \c_grph_include_test_only
744     \setfalse\c_grph_include_flush % == test ?
745     \externalfigure[#1][#2][#3]% or
746     \externalfigure[#1][#2,\c!display=,\c!mask=,\c!object=\v!no]%
747   \stopnointerference}
748
749% Helpers will be replaced when xforms are accessible at the lua end but then
750% we need to do the object offset there too.
751
752\permanent\protected\def\dosetfigureobject#1#2%
753  {\setobject{#1}{#2}\vpack{\box\foundexternalfigure}}
754
755\permanent\protected\def\doboxfigureobject#1#2%
756  {\global\setbox\foundexternalfigure\vpack{\getobject{#1}{#2}}} % probably one vbox too many
757
758% Figure bases
759
760\permanent\protected\def\usefigurebase[#1]%
761  {\clf_usefigurebase{#1}}
762
763\appendtoks
764   \setfigurepathlist  % the path may be used elsewhere too (as in x-res-04)
765\to \everysetupexternalfigure
766
767\appendtoks
768    \clf_setfigurelookuporder{\externalfigureparameter\c!order}%
769\to \everysetupexternalfigure
770
771\definecolor[missingfigurecolor][s=.8]
772
773\def\grph_include_replacement#1#2#3%
774  {\bgroup
775   \letexternalfigureparameter\c!width\figurewidth
776   \letexternalfigureparameter\c!height\figureheight
777   \letexternalfigureparameter\c!background\v!color
778   \setexternalfigureparameter\c!backgroundcolor{missingfigurecolor}%
779   \setexternalfigureparameter\c!align{\v!middle,\v!lohi}% we default to \v!none
780   \inheritedexternalfigureframed
781     {\tt\tfxx \nohyphens
782      name:  \expanded{\verbatimstring{#1}}\\%
783      file:  \expanded{\verbatimstring{#2}}\\%
784      state: \expanded{\verbatimstring{#3}}}%
785   \egroup}
786
787% maybe setuphandler
788
789\newconditional\c_grph_include_in_collection
790
791\newdimen\d_grph_include_collection_minwidth
792\newdimen\d_grph_include_collection_maxwidth
793\newdimen\d_grph_include_collection_minheight
794\newdimen\d_grph_include_collection_maxheight
795
796\def\grph_include_analyze_collection[#1][#2]%
797  {\ifconditional\c_grph_include_in_collection
798      \setfalse\c_grph_include_in_collection
799      \getfiguredimensions[#1][#2]%
800      \settrue\c_grph_include_in_collection
801      \scratchdimen\naturalfigurewidth
802      \ifdim\scratchdimen>\d_grph_include_collection_maxwidth  \d_grph_include_collection_maxwidth \scratchdimen \fi
803      \ifdim\scratchdimen<\d_grph_include_collection_minwidth  \d_grph_include_collection_minwidth \scratchdimen \fi
804      \scratchdimen\naturalfigureheight
805      \ifdim\scratchdimen>\d_grph_include_collection_maxheight \d_grph_include_collection_maxheight\scratchdimen \fi
806      \ifdim\scratchdimen<\d_grph_include_collection_minheight \d_grph_include_collection_minheight\scratchdimen \fi
807   \fi}
808
809\mutable\let\currentexternalfigurecollection\empty
810
811\permanent\protected\def\startexternalfigurecollection[#1]%
812  {\begingroup
813   \def\currentexternalfigurecollection{#1}%
814   \settrue\c_grph_include_in_collection
815   \d_grph_include_collection_minwidth \maxdimen
816   \d_grph_include_collection_maxwidth \zeropoint
817   \d_grph_include_collection_minheight\maxdimen
818   \d_grph_include_collection_maxheight\zeropoint}
819
820\permanent\protected\def\stopexternalfigurecollection
821  {\xdefcsname\??externalfigurecollection\currentexternalfigurecollection:\c!minwidth \endcsname{\the\d_grph_include_collection_minwidth }%
822   \xdefcsname\??externalfigurecollection\currentexternalfigurecollection:\c!maxwidth \endcsname{\the\d_grph_include_collection_maxwidth }%
823   \xdefcsname\??externalfigurecollection\currentexternalfigurecollection:\c!minheight\endcsname{\the\d_grph_include_collection_minheight}%
824   \xdefcsname\??externalfigurecollection\currentexternalfigurecollection:\c!maxheight\endcsname{\the\d_grph_include_collection_maxheight}%
825   \endgroup}
826
827\permanent\def\externalfigurecollectionparameter#1#2%
828  {\csname
829     \ifcsname\??externalfigurecollection#1:#2\endcsname
830       \??externalfigurecollection#1:#2%
831     \else
832       \s!empty
833     \fi
834   \endcsname}
835
836\permanent\def\externalfigurecollectionminwidth #1{\externalfigurecollectionparameter{#1}\c!minwidth }
837\permanent\def\externalfigurecollectionmaxwidth #1{\externalfigurecollectionparameter{#1}\c!maxwidth }
838\permanent\def\externalfigurecollectionminheight#1{\externalfigurecollectionparameter{#1}\c!minheight}
839\permanent\def\externalfigurecollectionmaxheight#1{\externalfigurecollectionparameter{#1}\c!maxheight}
840
841\aliased\let\efcparameter\externalfigurecollectionparameter % still needed ?
842\aliased\let\efcminwidth \externalfigurecollectionminwidth  % still needed ?
843\aliased\let\efcmaxwidth \externalfigurecollectionmaxwidth  % still needed ?
844\aliased\let\efcminheight\externalfigurecollectionminheight % still needed ?
845\aliased\let\efcmaxheight\externalfigurecollectionmaxheight % still needed ?
846
847% \startexternalfigurecollection[name]
848%     \useexternalfigure[cow] [cow.pdf]
849%     \useexternalfigure[mill][mill.png]
850% \stopexternalfigurecollection
851% \starttext
852% \bTABLE
853%     \bTR
854%         \bTD \externalfigure[cow] [height=\externalfigurecollectionmaxheight{name}] \eTD
855%         \bTD \externalfigure[mill][height=\externalfigurecollectionmaxheight{name}] \eTD
856%     \eTR
857% \eTABLE
858% \stoptext
859
860\permanent\protected\def\overlayfigure#1%
861  {\externalfigure[#1][\c!width=\d_overlay_width,\c!height=\d_overlay_height]}
862
863% Experimental (will become cleaner):
864
865\permanent\tolerant\protected\def\includesvgfile[#1]#*[#2]%
866  {\dontleavehmode\begingroup
867   \getdummyparameters[\c!offset=\zeropoint,#2]%
868   \clf_includesvgfile{#1}\dimexpr\dummyparameter\c!offset\relax
869   \endgroup}
870
871\permanent\tolerant\protected\def\includesvgbuffer[#1]#*[#2]%
872  {\dontleavehmode\begingroup
873   \getdummyparameters[\c!offset=\zeropoint,#2]%
874   \clf_includesvgbuffer{#1}\dimexpr\dummyparameter\c!offset\relax
875   \endgroup}
876
877% Bonus:
878
879\useexternalfigure
880  [\v!buffer]
881  [\jobname.buffer]
882  [\c!object=\v!no]
883
884% Another two:
885
886\defineexternalfigure
887  [\v!inline]
888  [\c!height=\lineheight]
889
890\definemeasure
891  [\v!combination]
892  [(\textwidth
893    -\effectiveleftskip
894    -\effectiverightskip
895    -\numexpr\combinationparameter\c!nx-\plusone\relax\dimexpr\combinationparameter\c!distance\relax
896   )/\combinationparameter\c!nx]
897
898\defineexternalfigure
899  [\v!combination]
900  [\c!width=\measure{\v!combination}]
901
902% \startcombination[nx=2,ny=1]
903%     {\externalfigure[dummy][combination]} {}
904%     {\externalfigure[dummy][combination]} {}
905% \stopcombination
906
907% \startcombination[nx=2,ny=1]
908%     {\externalfigure[dummy][width=\measure{combination}]} {}
909%     {\externalfigure[dummy][width=\measure{combination}]} {}
910% \stopcombination
911
912% \startcombination[nx=2,ny=2]
913%     {\externalfigure[dummy][combination]} {}
914%     {\externalfigure[dummy][combination]} {}
915%     {\externalfigure[dummy][combination]} {}
916%     {\externalfigure[dummy][combination]} {}
917% \stopcombination
918
919% \startcombination[nx=3,ny=1]
920%     {\externalfigure[dummy][combination]} {}
921%     {\externalfigure[dummy][combination]} {}
922%     {\externalfigure[dummy][combination]} {}
923% \stopcombination
924
925% \startcombination[nx=4,ny=1]
926%     {\externalfigure[dummy][combination]} {}
927%     {\externalfigure[dummy][combination]} {}
928%     {\externalfigure[dummy][combination]} {}
929%     {\externalfigure[dummy][combination]} {}
930% \stopcombination
931
932\permanent\protected\def\inlinefigure[#1]%
933  {\dontleavehmode
934   \sbox{\externalfigure[#1][\v!inline]}}
935
936%D Needs to be done global:
937
938\definelayer[epdfcontent]
939
940\protect \endinput
941
942%D Moved here because this already old code is nowhere documents (so I need to check
943%D it:
944%
945% \starttyping
946% \starttext
947%
948% \startluaparameterset [u3d:myset:controls:1]
949%     view = {
950%         name = 'default',
951%         bg = {1,1,1},
952%         mag = 100,
953%         coo = {0,0,0},
954%         c2c = {0,0,1},
955%         rot = {40,0,60},
956%         roo = 6,
957%         lights = 'CAD'
958%     },
959%     js = 'cloudq.js'
960% \stopluaparameterset
961%
962% \startluaparameterset [u3d:myset:controls:2]
963%     views = {
964%         {
965%             name = 'AnglePositioning',
966%             bg = {1,1,1},
967%             azimuth = 45,
968%             altitude = 45,
969%             roo = 50,
970%             aac = 2.5,
971%             lights = 'Artwork'
972%         },
973%         {
974%             name = 'RotationPositioning',
975%             bg = {1,1,1},
976%             rot = {0,45,45},
977%             roo = 50,
978%             aac = 2.5,
979%             lights = 'Artwork'
980%         },
981%         {
982%             name = 'VectorPositioning',
983%             bg = {1,0,0},
984%             c2c = {1,1,math.sqrt(2)},
985%             roo = 50,
986%             aac = 2.5,
987%             lights = 'CAD'
988%         },
989%         {
990%             name = 'PositionPositioning',
991%             bg = {1,0,0},
992%             pos = {1+25,1+25,1+50/math.sqrt(2)},
993%             aac = 2.5,
994%             lights = 'CAD'
995%         },
996%         {
997%             name = 'ortho',
998%             bg = {1,1,1},
999%             mag = 300,
1000%             lights = 'CAD',
1001%             crossection = {}
1002%         }
1003%     },
1004%     view = {
1005%         name = 'default',
1006%         bg = {1,1,1},
1007%         c2c = {-1,-1,0},
1008%         roo = 50,
1009%         aac = 2.5,
1010%         roll = 45,
1011%         lights = 'CAD',
1012%         crossection = {
1013%             normal = {-1,-1,-1},
1014%             transparent = true
1015%         },
1016%         nodes = {
1017%             {
1018%                 name = 'xlabel',
1019%                 visible = false
1020%             },
1021%             {
1022%                 name = 'ylabel',
1023%                 opacity = 0.5
1024%             },
1025%             {
1026%                 name = 'zlabel',
1027%                 rendermode = 'Wireframe'
1028%             }
1029%         }
1030%     }
1031% \stopluaparameterset
1032%
1033% \useexternalfigure
1034%   [cloudq]
1035%   [cloudq.u3d]
1036%   [width=0.7\textwidth,
1037%    height=.7\textwidth,
1038%    display=u3d:myset:display:1,
1039%    controls=u3d:myset:controls:1]
1040%
1041% \useexternalfigure
1042%   [axes]
1043%   [axes.u3d]
1044%   [width=0.7\textwidth,
1045%    height=.7\textwidth,
1046%    controls=u3d:myset:controls:1]
1047%
1048% \startluaparameterset[u3d:myset:display:2]
1049%     toolbar = true,
1050%     preview = 'cloudq.png'
1051% \stopluaparameterset
1052% \startluaparameterset[u3d:myset:display:3]
1053%     toolbar = true,
1054%     tree = false,
1055%     preview = 'axes.png'
1056% \stopluaparameterset
1057% \startluaparameterset[u3d:myset:display:4]
1058%     toolbar = true,
1059%     tree = false,
1060%     view = {
1061%         name = 'view',
1062%         bg = {0.1,0.1,0.1},
1063%         c2c = {-1,-1,0},
1064%         roo = 50,
1065%         aac = 2.5,
1066%         roll = 45,
1067%         lights = 'Red'
1068%     }
1069% \stopluaparameterset
1070% \startluaparameterset[u3d:myset:display:5]
1071%     toolbar = true,
1072%     tree = false,
1073%     view = 'ortho'
1074% \stopluaparameterset
1075%
1076% \placefigure[here]{none}{\externalfigure[cloudq][frame=on,display=u3d:myset:display:2]}
1077% \placefigure[here]{none}{\externalfigure[axes]  [frame=on,display=u3d:myset:display:3]}
1078% \placefigure[here]{none}{\externalfigure[axes]  [frame=on,display=u3d:myset:display:4]}
1079% \placefigure[here]{none}{\externalfigure[axes]  [frame=on,display=u3d:myset:display:5,width=0.5\textwidth,height=.5\textwidth]}
1080%
1081% \stoptext
1082