-
Notifications
You must be signed in to change notification settings - Fork 0
/
luatexko-mplib.sty
120 lines (117 loc) · 3.45 KB
/
luatexko-mplib.sty
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
% $Id: luatexko-mplib.sty,v 1.3 2011/07/04 15:26:25 nomos Exp $
%
% written by Dohyun Kim <nomos at ktug org>
% public domain
%
% This file is a derivative of luatex-mplib.tex by Hans Hagen
\bgroup\expandafter\expandafter\expandafter\egroup
\expandafter\ifx\csname RequirePackage\endcsname\relax
\input luatexbase-modutils.sty
\input pdftexcmds.sty
\else
\ProvidesPackage{luatexko-mplib}[2013/03/30 v1.1 Metapost with LuaTeX]
\RequirePackage{luatexbase-modutils}
\RequirePackage{pdftexcmds}
\fi
%%%%%%%%%%
\RequireLuaModule{luatexko-mplib}
%%%%%%%%%%
\def\mplibsetformat#1{%
\directlua{luatexkomplib.setformat("\luatexluaescapestring{#1}")}}
\ifnum\pdfoutput>0
\let\mplibtoPDF\pdfliteral
\else
%\def\MPLIBtoPDF#1{\special{pdf:literal direct #1}} % not ok yet
\def\mplibtoPDF#1{}
\expandafter\ifx\csname PackageWarning\endcsname\relax
\write16{}
\write16{Warning: MPLib only works in PDF mode, no figure will be output.}
\write16{}
\else
\PackageWarning{mplib}{MPLib only works in PDF mode, no figure will be output.}
\fi
\fi
%%%%%%%%%%
\def\mplibsetupcatcodes{% exclude comment sign
\catcode`\{=12 \catcode`\}=12 \catcode`\#=12 \catcode`\^=12 \catcode`\~=12
\catcode`\_=12 \catcode`\&=12 \catcode`\$=12 }
\def\mplibputtextbox#1{\vbox to 0pt{\vss\hbox to 0pt{\raise\dp#1\box#1\hss}}}
%%%%%%%%%%
\bgroup\expandafter\expandafter\expandafter\egroup
\expandafter\ifx\csname RequirePackage\endcsname\relax
\def\mplibcode{%
\bgroup %
\mplibsetupcatcodes %
\mplibdocode %
}
%%%%%%%%%%
\long\def\mplibdocode#1\endmplibcode{\egroup
\directlua{ luatexkomplib.luakosetboxes([===[\unexpanded{#1}]===]) }%
\directlua{ local data = luatexkomplib.luakogetboxes([===[\unexpanded{#1}]===])
luatexkomplib.process(data) }}
%%%%%%%%%%
\else
%%%%%%%%%%
\newenvironment{mplibcode}{\toks@{}\ltxdomplibcode}{}
\def\ltxdomplibcode{\bgroup\mplibsetupcatcodes\ltxdomplibcodeX}
\long\def\ltxdomplibcodeX#1\end{\egroup
\toks@\expandafter{\the\toks@#1}\ltxdomplibcodeXX}
\def\ltxdomplibcodeXX#1{%
\ifnum\pdf@strcmp{#1}{mplibcode}=\z@
\directlua{luatexkomplib.luakosetboxes([===[\the\toks@]===])}%
\directlua{local data = luatexkomplib.luakogetboxes([===[\the\toks@]===])
luatexkomplib.process(data)}\end{mplibcode}%
\else
\toks@\expandafter{\the\toks@\end{#1}}\expandafter\ltxdomplibcode
\fi}
%%%%%%%%%%
\fi
\ifx\mplibscratchbox\undefined \newbox\mplibscratchbox \fi
\def\mplibstarttoPDF#1#2#3#4{%
\hbox\bgroup
\xdef\MPllx{#1}\xdef\MPlly{#2}%
\xdef\MPurx{#3}\xdef\MPury{#4}%
\xdef\MPwidth{\the\dimexpr#3bp-#1bp\relax}%
\xdef\MPheight{\the\dimexpr#4bp-#2bp\relax}%
\parskip0pt%
\leftskip0pt%
\parindent0pt%
\everypar{}%
\setbox\mplibscratchbox\vbox\bgroup
\noindent
}
\def\mplibstoptoPDF{%
\egroup %
\setbox\mplibscratchbox\hbox %
{\hskip-\MPllx bp%
\raise-\MPlly bp%
\box\mplibscratchbox}%
\setbox\mplibscratchbox\vbox to \MPheight
{\vfill
\hsize\MPwidth
\wd\mplibscratchbox0pt%
\ht\mplibscratchbox0pt%
\dp\mplibscratchbox0pt%
\box\mplibscratchbox}%
\wd\mplibscratchbox\MPwidth
\ht\mplibscratchbox\MPheight
\box\mplibscratchbox
\egroup
}
\def\mplibtextext#1#2#3#4#5{%
\begingroup
\setbox\mplibscratchbox\hbox
{\font\temp=#1 at #2bp%
\temp
#3}%
\setbox\mplibscratchbox\hbox
{\hskip#4 bp%
\raise#5 bp%
\box\mplibscratchbox}%
\wd\mplibscratchbox0pt%
\ht\mplibscratchbox0pt%
\dp\mplibscratchbox0pt%
\box\mplibscratchbox
\endgroup
}
\endinput