-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrender.go
459 lines (410 loc) · 11.5 KB
/
render.go
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
package docgen
import (
"fmt"
"math"
"reflect"
"strings"
"github.com/Maldris/commonmarkDocgen/rules"
"gitlab.com/golang-commonmark/markdown"
)
const (
nominalIndent = 10
bulletIndent = 5
nominalFontSize = 12
heading1FontSize = 24
heading2FontSize = 22
heading3FontSize = 20
heading4FontSize = 18
heading5FontSize = 16
heading6FontSize = 14
cellMargin = 2 // marge top/bottom of cell
)
func (d *Document) render(tok markdown.Token) {
if d.Debug {
fmt.Printf("[docgen] [%v] %v :: %v # %v\r\n", tok.Tag(), reflect.TypeOf(tok), tok.Block(), tok)
}
switch tok.(type) {
case *markdown.BlockquoteOpen:
d.leftMargin += d.sizes.NominalIndent
d.fpdf.SetLeftMargin(d.leftMargin)
case *markdown.BlockquoteClose:
d.leftMargin -= d.sizes.NominalIndent
d.fpdf.SetLeftMargin(d.leftMargin)
d.flushTextStyling()
d.fpdf.SetLeftMargin(d.leftMargin)
// d.fpdf.Write(d.lineHeight, "\n")
d.fpdf.SetX(d.leftMargin)
case *markdown.BulletListOpen:
d.listStyle = dash
d.leftMargin += d.sizes.NominalIndent
d.fpdf.SetLeftMargin(d.leftMargin)
d.flushTextStyling()
d.fpdf.SetLeftMargin(d.leftMargin)
case *markdown.BulletListClose:
d.listStyle = none
d.leftMargin -= d.sizes.NominalIndent
d.fpdf.SetLeftMargin(d.leftMargin)
d.flushTextStyling()
d.fpdf.SetLeftMargin(d.leftMargin)
d.fpdf.SetX(d.leftMargin)
case *markdown.OrderedListOpen:
tk := tok.(*markdown.OrderedListOpen)
d.listStart = tk.Map[0]
d.listStyle = numberedDot
d.leftMargin += d.sizes.NominalIndent
d.fpdf.SetLeftMargin(d.leftMargin)
d.flushTextStyling()
d.fpdf.SetLeftMargin(d.leftMargin)
case *markdown.OrderedListClose:
d.listStyle = none
d.leftMargin -= d.sizes.NominalIndent
d.fpdf.SetLeftMargin(d.leftMargin)
d.flushTextStyling()
d.fpdf.SetLeftMargin(d.leftMargin)
d.fpdf.SetX(d.leftMargin)
case *markdown.ListItemOpen:
switch d.listStyle {
case dash:
d.fpdf.Write(d.lineHeight, "-")
case carat:
d.fpdf.Write(d.lineHeight, ">")
case numberedDot:
tk := tok.(*markdown.ListItemOpen)
d.fpdf.Writef(d.lineHeight, "%v.", (tk.Map[0]-d.listStart)+1)
}
d.leftMargin += d.sizes.BulletIndent
d.fpdf.SetLeftMargin(d.leftMargin)
d.flushTextStyling()
d.fpdf.SetLeftMargin(d.leftMargin)
case *markdown.ListItemClose:
d.leftMargin -= d.sizes.BulletIndent
d.fpdf.SetLeftMargin(d.leftMargin)
d.flushTextStyling()
d.fpdf.SetLeftMargin(d.leftMargin)
d.fpdf.SetX(d.leftMargin)
case *markdown.CodeBlock:
tk := tok.(*markdown.CodeBlock)
d.codeBlock(strings.Replace(tk.Content, "\n ", "\n", -1))
case *markdown.CodeInline: // TODO
ci := tok.(*markdown.CodeInline)
d.fpdf.Write(d.lineHeight, ci.Content)
case *markdown.Fence:
tk := tok.(*markdown.Fence)
d.codeBlock(tk.Content)
case *markdown.EmphasisOpen:
d.applyStyle("I")
case *markdown.EmphasisClose:
d.removeStyle("I")
case *markdown.StrongOpen:
d.applyStyle("B")
case *markdown.StrongClose:
d.removeStyle("B")
case *markdown.StrikethroughOpen: // TODO
// d.writeMode = strikethrough
case *markdown.StrikethroughClose: // TODO: make this work, the original attempt (commented below) didnt work
// html := d.fpdf.HTMLBasicNew()
// html.Write(d.lineHeight, "<del>"+d.buffer+"<del>")
// d.writeMode = normal
case *markdown.Softbreak:
d.fpdf.Write(d.lineHeight, "\n")
case *markdown.Hardbreak:
d.fpdf.Write(d.lineHeight, "\n")
case *markdown.HeadingOpen:
hd := tok.(*markdown.HeadingOpen)
switch hd.HLevel {
case 1:
d.fontSize = int(d.sizes.Heading1FontSize)
case 2:
d.fontSize = int(d.sizes.Heading2FontSize)
case 3:
d.fontSize = int(d.sizes.Heading3FontSize)
case 4:
d.fontSize = int(d.sizes.Heading4FontSize)
case 5:
d.fontSize = int(d.sizes.Heading5FontSize)
case 6:
d.fontSize = int(d.sizes.Heading6FontSize)
}
d.lineHeight *= float64(d.fontSize) / d.sizes.NominalFontSize
d.flushTextStyling()
case *markdown.HeadingClose:
d.lineHeight /= float64(d.fontSize) / d.sizes.NominalFontSize
d.fontSize = int(d.sizes.NominalFontSize)
d.flushTextStyling()
d.fpdf.Write(d.lineHeight, "\n\n")
case *markdown.HTMLBlock:
tk := tok.(*markdown.HTMLBlock)
html := d.fpdf.HTMLBasicNew()
html.Write(d.lineHeight, tk.Content)
d.fpdf.SetX(d.leftMargin)
case *markdown.HTMLInline:
tk := tok.(*markdown.HTMLInline)
html := d.fpdf.HTMLBasicNew()
html.Write(d.lineHeight, tk.Content)
case *markdown.Hr:
wd, _ := d.fpdf.GetPageSize()
d.fpdf.Line(d.leftMargin, d.fpdf.GetY(), wd-d.leftMargin, d.fpdf.GetY())
d.fpdf.Write(d.lineHeight, "\n")
case *markdown.Image: // TODO
case *markdown.Inline:
il := tok.(*markdown.Inline)
for _, tok := range il.Children {
d.render(tok)
}
case *markdown.LinkOpen:
ln := tok.(*markdown.LinkOpen)
d.writeMode = link
d.link.ref = ln.Href
case *markdown.LinkClose:
d.applyStyle("U")
if d.link.ref != "" {
d.fpdf.WriteLinkString(d.lineHeight, d.link.text, d.link.ref)
} else {
d.fpdf.Write(d.lineHeight, d.link.text)
}
d.removeStyle("U")
d.writeMode = normal
case *markdown.ParagraphOpen:
case *markdown.ParagraphClose:
d.fpdf.Write(d.lineHeight, "\n\n")
d.fpdf.SetX(d.leftMargin)
case *markdown.TableOpen:
d.table.rows = [][]cell{}
case *markdown.TableClose:
d.tableMulti()
d.flushTextStyling()
d.fpdf.SetLeftMargin(d.leftMargin)
d.fpdf.SetX(d.leftMargin)
d.fpdf.Write(d.lineHeight, "\n")
case *markdown.TheadOpen:
case *markdown.TheadClose:
case *markdown.TrOpen:
d.table.rows = append(d.table.rows, []cell{})
case *markdown.TrClose:
case *markdown.ThOpen:
d.writeMode = tableHead
case *markdown.ThClose:
d.writeMode = normal
case *markdown.TbodyOpen:
case *markdown.TbodyClose:
case *markdown.TdOpen:
d.writeMode = tableCell
case *markdown.TdClose:
d.writeMode = normal
case *markdown.Text:
txt := tok.(*markdown.Text)
content := strings.Replace(txt.Content, "~", " ", -1) // REVIEW: temp till editor buttons for tab in ui
content = strings.Replace(content, "\t", " ", -1)
switch d.writeMode {
case normal:
switch d.alignment {
case alignLeft:
d.fpdf.Write(d.lineHeight, content)
case alignCenter:
d.fpdf.WriteAligned(0, d.lineHeight, content, "C")
case alignRight:
d.fpdf.WriteAligned(0, d.lineHeight, content, "R")
}
case tableHead:
// d.table.head = append(d.table.head, content)
d.table.rows[len(d.table.rows)-1] = append(d.table.rows[len(d.table.rows)-1], cell{text: strings.Replace(content, "\\n", "\n", -1), head: true})
case tableCell:
d.table.rows[len(d.table.rows)-1] = append(d.table.rows[len(d.table.rows)-1], cell{text: strings.Replace(content, "\\n", "\n", -1), head: false})
case link:
d.link.text = content
case strikethrough:
d.buffer = content
}
case *rules.JustifyOpen:
tk := tok.(*rules.JustifyOpen)
switch tk.Type {
case 0:
d.alignment = alignLeft
case 1:
d.alignment = alignCenter
case 2:
d.alignment = alignRight
}
case *rules.JustifyClose:
d.alignment = alignLeft
case *rules.PageBreak:
d.fpdf.AddPage()
case *rules.OpenHangingIndent:
if d.indents == nil {
d.indents = []float64{}
}
d.indents = append(d.indents, d.leftMargin)
d.leftMargin = d.fpdf.GetX()
d.fpdf.SetLeftMargin(d.leftMargin)
case *rules.ClosenHangingIndent:
l := len(d.indents)
if l > 0 {
d.leftMargin = d.indents[l-1]
d.fpdf.SetLeftMargin(d.leftMargin)
d.indents = d.indents[:l-1]
} else {
d.fpdf.SetLeftMargin(10)
}
case *rules.TableHeader:
tk := tok.(*rules.TableHeader)
d.table.lines = tk.Lines
d.table.size = tk.Size
d.table.cols = tk.Cols
d.table.colsum = tk.Colsum
d.table.align = tk.Alignment
case *rules.OpenHideText:
tk := tok.(*rules.OpenHideText)
d.fpdf.SetFontSize(0)
d.fpdf.Write(d.lineHeight, tk.Content)
d.fpdf.SetFontSize(float64(d.fontSize))
}
}
func (d *Document) applyStyle(style string) {
if !strings.Contains(d.fontStyle, style) {
d.fontStyle += style
}
d.flushTextStyling()
}
func (d *Document) removeStyle(style string) {
d.fontStyle = strings.Replace(d.fontStyle, style, "", -1)
d.flushTextStyling()
}
func (d *Document) flushTextStyling() {
d.fpdf.SetFont(d.fontFamily, d.fontStyle, float64(d.fontSize))
}
func (d *Document) codeBlock(content string) {
wpage, _ := d.fpdf.GetPageSize()
lmarge, _, rmarge, _ := d.fpdf.GetMargins()
r, g, b := d.fpdf.GetFillColor()
d.fpdf.SetFillColor(180, 180, 180)
d.leftMargin += d.sizes.NominalIndent
d.fpdf.SetLeftMargin(d.leftMargin)
d.fpdf.MultiCell(wpage-(lmarge+rmarge)-20, d.lineHeight, content, "", "", true)
d.leftMargin -= d.sizes.NominalIndent
d.fpdf.SetLeftMargin(d.leftMargin)
d.fpdf.SetFillColor(r, g, b)
d.fpdf.Write(d.lineHeight, "\n")
}
func (d *Document) tableMulti() {
line := d.fpdf.GetLineWidth()
d.fpdf.SetLineWidth(line / 4)
_, pageh := d.fpdf.GetPageSize()
_, _, _, mbottom := d.fpdf.GetMargins()
cols, lmarg := d.calcTableColumnWidths()
if d.table.align == rules.AlignCenter {
lmarg = lmarg / 2
} else if d.table.align == rules.AlignLeft {
lmarg = 0
}
for _, row := range d.table.rows {
curx, y := d.fpdf.GetXY()
x := curx + lmarg
d.fpdf.SetX(x)
height := 0.0
for i, cell := range row {
if cell.head {
d.applyStyle("B")
}
lines := d.fpdf.SplitLines([]byte(cell.text), cols[i])
h := float64(len(lines))*d.lineHeight + float64(int(d.sizes.CellMargin)*len(lines))
if h > height {
height = h
}
if cell.head {
d.removeStyle("B")
}
}
// add a new page if the height of the row doesn't fit on the page
if d.fpdf.GetY()+height > pageh-mbottom {
d.fpdf.AddPage()
y = d.fpdf.GetY()
}
for i, cell := range row {
if cell.head {
d.applyStyle("B")
}
width := cols[i]
if d.table.lines {
d.fpdf.Rect(x, y, width, height, "")
}
d.fpdf.MultiCell(width, d.lineHeight+d.sizes.CellMargin, cell.text, "", "", false)
x += width
d.fpdf.SetXY(x, y)
if cell.head {
d.removeStyle("B")
}
}
d.fpdf.SetXY(curx, y+height)
}
d.fpdf.SetLineWidth(line)
}
func (d *Document) calcTableColumnWidths() ([]float64, float64) {
wdspace := math.Ceil(d.fpdf.GetStringWidth(" "))
type column struct {
max float64
total float64
count float64
average float64
}
var c []column
for _, rowVal := range d.table.rows {
for col, colVal := range rowVal {
if len(c) < col+1 {
c = append(c, column{})
}
if colVal.head {
d.removeStyle("B")
d.flushTextStyling()
}
wd := d.fpdf.GetStringWidth(colVal.text) + (wdspace * float64(strings.Count(colVal.text, " ")+1+4))
if colVal.head {
d.removeStyle("B")
d.flushTextStyling()
}
if wd > c[col].max {
c[col].max = wd
}
c[col].total += wd
c[col].count++
c[col].average = c[col].total / c[col].count
}
}
total := float64(0)
avgTotal := float64(0)
for _, col := range c {
total += col.max
avgTotal += col.average
}
wpage, _ := d.fpdf.GetPageSize()
lmarge, _, rmarge, _ := d.fpdf.GetMargins()
max := wpage - (lmarge + rmarge)
if d.table.size == rules.SizeHalf {
max = max / 2
}
var cols []float64
var sum float64
if len(d.table.cols) > 0 {
cols = append(cols, d.table.cols...)
for i := range cols {
cols[i] = (cols[i] / d.table.colsum) * max
sum += (cols[i] / d.table.colsum) * max
}
} else {
if d.table.size == rules.SizeWrap && total <= (max) {
for _, col := range c {
cols = append(cols, col.max)
sum += col.max
}
} else {
for _, col := range c {
cols = append(cols, (col.average/avgTotal)*(max))
sum += (col.average / avgTotal) * (max)
}
}
}
offset := ((wpage - (lmarge + rmarge)) - sum)
if offset < 0 {
offset = 0
}
return cols, offset
}