summaryrefslogtreecommitdiff
path: root/tex/context/base/grph-inc.mkiv
blob: 633130ea939dcfa64e58d1871bb0b7170a69d105 (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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
%D \module
%D   [       file=grph-inc, % moved from core-fig
%D        version=2006.08.26, % overhaul of 1997.03.31
%D          title=\CONTEXT\ Graphic Macros,
%D       subtitle=Figure Inclusion,
%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 Graphic Macros / Figure Inclusion}

%D todo:
%D
%D - color conversion
%D - alternative images
%D - a few more obscure things

% use framedcommandhandler

\registerctxluafile{grph-inc}{1.001}
\registerctxluafile{grph-fil}{1.001}
\registerctxluafile{grph-u3d}{1.001} % this will change
\registerctxluafile{grph-swf}{1.001} % this will change

\unprotect

%D The following registers are used (if only to be downward compatible).

\newbox  \foundexternalfigure
\newif   \ifskipexternalfigures
\newtoks \everyexternalfigureresets
\newtoks \everyexternalfigurechecks
\newtoks \externalfigurepostprocessors

\def\resetfigurevariables {\the\everyexternalfigureresets}
\def\checkfigurevariables {\the\everyexternalfigurechecks}

%D Historic feature:

\appendtoks
    \global\let\externalfigurelog\empty
\to \everyexternalfigureresets

\let\runutilityfiletrue    \relax \let\runutilityfilefalse    \relax
\let\consultutilityfiletrue\relax \let\consultutilityfilefalse\relax

%D You can register additional suffixes with the following command:
%D
%D \starttyping
%D \definegraphictypesynonym[jbig] [jb2]
%D \definegraphictypesynonym[jbig2][jb2]
%D \definegraphictypesynonym[jbg]  [jb2]
%D \stoptyping

\unexpanded\def\definegraphictypesynonym
  {\dodoubleargument\dodefinegraphictypesynonym}

\def\dodefinegraphictypesynonym[#1][#2]%
  {\ctxlua{figures.registersuffix("#1","#2")}}

%D Additional paths can be installed with the regular setup command. The next
%D macro picks up the list.

\def\setfigurepathlist
  {\ctxlua{figures.setpaths("\@@exlocation",\!!bs\@@exdirectory\!!es)}}

%D Variables:

\def\defaultfigurewidth  {8\lineheight}
\def\defaultfigureheight {6\lineheight}

\def\figurestatus        {\numexpr\ctxlua{figures.tprint("status","status",0)}\relax} % number: 0 = not found
\def\figurewidth         {\ctxlua{figures.tprint("status","width",0)}sp}
\def\figureheight        {\ctxlua{figures.tprint("status","height",0)}sp}
\def\figurexscale        {\ctxlua{figures.tprint("status","xscale",1)}}
\def\figureyscale        {\ctxlua{figures.tprint("status","yscale",1)}}

\def\figuresize          {\ctxlua{figures.tprint("request","size")}}
\def\figurelabel         {\ctxlua{figures.tprint("request","label")}}
\def\figurefileoriginal  {\ctxlua{figures.tprint("request","name")}}
\def\figurefilepage      {\ctxlua{figures.tprint("request","page",1)}}
\def\figurefileoptions   {\ctxlua{figures.tprint("request","options")}}
\def\figurefileconversion{\ctxlua{figures.tprint("request","conversion")}}
\def\figurefilecache     {\ctxlua{figures.tprint("request","cache")}}
\def\figurefileprefix    {\ctxlua{figures.tprint("request","prefix")}}

\def\figurenaturalwidth  {\ctxlua{figures.tprint("used","width", \number\dimexpr\defaultfigurewidth \relax)}sp}
\def\figurenaturalheight {\ctxlua{figures.tprint("used","height",\number\dimexpr\defaultfigureheight\relax)}sp}
\def\figurexresolution   {\ctxlua{figures.tprint("used","xresolution",0)}}
\def\figureyresolution   {\ctxlua{figures.tprint("used","yresolution",0)}}
\def\figurexsize         {\ctxlua{figures.tprint("used","xsize",0)}}
\def\figureysize         {\ctxlua{figures.tprint("used","ysize",0)}}
\def\figurecolordepth    {\ctxlua{figures.tprint("used","colordepth",0)}}
\def\figuredepth         {\ctxlua{figures.tprint("used","depth",0)}}

\def\figurefullname      {\ctxlua{figures.tprint("used","fullname")}}
\def\noffigurepages      {\ctxlua{figures.tprint("used","pages",0)}}

\def\figurefilepath      {\cldcontext{file.dirname (figures.get("used","fullname"))}}
\def\figurefilename      {\cldcontext{file.nameonly(figures.get("used","fullname"))}}
\def\figurefiletype      {\cldcontext{file.extname (figures.get("used","fullname"))}}

\let\naturalfigurewidth  \figurenaturalwidth
\let\naturalfigureheight \figurenaturalheight

\let\figurescalewidth    \figurewidth
\let\figurescaleheight   \figureheight
\let\figurescalexscale   \figurexscale
\let\figurescaleyscale   \figureyscale

\appendtoks
    \ctxlua { % figures.defaultwidth .. why not dimen
        figures.setpaths("\@@exlocation","\@@exdirectory") ;
        figures.defaultwidth  = \number\dimexpr\defaultfigurewidth \relax ;
        figures.defaultheight = \number\dimexpr\defaultfigureheight\relax ;
        figures.boxnumber     = \number\foundexternalfigure ;
    }%
\to \everyexternalfigureresets

%D In some situations we need to make sure that the figure related variables
%D are reset. This is especially important when we are nesting. Is this still
%D needed in \MKIV.

\def\resetexternalfigures
  {\let\@@efoption         \empty % \let\@@efprefix\empty
   \let\@@efmaxwidth       \empty % \let\@@efcache \empty
   \let\@@efmaxheight      \empty % \let\@@efframe \v!off
   \let\@@efforegroundcolor\empty
   \let\@@efcolor          \empty
   \let\@@efconversion     \empty
   \let\@@efbackground     \empty
   \let\@@efresolution     \empty}

\appendtoks \resetexternalfigures \to \everyoverlay
\appendtoks \resetexternalfigures \to \everybeforepagebody % not really needed

\def\resetfigureusersettings % if we use a command handler we can simply define a new instance
  {%
   \let\@@efmethod    \empty
   \let\@@eflabel     \empty
   \let\@@efsize      \empty
   \let\@@efconversion\@@exconversion
   \let\@@efresolution\@@exresolution
   \let\@@efprefix    \@@exprefix
   \let\@@efcache     \@@excache
   \let\@@efpage      \!!zerocount
   \let\@@efobject    \@@exobject
   \let\@@efdisplay   \empty
   \let\@@efmask      \empty
   %
   \let\@@efpreset    \v!yes
   \let\@@efsplit     \empty
   \let\@@efcolor     \empty
   %
   \let\@@efsymbol    \v!no
   %
   \let\@@efcontrols  \v!no
   \let\@@efresources \empty
   \let\@@efpreview   \v!no
   \let\@@efrepeat    \v!no
   %
   \let\@@efforegroundcolor\empty
   %
   \let\@@efinteraction\@@exinteraction
   %
   \let\@@efhfactor   \empty
   \let\@@efwfactor   \empty
   \let\@@effactor    \empty
   \let\@@efmaxwidth  \@@exmaxwidth
   \let\@@efmaxheight \@@exmaxheight
   \let\@@efxscale    \empty
   \let\@@efyscale    \empty
   \let\@@efscale     \empty
   \let\@@efsx        \!!plusone
   \let\@@efsy        \!!plusone
   \let\@@efwidth     \empty
   \let\@@efheight    \empty
   \let\@@eflines     \empty
   \let\@@efgrid      \empty}

\resetfigureusersettings

\appendtoks
    \resetfigureusersettings
\to \everyexternalfigureresets

\def\checkfigureusersettings
  {% old features
   \doif\@@exoption\v!frame
     {\let\@@efframe\v!on}%
   \doif\@@exoption\v!empty
     {\skipexternalfigurestrue
      \let\@@efframe\v!off}%
   \doifsomething\@@efwidth {\doifdimensionelse\@@efwidth {\edef\@@efwidth {\the\dimexpr\@@efwidth }}\donothing}%
   \doifsomething\@@efheight{\doifdimensionelse\@@efheight{\edef\@@efheight{\the\dimexpr\@@efheight}}\donothing}%
   % fake color in gray bitmaps, assumes that
   % a transparent color is used
   \doifsomething\@@efforegroundcolor
     {\def\@@efbackground{\v!foreground,\v!color}%
      \def\@@efbackgroundcolor{\@@efforegroundcolor}}}

\appendtoks
    \checkfigureusersettings
\to \everyexternalfigurechecks

%D Internal graphics are handled at the \TEX\ end:

\def\doprocesstexlikefigure#1% retrofit into mkii
  {\global\setbox\foundexternalfigure\vbox\framed
     [\c!strut=\v!no,\c!align=\v!normal,\c!frame=\v!off,
      \c!offset=\v!overlay,\c!width=\v!fit,\c!height=\v!fit]
     {\blank[\v!disable]#1\endgraf\removelastskip}} % disable should stay here!

\def\doprocessmpslikefigure#1% retrofit into mkii
  {\global\setbox\foundexternalfigure\vbox{\convertMPtoPDF{#1}11}}

\def\doprocesscldlikefigure#1%
  {\global\setbox\foundexternalfigure\vbox{\cldprocessfile{#1}}}

\def\docheckfigurebuffer  #1{\doprocesstexlikefigure{\getbuffer[#1]}}
\def\docheckfiguretex     #1{\doprocesstexlikefigure{\input{#1}}}
\def\docheckfigurecld     #1{\doprocesscldlikefigure{#1}} % we can always add cldrun
\def\docheckfiguremps     #1{\doprocessmpslikefigure{#1}}
\def\docheckfiguremprun #1#2{\doprocesstexlikefigure{\useMPrun{#1}{#2}}}

% \def\doscalefigure
%   {\global\setbox\foundexternalfigure\vbox{\doscalebox\??ef{\dowithfigure{\box\foundexternalfigure}}}}

\def\doscalefigure
  {\global\setbox\foundexternalfigure\vbox{\scale[\v!figure]{\dowithfigure{\box\foundexternalfigure}}}}

% quick hack: chaining \??ef via \s!parent happens when this is also a commandhandler

\definescale
  [\v!figure]
  [\c!hfactor  =\@@efhfactor  ,
   \c!wfactor  =\@@efwfactor  ,
   \c!factor   =\@@effactor   ,
   \c!maxwidth =\@@efmaxwidth ,
   \c!maxheight=\@@efmaxheight,
   \c!xscale   =\@@efxscale   ,
   \c!yscale   =\@@efyscale   ,
   \c!scale    =\@@efscale    ,
   \c!sx       =\@@efsx       ,
   \c!sy       =\@@efsy       ,
   \c!width    =\@@efwidth    ,
   \c!height   =\@@efheight   ,
   \c!lines    =\@@eflines    ]

\newconditional\testexternalfigureonly

% \enabletrackers[figures.conversion]
% \externalfigure[demo.svg]
% \externalfigure[demo.svg][conversion=png]

\unexpanded\def\calculateexternalfigure[#1][#2][#3][#4][#5][#6]% \cmd label filename parent_id preset current
  {\dontcomplain
   \restorecatcodes
   \forgetall
   \resetfigurevariables
   \dosetefparameters{#4}{#5}{#6}%
   \checkfigurevariables
%    \begingroup
%    \color[\@@efcolor]{\xdef\globcolorattr{\internalspotcolorname}}
%    \endgroup
   \ctxlua{figures.push {
        name       = "#3",
        label      = "#2", % todo: \@eflabel
        page       = "\@@efpage",
        size       = "\@@efsize",
        object     = "\@@efobject",
        prefix     = "\@@efprefix",
        cache      = "\@@efcache",
        format     = "\@@efmethod",
        preset     = "\@@efprefix",
        controls   = "\@@efcontrols",
        resources  = "\@@efresources",
        preview    = "\@@efpreview",
        display    = "\@@efdisplay",
        mask       = "\@@efmask",
        conversion = "\@@efconversion",
        resolution = "\@@efresolution",
        color      = "\internalspotcolorparent\@@efcolor", % hack is needed
        ["repeat"] = "\@@efrepeat",
        width      = "\@@efwidth", % can be crap
        height     = "\@@efheight", % can be crap
    } }%
   \ctxlua{figures.identify()}%
   % also mode: checkpresense only
   \ifconditional\testexternalfigureonly
      \ifcase\figurestatus \else
        \ctxlua{figures.check()}%
        \ctxlua{figures.dummy()}%
        \ctxlua{figures.scale()}%
        \ctxlua{figures.done()}%
      \fi
      \signalexternalfigure
   \else
      \ifcase\figurestatus
        \ctxlua{figures.dummy()}%
        \ctxlua{figures.scale()}%
      \else
        \ctxlua{figures.check()}%
        \ctxlua{figures.include()}%
        \ctxlua{figures.scale()}%
      \fi
      \ctxlua{figures.done()}%
      \signalexternalfigure
      \finishexternalfigure
   \fi
   \ctxlua{figures.pop()}}

\def\relocateexternalfigure % easier here than in lua
  {\global\setbox\foundexternalfigure\vbox to \ht\foundexternalfigure\bgroup
     \vss
     \ht\foundexternalfigure\zeropoint
     \hbox to \wd\foundexternalfigure\bgroup
        \box\foundexternalfigure
        \hss
     \egroup
   \egroup}

\newsystemmode\v!figure

\def\signalexternalfigure % global (dup call to status)
  {\ifcase\figurestatus
     \global\resetsystemmode\v!figure % todo, also: \v!resource
   \else
     \global\setsystemmode  \v!figure % todo, also: \v!resource
   \fi}

\unexpanded\def\startfoundexternalfigure#1#2% ht wd
  {\global\setbox\foundexternalfigure\vbox to #2\bgroup\vss\hbox to #1\bgroup}

\unexpanded\def\stopfoundexternalfigure
  {\hss\egroup\egroup}

\def\emptyfoundexternalfigure
  {\startfoundexternalfigure\defaultfigurewidth\defaultfigureheight
   \stopfoundexternalfigure}

\def\finishexternalfigure % here we use \figurevariables
  {\global\setbox\foundexternalfigure\vbox
     {\ifcase\figurestatus
        \let\@@efframe\v!on
      \fi
      \ifconditional\externalfigureflush
        \ifconditional\externalfigurelevel % probably background
          \ifskipexternalfigures
            % nothing
            \fakebox\foundexternalfigure
          \else\ifcase\figurestatus
            % nothing
          \else
            \the\externalfigurepostprocessors
            \box\foundexternalfigure
          \fi\fi
        \else
          \iftrialtypesetting \else \feedbackexternalfigure \fi
          \settrue\externalfigurelevel
          \ifskipexternalfigures
            \ifcase\figurestatus
              \externalfigurereplacement\figurelabel\figurefileoriginal{unknown}%
            \else
              \externalfigurereplacement\figurelabel\figurefullname{skipped}%
            \fi
          \else\ifcase\figurestatus
            \externalfigurereplacement\figurelabel\figurefileoriginal{unknown}%
          \else
            \the\externalfigurepostprocessors
            \doifelse\@@efreset\v!yes
              {\wd\foundexternalfigure\figurewidth
               \ht\foundexternalfigure\figureheight
               \dp\foundexternalfigure\zeropoint
               \box\foundexternalfigure}
              {\localframed % should also be applied to high res !
                 [\??ef]
                 [\c!offset=\v!overlay,
                  \c!width=\figurewidth,
                  \c!height=\figureheight]
                 {\vfilll\box\foundexternalfigure}}%
          \fi\fi
        \fi
      \else
        % maybe also \the\externalfigurepostprocessors
        \iftrialtypesetting \else \feedbackexternalfigure \fi
      \fi}}

\ifx\externalfigurereplacement\undefined\let\externalfigurereplacement\gobblethreearguments\fi
\ifx\externalfigureplaceholder\undefined\let\externalfigureplaceholder\gobblethreearguments\fi

\let\feedbackexternalfigure\relax % \gobblefourarguments
\let\dowithfigure          \relax

\def\getfiguredimensions
  {\dodoubleempty\dogetfiguredimensions}

\def\dogetfiguredimensions[#1][#2]%
  {\startnointerference
     \settrue\testexternalfigureonly
     \externalfigure[#1][#2,\c!display=,\c!mask=,\c!object=\v!no]%
   \stopnointerference}

\let\getfiguredimensionsonly\getfiguredimensions

% \unexpanded\def\doiffigureelse#1%
%   {\getfiguredimensions[#1]% so data is available !
%    \ifdim\figurewidth=\zeropoint % todo: \figurestatus
%      \expandafter\secondoftwoarguments
%    \else
%      \expandafter\firstoftwoarguments
%    \fi}

% \unexpanded\def\doiffigureelse#1% just low level call
%   {\getfiguredimensions[#1]% so data is available ! ... grouped so status is local
%    \doifmodeelse{*\v!figure}\firstoftwoarguments\secondoftwoarguments} % mode is set global

\unexpanded\def\doiffigureelse#1%
  {\getfiguredimensions[#1]% so data is available !
   \ifcase\figurestatus
     \expandafter\secondoftwoarguments
   \else
     \expandafter\firstoftwoarguments
   \fi}

\def\registerexternalfigure % no placement, handy for preprocessing
  {\dotripleempty\doregisterexternalfigure}

\def\doregisterexternalfigure[#1][#2][#3]%
  {\startnointerference
     \testexternalfigureonly
     \setfalse\externalfigureflush % == test ?
     \externalfigure[#1][#2][#3]% or \doexternalfigure
     \externalfigure[#1][#2,\c!display=,\c!mask=,\c!object=\v!no]%
   \stopnointerference}

% helpers (will be replaced when xforms are accessible at the lua end)

\def\dosetfigureobject#1%
  {\setobject{FIG}{#1}\vbox{\box\foundexternalfigure}}

\def\doboxfigureobject#1%
  {\global\setbox\foundexternalfigure\vbox{\getobject{FIG}{#1}}} % probably one vbox too many

% figurebases

\def\usefigurebase[#1]%
  {\ctxlua{figures.bases.use("#1")}}

\protect \endinput

% \startbuffer
% \definecolor [blue]   [c=1,m=.38,y=0,k=.64]
% \definecolor [yellow] [c=0,m=.28,y=1,k=.06]
%
% \definespotcolor [blue-100]   [blue]   [p=1]
% \definespotcolor [yellow-100] [yellow] [p=1]
%
% \definemultitonecolor [combicolor]   [blue=.12,yellow=.28] [c=.1,m=.1,y=.3,k=.1]
%
% \definemultitonecolor [combicolor-b] [blue=1]   [c=1,m=.38,y=0,k=.64] % force multitone
% \definemultitonecolor [combicolor-y] [yellow=1] [c=0,m=.28,y=1,k=.06] % force multitone
%
% \useexternalfigure[demo-a][mill.png]     [object=no,width=.2\textwidth]
% \useexternalfigure[demo-b][hacker-bw.jpg][object=no,width=.2\textwidth]
%
% \startbaselinecorrection \startcombination[4*1]
%   {\externalfigure[demo-a]}                     {no color}
%   {\externalfigure[demo-a][color=combicolor]}   {indexed duotone}
%   {\externalfigure[demo-a][color=combicolor-b]} {spot color}
%   {\externalfigure[demo-a][color=combicolor-y]} {spot color}
% \stopcombination \stopbaselinecorrection
%
% \startbaselinecorrection \startcombination[4*1]
%   {\externalfigure[demo-b]}                     {no color}
%   {\externalfigure[demo-b][color=combicolor]}   {indexed duotone}
%   {\externalfigure[demo-b][color=combicolor-b]} {spot color}
%   {\externalfigure[demo-b][color=combicolor-y]} {spot color}
% \stopcombination \stopbaselinecorrection
%
% \startbaselinecorrection \startcombination[4*1]
%   {\externalfigure[demo-a]}                     {no color}
%   {\externalfigure[demo-a][color=combicolor]}   {indexed duotone}
%   {\externalfigure[demo-a][color=blue-100]}     {spot color}
%   {\externalfigure[demo-a][color=yellow-100]}   {spot color}
% \stopcombination \stopbaselinecorrection
%
% \startbaselinecorrection \startcombination[4*1]
%   {\externalfigure[demo-b]}                     {no color}
%   {\externalfigure[demo-b][color=combicolor]}   {indexed duotone}
%   {\externalfigure[demo-b][color=blue-100]}     {spot color}
%   {\externalfigure[demo-b][color=yellow-100]}   {spot color}
% \stopcombination \stopbaselinecorrection
% \stopbuffer
%
% \getbuffer \typebuffer