sile 0.15.2

Simon’s Improved Layout Engine
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
local base = require("outputters.base")
local pdf = require("justenoughlibtexpdf")

local cursorX = 0
local cursorY = 0

local started = false
local lastkey = false

local debugfont = SILE.font.loadDefaults({ family = "Gentium Plus", language = "en", size = 10 })

local glyph2string = function (glyph)
   return string.char(math.floor(glyph % 2 ^ 32 / 2 ^ 8)) .. string.char(glyph % 0x100)
end

local _dl = 0.5

local _debugfont
local _font

local outputter = pl.class(base)
outputter._name = "libtexpdf"
outputter.extension = "pdf"

-- N.B. Sometimes setCoord is called before the outputter has ensured initialization.
-- This ok for coordinates manipulation, at these points we know the page size.
local deltaX
local deltaY
local function trueXCoord (x)
   if not deltaX then
      local sheetSize = SILE.documentState.sheetSize or SILE.documentState.paperSize
      deltaX = (sheetSize[1] - SILE.documentState.paperSize[1]) / 2
   end
   return x + deltaX
end
local function trueYCoord (y)
   if not deltaY then
      local sheetSize = SILE.documentState.sheetSize or SILE.documentState.paperSize
      deltaY = (sheetSize[2] - SILE.documentState.paperSize[2]) / 2
   end
   return y + deltaY
end

-- The outputter init can't actually initialize output (as logical as it might
-- have seemed) because that requires a page size which we don't know yet.
-- function outputter:_init () end

function outputter:_ensureInit ()
   if not started then
      local sheetSize = SILE.documentState.sheetSize or SILE.documentState.paperSize
      local w, h = sheetSize[1], sheetSize[2]
      local fname = self:getOutputFilename()
      -- Ideally we could want to set the PDF CropBox, BleedBox, TrimBox...
      -- Our wrapper only manages the MediaBox at this point.
      pdf.init(fname == "-" and "/dev/stdout" or fname, w, h, SILE.full_version)
      pdf.beginpage()
      started = true
   end
end

function outputter:newPage ()
   self:_ensureInit()
   pdf.endpage()
   pdf.beginpage()
end

-- pdf structure package needs a tie in here
function outputter._endHook (_) end

function outputter:finish ()
   self:_ensureInit()
   pdf.endpage()
   self:runHooks("prefinish")
   pdf.finish()
   started = false
   lastkey = nil
end

function outputter.getCursor (_)
   return cursorX, cursorY
end

function outputter.setCursor (_, x, y, relative)
   x = SU.cast("number", x)
   y = SU.cast("number", y)
   local offset = relative and { x = cursorX, y = cursorY } or { x = 0, y = 0 }
   cursorX = offset.x + x
   cursorY = offset.y + (relative and 0 or SILE.documentState.paperSize[2]) - y
end

function outputter:setColor (color)
   self:_ensureInit()
   if color.r then
      pdf.setcolor_rgb(color.r, color.g, color.b)
   end
   if color.c then
      pdf.setcolor_cmyk(color.c, color.m, color.y, color.k)
   end
   if color.l then
      pdf.setcolor_gray(color.l)
   end
end

function outputter:pushColor (color)
   self:_ensureInit()
   if color.r then
      pdf.colorpush_rgb(color.r, color.g, color.b)
   end
   if color.c then
      pdf.colorpush_cmyk(color.c, color.m, color.y, color.k)
   end
   if color.l then
      pdf.colorpush_gray(color.l)
   end
end

function outputter:popColor ()
   self:_ensureInit()
   pdf.colorpop()
end

function outputter:_drawString (str, width, x_offset, y_offset)
   local x, y = self:getCursor()
   pdf.colorpush_rgb(0, 0, 0)
   pdf.colorpop()
   pdf.setstring(trueXCoord(x + x_offset), trueYCoord(y + y_offset), str, string.len(str), _font, width)
end

