Debian Codegraph @

texmf-dist/tex/context/base/mkii/supp-pdf.mkii

raw
1 %D \module
2 %D [ file=supp-pdf,
3 %D version=2006.09.02, % 2004.12.16,
4 %D title=\CONTEXT\ Support Macros,
5 %D subtitle=\METAPOST\ to \PDF\ conversion,
6 %D author=Hans Hagen \& others (see text),
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 \ifx\writestatus\undefined
15 \immediate\write16{[Loading MPS to PDF converter (version 2006.09.02).]}
16 \else
17 \writestatus{loading}{ConTeXt Support Macros / PDF}
18 \fi
19
20 %D This module is not optimized because it is used outside
21 %D \CONTEXT\ and we cannot forsee what interference will take
22 %D place. We no longer load supp-mis. I got too tired of
23 %D keeping track of interferences with non \CONTEXT\ usage so
24 %D I moved the necessary code in here and use a derived version
25 %D in \CONTEXT. When I have the time I will clean up some of the
26 %D code that is of no use for non||\CONTEXT\ users.
27
28 \ifx\contextversion\undefined
29
30 \chardef\mptopdfstoredatcode\catcode`\@
31
32 \catcode\string`@=11
33
34 \def\unprotect
35 {\let\@@mpstopdf@@unprotect \unprotect
36 \let\@@mpstopdf@@protect \protect
37 \edef\protect
38 {\noexpand \let \noexpand \protect \noexpand \@@mpstopdf@@protect
39 \noexpand \let \noexpand \unprotect \noexpand \@@mpstopdf@@unprotect
40 % context specific
41 \catcode\string`\noexpand @=\the\catcode\string`@
42 \catcode\string`\noexpand !=\the\catcode\string`!
43 \catcode\string`\noexpand ?=\the\catcode\string`?
44 % latex specific
45 \catcode\string`\noexpand /=\the\catcode\string`/
46 \catcode\string`\noexpand -=\the\catcode\string`-
47 \catcode\string`\noexpand :=\the\catcode\string`:
48 \catcode\string`\noexpand ;=\the\catcode\string`;
49 \catcode\string`\noexpand "=\the\catcode\string`"
50 \catcode\string`\noexpand <=\the\catcode\string`<
51 \catcode\string`\noexpand >=\the\catcode\string`>
52 \catcode\string`\noexpand `=\the\catcode\string``}
53 \catcode\string`@=11 \catcode\string`!=11 \catcode\string`?=11
54 \catcode\string`/=12 \catcode\string`-=12
55 \catcode\string`:=12 \catcode\string`;=12
56 \catcode\string`>=12 \catcode\string`<=12
57 \catcode\string`"=12 \catcode\string``=12 }
58
59 \def\defineMPtoPDFfallbacks
60 {% shortcuts
61 \chardef\@@escape 0
62 \chardef\@@begingroup 1
63 \chardef\@@endgroup 2
64 \chardef\@@endofline 5
65 \chardef\@@ignore 9
66 \chardef\@@space 10
67 \chardef\@@letter 11
68 \chardef\@@other 12
69 \chardef\@@active 13
70 \chardef\@@comment 14
71 % placeholders
72 \ifx\withoutpt \undefined \let\withoutpt \@@mptopdf@@withoutpt \fi
73 \ifx\twodigitrounding \undefined \let\twodigitrounding \@@mptopdf@@twodigitrounding \fi
74 \ifx\forgetall \undefined \let\forgetall \@@mptopdf@@forgetall \fi
75 \ifx\uncatcodespecials \undefined \let\uncatcodespecials \@@mptopdf@@uncatcodespecials \fi
76 \ifx\setnaturalcatcodes\undefined \let\setnaturalcatcodes\@@mptopdf@@setnaturalcatcodes \fi
77 \ifx\dospecials \undefined \let\dospecials \@@mptopdf@@dospecials \fi}
78
79 \def\@@mptopdf@@forgetall
80 {\parskip0pt\leftskip0pt\parindent0pt\everypar{}}
81
82 \def\@@mptopdf@@twodigitrounding#1%
83 {#1}%
84
85 \def\@@mptopdf@@uncatcodespecials
86 {\def\do##1{\catcode`##1=\@@other}\dospecials
87 \catcode`\ =\@@space
88 \catcode`\^^L=\@@ignore
89 \catcode`\^^M=\@@endofline
90 \catcode`\^^?=\@@ignore}
91
92 \def\@@mptopdf@@setnaturalcatcodes
93 {\catcode`\!=\@@other \catcode`\?=\@@other \catcode`\@=\@@other
94 \catcode`\#=\@@other \catcode`\&=\@@other \catcode`\|=\@@other
95 \catcode`\$=\@@other \catcode`\^=\@@other \catcode`\_=\@@other
96 \catcode`\*=\@@other \catcode`\/=\@@other
97 \catcode`\-=\@@other \catcode`+=\@@other
98 \catcode`\==\@@other \catcode`\<=\@@other \catcode`\>=\@@other
99 \catcode`\"=\@@other \catcode`\'=\@@other \catcode`\`=\@@other
100 \catcode`\:=\@@other \catcode`\;=\@@other
101 \catcode`\,=\@@other \catcode`\.=\@@other \catcode`\~=\@@other
102 \catcode`\(=\@@other \catcode`\)=\@@other
103 \catcode`\{=\@@other \catcode`\}=\@@other
104 \catcode`\\=\@@other \catcode`\%=\@@other}
105
106 \def\@@mptopdf@@dospecials
107 {\do\ \do\\\do\{\do\}\do\$%
108 \do\#\do\^\do\_\do\&\do\%%
109 \do\~\do\^^A\do\^^K}
110
111 {
112 \catcode`\.=12
113 \catcode`\p=12
114 \catcode`\t=12
115 \gdef\@@MPTOPDF@@WITHOUTPT#1pt{#1}
116 }
117
118 \def\@@mptopdf@@withoutpt#1%
119 {\expandafter\@@MPTOPDF@@WITHOUTPT#1}
120
121 % \def\@@mptopdf@@newabove#1#2% \dimen \name
122 % {#1#2%
123 % \ifnum\expandafter\gobblesixarguments\meaning#2>20\else
124 % \expandafter\@@mptopdf@@newabove\expandafter#1\expandafter#2%
125 % \fi}
126
127 \def\@@mptopdf@@stripnewabove#1%
128 {\ifnum10<9#1 #1\else\expandafter\@@mptopdf@@stripnewabove\fi}%
129
130 \def\@@mptopdf@@newabove\csname#1\endcsname#2% \dimen \name
131 {\csname#1\endcsname#2%
132 \ifnum\expandafter\@@mptopdf@@stripnewabove\meaning#2>20\else
133 \@@mptopdf@@newabove\csname#1\endcsname#2%
134 \fi}
135
136 \@@mptopdf@@newabove \csname newcount\endcsname \scratchcounter
137 \@@mptopdf@@newabove \csname newdimen\endcsname \scratchdimen
138 \csname newbox\endcsname \scratchbox
139
140 \expandafter \newif \csname ifdone\endcsname
141
142 \catcode`\@=\mptopdfstoredatcode
143
144 \else
145
146 \let\defineMPtoPDFfallbacks\relax
147
148 \fi
149
150 %D These macros are written as generic as possible. Some
151 %D general support macro's are loaded from a small module
152 %D especially made for non \CONTEXT\ use. In this module I
153 %D use a matrix transformation macro written by Tanmoy
154 %D Bhattacharya. Thanks to extensive testing by Sebastian
155 %D Ratz I was able to complete this module within reasonable
156 %D time. This module has support for \METAPOST\ extensions
157 %D built in.
158 %D
159 %D Daniel H. Luecking came up with a better (more precise)
160 %D transformation method. You can recognize his comment by
161 %D his initials. (We keep the old code around because it's a
162 %D nice illustration on how a module like this evolves.)
163
164 %D Beware, we cannot use \type{\zeropoint} here since it may be
165 %D defined in the range \type{\dimen0} - 20 which we happen to use
166 %D as scratch registers; inside context we may consider
167 %D using dedicated registers.
168
169 %D This module handles some \PDF\ conversion and insertions
170 %D topics. By default, the macros use the \PDFTEX\ primitive
171 %D \type{\pdfliteral} when available. Since \PDFTEX\ is now the
172 %D default engine for \TEX\ distributions, we need a more complex
173 %D test.
174
175 \unprotect
176
177 \ifx\PDFcode\undefined
178 \ifx\pdfliteral\undefined
179 \def\PDFcode#1{\special{PDF: #1}}
180 \else\ifx\pdfoutput\undefined
181 \def\PDFcode#1{\special{PDF: #1}}
182 \else\ifcase\pdfoutput
183 \def\PDFcode#1{\special{PDF: #1}}
184 \else % pdftex as well as in pdf mode
185 \let\PDFcode\pdfliteral
186 \fi\fi\fi
187 \else
188 % we probably use context
189 \fi
190
191 %D First we define a handy constant:
192
193 \bgroup \defineMPtoPDFfallbacks \catcode`\%=\@@other \xdef\letterpercent{\string%} \egroup
194
195 %D \macros
196 %D {pdfimage,pdfimages,pdfclippedimage}
197 %D
198 %D Starting with pdftex version 14, images are included more
199 %D natural to the form embedding. This enables alternative
200 %D images to be embedded.
201 %D
202 %D \starttyping
203 %D \pdfimage <optional dimensions> {file}
204 %D \pdfimages <optional dimensions> {high res file} {low res file}
205 %D \stoptyping
206 %D
207 %D The first one replaces the pre||version||14 original,
208 %D while the latter provides alternative images.
209 %D
210 %D The next macro is dedicated to Maarten Gelderman, who
211 %D needed to paste prepared \PDF\ pages into conference
212 %D proceedings.
213 %D
214 %D \starttyping
215 %D \pdfclippedimage <optional dimensions> {file} {l} {r} {t} {b}
216 %D \stoptyping
217
218 \ifx\pdftexversion\undefined \else \ifnum\pdftexversion>13
219
220 \def\pdfimage#1#%
221 {\dopdfimage{#1}}
222
223 \def\dopdfimage#1#2%
224 {\immediate\pdfximage#1{#2}%
225 \pdfrefximage\pdflastximage}
226
227 \def\pdfimages#1#%
228 {\dopdfimages{#1}}
229
230 \def\dopdfimages#1#2#3%
231 {\immediate\pdfximage#1{#2}%
232 \immediate\pdfobj{[ << /Image \the\pdflastximage\space0 R /DefaultForPrinting true >> ]}%
233 \immediate\pdfximage#1 attr {/Alternates \the\pdflastobj\space0 R}{#3}%
234 \pdfrefximage\pdflastximage}
235
236 \def\pdfclippedimage#1#% specs {file}{left}{right}{top}{bottom}
237 {\dopdfclippedimage{#1}}
238
239 \def\dopdfclippedimage#1#2#3#4#5#6%
240 {\bgroup
241 \pdfximage#1{#2}%
242 \setbox\scratchbox\hbox
243 {\pdfrefximage\pdflastximage}%
244 \hsize\wd\scratchbox
245 \advance\hsize -#3
246 \advance\hsize -#4
247 \vsize\ht\scratchbox
248 \advance\vsize -#5
249 \advance\vsize -#6
250 \setbox\scratchbox\vbox to \vsize
251 {\vskip-#5\hbox to \hsize{\hskip-#3\box\scratchbox\hss}}%
252 \pdfxform\scratchbox
253 \pdfrefxform\pdflastxform
254 \egroup}
255
256 \fi \fi
257
258 %D If you want to save a few hash entries, you may prefer the
259 %D less readable alternatives, like:
260 %D
261 %D \starttyping
262 %D \def\pdfimage#1#% This one is less readable but needs no additional
263 %D {\bgroup % hash entry for the second stage macro.
264 %D \def\pdfimage##1%
265 %D {\immediate\pdfximage##1{#2}%
266 %D \pdfrefximage\pdflastximage\egroup}}
267 %D \stoptyping
268
269 %D For old times sake we provide a few aliases. These will be
270 %D removed some day.
271
272 \ifx\pdftexversion\undefined \else \ifnum\pdftexversion>13
273
274 \let\pdfform =\pdfxform
275 \let\pdflastform=\pdflastxform
276 \let\pdfrefform =\pdfrefxform
277
278 \fi \fi
279
280 %D The main conversion macro wraps the \PDF\ codes in a box
281 %D that is output as an object. The graphics are embedded
282 %D in~\type{q} and~\type{Q} and are scaled and positioned using
283 %D one transform call (\type{cm}). This saves some additional
284 %D scaling.
285
286 %D \macros
287 %D {convertMPtoPDF}
288 %D
289 %D The next set of macros implements \METAPOST\ to \PDF\
290 %D conversion. Because we want to test as fast as possible, we
291 %D first define the \POSTSCRIPT\ operators that \METAPOST\
292 %D uses. We don't define irrelevant ones, because these are
293 %D skipped anyway.
294
295 %D The converter can be made a bit faster by replacing the
296 %D two test macros (the ones with the many \type {\if's}) by
297 %D a call to named branch macros (something \typ {\getvalue
298 %D {xPSmoveto}}. For everyday documents with relatively
299 %D small graphics the gain in speed can be neglected.
300
301 \def \PScurveto {curveto}
302 \def \PSlineto {lineto}
303 \def \PSmoveto {moveto}
304 \def \PSshowpage {showpage}
305 \def \PSnewpath {newpath}
306 \def \PSfshow {fshow}
307 \def \PSclosepath {closepath}
308 \def \PSfill {fill}
309 \def \PSstroke {stroke}
310 \def \PSclip {clip}
311 \def \PSrlineto {rlineto}
312 \def \PSsetlinejoin {setlinejoin}
313 \def \PSsetlinecap {setlinecap}
314 \def \PSsetmiterlimit {setmiterlimit}
315 \def \PSsetgray {setgray}
316 \def \PSsetrgbcolor {setrgbcolor}
317 \def \PSsetcmykcolor {setcmykcolor}
318 \def \PSsetdash {setdash}
319 \def \PSgsave {gsave}
320 \def \PSgrestore {grestore}
321 \def \PStranslate {translate}
322 \def \PSscale {scale}
323 \def \PSconcat {concat}
324 \def \PSdtransform {dtransform}
325 \def \PSsetlinewidth {setlinewidth}
326 \def \PSpop {pop}
327
328 \def \PSnfont {nfont} % was needed for TUG98 proceedings
329 \def \PSspecial {special} % extensions to MetaPost
330
331 %D A previous version set \type {%} to ignore, which
332 %D simplified the following definitions. At the start of
333 %D conversion the percent character was made active again.
334 %D Because the whole graphic is one paragraph (there are no
335 %D empty lines) this does not give the desired effect. This
336 %D went unnoticed untill Scott Pakin sent me a test file
337 %D percent characters in a string. So, from now on we have
338 %D to prefix the following strings with percentages.
339
340 %D Some day I'll figure out a better solution (line by line reading
341 %D using \ETEX).
342
343 \edef \PSBoundingBox {\letterpercent\letterpercent BoundingBox:}
344 \edef \PSHiResBoundingBox {\letterpercent\letterpercent HiResBoundingBox:}
345 \edef \PSExactBoundingBox {\letterpercent\letterpercent ExactBoundingBox:}
346 \edef \PSMetaPostSpecial {\letterpercent\letterpercent MetaPostSpecial:}
347 \edef \PSMetaPostSpecials {\letterpercent\letterpercent MetaPostSpecials:}
348 \edef \PSPage {\letterpercent\letterpercent Page:}
349 \edef \PSBeginProlog {\letterpercent\letterpercent BeginProlog}
350 \edef \PSEndProlog {\letterpercent\letterpercent EndProlog}
351 \edef \PSEof {\letterpercent\letterpercent EOF}
352
353 %D By the way, the \type {setcmykcolor} operator is not
354 %D output by \METAPOST\ but can result from converting the
355 %D \cap{RGB} color specifications, as implemented in
356 %D \type{supp-mps}.
357
358 %D In \POSTSCRIPT\ arguments precede the operators. Due to the
359 %D fact that in some translations we need access to those
360 %D arguments, and also because sometimes we have to skip them,
361 %D we stack them up. The stack is one||dimensional for non path
362 %D operators and two||dimensional for operators inside a path.
363 %D This is because we have to save the whole path for
364 %D (optional) postprocessing. Values are pushed onto the stack
365 %D by:
366 %D
367 %D \starttyping
368 %D \setMPargument {value}
369 %D \stoptyping
370 %D
371 %D They can be retrieved by the short named macros:
372 %D
373 %D \starttyping
374 %D \gMPa {number}
375 %D \gMPs {number}
376 %D \stoptyping
377 %D
378 %D When scanning a path specification, we also save the
379 %D operator, using
380 %D
381 %D \starttyping
382 %D \setMPkeyword {n}
383 %D \stoptyping
384 %D
385 %D The path drawing operators are coded for speed: \type{clip},
386 %D \type{stroke}, \type{fill} and \type{fillstroke} become
387 %D 1, 2, 3 and~4.
388 %D
389 %D When processing the path this code can be retrieved
390 %D using
391 %D
392 %D \starttyping
393 %D \getMPkeyword % {n}
394 %D \stoptyping
395 %D
396 %D When setting an argument, the exact position on the stack
397 %D depends on the current value of the \COUNTERS\
398 %D \type{\nofMPsegments} and \type{\nofMParguments}.
399
400 \newcount\nofMPsegments
401 \newcount\nofMParguments
402
403 %D These variables hold the coordinates. The argument part of
404 %D the stack is reset by:
405 %D
406 %D \starttyping
407 %D \resetMPstack
408 %D \stoptyping
409 %D
410 %D We use the prefix \type{@@MP} to keep the stack from
411 %D conflicting with existing macros. To speed up things a bit
412 %D more, we use the constant \type{\@@MP}.
413
414 \def\@@MP{@@MP}
415
416 \def\setMPargument% #1%
417 {\advance\nofMParguments 1
418 \expandafter\def
419 \csname\@@MP\the\nofMPsegments\the\nofMParguments\endcsname} % {#1}
420
421 \def\letMPargument
422 {\advance\nofMParguments 1
423 \expandafter\let
424 \csname\@@MP\the\nofMPsegments\the\nofMParguments\endcsname}
425
426 \def\setMPsequence#1 %
427 {\advance\nofMParguments 1
428 \expandafter\def
429 \csname\@@MP\the\nofMPsegments\the\nofMParguments\endcsname{#1}%
430 \handleMPsequence}
431
432 \def\gMPa#1%
433 {\csname\@@MP0\number#1\endcsname}
434
435 \def\gMPs#1%
436 {\csname\@@MP\the\nofMPsegments\number#1\endcsname}
437
438 \def\dogMPa#1%
439 {\expandafter\expandafter\expandafter\do\csname\@@MP0\number#1\endcsname}
440
441 \def\setMPkeyword#1 %
442 {\expandafter\def\csname\@@MP\the\nofMPsegments0\endcsname{#1}%
443 \advance\nofMPsegments 1
444 \nofMParguments0}
445
446 \def\getMPkeyword% #1%
447 {\csname\@@MP\the\nofMPsegments0\endcsname} % {\csname\@@MP#10\endcsname}
448
449 \def\docleanupMPargument#1% we need this because args can have [ or ] pre/appended
450 {\expandafter\edef\csname\@@MP\the\nofMPsegments\number#1\endcsname
451 {\csname\@@MP\the\nofMPsegments\number#1\endcsname}}
452
453 %D When we reset the stack, we can assume that all further
454 %D comment is to be ignored and handled in strings.
455 %D By redefining the reset macro after the first call, we
456 %D save some run time. Only use this macro after all
457 %D comments are processed and use the simple alternative
458 %D when dealing with comments.
459
460 \def\doresetMPstack
461 {\nofMParguments0\relax}
462
463 \def\resetMPstack
464 {\let\handleMPgraphic\handleMPendgraphic
465 \let\resetMPstack\doresetMPstack
466 \resetMPstack}
467
468 %D The arguments are saved with the preceding command
469 %D \type{\do}. By default this command expands to nothing, but
470 %D when we deal with strings it's used to strip off the
471 %D \type{(} and \type{)}.
472 %D
473 %D Strings are kind of tricky, because characters can be
474 %D passed verbatim \type{(hello)}, by octal number
475 %D \type{(\005)} or as command \type{(\()}. We therefore
476 %D cannot simply ignore \type{(} and \type{)}, the way we do
477 %D with \type{[} and \type{]}. Another complication is that
478 %D strings may contain characters that normally have a
479 %D special meaning in \TEX, like \type{$} and \type{{}}.
480 %D
481 %D A previous solution made \type{\} an active character and
482 %D let it look ahead for a number or characters. We had to
483 %D abandon this scheme because of the need for verbatim
484 %D support. The next solution involved some \CATCODE\
485 %D trickery but works well.
486
487 \def\octalMPcharacter#1#2#3%
488 {\char'#1#2#3\relax}
489
490 %D curly braces and squarly brackets are stored in the argument stack
491 %D as part of strings, for instance in:
492 %D
493 %D \starttyping
494 %D /fshow {exch findfont exch scalefont setfont show}bind def
495 %D [3 3 ] 0 setdash
496 %D \stoptyping
497 %D
498 %D but we need to keep them in situation like
499 %D
500 %D \starttyping
501 %D ([bla bla] bla bla) ec-lmr10 9.96265 fshow
502 %D ({bla bla} bla bla) ec-lmr10 9.96265 fshow
503 %D \stoptyping
504 %D
505 %D So, when we store the snippets, we keep the special tokens, and
506 %D when needed we either ignore or obey them
507
508 \bgroup
509 \defineMPtoPDFfallbacks
510 \catcode`\|=\@@comment
511 \catcode`\%=\@@active
512 \catcode`\[=\@@active
513 \catcode`\]=\@@active
514 \catcode`\{=\@@active
515 \catcode`\}=\@@active
516 \catcode`B=\@@begingroup
517 \catcode`E=\@@endgroup
518 \gdef\keepMPspecials|
519 B\let%\letterpercent|
520 \def[B\noexpand[E|
521 \def]B\noexpand]E|
522 \def{B\noexpand{E|
523 \def}B\noexpand}EE
524 \gdef\ignoreMPspecials|
525 B\let%\letterpercent|
526 \def[BE|
527 \def]BE|
528 \def{BE|
529 \def}BEE
530 \gdef\obeyMPspecials|
531 B\def%B\char 37\relax E|
532 \def[B\char 91\relax E|
533 \def]B\char 93\relax E|
534 \def{B\char123\relax E|
535 \def}B\char125\relax EE
536 \gdef\setMPspecials|
537 B\setnaturalcatcodes
538 \catcode`\\=\@@escape
539 \catcode`\%=\@@active
540 \catcode`\[=\@@active
541 \catcode`\]=\@@active
542 \catcode`\{=\@@active
543 \catcode`\}=\@@active
544 \lccode`\-=0 | latex sets this to `\-
545 \lccode`\%=`\% | otherwise it's seen as a number
546 \def\(B\char40\relax E|
547 \def\)B\char41\relax E|
548 \def\\B\char92\relax E|
549 \def\0B\octalMPcharacter0E|
550 \def\1B\octalMPcharacter1E|
551 \def\2B\octalMPcharacter2E|
552 \def\3B\octalMPcharacter3E|
553 \def\4B\octalMPcharacter4E|
554 \def\5B\octalMPcharacter5E|
555 \def\6B\octalMPcharacter6E|
556 \def\7B\octalMPcharacter7E|
557 \def\8B\octalMPcharacter8E|
558 \def\9B\octalMPcharacter9EE
559 \egroup
560
561 %D We use the comment symbol as a sort of trigger. Beware!
562 %D The whole graphic is seen as on eparagraph, which means
563 %D that we cannot change the catcodes in between.
564
565 \bgroup
566 \defineMPtoPDFfallbacks
567 \catcode`\%=\@@active
568 \gdef\startMPscanning{\let%=\startMPconversion}
569 \egroup
570
571 %D In earlier versions we used the sequence
572 %D
573 %D \starttyping
574 %D \expandafter\handleMPsequence\input filename\relax
575 %D \stoptyping
576 %D
577 %D Persistent problems in \LATEX\ however forced us to use a
578 %D different scheme. Every \POSTSCRIPT\ file starts with a
579 %D \type{%}, so we temporary make this an active character
580 %D that starts the scanning and redefines itself. (The problem
581 %D originates in the redefinition by \LATEX\ of the
582 %D \type{\input} primitive.)
583
584 \def\startMPconversion
585 {\keepMPspecials
586 \handleMPsequence}
587
588 %D Here comes the main loop. Most arguments are numbers. This
589 %D means that they can be recognized by their \type{\lccode}.
590 %D This method saves a lot of processing time. We could
591 %D speed up the conversion by handling the \type{path}
592 %D seperately.
593
594 \def\dohandleMPsequence#1%
595 {\ifdone
596 \ifcase\lccode`#1\relax
597 \expandafter\expandafter\expandafter\dohandleMPsequenceA
598 \else
599 \expandafter\expandafter\expandafter\dohandleMPsequenceB
600 \fi
601 \else
602 \expandafter\dohandleMPsequenceC
603 \fi#1}
604
605 \let\dohandleMPsequenceA\setMPsequence
606
607 \def\installMPSkeywordN#1#2%
608 {\expandafter\def\csname\@@MP:N:#1\endcsname{#2}}
609
610 \def\installMPSshortcutN#1#2% todo: \let
611 {\expandafter\let\csname\@@MP:N:#1\expandafter\endcsname\csname\@@MP:N:#2\endcsname}
612
613 \def\dohandleMPsequenceB#1 %
614 {\edef\somestring{#1}%
615 \ifcsname\@@MP:N:\somestring\endcsname
616 \csname\@@MP:N:\somestring\expandafter\endcsname
617 \else
618 \expandafter\handleMPgraphic
619 \fi
620 \handleMPsequence}
621
622 \ifx\eTeXversion\undefined
623 \def\dohandleMPsequenceB#1 %
624 {\edef\somestring{#1}%
625 \expandafter\ifx\csname\@@MP:N:\somestring\endcsname\relax
626 \expandafter\handleMPgraphic
627 \else
628 \csname\@@MP:N:\somestring\expandafter\endcsname
629 \fi
630 \handleMPsequence}
631 \fi
632
633 \installMPSkeywordN \PSmoveto
634 {\edef\lastMPmoveX{\gMPa1}%
635 \edef\lastMPmoveY{\gMPa2}%
636 \resetMPstack}
637 \installMPSkeywordN \PSnewpath
638 {\let\handleMPsequence\handleMPpath}
639 \installMPSkeywordN \PSgsave
640 {\PDFcode{q}%
641 \resetMPstack}
642 \installMPSkeywordN \PSgrestore
643 {\PDFcode{Q}%
644 \resetMPstack}
645 \installMPSkeywordN \PSdtransform % == setlinewidth
646 {\let\handleMPsequence\handleMPdtransform}
647 % after that we will encounter more tokens until setlinewidth+pop
648 % or pop+setlinewidth which we catch next; we explicitly need to
649 % reset the stack since [] n setdash may follow; a more clever
650 % approach would be to read on till the condition is met, but it's
651 % the only pop / setlinewidth we will encounter so ...
652 \installMPSkeywordN \PSsetlinewidth
653 {% already handled in dtransform
654 \resetMPstack}
655 \installMPSkeywordN \PSpop
656 {% already handled in dtransform
657 \resetMPstack}
658 \installMPSkeywordN \PSconcat
659 {\cleanupMPconcat
660 \PDFcode{\gMPa1 \gMPa2 \gMPa3 \gMPa4 \gMPa5 \gMPa6 cm}%
661 \resetMPstack}
662 \installMPSkeywordN \PSsetrgbcolor
663 {\handleMPrgbcolor
664 \resetMPstack}
665 \installMPSkeywordN \PSsetcmykcolor
666 {\handleMPcmykcolor
667 \resetMPstack}
668 \installMPSkeywordN \PSsetgray
669 {\handleMPgraycolor
670 \resetMPstack}
671 \installMPSkeywordN \PStranslate
672 {\PDFcode{1 0 0 1 \gMPa1 \gMPa2 cm}%
673 \resetMPstack}
674 \installMPSkeywordN \PSsetdash
675 {\handleMPsetdash
676 \resetMPstack}
677 \installMPSkeywordN \PSsetlinejoin
678 {\PDFcode{\gMPa1 j}%
679 \resetMPstack}
680 \installMPSkeywordN \PSsetmiterlimit
681 {\PDFcode{\gMPa1 M}%
682 \resetMPstack}
683 \installMPSkeywordN \PSfshow
684 {%\PDFcode{n}% removed !
685 \handleMPfshow
686 \resetMPstack}
687 \installMPSkeywordN \PSsetlinecap
688 {\PDFcode{\gMPa1 J}%
689 \resetMPstack}
690 \installMPSkeywordN \PSrlineto
691 {\flushMPmoveto
692 \PDFcode{\!MP\lastMPmoveX\space\!MP\lastMPmoveY\space l S}%
693 \resetMPmoveto
694 \resetMPstack}
695 \installMPSkeywordN \PSscale
696 {\PDFcode{\gMPa1 0 0 \gMPa2 0 0 cm}%
697 \resetMPstack}
698 \installMPSkeywordN \PSspecial
699 {\handleMPspecialcommand
700 \resetMPstack}
701
702 \installMPSshortcutN {n} \PSnewpath
703 \installMPSshortcutN {p} \PSclosepath
704 \installMPSshortcutN {l} \PSlineto
705 \installMPSshortcutN {r} \PSrlineto
706 \installMPSshortcutN {m} \PSmoveto
707 \installMPSshortcutN {c} \PScurveto
708 \installMPSshortcutN {C} \PSsetcmykcolor
709 \installMPSshortcutN {G} \PSsetgray
710 \installMPSshortcutN {R} \PSsetrgbcolor
711 \installMPSshortcutN {lj} \PSsetlinejoin
712 \installMPSshortcutN {ml} \PSsetmiterlimit
713 \installMPSshortcutN {lc} \PSsetlinecap
714 \installMPSshortcutN {sd} \PSsetdash
715 \installMPSshortcutN {S} \PSstroke
716 \installMPSshortcutN {F} \PSfill
717 \installMPSshortcutN {W} \PSclip
718
719 \installMPSshortcutN {q} \PSgsave
720 \installMPSshortcutN {Q} \PSgrestore
721
722 \installMPSshortcutN {s} \PSscale
723 \installMPSshortcutN {t} \PSconcat
724 \installMPSshortcutN {P} \PSshowpage
725
726 \installMPSkeywordN {hlw} {\PDFcode{\gMPa1 w}\resetMPstack}
727 \installMPSkeywordN {vlw} {\PDFcode{\gMPa1 w}\resetMPstack}
728 \installMPSkeywordN {rd} {\PDFcode{[] 0 d}\resetMPstack}
729
730 \def\dohandleMPsequenceC#1 %
731 {\edef\somestring{#1}%
732 \handleMPgraphic % {#1}%
733 \handleMPsequence}
734
735 %D Since colors are not sensitive to transformations, they
736 %D are sometimes used for signaling. Therefore, we handle them
737 %D separately. The next macro can be redefined if needed.
738
739 \def\handleMPrgbcolor
740 {\PDFcode{\!MPgMPa1 \!MPgMPa2 \!MPgMPa3 rg
741 \!MPgMPa1 \!MPgMPa2 \!MPgMPa3 RG}}
742
743 \def\handleMPcmykcolor
744 {\PDFcode{\!MPgMPa1 \!MPgMPa2 \!MPgMPa3 \!MPgMPa4 k
745 \!MPgMPa1 \!MPgMPa2 \!MPgMPa3 \!MPgMPa4 K}}
746
747 \def\handleMPgraycolor
748 {\PDFcode{\!MPgMPa1 g
749 \!MPgMPa1 G}}
750
751 \def\handleMPspotcolor
752 {\PDFcode{0 g
753 0 G}}
754
755 %D Beginning and ending the graphics is taken care of by the
756 %D macro \type{\handleMPgraphic}, which is redefined when
757 %D the first graphics operator is met.
758
759 \def\handleMPendgraphic % #1%
760 {\ifx\somestring\PSshowpage
761 \let\handleMPsequence\finishMPgraphic
762 \else\ifx\somestring\PSEof
763 \let\handleMPsequence\finishMPgraphic
764 \else
765 \letMPargument\somestring % {#1}%
766 \fi\fi}
767
768 \def\handleMPbegingraphic % #1%
769 {\ifx\somestring\PSBoundingBox
770 \def\handleMPsequence{\handleMPboundingbox1}%
771 \else\ifx\somestring\PSHiResBoundingBox
772 \def\handleMPsequence{\handleMPboundingbox2}%
773 \else\ifx\somestring\PSExactBoundingBox
774 \def\handleMPsequence{\handleMPboundingbox3}%
775 \else\ifx\somestring\PSshowpage
776 \let\handleMPsequence\finishMPgraphic
777 \else\ifx\somestring\PSEof
778 \let\handleMPsequence\finishMPgraphic
779 \else\ifx\somestring\PSPage
780 \let\handleMPsequence\handleMPpage
781 \else\ifx\somestring\PSMetaPostSpecials
782 \let\handleMPsequence\handleMPspecialscomment
783 \else\ifx\somestring\PSMetaPostSpecial
784 \let\handleMPsequence\handleMPspecialcomment
785 \else\ifx\somestring\PSBeginProlog
786 \let\handleMPsequence\handleMPprolog
787 \else
788 \letMPargument\somestring % {#1}%
789 \fi\fi\fi\fi\fi\fi\fi\fi\fi}
790
791 \let\handleMPgraphic=\handleMPbegingraphic
792
793 %D New: we can best filter the prolog because nowdays it can contain
794 %D quite some code.
795
796 % hm, catcode mess, so we need to tweak %'s catcode here
797 % \long\expandafter\def\expandafter\handleMPprolog\expandafter#\expandafter1\PSEndProlog%
798 % but today i'm not in the mood for ugly stuff
799
800 \long\def\handleMPprolog#1EndProlog %
801 {\doresetMPstack
802 \let\handleMPsequence\dohandleMPsequence
803 \handleMPsequence}
804
805 %D We check for three kind of bounding boxes: the normal one
806 %D and two high precision ones:
807 %D
808 %D \starttyping
809 %D BoundingBox: llx lly ucx ucy
810 %D HiResBoundingBox: llx lly ucx ucy
811 %D ExactBoundingBox: llx lly ucx ucy
812 %D \stoptyping
813 %D
814 %D The original as well as the recalculated dimensions are
815 %D saved for later use.
816
817 \newif\ifskipemptyMPgraphic \skipemptyMPgraphicfalse
818
819 \chardef\currentMPboundingbox=0
820
821 \def\handleMPboundingbox#1#2 #3 #4 #5
822 {\ifnum#1>\currentMPboundingbox
823 \xdef\MPllx{#2}\xdef\MPlly{#3}%
824 \xdef\MPurx{#4}\xdef\MPury{#5}%
825 \dimen0=#2pt
826 \dimen0=-\MPxscale\dimen0
827 \dimen2=#3pt
828 \dimen2=-\MPyscale\dimen2
829 \xdef\MPxoffset{\withoutpt\the\dimen0}%
830 \xdef\MPyoffset{\withoutpt\the\dimen2}%
831 \dimen0=#2bp
832 \dimen0=-\dimen0
833 \dimen2=#3bp
834 \dimen2=-\dimen2
835 \advance\dimen0 #4bp
836 \dimen0=\MPxscale\dimen0
837 \xdef\MPwidth{\the\dimen0}%
838 \advance\dimen2 #5bp
839 \xdef\MPyshift{\the\dimen2}% unscaled
840 \dimen2=\MPyscale\dimen2
841 \xdef\MPheight{\the\dimen2}%
842 \chardef\currentMPboundingbox#1\relax
843 \fi
844 \doresetMPstack
845 \let\handleMPsequence\dohandleMPsequence
846 \let\next\handleMPsequence
847 \ifskipemptyMPgraphic
848 \ifdim\MPheight=0pt\relax\ifdim\MPwidth=0pt\relax
849 \def\next{\endinput\finishMPgraphic}%
850 \fi\fi
851 \fi
852 \next}
853
854 %D Unless defined otherwise, we simply ignore specialcomments.
855
856 \def\handleMPspecialcomment
857 {\doresetMPstack
858 \let\handleMPsequence\dohandleMPsequence
859 \handleMPsequence}
860
861 \let\handleMPspecialscomment\handleMPspecialcomment
862
863 %D We use the \type{page} comment as a signal that
864 %D stackbuilding can be started.
865
866 \def\handleMPpage #1 #2
867 {\doresetMPstack
868 \donetrue
869 \let\handleMPsequence\dohandleMPsequence
870 \handleMPsequence}
871
872 %D The same applies to the special extensions.
873
874 \def\handleMPspecialcommand
875 {\doresetMPstack
876 \let\handleMPsequence\dohandleMPsequence
877 \handleMPsequence}
878
879 %D \METAPOST\ draws its dots by moving to a location and
880 %D invoking \type{0 0 rlineto}. This operator is not
881 %D available in \PDF. Our solution is straightforward: we draw
882 %D a line from $(current\_x, current\_y)$ to itself. This
883 %D means that the arguments of the preceding \type{moveto} have
884 %D to be saved.
885
886 \def\lastMPmoveX{0}
887 \def\lastMPmoveY{0}
888
889 %D These saved coordinates are also used when we handle the
890 %D texts. Text handling proved to be a bit of a nuisance, but
891 %D finally I saw the light. It proved that we also had to
892 %D take care of \type{(split arguments)}.
893
894 % \def\setMPfshowfont#1#2%
895 % {\font\temp=#1\space at #2\relax\temp}
896
897 % \startMPcode
898 % draw btex Ga toch effe f\kern0ptietsen?{}` etex ;
899 % \stopMPcode
900
901 \newtoks \everyMPshowfont
902
903 \def\setMPfshowfont#1#2%
904 {\font\temp=#1\space at #2\relax\temp
905 \the\everyMPshowfont}
906
907 \let\MPfshowcommand\empty
908
909 %D The next hackery handles characters one by one. We only support this
910 %D for the latest greatest \METAPOST\ binaries, the ones that escape
911 %D problematic chars.
912
913 \def\doflushMPtext#1%
914 {\edef\!!stringa{#1}%
915 \expandafter\dodoflushMPtext\!!stringa\relax}
916
917 \def\dodoflushMPtext
918 {\afterassignment\dododoflushMPtext\let\nexttoken=}
919
920 \def\dododoflushMPtext
921 {\ifx\nexttoken\relax
922 % done
923 \else\ifx\nexttoken\char
924 \expandafter\expandafter\expandafter\dodododoflushMPtext
925 \else
926 {\nexttoken}%
927 \expandafter\expandafter\expandafter\dodoflushMPtext
928 \fi\fi}
929
930 \def\dodododoflushMPtext
931 {\afterassignment\dododododoflushMPtext\scratchcounter}
932
933 \def\dododododoflushMPtext
934 {{\char\scratchcounter}\let\next\dodoflushMPtext}
935
936 \def\dohandleMPfshow
937 {\bgroup
938 \setbox\scratchbox\hbox
939 {\obeyMPspecials
940 \let\ \relax % mp breaks long lines and appends a \
941 \edef\size{\gMPa\nofMParguments}%
942 \ifx\size\PSnfont % round font size (to pt)
943 \advance\nofMParguments -1
944 \expandafter\scratchdimen\gMPa\nofMParguments pt\relax
945 \ifdim\scratchdimen<1pt
946 \def\size{1pt}%
947 \else
948 \advance\scratchdimen .5pt
949 \def\size##1.##2\relax{\def\size{##1pt}}%
950 \expandafter\size\the\scratchdimen\relax
951 \fi
952 \else
953 \edef\size{\size bp}%
954 \fi
955 \advance\nofMParguments -1
956 %\font\temp=\gMPa\nofMParguments\space at \size
957 \let\temp\relax % to be sure
958 \setMPfshowfont{\gMPa\nofMParguments}\size
959 \advance\nofMParguments -1
960 \temp
961 \MPfshowcommand
962 {\ifnum\nofMParguments=1
963 \def\do(##1){##1}%
964 \doflushMPtext{\dogMPa1}% only latest mp gets this treatment
965 \else
966 % we need to catch ( a ) (a a a) (\123 \123 \123) etc
967 \scratchcounter1
968 \def\dodo##1% Andreas Fieger's bug: (\304...)
969 {\edef\!!stringa{##1\empty\empty}% and another one: ( 11) -> \ifx 11
970 \ifx\!!stringa\MPspacechar\MPspacechar\else\expandafter##1\fi}%
971 \def\do(##1{\dodo{##1}}%
972 \dogMPa\scratchcounter\MPspacechar
973 \let\do\relax
974 \loop
975 \advance\scratchcounter 1
976 \ifnum\scratchcounter<\nofMParguments\relax
977 \gMPa\scratchcounter\MPspacechar
978 \repeat
979 \def\do##1){\dodo{##1}}%
980 \dogMPa\scratchcounter
981 \fi
982 \unskip}}%
983 \setbox\scratchbox\hbox
984 {\hskip\lastMPmoveX bp\raise\lastMPmoveY bp\box\scratchbox}%
985 \ht\scratchbox0pt%
986 \dp\scratchbox0pt%
987 \wd\scratchbox0pt%
988 \box\scratchbox
989 \egroup}
990
991 \let\handleMPfshow\dohandleMPfshow % so we can overload this one later
992
993 %D You could consider the following definition to be the most
994 %D natural one.
995
996 % \def\MPspacechar{\space} % normal case
997
998 \def\MPspacechar{\char32\relax} % old solution does not work with math
999
1000 %D However, the following implementation is more robust, since
1001 %D some fonts have funny visible spaces in the space slot. This
1002 %D gives a mismatch between the space that \METAPOST\ took into
1003 %D account and the \quote {natural} space. This only happens in
1004 %D labels, since \type {btex}||\type {etex} thingies don't have
1005 %D spaces. This phenomena showed up when preparing the
1006 %D \METAFUN\ manual, where Palatino fonts are used. We can
1007 %D safely assume that \METAPOST\ considers \type {\char32} to
1008 %D be the space.
1009
1010 \def\MPspacechar{\setbox\scratchbox\hbox{\char32}\kern\wd\scratchbox}
1011
1012 %D Well, this does not work with math fonts, so:
1013
1014 \def\MPspacechar{\char32\relax}
1015
1016 %D Most operators are just converted and keep their
1017 %D arguments. Dashes however need a bit different treatment,
1018 %D otherwise \PDF\ viewers complain loudly. Another
1019 %D complication is that one argument comes after the \type{]}.
1020 %D When reading the data, we simply ignore the array boundary
1021 %D characters. We save ourselves some redundant newlines and
1022 %D at the same time keep the output readable by packing the
1023 %D literals.
1024
1025 \def\handleMPsetdash
1026 {\bgroup
1027 \ignoreMPspecials
1028 \def\somestring{[}%
1029 \scratchcounter1
1030 \loop
1031 \ifnum\scratchcounter<\nofMParguments
1032 \edef\somestring{\somestring\space\gMPa\scratchcounter}%
1033 \advance\scratchcounter 1
1034 \repeat
1035 \edef\somestring{\somestring]\gMPa\scratchcounter\space d}%
1036 \PDFcode{\somestring}%
1037 \egroup}
1038
1039 %D The \type{setlinewidth} commands looks a bit complicated. There are
1040 %D two alternatives, that result in a similar look in both
1041 %D $x$- and $y$-dorection. As John Hobby says:
1042 %D
1043 %D \startnarrower \switchtobodyfont[ss]
1044 %D \starttyping
1045 %D x 0 dtransform exch truncate exch idtransform pop setlinewidth
1046 %D 0 y dtransform truncate idtransform setlinewidth pop
1047 %D \stoptyping
1048 %D
1049 %D These are just fancy versions of \type{x setlinewidth} and
1050 %D \type{y setlinewidth}. The \type{x 0 ...} form is used if
1051 %D the path is {\em primarily vertical}. It rounds the width
1052 %D so that vertical lines come out an integer number of pixels
1053 %D wide in device space. The \type{0 y ...} form does the same
1054 %D for paths that are {\em primarily horizontal}. The reason
1055 %D why I did this is Knuth insists on getting exactly the
1056 %D widths \TEX\ intends for the horizontal and vertical rules
1057 %D in \type{btex...etex} output. (Note that PostScript scan
1058 %D conversion rules cause a horizontal or vertical line of
1059 %D integer width $n$ in device space to come out $n+1$ pixels
1060 %D wide, regardless of the phase relative to the pixel grid.)
1061 %D \stopnarrower
1062 %D
1063 %D The common operator in these sequences is \type{dtransform},
1064 %D so we can use this one to trigger setting the linewidth.
1065
1066 \def\handleMPdtransform
1067 {\ifdim\gMPa1 pt>0pt
1068 \PDFcode{\gMPa1 w}%
1069 \def\next##1 ##2 ##3 ##4 ##5 ##6 {\handleMPsequence}%
1070 \else
1071 \PDFcode{\gMPa2 w}%
1072 \def\next##1 ##2 ##3 ##4 {\handleMPsequence}%
1073 \fi
1074 \let\handleMPsequence\dohandleMPsequence
1075 \resetMPstack
1076 \next}
1077
1078 %D The most complicated command is \type{concat}. \METAPOST\
1079 %D applies this operator to \type{stroke}. At that moment the
1080 %D points set by \type{curveto} and \type{moveto}, are already
1081 %D fixed. In \PDF\ however the \type{cm} operator affects the
1082 %D points as well as the pen (stroke). Like more \PDF\
1083 %D operators, \type{cm} is defined in a bit ambiguous way.
1084 %D The only save route for non||circular penshapes, is saving
1085 %D the path, recalculating the points and applying the
1086 %D transformation matrix in such a way that we can be sure
1087 %D that its behavior is well defined. This comes down to
1088 %D inverting the path and applying \type{cm} to that path as
1089 %D well as the pen. This all means that we have to save the
1090 %D path.
1091
1092 %D In \METAPOST\ there are three ways to handle a path $p$:
1093 %D
1094 %D \starttyping
1095 %D draw p; fill p; filldraw p;
1096 %D \stoptyping
1097 %D
1098 %D The last case outputs a \type{gsave fill grestore} before
1099 %D \type{stroke}. Handling the path outside the main loops
1100 %D saves about 40\% run time.\footnote{We can save some more by
1101 %D following the \METAPOST\ output routine, but for the moment
1102 %D we keep things simple.} Switching between the main loop and
1103 %D the path loop is done by means of the recursely called
1104 %D macro \type{\handleMPsequence}.
1105
1106 \def\handleMPpath
1107 {\chardef\finiMPpath0
1108 \let\closeMPpath\relax
1109 \let\flushMPpath\flushnormalMPpath
1110 \resetMPstack
1111 \nofMPsegments1
1112 \let\handleMPsequence\dohandleMPpath
1113 \dohandleMPpath}
1114
1115 %D Most paths are drawn with simple round pens. Therefore we've
1116 %D split up the routine in two.
1117
1118 \def\flushnormalMPsegment
1119 {\ifcase\getMPkeyword\relax
1120 \PDFcode{\!MPgMPs1 \!MPgMPs2 l}%
1121 \or
1122 \PDFcode{\!MPgMPs1 \!MPgMPs2 \!MPgMPs3 \!MPgMPs4 \!MPgMPs5 \!MPgMPs6 c}%
1123 \or
1124 \PDFcode{\!MP\lastMPmoveX\space\!MP\lastMPmoveY\space l}%
1125 \or
1126 \edef\lastMPmoveX{\gMPs1}% evt \!MP here
1127 \edef\lastMPmoveY{\gMPs2}%
1128 \PDFcode{\!MP\lastMPmoveX\space \!MP\lastMPmoveY\space m}%
1129 \fi}
1130
1131 \def\flushconcatMPsegment
1132 {\ifcase\getMPkeyword\relax
1133 \doMPconcat{\gMPs1}\a{\gMPs2}\b%
1134 \PDFcode{\!MP\a\space\!MP\b\space l}%
1135 \or
1136 \doMPconcat{\gMPs1}\a{\gMPs2}\b%
1137 \doMPconcat{\gMPs3}\c{\gMPs4}\d%
1138 \doMPconcat{\gMPs5}\e{\gMPs6}\f%
1139 \PDFcode{\!MP\a\space\!MP\b\space
1140 \!MP\c\space\!MP\d\space
1141 \!MP\e\space\!MP\f\space c}%
1142 \or
1143 \bgroup
1144 \noMPtranslate
1145 \doMPconcat\lastMPmoveX\a\lastMPmoveY\b%
1146 \PDFcode{\!MP\a\space\!MP\b\space l S}%
1147 \egroup
1148 \or
1149 \edef\lastMPmoveX{\gMPs1}%
1150 \edef\lastMPmoveY{\gMPs2}%
1151 \doMPconcat\lastMPmoveX\a\lastMPmoveY\b%
1152 \PDFcode{\!MP\a\space\!MP\b\space m}%
1153 \fi}
1154
1155 % \def\flushnormalMPpath
1156 % {\scratchcounter\nofMPsegments
1157 % \nofMPsegments 1
1158 % \loop
1159 % \flushnormalMPsegment
1160 % \advance\nofMPsegments 1
1161 % \ifnum\nofMPsegments<\scratchcounter
1162 % \repeat}
1163 %
1164 % \def\flushconcatMPpath
1165 % {\scratchcounter\nofMPsegments
1166 % \nofMPsegments 1
1167 % \loop
1168 % \flushconcatMPsegment
1169 % \advance\nofMPsegments 1
1170 % \ifnum\nofMPsegments<\scratchcounter
1171 % \repeat}
1172 %
1173 % an alternative is presented below: (no \def assignment)
1174
1175 \def\doflushsomeMPpath
1176 {\dodoflushsomeMPpath
1177 \advance\nofMPsegments 1
1178 \ifnum\nofMPsegments<\scratchcounter
1179 \expandafter\doflushsomeMPpath
1180 \fi}
1181
1182 \def\flushsomeMPpath
1183 {\scratchcounter\nofMPsegments
1184 \nofMPsegments 1
1185 \doflushsomeMPpath}
1186
1187 \def\flushnormalMPpath{\let\dodoflushsomeMPpath\flushnormalMPsegment\flushsomeMPpath}
1188
1189 %OLD \def\flushconcatMPpath{\let\dodoflushsomeMPpath\flushconcatMPsegment\flushsomeMPpath}
1190
1191 %NEW pre-calculate 1/D so it needn't be repeated for each control point.
1192
1193 \def\flushconcatMPpath
1194 {\MPreciprocaldeterminant
1195 \let\dodoflushsomeMPpath\flushconcatMPsegment\flushsomeMPpath}
1196
1197 %D The transformation of the coordinates is handled by one of
1198 %D the macros Tanmoy posted to the \PDFTEX\ mailing list.
1199 %D I rewrote and optimized the original macro to suit the other
1200 %D macros in this module.
1201 %D
1202 %D \starttyping
1203 %D \doMPconcat {x position} \xresult {y position} \yresult
1204 %D \stoptyping
1205 %D
1206 %D By setting the auxiliary \DIMENSIONS\ \type{\dimen0} upto
1207 %D \type{\dimen10} only once per path, we save over 20\% run
1208 %D time. Some more speed was gained by removing some parameter
1209 %D passing. These macros can be optimized a bit more by using
1210 %D more constants. There is however not much need for further
1211 %D optimization because penshapes usually are round and
1212 %D therefore need no transformation. Nevertheless we move the
1213 %D factor to the outer level and use a bit different \type{pt}
1214 %D removal macro. Although the values represent base points,
1215 %D we converted them to pure points, simply because those can
1216 %D be converted back.
1217
1218 %OLD \mathchardef\MPconcatfactor=256 % beware don't remove spaces before it
1219
1220 %OLD \def\doMPreducedimen#1
1221 %OLD {\count0\MPconcatfactor
1222 %OLD \advance\dimen#1 \ifdim\dimen#1>0pt .5\else -.5\fi\count0
1223 %OLD \divide\dimen#1 \count0\relax}
1224
1225 %OLD % too inaccurate (see old pragma logo)
1226 %OLD
1227 %OLD \def\doMPreducedimen#1
1228 %OLD {\count0=\MPconcatfactor
1229 %OLD \divide\dimen#1 \count0\relax}
1230
1231 %OLD \def\doMPreducedimen#1
1232 %OLD {\advance\dimen#1 \ifdim\dimen#1>0pt .5\else -.5\fi\MPconcatfactor
1233 %OLD \divide\dimen#1 \MPconcatfactor}
1234
1235 %D The transformation code is rewritten by Daniel H. Luecking who
1236 %D describes his patch as follows:
1237 %D
1238 %D We would like to divide 1 by $X$, but all divisions are integer so
1239 %D for accuracy we want to convert to large integers and make sure the
1240 %D integer quotient has as many significant digits as possible. Thus we
1241 %D need to replace $1/X$ with $M/N$ where $N$ is as large as possible
1242 %D and $M/N$ is as large as possible. Also for simplicity $M$ should be
1243 %D a power of 2. So we make $M = 2^{30}$ \footnote{$2^{31} - 1$ is the
1244 %D largest legal integer. Using it (and simply ignoring the inaccuracy
1245 %D caused by $-1$) turns out to be at least as accurate in all cases,
1246 %D and more accurate in some.} (largest legal power of 2) and adjust
1247 %D $X$ downward (if necessary) to the the range $1-2^{16}$. This gives
1248 %D at least 15 significant binary digits, (almost as accurate as
1249 %D \METAPOST\ for numbers near 1) or almost 5 significant figures
1250 %D (decimal).
1251
1252 \newcount\MPscratchCnt
1253 \newdimen\MPscratchDim % will be assigned global
1254
1255 \def\MPadjustdimen % sets \MPscratchDim and \MPscratchCnt
1256 {\MPscratchCnt0
1257 \doMPadjustdimen}
1258
1259 \def\doMPadjustdimen
1260 {\ifdim\MPscratchDim>1pt
1261 \divide\MPscratchDim 2
1262 \advance\MPscratchCnt 1
1263 \expandafter\doMPadjustdimen
1264 \fi}
1265
1266 %OLD \def\doMPexpanddimen#1
1267 %OLD {\multiply\dimen#1 \MPconcatfactor\relax}
1268
1269 %D DHL: When viewed as an integer, $1 \hbox{pt}=2^{16}$ so $2^{32}/X$
1270 %D is the right way to do $(1 \hbox{pt})/(X \hbox{pt})$ and get the
1271 %D answer in points. But we are limited to $2^{30}/X$. However, we
1272 %D actually do $[ 2^{30} / (X/2^K) ]*2^{2-K}$ where $K$ is the number
1273 %D of halvings it takes to bring $X$ below $1 \hbox{pt}$. If $K$ is 0
1274 %D or 1 we readjust by multiplying by 4 or 2, otherwise by halving
1275 %D $(K-2)$ times \type {\MPscratchCnt} holds the value of $K$ from
1276 %D \type {\MPadjustdimen}.
1277
1278 \def\MPreadjustdimen % acts on \MPscratchDim and MPscratchCnt
1279 {\ifcase\MPscratchCnt
1280 \multiply\scratchdimen 4
1281 \or
1282 \multiply\scratchdimen 2
1283 \else
1284 \expandafter\doMPreadjustdimen
1285 \fi}
1286
1287 \def\doMPreadjustdimen
1288 {\ifnum\MPscratchCnt>2
1289 \divide\scratchdimen 2
1290 \advance\MPscratchCnt -1
1291 \expandafter\doMPreadjustdimen
1292 \fi}
1293
1294 \def\MPreciprocaldeterminant
1295 {\scratchdimen\withoutpt\the\dimen0 \dimen6 % s_x*s_y
1296 \advance\scratchdimen - \withoutpt\the\dimen2 \dimen4 % s_x*s_y - r_x*r_y
1297 \ifdim\scratchdimen<0pt % we need a positive dimension
1298 \scratchdimen-\scratchdimen % for \MPadjustdimen (?)
1299 \doMPreciprocal
1300 \scratchdimen-\scratchdimen
1301 \else
1302 \doMPreciprocal
1303 \fi
1304 \edef\MPreciprocal{\withoutpt\the\scratchdimen}}
1305
1306 \newcount\MPnumerator \MPnumerator = 1073741824 % 2^{30}
1307
1308 % todo: dimexpr
1309
1310 \def\doMPreciprocal % replace \scratchdimen with its reciprocal
1311 {\ifdim\scratchdimen=1pt \else
1312 \MPadjustdimen
1313 \scratchcounter\MPnumerator
1314 \divide\scratchcounter\scratchdimen
1315 \scratchdimen1\scratchcounter % 1 needed !
1316 \MPreadjustdimen
1317 \fi}
1318
1319 %OLD \def\presetMPconcat
1320 %OLD {\dimen 0=\gMPs1pt \doMPreducedimen 0 % r_x
1321 %OLD \dimen 2=\gMPs2pt \doMPreducedimen 2 % s_x
1322 %OLD \dimen 4=\gMPs3pt \doMPreducedimen 4 % s_y
1323 %OLD \dimen 6=\gMPs4pt \doMPreducedimen 6 % r_y
1324 %OLD \dimen 8=\gMPs5pt \doMPreducedimen 8 % t_x
1325 %OLD \dimen10=\gMPs6pt \doMPreducedimen10 } % t_y
1326 %OLD
1327 %OLD \def\presetMPscale
1328 %OLD {\dimen 0=\gMPs1pt \doMPreducedimen 0
1329 %OLD \dimen 2=0pt
1330 %OLD \dimen 4=0pt
1331 %OLD \dimen 6=\gMPs2pt \doMPreducedimen 6
1332 %OLD \dimen 8=0pt
1333 %OLD \dimen10=0pt}
1334
1335 \def\cleanupMPconcat
1336 {\ignoreMPspecials
1337 \docleanupMPargument1%
1338 \docleanupMPargument6%
1339 \keepMPspecials}
1340
1341 \def\presetMPconcat
1342 {\dimen 0=\gMPs1pt % s_x
1343 \dimen 2=\gMPs2pt % r_x
1344 \dimen 4=\gMPs3pt % r_y
1345 \dimen 6=\gMPs4pt % s_y
1346 \dimen 8=\gMPs5pt % t_x
1347 \dimen10=\gMPs6pt} % t_y
1348
1349 \def\presetMPscale
1350 {\dimen 0=\gMPs1pt
1351 \dimen 2=0pt
1352 \dimen 4=0pt
1353 \dimen 6=\gMPs2pt
1354 \dimen 8=0pt
1355 \dimen10=0pt}
1356
1357 \def\noMPtranslate % use this one grouped
1358 {\dimen 8=0pt % t_x
1359 \dimen10=0pt} % t_y
1360
1361 %D \starttyping
1362 %D \def\doMPconcat#1#2#3#4%
1363 %D {\dimen12=#1 pt \doMPreducedimen12 % p_x
1364 %D \dimen14=#3 pt \doMPreducedimen14 % p_y
1365 %D %
1366 %D \dimen16 \dimen 0
1367 %D \multiply \dimen16 \dimen 6
1368 %D \dimen20 \dimen 2
1369 %D \multiply \dimen20 \dimen 4
1370 %D \advance \dimen16 -\dimen20
1371 %D %
1372 %D \dimen18 \dimen12
1373 %D \multiply \dimen18 \dimen 6
1374 %D \dimen20 \dimen14
1375 %D \multiply \dimen20 \dimen 4
1376 %D \advance \dimen18 -\dimen20
1377 %D \dimen20 \dimen 4
1378 %D \multiply \dimen20 \dimen10
1379 %D \advance \dimen18 \dimen20
1380 %D \dimen20 \dimen 6
1381 %D \multiply \dimen20 \dimen 8
1382 %D \advance \dimen18 -\dimen20
1383 %D %
1384 %D \multiply \dimen12 -\dimen 2
1385 %D \multiply \dimen14 \dimen 0
1386 %D \advance \dimen12 \dimen14
1387 %D \dimen20 \dimen 2
1388 %D \multiply \dimen20 \dimen 8
1389 %D \advance \dimen12 \dimen20
1390 %D \dimen20 \dimen 0
1391 %D \multiply \dimen20 \dimen10
1392 %D \advance \dimen12 -\dimen20
1393 %D %
1394 %D \doMPreducedimen16
1395 %D \divide \dimen18 \dimen16 \doMPexpanddimen18
1396 %D \divide \dimen12 \dimen16 \doMPexpanddimen12
1397 %D %
1398 %D \edef#2{\withoutpt\the\dimen18}% % p_x^\prime
1399 %D \edef#4{\withoutpt\the\dimen12}} % p_y^\prime
1400 %D \stoptyping
1401
1402 %D The following optimization resulted from some tests by
1403 %D and email exchanges with Sanjoy Mahajan.
1404 %D
1405 %D \starttyping
1406 %D \def\doMPconcat#1#2#3#4%
1407 %D {\dimen12=#1 pt \doMPreducedimen12 % p_x
1408 %D \dimen14=#3 pt \doMPreducedimen14 % p_y
1409 %D %
1410 %D \dimen16 \dimen 0
1411 %D \multiply \dimen16 \dimen 6
1412 %D \dimen20 \dimen 2
1413 %D \multiply \dimen20 \dimen 4
1414 %D \advance \dimen16 -\dimen20
1415 %D %
1416 %D \dimen18 \dimen12
1417 %D \multiply \dimen18 \dimen 6
1418 %D \dimen20 \dimen14
1419 %D \multiply \dimen20 \dimen 4
1420 %D \advance \dimen18 -\dimen20
1421 %D \dimen20 \dimen 4
1422 %D \multiply \dimen20 \dimen10
1423 %D \advance \dimen18 \dimen20
1424 %D \dimen20 \dimen 6
1425 %D \multiply \dimen20 \dimen 8
1426 %D \advance \dimen18 -\dimen20
1427 %D %
1428 %D \multiply \dimen12 -\dimen 2
1429 %D \multiply \dimen14 \dimen 0
1430 %D \advance \dimen12 \dimen14
1431 %D \dimen20 \dimen 2
1432 %D \multiply \dimen20 \dimen 8
1433 %D \advance \dimen12 \dimen20
1434 %D \dimen20 \dimen 0
1435 %D \multiply \dimen20 \dimen10
1436 %D \advance \dimen12 -\dimen20
1437 %D %
1438 %D %\ifdim\dimen16>1pt % oeps, can be < 1pt too
1439 %D \ifdim\dimen16=1pt \else
1440 %D \ifdim\dimen16>\MPconcatfactor pt
1441 %D \doMPreducedimen16
1442 %D \divide \dimen18 \dimen16 \doMPexpanddimen18
1443 %D \divide \dimen12 \dimen16 \doMPexpanddimen12
1444 %D \else
1445 %D \divide \dimen18 \dimen16 \doMPexpanddimen18 \doMPexpanddimen18
1446 %D \divide \dimen12 \dimen16 \doMPexpanddimen12 \doMPexpanddimen12
1447 %D \fi
1448 %D \fi
1449 %D %
1450 %D \edef#2{\withoutpt\the\dimen18}% % p_x^\prime
1451 %D \edef#4{\withoutpt\the\dimen12}} % p_y^\prime
1452 %D \stoptyping
1453 %D
1454 %D But, this one is still too inaccurate, so we now have:
1455
1456 % \def\doMPconcat#1#2#3#4%
1457 % {\dimen12=#1pt % p_x
1458 % \dimen14=#3pt % p_y
1459 % %
1460 % % we should test for >-1024 too, but for the moment take the gamble
1461 % \chardef\MPfactor1\ifdim\dimen12<1024pt \ifdim\dimen14<1024pt 6\fi\fi
1462 % %
1463 % \multiply\dimen12 \MPfactor
1464 % \multiply\dimen14 \MPfactor
1465 % %
1466 % \doMPreducedimen12
1467 % \doMPreducedimen14
1468 % %
1469 % \dimen16 \dimen 0
1470 % \multiply \dimen16 \dimen 6
1471 % \dimen20 \dimen 2
1472 % \multiply \dimen20 \dimen 4
1473 % \advance \dimen16 -\dimen20
1474 % %
1475 % \dimen18 \dimen12
1476 % \multiply \dimen18 \dimen 6
1477 % \dimen20 \dimen14
1478 % \multiply \dimen20 \dimen 4
1479 % \advance \dimen18 -\dimen20
1480 % \dimen20 \dimen 4
1481 % \multiply \dimen20 \dimen10
1482 % \advance \dimen18 \dimen20
1483 % \dimen20 \dimen 6
1484 % \multiply \dimen20 \dimen 8
1485 % \advance \dimen18 -\dimen20
1486 % %
1487 % \multiply \dimen12 -\dimen 2
1488 % \multiply \dimen14 \dimen 0
1489 % \advance \dimen12 \dimen14
1490 % \dimen20 \dimen 2
1491 % \multiply \dimen20 \dimen 8
1492 % \advance \dimen12 \dimen20
1493 % \dimen20 \dimen 0
1494 % \multiply \dimen20 \dimen10
1495 % \advance \dimen12 -\dimen20
1496 % %
1497 % \ifdim\dimen16=1pt \else
1498 % \ifdim\dimen16>\MPconcatfactor pt
1499 % \doMPreducedimen16
1500 % \divide \dimen18 \dimen16 \doMPexpanddimen18
1501 % \divide \dimen12 \dimen16 \doMPexpanddimen12
1502 % \else
1503 % \divide \dimen18 \dimen16 \doMPexpanddimen18 \doMPexpanddimen18
1504 % \divide \dimen12 \dimen16 \doMPexpanddimen12 \doMPexpanddimen12
1505 % \fi
1506 % \fi
1507 % %
1508 % \divide\dimen18 \MPfactor
1509 % \divide\dimen12 \MPfactor
1510 % %
1511 % \edef#2{\withoutpt\the\dimen18}% % p_x^\prime
1512 % \edef#4{\withoutpt\the\dimen12}} % p_y^\prime
1513
1514 %D DHL: Ideally, $r_x$, $r_y$, $s_x$, $s_y$ should be in macros, not
1515 %D dimensions (they are scalar quantities after all, not lengths). I
1516 %D suppose the authors decided to do calculations with integer
1517 %D arithmetic instead of using real factors because it's faster.
1518 %D However, the actual macros test slower, possibly because I've
1519 %D omitted three nested loops. In my test files, my approach is more
1520 %D accurate. It is also far simpler and overflow does not seem to be a
1521 %D significant concern. The scale factors written by Metapost are (?)
1522 %D always $<=1$ (it scales coordinates internally) and coordinates are
1523 %D always likely to be less than \type {\maxdimen}.
1524 %D
1525 %D If this should ever cause problems, the scale factors can be reduced.
1526
1527 % \def\doMPconcat#1#2#3#4%
1528 % {\dimen12=#1pt % p_x % #1pt
1529 % \dimen14=#3pt % p_y % #3pt
1530 % \advance\dimen12 -\dimen8 % p_x - t_x
1531 % \advance\dimen14 -\dimen10 % p_y - t_y
1532 % \dimen18=\withoutpt\the\dimen6 \dimen12 % s_y(p_x - t_x)
1533 % \advance\dimen18 -\withoutpt\the\dimen4 \dimen14 % - r_y(p_y-t_y)
1534 % \dimen14=\withoutpt\the\dimen0 \dimen14 % s_x(p_y-t_y)
1535 % \advance\dimen14 -\withoutpt\the\dimen2 \dimen12 % - r_x(p_x-t_x)
1536 % % \MPscratchDim contains precomputed 1/D:
1537 % \dimen18=\withoutpt\the\MPscratchDim \dimen18
1538 % \dimen14=\withoutpt\the\MPscratchDim \dimen14
1539 % \edef#2{\withoutpt\the\dimen18}% % p_x^\prime
1540 % \edef#4{\withoutpt\the\dimen14}} % p_y^\prime
1541
1542 \def\doMPconcat#1#2#3#4%
1543 {\dimen12=#1pt % p_x % #1pt
1544 \dimen14=#3pt % p_y % #3pt
1545 \advance\dimen12 -\dimen8 % p_x - t_x
1546 \advance\dimen14 -\dimen10 % p_y - t_y
1547 \dimen18=\withoutpt\the\dimen6 \dimen12 % s_y(p_x - t_x)
1548 \advance\dimen18 -\withoutpt\the\dimen4 \dimen14 % - r_y(p_y-t_y)
1549 \dimen14=\withoutpt\the\dimen0 \dimen14 % s_x(p_y-t_y)
1550 \advance\dimen14 -\withoutpt\the\dimen2 \dimen12 % - r_x(p_x-t_x)
1551 % \MPreciprocal contains precomputed 1/D:
1552 \dimen18=\MPreciprocal\dimen18
1553 \dimen14=\MPreciprocal\dimen14
1554 \edef#2{\withoutpt\the\dimen18}% % p_x^\prime
1555 \edef#4{\withoutpt\the\dimen14}} % p_y^\prime
1556
1557 % faster but not that often used
1558 %
1559 % \def\doMPconcat#1#2#3#4%
1560 % {\dimen12\dimexpr#1\points-\dimen 8\relax % p_x-t_x
1561 % \dimen14\dimexpr#3\points-\dimen10\relax % p_y-t_y
1562 % \dimen18\dimexpr\withoutpt\the\dimen6\dimen12-\withoutpt\the\dimen4\dimen14\relax % s_y(p_x-t_x)-r_y(p_y-t_y)
1563 % \dimen14\dimexpr\withoutpt\the\dimen0\dimen14-\withoutpt\the\dimen2\dimen12\relax % s_x(p_y-t_y)-r_x(p_x-t_x)
1564 % \edef#2{\withoutpt\the\dimexpr\MPreciprocal\dimen18\relax}% % p_x^\prime
1565 % \edef#4{\withoutpt\the\dimexpr\MPreciprocal\dimen14\relax}} % p_y^\prime
1566
1567 %D One reason for Daniel to write this patch was that at small sizes
1568 %D the accuracy was less than optimal. Here is a test that demonstrates
1569 %D that his alternative is pretty good:
1570 %D
1571 %D \startlinecorrection
1572 %D \startMPcode
1573 %D for i = 5cm,1cm,5mm,1mm,.5mm,.1mm,.01mm :
1574 %D draw fullcircle scaled i withpen pencircle xscaled (i/10) yscaled (i/20) rotated 45 ;
1575 %D endfor ;
1576 %D \stopMPcode
1577 %D \stoplinecorrection
1578
1579 %D The following explanation of the conversion process was
1580 %D posted to the \PDFTEX\ mailing list by Tanmoy. The original
1581 %D macro was part of a set of macro's that included sinus and
1582 %D cosinus calculations as well as scaling and translating. The
1583 %D \METAPOST\ to \PDF\ conversion however only needs
1584 %D transformation.
1585
1586 %M \start \switchtobodyfont [ss]
1587
1588 %D Given a point $(U_x, U_y)$ in user coordinates, the business
1589 %D of \POSTSCRIPT\ is to convert it to device space. Let us say
1590 %D that the device space coordinates are $(D_x, D_y)$. Then, in
1591 %D \POSTSCRIPT\ $(D_x, D_y)$ can be written in terms of
1592 %D $(U_x, U_y)$ in matrix notation, either as
1593 %D
1594 %D \placeformula
1595 %D \startformula
1596 %D \pmatrix{D_x&D_y&1\cr} = \pmatrix{U_x&U_y&1\cr}
1597 %D \pmatrix{s_x&r_x&0\cr
1598 %D r_y&s_y&0\cr
1599 %D t_x&t_y&1\cr}
1600 %D \stopformula
1601 %D
1602 %D or
1603 %D
1604 %D \placeformula
1605 %D \startformula
1606 %D \pmatrix{D_x\cr D_y\cr 1} = \pmatrix{s_x&r_y&t_x\cr
1607 %D r_x&s_y&t_y\cr
1608 %D 0 &0 &1 \cr}
1609 %D \pmatrix{U_x\cr
1610 %D U_y\cr
1611 %D 1 \cr}
1612 %D \stopformula
1613 %D
1614 %D both of which is a shorthand for the same set of equations:
1615 %D
1616 %D \placeformula
1617 %D \startformula
1618 %D D_x = s_x U_x + r_y U_y + t_x
1619 %D \stopformula
1620 %D
1621 %D \placeformula
1622 %D \startformula
1623 %D D_y = r_x U_x + s_y U_y + t_y
1624 %D \stopformula
1625 %D
1626 %D which define what is called an `affine transformation'.
1627 %D
1628 %D \POSTSCRIPT\ represents the `transformation matrix' as a
1629 %D six element matrix instead of a $3\times 3$ array because
1630 %D three of the elements are always~0, 0 and~1. Thus the above
1631 %D transformation is written in postscript as $[s_x\, r_x\,
1632 %D r_y\, s_y\, t_x\, t_y]$. However, when doing any
1633 %D calculations, it is useful to go back to the original
1634 %D matrix notation (whichever: I will use the second) and
1635 %D continue from there.
1636 %D
1637 %D As an example, if the current transformation matrix is
1638 %D $[s_x\, r_x\, r_y\, s_y\, t_x\, t_y]$ and you say \typ{[a b
1639 %D c d e f] concat}, this means:
1640 %D
1641 %D \startnarrower
1642 %D Take the user space coordinates and transform them to an
1643 %D intermediate set of coordinates using array $[a\, b\, c\, d\,
1644 %D e\, f]$ as the transformation matrix.
1645 %D
1646 %D Take the intermediate set of coordinates and change them to
1647 %D device coordinates using array $[s_x\, r_x\, r_y\, s_y\, t_x\, t_y]$
1648 %D as the transformation matrix.
1649 %D \stopnarrower
1650 %D
1651 %D Well, what is the net effect? In matrix notation, it is
1652 %D
1653 %D \placeformula
1654 %D \startformula
1655 %D \pmatrix{I_x\cr I_y\cr 1\cr} = \pmatrix{a&c&e\cr
1656 %D b&d&f\cr
1657 %D 0&0&1\cr}
1658 %D \pmatrix{U_x\cr
1659 %D U_y\cr
1660 %D 1 \cr}
1661 %D \stopformula
1662 %D
1663 %D \placeformula
1664 %D \startformula
1665 %D \pmatrix{D_y\cr D_y\cr 1\cr} = \pmatrix{s_x&r_y&t_x\cr
1666 %D r_x&s_y&t_y\cr
1667 %D 0 &0 &1 \cr}
1668 %D \pmatrix{I_x\cr
1669 %D I_y\cr
1670 %D 1 \cr}
1671 %D \stopformula
1672 %D
1673 %D where $(I_x, I_y)$ is the intermediate coordinate.
1674 %D
1675 %D Now, the beauty of the matrix notation is that when there is
1676 %D a chain of such matrix equations, one can always compose
1677 %D them into one matrix equation using the standard matrix
1678 %D composition law. The composite matrix from two matrices can
1679 %D be derived very easily: the element in the $i$\high{th}
1680 %D horizontal row and $j$\high{th} vertical column is
1681 %D calculated by`multiplying' the $i$\high{th} row of the first
1682 %D matrix and the $j$\high{th} column of the second matrix (and
1683 %D summing over the elements). Thus, in the above:
1684 %D
1685 %D \placeformula
1686 %D \startformula
1687 %D \pmatrix{D_x\cr D_y\cr 1} = \pmatrix{s_x^\prime&r_y^\prime&t_x^\prime\cr
1688 %D r_x^\prime&s_y^\prime&t_y^\prime\cr
1689 %D 0 &0 &0 \cr}
1690 %D \pmatrix{U_x\cr
1691 %D U_y\cr
1692 %D 1 \cr}
1693 %D \stopformula
1694 %D
1695 %D with
1696 %D
1697 %D \placeformula
1698 %D \startformula
1699 %D \eqalign
1700 %D {s_x^\prime & = s_x a + r_y b \cr
1701 %D r_x^\prime & = r_x a + s_y b \cr
1702 %D r_y^\prime & = s_x c + r_y d \cr
1703 %D s_y^\prime & = r_x c + s_y d \cr
1704 %D t_x^\prime & = s_x e + r_y f + t_x \cr
1705 %D t_y^\prime & = r_x e + s_y f + t_y \cr}
1706 %D \stopformula
1707
1708 %D In fact, the same rule is true not only when one is going
1709 %D from user coordinates to device coordinates, but whenever
1710 %D one is composing two `transformations' together
1711 %D (transformations are `associative'). Note that the formula
1712 %D is not symmetric: you have to keep track of which
1713 %D transformation existed before (i.e.\ the equivalent of
1714 %D $[s_x\, r_x\, r_y\, s_y\, t_x\, t_y]$) and which was
1715 %D specified later (i.e.\ the equivalent of $[a\, b\, c\, d\,
1716 %D e\, f]$). Note also that the language can be rather
1717 %D confusing: the one specified later `acts earlier',
1718 %D converting the user space coordinates to intermediate
1719 %D coordinates, which are then acted upon by the pre||existing
1720 %D transformation. The important point is that order of
1721 %D transformation matrices cannot be flipped (transformations
1722 %D are not `commutative').
1723 %D
1724 %D Now what does it mean to move a transformation matrix
1725 %D before a drawing? What it means is that given a point
1726 %D $(P_x, P_y)$ we need a different set of coordinates
1727 %D $(P_x^\prime, P_y^\prime)$ such that if the transformation
1728 %D acts on $(P_x^\prime, P_y^\prime)$, they produce $(P_x,
1729 %D P_y)$. That is we need to solve the set of equations:
1730 %D
1731 %D \placeformula
1732 %D \startformula
1733 %D \pmatrix{P_x\cr P_y\cr 1\cr} = \pmatrix{s_x&r_y&t_x\cr
1734 %D r_x&s_y&t_y\cr
1735 %D 0 &0 &1 \cr}
1736 %D \pmatrix{P_x^\prime\cr
1737 %D P_y^\prime\cr
1738 %D 1 \cr}
1739 %D \stopformula
1740 %D
1741 %D Again matrix notation comes in handy (i.e. someone has
1742 %D already solved the problem for us): we need the inverse
1743 %D transformation matrix. The inverse transformation matrix can
1744 %D be calculated very easily:
1745 %D
1746 %D \placeformula
1747 %D \startformula
1748 %D \pmatrix{P_x^\prime\cr P_y^\prime\cr 1\cr} =
1749 %D \pmatrix{s_x^\prime&r_y^\prime&t_x^\prime\cr
1750 %D r_x^\prime&s_y^\prime&t_y^\prime\cr
1751 %D 0 &0 &1 \cr}
1752 %D \pmatrix{P_x\cr
1753 %D P_y\cr
1754 %D 1 \cr}
1755 %D \stopformula
1756 %D
1757 %D where, the inverse transformation matrix is given by
1758 %D
1759 %D \placeformula
1760 %D \startformula
1761 %D \eqalign
1762 %D {D & = s_x s_y - r_x r_y \cr
1763 %D s_x^\prime & = s_y / D \cr
1764 %D s_y^\prime & = s_x / D \cr
1765 %D r_x^\prime & = - r_x / D \cr
1766 %D r_y^\prime & = - r_y / D \cr
1767 %D t_x^\prime & = ( - s_y t_x + r_y t_y ) / D \cr
1768 %D t_y^\prime & = ( r_x t_x - s_x t_y ) / D \cr}
1769 %D \stopformula
1770 %D
1771 %D And you can see that when expanded out, this does
1772 %D give the formulas:
1773 %D
1774 %D \placeformula
1775 %D \startformula
1776 %D P_x^\prime = { { s_y(p_x-t_x) + r_y(t_y-p_y) } \over
1777 %D { s_x s_y-r_x r_y } }
1778 %D \stopformula
1779 %D
1780 %D \placeformula
1781 %D \startformula
1782 %D P_y^\prime = { { s_x(p_y-t_y) + r_x(t_x-p_x) } \over
1783 %D { s_x*s_y-r_x*r_y } }
1784 %D \stopformula
1785 %D
1786 %D The code works by representing a real number by converting
1787 %D it to a dimension to be put into a \DIMENSION\ register: 2.3 would
1788 %D be represented as 2.3pt for example. In this scheme,
1789 %D multiplying two numbers involves multiplying the \DIMENSION\
1790 %D registers and dividing by 65536. Accuracy demands that the
1791 %D division be done as late as possible, but overflow
1792 %D considerations need early division.
1793 %D
1794 %D Division involves dividing the two \DIMENSION\ registers and
1795 %D multiplying the result by 65536. Again, accuracy would
1796 %D demand that the numerator be multiplied (and|/|or the
1797 %D denominator divided) early: but that can lead to overflow
1798 %D which needs to be avoided.
1799 %D
1800 %D If nothing is known about the numbers to start with (in
1801 %D concat), I have chosen to divide the 65536 as a 256 in each
1802 %D operand. However, in the series calculating the sine and
1803 %D cosine, I know that the terms are small (because I never
1804 %D have an angle greater than 45 degrees), so I chose to
1805 %D apportion the factor in a different way.
1806
1807 %M \stop
1808
1809 %D The path is output using the values saved on the stack. If
1810 %D needed, all coordinates are recalculated.
1811
1812 \def\finishMPpath
1813 {\PDFcode{\ifcase\finiMPpath W n\or S\or f\or B\fi}}
1814
1815 \def\processMPpath
1816 {\checkMPpath
1817 \ifcase\nofMPsegments\else
1818 \flushMPpath
1819 \closeMPpath
1820 \finishMPpath
1821 \fi
1822 \let\handleMPsequence\dohandleMPsequence
1823 \resetMPstack
1824 \nofMPsegments0
1825 \handleMPsequence}
1826
1827 %D The following \METAPOST\ code is quite valid but, when
1828 %D processed and converted to \PDF, will make a file
1829 %D unprintable on a Hewlett Packard printer (from Acrobat
1830 %D $v<=5$). Who is to blame, the driver of the OS layer in
1831 %D between, is hard to determine, so we add an additional
1832 %D check.
1833 %D
1834 %D \starttyping
1835 %D clip currentpicture to origin -- cycle ;
1836 %D setbounds currentpicture to fullsquare scaled 5cm ;
1837 %D \stoptyping
1838
1839 \def\checkMPpath
1840 {\ifcase\finiMPpath
1841 \ifnum\nofMPsegments<3 % n is one ahead
1842 \message{omitting zero clip path}%
1843 \nofMPsegments0
1844 \fi
1845 \fi}
1846
1847 %D In \PDF\ the \type{cm} operator must precede the path
1848 %D specification. We therefore can output the \type{cm} at
1849 %D the moment we encounter it.
1850
1851 \def\handleMPpathconcat
1852 {\presetMPconcat
1853 \PDFcode{\gMPs1 \gMPs2 \gMPs3 \gMPs4 \gMPs5 \gMPs6 cm}%
1854 \resetMPstack}
1855
1856 \def\handleMPpathscale
1857 {\presetMPscale
1858 \PDFcode{\gMPs1 0 0 \gMPs2 0 0 cm}%
1859 \resetMPstack}
1860
1861 %D This macro interprets the path and saves it as compact as
1862 %D possible.
1863
1864 \def\dohandleMPpath#1%
1865 {\ifcase\lccode`#1\relax
1866 \expandafter\dohandleMPpathA
1867 \else
1868 \expandafter\dohandleMPpathB
1869 \fi#1}
1870
1871 %\def\dohandleMPpathA#1 %
1872 % {\setMPargument{#1}%
1873 % \handleMPsequence}
1874
1875 \let\dohandleMPpathA\setMPsequence
1876
1877 % \def\dohandleMPpathB#1 %
1878 % {\def\somestring{#1}%
1879 % \ifx\somestring\PSlineto
1880 % \setMPkeyword0
1881 % \else\ifx\somestring\PScurveto
1882 % \setMPkeyword1
1883 % \else\ifx\somestring\PSrlineto
1884 % \setMPkeyword2
1885 % \else\ifx\somestring\PSmoveto
1886 % \setMPkeyword3
1887 % \else\ifx\somestring\PSclip
1888 % % \chardef\finiMPpath0 % already
1889 % \let\handleMPsequence\processMPpath
1890 % \else\ifx\somestring\PSgsave
1891 % \chardef\finiMPpath3
1892 % \else\ifx\somestring\PSgrestore
1893 % \else\ifx\somestring\PSfill
1894 % \ifcase\finiMPpath
1895 % \chardef\finiMPpath2
1896 % \let\handleMPsequence\processMPpath
1897 % \fi
1898 % \else\ifx\somestring\PSstroke
1899 % \ifcase\finiMPpath
1900 % \chardef\finiMPpath1
1901 % \fi
1902 % \let\handleMPsequence\processMPpath
1903 % \else\ifx\somestring\PSclosepath
1904 % \def\closeMPpath{\PDFcode{h}}%
1905 % \else\ifx\somestring\PSconcat
1906 % \cleanupMPconcat
1907 % \let\flushMPpath\flushconcatMPpath
1908 % \handleMPpathconcat
1909 % \else\ifx\somestring\PSscale
1910 % \let\flushMPpath\flushconcatMPpath
1911 % \handleMPpathscale
1912 % \fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi
1913 % \handleMPsequence}
1914
1915 \def\installMPSkeywordP#1#2%
1916 {\expandafter\def\csname\@@MP:P:#1\endcsname{#2}}
1917
1918 \def\installMPSshortcutP#1#2% todo: \let
1919 {\expandafter\let\csname\@@MP:P:#1\expandafter\endcsname\csname\@@MP:P:#2\endcsname}
1920
1921 \def\dohandleMPpathB#1 %
1922 {\def\somestring{#1}%
1923 \ifcsname\@@MP:P:\somestring\endcsname
1924 \csname\@@MP:P:\somestring\expandafter\endcsname
1925 \fi
1926 \handleMPsequence}
1927
1928 \ifx\eTeXversion\undefined
1929 \def\dohandleMPpathB#1 %
1930 {\def\somestring{#1}%
1931 \csname\@@MP:P:\somestring\endcsname
1932 \handleMPsequence}
1933 \fi
1934
1935 \installMPSkeywordP \PSlineto
1936 {\setMPkeyword0 }
1937 \installMPSkeywordP \PScurveto
1938 {\setMPkeyword1 }
1939 \installMPSkeywordP \PSrlineto
1940 {\setMPkeyword2 }
1941 \installMPSkeywordP \PSmoveto
1942 {\edef\lastMPmoveX{\gMPs1}%
1943 \edef\lastMPmoveY{\gMPs2}%
1944 \resetMPstack
1945 \setMPkeyword3 }
1946 \installMPSkeywordP \PSclip
1947 {% \chardef\finiMPpath\zerocount % already
1948 \let\handleMPsequence\processMPpath}
1949 \installMPSkeywordP \PSgsave
1950 {\chardef\finiMPpath3 }
1951 \installMPSkeywordP \PSgrestore
1952 {}
1953 \installMPSkeywordP \PSfill
1954 {\ifcase\finiMPpath
1955 \chardef\finiMPpath2
1956 \let\handleMPsequence\processMPpath
1957 \fi}
1958 \installMPSkeywordP \PSstroke
1959 {\ifcase\finiMPpath
1960 \chardef\finiMPpath1
1961 \fi
1962 \let\handleMPsequence\processMPpath}
1963 \installMPSkeywordP \PSclosepath
1964 {\def\closeMPpath{\PDFcode{h}}}
1965 \installMPSkeywordP \PSconcat
1966 {\cleanupMPconcat
1967 \let\flushMPpath\flushconcatMPpath
1968 \handleMPpathconcat}
1969 \installMPSkeywordP \PSscale
1970 {\let\flushMPpath\flushconcatMPpath
1971 \handleMPpathscale}
1972
1973 \installMPSshortcutP {l} \PSlineto
1974 \installMPSshortcutP {r} \PSrlineto
1975 \installMPSshortcutP {m} \PSmoveto
1976 \installMPSshortcutP {c} \PScurveto
1977
1978 \installMPSshortcutP {q} \PSgsave
1979 \installMPSshortcutP {Q} \PSgrestore
1980 \installMPSshortcutP {S} \PSstroke
1981 \installMPSshortcutP {F} \PSfill
1982 \installMPSshortcutP {B} \PSgsave
1983 \installMPSshortcutP {W} \PSclip
1984 \installMPSshortcutP {p} \PSclosepath
1985
1986 \installMPSshortcutP {s} \PSscale
1987 \installMPSshortcutP {t} \PSconcat
1988
1989 %D The main conversion command is:
1990 %D
1991 %D \starttyping
1992 %D \convertMPtoPDF {filename} {x scale} {y scale}
1993 %D \stoptyping
1994 %D
1995 %D The dimensions are derived from the bounding box. So we
1996 %D only have to say:
1997 %D
1998 %D \starttyping
1999 %D \convertMPtoPDF{mp-pra-1.eps}{1}{1}
2000 %D \convertMPtoPDF{mp-pra-1.eps}{.5}{.5}
2001 %D \stoptyping
2002
2003 %D \macros
2004 %D {makeMPintoPDFobject,lastPDFMPobject}
2005 %D
2006 %D For experts there are a few more options. When attributes
2007 %D are to be added, the code must be embedded in an object
2008 %D accompanied with the appropriate directives. One can
2009 %D influence this process with \type {\makeMPintoPDFobject}.
2010 %D
2011 %D This option defaults to~0, because \CONTEXT\ takes care
2012 %D of objects at another level, which saves some bytes.
2013 %D
2014 %D \starttabulate[|l|l|p|]
2015 %D \NC 0 \NC never \NC don't use an object \NC\NR
2016 %D \NC 1 \NC always \NC always use an object \NC\NR
2017 %D \NC 2 \NC optional \NC use object when needed \NC\NR
2018 %D \stoptabulate
2019 %D
2020 %D The last object number used is avaliable in the macro
2021 %D \type {\lastPDFMPobject}.
2022
2023 \ifx\makeMPintoPDFobject\undefined \newcount\makeMPintoPDFobject \fi
2024
2025 \def\lastPDFMPobject{0}
2026
2027 %D The additional code needed can be made available in the
2028 %D (global) macro \type {\currentPDFresources}.
2029
2030 \let\currentPDFresources\empty
2031
2032 \newtoks\everyMPtoPDFconversion
2033
2034 \def\convertMPtoPDF % #1#2#3%
2035 {\bgroup
2036 \defineMPtoPDFfallbacks
2037 \ifx\pdfdecimaldigits\undefined\else \pdfdecimaldigits=5 \fi % new
2038 \setbox\scratchbox\vbox\bgroup
2039 \xdef\MPheight{0pt}%
2040 \xdef\MPwidth {0pt}%
2041 \forgetall
2042 \offinterlineskip
2043 \startMPresources
2044 \doprocessMPtoPDFfile} %
2045
2046 %D The next one is kind of private and probably will become obsolete):
2047
2048 \def\processMPtoPDFfile % file xscale yscale
2049 {\bgroup
2050 \let\finishMPgraphic\egroup
2051 \doprocessMPtoPDFfile}
2052
2053 \let\setMPextensions\relax
2054
2055 \def\doprocessMPtoPDFfile#1#2#3% file xscale yscale
2056 {\setMPspecials
2057 \setMPextensions
2058 \the\everyMPtoPDFconversion
2059 \catcode`\^^M=\@@endofline
2060 \startMPscanning
2061 \let\do\empty
2062 \xdef\MPxscale{#2}%
2063 \xdef\MPyscale{#3}%
2064 \xdef\MPxoffset{0}%
2065 \xdef\MPyoffset{0}%
2066 \xdef\MPyshift{0pt}%
2067 \donefalse
2068 \let\handleMPsequence\dohandleMPsequence
2069 \message{[MP to PDF]}% was: [MP to PDF #1] but there is a (#1) anyway
2070 \input#1\relax}
2071
2072 \def\PDFMPformoffset
2073 {\ifx\objectoffset\undefined0pt\else\objectoffset\fi}
2074
2075 \chardef\blackoutMPgraphic0 % in ConTeXt 1
2076
2077 \def\finishMPgraphic
2078 {\stopMPresources
2079 \egroup
2080 \setbox\scratchbox\vbox
2081 {\forgetall
2082 \hbox
2083 {\PDFcode{q \MPxscale\space 0 0 \MPyscale\space \MPxoffset\space \MPyoffset\space cm}%
2084 \ifcase\blackoutMPgraphic\or\PDFcode{0 g 0 G}\fi
2085 \lower\MPyshift\box\scratchbox % unscaled shift
2086 \PDFcode{Q}}}%
2087 \ht\scratchbox\MPheight
2088 \wd\scratchbox\MPwidth
2089 \dp\scratchbox0pt\relax
2090 \dopackageMPgraphic\scratchbox
2091 \egroup
2092 \endinput}
2093
2094 %D Alternative for \PDFTEX. We cannot come up with something more contexy
2095 %D because this module is also used in \LATEX.
2096
2097 \def\dopackageMPgraphic#1% #1 = boxregister
2098 {%\ifx\pdfxform\undefined
2099 % \makeMPintoPDFobject0 % no pdftex at all
2100 %\else\ifx\pdftexversion\undefined
2101 % \makeMPintoPDFobject0 % no pdftex at all
2102 %\else\ifnum\pdftexversion<14
2103 % \makeMPintoPDFobject0 % no resource support
2104 %\else
2105 % % keep the default value
2106 %\fi\fi\fi
2107 \ifcase\makeMPintoPDFobject\or\or\ifx\currentPDFresources\empty\else
2108 % an existing value of 2 signals object support (set elsewhere)
2109 \makeMPintoPDFobject1
2110 \fi\fi
2111 \ifcase\makeMPintoPDFobject
2112 \box#1%
2113 \or
2114 \scratchdimen\PDFMPformoffset\relax
2115 \ifdim\scratchdimen>0pt % compensate for error
2116 \setbox#1\vbox spread 2\scratchdimen
2117 {\forgetall\vss\hbox spread 2\scratchdimen{\hss\box#1\hss}\vss}%
2118 \fi
2119 \setMPPDFobject{\currentPDFresources}{#1}%
2120 \ifdim\scratchdimen>0pt % compensate for error
2121 \vbox to \MPheight
2122 {\forgetall\vss\hbox to \MPwidth{\hss\getMPPDFobject\hss}\vss}%
2123 \else
2124 \getMPPDFobject
2125 \fi
2126 \global\let\currentPDFresources\empty
2127 \else
2128 \box#1%
2129 \fi}
2130
2131 \def\setMPPDFobject#1#2% resources boxnumber
2132 {\ifx\pdfxform\undefined
2133 \def\getMPPDFobject{\box#2}%
2134 \else\ifx\pdftexversion\undefined
2135 \def\getMPPDFobject{\box#2}%
2136 \else\ifnum\pdftexversion<14
2137 \def\getMPPDFobject{\box#2}%
2138 \else
2139 \ifx\everyPDFxform\undefined\else\the\everyPDFxform\fi
2140 \immediate\pdfxform resources{#1}#2%
2141 \edef\getMPPDFobject{\noexpand\pdfrefxform\the\pdflastxform}%
2142 \fi\fi\fi}
2143
2144 \let\getMPPDFobject\relax
2145
2146 %D \macros
2147 %D {deleteMPgraphic,
2148 %D startMPresources,
2149 %D stopMPresources}
2150 %D
2151 %D Here are a few hooks for \CONTEXT\ specific things.
2152
2153 \ifx\deleteMPgraphic\undefined
2154 \def\deleteMPgraphic#1{}
2155 \fi
2156
2157 \ifx\startMPresources\undefined
2158 \let\startMPresources\relax
2159 \let\stopMPresources\relax
2160 \fi
2161
2162 %D \macros
2163 %D {twodigitMPoutput}
2164 %D
2165 %D We can limit the precision to two digits after the comma
2166 %D by saying:
2167 %D
2168 %D \starttyping
2169 %D \twodigitMPoutput
2170 %D \stoptyping
2171 %D
2172 %D This option only works in \CONTEXT\ combined with \ETEX.
2173
2174 \def\twodigitMPoutput
2175 {\let\!MP \twodigitrounding
2176 \def\!MPgMPs##1{\twodigitrounding{\gMPs##1}}%
2177 \def\!MPgMPa##1{\twodigitrounding{\gMPa##1}}}
2178
2179 \let\!MP \empty
2180 \let\!MPgMPa\gMPa
2181 \let\!MPgMPs\gMPs
2182
2183 %D This kind of conversion is possible because \METAPOST\
2184 %D does all the calculations. Converting other \POSTSCRIPT\
2185 %D files would drive both me and \TEX\ crazy.
2186
2187 \protect \endinput