-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathmarkdown_layout.go
More file actions
75 lines (66 loc) · 2.54 KB
/
markdown_layout.go
File metadata and controls
75 lines (66 loc) · 2.54 KB
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
package gopdf
import "github.com/tiechui1994/gopdf/core"
// markdown_layout.go:复合结点子结点队列上的分页与切片策略(CommonGenerateAtomicCell)。
// applyBlockTop 在块级结点首次绘制子结点之前下移光标,仅施加盒模型的上边距与上内边距(水平Inset 在 SetToken 时并入子结点 Margin,避免跨页后丢失)。
func applyBlockTop(r *core.Report, box MdBoxModel) {
if r == nil {
return
}
x, y := r.GetXY()
y += box.Margin.Top + box.Padding.Top
r.SetXY(x, y)
}
// applyBlockBottom 在块级结点全部子结点绘制完毕后施加下边距与下内边距。
func applyBlockBottom(r *core.Report, box MdBoxModel) {
if r == nil {
return
}
x, y := r.GetXY()
y += box.Margin.Bottom + box.Padding.Bottom
r.SetXY(x, y)
}
// mergeBlockHorizontalInsets 将块级盒模型的左右 Margin/Padding 累加到 ElementBase(竖直方向由 applyBlockTop/Bottom 处理)。
func mergeBlockHorizontalInsets(box MdBoxModel, e *ElementBase) {
if e == nil {
return
}
e.Margin.Left += box.Margin.Left + box.Padding.Left
e.Margin.Right += box.Margin.Right + box.Padding.Right
}
// mergeInlineBoxModel 将行内类型对应的盒模型并入 ElementBase(各边相加)。
func mergeInlineBoxModel(ib MdBoxModel, e *ElementBase) {
if e == nil {
return
}
e.Margin.Top += ib.Margin.Top
e.Margin.Right += ib.Margin.Right
e.Margin.Bottom += ib.Margin.Bottom
e.Margin.Left += ib.Margin.Left
e.Padding.Top += ib.Padding.Top
e.Padding.Right += ib.Padding.Right
e.Padding.Bottom += ib.Padding.Bottom
e.Padding.Left += ib.Padding.Left
}
// CommonGenerateAtomicCell 顺序执行 children 中每个 markdownNode。
//
// 与分页相关的切片约定(须与顶层 MarkdownText.GenerateAtomicCell 索引策略一致):
// - 若第 i 个子结点返回 pagebreak==true 且 over==true,且后面仍有兄弟:剪掉 [0,i](含当前结点),
// 下一页从原 i+1 继续(典型:MdSpace 页底放不下时被丢弃)。
// - 其它 pagebreak:保留切片从 i 起;若 over==false,同一 MdText 等会在下一页从头结点继续。
func CommonGenerateAtomicCell(children *[]markdownNode) (pagebreak, over bool, err error) {
for i, comment := range *children {
pagebreak, over, err = comment.GenerateAtomicCell()
if err != nil {
return
}
if pagebreak {
if over && i != len(*children)-1 {
*children = (*children)[i+1:]
return pagebreak, len(*children) == 0, nil
}
*children = (*children)[i:]
return pagebreak, len(*children) == 0, nil
}
}
return false, true, nil
}