mlib-pdf.mkiv /size: 4660 b    last modification: 2020-07-01 14:35
1%D \module
2%D   [       file=mlib-pdf,
3%D        version=2008.03.25,
4%D          title=\METAPOST\ Integrated Graphics,
5%D       subtitle=Conversion to PDF,
6%D         author=Hans Hagen,
7%D           date=\currentdate,
8%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
9%C
10%C This module is part of the \CONTEXT\ macro||package and is
11%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
12%C details.
13
14\unprotect
15
16%D We use bit more code that needed because we want to limit the amount of boxing.
17
18\registerctxluafile{mlib-pdf}{}
19
20%D Some code is shared between MPLIB and MPS. The following variables are also
21%D available for introspection and other purposes.
22
23\ifdefined\MPwidth  \else \newdimen\MPwidth  \fi
24\ifdefined\MPheight \else \newdimen\MPheight \fi
25
26\ifdefined\MPllx    \else \newdimen\MPllx    \fi
27\ifdefined\MPlly    \else \newdimen\MPlly    \fi
28\ifdefined\MPurx    \else \newdimen\MPurx    \fi
29\ifdefined\MPury    \else \newdimen\MPury    \fi
30
31\ifdefined\MPbox    \else \newbox  \MPbox    \fi
32
33\unexpanded\def\setMPboundingbox#1#2#3#4% at some point we might pass them as base or scaled points
34  {\global\MPllx   #1\onebasepoint
35   \global\MPlly   #2\onebasepoint
36   \global\MPurx   #3\onebasepoint
37   \global\MPury   #4\onebasepoint
38   \global\MPwidth \dimexpr\MPurx-\MPllx\relax
39   \global\MPheight\dimexpr\MPury-\MPlly\relax}
40
41\unexpanded\def\resetMPboundingbox
42  {\global\MPwidth \zeropoint
43   \global\MPheight\zeropoint
44   \global\MPllx   \zeropoint
45   \global\MPlly   \zeropoint
46   \global\MPurx   \zeropoint
47   \global\MPury   \zeropoint}
48
49\let\popMPboundingbox\relax
50
51\unexpanded\def\pushMPboundingbox
52  {\edef\popMPboundingbox
53     {\global\MPwidth \the\MPwidth
54      \global\MPheight\the\MPheight
55      \global\MPllx   \the\MPllx
56      \global\MPlly   \the\MPlly
57      \global\MPurx   \the\MPurx
58      \global\MPury   \the\MPury
59      \relax}}
60
61\unexpanded\def\repositionMPboxindeed
62  {\setbox\MPbox\hpack\bgroup
63     \kern-\MPllx
64     \raise-\MPlly
65     \box\MPbox
66   \egroup}
67
68\unexpanded\def\repositionMPbox
69  {\ifzeropt\MPllx
70     \ifzeropt\MPlly
71       % okay
72     \else
73       \repositionMPboxindeed
74     \fi
75   \else
76     \repositionMPboxindeed
77   \fi}
78
79\unexpanded\def\finalizeMPbox
80  {\repositionMPbox
81   \setbox\MPbox\vpack to \MPheight\bgroup
82     \vfill
83     \hsize\MPwidth
84     \smashbox\MPbox
85     \box\MPbox
86   \egroup
87   \wd\MPbox\MPwidth
88   \ht\MPbox\MPheight}
89
90% MPLIB specific:
91
92\def\MPLIBtoPDF{\clf_mpflushliteral} % expanded
93
94\unexpanded\def\startMPLIBtoPDF#1#2#3#4%
95  {\meta_process_graphic_figure_start
96   \dostarttagged\t!mpgraphic\empty
97   \naturalhpack attr \imageattribute \plusone \bgroup
98   \dousecolorparameter\s!black\forcecolorhack
99   \setMPboundingbox{#1}{#2}{#3}{#4}%
100   \setbox\MPbox\vpack\bgroup
101 % \forgetall % already done elsewhere
102   \noindent} % forces the proper cm in the backend
103
104\unexpanded\def\stopMPLIBtoPDF
105  {\egroup
106   \finalizeMPbox
107   \box\MPbox
108   \egroup
109   \dostoptagged
110   \meta_process_graphic_figure_stop}
111
112\def\MPLIBflushreset % This can (will) move to the Lua end.
113  {\clf_mpflushreset}
114
115%D Kind of special:
116%
117% test.mp:
118%
119% beginfig(1) fill fullcircle scaled 10cm withcolor red  ; endfig ;
120% beginfig(2) draw fullcircle scaled  5cm withcolor blue ; endfig ;
121% beginfig(3) draw textext("just some text")             ; endfig ;
122%
123% standalone pages:
124%
125% \starttext
126%     \directMPgraphic{input "test.mp" ;}
127% \stoptext
128
129\let\normalstartMPLIBtoPDF\startMPLIBtoPDF
130\let\normalstopMPLIBtoPDF \stopMPLIBtoPDF
131
132\unexpanded\def\directstartMPLIBtoPDF{\startTEXpage\normalstartMPLIBtoPDF}
133\unexpanded\def\directstopMPLIBtoPDF {\normalstopMPLIBtoPDF\stopTEXpage}
134
135\unexpanded\def\directMPgraphic
136  {\dodoublegroupempty\mlib_direct_graphic}
137
138\def\mlib_direct_graphic#1#2% makes pages (todo: make boxes)
139  {\meta_begin_graphic_group{#1}%
140   \let\startMPLIBtoPDF\directstartMPLIBtoPDF
141   \let\stopMPLIBtoPDF \directstopMPLIBtoPDF
142   \meta_start_current_graphic
143     \forgetall
144     \edef\p_extensions{\MPinstanceparameter\s!extensions}%
145     \normalexpanded{\noexpand\clf_mpgraphic
146         instance        {\currentMPinstance}%
147         format          {\currentMPformat}%
148         data            {#2;}%
149         initializations {\meta_flush_current_initializations}%
150    \ifx\p_extensions\v!yes
151         extensions      {\clf_getmpextensions{\currentMPinstance}}%
152    \fi
153         inclusions      {\meta_flush_current_inclusions}%
154         definitions     {\meta_flush_current_definitions}%
155         figure          {all}%
156         method          {\MPinstanceparameter\c!method}%
157     \relax}%
158   \meta_stop_current_graphic
159   \meta_end_graphic_group}
160
161\protect \endinput
162