-
-
Notifications
You must be signed in to change notification settings - Fork 97
/
podofo.lua
115 lines (97 loc) · 3.09 KB
/
podofo.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
local base = require("outputters.base")
-- This output package is deprecated and should only be used as an
-- example of how to create alternative output backends, in comparison
-- with the libtexpdf and debug backends.
local pdf = require("podofo")
local imagesize = require("imagesize")
local cursorX = 0
local cursorY = 0
local document
local page
local painter
local pagesize
local lastkey
local podofoFaces = {}
local outputter = pl.class(base)
outputter._name = "podofo"
outputter.extension = "pdf"
function outputter._init (_)
base._init(self)
document = pdf.PdfMemDocument()
pagesize = pdf.PdfRect()
pagesize:SetWidth(SILE.documentState.paperSize[1])
pagesize:SetHeight(SILE.documentState.paperSize[2])
page = document:CreatePage(pagesize)
painter = pdf.PdfPainter()
painter:SetPage(page)
end
function outputter.newPage (_)
painter:FinishPage()
page = document:CreatePage(pagesize)
painter:SetPage(page)
end
function outputter:finish ()
painter:FinishPage()
self:runHooks("prefinish")
local fname = self:getOutputFilename()
document:Write(fname == "-" and "/dev/stdout" or fname)
end
function outputter.getCursor (_)
return cursorX, cursorY
end
function outputter.setCursor (_, x, y, relative)
local offset = relative and { x = cursorX, y = cursorY } or { x = 0, y = 0 }
cursorX = offset.x + x
cursorY = offset.y + SILE.documentState.paperSize[2] - y
end
function outputter.setColor (_, color)
painter:SetColor(color.r, color.g, color.b)
end
function outputter:drawHbox (value, _)
local x, y = self:getCursor()
if not value.glyphNames then return end
for i = 1, #(value.glyphNames) do
painter:DrawGlyph(document, x, y, value.glyphNames[i])
end
end
function outputter.setFont (_, options)
if SILE.font._key(options) == lastkey then return end
lastkey = SILE.font._key(options)
if not podofoFaces[lastkey] then
local ftface = SILE.font.cache(options, function () SU.error("Font should exist") end)
print(ftface.filename)
podofoFaces[lastkey] = document:CreateFont(ftface.face)
end
podofoFaces[lastkey]:SetFontSize(options.size)
painter:SetFont(podofoFaces[lastkey])
-- Podofo trashes the font, so we need to recompute.
SILE.fontCache[lastkey] = nil
end
function outputter.getImageSize (_, src)
local box_width, box_height, err = imagesize.imgsize(src)
if not box_width then
SU.error(err.." loading image")
end
return box_width, box_height
end
function outputter.drawRule (_, x, y, width, depth)
painter:Rectangle(x, SILE.documentState.paperSize[2] - y, width, depth)
painter:Close()
painter:Fill()
end
function outputter:debugHbox (hbox, scaledWidth)
painter:SetColor(0.9, 0.9, 0.9)
painter:SetStrokeWidth(0.5)
local x, y = self:getCursor()
painter:Rectangle(x, y+(hbox.height), scaledWidth, hbox.height+hbox.depth)
if (hbox.depth) then painter:Rectangle(x, y+(hbox.height), scaledWidth, hbox.height); end
painter:Stroke()
painter:SetColor(0, 0, 0)
--cr:move_to(x, y)
end
-- untested
function outputter:drawRaw (literal)
local x, y = self:getCursor()
painter:DrawText(x, y, literal, string.len(literal)
end
return outputter