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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
hpdf = require("hpdf")
mm = function(mm)
return mm * (72.0 / 25.4)
end
inch = function(inch)
return inch * 72.0
end
Document = {}
function Document:New()
local o = {
_inst = hpdf.New()
}
setmetatable(o, self)
self.__index = self
return o
end
function Document:SetCompressionMode(mode)
local modes = {
all = hpdf.COMP_ALL,
none = hpdf.COMP_NONE,
text = hpdf.COMP_TEXT,
image = hpdf.COMP_IMAGE,
metadata = hpdf.COMP_METADATA,
all = hpdf.COMP_ALL,
mask = hpdf.COMP_MASK,
}
hpdf.SetCompressionMode(self._inst, modes[mode])
end
function Document:UseUTFEncodings()
hpdf.UseUTFEncodings(self._inst)
end
function Document:GetFont(name, encoding)
return hpdf.GetFont(self._inst, name, encoding)
end
function Document:LoadTTFontFromFile(filename, embed, encoding)
local name = hpdf.LoadTTFontFromFile(self._inst, filename, embed)
return self:GetFont(name, encoding)
end
function Document:Save(filename)
hpdf.SaveToFile(self._inst, filename)
end
function Document:AddPage()
return Page:New(self)
end
Page = {}
function Page:New(doc)
local o = {
_inst = hpdf.AddPage(doc._inst),
default_line_width = mm(0.2),
margin = mm(10)
}
setmetatable(o, self)
self.__index = self
return o
end
function Page:SetSize(size, ori)
local sizes = {
A4 = "HPDF_PAGE_SIZE_A4",
}
local orientations = {
portrait = "HPDF_PAGE_PORTRAIT",
landscape = "HPDF_PAGE_LANDSCAPE",
}
hpdf.Page_SetSize(self._inst, sizes[size], orientations[ori])
end
function Page:BeginText()
hpdf.Page_BeginText(self._inst)
end
function Page:EndText()
hpdf.Page_EndText(self._inst)
end
function Page:TextRect(left, top, right, bottom, text, align)
local aligns = {
left = "HPDF_TALIGN_LEFT",
right = "HPDF_TALIGN_RIGHT",
center = "HPDF_TALIGN_CENTER",
justify = "HPDF_TALIGN_JUSTIFY",
}
hpdf.Page_TextRect(self._inst, left, top, right, bottom, text,
aligns[align])
end
function Page:SetFontAndSize(font, size)
hpdf.Page_SetFontAndSize(self._inst, font, size)
end
function Page:SetLineWidth(width)
hpdf.Page_SetLineWidth(self._inst, width)
end
function Page:MoveTo(x, y)
hpdf.Page_MoveTo(self._inst, x, y)
end
function Page:LineTo(x, y)
hpdf.Page_LineTo(self._inst, x, y)
end
function Page:Stroke()
hpdf.Page_Stroke(self._inst)
end
function Page:Line(x1, y1, x2, y2, width)
width = width or self.default_line_width
self:SetLineWidth(width)
self:MoveTo(x1, y1)
self:LineTo(x2, y2)
self:Stroke()
end
function Page:GetWidth()
return hpdf.Page_GetWidth(self._inst)
end
function Page:GetHeight()
return hpdf.Page_GetHeight(self._inst)
end
function Page:GetContentWidth()
return self:GetWidth() - self.margin * 2
end
function Page:GetContentHeight()
return self:GetHeight() - self.margin * 2
end
function Page:SetMargin(margin)
self.margin = margin
end
function Page:GetMargin()
return self.margin
end
function Page:CreateGrid(columns, rows)
self.columns = columns
self.column_width = self:GetContentWidth() / columns
self.rows = rows
self.row_height = self:GetContentHeight() / rows
end
function Page:DrawGrid()
local left = self:GetMargin()
local top = self:GetHeight() - self:GetMargin()
local right = self:GetWidth() - self:GetMargin()
local bottom = self:GetMargin()
for col = 0, self.columns do
local x = left + col * self.column_width
self:Line(x, top, x, bottom)
end
for row = 0, self.rows do
local y = top - row * self.row_height
self:Line(left, y, right, y)
end
end
function Page:TextCell(column, row, colspan, rowspan, text, align)
local left = self:GetMargin() + column * self.column_width
local top = self:GetHeight() - self:GetMargin() - row * self.row_height
local right = left + colspan * self.column_width
local bottom = top - rowspan * self.row_height
self:TextRect(left, top, right, bottom, text, align)
end
|