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

sile-typesetter / sile / 9507147410

13 Jun 2024 09:40PM UTC coverage: 50.521% (-18.7%) from 69.177%
9507147410

push

github

web-flow
Merge pull request #2062 from alerque/plug-fluent-leak

Link document.language setting more closely with Fluent locale

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

3244 existing lines in 65 files now uncovered.

8586 of 16995 relevant lines covered (50.52%)

4659.09 hits per line

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

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

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

6
local pdf
7
local stPointer
UNCOV
8
local mcid = 0
×
UNCOV
9
local actualtext = {}
×
10
local structureNumberTree
UNCOV
11
local numberTreeIndex = 0
×
12

13
local function stNode (notetype)
UNCOV
14
   return {
×
15
      notetype = notetype,
16
      lang = SILE.settings:get("document.language"),
17
      kids = {},
18
      parent = stPointer,
19
   }
20
end
21

22
local function addChild (node)
UNCOV
23
   stPointer.kids[#stPointer.kids + 1] = node
×
UNCOV
24
   node.parent = stPointer
×
25
end
26

27
local function ensureStructureNumber (node, pdfnode)
UNCOV
28
   local p = node.page
×
UNCOV
29
   if not pdf.lookup_dictionary(p, "StructParents") then
×
UNCOV
30
      pdf.add_dict(p, pdf.parse("/StructParents"), pdf.parse(numberTreeIndex))
×
UNCOV
31
      local nums = pdf.lookup_dictionary(structureNumberTree, "Nums")
×
UNCOV
32
      pdf.push_array(nums, pdf.parse(numberTreeIndex))
×
UNCOV
33
      pdf.push_array(nums, pdf.parse("[]"))
×
UNCOV
34
      numberTreeIndex = numberTreeIndex + 1
×
35
   end
UNCOV
36
   local nums = pdf.lookup_dictionary(structureNumberTree, "Nums")
×
37
   -- This is an array and its last element is an array
UNCOV
38
   local r = pdf.get_array(nums, pdf.array_length(nums) - 1)
×
UNCOV
39
   pdf.push_array(r, pdf.reference(pdfnode))
×
40
end
41

42
local function dumpTree (node)
UNCOV
43
   local k = {}
×
UNCOV
44
   local pdfNode = pdf.parse("<< /Type /StructElem /S /" .. node.notetype .. ">>")
×
UNCOV
45
   if #node.kids > 0 then
×
UNCOV
46
      for i = 1, #node.kids do
×
UNCOV
47
         k[#k + 1] = dumpTree(node.kids[i])
×
48
      end
UNCOV
49
      local kArray = pdf.parse("[]")
×
UNCOV
50
      for i = 1, #k do
×
UNCOV
51
         pdf.push_array(kArray, k[i])
×
52
      end
UNCOV
53
      pdf.add_dict(pdfNode, pdf.parse("/K"), kArray)
×
54
   else
UNCOV
55
      pdf.add_dict(pdfNode, pdf.parse("/K"), pdf.parse(node.mcid))
×
56
   end
UNCOV
57
   if node.page then
×
UNCOV
58
      pdf.add_dict(pdfNode, pdf.parse("/Pg"), pdf.reference(node.page))
×
UNCOV
59
      ensureStructureNumber(node, pdfNode)
×
60
   end
UNCOV
61
   if node.lang then
×
UNCOV
62
      pdf.add_dict(pdfNode, pdf.parse("/Lang"), pdf.parse("(" .. node.lang:upper() .. ")"))
×
63
   end
64

UNCOV
65
   if node.actualtext then
×
UNCOV
66
      pdf.add_dict(pdfNode, pdf.parse("/ActualText"), pdf.string(node.actualtext))
×
67
   end
UNCOV
68
   local ref = pdf.reference(pdfNode)
×
UNCOV
69
   pdf.release(pdfNode)
×
UNCOV
70
   return ref
×
71
end
72

UNCOV
73
function package:_init ()
×
UNCOV
74
   base._init(self)
×
UNCOV
75
   pdf = require("justenoughlibtexpdf")
×
UNCOV
76
   local _typeset = SILE.typesetter.typeset
×
UNCOV
77
   SILE.typesetter.typeset = function (node, text)
×
UNCOV
78
      actualtext[#actualtext] = tostring(actualtext[#actualtext]) .. text
×
UNCOV
79
      _typeset(node, text)
×
80
   end
UNCOV
81
   local stRoot = stNode("Document")
×
UNCOV
82
   stPointer = stRoot
×
UNCOV
83
   self:loadPackage("pdf")
×
UNCOV
84
   SILE.outputter:registerHook("prefinish", function ()
×
UNCOV
85
      local catalog = pdf.get_dictionary("Catalog")
×
UNCOV
86
      local structureTree = pdf.parse("<< /Type /StructTreeRoot >>")
×
UNCOV
87
      pdf.add_dict(catalog, pdf.parse("/StructTreeRoot"), pdf.reference(structureTree))
×
UNCOV
88
      structureNumberTree = pdf.parse("<< /Nums [] >>")
×
UNCOV
89
      pdf.add_dict(structureTree, pdf.parse("/ParentTree"), pdf.reference(structureNumberTree))
×
UNCOV
90
      pdf.add_dict(structureTree, pdf.parse("/K"), dumpTree(stRoot))
×
UNCOV
91
      if structureNumberTree then
×
UNCOV
92
         pdf.release(structureNumberTree)
×
93
      end
UNCOV
94
      if structureTree then
×
UNCOV
95
         pdf.release(structureTree)
×
96
      end
97
   end)
98
end
99

UNCOV
100
function package:registerCommands ()
×
UNCOV
101
   self:registerCommand("pdf:structure", function (options, content)
×
UNCOV
102
      local notetype = SU.required(options, "type", "pdf structure")
×
UNCOV
103
      local node = stNode(notetype)
×
UNCOV
104
      addChild(node)
×
UNCOV
105
      node.lang = SILE.settings:get("document.language")
×
UNCOV
106
      if type(SILE.outputter._ensureInit) == "function" then
×
UNCOV
107
         SILE.outputter:_ensureInit()
×
108
      end
UNCOV
109
      node.page = pdf.get_dictionary("@THISPAGE")
×
UNCOV
110
      node.mcid = mcid
×
UNCOV
111
      local oldstPointer = stPointer
×
UNCOV
112
      stPointer = node
×
UNCOV
113
      actualtext[#actualtext + 1] = ""
×
UNCOV
114
      if not options.block then
×
UNCOV
115
         SILE.call("pdf:literal", {}, { "/" .. notetype .. " <</MCID " .. mcid .. " >>BDC" })
×
UNCOV
116
         mcid = mcid + 1
×
UNCOV
117
         SILE.process(content)
×
UNCOV
118
         SILE.call("pdf:literal", {}, { "EMC" })
×
119
      else
UNCOV
120
         SILE.process(content)
×
121
      end
UNCOV
122
      stPointer.actualtext = actualtext[#actualtext]
×
UNCOV
123
      actualtext[#actualtext] = nil
×
UNCOV
124
      stPointer = oldstPointer
×
125
   end)
126

UNCOV
127
   self:registerCommand("pdf:literal", function (_, content)
×
128
      -- NOTE: This method is used by the pdfstructure package and should
129
      -- probably be moved elsewhere, so there's no attempt here to delegate
130
      -- the low-level libtexpdf call to te outputter.
UNCOV
131
      if SILE.outputter._name ~= "libtexpdf" then
×
132
         SU.error("pdf package requires libtexpdf backend")
×
133
      end
UNCOV
134
      SILE.typesetter:pushHbox({
×
135
         value = nil,
136
         height = SILE.types.measurement(0),
137
         width = SILE.types.measurement(0),
138
         depth = SILE.types.measurement(0),
139
         outputYourself = function (_, _, _)
UNCOV
140
            SILE.outputter:drawRaw(content[1])
×
141
         end,
142
      })
143
   end)
144
end
145

146
package.documentation = [[
147
\begin{document}
148
\use[module=packages.pdfstructure]
149
\pdf:structure[type=P]{%
150
For PDF documents to be considered accessible, they must contain a description of the PDF’s document structure.
151
This package allows structure trees to be created and saved to the PDF file.
152
Currently this provides a low-level interface to creating nodes in the tree;
153
   classes which require PDF accessibility should use the \autodoc:command{\pdf:structure} command in their sectioning implementation to declare the document structure.
154
}
155

156
\pdf:structure[type=P]{%
157
See \code{tests/pdf.sil} for an example of using the \autodoc:package{pdfstructure} package to create a PDF/UA compatible document.
158
}
159
\end{document}
UNCOV
160
]]
×
161

UNCOV
162
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