Debian Codegraph @

texmf-dist/tex/generic/context/mptopdf/mptopdf.tex

raw
1 %D \module
2 %D [ file=mptopdf,
3 %D version=2011.05.19, % 2000.03.27,
4 %D title=\METAPOST,
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 %D The file \type {mptopdf} provides a quick way to convert
15 %D \METAPOST\ files to \PDF\ using a slightly stripped down
16 %D plain \TEX, \PDFTEX, and a few \CONTEXT\ modules.
17 %D
18 %D First generate a format, which in \WEBC\ looks like:
19 %D
20 %D \starttyping
21 %D pdftex --ini mptopdf
22 %D \stoptyping
23 %D
24 %D or:
25 %D
26 %D \starttyping
27 %D texexec --make --tex=pdftex --format=mptopdf --alone
28 %D \stoptyping
29 %D
30 %D Since this conversion only works with \PDFTEX\ or \PDFETEX,
31 %D the session is aborted when another \TEX\ is used. When
32 %D finished, the resulting \type {fmt} file should be moved to
33 %D the right location.
34 %D
35 %D The conversion itself is accomplished by:
36 %D
37 %D \starttyping
38 %D pdftex &mptopdf \relax filename.number
39 %D \stoptyping
40 %D
41 %D The \type {\relax} is needed since we don't want to process
42 %D the file directly. Instead we pick up the filename using
43 %D \type {\everypar}. Since this file is still the first one
44 %D we load, although delayed, the jobname is as we expect. So,
45 %D at least in \WEBC, the result of the conversion comes
46 %D available in the file \type {filename.pdf}. This conversion
47 %D process is roughly compatible with:
48 %D
49 %D \starttyping
50 %D texexec --pdf --fig=c --result=filename filename.number
51 %D \stoptyping
52 %D
53 %D This uses \CONTEXT, and is therefore slower. Therefore,
54 %D we provide a small \PERL\ script that does a faster job,
55 %D using the minimal format. Given that a format is
56 %D generated, one can say:
57 %D
58 %D \starttyping
59 %D mptopdf somefile
60 %D mptopdf somefile.123
61 %D mptopdf mp*.*
62 %D \stoptyping
63 %D
64 %D The results are copied into files named \type
65 %D {somefile-number}. This mechanism will also be available
66 %D in a next release of \TEXUTIL.
67
68 %D The \TEX\ implementation is rather simple, since we use some
69 %D generic \CONTEXT\ modules. Because we need a few register
70 %D allocation macros, we preload plain \TEX. We don't load
71 %D fonts yet.
72
73 \input syst-tex.mkii
74
75 %D We check for the usage of \PDFTEX, and quit if another
76 %D \TEX\ is used.
77
78 \ifx\pdfoutput\undefined
79 \message{Sorry, you should use pdf(e)TeX instead.}
80 \expandafter \endinput
81 \fi
82
83 %D The conversion to \PDF\ is carried out by macros, that
84 %D are collected in the file:
85
86 \input supp-mis.mkii
87 \input supp-pdf.mkii
88 \input supp-mpe.mkii \MPcmykcolorstrue \MPspotcolorstrue
89
90 %D We use no output routine.
91
92 \output{}
93
94 %D Since we need to calculate and set the bounding box,
95 %D we definitely don't want to indent paragraphs.
96
97 \parindent=0pt
98
99 %D We use \type {\everypar} to pick up the filename and
100 %D process the \METAPOST\ graphic.
101
102 \everypar{\processMPfile}
103
104 %D The main macro shows a few \PDFTEX\ primitives. The main
105 %D work is done by the macro \type {\convertMPtoPDF} which is
106 %D defined in \type supp-pdf}. This macro interprets the
107 %D \METAPOST\ file. Close reading of this macro will probably
108 %D learn a few (\PDF) tricks. Apart from some path
109 %D transformations, which are needed since \PDF\ has a
110 %D different vision on paths, the graphic is positioned in
111 %D such a way that accuracy in \PDF\ xforms is guaranteed.
112
113 \ifx\makeMPintoPDFobject\undefined \newcount\makeMPintoPDFobject \fi
114
115 \def\processMPfile#1 %
116 {\pdfoutput=1
117 \pdfpkresolution600
118 \pdfcompresslevel=9
119 \makeMPintoPDFobject=1
120 \hsize=100in
121 \vsize=\hsize
122 \hoffset=-1in
123 \voffset=\hoffset
124 \topskip=0pt
125 \setbox0=\vbox{\convertMPtoPDF{#1}{1}{1}}%
126 \ifdim\wd0<1in \message{[warning: the width is less than 1in]}\fi
127 \ifdim\ht0<1in \message{[warning: the height is less than 1in]}\fi
128 \pdfpageheight=\ht0
129 \pdfpagewidth=\wd0
130 \box0
131 \bye}
132
133 %D The \type {\chardef} forces the converter to build a so
134 %D called xform object. This is needed in case the graphic
135 %D uses special trickery, like shading.
136
137 %D Since \ACROBAT\ has troubles with figures smaller than
138 %D 1~inch, we issue a warning. When embedding graphics in
139 %D documents, a size less that 1~inch does not harm. In
140 %D order to overload runtime directives in the \PDFTEX\
141 %D configuration file, we set the offsets and output method
142 %D in the macro.
143 %D
144 %D The resulting \PDF\ file is about as efficient as such a
145 %D self contained file can be. However, if needed, this \PDF\
146 %D file can be converted to \EPS\ using for instance the
147 %D \PDFTOPS\ program (in \WEBC) or \GHOSTSCRIPT.
148
149 %D A few helpers:
150
151 {\catcode`\.=12
152 \catcode`\p=12
153 \catcode`\t=12
154 \gdef\WITHOUTPT#1pt{#1}}
155
156 \def\withoutpt#1%
157 {\expandafter\WITHOUTPT#1}
158
159 \def\negatecolorcomponent#1% #1 = \macro
160 {\scratchdimen1pt\advance\scratchdimen-#1\onepoint
161 \ifdim\scratchdimen<\zeropoint\scratchdimen\zeropoint\fi
162 \edef#1{\withoutpt\the\scratchdimen}}
163
164 \let\negatedcolorcomponent\firstofoneargument
165
166 \def\negatedcolorcomponent#1%
167 {\ifdim\dimexpr1pt-#1pt\relax<\zeropoint
168 0pt%
169 \else
170 \expandafter\withoutpt\the\dimexpr1pt-#1pt\relax
171 \fi}
172
173 \def\negatecolorcomponent#1% #1 = \macro
174 {\edef#1{\negatedcolorcomponent{#1}}}
175
176 \countdef\realpageno=0 % to satisfy mkiv status reports
177
178 \dump