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

sile-typesetter / sile / 10881107806

16 Sep 2024 09:29AM UTC coverage: 61.663% (-7.2%) from 68.912%
10881107806

push

github

web-flow
chore(deps): Bump DeterminateSystems/nix-installer-action from 13 to 14 (#2110)

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

10751 of 17435 relevant lines covered (61.66%)

2338.25 hits per line

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

78.69
/packages/svg/init.lua
1
local base = require("packages.base")
2✔
2

3
local package = pl.class(base)
2✔
4
package._name = "svg"
2✔
5

6
local svg = require("svg")
2✔
7
local otparser = require("core.opentype-parser")
2✔
8

9
local _drawSVG = function (svgdata, width, height, density, drop)
10
   local svgfigure, svgwidth, svgheight = svg.svg_to_ps(svgdata, density)
1✔
11
   SU.debug("svg", string.format("PS: %s\n", svgfigure))
1✔
12
   local scalefactor = 1
1✔
13
   if width and height then
1✔
14
      -- local aspect = svgwidth / svgheight
15
      SU.error("SILE cannot yet change SVG aspect ratios, specify either width or height but not both")
×
16
   elseif width then
1✔
17
      scalefactor = width:tonumber() / svgwidth
2✔
18
   elseif height then
×
19
      scalefactor = height:tonumber() / svgheight
×
20
   end
21
   width = SILE.types.measurement(svgwidth * scalefactor)
2✔
22
   height = SILE.types.measurement(svgheight * scalefactor)
2✔
23
   scalefactor = scalefactor * density / 72
1✔
24
   SILE.typesetter:pushHbox({
2✔
25
      value = nil,
26
      height = height,
1✔
27
      width = width,
1✔
28
      depth = 0,
29
      outputYourself = function (self, typesetter)
30
         SILE.outputter:drawSVG(
2✔
31
            svgfigure,
1✔
32
            typesetter.frame.state.cursorX,
1✔
33
            typesetter.frame.state.cursorY,
1✔
34
            self.width,
1✔
35
            drop and 0 or self.height,
1✔
36
            scalefactor
37
         )
1✔
38
         typesetter.frame:advanceWritingDirection(self.width)
1✔
39
      end,
40
   })
41
end
42

43
function package:registerRawHandlers ()
2✔
44
   self:registerRawHandler("svg", function (options, content)
4✔
45
      local svgdata = content[1]
×
46
      local width = options.width and SU.cast("measurement", options.width):absolute() or nil
×
47
      local height = options.height and SU.cast("measurement", options.height):absolute() or nil
×
48
      local density = options.density or 72
×
49
      -- See issue #1375: svg.svg_to_ps() called in _drawSVG has a apparently a side effect
50
      -- on the internal representation of the Lua string and corrupts it.
51
      -- So as a workaround, for the original string to be able to be reused, we must get a
52
      -- copy... So let's force some stupid comment concatenation here.
53
      _drawSVG("<!-- copy -->" .. svgdata, width, height, density)
×
54
   end)
55
end
56

57
function package:registerCommands ()
2✔
58
   self:registerCommand("svg", function (options, _)
4✔
59
      local src = SU.required(options, "src", "filename")
1✔
60
      src = SILE.resolveFile(src) or SU.error("Couldn't find file " .. src)
2✔
61
      local width = options.width and SU.cast("measurement", options.width):absolute() or nil
3✔
62
      local height = options.height and SU.cast("measurement", options.height):absolute() or nil
1✔
63
      local density = options.density or 72
1✔
64
      local svgfile = io.open(src)
1✔
65
      local svgdata = svgfile:read("*all")
1✔
66
      _drawSVG(svgdata, width, height, density)
1✔
67
   end)
68

69
   self:registerCommand("include-svg-file", function (_, _)
4✔
70
      SU.deprecated("\\include-svg-file", "\\svg", "0.10.10", "0.11.0")
×
71
   end, "Deprecated")
2✔
72

73
   self:registerCommand("svg-glyph", function (_, content)
4✔
74
      local fontoptions = SILE.font.loadDefaults({})
5✔
75
      local items = SILE.shaper:shapeToken(content[1], fontoptions)
5✔
76
      local face = SILE.shaper.getFace(fontoptions)
5✔
77
      otparser.parseFont(face)
5✔
78
      if not face.font.svg then
5✔
79
         return SILE.process(content)
5✔
80
      end
81
      for i = 1, #items do
×
82
         local svg_data = otparser.getSVG(face, items[i].gid)
×
83
         if svg_data then
×
84
            _drawSVG(svg_data, nil, fontoptions.size, 72, true)
×
85
         end
86
      end
87
   end)
88
end
89

90
package.documentation = [[
91
\begin{document}
92
This package provides two commands.
93

94
The first is \autodoc:command{\svg[src=<file>]}.
95
This loads and parses an SVG file and attempts to render it in the current document.
96
Optional \autodoc:parameter{width} or \autodoc:parameter{height} options will scale the SVG canvas to the given size calculated at a given \autodoc:parameter{density} option (which defaults to 72 ppi).
97
For example, the command \autodoc:command{\svg[src=packages/svg/smiley.svg,height=12pt]} produces the following:
98

99
\svg[src=packages/svg/smiley.svg,height=12pt]
100

101
The second is a more experimental \autodoc:command{\svg-glyph}.
102
When the current font is set to an SVG font, SILE does not currently render the SVG glyphs automatically.
103
This command is intended to be used as a means of eventually implementing SVG fonts; it retrieves the SVG glyph provided and renders it.
104

105
In both cases the rendering is done with our own SVG drawing library; it is currently very minimal, only handling lines, curves, strokes and fills.
106
For a fuller implementation, consider using a \autodoc:package{converters} registration to render your SVG file to PDF and include it on the fly.
107
\end{document}
108
]]
2✔
109

110
return package
2✔
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