-
Notifications
You must be signed in to change notification settings - Fork 7
/
display.go
173 lines (152 loc) · 3.4 KB
/
display.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
package htmlPDF
import (
"fmt"
"github.com/jung-kurt/gofpdf"
)
type DisplayCommand struct {
command interface{}
}
type SolidColor struct {
color Color
rect Rect
}
type Text struct {
color Color
text string
rect Rect
}
func (d DisplayCommand) draw(pdf *gofpdf.Fpdf) {
switch command := d.command.(type) {
case SolidColor:
r := command.rect
c := command.color
pdf.SetFillColor(int(c.r), int(c.g), int(c.b))
pdf.Rect(r.x, r.y, r.width, r.height, "F")
case Text:
r := command.rect
c := command.color
t := command.text
pdf.SetTextColor(int(c.r), int(c.g), int(c.b))
pdf.Text(r.x, r.y, t)
}
}
func buildDisplayList(layoutRoot *LayoutBox) map[int]DisplayCommand {
list := map[int]DisplayCommand{}
renderLayoutBox(layoutRoot, list)
return list
}
func renderLayoutBox(layoutBox *LayoutBox, list map[int]DisplayCommand) {
//renderBackground
renderBackground(layoutBox, list)
//renderBorders
renderBorders(layoutBox, list)
//renderText
renderText(layoutBox, list)
//Render child
for _, child := range layoutBox.children {
renderLayoutBox(child, list)
}
}
func renderText(layoutBox *LayoutBox, list map[int]DisplayCommand) {
colorText := getColor(layoutBox, "color")
if colorText == nil {
return
}
text := layoutBox.style.node.node_type.text
if len(text) == 0 {
return
}
fmt.Println(text)
list[len(list)] = DisplayCommand{
command: Text{
color: *colorText,
text: text,
rect: layoutBox.dimensions.textBox(),
},
}
}
func renderBackground(layoutBox *LayoutBox, list map[int]DisplayCommand) {
colorBackrgound := getColor(layoutBox, "background")
if colorBackrgound == nil {
return
}
list[len(list)] = DisplayCommand{
command: SolidColor{
color: *colorBackrgound,
rect: layoutBox.dimensions.borderBox(),
},
}
}
func renderBorders(layoutBox *LayoutBox, list map[int]DisplayCommand) {
colorBorder := getColor(layoutBox, "border-color")
if colorBorder == nil {
return
}
//Return if white
//TODO change create Color with nil
if colorBorder.r == 255 && colorBorder.g == 255 && colorBorder.b == 255 {
return
}
d := layoutBox.dimensions
borderBox := d.borderBox()
// Left border
list[len(list)] = DisplayCommand{
command: SolidColor{
color: *colorBorder,
rect: Rect{
x: borderBox.x,
y: borderBox.y,
width: d.border.left,
height: borderBox.height,
},
},
}
// Right border
list[len(list)] = DisplayCommand{
command: SolidColor{
color: *colorBorder,
rect: Rect{
x: borderBox.x + borderBox.width - d.border.right,
y: borderBox.y,
width: d.border.right,
height: borderBox.height,
},
},
}
// Top border
list[len(list)] = DisplayCommand{
command: SolidColor{
color: *colorBorder,
rect: Rect{
x: borderBox.x,
y: borderBox.y,
width: borderBox.width,
height: d.border.top,
},
},
}
// Bottom border
list[len(list)] = DisplayCommand{
command: SolidColor{
color: *colorBorder,
rect: Rect{
x: borderBox.x,
y: borderBox.y + borderBox.height - d.border.bottom,
width: borderBox.width,
height: d.border.bottom,
},
},
}
}
//Return the specified color for CSS property name
func getColor(layoutBox *LayoutBox, name string) *Color {
switch layoutBox.box_type.(type) {
case BlockNode, InlineNode:
color := layoutBox.style.value(name).color
return &color
case AnonymousBlock:
return nil
default:
return nil
}
}