function outputter:drawHbox (value, width)
   width = SU.cast("number", width)
   self:_ensureInit()
   if not value.glyphString then
      return
   end
   -- Nodes which require kerning or have offsets to the glyph
   -- position should be output a glyph at a time. We pass the
   -- glyph advance from the htmx table, so that libtexpdf knows
   -- how wide each glyph is. It uses this to then compute the
   -- relative position between the pen after the glyph has been
   -- painted (cursorX + glyphAdvance) and the next painting
   -- position (cursorX + width - remember that the box's "width"
   -- is actually the shaped x_advance).
   if value.complex then
      for i = 1, #value.items do
         local item = value.items[i]
         local buf = glyph2string(item.gid)
         self:_drawString(buf, item.glyphAdvance, item.x_offset or 0, item.y_offset or 0)
         self:setCursor(item.width, 0, true)
      end
   else
      local buf = {}
      for i = 1, #value.glyphString do
         buf[i] = glyph2string(value.glyphString[i])
      end
      buf = table.concat(buf, "")
      self:_drawString(buf, width, 0, 0)
   end
end

function outputter:_withDebugFont (callback)
   if not _debugfont then
      _debugfont = self:setFont(debugfont)
   end
   local oldfont = _font
   _font = _debugfont
   callback()
   _font = oldfont
end

function outputter:setFont (options)
   self:_ensureInit()
   local key = SILE.font._key(options)
   if lastkey and key == lastkey then
      return _font
   end
   local font = SILE.font.cache(options, SILE.shaper.getFace)
   if options.direction == "TTB" then
      font.layout_dir = 1
   end
   if SILE.typesetter.frame and SILE.typesetter.frame:writingDirection() == "TTB" then
      pdf.setdirmode(1)
   else
      pdf.setdirmode(0)
   end
   _font = pdf.loadfont(font)
   if _font < 0 then
      SU.error("Font loading error for " .. pl.pretty.write(options, ""))
   end
   lastkey = key
   return _font
end

function outputter:drawImage (src, x, y, width, height, pageno)
   x = SU.cast("number", x)
   y = SU.cast("number", y)
   width = SU.cast("number", width)
   height = SU.cast("number", height)
   self:_ensureInit()
   pdf.drawimage(src, trueXCoord(x), trueYCoord(y), width, height, pageno or 1)
end

function outputter:getImageSize (src, pageno)
   self:_ensureInit() -- in case it's a PDF file
   local llx, lly, urx, ury, xresol, yresol = pdf.imagebbox(src, pageno or 1)
   return (urx - llx), (ury - lly), xresol, yresol
end

function outputter:drawSVG (figure, x, y, _, height, scalefactor)
   self:_ensureInit()
   x = SU.cast("number", x)
   y = SU.cast("number", y)
   height = SU.cast("number", height)
   pdf.add_content("q")
   self:setCursor(x, y)
   x, y = self:getCursor()
   local sheetSize = SILE.documentState.sheetSize or SILE.documentState.paperSize
   local newy = y - SILE.documentState.paperSize[2] / 2 + height - sheetSize[2] / 2
   pdf.add_content(table.concat({ scalefactor, 0, 0, -scalefactor, trueXCoord(x), newy, "cm" }, " "))
   pdf.add_content(figure)
   pdf.add_content("Q")
end

function outputter:drawRule (x, y, width, height)
   x = SU.cast("number", x)
   y = SU.cast("number", y)
   width = SU.cast("number", width)
   height = SU.cast("number", height)
   self:_ensureInit()
   local paperY = SILE.documentState.paperSize[2]
   pdf.setrule(trueXCoord(x), trueYCoord(paperY - y - height), width, height)
end

