Skip to content

Commit

Permalink
Fix inline tag, add print debug
Browse files Browse the repository at this point in the history
  • Loading branch information
janczer committed Mar 12, 2017
1 parent a10fc59 commit 3f1c306
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 14 deletions.
74 changes: 63 additions & 11 deletions box.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,25 @@ func NewLayoutBox(boxType interface{}, style StyleNode) *LayoutBox {
}
}

func (s LayoutBox) print(l int) {
tab(l)
fmt.Printf("dimensions %v\n", s.dimensions)
tab(l)
fmt.Printf("box type %#v\n", s.box_type)
tab(l)
fmt.Printf("style %v\n", s.style)
tab(l)
fmt.Printf("childrens: \n")
l++
for i := 0; i < len(s.children); i++ {
s.children[i].print(l + 1)
}
}

func buildLayoutTree(styleNode StyleNode) *LayoutBox {

display := styleNode.display()
fmt.Println(display)
var boxType interface{}
switch display {
case "block":
Expand All @@ -126,34 +143,64 @@ func buildLayoutTree(styleNode StyleNode) *LayoutBox {
default:
panic("Root node has display: none.")
}
root := NewLayoutBox(boxType, styleNode)
fmt.Println(boxType)

l := LayoutBox{
box_type: boxType,
children: map[int]*LayoutBox{},
style: styleNode,
}

for _, child := range styleNode.children {
display = child.display()
switch display {
case "block":
root.children[len(root.children)] = buildLayoutTree(child)
childLayoutTree := buildLayoutTree(child)
l.children[len(l.children)] = childLayoutTree
case "inline":
fmt.Println("some inline")
inline := root.getInlineContainer()
inline.style = child
inline.children[len(inline.children)] = buildLayoutTree(child)
root.children[len(root.children)] = inline
default:
boxT := l.getLastContainer().box_type
//add anonymous box
switch boxT.(type) {
case AnonymousBlock, InlineNode:
childLayoutTree := buildLayoutTree(child)
l.children[len(l.children)] = childLayoutTree
case BlockNode:
//create AnonymousBlock
anonymous := LayoutBox{
box_type: AnonymousBlock{},
children: map[int]*LayoutBox{},
}
//buildLayoutTree
childLayoutTree := buildLayoutTree(child)
//add to AnonymousBlock
anonymous.children[len(anonymous.children)] = childLayoutTree
//add anonymousBox to child
l.children[len(l.children)] = &anonymous
}
}
}

return root
return &l
}

func (l *LayoutBox) getLastContainer() *LayoutBox {
if len(l.children) == 0 {
return l
}

if len(l.children) == 1 {
return l.children[0]
}

return l.children[len(l.children)-1]
}

func (l *LayoutBox) getInlineContainer() *LayoutBox {
boxT := l.box_type
switch boxT.(type) {
case AnonymousBlock, InlineNode:
fmt.Println("anonymous")
return l
case BlockNode:
fmt.Println("blocknode")
return NewLayoutBox(AnonymousBlock{}, StyleNode{})
default:
return l
Expand Down Expand Up @@ -189,6 +236,11 @@ func (l *LayoutBox) inlineBox(containBlock *Dimensions) {
l.dimensions.content.x = containBlock.content.x
l.dimensions.content.y = containBlock.content.y
//l.dimensions.content.y = containBlock.content.height
d := &l.dimensions

for _, child := range l.children {
child.layout(d)
}
}

func (l *LayoutBox) anonymousBox(containBlock *Dimensions) {
Expand Down
9 changes: 9 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ func Generate(html string, css string, out string) {

//parse html to Node tree
n := ParseHtml(string(xmlFile))
fmt.Println("\x1b[41m\x1b[1mprint Node\x1b[0m")
n.print(0)
fmt.Println("\x1b[41m\x1b[1mend print Node\x1b[0m\n")

cssFile, err := ioutil.ReadFile(css)
if err != nil {
Expand All @@ -28,13 +30,20 @@ func Generate(html string, css string, out string) {
stylesheet := p2.parseRules()

styletree := styleTree(n, &stylesheet)
fmt.Println("\x1b[41m\x1b[1mprint StyleTree\x1b[0m")
styletree.print(0)
fmt.Println("\x1b[41m\x1b[1mend print StyleTree\x1b[0m\n")

viewport := Dimensions{}
viewport.content.width = 210
viewport.content.height = 600

layoutTree := layoutTree(styletree, viewport)
fmt.Println("\n\x1b[41m\x1b[1mprint LayoutTree\x1b[0m")
layoutTree.print(0)
fmt.Println("\x1b[41m\x1b[1mend print LayoutTree\x1b[0m")
list := buildDisplayList(layoutTree)
fmt.Println(layoutTree)

pdf := gofpdf.New("P", "mm", "A4", "")
pdf.AddPage()
Expand Down
4 changes: 1 addition & 3 deletions node.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,8 @@ func tab(i int) {
func (n *Node) print(l int) {
tab(l)
l++
fmt.Printf("%s\n", n.node_type.element.tag_name)
fmt.Printf("%s text: %s\n", n.node_type.element.tag_name, n.node_type.text)
for i := 0; i < len(n.children); i++ {
tab(l)
fmt.Printf("%s text: %s\n", n.children[i].node_type.element.tag_name, n.children[i].node_type.text)
n.children[i].print(l + 1)
}
}
Expand Down
14 changes: 14 additions & 0 deletions style.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package htmlPDF

import (
"fmt"
"sort"
)

Expand Down Expand Up @@ -103,6 +104,19 @@ func styleTree(root *Node, stylesheet *Stylesheet) StyleNode {
}
}

func (s StyleNode) print(l int) {
tab(l)
fmt.Printf("node %v\n", s.node)
tab(l)
fmt.Printf("specified_values len %d\n", len(s.specified_values))
tab(l)
fmt.Printf("childrens: \n")
l++
for i := 0; i < len(s.children); i++ {
s.children[i].print(l + 1)
}
}

//Return true if ElementData contain one or more class
func (e ElementData) classContains(class map[int]string) bool {
if len(class) == 0 {
Expand Down

0 comments on commit 3f1c306

Please sign in to comment.