• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

sile-typesetter / sile / 14335207249

08 Apr 2025 01:46PM UTC coverage: 60.462% (+0.9%) from 59.535%
14335207249

push

github

web-flow
Merge pull request #2257 from alerque/lintery

98 of 147 new or added lines in 46 files covered. (66.67%)

1 existing line in 1 file now uncovered.

13061 of 21602 relevant lines covered (60.46%)

4611.17 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/outputters/cairo.lua
1
local base = require("outputters.base")
×
2

3
-- This output package is deprecated and should only be used as an
4
-- example of how to create alternative output backends, in comparison
5
-- with the libtexpdf and debug backends.
6
local lgi = require("lgi")
×
7
local cairolgi = lgi.cairo
×
8
-- local pango = lgi.Pango
9
-- local fm = lgi.PangoCairo.FontMap.get_default()
10
-- local pango_context = lgi.Pango.FontMap.create_context(fm)
11
local imagesize = require("imagesize")
×
12

13
local cursorX = 0
×
14
local cursorY = 0
×
15

16
local cr
17
local move -- See https://github.com/pavouk/lgi/issues/48
18
local sgs
19

20
local outputter = pl.class(base)
×
21
outputter._name = "cairo"
×
22
outputter.extension = "pdf"
×
23

24
function outputter:_init ()
×
25
   base._init(self)
×
26
   local fname = self:getOutputFilename()
×
27
   local surface = cairolgi.PdfSurface.create(
×
28
      fname == "-" and "/dev/stdout" or fname,
29
      SILE.documentState.paperSize[1],
×
30
      SILE.documentState.paperSize[2]
×
31
   )
32
   cr = cairolgi.Context.create(surface)
×
33
   move = cr.move_to
×
34
   sgs = cr.show_glyph_string
×
35
end
36

NEW
37
function outputter:newPage ()
×
38
   cr:show_page()
×
39
end
40

NEW
41
function outputter:getCursor ()
×
42
   return cursorX, cursorY
×
43
end
44

NEW
45
function outputter:setCursor (x, y, relative)
×
46
   local offset = relative and { x = cursorX, y = cursorY } or { x = 0, y = 0 }
×
47
   cursorX = offset.x + x
×
48
   cursorY = offset.y - y
×
49
   move(cr, cursorX, cursorY)
×
50
end
51

NEW
52
function outputter:setColor (color)
×
53
   cr:set_source_rgb(color.r, color.g, color.b)
×
54
end
55

NEW
56
function outputter:drawHbox (value, _)
×
57
   if not value then
×
58
      return
×
59
   end
60
   if value.pgs then
×
61
      sgs(cr, value.font, value.pgs)
×
62
   elseif value.text then
×
63
      cr:show_text(value.text)
×
64
   end
65
end
66

NEW
67
function outputter:setFont (options)
×
68
   cr:select_font_face(options.font, options.style:lower() == "italic" and 1 or 0, options.weight > 100 and 0 or 1)
×
69
   cr:set_font_size(options.size)
×
70
end
71

NEW
72
function outputter:drawImage (src, x, y, width, height)
×
73
   local image = cairolgi.ImageSurface.create_from_png(src)
×
74
   if not image then
×
75
      SU.error("Could not load image " .. src)
×
76
   end
77
   local src_width = image:get_width()
×
78
   local src_height = image:get_height()
×
79
   if not (src_width > 0) then
×
80
      SU.error("Something went wrong loading image " .. src)
×
81
   end
82
   cr:save()
×
83
   cr:set_source_surface(image, 0, 0)
×
84
   local p = cr:get_source()
×
85
   local matrix, sx, sy
86
   if width or height then
×
87
      if width > 0 then
×
88
         sx = src_width / width
×
89
      end
90
      if height > 0 then
×
91
         sy = src_height / height
×
92
      end
93
      matrix = cairolgi.Matrix.create_scale(sx or sy, sy or sx)
×
94
   else
95
      matrix = cairolgi.Matrix.create_identity()
×
96
   end
97
   matrix:translate(-x, -y)
×
98
   p:set_matrix(matrix)
×
99
   cr:paint()
×
100
   cr:restore()
×
101
end
102

NEW
103
function outputter:getImageSize (src)
×
104
   local box_width, box_height, err = imagesize.imgsize(src)
×
105
   if not box_width then
×
106
      SU.error(err .. " loading image")
×
107
   end
108
   return box_width, box_height
×
109
end
110

NEW
111
function outputter:drawRule (x, y, width, depth)
×
112
   cr:rectangle(x, y, width, depth)
×
113
   cr:fill()
×
114
end
115

NEW
116
function outputter:debugFrame (frame)
×
117
   cr:set_source_rgb(0.8, 0, 0)
×
118
   cr:set_line_width(0.5)
×
119
   cr:rectangle(frame:left(), frame:top(), frame:width(), frame:height())
×
120
   cr:stroke()
×
121
   cr:move_to(frame:left() - 10, frame:top() - 2)
×
122
   cr:show_text(frame.id)
×
123
   cr:set_source_rgb(0, 0, 0)
×
124
end
125

126
function outputter:debugHbox (hbox, scaledWidth)
×
127
   cr:set_source_rgb(0.9, 0.9, 0.9)
×
128
   cr:set_line_width(0.5)
×
129
   local x, y = self:getCursor()
×
130
   cr:rectangle(x, y - hbox.height, scaledWidth, hbox.height + hbox.depth)
×
131
   if hbox.depth then
×
132
      cr:rectangle(x, y - hbox.height, scaledWidth, hbox.height)
×
133
   end
134
   cr:stroke()
×
135
   cr:set_source_rgb(0, 0, 0)
×
136
   cr:move_to(x, y)
×
137
end
138

139
-- untested
NEW
140
function outputter:drawRaw (literal)
×
141
   cr:show_text(literal)
×
142
end
143

144
return outputter
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc