Diff
diff --git a/cstyle/main.go b/cstyle/main.go
index eb88a53..0047bbb 100644
--- a/cstyle/main.go
+++ b/cstyle/main.go
@@ -5,0 +6 @@ import (
+ "math"
@@ -8,2 +9,2 @@ import (
-
- "gui/utils"
+ "regexp"
+ "strconv"
@@ -30,6 +30,0 @@ type Mapped struct {
- Render []Node
-}
-
-type Node struct {
- Node *html.Node
- Id string
@@ -56,0 +52 @@ func (c *CSS) Map(doc *html.Node) Mapped {
+ fmt.Printf("%s %s\n", key, matching)
@@ -68 +64 @@ func (c *CSS) Map(doc *html.Node) Mapped {
- styleMap[id] = utils.Merge(styleMap[id], styles)
+ styleMap[id] = merge(styleMap[id], styles)
@@ -75 +70,0 @@ func (c *CSS) Map(doc *html.Node) Mapped {
- // Inherit CSS styles from parent
@@ -77,7 +72 @@ func (c *CSS) Map(doc *html.Node) Mapped {
- // Calculate the width and height
- fmt.Printf("123 %f %f\n", c.Width, c.Height)
- size(doc, styleMap, c.Width, c.Height)
- // Calculate the X and Y values
- position(doc, styleMap, 0, 0, c.Width, c.Height, c.Width, c.Height)
-
- renderLine := flatten(doc)
+ size(doc, styleMap, c)
@@ -88 +76,0 @@ func (c *CSS) Map(doc *html.Node) Mapped {
- Render: renderLine,
@@ -93,67 +81,2 @@ func (c *CSS) Map(doc *html.Node) Mapped {
-func flatten(n *html.Node) []Node {
- var nodes []Node
- id := dom.GetAttribute(n, "DOMNODEID")
- nodes = append(nodes, Node{
- Node: n,
- Id: id,
- })
-
- children := dom.Children(n)
- if len(children) > 0 {
- for _, ch := range children {
- chNodes := flatten(ch)
- nodes = append(nodes, chNodes...)
- }
- }
- return nodes
-}
-
-func position(n *html.Node, styleMap map[string]map[string]string, x1, y1, x2, y2, windowWidth, windowHeight float32) (float32, float32, float32, float32) {
- id := dom.GetAttribute(n, "DOMNODEID")
- if len(id) == 0 {
- id = dom.TagName(n) + fmt.Sprint(rand.Int63())
- dom.SetAttribute(n, "DOMNODEID", id)
- }
-
- width, _ := utils.ConvertToPixels(styleMap[id]["width"], windowWidth)
- height, _ := utils.ConvertToPixels(styleMap[id]["height"], windowHeight)
-
- x2 = width
- y2 = height
-
- if styleMap[id]["margin-left"] != "" {
- v, _ := utils.ConvertToPixels(styleMap[id]["margin-left"], windowWidth)
- x1 += v
- }
- if styleMap[id]["margin-top"] != "" {
- v, _ := utils.ConvertToPixels(styleMap[id]["margin-top"], windowHeight)
- y1 += v
- }
-
- if styleMap[id]["margin-right"] != "" {
- v, _ := utils.ConvertToPixels(styleMap[id]["margin-left"], windowWidth)
- x2 += v
- }
- if styleMap[id]["margin-bottom"] != "" {
- v, _ := utils.ConvertToPixels(styleMap[id]["margin-top"], windowHeight)
- y2 += v
- }
-
- children := dom.Children(n)
-
- if len(children) > 0 {
- for _, ch := range children {
- _, b, _, d := position(ch, styleMap, x1, y1, x2, y2, width, height)
- y1 += b + d
- }
- }
- if styleMap[id] == nil {
- styleMap[id] = make(map[string]string)
- }
- styleMap[id]["x"] = fmt.Sprintf("%g", x1)
- styleMap[id]["y"] = fmt.Sprintf("%g", y1)
- return x1, y1, x2, y2
-}
-
-func size(n *html.Node, styleMap map[string]map[string]string, windowWidth, windowHeight float32) (float32, float32) {
- fmt.Printf("%f %f\n", windowWidth, windowHeight)
+func size(n *html.Node, styleMap map[string]map[string]string, c *CSS) (float32, float32) {
+ println("NAME: ", dom.TagName(n))
@@ -166,0 +90,2 @@ func size(n *html.Node, styleMap map[string]map[string]string, windowWidth, wind
+ var fixedWidth, fixedHeight bool
+
@@ -168,5 +93,2 @@ func size(n *html.Node, styleMap map[string]map[string]string, windowWidth, wind
- width, _ = utils.ConvertToPixels(styleMap[id]["width"], windowWidth)
- fmt.Printf("%f %f %s %s\n", width, windowWidth, dom.TagName(n), styleMap[id]["width"])
- fmt.Printf("%s\n", styleMap[id])
- t, _ := utils.ConvertToPixels("50%", 100)
- fmt.Printf("%s\n", t)
+ fixedWidth = false
+ width, _ = ConvertToPixels(styleMap[id]["width"], c)
@@ -176 +98,2 @@ func size(n *html.Node, styleMap map[string]map[string]string, windowWidth, wind
- height, _ = utils.ConvertToPixels(styleMap[id]["height"], windowHeight)
+ fixedHeight = false
+ height, _ = ConvertToPixels(styleMap[id]["height"], c)
@@ -178 +100,0 @@ func size(n *html.Node, styleMap map[string]map[string]string, windowWidth, wind
-
@@ -182,2 +104,3 @@ func size(n *html.Node, styleMap map[string]map[string]string, windowWidth, wind
- if width == 0 {
- width = windowWidth
+ w, h := size(ch, styleMap, c)
+ if !fixedWidth {
+ width = max(w, width)
@@ -185,2 +108,2 @@ func size(n *html.Node, styleMap map[string]map[string]string, windowWidth, wind
- if height == 0 {
- height = windowHeight
+ if !fixedHeight {
+ height = max(h, height)
@@ -188,6 +110,0 @@ func size(n *html.Node, styleMap map[string]map[string]string, windowWidth, wind
- w, h := size(ch, styleMap, width, height)
-
- width = utils.Max(w, width)
-
- height += h
-
@@ -201,7 +118,9 @@ func size(n *html.Node, styleMap map[string]map[string]string, windowWidth, wind
- fs, _ := utils.ConvertToPixels(styleMap[id]["font-size"], width)
- w, h := utils.GetTextBounds(text, fs, width, height)
-
- width = w
-
- height = h
-
+ fs, _ := ConvertToPixels(styleMap[id]["font-size"], c)
+ w, h := getTextBounds(text, fs, width, height)
+ fmt.Printf("%f, %f\n", w, h)
+ if !fixedWidth {
+ width = max(w, width)
+ }
+ if !fixedHeight {
+ height = max(h, height)
+ }
@@ -212,7 +131 @@ func size(n *html.Node, styleMap map[string]map[string]string, windowWidth, wind
- width, height = utils.AddMarginAndPadding(styleMap, id, width, height)
-
- if styleMap[id] == nil {
- styleMap[id] = make(map[string]string)
- }
- styleMap[id]["width"] = fmt.Sprintf("%g", width)
- styleMap[id]["height"] = fmt.Sprintf("%g", height)
+ fmt.Printf("RET: %s %f %f\n", dom.TagName(n), width, height)
@@ -223,0 +137 @@ func inherit(n *html.Node, styleMap map[string]map[string]string) {
+ fmt.Println("Element:", n.Data)
@@ -237 +151 @@ func inherit(n *html.Node, styleMap map[string]map[string]string) {
- styleMap[id] = utils.ExMerge(styleMap[id], styleMap[pId])
+ styleMap[id] = xMerge(styleMap[id], styleMap[pId])
@@ -244,0 +159,86 @@ func inherit(n *html.Node, styleMap map[string]map[string]string) {
+
+func merge(m1, m2 map[string]string) map[string]string {
+ // Create a new map and copy m1 into it
+ result := make(map[string]string)
+ for k, v := range m1 {
+ result[k] = v
+ }
+
+ // Merge m2 into the new map
+ for k, v := range m2 {
+ result[k] = v
+ }
+
+ return result
+}
+
+func xMerge(m1, m2 map[string]string) map[string]string {
+ // Create a new map and copy m1 into it
+ result := make(map[string]string)
+ for k, v := range m1 {
+ result[k] = v
+ }
+
+ // Merge m2 into the new map only if the key is not already present
+ for k, v := range m2 {
+ if result[k] == "" {
+ result[k] = v
+ }
+ }
+
+ return result
+}
+
+// ConvertToPixels converts a CSS measurement to pixels.
+func ConvertToPixels(value string, c *CSS) (float32, error) {
+ // Define conversion factors for different units
+ unitFactors := map[string]float32{
+ "px": 1,
+ "em": 16, // Assuming 1em = 16px (typical default font size in browsers)
+ "pt": 1.33, // Assuming 1pt = 1.33px (typical conversion)
+ "pc": 16.89, // Assuming 1pc = 16.89px (typical conversion)
+ "vw": c.Width / 100,
+ "vh": c.Height / 100,
+ }
+
+ // Extract numeric value and unit using regular expression
+ re := regexp.MustCompile(`^(\d+(?:\.\d+)?)\s*([a-zA-Z]+)$`)
+ match := re.FindStringSubmatch(value)
+
+ if len(match) != 3 {
+ return 0, fmt.Errorf("invalid input format")
+ }
+
+ numericValue, err := (strconv.ParseFloat(match[1], 64))
+ numericValue32 := float32(numericValue)
+ check(err)
+
+ unit, ok := unitFactors[match[2]]
+ if !ok {
+ return 0, fmt.Errorf("unsupported unit: %s", match[2])
+ }
+
+ return numericValue32 * unit, nil
+}
+
+func max(a, b float32) float32 {
+ if a > b {
+ return a
+ } else {
+ return b
+ }
+}
+
+func getTextBounds(text string, fontSize, width, height float32) (float32, float32) {
+ w := float32(len(text) * int(fontSize))
+ h := fontSize
+ if width > 0 && height > 0 {
+ if w > width {
+ height = max(height, float32(math.Ceil(float64(w/width)))*h)
+ }
+ return width, height
+ } else {
+ return w, h
+ }
+
+}