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

sile-typesetter / sile / 5017842917

pending completion
5017842917

push

github

GitHub
Merge pull request #1738 from alerque/multi-inputs

16 of 16 new or added lines in 2 files covered. (100.0%)

9954 of 15641 relevant lines covered (63.64%)

6575.94 hits per line

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

75.93
/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.measurement(svgwidth * scalefactor)
2✔
22
  height = SILE.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(svgfigure, typesetter.frame.state.cursorX, typesetter.frame.state.cursorY, self.width, drop and 0 or self.height, scalefactor)
1✔
31
        typesetter.frame:advanceWritingDirection(self.width)
1✔
32
      end
33
    })
34
end
35

36
function package:registerRawHandlers ()
2✔
37

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

50
end
51

52
function package:registerCommands ()
2✔
53

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

65
  self:registerCommand("include-svg-file", function (_, _)
4✔
66
    SU.deprecated("\\include-svg-file", "\\svg", "0.10.10", "0.11.0")
×
67
  end, "Deprecated")
2✔
68

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

83
end
84

85
package.documentation = [[
86
\begin{document}
87
This package provides two commands.
88

89
The first is \autodoc:command{\svg[src=<file>]}.
90
This loads and parses an SVG file and attempts to render it in the current document.
91
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).
92
For example, the command \autodoc:command{\svg[src=packages/svg/smiley.svg,height=12pt]} produces the following:
93

94
\svg[src=packages/svg/smiley.svg,height=12pt]
95

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

100
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.
101
For a fuller implementation, consider using a \autodoc:package{converters} registration to render your SVG file to PDF and include it on the fly.
102
\end{document}
103
]]
2✔
104

105
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