Commit 9ede5c7a90d494de4c0904120595b3a82c933adc
1 parent
28aa66b2f4
Exists in
master
removed unused stuff
Showing
1 changed file
with
55 additions
and
150 deletions
Show diff stats
pdfhelper/pdf.go
... | ... | @@ -29,6 +29,17 @@ const ( |
29 | 29 | latinEncoding = "cp1250" |
30 | 30 | ) |
31 | 31 | |
32 | +const threeDots = "\u2056\u2056\u2056" | |
33 | + | |
34 | +type TableCell struct { | |
35 | + W, H float64 | |
36 | + Text string | |
37 | + Font, FontStyle string | |
38 | + FontSize float64 | |
39 | + Border string | |
40 | + Alignment string | |
41 | +} | |
42 | + | |
32 | 43 | // Helper ... |
33 | 44 | type Helper struct { |
34 | 45 | *gofpdf.Fpdf |
... | ... | @@ -50,106 +61,31 @@ func (pdf *Helper) LoadTranslators() { |
50 | 61 | pdf.translators[cyrillicEncoding] = pdf.UnicodeTranslatorFromDescriptor(cyrillicEncoding) |
51 | 62 | } |
52 | 63 | |
53 | -// InsertTab ... | |
54 | -func (pdf *Helper) InsertTab(count int, w, h float64) { | |
55 | - for i := 0; i < count; i++ { | |
56 | - pdf.Cell(w, h, "") | |
57 | - } | |
58 | -} | |
59 | - | |
60 | -// CellWithBox ... | |
61 | -func (pdf *Helper) CellWithBox(w, h float64, text, align string) { | |
62 | - //pdf.drawCellMargins(w, h) | |
63 | - //pdf.Cell(w, h, text) | |
64 | - pdf.CellFormat(w, h, pdf.ToUTF8(text), FULL, CONTINUE, align, false, 0, "") | |
65 | -} | |
66 | - | |
67 | -func (pdf *Helper) CellWithMargins(w, h float64, text, align string, index, of int) { | |
68 | - len := of - 1 | |
69 | - border := "" | |
70 | - | |
71 | - // if top cell | |
72 | - if index == 0 { | |
73 | - border += "T" | |
74 | - } | |
75 | - | |
76 | - border += "LR" // always draw these | |
77 | - | |
78 | - // if bottom cell | |
79 | - if index == len { | |
80 | - border += "B" | |
81 | - } | |
82 | - | |
83 | - pdf.CellFormat(w, h, pdf.ToUTF8(text), border, CONTINUE, align, false, 0, "") | |
84 | -} | |
85 | - | |
86 | -// MultiRowCellWithBox ... | |
87 | -func (pdf *Helper) MultiRowCellWithBox(w, h float64, text []string, align string) { | |
88 | - pdf.drawCellMargins(w, h*float64(len(text))) | |
89 | - for i := range text { | |
90 | - pdf.CellFormat(w, h, pdf.ToUTF8(text[i]), "", BELLOW, align, false, 0, "") | |
91 | - } | |
92 | -} | |
93 | - | |
94 | -// CellFormat(w, h, text, borders, newLine, fill) | |
95 | - | |
96 | -type FormatedCell struct { | |
97 | - W, H float64 | |
98 | - Text string | |
99 | - Font, FontStyle string | |
100 | - FontSize float64 | |
101 | - Border string | |
102 | - Alignment string | |
103 | -} | |
104 | - | |
105 | -func (pdf *Helper) FCell(x, y float64, c FormatedCell) { | |
64 | +func (pdf *Helper) DrawCell(x, y float64, c TableCell) { | |
106 | 65 | pdf.SetXY(x, y) |
107 | 66 | pdf.SetFont(c.Font, c.FontStyle, c.FontSize) |
108 | - pdf.CellFormat(c.W, c.H, pdf.ToUTF8(c.Text), c.Border, BELLOW, c.Alignment, false, 0, "") | |
67 | + pdf.CellFormat(c.W, c.H, pdf.toUTF8(c.Text), c.Border, BELLOW, c.Alignment, false, 0, "") | |
109 | 68 | } |
110 | 69 | |
111 | -func (pdf *Helper) Column(x, y float64, cells []FormatedCell) { | |
70 | +func (pdf *Helper) DrawColumn(x, y float64, cells []TableCell) { | |
112 | 71 | pdf.SetXY(x, y) |
113 | 72 | for _, c := range cells { |
114 | 73 | pdf.SetFont(c.Font, c.FontStyle, c.FontSize) |
115 | - pdf.CellFormat(c.W, c.H, pdf.ToUTF8(c.Text), c.Border, BELLOW, c.Alignment, false, 0, "") | |
116 | - //pdf.CellFormat(c.w, c.h, c.text, c.border, BELLOW, align, false, 0, "") | |
74 | + pdf.CellFormat(c.W, c.H, pdf.toUTF8(c.Text), c.Border, BELLOW, c.Alignment, false, 0, "") | |
117 | 75 | } |
118 | 76 | } |
119 | 77 | |
120 | -// NewLine ... | |
121 | -func (pdf *Helper) NewLine(h float64) { | |
122 | - pdf.Ln(h) | |
123 | -} | |
124 | - | |
125 | -// WriteColumns ... | |
126 | -func (pdf *Helper) WriteColumns(align string, cols []PDFCell) { | |
127 | - for _, c := range cols { | |
128 | - pdf.CellFormat(c.width, c.height, pdf.ToUTF8(c.data), "", CONTINUE, align, false, 0, "") | |
129 | - } | |
130 | -} | |
131 | - | |
132 | -func (pdf *Helper) Row(x, y float64, cells []FormatedCell) { | |
78 | +func (pdf *Helper) DrawRow(x, y float64, cells []TableCell) { | |
133 | 79 | pdf.SetXY(x, y) |
134 | 80 | for _, c := range cells { |
135 | 81 | pdf.SetFont(c.Font, c.FontStyle, c.FontSize) |
136 | - pdf.CellFormat(c.W, c.H, pdf.ToUTF8(c.Text), c.Border, CONTINUE, c.Alignment, false, 0, "") | |
82 | + pdf.CellFormat(c.W, c.H, pdf.toUTF8(c.Text), c.Border, CONTINUE, c.Alignment, false, 0, "") | |
137 | 83 | } |
138 | 84 | } |
139 | 85 | |
140 | -const threeDots = "\u2056\u2056\u2056" | |
141 | - | |
142 | -// WriteColumnsWithAlignment ... | |
143 | -func (pdf *Helper) WriteColumnsWithAlignment(cols []PDFCellAligned) { | |
144 | - for _, c := range cols { | |
145 | - lines := pdf.SplitText(c.data, c.width) | |
146 | - if len(lines) == 1 { | |
147 | - pdf.CellFormat(c.width, c.height, pdf.ToUTF8(lines[0]), "", CONTINUE, c.alignment, false, 0, "") | |
148 | - } else { | |
149 | - pdf.CellFormat(c.width, c.height, pdf.ToUTF8(lines[0]+threeDots), "", CONTINUE, c.alignment, false, 0, "") | |
150 | - } | |
151 | - } | |
152 | - | |
86 | +func (pdf *Helper) TextLength(txt, family, style string, size float64) float64 { | |
87 | + family, _, _, _ = pdf.setCorrectFontFamily(textEncoding(txt)) | |
88 | + return pdf.Fpdf.TextLength(txt, family, style, size) | |
153 | 89 | } |
154 | 90 | |
155 | 91 | func (pdf *Helper) LimitText(text, limiter string, maxWidth float64) string { |
... | ... | @@ -176,22 +112,45 @@ func (pdf *Helper) InsertImage(img string, x, y, w, h float64) { |
176 | 112 | pdf.ImageOptions(img, x, y, w, h, autoBreak, opt, 0, "") |
177 | 113 | } |
178 | 114 | |
179 | -// PDFCell ... | |
180 | -type PDFCell struct { | |
181 | - data string | |
182 | - height, width float64 | |
115 | +func (pdf *Helper) PageHasSpace(requiredHeight float64) bool { | |
116 | + _, h := pdf.GetPageSize() | |
117 | + _, _, _, bot := pdf.GetMargins() | |
118 | + return (h - bot - pdf.GetY()) > requiredHeight | |
183 | 119 | } |
184 | 120 | |
185 | -// PDFCellAligned ... | |
186 | -type PDFCellAligned struct { | |
187 | - alignment string | |
188 | - data string | |
189 | - height, width float64 | |
121 | +// DrawBox ... | |
122 | +func (pdf Helper) DrawBox(x0, y0, w, h float64) { | |
123 | + pdf.Line(x0, y0, x0+w, y0) | |
124 | + pdf.Line(x0+w, y0, x0+w, y0+h) | |
125 | + pdf.Line(x0+w, y0+h, x0, y0+h) | |
126 | + pdf.Line(x0, y0+h, x0, y0) | |
190 | 127 | } |
191 | 128 | |
192 | -func (pdf *Helper) TextLength(txt, family, style string, size float64) float64 { | |
193 | - family, _, _, _ = pdf.setCorrectFontFamily(textEncoding(txt)) | |
194 | - return pdf.Fpdf.TextLength(txt, family, style, size) | |
129 | +// Strana %d/{TotalPages} | |
130 | +func (pdf *Helper) InsertPageNumber(x, y float64, format string) { | |
131 | + num := fmt.Sprintf(format, pdf.PageNo()) | |
132 | + pdf.DrawColumn(x, y, []TableCell{{10, 1, num, "DejaVuSans", "", 8, NOBORDER, LEFT}}) | |
133 | +} | |
134 | + | |
135 | +func (pdf *Helper) SuperscriptText(x, y, cw, ch float64, text, script string) { | |
136 | + family, style, size, sizeU := pdf.setCorrectFontFamily(textEncoding(text)) | |
137 | + | |
138 | + pdf.DrawCell(x, y, TableCell{cw, ch, text, family, style, size, NOBORDER, LEFT}) | |
139 | + | |
140 | + sx := x + pdf.TextLength(text, family, style, size) | |
141 | + sy := y - sizeU*0.2 | |
142 | + pdf.DrawCell(sx, sy, TableCell{cw, ch, script, family, style, size - 2, NOBORDER, LEFT}) | |
143 | +} | |
144 | + | |
145 | +// toUTF8 ... | |
146 | +func (pdf *Helper) toUTF8(s string) string { | |
147 | + encoding := textEncoding(s) | |
148 | + pdf.setCorrectFontFamily(encoding) | |
149 | + translator, ok := pdf.translators[encoding] | |
150 | + if !ok { | |
151 | + return "" | |
152 | + } | |
153 | + return translator(s) | |
195 | 154 | } |
196 | 155 | |
197 | 156 | func textEncoding(s string) string { |
... | ... | @@ -206,17 +165,6 @@ func textEncoding(s string) string { |
206 | 165 | return encoding |
207 | 166 | } |
208 | 167 | |
209 | -// ToUTF8 ... | |
210 | -func (pdf *Helper) ToUTF8(s string) string { | |
211 | - encoding := textEncoding(s) | |
212 | - pdf.setCorrectFontFamily(encoding) | |
213 | - translator, ok := pdf.translators[encoding] | |
214 | - if !ok { | |
215 | - return "" | |
216 | - } | |
217 | - return translator(s) | |
218 | -} | |
219 | - | |
220 | 168 | func (pdf *Helper) setCorrectFontFamily(enc string) (family, style string, ptSize, unitSize float64) { |
221 | 169 | family, style, ptSize, unitSize = pdf.GetFontInfo() |
222 | 170 | if enc == cyrillicEncoding { |
... | ... | @@ -231,46 +179,3 @@ func (pdf *Helper) setCorrectFontFamily(enc string) (family, style string, ptSiz |
231 | 179 | pdf.SetFont(family, style, ptSize) |
232 | 180 | return family, style, ptSize, unitSize |
233 | 181 | } |
234 | - | |
235 | -func (pdf *Helper) PageHasSpace(requiredHeight float64) bool { | |
236 | - _, h := pdf.GetPageSize() | |
237 | - _, _, _, bot := pdf.GetMargins() | |
238 | - return (h - bot - pdf.GetY()) > requiredHeight | |
239 | -} | |
240 | - | |
241 | -func (pdf *Helper) imageCenterOffset(w, h float64) (x, y float64) { | |
242 | - pageW, pageH := pdf.GetPageSize() | |
243 | - x = pageW/2.0 - w/2.0 | |
244 | - y = pageH/2.0 - h/2.0 | |
245 | - return x, y | |
246 | -} | |
247 | - | |
248 | -// call before drawing the cell | |
249 | -func (pdf *Helper) drawCellMargins(cw, ch float64) { | |
250 | - x0, y0 := pdf.GetX(), pdf.GetY() | |
251 | - pdf.DrawBox(x0, y0, cw, ch) | |
252 | -} | |
253 | - | |
254 | -// DrawBox ... | |
255 | -func (pdf Helper) DrawBox(x0, y0, w, h float64) { | |
256 | - pdf.Line(x0, y0, x0+w, y0) | |
257 | - pdf.Line(x0+w, y0, x0+w, y0+h) | |
258 | - pdf.Line(x0+w, y0+h, x0, y0+h) | |
259 | - pdf.Line(x0, y0+h, x0, y0) | |
260 | -} | |
261 | - | |
262 | -// Strana %d/{TotalPages} | |
263 | -func (pdf *Helper) InsertPageNumber(x, y float64, format string) { | |
264 | - num := fmt.Sprintf(format, pdf.PageNo()) | |
265 | - pdf.Column(x, y, []FormatedCell{{10, 1, num, "DejaVuSans", "", 8, NOBORDER, LEFT}}) | |
266 | -} | |
267 | - | |
268 | -func (pdf *Helper) SuperscriptText(x, y, cw, ch float64, text, script string) { | |
269 | - family, style, size, sizeU := pdf.setCorrectFontFamily(textEncoding(text)) | |
270 | - | |
271 | - pdf.FCell(x, y, FormatedCell{cw, ch, text, family, style, size, NOBORDER, LEFT}) | |
272 | - | |
273 | - sx := x + pdf.TextLength(text, family, style, size) | |
274 | - sy := y - sizeU*0.2 | |
275 | - pdf.FCell(sx, sy, FormatedCell{cw, ch, script, family, style, size - 2, NOBORDER, LEFT}) | |
276 | -} | ... | ... |