-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
pdf_renderer.go
1295 lines (1140 loc) · 36.2 KB
/
pdf_renderer.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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Lute PDF - 一款通过 Markdown 生成 PDF 的小工具
// Copyright (c) 2020-present, b3log.org
//
// LianDi is licensed under Mulan PSL v2.
// You can use this software according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of Mulan PSL v2 at:
// http://license.coscl.org.cn/MulanPSL2
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
// See the Mulan PSL v2 for more details.
package main
import (
"bytes"
"fmt"
"image"
"io/ioutil"
"math"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"time"
"github.com/88250/lute/ast"
"github.com/88250/lute/lex"
"github.com/88250/lute/parse"
"github.com/88250/lute/render"
"github.com/88250/lute/util"
"github.com/signintech/gopdf"
)
// PdfRenderer 描述了 PDF 渲染器。
type PdfRenderer struct {
*render.BaseRenderer
Cover *PdfCover // 封面
RegularFont string // 正常字体文件路径
BoldFont string // 粗体字体文件路径
ItalicFont string // 斜体字体文件路径
pdf *gopdf.GoPdf // PDF 生成器句柄
pageSize *gopdf.Rect // 页面大小
zoom float64 // 字体、行高大小倍数
fontSize int // 字体大小
lineHeight float64 // 行高
heading1Size float64 // 一级标题大小
heading2Size float64 // 二级标题大小
heading3Size float64 // 三级标题大小
heading4Size float64 // 四级标题大小
heading5Size float64 // 五级标题大小
heading6Size float64 // 六级标题大小
margin float64 // 页边距
x []float64 // 当前横坐标栈
fonts []*Font // 当前字体栈
textColors []*RGB // 当前文本颜色栈
}
// PdfCover 描述了 PDF 封面。
type PdfCover struct {
Title string // 标题
AuthorLabel string // 作者:
Author string // 作者
AuthorLink string // 作者链接
LinkLabel string // 原文链接:
Link string // 原文链接
SourceLabel string // 来源网站:
Source string // 来源网站
SourceLink string // 来源网站链接
LicenseLabel string // 许可协议:
License string // 许可协议
LicenseLink string // 许可协议链接
LogoLink string // 图标链接
LogoTitle string // 图片标题
LogoTitleLink string // 图标标题链接
}
func (r *PdfRenderer) RenderCover() {
r.pdf.AddPage()
logoImgPath, ok, isTemp := r.downloadImg(r.Cover.LogoLink)
if ok {
imgW, imgH := r.getImgSize(logoImgPath)
x := (r.pageSize.W)/2 - imgW/2
y := r.pageSize.H/2 - r.margin - 128
r.pdf.Image(logoImgPath, x, y, nil)
r.pdf.SetY(y)
r.pdf.Br(imgH + 10)
r.pdf.SetFontWithStyle("regular", gopdf.Regular, 20)
width, _ := r.pdf.MeasureTextWidth(r.Cover.LogoTitle)
x = (r.pageSize.W)/2 - width/2
r.pdf.SetX(x)
y = r.pdf.GetY()
r.pdf.Cell(nil, r.Cover.LogoTitle)
r.pdf.AddExternalLink(r.Cover.LogoTitleLink, x, y, width, 20)
r.pdf.Br(48)
if isTemp {
os.Remove(logoImgPath)
}
}
r.pdf.SetFontWithStyle("regular", gopdf.Regular, 28)
lines, _ := r.pdf.SplitText(r.Cover.Title, r.pageSize.W-r.margin)
for _, line := range lines {
width, _ := r.pdf.MeasureTextWidth(line)
x := (r.pageSize.W)/2 - width/2
r.pdf.SetX(x)
r.pdf.Cell(nil, line)
r.pdf.Br(30)
}
fontSize := 12
r.pdf.Br(45)
r.pdf.SetX(r.margin)
r.pdf.SetFontWithStyle("regular", gopdf.Regular, fontSize)
r.pdf.Cell(nil, r.Cover.AuthorLabel)
x := r.pdf.GetX()
width, _ := r.pdf.MeasureTextWidth(r.Cover.Author)
r.pdf.SetTextColor(66, 133, 244)
r.pdf.Cell(nil, r.Cover.Author)
r.pdf.AddExternalLink(r.Cover.AuthorLink, x, r.pdf.GetY(), width, float64(fontSize))
r.pdf.SetTextColor(0, 0, 0)
r.pdf.Br(22)
r.pdf.Cell(nil, r.Cover.LinkLabel)
x = r.pdf.GetX()
width, _ = r.pdf.MeasureTextWidth(r.Cover.Link)
r.pdf.SetTextColor(66, 133, 244)
r.pdf.Cell(nil, r.Cover.Link)
r.pdf.AddExternalLink(r.Cover.Link, x, r.pdf.GetY(), width, float64(fontSize))
r.pdf.SetTextColor(0, 0, 0)
r.pdf.Br(22)
r.pdf.Cell(nil, r.Cover.SourceLabel)
x = r.pdf.GetX()
width, _ = r.pdf.MeasureTextWidth(r.Cover.Source)
r.pdf.SetTextColor(66, 133, 244)
r.pdf.Cell(nil, r.Cover.Source)
r.pdf.AddExternalLink(r.Cover.SourceLink, x, r.pdf.GetY(), width, float64(fontSize))
r.pdf.SetTextColor(0, 0, 0)
r.pdf.Br(22)
r.pdf.Cell(nil, r.Cover.LicenseLabel)
x = r.pdf.GetX()
width, _ = r.pdf.MeasureTextWidth(r.Cover.License)
r.pdf.SetTextColor(66, 133, 244)
r.pdf.Cell(nil, r.Cover.License)
r.pdf.AddExternalLink(r.Cover.LicenseLink, x, r.pdf.GetY(), width, float64(fontSize))
r.pdf.SetTextColor(0, 0, 0)
r.pdf.Br(20)
r.pdf.AddPage()
}
// NewPdfRenderer 创建一个 HTML 渲染器。
func NewPdfRenderer(tree *parse.Tree, options *render.Options, regularFont, boldFont, italicFont string) *PdfRenderer {
pdf := &gopdf.GoPdf{}
ret := &PdfRenderer{BaseRenderer: render.NewBaseRenderer(tree, options), pdf: pdf}
ret.zoom = 0.8
ret.fontSize = int(math.Floor(14 * ret.zoom))
ret.lineHeight = 24.0 * ret.zoom
ret.heading1Size = 24 * ret.zoom
ret.heading2Size = 22 * ret.zoom
ret.heading3Size = 20 * ret.zoom
ret.heading4Size = 18 * ret.zoom
ret.heading5Size = 16 * ret.zoom
ret.heading6Size = 14 * ret.zoom
ret.margin = 60 * ret.zoom
ret.RegularFont = regularFont
ret.BoldFont = boldFont
ret.ItalicFont = italicFont
ret.pageSize = gopdf.PageSizeA4
pdf.Start(gopdf.Config{PageSize: *ret.pageSize})
var err error
err = pdf.AddTTFFont("regular", ret.RegularFont)
if err != nil {
logger.Fatal(err)
}
err = pdf.AddTTFFontWithOption("bold", ret.BoldFont, gopdf.TtfOption{Style: gopdf.Bold})
if err != nil {
logger.Fatal(err)
}
err = pdf.AddTTFFontWithOption("italic", ret.ItalicFont, gopdf.TtfOption{Style: gopdf.Italic})
if err != nil {
logger.Fatal(err)
}
//err = pdf.AddTTFFont("emoji", "fonts/seguiemj.ttf")
//if err != nil {
// logger.Fatal(err)
//}
ret.pushFont(&Font{"regular", "R", ret.fontSize})
ret.pushTextColor(&RGB{0, 0, 0})
pdf.SetMargins(ret.margin, ret.margin, ret.margin, ret.margin)
ret.RendererFuncs[ast.NodeDocument] = ret.renderDocument
ret.RendererFuncs[ast.NodeParagraph] = ret.renderParagraph
ret.RendererFuncs[ast.NodeText] = ret.renderText
ret.RendererFuncs[ast.NodeCodeSpan] = ret.renderCodeSpan
ret.RendererFuncs[ast.NodeCodeSpanOpenMarker] = ret.renderCodeSpanOpenMarker
ret.RendererFuncs[ast.NodeCodeSpanContent] = ret.renderCodeSpanContent
ret.RendererFuncs[ast.NodeCodeSpanCloseMarker] = ret.renderCodeSpanCloseMarker
ret.RendererFuncs[ast.NodeCodeBlock] = ret.renderCodeBlock
ret.RendererFuncs[ast.NodeCodeBlockFenceOpenMarker] = ret.renderCodeBlockOpenMarker
ret.RendererFuncs[ast.NodeCodeBlockFenceInfoMarker] = ret.renderCodeBlockInfoMarker
ret.RendererFuncs[ast.NodeCodeBlockCode] = ret.renderCodeBlockCode
ret.RendererFuncs[ast.NodeCodeBlockFenceCloseMarker] = ret.renderCodeBlockCloseMarker
ret.RendererFuncs[ast.NodeMathBlock] = ret.renderMathBlock
ret.RendererFuncs[ast.NodeMathBlockOpenMarker] = ret.renderMathBlockOpenMarker
ret.RendererFuncs[ast.NodeMathBlockContent] = ret.renderMathBlockContent
ret.RendererFuncs[ast.NodeMathBlockCloseMarker] = ret.renderMathBlockCloseMarker
ret.RendererFuncs[ast.NodeInlineMath] = ret.renderInlineMath
ret.RendererFuncs[ast.NodeInlineMathOpenMarker] = ret.renderInlineMathOpenMarker
ret.RendererFuncs[ast.NodeInlineMathContent] = ret.renderInlineMathContent
ret.RendererFuncs[ast.NodeInlineMathCloseMarker] = ret.renderInlineMathCloseMarker
ret.RendererFuncs[ast.NodeEmphasis] = ret.renderEmphasis
ret.RendererFuncs[ast.NodeEmA6kOpenMarker] = ret.renderEmAsteriskOpenMarker
ret.RendererFuncs[ast.NodeEmA6kCloseMarker] = ret.renderEmAsteriskCloseMarker
ret.RendererFuncs[ast.NodeEmU8eOpenMarker] = ret.renderEmUnderscoreOpenMarker
ret.RendererFuncs[ast.NodeEmU8eCloseMarker] = ret.renderEmUnderscoreCloseMarker
ret.RendererFuncs[ast.NodeStrong] = ret.renderStrong
ret.RendererFuncs[ast.NodeStrongA6kOpenMarker] = ret.renderStrongA6kOpenMarker
ret.RendererFuncs[ast.NodeStrongA6kCloseMarker] = ret.renderStrongA6kCloseMarker
ret.RendererFuncs[ast.NodeStrongU8eOpenMarker] = ret.renderStrongU8eOpenMarker
ret.RendererFuncs[ast.NodeStrongU8eCloseMarker] = ret.renderStrongU8eCloseMarker
ret.RendererFuncs[ast.NodeBlockquote] = ret.renderBlockquote
ret.RendererFuncs[ast.NodeBlockquoteMarker] = ret.renderBlockquoteMarker
ret.RendererFuncs[ast.NodeHeading] = ret.renderHeading
ret.RendererFuncs[ast.NodeHeadingC8hMarker] = ret.renderHeadingC8hMarker
ret.RendererFuncs[ast.NodeList] = ret.renderList
ret.RendererFuncs[ast.NodeListItem] = ret.renderListItem
ret.RendererFuncs[ast.NodeThematicBreak] = ret.renderThematicBreak
ret.RendererFuncs[ast.NodeHardBreak] = ret.renderHardBreak
ret.RendererFuncs[ast.NodeSoftBreak] = ret.renderSoftBreak
ret.RendererFuncs[ast.NodeHTMLBlock] = ret.renderHTML
ret.RendererFuncs[ast.NodeInlineHTML] = ret.renderInlineHTML
ret.RendererFuncs[ast.NodeLink] = ret.renderLink
ret.RendererFuncs[ast.NodeImage] = ret.renderImage
ret.RendererFuncs[ast.NodeBang] = ret.renderBang
ret.RendererFuncs[ast.NodeOpenBracket] = ret.renderOpenBracket
ret.RendererFuncs[ast.NodeCloseBracket] = ret.renderCloseBracket
ret.RendererFuncs[ast.NodeOpenParen] = ret.renderOpenParen
ret.RendererFuncs[ast.NodeCloseParen] = ret.renderCloseParen
ret.RendererFuncs[ast.NodeLinkText] = ret.renderLinkText
ret.RendererFuncs[ast.NodeLinkSpace] = ret.renderLinkSpace
ret.RendererFuncs[ast.NodeLinkDest] = ret.renderLinkDest
ret.RendererFuncs[ast.NodeLinkTitle] = ret.renderLinkTitle
ret.RendererFuncs[ast.NodeStrikethrough] = ret.renderStrikethrough
ret.RendererFuncs[ast.NodeStrikethrough1OpenMarker] = ret.renderStrikethrough1OpenMarker
ret.RendererFuncs[ast.NodeStrikethrough1CloseMarker] = ret.renderStrikethrough1CloseMarker
ret.RendererFuncs[ast.NodeStrikethrough2OpenMarker] = ret.renderStrikethrough2OpenMarker
ret.RendererFuncs[ast.NodeStrikethrough2CloseMarker] = ret.renderStrikethrough2CloseMarker
ret.RendererFuncs[ast.NodeTaskListItemMarker] = ret.renderTaskListItemMarker
ret.RendererFuncs[ast.NodeTable] = ret.renderTable
ret.RendererFuncs[ast.NodeTableHead] = ret.renderTableHead
ret.RendererFuncs[ast.NodeTableRow] = ret.renderTableRow
ret.RendererFuncs[ast.NodeTableCell] = ret.renderTableCell
ret.RendererFuncs[ast.NodeEmoji] = ret.renderEmoji
ret.RendererFuncs[ast.NodeEmojiUnicode] = ret.renderEmojiUnicode
ret.RendererFuncs[ast.NodeEmojiImg] = ret.renderEmojiImg
ret.RendererFuncs[ast.NodeEmojiAlias] = ret.renderEmojiAlias
ret.RendererFuncs[ast.NodeFootnotesDefBlock] = ret.renderFootnotesDefBlock
ret.RendererFuncs[ast.NodeFootnotesDef] = ret.renderFootnotesDef
ret.RendererFuncs[ast.NodeFootnotesRef] = ret.renderFootnotesRef
ret.RendererFuncs[ast.NodeToC] = ret.renderToC
ret.RendererFuncs[ast.NodeBackslash] = ret.renderBackslash
ret.RendererFuncs[ast.NodeBackslashContent] = ret.renderBackslashContent
ret.RendererFuncs[ast.NodeHTMLEntity] = ret.renderHtmlEntity
ret.RendererFuncs[ast.NodeYamlFrontMatter] = ret.renderYamlFrontMatter
return ret
}
func (r *PdfRenderer) Render() (output []byte) {
r.LastOut = lex.ItemNewline
ast.Walk(r.Tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
extRender := r.ExtRendererFuncs[n.Type]
if nil != extRender {
output, status := extRender(n, entering)
r.WriteString(output)
return status
}
render := r.RendererFuncs[n.Type]
if nil == render {
if nil != r.DefaultRendererFunc {
return r.DefaultRendererFunc(n, entering)
} else {
return r.renderDefault(n, entering)
}
}
return render(n, entering)
})
if 0 < len(r.FootnotesDefs) {
output = r.RenderFootnotesDefs(r.Tree.Context)
}
return
}
func (r *PdfRenderer) renderDefault(n *ast.Node, entering bool) ast.WalkStatus {
return ast.WalkContinue
}
func (r *PdfRenderer) renderYamlFrontMatter(node *ast.Node, entering bool) ast.WalkStatus {
if entering {
r.renderCodeBlockLike(node.Tokens)
}
return ast.WalkContinue
}
func (r *PdfRenderer) renderHtmlEntity(node *ast.Node, entering bool) ast.WalkStatus {
if entering {
r.Write(node.HtmlEntityTokens)
}
return ast.WalkContinue
}
func (r *PdfRenderer) renderBackslashContent(node *ast.Node, entering bool) ast.WalkStatus {
if entering {
r.Write(node.Tokens)
}
return ast.WalkContinue
}
func (r *PdfRenderer) renderBackslash(node *ast.Node, entering bool) ast.WalkStatus {
return ast.WalkContinue
}
func (r *PdfRenderer) renderToC(node *ast.Node, entering bool) ast.WalkStatus {
if entering {
headings := r.headings()
length := len(headings)
if 1 > length {
return ast.WalkContinue
}
r.WriteString("<div class=\"toc-div\">")
for i, heading := range headings {
level := strconv.Itoa(heading.HeadingLevel)
spaces := (heading.HeadingLevel - 1) * 2
r.WriteString(strings.Repeat(" ", spaces))
r.WriteString("<span class=\"toc-h" + level + "\">")
r.WriteString("<a class=\"toc-a\" href=\"#toc_h" + level + "_" + strconv.Itoa(i) + "\">" + heading.Text() + "</a></span><br>")
}
r.WriteString("</div>\n\n")
}
return ast.WalkContinue
}
func (r *PdfRenderer) headings() (ret []*ast.Node) {
for n := r.Tree.Root.FirstChild; nil != n; n = n.Next {
r.headings0(n, &ret)
}
return
}
func (r *PdfRenderer) headings0(n *ast.Node, headings *[]*ast.Node) {
if ast.NodeHeading == n.Type {
*headings = append(*headings, n)
return
}
if ast.NodeList == n.Type || ast.NodeListItem == n.Type || ast.NodeBlockquote == n.Type {
for c := n.FirstChild; nil != c; c = c.Next {
r.headings0(c, headings)
}
}
}
func (r *PdfRenderer) RenderFootnotesDefs(context *parse.Context) []byte {
if 1 > len(r.FootnotesDefs) || r.RenderingFootnotes {
return nil
}
r.RenderingFootnotes = true
r.addPage()
r.renderThematicBreak(nil, false)
for i, def := range r.FootnotesDefs {
r.pdf.SetAnchor(string(def.Tokens))
r.WriteString(fmt.Sprint(i+1) + ". ")
tree := &parse.Tree{Name: "", Context: context}
tree.Context.Tree = tree
tree.Root = &ast.Node{Type: ast.NodeDocument}
tree.Root.AppendChild(def)
r.Tree = tree
r.Render()
r.Newline()
}
r.renderFooter()
return nil
}
func (r *PdfRenderer) renderFootnotesRef(node *ast.Node, entering bool) ast.WalkStatus {
if entering {
x := r.pdf.GetX() + 1
r.pdf.SetX(x)
y := r.pdf.GetY()
r.pdf.SetFont("regular", "R", 8)
r.pdf.SetTextColor(66, 133, 244)
idx := string(node.Tokens)
width, _ := r.pdf.MeasureTextWidth(idx[1:])
r.pdf.SetY(y - 4)
r.pdf.Cell(nil, idx[1:])
r.pdf.AddInternalLink(idx, x-3, y-9, width+4, r.lineHeight)
x += width
r.pdf.SetX(x)
r.pdf.SetY(y)
font := r.peekFont()
r.pdf.SetFont(font.family, font.style, font.size)
textColor := r.peekTextColor()
r.pdf.SetTextColor(textColor.R, textColor.G, textColor.B)
}
return ast.WalkContinue
}
func (r *PdfRenderer) renderFootnotesDefBlock(node *ast.Node, entering bool) ast.WalkStatus {
return ast.WalkContinue
}
func (r *PdfRenderer) renderFootnotesDef(node *ast.Node, entering bool) ast.WalkStatus {
if entering {
if !r.RenderingFootnotes {
var found bool
for _, n := range r.FootnotesDefs {
if bytes.EqualFold(node.Tokens, n.Tokens) {
found = true
break
}
}
if !found {
r.FootnotesDefs = append(r.FootnotesDefs, node)
}
return ast.WalkSkipChildren
}
}
return ast.WalkContinue
}
func (r *PdfRenderer) renderCodeBlock(node *ast.Node, entering bool) ast.WalkStatus {
if entering {
if !node.IsFencedCodeBlock {
// 缩进代码块处理
r.renderCodeBlockLike(node.Tokens)
return ast.WalkContinue
}
}
return ast.WalkContinue
}
// renderCodeBlockCode 进行代码块 HTML 渲染,实现语法高亮。
func (r *PdfRenderer) renderCodeBlockCode(node *ast.Node, entering bool) ast.WalkStatus {
if entering {
r.renderCodeBlockLike(node.Tokens)
}
return ast.WalkContinue
}
func (r *PdfRenderer) renderCodeBlockLike(content []byte) {
r.Newline()
r.pdf.SetY(r.pdf.GetY() + 6)
r.pushTextColor(&RGB{86, 158, 61})
r.WriteString(util.BytesToStr(content))
r.popTextColor()
r.pdf.SetY(r.pdf.GetY() + 6)
r.Newline()
}
func (r *PdfRenderer) renderCodeSpanLike(content []byte) {
r.pushTextColor(&RGB{255, 153, 51})
r.WriteString(util.BytesToStr(content))
r.popTextColor()
}
func (r *PdfRenderer) renderCodeBlockCloseMarker(node *ast.Node, entering bool) ast.WalkStatus {
return ast.WalkContinue
}
func (r *PdfRenderer) renderCodeBlockInfoMarker(node *ast.Node, entering bool) ast.WalkStatus {
return ast.WalkContinue
}
func (r *PdfRenderer) renderCodeBlockOpenMarker(node *ast.Node, entering bool) ast.WalkStatus {
return ast.WalkContinue
}
func (r *PdfRenderer) renderEmojiAlias(node *ast.Node, entering bool) ast.WalkStatus {
if entering {
r.pushFont(&Font{"emoji", "R", r.fontSize})
alias := node.Tokens[1 : len(node.Tokens)-1]
r.Write(alias)
r.popFont()
}
return ast.WalkContinue
}
func (r *PdfRenderer) renderEmojiImg(node *ast.Node, entering bool) ast.WalkStatus {
return ast.WalkContinue
}
func (r *PdfRenderer) renderEmojiUnicode(node *ast.Node, entering bool) ast.WalkStatus {
if entering {
r.pushFont(&Font{"emoji", "R", r.fontSize})
r.Write(node.Tokens)
r.popFont()
}
return ast.WalkContinue
}
func (r *PdfRenderer) renderEmoji(node *ast.Node, entering bool) ast.WalkStatus {
// 暂不渲染 Emoji,字体似乎有问题
return ast.WalkContinue
}
func (r *PdfRenderer) renderInlineMathCloseMarker(node *ast.Node, entering bool) ast.WalkStatus {
return ast.WalkContinue
}
func (r *PdfRenderer) renderInlineMathContent(node *ast.Node, entering bool) ast.WalkStatus {
if entering {
r.renderCodeSpanLike(node.Tokens)
}
return ast.WalkContinue
}
func (r *PdfRenderer) renderInlineMathOpenMarker(node *ast.Node, entering bool) ast.WalkStatus {
return ast.WalkContinue
}
func (r *PdfRenderer) renderInlineMath(node *ast.Node, entering bool) ast.WalkStatus {
return ast.WalkContinue
}
func (r *PdfRenderer) renderMathBlockCloseMarker(node *ast.Node, entering bool) ast.WalkStatus {
return ast.WalkContinue
}
func (r *PdfRenderer) renderMathBlockContent(node *ast.Node, entering bool) ast.WalkStatus {
if entering {
r.renderCodeBlockLike(node.Tokens)
}
return ast.WalkContinue
}
func (r *PdfRenderer) renderMathBlockOpenMarker(node *ast.Node, entering bool) ast.WalkStatus {
return ast.WalkContinue
}
func (r *PdfRenderer) renderMathBlock(node *ast.Node, entering bool) ast.WalkStatus {
r.Newline()
return ast.WalkContinue
}
func (r *PdfRenderer) renderTableCell(node *ast.Node, entering bool) ast.WalkStatus {
if entering {
// TODO: table align
//var attrs [][]string
//switch node.TableCellAlign {
//case 1:
// attrs = append(attrs, []string{"align", "left"})
//case 2:
// attrs = append(attrs, []string{"align", "center"})
//case 3:
// attrs = append(attrs, []string{"align", "right"})
//}
x := r.pdf.GetX()
cols := float64(r.tableCols(node))
maxWidth := (r.pageSize.W - r.margin*2) / cols
if node.Parent.FirstChild != node {
prevWidth, _ := r.pdf.MeasureTextWidth(strings.Repeat("爱", node.Previous.TableCellContentWidth))
x += maxWidth - prevWidth
r.pdf.SetX(x)
}
// TODO: table border
// r.pdf.RectFromUpperLeftWithStyle(x, r.pdf.GetY(), maxWidth, r.lineHeight, "D")
r.pdf.SetX(r.pdf.GetX() + 4)
r.pdf.SetY(r.pdf.GetY() + 4)
} else {
r.pdf.SetX(r.pdf.GetX() - 4)
r.pdf.SetY(r.pdf.GetY() - 4)
}
return ast.WalkContinue
}
func (r *PdfRenderer) tableCols(cell *ast.Node) int {
for parent := cell.Parent; nil != parent; parent = parent.Parent {
if nil != parent.TableAligns {
return len(parent.TableAligns)
}
}
return 0
}
func (r *PdfRenderer) renderTableRow(node *ast.Node, entering bool) ast.WalkStatus {
r.Newline()
return ast.WalkContinue
}
func (r *PdfRenderer) renderTableHead(node *ast.Node, entering bool) ast.WalkStatus {
if entering {
r.pushFont(&Font{"bold", "B", r.fontSize})
} else {
r.popFont()
}
return ast.WalkContinue
}
func (r *PdfRenderer) renderTable(node *ast.Node, entering bool) ast.WalkStatus {
if entering {
r.pdf.SetY(r.pdf.GetY() + 6)
} else {
r.pdf.SetY(r.pdf.GetY() + 6)
r.Newline()
}
return ast.WalkContinue
}
func (r *PdfRenderer) renderStrikethrough(node *ast.Node, entering bool) ast.WalkStatus {
return ast.WalkContinue
}
func (r *PdfRenderer) renderStrikethrough1OpenMarker(node *ast.Node, entering bool) ast.WalkStatus {
if entering {
r.pushX(r.pdf.GetX())
}
return ast.WalkContinue
}
func (r *PdfRenderer) renderStrikethrough1CloseMarker(node *ast.Node, entering bool) ast.WalkStatus {
if entering {
x := r.popX()
r.pdf.Line(x, r.pdf.GetY()+float64(r.fontSize)/2, r.pdf.GetX(), r.pdf.GetY()+float64(r.fontSize)/2)
}
return ast.WalkContinue
}
func (r *PdfRenderer) renderStrikethrough2OpenMarker(node *ast.Node, entering bool) ast.WalkStatus {
if entering {
r.pushX(r.pdf.GetX())
}
return ast.WalkContinue
}
func (r *PdfRenderer) renderStrikethrough2CloseMarker(node *ast.Node, entering bool) ast.WalkStatus {
if entering {
x := r.popX()
r.pdf.Line(x, r.pdf.GetY()+float64(r.fontSize)/2, r.pdf.GetX(), r.pdf.GetY()+float64(r.fontSize)/2)
}
return ast.WalkContinue
}
func (r *PdfRenderer) renderLinkTitle(node *ast.Node, entering bool) ast.WalkStatus {
return ast.WalkContinue
}
func (r *PdfRenderer) renderLinkDest(node *ast.Node, entering bool) ast.WalkStatus {
return ast.WalkContinue
}
func (r *PdfRenderer) renderLinkSpace(node *ast.Node, entering bool) ast.WalkStatus {
return ast.WalkContinue
}
func (r *PdfRenderer) renderLinkText(node *ast.Node, entering bool) ast.WalkStatus {
if entering {
if ast.NodeImage != node.Parent.Type {
r.Write(node.Tokens)
}
}
return ast.WalkContinue
}
func (r *PdfRenderer) renderCloseParen(node *ast.Node, entering bool) ast.WalkStatus {
return ast.WalkContinue
}
func (r *PdfRenderer) renderOpenParen(node *ast.Node, entering bool) ast.WalkStatus {
return ast.WalkContinue
}
func (r *PdfRenderer) renderCloseBracket(node *ast.Node, entering bool) ast.WalkStatus {
return ast.WalkContinue
}
func (r *PdfRenderer) renderOpenBracket(node *ast.Node, entering bool) ast.WalkStatus {
return ast.WalkContinue
}
func (r *PdfRenderer) renderBang(node *ast.Node, entering bool) ast.WalkStatus {
return ast.WalkContinue
}
func (r *PdfRenderer) renderImage(node *ast.Node, entering bool) ast.WalkStatus {
if entering {
if 0 == r.DisableTags {
destTokens := node.ChildByType(ast.NodeLinkDest).Tokens
src := util.BytesToStr(destTokens)
src, ok, isTemp := r.downloadImg(src)
if ok {
_, height := r.getImgSize(src)
y := r.pdf.GetY()
if math.Ceil(y)+height > math.Floor(r.pageSize.H-r.margin) {
r.addPage()
}
r.pdf.Image(src, r.pdf.GetX(), r.pdf.GetY(), nil)
r.pdf.SetY(r.pdf.GetY() + height)
if isTemp {
os.Remove(src)
}
}
}
r.DisableTags++
return ast.WalkContinue
}
r.DisableTags--
if 0 == r.DisableTags {
//r.WriteString("\"")
//if title := node.ChildByType(ast.NodeLinkTitle); nil != title && nil != title.Tokens {
// r.WriteString(" title=\"")
// r.Write(title.Tokens)
// r.WriteString("\"")
//}
//r.WriteString(" />")
}
return ast.WalkContinue
}
func (r *PdfRenderer) renderLink(node *ast.Node, entering bool) ast.WalkStatus {
if entering {
r.pushX(r.pdf.GetX())
r.pushTextColor(&RGB{66, 133, 244})
} else {
x := r.popX()
width := r.pdf.GetX() - x
dest := node.ChildByType(ast.NodeLinkDest)
destTokens := dest.Tokens
destTokens = r.RelativePath(destTokens)
r.pdf.AddExternalLink(util.BytesToStr(destTokens), x, r.pdf.GetY(), width, r.lineHeight)
r.popTextColor()
}
return ast.WalkContinue
}
func (r *PdfRenderer) renderHTML(node *ast.Node, entering bool) ast.WalkStatus {
if entering {
r.renderCodeBlockLike(node.Tokens)
}
return ast.WalkContinue
}
func (r *PdfRenderer) renderInlineHTML(node *ast.Node, entering bool) ast.WalkStatus {
if entering {
r.renderCodeSpanLike(node.Tokens)
}
return ast.WalkContinue
}
func (r *PdfRenderer) renderDocument(node *ast.Node, entering bool) ast.WalkStatus {
if !entering {
r.renderFooter()
}
return ast.WalkContinue
}
func (r *PdfRenderer) Save(pdfPath string) {
if err := r.pdf.WritePdf(pdfPath); nil != err {
logger.Fatal(err)
}
if err := r.pdf.Close(); nil != err {
logger.Fatal(err)
}
}
func (r *PdfRenderer) renderParagraph(node *ast.Node, entering bool) ast.WalkStatus {
inList := false
grandparent := node.Parent.Parent
inTightList := false
if nil != grandparent && ast.NodeList == grandparent.Type {
inList = true
inTightList = grandparent.ListData.Tight
}
if inTightList { // List.ListItem.Paragraph
return ast.WalkContinue
}
if entering {
if !inList {
r.Newline()
r.pdf.SetY(r.pdf.GetY() + 6)
}
} else {
r.Newline()
}
return ast.WalkContinue
}
func (r *PdfRenderer) renderText(node *ast.Node, entering bool) ast.WalkStatus {
if entering {
text := util.BytesToStr(node.Tokens)
r.WriteString(text)
}
return ast.WalkContinue
}
func (r *PdfRenderer) renderCodeSpan(node *ast.Node, entering bool) ast.WalkStatus {
return ast.WalkContinue
}
func (r *PdfRenderer) renderCodeSpanOpenMarker(node *ast.Node, entering bool) ast.WalkStatus {
return ast.WalkContinue
}
func (r *PdfRenderer) renderCodeSpanContent(node *ast.Node, entering bool) ast.WalkStatus {
if entering {
r.renderCodeSpanLike(node.Tokens)
}
return ast.WalkContinue
}
func (r *PdfRenderer) renderCodeSpanCloseMarker(node *ast.Node, entering bool) ast.WalkStatus {
return ast.WalkContinue
}
func (r *PdfRenderer) renderEmphasis(node *ast.Node, entering bool) ast.WalkStatus {
return ast.WalkContinue
}
func (r *PdfRenderer) renderEmAsteriskOpenMarker(node *ast.Node, entering bool) ast.WalkStatus {
if entering {
r.pushFont(&Font{"italic", "I", r.fontSize})
}
return ast.WalkContinue
}
func (r *PdfRenderer) renderEmAsteriskCloseMarker(node *ast.Node, entering bool) ast.WalkStatus {
if entering {
r.popFont()
}
return ast.WalkContinue
}
func (r *PdfRenderer) renderEmUnderscoreOpenMarker(node *ast.Node, entering bool) ast.WalkStatus {
if entering {
r.pushFont(&Font{"italic", "I", r.fontSize})
}
return ast.WalkContinue
}
func (r *PdfRenderer) renderEmUnderscoreCloseMarker(node *ast.Node, entering bool) ast.WalkStatus {
if entering {
r.popFont()
}
return ast.WalkContinue
}
func (r *PdfRenderer) renderStrong(node *ast.Node, entering bool) ast.WalkStatus {
return ast.WalkContinue
}
func (r *PdfRenderer) renderStrongA6kOpenMarker(node *ast.Node, entering bool) ast.WalkStatus {
if entering {
r.pushFont(&Font{"bold", "B", r.fontSize})
}
return ast.WalkContinue
}
func (r *PdfRenderer) renderStrongA6kCloseMarker(node *ast.Node, entering bool) ast.WalkStatus {
if entering {
r.popFont()
}
return ast.WalkContinue
}
func (r *PdfRenderer) renderStrongU8eOpenMarker(node *ast.Node, entering bool) ast.WalkStatus {
if entering {
r.pushFont(&Font{"bold", "B", r.fontSize})
}
return ast.WalkContinue
}
func (r *PdfRenderer) renderStrongU8eCloseMarker(node *ast.Node, entering bool) ast.WalkStatus {
if entering {
r.popFont()
}
return ast.WalkContinue
}
func (r *PdfRenderer) renderBlockquote(node *ast.Node, entering bool) ast.WalkStatus {
if entering {
r.Newline()
r.pushTextColor(&RGB{106, 115, 125})
r.pushX(r.pdf.GetX())
} else {
x := r.popX()
r.pdf.SetX(r.pdf.GetX() - x + r.margin)
r.popTextColor()
r.Newline()
}
return ast.WalkContinue
}
func (r *PdfRenderer) renderBlockquoteMarker(node *ast.Node, entering bool) ast.WalkStatus {
return ast.WalkContinue
}
func (r *PdfRenderer) renderHeading(node *ast.Node, entering bool) ast.WalkStatus {
if entering {
r.Newline()
r.pdf.SetY(r.pdf.GetY() + 10)
headingSize := r.heading2Size
switch node.HeadingLevel {
case 1:
headingSize = r.heading1Size
case 2:
headingSize = r.heading2Size
case 3:
headingSize = r.heading3Size
case 4:
headingSize = r.heading4Size
case 5:
headingSize = r.heading5Size
case 6:
headingSize = r.heading6Size
default:
headingSize = float64(r.fontSize)
}
r.pushFont(&Font{"bold", "B", int(math.Round(headingSize))})
} else {
r.popFont()
r.pdf.SetY(r.pdf.GetY() + 6)
r.Newline()
}
return ast.WalkContinue
}
func (r *PdfRenderer) renderHeadingC8hMarker(node *ast.Node, entering bool) ast.WalkStatus {
return ast.WalkContinue
}
func (r *PdfRenderer) renderList(node *ast.Node, entering bool) ast.WalkStatus {
if entering {
r.Newline()
r.pdf.SetY(r.pdf.GetY() + 4)
nestedLevel := r.countParentContainerBlocks(node)
indent := float64(nestedLevel * 16)
r.pdf.SetX(r.pdf.GetX() + indent)
} else {
r.pdf.SetY(r.pdf.GetY() + 4)
r.Newline()
}
return ast.WalkContinue
}
func (r *PdfRenderer) renderListItem(node *ast.Node, entering bool) ast.WalkStatus {
if entering {
if node.Parent.FirstChild != node {
nestedLevel := r.countParentContainerBlocks(node) - 1
indent := float64(nestedLevel * 16)
r.pdf.SetX(r.pdf.GetX() + indent)
}
if 3 == node.ListData.Typ && "" != r.Options.GFMTaskListItemClass &&
nil != node.FirstChild && nil != node.FirstChild.FirstChild && ast.NodeTaskListItemMarker == node.FirstChild.FirstChild.Type {
r.WriteString(fmt.Sprintf("%s", node.ListData.Marker))
} else {
if 0 != node.ListData.BulletChar {
r.WriteString("● ")
} else {
r.WriteString(fmt.Sprint(node.ListData.Num) + ". ")
}
}
} else {
r.Newline()
}
return ast.WalkContinue
}
func (r *PdfRenderer) renderTaskListItemMarker(node *ast.Node, entering bool) ast.WalkStatus {
if entering {
var attrs [][]string
if node.TaskListItemChecked {
attrs = append(attrs, []string{"checked", ""})
}
attrs = append(attrs, []string{"disabled", ""}, []string{"type", "checkbox"})
//r.tag("input", attrs, true)
}
return ast.WalkContinue
}
func (r *PdfRenderer) renderThematicBreak(node *ast.Node, entering bool) ast.WalkStatus {