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

sile-typesetter / sile / 6713098919

31 Oct 2023 10:21PM UTC coverage: 52.831% (-21.8%) from 74.636%
6713098919

push

github

web-flow
Merge d0a2a1ee9 into b185d4972

45 of 45 new or added lines in 3 files covered. (100.0%)

8173 of 15470 relevant lines covered (52.83%)

6562.28 hits per line

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

0.0
/packages/svg/init.lua
1
local base = require("packages.base")
×
2

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

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

9
local _drawSVG = function (svgdata, width, height, density, drop)
10
  local svgfigure, svgwidth, svgheight = svg.svg_to_ps(svgdata, density)
×
11
  SU.debug("svg", string.format("PS: %s\n", svgfigure))
×
12
  local scalefactor = 1
×
13
  if width and height then
×
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
×
17
    scalefactor = width:tonumber() / svgwidth
×
18
  elseif height then
×
19
    scalefactor = height:tonumber() / svgheight
×
20
  end
21
  width = SILE.measurement(svgwidth * scalefactor)
×
22
  height = SILE.measurement(svgheight * scalefactor)
×
23
  scalefactor = scalefactor * density / 72
×
24
  SILE.typesetter:pushHbox({
×
25
      value = nil,
26
      height = height,
27
      width = width,
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)
×
31
        typesetter.frame:advanceWritingDirection(self.width)
×
32
      end
33
    })
34
end
35

36
function package:registerRawHandlers ()
×
37

38
  self:registerRawHandler("svg", function(options, content)
×
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 ()
×
53

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

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

69
  self:registerCommand("svg-glyph", function(_, content)
×
70
    local fontoptions = SILE.font.loadDefaults({})
×
71
    local items = SILE.shaper:shapeToken(content[1], fontoptions)
×
72
    local face = SILE.shaper.getFace(fontoptions)
×
73
    otparser.parseFont(face)
×
74
    if not face.font.svg then return SILE.process(content) end
×
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
]]
×
104

105
return package
×
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

© 2025 Coveralls, Inc