summaryrefslogtreecommitdiff
path: root/tex/context/base/mlib-pdf.mkiv
blob: 0f2f4c728c3bd74cde30c1fff9be7f140ed1e0a7 (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
%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

\registerctxluafile{mlib-pdf}{1.001}

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

\def\MPLIBboundingbox#1#2#3#4%
  {\xdef\MPllx{#1}%
   \xdef\MPlly{#2}%
   \xdef\MPurx{#3}%
   \xdef\MPury{#4}%
   \xdef\MPwidth {\the\dimexpr#3\onebasepoint-#1\onebasepoint\relax}%
   \xdef\MPheight{\the\dimexpr#4\onebasepoint-#2\onebasepoint\relax}}

\def\startMPLIBtoPDF#1#2#3#4% watch the transparency reset
  {\dostarttagged\t!mpgraphic\empty
   \naturalhbox attr \imageattribute 1 \bgroup
   \doactivatecolor\s!black\forcecolorhack
   \MPLIBboundingbox{#1}{#2}{#3}{#4}%
  %\forgetall % done already elsewhere
   \setbox\scratchbox\vbox\bgroup
   \noindent % this is really needed in order to force tex into proper cm's
   \startMPresources}

\def\stopMPLIBtoPDF % watch the transparency reset
  {%\dohandleMPresettransparency % not needed
   \stopMPresources
   \egroup
   \setbox\scratchbox\hbox\bgroup
     \hskip-\MPllx\onebasepoint
     \raise-\MPlly\onebasepoint
     \box\scratchbox
   \egroup
   \setbox\scratchbox\vbox to \MPheight\bgroup
     \vfill
     \hsize\MPwidth
     \smashbox\scratchbox
     \box\scratchbox
   \egroup
   \wd\scratchbox\MPwidth
   \ht\scratchbox\MPheight
   \dopackageMPgraphic\scratchbox
   \egroup
   \dostoptagged}

% \def\MPLIBtextext#1#2#3#4#5%
%   {\begingroup
%    \def\MPtextdata{#3}% delegate the splitter to lua
%    \defconvertedcommand\MPtextdata\MPtextdata % no edef
%    \splitstring\MPtextdata\at::::\to\MPtexttag\and\MPtextnumber
%    \executeifdefined{handleMPtext\MPtexttag}
%      {\setbox\scratchbox\hbox
%         {\font\temp=#1\space at #2\onebasepoint
%          \let\c\char
%          \temp
%          \MPfshowcommand{#3}}%
%       \setbox\scratchbox\hbox
%         {\hskip#4\onebasepoint
%          \raise#5\onebasepoint
%          \box\scratchbox}%
%       \smashbox\scratchbox
%       \box\scratchbox}%
%    \endgroup}

\def\MPLIBtextext#1#2#3#4#5%
  {\begingroup
   \setbox\scratchbox\hbox
     {\font\temp=#1\space at #2\onebasepoint
      \let\c\char
      \temp
      #3}%
   \setbox\scratchbox\hbox
     {\hskip#4\onebasepoint
      \raise#5\onebasepoint
      \box\scratchbox}%
   \smashbox\scratchbox
   \box\scratchbox
   \endgroup}

\def\MPLIBflushreset
  {\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\dodirectMPgraphic}

\unexpanded\long\def\directMPgraphic#1#2% makes pages (todo: make boxes)
  {\beginMPgraphicgroup{#1}%
   \let\startMPLIBtoPDF\directstartMPLIBtoPDF
   \let\stopMPLIBtoPDF \directstopMPLIBtoPDF
   \dostartcurrentMPgraphic
   \forgetall
   \normalexpanded{\noexpand\ctxlua{metapost.graphic(
     "\currentMPgraphicinstance",
     "\currentMPgraphicformat",
      \!!bs#2;\!!es,
      \!!bs\currentMPinitializations;\!!es,
      \!!bs\currentMPpreamble;\!!es,
     "all"
   )}}%
   \dostopcurrentMPgraphic
   \endMPgraphicgroup}

\protect \endinput