summaryrefslogtreecommitdiff
path: root/macros/luatex/generic/barracuda/src/lib-driver/brcd-drv-pdfliteral.lua
blob: 50baea1472cc4b92d96e081d9d3309501741e503 (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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
--
-- ga graphic assembler or
-- Intermediate Graphic Language for barcode drawing
-- Copyright (C) 2019 Roberto Giacomelli
--
-- All dimensions are in scaled point (sp)
-- ga LuaTeX Driver (native implementation node+pdfliteral)

-- class for drawing elementary geometric elements
local PDFnative = {
    _VERSION     = "PDFnative v0.0.4",
    _NAME        = "PDFnative",
    _DESCRIPTION = "A LuaTeX native pdfliteral driver for ga graphic stream",
}

PDFnative.ext = "txt" -- file extension
PDFnative.buf_sep = "\n" -- separation string for buffer concat

function PDFnative.init_buffer(st) --> buffer, text buffer
    st.head = nil -- addition for text processing (to remember purpose)
    st.last = nil
    st.hbox = nil
    st.cw = nil
    st.x_hbox = nil
    st.char_counter = nil
    local bf = {"q"} -- create a new graphic stack
    local xt = {} -- text buffer
    return bf, xt
end

function PDFnative.close_buffer(st, bf, xt)
    bf[#bf + 1] = "Q" -- stack restoring
end

-- text utility special functions

local function newglue(w) --> node
    local n = node.new("glue")
    n.width = w
    return n
end

local function newglyph(c) --> node -- ? U+2423 ␣ for whitespace?
    if c == 32 then
        return newglue(tex.sp "3.5pt")
    end
    local n = node.new("glyph")
    n.char = c
    n.font = font.current()
    return n
end

local function newpdfliteral(buf) --> node
    local npdf = node.new("whatsit", "pdf_literal")
    npdf.mode = 0
    npdf.data = table.concat(buf, "\n")
    return npdf
end

local function append_glyph(head, last, c) --> head, last, xdim
    if c == 32 then -- space
        local space = tex.sp "3.5pt"
        head, last = node.insert_after(head, last, newglue(space))
        return head, last, space
    else
        local g = newglyph(c)
        local xdim = g.width
        head, last = node.insert_after(head, last, g)
        return head, last, xdim
    end
end

-- special drawing function
function PDFnative.hboxcreate(hboxname, st, buf, txt) --> ok, err
    local node = assert(node, "This is not LuaTeX!")
    local tex = assert(tex)
    local font = assert(font)
    if not tex.isbox(hboxname) then
        return nil, string.format("Box register [%s] doesn’t exist", hboxname)
    end
    local bb_x1, bb_y1, bb_x2, bb_y2 = st.bb_x1, st.bb_y1, st.bb_x2, st.bb_y2
    local npdf = newpdfliteral(buf) -- node whatsit pdfliteral
    -- vboxing, vertical correction
    local vpdf = node.vpack(npdf)
    vpdf.height = -bb_y1
    -- glue, for horizontal correction
    local ng = newglue(-bb_x1)
    local head, last = node.insert_after(ng, ng, vpdf)
    local xprev = 0.0
    for _, t in ipairs(txt) do -- text processing
        local gy = newglue(t[3] - bb_y1) -- n, x, y, w, h -- t[3] -t[5] - bb_y1
        local nvtxt = node.insert_after(t[1], t[1], gy)
        local vtxt = node.vpack(nvtxt)
        local gx = newglue(t[2]-xprev)
        head, last = node.insert_after(head, last, gx)
        head, last = node.insert_after(head, last, vtxt)
        xprev = t[2] + t[4]
    end
    -- hboxing
    local hbox = node.hpack(head)
    hbox.width  = bb_x2 - bb_x1
    hbox.height = bb_y2 - bb_y1
    tex.box[hboxname] = hbox
    return true, nil
end

-- PDF literal encoding functions

-- 1 <W: dim>; set line width
function PDFnative.append_001(st, bf, xt, w)
    local bp = st.bp -- 1bp (sp)
    bf[#bf + 1] = string.format("%0.6f w", w/bp)
end

-- 2 <enum: u8>; set line cap style
function PDFnative.append_002(st, bf, xt, cap)
    bf[#bf + 1] = string.format("%d J", cap)
end

-- 3 <enum: u8>; set line join style
function PDFnative.append_003(st, bf, xt, j)
    bf[#bf + 1] = string.format("%d j", j)
end

-- draw an horizontal line
-- 33 <x1: DIM> <x2: DIM> <y: DIM>
function PDFnative.append_033(st, bf, xt, x1, x2, y)
    local bp = st.bp -- conversion factor bp -> sp
    bf[#bf + 1] = string.format("% 0.6f %0.6f m", x1/bp, y/bp)
    bf[#bf + 1] = string.format("% 0.6f %0.6f l", x2/bp, y/bp)
    bf[#bf + 1] = "S" -- stroke
end

-- vline
-- draw a vertical line
-- 34 <y1: DIM> <y2: DIM> <x: DIM>
function PDFnative.append_034(st, bf, xt, y1, y2, x)
    local bp = st.bp -- conversion factor bp -> sp
    bf[#bf + 1] = string.format("% 0.6f %0.6f m", x/bp, y1/bp)
    bf[#bf + 1] = string.format("% 0.6f %0.6f l", x/bp, y2/bp)
    bf[#bf + 1] = "S" -- stroke
end

-- Vbar
-- draw a group of vertical lines
-- 36 <y1: DIM> <y2: DIM> <b: UINT> <x1: DIM> <t1: DIM> ...
function PDFnative.append_036_bar(st, bf, xt, x, w, y1, y2)
    local bp = st.bp -- conversion factor bp -> sp
    local fmt = "%0.6f %0.6f %0.6f %0.6f re"
    local x1 = x - w/2
    local h = y2 - y1
    -- pdf literal insertion <x y w h re>
    bf[#bf + 1] = string.format(fmt, x1/bp, y1/bp, w/bp, h/bp)
end
function PDFnative.append_036_stop(st, bf, xt, nbar, y1, y2)
    bf[#bf + 1] = "f" -- fill
    bf[#bf + 1] = "S" -- stroke
end

-- draw a rectangle
-- 48 <x1: DIM> <y1: DIM> <x2: DIM> <y2: DIM>
function PDFnative.append_048(st, bf, xt, x1, y1, x2, y2)
    local w = x2 - x1 -- rectangle width
    assert(w > 0)
    local h = y2 - y1 -- rectangle height
    assert(h > 0)
    local bp = st.bp -- conversion factor
    local fmt = "%0.6f %0.6f %0.6f %0.6f re S"
    -- pdf literal string <x y w h re>
    bf[#bf + 1] = string.format(fmt, x1/bp, y1/bp, w/bp, h/bp)
end

-- 130 <text> Text with several glyphs
-- 130 <ax: FLOAT> <ay: FLOAT> <xpos: DIM> <ypos: DIM> <c: CHARS>
function PDFnative.append_130_char(st, bf, xt, c)
    local head, last = st.head, st.last
    head, last = append_glyph(head, last, c)
    st.head = head
    st.last = last
end
-- return bounding box of the text object without deep
function PDFnative.append_130_stop(st, bf, xt, xpos, ypos, ax, ay)
    local head = assert(st.head)
    st.head = nil -- reset temporary reference
    st.last = nil
    local hbox = node.hpack(head)
    local w, h, d = node.dimensions(hbox)
    local x1 = xpos - ax*w -- text position
    local y1; if ay > 0 then
        y1 = ypos - h*ay
    else
        y1 = ypos - d*ay
    end
    xt[#xt + 1] = {hbox, x1, y1 - d, w, h}
    return x1, y1, x1 + w, y1 + h
end

-- 131 <text_xspaced>, Text with glyphs equally spaced on its vertical axis
-- 131 <x1: DIM> <xgap: DIM> <ay: FLOAT> <ypos: DIM> <c: CHARS>
function PDFnative.append_131_char(st, bf, xt, c, xgap)
    local head, last, prec_cw = st.head, st.last, st.cw
    local cw
    if prec_cw then
        local g = newglyph(c)
        cw = g.width
        local isp = xgap - (cw + prec_cw)/2
        local s = newglue(isp)
        head, last = node.insert_after(head, last, s)
        head, last = node.insert_after(head, last, g)
    else -- first char
        head, last, cw = append_glyph(head, last, c)
        st.x_hbox = cw/2
    end
    st.cw = cw
    st.head = head
    st.last = last
end
function PDFnative.append_131_stop(st, bf, xt, x1, xgap, ypos, ay) --> bb
    local head = assert(st.head)
    st.head = nil -- reset temporary reference
    st.last = nil
    st.cw = nil
    local hbox = node.hpack(head)
    local w, h, d = node.dimensions(hbox)
    local bx1 = x1 - st.x_hbox
    st.x_hbox = nil -- reset temporary references
    local y1; if ay > 0 then
        y1 = ypos - h*ay
    else
        y1 = ypos - d*ay
    end
    xt[#xt + 1] = {hbox, bx1, y1 - d, w, h}
    return bx1, y1, bx1 + w, y1 + h
end

-- 132 <text_xwidth> Glyphs equally spaced on vertical axis between two x coordinates
-- 132 <ay: FLOAT> <x1: DIM> <x2: DIM> <y: DIM> <c: CHARS>
function PDFnative.append_132_char(st, bf, xt, c)
    local head, last = st.head, st.last
    if head then
        local g = newglyph(c)
        local s = newglue(1)
        head, last = node.insert_after(head, last, s)
        head, last = node.insert_after(head, last, g)
        st.char_counter = st.char_counter + 1
    else -- first char
        head, last, cw = append_glyph(head, last, c)
        st.cw = cw
        st.char_counter = 1
    end
    st.head = head
    st.last = last
end
function PDFnative.append_132_stop(st, bf, xt, x1, x2, ypos, ay) --> p1, p2
    local head, last = st.head, st.last
    st.head = nil -- reset temporary registry
    st.last = nil
    local w_1 = st.cw
    st.cw = nil
    local i = st.char_counter
    st.char_counter = nil
    local w_n = last.width
    local xgap = (x2 - x1 - (w_1 + w_n)/2)/(i - 1)
    local c_curr = head
    for _ = 1, i - 1 do
        local g = c_curr.next
        local c_next = g.next
        local w_2
        w_1, w_2 = c_curr.width, c_next.width
        g.width = xgap - (w_1 + w_2)/2
        c_curr = c_next
    end
    local hbox = node.hpack(head)
    local w, h, d = node.dimensions(hbox)
    local y; if ay > 0 then
        y = ypos - h*ay
    else
        y = ypos - d*ay
    end
    xt[#xt + 1] = {hbox, x1, y - d, w, h}
    return x1, y, x2, y + h
end

return PDFnative