mlib-pps.mkxl /size: 10 Kb    last modification: 2023-12-21 09:44
1%D \module
2%D   [       file=mlib-pps,
3%D        version=2008.03.25,
4%D          title=\METAPOST\ Integrated Graphics,
5%D       subtitle=Basics,
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% todo: remove MPenvironment code
15
16\unprotect
17
18\registerctxluafile{mlib-pps}{autosuffix}
19
20%D Todo: catch nested graphics like external figures with dummies.
21
22% todo: figure out why this is shifted
23%
24% \setupbodyfont[palatino]
25%
26% \starttext
27%     \startMPpage
28%         draw textext("123") ;
29%     \stopMPpage
30% \stoptext
31%
32% A slightly larger picture works ok so maybe there is some kind
33% of interference with the page builder.
34
35\newbox \MPtextbox
36\newtoks\everyMPLIBsettext % not used
37
38\permanent\protected\def\mlib_flush_environment
39  {%\writestatus\m!metapost{flushing environment}%
40   \clf_mptexget
41   \enforced\let\MPLIBflushenvironment\relax}% MPenvironments are depricated}
42
43\aliased\let\MPLIBflushenvironment\mlib_flush_environment
44
45\permanent\protected\def\MPLIBsetNtext#1% #2% box text
46  {\MPLIBflushenvironment
47   \dowithnextbox{\clf_mpsettext\nextbox #1}\hbox\bgroup % text
48     \meta_set_current_color
49     \enforced\let\MPLIBflushenvironment\mlib_flush_environment
50     \let\next} % gobble open brace
51
52\permanent\protected\def\MPLIBsetCtext#1#2% #3% box colorspec text
53  {\MPLIBflushenvironment
54   \dowithnextbox{\clf_mpsettext\nextbox #1}\hbox\bgroup % text
55     \directcolored[#2]%
56     \meta_set_current_color % so, textcolor wins !
57     \enforced\let\MPLIBflushenvironment\mlib_flush_environment
58     \let\next} % gobble open brace
59
60\aliased\let\MPLIBsettext\MPLIBsetNtext
61
62\permanent\protected\def\MPLIBsetNtextX#1% #2% box text
63  {\MPLIBflushenvironment
64   \hbox\bgroup % text
65   \meta_set_current_color
66   \enforced\let\MPLIBflushenvironment\mlib_flush_environment
67   \let\next}
68
69\permanent\protected\def\MPLIBsetCtextX#1#2% #3% box colorspec text
70  {\MPLIBflushenvironment
71   \hbox\bgroup % text
72   \directcolored[#2]%
73   \meta_set_current_color % so, textcolor wins !
74   \enforced\let\MPLIBflushenvironment\mlib_flush_environment
75   \let\next}
76
77\aliased\let\MPLIBsettextX\MPLIBsetNtextX
78
79% \permanent\permanent\protected\def\MPLIBgettextscaled#1#2#3%  why a copy .. can be used more often
80%   {\clf_mpgettext\MPtextbox #1%
81%    \vpack to \zeropoint
82%      {\vss
83%      %\hpack to \zeropoint \s!container
84%       \hcontainer to \zeropoint
85%         {\scale
86%           [\c!sx=#2,\c!sy=#3]%
87%           {\raise\dp\MPtextbox\box\MPtextbox}\forcecolorhack\hss}}}
88
89\permanent\permanent\protected\def\MPLIBgettextscaled#1#2#3%  why a copy .. can be used more often
90  {\clf_mpgettext\MPtextbox #1%
91   \vpack to \zeropoint
92     {\vss
93      \hcontainer to \zeropoint
94        {\scale
95          [\c!sx=#2,\c!sy=#3]%
96          {\raise\dp\MPtextbox\box\MPtextbox}\hss}}}
97
98\let\mlib_sx\!!zerocount % set at the lua end
99\let\mlib_sy\!!zerocount % set at the lua end
100
101\permanent\protected\def\MPLIBfigure#1#2%
102  {\setbox\scratchbox\hpack{\externalfigure[#1][\c!mask=#2]}%
103   \clf_mpsetsxsy\wd\scratchbox\ht\scratchbox\zeropoint
104   \vpack to \zeropoint
105     {\vss
106      \hpack to \zeropoint
107        {\fastsxsy{\mlib_sx}{\mlib_sy}{\box\scratchbox}\hss}}}
108
109% horrible (we could inline scale and matrix code):
110
111% \permanent\protected\def\MPLIBgettextscaledcm#1#2#3#4#5#6#7#8#9% 2-7: sx,rx,ry,sy,tx,ty
112%   {\clf_mpgettext\MPtextbox #1%
113%    \setbox\MPbox\hpack\bgroup
114%      \dotransformnextbox{#2}{#3}{#4}{#5}{#6}{#7}% does push pop ... will be changed to proper lua call (avoid small numbers)
115%        \vpack to \zeropoint\bgroup
116%           \vss
117%          %\hpack to \zeropoint \s!container \bgroup
118%           \hcontainer to \zeropoint \bgroup
119%            % \scale[\c!sx=#8,\c!sy=#9]{\raise\dp\MPtextbox\box\MPtextbox}%
120%            % \scale[\c!sx=#8,\c!sy=#9,\c!depth=\v!no]{\box\MPtextbox}%
121%              \fastsxsy{#8}{#9}{\raise\dp\MPtextbox\box\MPtextbox}%
122%              % This gives: LuaTeX warning: Misplaced \pdfrestore .. don't ask me why.
123%              % but I'll retry it some day soon.
124%              % \dostartscaling{#8}{#9}%
125%              %   \raise\dp\MPtextbox\box\MPtextbox
126%              % \dostopscaling
127%              \forcecolorhack % can go away ... already in the scale macro
128%              \hss
129%          \egroup
130%        \egroup
131%    \egroup
132%    \smashbox\MPbox
133%    \box\MPbox}
134
135\permanent\protected\def\MPLIBgettextscaledcm#1#2#3#4#5#6#7#8#9% 2-7: sx,rx,ry,sy,tx,ty
136  {\clf_mpgettext\MPtextbox #1%
137   \setbox\MPbox\hpack\bgroup
138     \dotransformnextbox{#2}{#3}{#4}{#5}{#6}{#7}% does push pop ... will be changed to proper lua call (avoid small numbers)
139       \vpack to \zeropoint\bgroup
140          \vss \hcontainer to \zeropoint \bgroup
141             \fastsxsy{#8}{#9}{\raise\dp\MPtextbox\box\MPtextbox}\hss
142         \egroup
143       \egroup
144   \egroup
145   \smashbox\MPbox
146   \box\MPbox}
147
148% \putnextboxincache{hans}{1}\hbox{foo}
149%
150% \startMPcode
151%     draw boundingbox rawtexbox("hans",1) ;
152%     draw rawtexbox("hans",1) ;
153% \stopMPcode
154
155\permanent\protected\def\MPLIBgetboxscaledcm#1#2%
156  {\begingroup
157   \copyboxfromcache{#1}{#2}\MPtextbox % can be \clf_
158   \mlib_get_box_scaled_cm_next}
159
160\protected\def\mlib_get_box_scaled_cm_next#1#2#3#4#5#6#7#8% 1-6: sx,rx,ry,sy,tx,ty
161  {\setbox\MPbox\hpack\bgroup
162     \dotransformnextbox{#1}{#2}{#3}{#4}{#5}{#6}%
163       \vpack to \zeropoint\bgroup
164          \vss
165          \hpack to \zeropoint \bgroup
166             \fastsxsy{#7}{#8}{\raise\dp\MPtextbox\box\MPtextbox}%
167             \hss
168         \egroup
169       \egroup
170   \egroup
171   \smashbox\MPbox
172   \box\MPbox
173   \endgroup}
174
175\permanent\protected\def\MPLIBscaledcm#1#2#3#4#5#6#7% 2-7: sx,rx,ry,sy,tx,ty
176  {\begingroup
177% \setbox\MPbox
178   \hpack\bgroup
179     \dotransformnextbox{#2}{#3}{#4}{#5}{#6}{#7}%
180    %\dotransformnextbox{\luaexpr{#2/10}}{\luaexpr{#3/10}}{\luaexpr{#4/10}}{\luaexpr{#5/10}}{\luaexpr{#6/10}}{\luaexpr{#7/10}}%
181     \vpack to \zeropoint\bgroup
182       \vss
183       \hcontainer to \zeropoint \bgroup
184         #1\hss % used for mp bitmaps
185        %\fastsxsy{10}{10}{#1}\hss
186       \egroup
187     \egroup
188   \egroup
189% \smashbox\MPbox\box\MPbox
190   \endgroup}
191
192\permanent\protected\def\MPLIBgraphictext#1% use at mp end
193  {\startTEXpage[\c!scale=10000]#1\stopTEXpage}
194
195%D \startbuffer
196%D \definelayer[test]
197%D
198%D \setlayerframed
199%D   [test]
200%D   [x=\MPx{somepos-1},y=\MPy{somepos-1}]
201%D   [width=\MPw{somepos-1},height=\MPh{somepos-1}]
202%D   {Whatever we want here!}
203%D
204%D \setlayerframed
205%D   [test]
206%D   [x=\MPx{somepos-2},y=\MPy{somepos-2}]
207%D   [width=\MPw{somepos-2},height=\MPh{somepos-2}]
208%D   {Whatever we need there!}
209%D
210%D \startuseMPgraphic{oeps}
211%D     draw fullcircle scaled 6cm withcolor red ;
212%D     register ("somepos-1",5cm,1cm,center currentpicture) ;
213%D     register ("somepos-2",4cm,3cm,(-1cm,-2cm)) ;
214%D \stopuseMPgraphic
215%D
216%D \framed[background=test,offset=overlay]{\useMPgraphic{oeps}}
217%D \stopbuffer
218%D
219%D \typebuffer \startlinecorrection \getbuffer \stoplinecorrection
220
221\permanent\protected\def\MPLIBpositionwhd#1#2#3#4#5% bp !
222  {\dosavepositionwhd{#1}\zerocount{#2\onebasepoint}{#3\onebasepoint}{#4\onebasepoint}{#5\onebasepoint}\zeropoint}
223
224\def\mlib_stop_group#1#2#3#4#5#6% some day this might happen elsewhere
225  {\egroup
226   \setbox\scratchbox\hpack{\kern\onebasepoint\box\scratchbox}% weird correction
227   \wd\scratchbox \dimexpr#5\onebasepoint-#3\onebasepoint+2\onebasepoint\relax
228   \ht\scratchbox #6\onebasepoint
229   \dp\scratchbox-#4\onebasepoint
230   \setbox\scratchbox\hpack\bgroup
231     \kern-#3\onebasepoint
232     \box\scratchbox
233   \egroup
234   \saveboxresource
235     attr      {/Group << /S /Transparency /I \ifnum#1=1 true \else false \fi /K \ifnum#2=1 true \else false \fi >>}
236     resources {\pdfbackendcurrentresources}
237     \scratchbox
238   \setbox\scratchbox\hpack\bgroup
239     \kern#3\onebasepoint
240     \kern-\onebasepoint
241     \useboxresource\lastsavedboxresourceindex
242   \egroup
243   \wd\scratchbox\zeropoint
244   \ht\scratchbox\zeropoint
245   \dp\scratchbox\zeropoint
246   \box\scratchbox
247   \endgroup}
248
249\permanent\protected\lettonothing\MPLIBstopgroup
250
251\permanent\protected\def\MPLIBstartgroup#1#2#3#4#5#6% isolated 0/1, knockout 0/1 llx lly urx ury
252  {\begingroup
253   \setbox\scratchbox\hpack\bgroup
254   \enforced\permanent\protected\def\MPLIBstopgroup{\mlib_stop_group{#1}{#2}{#3}{#4}{#5}{#6}}}
255
256%D Not that much code is needed because we already have a mechanism already. For
257%D some reason there is no option to use the current positions (pattern space mess)
258%D so we need a position. I could delegate this to the backend but patterns are
259%D selsom used.
260
261\permanent\protected\def\MPLIBstartsetpattern#1#2#3#4#5#6%
262  {\normalexpanded{\registerpattern[mp-pattern-#1]\ifnum#6=\zerocount[hoffset=\MPx{mp-pattern-#1},voffset=\MPy{mp-pattern-#1}]\fi}%
263     \bgroup
264        \dowithnextbox{%
265          \boxxoffset\nextbox-#2\onebasepoint
266          \boxyoffset\nextbox-#3\onebasepoint
267          \wd\nextbox\dimexpr#4\onebasepoint-#2\onebasepoint\relax
268          \ht\nextbox\dimexpr#5\onebasepoint-#3\onebasepoint\relax
269          \box\nextbox}%
270        \hpack\bgroup}
271
272\permanent\protected\def\MPLIBstopsetpattern
273  {\egroup
274   \egroup}
275
276\permanent\protected\def\MPLIBstartgetpattern#1#2#3#4#5#6%
277  {\setbox\nextbox\hpack\bgroup
278     \applypattern
279       [\c!name=mp-pattern-#1,
280%       \c!width=2\dimexpr#4\onebasepoint-#2\onebasepoint\relax,
281        \c!width=\dimexpr#4\onebasepoint-#2\onebasepoint\relax,
282%       \c!height=2\dimexpr#5\onebasepoint-#3\onebasepoint\relax]%
283        \c!height=\dimexpr#5\onebasepoint-#3\onebasepoint\relax]%
284    \hss
285   \egroup
286 % \boxyoffset\nextbox -.5\dimexpr#5\onebasepoint-#3\onebasepoint\relax
287   \boxyoffset\nextbox \dimexpr#3\onebasepoint\relax
288 % \boxxoffset\nextbox -.5\dimexpr#4\onebasepoint-#2\onebasepoint\relax
289   \boxxoffset\nextbox \dimexpr#2\onebasepoint\relax
290   \ht\nextbox\zeropoint
291   \wd\nextbox\zeropoint
292   \dp\nextbox\zeropoint
293   \normalexpanded{\ifnum#6=\zerocount\hpos{mp-pattern-#1}\fi}{\box\nextbox}}
294
295\permanent\protected\lettonothing\MPLIBstopgetpattern
296
297% For now here ... will be cleaned up and become macro calls instead:
298
299\newtoks\mptexttoks
300\newbox \mptextbox
301\newtoks\mpoutlinetoks
302\newtoks\mpgraphictexttoks
303
304\mptexttoks       {\global\setbox\mptextbox\hbox{\clf_mptexttoks}}
305\mpoutlinetoks    {\global\setbox\mptextbox\vbox{\clf_mpoutlinetoks}}
306\mpgraphictexttoks{\global\setbox\mptextbox\vbox{\clf_mpgraphictexttoks}}
307
308\protect \endinput
309