Skip to content

Commit 13bd9d4

Browse files
committed
fix(outputters): Update cairo backend to work with current API
1 parent 4543aa3 commit 13bd9d4

1 file changed

Lines changed: 40 additions & 8 deletions

File tree

outputters/cairo.lua

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,39 +21,65 @@ local outputter = pl.class(base)
2121
outputter._name = "cairo"
2222
outputter.extension = "pdf"
2323

24+
local started = false
25+
local surface
26+
2427
function outputter:_init ()
2528
base._init(self)
29+
end
30+
31+
function outputter:_ensureInit ()
2632
local fname = self:getOutputFilename()
27-
local surface = cairo.PdfSurface.create(
28-
fname == "-" and "/dev/stdout" or fname,
29-
SILE.documentState.paperSize[1],
30-
SILE.documentState.paperSize[2]
31-
)
32-
cr = cairo.Context.create(surface)
33-
move = cr.move_to
34-
sgs = cr.show_glyph_string
33+
if not started then
34+
surface = cairo.PdfSurface.create(
35+
fname == "-" and "/dev/stdout" or fname,
36+
SILE.documentState.paperSize[1],
37+
SILE.documentState.paperSize[2]
38+
)
39+
cr = cairo.Context.create(surface)
40+
move = cr.move_to
41+
sgs = cr.show_glyph_string
42+
end
3543
end
3644

3745
function outputter:newPage ()
46+
self:_ensureInit()
47+
cr:show_page()
48+
end
49+
50+
function outputter:abort ()
51+
if started then
52+
surface:finish()
53+
end
54+
end
55+
56+
function outputter:finish()
57+
-- allows generation of empty PDFs
58+
self:_ensureInit()
59+
self:runHooks("prefinish")
3860
cr:show_page()
61+
surface:finish()
3962
end
4063

4164
function outputter:getCursor ()
4265
return cursorX, cursorY
4366
end
4467

4568
function outputter:setCursor (x, y, relative)
69+
self:_ensureInit()
4670
local offset = relative and { x = cursorX, y = cursorY } or { x = 0, y = 0 }
4771
cursorX = offset.x + x
4872
cursorY = offset.y - y
4973
move(cr, cursorX, cursorY)
5074
end
5175

5276
function outputter:setColor (color)
77+
self:_ensureInit()
5378
cr:set_source_rgb(color.r, color.g, color.b)
5479
end
5580

5681
function outputter:drawHbox (value, _)
82+
self:_ensureInit()
5783
if not value then
5884
return
5985
end
@@ -65,11 +91,13 @@ function outputter:drawHbox (value, _)
6591
end
6692

6793
function outputter:setFont (options)
94+
self:_ensureInit()
6895
cr:select_font_face(options.font, options.style:lower() == "italic" and 1 or 0, options.weight > 100 and 0 or 1)
6996
cr:set_font_size(options.size)
7097
end
7198

7299
function outputter:drawImage (src, x, y, width, height)
100+
self:_ensureInit()
73101
local image = cairo.ImageSurface.create_from_png(src)
74102
if not image then
75103
SU.error("Could not load image " .. src)
@@ -109,11 +137,13 @@ function outputter:getImageSize (src)
109137
end
110138

111139
function outputter:drawRule (x, y, width, depth)
140+
self:_ensureInit()
112141
cr:rectangle(x, y, width, depth)
113142
cr:fill()
114143
end
115144

116145
function outputter:debugFrame (frame)
146+
self:_ensureInit()
117147
cr:set_source_rgb(0.8, 0, 0)
118148
cr:set_line_width(0.5)
119149
cr:rectangle(frame:left(), frame:top(), frame:width(), frame:height())
@@ -124,6 +154,7 @@ function outputter:debugFrame (frame)
124154
end
125155

126156
function outputter:debugHbox (hbox, scaledWidth)
157+
self:_ensureInit()
127158
cr:set_source_rgb(0.9, 0.9, 0.9)
128159
cr:set_line_width(0.5)
129160
local x, y = self:getCursor()
@@ -138,6 +169,7 @@ end
138169

139170
-- untested
140171
function outputter:drawRaw (literal)
172+
self:_ensureInit()
141173
cr:show_text(literal)
142174
end
143175

0 commit comments

Comments
 (0)