m-escrito.mkiv /size: 5165 b    last modification: 2021-10-28 13:51
1%D \module
2%D   [       file=m-escrito,
3%D        version=2015.09.27,
4%D          title=\CONTEXT\ Extra Modules,
5%D       subtitle=ESCRITO,
6%D         author={Taco Hoekwater \& Hans Hagen},
7%D           date=\currentdate,
8%D      copyright={\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\registerctxluafile{m-escrito}{}
15
16%D This is a fun project and not meant for production (yet). It's a follow up on a
17%D project by Taco presented at a Bacho\TEX\ meeting years ago. I probably messed up
18%D the code so much that some things don't work but then, fonts are not really
19%D supported well anyway. However for simple \POSTSCRIPT\ things work out ok.
20%D
21%D I (Hans) will occasionally have a look at the code. Who knows what our trips to
22%D \TEX\ meetings lead to.
23
24%D Ecause the (already split) barcode modules don't need this I'll postpone the
25%D splitting of this one till later. It's more fun project anyway and there are
26%D buglets as well as no support for tricky features.
27
28\unprotect
29
30\unexpanded\def\startESCRITOgraphic#1#2#3#4%
31  {\dontleavehmode
32   \begingroup
33   \MPllx#1\onebasepoint
34   \MPlly#2\onebasepoint
35   \MPurx#3\onebasepoint
36   \MPury#4\onebasepoint
37   \setbox\b_meta_graphic\hbox\bgroup}
38
39\unexpanded\def\stopESCRITOgraphic
40  {\egroup
41   \setbox\b_meta_graphic\ruledhbox\bgroup
42     \kern-\MPllx\raise-\MPlly\box\b_meta_graphic
43   \egroup
44   \wd\b_meta_graphic\dimexpr\MPurx-\MPllx\relax
45   \ht\b_meta_graphic\dimexpr\MPury-\MPlly\relax
46   \dp\b_meta_graphic\zeropoint
47   \box\b_meta_graphic
48   \endgroup}
49
50\unexpanded\def\flushESCRITOtext#1#2#3% no fratures so pretty weak, better use overlays
51  {\smash{\rlap{\definedfont[#1 at #2bp]#3}}}
52
53\unexpanded\def\stopESCRITO
54  {\edef\p_option{\namedbufferparameter{ESCRITO}\c!option}%
55   \ctxlua{escrito.convert {
56        buffer       = "\thedefinedbuffer{ESCRITO}",
57        calculatebox = \ifx\p_option\v!fit true\else false\fi,
58  }}}
59
60\unexpanded\def\processESCRITO[#1]%
61  {\begingroup
62   \getdummyparameters[\c!file=,\c!option=,#1]%
63   \edef\p_option{\dummyparameter\c!option}%
64   \ctxlua{escrito.convert {
65        filename     = "\dummyparameter\c!file",
66        calculatebox = \ifx\p_option\v!fit true\else false\fi,
67   }}%
68   \endgroup}
69
70\definebuffer
71  [ESCRITO]
72
73\setupbuffer
74  [ESCRITO]
75  [\c!option=,
76   \c!after=\processESCRITO]
77
78\protect
79
80% This will move to m-escrito.lua once we know how to deal with it ... no time
81% now.
82
83\startluacode
84
85    local p = escrito.devices.pdf
86
87    function p.startpage(llx,lly,urx,ury)
88        context.startESCRITOgraphic(llx,lly,urx,ury)
89    end
90
91    function p.stoppage()
92        context.stopESCRITOgraphic()
93    end
94
95    function p.flushpage(result)
96        context.pdfliteral(result) -- LMTX save (test suite reported issue)
97    end
98
99    -- todo
100
101    local fontnames = { }
102    local fontfiles = { }
103
104    fontnames['NimbusSanL-Regu']     = 'Sans'
105    fontnames['StandardSymL']        = 'rpsyr'
106    fontnames['dejavuserif-regular'] = 'dejavuserif-regular'
107
108    function p.showfont(object)
109        local color  = object.color
110        local ctype  = object.colortype
111        local matrix = object.matrix
112        local text   = object.string
113        local size   = object.fontmatrix[1] * 1000
114        local result = { "q" }
115        context(newliteral(formatters['%f %f %f %f %f %f cm'](matrix[1],matrix[2],matrix[3],matrix[4],matrix[5],matrix[6])))
116        if ctype == "rgb" then
117            local r, g, b = color[1], color[2], color[3]
118            context(newliteral(formatters["%f %f %f rg %f %f %f RG"](r,g,b,r,g,b)))
119        elseif ctype == "cmyk" then
120            local c, m, y, k = color[1], color[2], color[3], color[4]
121            context(newliteral(formatters["%f %f %f k %f %f %f K"](c,m,y,k,c,m,y,k)))
122        elseif ctype == "gray" then
123            context(newliteral(formatters["%f g %f G"](color,color)))
124        end
125        context.flushESCRITOtext(fontnames[object.fontname],size,text)
126        context(newliteral("Q"))
127    end
128
129    local function findfont(fontname)
130        return fontfiles[fontname]
131    end
132
133\stopluacode
134
135\continueifinputfile{m-escrito.mkiv}
136
137\starttext
138
139% \startluacode
140%     local n = 5
141%     for i=1,n do
142%         context.startTEXpage()
143%         local runtime = escrito.convert { filename = "tiger.eps", calculatebox = true }
144%         context.par()
145%         context("calculated boundingbox, time: %s",runtime)
146%         context.stopTEXpage()
147%     end
148%     for i=1,n do
149%         context.startTEXpage()
150%         local runtime = escrito.convert { filename = "tiger.eps", calculatebox = false }
151%         context.par()
152%         context("built in boundingbox, time: %s",runtime)
153%         context.stopTEXpage()
154%     end
155% \stopluacode
156
157\startTEXpage
158    \startESCRITO
159        (tiger.eps) run
160    \stopESCRITO
161\stopTEXpage
162
163\startTEXpage
164    \setupbuffer[ESCRITO][option=fit]%
165    \startESCRITO
166        (tiger.eps) run
167    \stopESCRITO
168\stopTEXpage
169
170\startTEXpage
171    \processESCRITO[file=tiger.eps]
172\stopTEXpage
173
174\startTEXpage
175    \processESCRITO[file=tiger.eps,option=fit]
176\stopTEXpage
177
178\stoptext
179
180