function outputter:debugFrame (frame)
   self:_ensureInit()
   self:pushColor(SILE.types.color({ r = 0.8, g = 0, b = 0 }))
   self:drawRule(frame:left() - _dl / 2, frame:top() - _dl / 2, frame:width() + _dl, _dl)
   self:drawRule(frame:left() - _dl / 2, frame:top() - _dl / 2, _dl, frame:height() + _dl)
   self:drawRule(frame:right() - _dl / 2, frame:top() - _dl / 2, _dl, frame:height() + _dl)
   self:drawRule(frame:left() - _dl / 2, frame:bottom() - _dl / 2, frame:width() + _dl, _dl)
   -- self:drawRule(frame:left() + frame:width()/2 - 5, (frame:top() + frame:bottom())/2+5, 10, 10)
   local stuff = SILE.shaper:createNnodes(frame.id, debugfont)
   stuff = stuff[1].nodes[1].value.glyphString -- Horrible hack
   local buf = {}
   for i = 1, #stuff do
      buf[i] = glyph2string(stuff[i])
   end
   buf = table.concat(buf, "")
   self:_withDebugFont(function ()
      self:setCursor(frame:left():tonumber() - _dl / 2, frame:top():tonumber() + _dl / 2)
      self:_drawString(buf, 0, 0, 0)
   end)
   self:popColor()
end

function outputter:debugHbox (hbox, scaledWidth)
   self:_ensureInit()
   self:pushColor(SILE.types.color({ r = 0.8, g = 0.3, b = 0.3 }))
   local paperY = SILE.documentState.paperSize[2]
   local x, y = self:getCursor()
   y = paperY - y
   self:drawRule(x - _dl / 2, y - _dl / 2 - hbox.height, scaledWidth + _dl, _dl)
   self:drawRule(x - _dl / 2, y - hbox.height - _dl / 2, _dl, hbox.height + hbox.depth + _dl)
   self:drawRule(x - _dl / 2, y - _dl / 2, scaledWidth + _dl, _dl)
   self:drawRule(x + scaledWidth - _dl / 2, y - hbox.height - _dl / 2, _dl, hbox.height + hbox.depth + _dl)
   if hbox.depth > SILE.types.length(0) then
      self:drawRule(x - _dl / 2, y + hbox.depth - _dl / 2, scaledWidth + _dl, _dl)
   end
   self:popColor()
end

-- The methods below are only implemented on outputters supporting these features.
-- In PDF, it relies on transformation matrices, but other backends may call
-- for a different strategy.
-- ! The API is unstable and subject to change. !

function outputter:scaleFn (xorigin, yorigin, xratio, yratio, callback)
   xorigin = SU.cast("number", xorigin)
   yorigin = SU.cast("number", yorigin)
   local x0 = trueXCoord(xorigin)
   local y0 = -trueYCoord(yorigin)
   self:_ensureInit()
   pdf:gsave()
   pdf.setmatrix(1, 0, 0, 1, x0, y0)
   pdf.setmatrix(xratio, 0, 0, yratio, 0, 0)
   pdf.setmatrix(1, 0, 0, 1, -x0, -y0)
   callback()
   pdf:grestore()
end

function outputter:rotateFn (xorigin, yorigin, theta, callback)
   xorigin = SU.cast("number", xorigin)
   yorigin = SU.cast("number", yorigin)
   local x0 = trueXCoord(xorigin)
   local y0 = -trueYCoord(yorigin)
   self:_ensureInit()
   pdf:gsave()
   pdf.setmatrix(1, 0, 0, 1, x0, y0)
   pdf.setmatrix(math.cos(theta), math.sin(theta), -math.sin(theta), math.cos(theta), 0, 0)
   pdf.setmatrix(1, 0, 0, 1, -x0, -y0)
   callback()
   pdf:grestore()
end

-- Other rotation unstable APIs

function outputter:enterFrameRotate (xa, xb, y, theta) -- Unstable API see rotate package
   xa = SU.cast("number", xa)
   xb = SU.cast("number", xb)
   y = SU.cast("number", y)
   -- Keep center point the same?
   local cx0 = trueXCoord(xa)
   local cx1 = trueXCoord(xb)
   local cy = -trueYCoord(y)
   self:_ensureInit()
   pdf:gsave()
   pdf.setmatrix(1, 0, 0, 1, cx1, cy)
   pdf.setmatrix(math.cos(theta), math.sin(theta), -math.sin(theta), math.cos(theta), 0, 0)
   pdf.setmatrix(1, 0, 0, 1, -cx0, -cy)
