mlib-pdf.mkxl /size: 4976 b    last modification: 2023-12-21 09:44
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}{autosuffix}
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\newdimension\MPwidth
24\newdimension\MPheight
25
26\newdimension\MPllx
27\newdimension\MPlly
28\newdimension\MPurx
29\newdimension\MPury
30
31\ifdefined\MPbox\else \newbox\MPbox\fi
32
33\permanent\protected\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\permanent\protected\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\permanent\protected\lettonothing\popMPboundingbox
50
51\permanent\protected\def\pushMPboundingbox
52  {\enforced\permanent\protected\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% we need to combine these
62
63\permanent\protected\def\repositionMPboxindeed
64  {\setbox\MPbox\hpack\bgroup
65     \kern-\MPllx
66     \raise-\MPlly
67     \box\MPbox
68   \egroup}
69
70\permanent\protected\def\repositionMPbox
71  {\ifzeropt\MPllx
72     \ifzeropt\MPlly
73       % okay
74     \else
75       \repositionMPboxindeed
76     \fi
77   \else
78     \repositionMPboxindeed
79   \fi}
80
81% combined
82
83\permanent\protected\def\finalizeMPbox
84  {\boxxoffset\MPbox-\MPllx\relax
85   \boxyoffset\MPbox\dimexpr\boxyoffset\MPbox-\MPheight-\MPlly\relax
86   \wd\MPbox\MPwidth
87   \ht\MPbox\MPheight}
88
89% MPLIB specific:
90
91% \permanent\protected\def\startMPLIBtoPDF#1#2#3#4%
92%   {\meta_process_graphic_figure_start
93%    \dostarttagged\t!mpgraphic\empty
94%    \naturalhpack attr \imageattribute \plusone \bgroup
95%    \dousecolorparameter\s!black\forcecolorhack
96%    \setMPboundingbox{#1}{#2}{#3}{#4}%
97%    \setbox\MPbox\vpack\bgroup
98%  % \forgetall % already done elsewhere
99%    \noindent} % forces the proper cm in the backend
100
101\permanent\protected\def\startMPLIBtoPDF#1#2#3#4%
102  {\meta_process_graphic_figure_start
103   \dostarttagged\t!mpgraphic\empty
104   \naturalhpack attr \imageattribute \plusone \bgroup
105   \dousecolorparameter\s!black
106   \setMPboundingbox{#1}{#2}{#3}{#4}%
107   \setbox\MPbox\vpack\bgroup
108   \noindent} % forces the proper cm in the backend
109
110\permanent\protected\def\stopMPLIBtoPDF
111  {\egroup
112   \finalizeMPbox
113   \box\MPbox
114   \egroup
115   \dostoptagged
116   \meta_process_graphic_figure_stop}
117
118%D Kind of special:
119%
120% test.mp:
121%
122% beginfig(1) fill fullcircle scaled 10cm withcolor red  ; endfig ;
123% beginfig(2) draw fullcircle scaled  5cm withcolor blue ; endfig ;
124% beginfig(3) draw textext("just some text")             ; endfig ;
125%
126% standalone pages:
127%
128% \starttext
129%     \directMPgraphic{input "test.mp" ;}
130% \stoptext
131
132\aliased\let\normalstartMPLIBtoPDF\startMPLIBtoPDF
133\aliased\let\normalstopMPLIBtoPDF \stopMPLIBtoPDF
134
135\permanent\protected\def\directstartMPLIBtoPDF{\startTEXpage\normalstartMPLIBtoPDF}
136\permanent\protected\def\directstopMPLIBtoPDF {\normalstopMPLIBtoPDF\stopTEXpage}
137
138% can become \permanent\protected\def\directMPgraphic#=#*#=% but best test first
139
140\permanent\protected\def\directMPgraphic
141  {\dodoublegroupempty\mlib_direct_graphic}
142
143\def\mlib_direct_graphic#1#2% makes pages (todo: make boxes)
144  {\meta_begin_graphic_group{#1}%
145   \enforced\let\startMPLIBtoPDF\directstartMPLIBtoPDF
146   \enforced\let\stopMPLIBtoPDF \directstopMPLIBtoPDF
147   \meta_start_current_graphic
148     \forgetall
149     \edef\p_extensions{\MPinstanceparameter\s!extensions}%
150     \normalexpanded{\noexpand\clf_mpgraphic
151         instance        {\currentMPinstance}%
152         format          {\currentMPformat}%
153         data            {#2;}%
154         initializations {\meta_flush_current_initializations}%
155    \ifx\p_extensions\v!yes
156         extensions      {\clf_getmpextensions{\currentMPinstance}}%
157    \fi
158         inclusions      {\meta_flush_current_inclusions}%
159         definitions     {\meta_flush_current_definitions}%
160         figure          {all}%
161         method          {\MPinstanceparameter\c!method}%
162     \relax}%
163   \meta_stop_current_graphic
164   \meta_end_graphic_group}
165
166\protect \endinput
167