summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/context/base/mlib-pdf.mkiv
blob: 0913b3699ae8ff758facda5921b9fb54d0a13d2c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
%D \module
%D   [       file=mlib-pdf,
%D        version=2008.03.25,
%D          title=\METAPOST\ Integrated Graphics,
%D       subtitle=Conversion to PDF,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

\unprotect

% We use bit more code that needed because we want to limit the
% amount of boxing.

\registerctxluafile{mlib-pdf}{1.001}

%D Some code is shared between MPLIB and MPS. The following variables
%D are also available for introspection and other purposes.

\ifdefined\MPwidth  \else \newdimen\MPwidth  \fi
\ifdefined\MPheight \else \newdimen\MPheight \fi

\ifdefined\MPllx    \else \newdimen\MPllx    \fi
\ifdefined\MPlly    \else \newdimen\MPlly    \fi
\ifdefined\MPurx    \else \newdimen\MPurx    \fi
\ifdefined\MPury    \else \newdimen\MPury    \fi

\ifdefined\MPbox    \else \newbox  \MPbox    \fi

\def\setMPboundingbox#1#2#3#4% at some point we might pass them as base or scaled points
  {\global\MPllx   #1\onebasepoint
   \global\MPlly   #2\onebasepoint
   \global\MPurx   #3\onebasepoint
   \global\MPury   #4\onebasepoint
   \global\MPwidth \dimexpr\MPurx-\MPllx\relax
   \global\MPheight\dimexpr\MPury-\MPlly\relax}

\def\resetMPboundingbox
  {\global\MPwidth \zeropoint
   \global\MPheight\zeropoint
   \global\MPllx   \zeropoint
   \global\MPlly   \zeropoint
   \global\MPurx   \zeropoint
   \global\MPury   \zeropoint}

\def\repositionMPboxindeed
  {\setbox\MPbox\hbox\bgroup
     \kern-\MPllx
     \raise-\MPlly
     \box\MPbox
   \egroup}

\def\repositionMPbox
  {\ifzeropt\MPllx
     \ifzeropt\MPlly
       % okay
     \else
       \repositionMPboxindeed
     \fi
   \else
     \repositionMPboxindeed
   \fi}

\def\finalizeMPbox
  {\repositionMPbox
   \setbox\MPbox\vbox to \MPheight\bgroup
     \vfill
     \hsize\MPwidth
     \smashbox\MPbox
     \box\MPbox
   \egroup
   \wd\MPbox\MPwidth
   \ht\MPbox\MPheight}

\def\MPtextext#1#2#3#4#5% beware: we use a different method now (see mlib-pps)
  {\begingroup
   \setbox\MPbox\hbox{\font\temp=#1\space at #2\onebasepoint \let\c\char \temp #3}%
   \MPllx-#4\onebasepoint
   \MPlly-#5\onebasepoint
   \repositionMPbox
   \smashbox\MPbox
   \box\MPbox
   \endgroup}

% MPLIB specific:

\def\MPLIBtoPDF#1{\ctxlua{metapost.flushliteral(#1)}}

\def\startMPLIBtoPDF#1#2#3#4%
  {\meta_process_graphic_figure_start
   \dostarttagged\t!mpgraphic\empty
   \naturalhbox attr \imageattribute 1 \bgroup
   \dousecolorparameter\s!black\forcecolorhack
   \setMPboundingbox{#1}{#2}{#3}{#4}%
   \setbox\MPbox\vbox\bgroup
 % \forgetall % already done elsewhere
   \noindent} % forces the proper cm in the backend

\def\stopMPLIBtoPDF
  {\egroup
   \finalizeMPbox
   \box\MPbox
   \egroup
   \dostoptagged
   \meta_process_graphic_figure_stop}

\def\MPLIBflushreset % This can (will) move to the Lua end.
  {\ctxlua{metapost.flushreset()}}

%D Kind of special:
%
% test.mp:
%
% beginfig(1) fill fullcircle scaled 10cm withcolor red  ; endfig ;
% beginfig(2) draw fullcircle scaled  5cm withcolor blue ; endfig ;
% beginfig(3) draw textext("just some text")             ; endfig ;
%
% standalone pages:
%
% \starttext
%     \directMPgraphic{input "test.mp" ;}
% \stoptext

\let\normalstartMPLIBtoPDF\startMPLIBtoPDF
\let\normalstopMPLIBtoPDF \stopMPLIBtoPDF

\def\directstartMPLIBtoPDF{\startTEXpage\normalstartMPLIBtoPDF}
\def\directstopMPLIBtoPDF {\normalstopMPLIBtoPDF\stopTEXpage}

\unexpanded\def\directMPgraphic
  {\dodoublegroupempty\mlib_direct_graphic}

\def\mlib_direct_graphic#1#2% makes pages (todo: make boxes)
  {\meta_begin_graphic_group{#1}%
   \let\startMPLIBtoPDF\directstartMPLIBtoPDF
   \let\stopMPLIBtoPDF \directstopMPLIBtoPDF
   \meta_start_current_graphic
   \forgetall
   \edef\p_extensions{\MPinstanceparameter\s!extensions}%
   \normalexpanded{\noexpand\ctxlua{metapost.graphic {
       instance        = "\currentMPinstance",
       format          = "\currentMPformat",
       data            = \!!bs#2;\!!es,
       initializations = \!!bs\meta_flush_current_initializations\!!es,
%      useextensions   = "\MPinstanceparameter\s!extensions",
\ifx\p_extensions\v!yes
       extensions      = \!!bs\ctxcommand{getmpextensions("\currentMPinstance")}\!!es,
\fi
       inclusions      = \!!bs\meta_flush_current_inclusions\!!es,
       definitions     = \!!bs\meta_flush_current_definitions\!!es,
       figure          = "all",
       method          = "\MPinstanceparameter\c!method",
     }}}%
   \meta_stop_current_graphic
   \meta_end_graphic_group}

\protect \endinput