end

function outputter.leaveFrameRotate (_)
   pdf:grestore()
end

-- Unstable link APIs

function outputter:setLinkAnchor (name, x, y)
   x = SU.cast("number", x)
   y = SU.cast("number", y)
   self:_ensureInit()
   pdf.destination(name, trueXCoord(x), trueYCoord(y))
end

local function borderColor (color)
   if color then
      if color.r then
         return "/C [" .. color.r .. " " .. color.g .. " " .. color.b .. "]"
      end
      if color.c then
         return "/C [" .. color.c .. " " .. color.m .. " " .. color.y .. " " .. color.k .. "]"
      end
      if color.l then
         return "/C [" .. color.l .. "]"
      end
   end
   return ""
end
local function borderStyle (style, width)
   if style == "underline" then
      return "/BS<</Type/Border/S/U/W " .. width .. ">>"
   end
   if style == "dashed" then
      return "/BS<</Type/Border/S/D/D[3 2]/W " .. width .. ">>"
   end
   return "/Border[0 0 " .. width .. "]"
end

function outputter:startLink (_, _) -- destination, options as argument
   self:_ensureInit()
   -- HACK:
   -- Looking at the code, pdf.begin_annotation does nothing, and Simon wrote a comment
   -- about tracking boxes. Unsure what he implied with this obscure statement.
   -- Sure thing is that some backends may need the destination here, e.g. an HTML backend
   -- would generate a <a href="#destination">, as well as the options possibly for styling
   -- on the link opening?
   pdf.begin_annotation()
end

function outputter.endLink (_, dest, opts, x0, y0, x1, y1)
   local bordercolor = borderColor(opts.bordercolor)
   local borderwidth = SU.cast("integer", opts.borderwidth)
   local borderstyle = borderStyle(opts.borderstyle, borderwidth)
   local target = opts.external and "/Type/Action/S/URI/URI" or "/S/GoTo/D"
   local d = "<</Type/Annot/Subtype/Link" .. borderstyle .. bordercolor .. "/A<<" .. target .. "(" .. dest .. ")>>>>"
   pdf.end_annotation(
      d,
      trueXCoord(x0),
      trueYCoord(y0 - opts.borderoffset),
      trueXCoord(x1),
      trueYCoord(y1 + opts.borderoffset)
   )
end

-- Bookmarks and metadata

local function validate_date (date)
   return string.match(date, [[^D:%d+%s*-%s*%d%d%s*'%s*%d%d%s*'?$]]) ~= nil
end

function outputter:setMetadata (key, value)
   if key == "Trapped" then
      SU.warn("Skipping special metadata key \\Trapped")
      return
   end

   if key == "ModDate" or key == "CreationDate" then
      if not validate_date(value) then
         SU.warn("Invalid date: " .. value)
         return
      end
   else
      -- see comment in on bookmark
      value = SU.utf8_to_utf16be(value)
   end
   self:_ensureInit()
   pdf.metadata(key, value)
end

function outputter:setBookmark (dest, title, level)
   -- For annotations and bookmarks, text strings must be encoded using
   -- either PDFDocEncoding or UTF16-BE with a leading byte-order marker.
   -- As PDFDocEncoding supports only limited character repertoire for
   -- European languages, we use UTF-16BE for internationalization.
   local ustr = SU.utf8_to_utf16be_hexencoded(title)
   local d = "<</Title<" .. ustr .. ">/A<</S/GoTo/D(" .. dest .. ")>>>>"
   self:_ensureInit()
   pdf.bookmark(d, level)
end

-- Assumes the caller known what they want to stuff in raw PDF format
function outputter:drawRaw (literal)
   self:_ensureInit()
   pdf.add_content(literal)
end

return outputter