-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathbun.go
102 lines (77 loc) Β· 2.5 KB
/
bun.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
package bun
import (
"fmt"
gg "../gg"
structs "../structs"
)
func RenderDocument(ctx *gg.Context, document *structs.HTMLDocument) {
//tree.Children[0] is head
body := document.RootElement.Children[1]
document.RootElement.Style.Width = float64(ctx.Width())
document.RootElement.Style.Height = float64(ctx.Height())
layoutDOM(ctx, body, 0)
}
func getNodeContent(NodeDOM *structs.NodeDOM) string {
return NodeDOM.Content
}
func getElementName(NodeDOM *structs.NodeDOM) string {
return NodeDOM.Element
}
func getNodeChildren(NodeDOM *structs.NodeDOM) []*structs.NodeDOM {
return NodeDOM.Children
}
func walkDOM(TreeDOM *structs.NodeDOM, d string) {
fmt.Println(d, getElementName(TreeDOM))
nodeChildren := getNodeChildren(TreeDOM)
for i := 0; i < len(nodeChildren); i++ {
walkDOM(nodeChildren[i], d+"-")
}
}
func layoutDOM(ctx *gg.Context, node *structs.NodeDOM, childIdx int) {
nodeChildren := getNodeChildren(node)
if node.Style.Display == "block" {
calculateBlockLayout(ctx, node, childIdx)
for i := 0; i < len(nodeChildren); i++ {
layoutDOM(ctx, nodeChildren[i], i)
}
paintBlockElement(ctx, node)
}
}
func paintBlockElement(ctx *gg.Context, node *structs.NodeDOM) {
ctx.DrawRectangle(node.Style.Left, node.Style.Top, node.Style.Width, node.Style.Height)
ctx.SetRGBA(node.Style.BackgroundColor.R, node.Style.BackgroundColor.G, node.Style.BackgroundColor.B, node.Style.BackgroundColor.A)
ctx.Fill()
ctx.SetRGBA(node.Style.Color.R, node.Style.Color.G, node.Style.Color.B, node.Style.Color.A)
ctx.DrawStringWrapped(node.Content, node.Style.Left, node.Style.Top, 0, 0, node.Style.Width, 1, gg.AlignLeft)
ctx.Fill()
}
func calculateBlockLayout(ctx *gg.Context, node *structs.NodeDOM, childIdx int) {
if node.Style.Width == 0 {
node.Style.Width = node.Parent.Style.Width
}
if node.Style.Height == 0 && len(node.Content) > 0 {
node.Style.Height = ctx.MeasureStringWrapped(node.Content, node.Style.Width, 1) + 2
}
if childIdx > 0 {
prev := node.Parent.Children[childIdx-1]
if prev.Style.Display == "block" {
node.Style.Top = prev.Style.Top + prev.Style.Height
} else {
node.Style.Top = prev.Style.Top
}
}
}
func GetPageTitle(TreeDOM *structs.NodeDOM) string {
nodeChildren := getNodeChildren(TreeDOM)
pageTitle := "Sem Titulo"
if getElementName(TreeDOM) == "title" {
return getNodeContent(TreeDOM)
}
for i := 0; i < len(nodeChildren); i++ {
nPageTitle := GetPageTitle(nodeChildren[i])
if nPageTitle != "Sem Titulo" {
pageTitle = nPageTitle
}
}
return pageTitle
}