@@ -21,39 +21,65 @@ local outputter = pl.class(base)
2121outputter ._name = " cairo"
2222outputter .extension = " pdf"
2323
24+ local started = false
25+ local surface
26+
2427function 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
3543end
3644
3745function 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 ()
3962end
4063
4164function outputter :getCursor ()
4265 return cursorX , cursorY
4366end
4467
4568function 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 )
5074end
5175
5276function outputter :setColor (color )
77+ self :_ensureInit ()
5378 cr :set_source_rgb (color .r , color .g , color .b )
5479end
5580
5681function outputter :drawHbox (value , _ )
82+ self :_ensureInit ()
5783 if not value then
5884 return
5985 end
@@ -65,11 +91,13 @@ function outputter:drawHbox (value, _)
6591end
6692
6793function 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 )
7097end
7198
7299function 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)
109137end
110138
111139function outputter :drawRule (x , y , width , depth )
140+ self :_ensureInit ()
112141 cr :rectangle (x , y , width , depth )
113142 cr :fill ()
114143end
115144
116145function 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)
124154end
125155
126156function 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 ()
138169
139170-- untested
140171function outputter :drawRaw (literal )
172+ self :_ensureInit ()
141173 cr :show_text (literal )
142174end
143175
0 commit comments