diff --git a/document/main.go b/document/main.go
index 8421c90..7575c98 100644
--- a/document/main.go
+++ b/document/main.go
@@ -4,0 +5 @@ import (
+ "gui/color"
@@ -5,0 +7 @@ import (
+ "gui/font"
@@ -6,0 +9 @@ import (
+ "gui/utils"
@@ -9,0 +13 @@ import (
+ "strconv"
@@ -52 +56,14 @@ func Open(index string) {
- css.Map(d.DOM)
+ // // Create vertical scrollbar
+ // verticalScrollBar := scroll.NewScrollBar(screenHeight, screenHeight, true)
+
+ // // Create horizontal scrollbar
+ // horizontalScrollBar := scroll.NewScrollBar(screenWidth, screenWidth, false)
+
+ rl.BeginDrawing()
+ rl.ClearBackground(rl.RayWhite)
+
+ var vO, hO float32
+ // var maxWidth, maxHeight int32
+
+ render(css, d, wm, screenWidth, screenHeight, 0, 0)
+ rl.EndDrawing()
@@ -61,0 +79,3 @@ func Open(index string) {
+ // vO1 := verticalScrollBar.Update(screenHeight, int32(maxHeight))
+ // hO1 := horizontalScrollBar.Update(screenWidth, int32(maxWidth))
+
@@ -62,0 +83 @@ func Open(index string) {
+ // if newWidth != screenWidth || newHeight != screenHeight || vO1 != vO || hO1 != hO {
@@ -69,9 +90,2 @@ func Open(index string) {
- // styles := css.Map(d.DOM)
- css.Map(d.DOM)
- // parent := cstyle.Node{
- // X: 0,
- // Y: 0,
- // Width: float32(screenWidth),
- // Height: float32(screenHeight),
- // }
- // render(styles, styles.Document, wm, parent)
+ // screenWidth, maxWidth, screenHeight, maxHeight = render(css, d, wm, screenWidth, screenHeight, vO, hO)
+ render(css, d, wm, screenWidth, screenHeight, vO, hO)
@@ -79,0 +94,3 @@ func Open(index string) {
+ // vO = vO1
+ // hO = hO1
+
@@ -81,0 +99,5 @@ func Open(index string) {
+ // Draw vertical scrollbar
+ // verticalScrollBar.Draw()
+
+ // Draw horizontal scrollbar
+ // horizontalScrollBar.Draw()
@@ -87,3 +109,8 @@ func Open(index string) {
-// func render(m cstyle.Mapped, n *html.Node, wm *painter.WindowManager, parent cstyle.Node) {
-// id := dom.GetAttribute(n, "DOMNODEID")
-// fs := font.GetFontSize(m.StyleMap[id])
+func render(css cstyle.CSS, d Doc, wm *painter.WindowManager, screenWidth, screenHeight int32, offsetY, offsetX float32) (int32, int32, int32, int32) {
+ p := css.Map(d.DOM)
+
+ maxWidth := 0
+ maxHeight := 0
+
+ for _, v := range p.Render {
+ styles := p.StyleMap[v.Id]
@@ -91 +118,4 @@ func Open(index string) {
-// x, y := m.Position(n, parent)
+ x, _ := strconv.ParseFloat(styles["x"], 32)
+ y, _ := strconv.ParseFloat(styles["y"], 32)
+ width, _ := strconv.ParseFloat(styles["width"], 32)
+ height, _ := strconv.ParseFloat(styles["height"], 32)
@@ -93,2 +123,2 @@ func Open(index string) {
-// width, _ := utils.ConvertToPixels(m.InheritProp(n, "width"), fs, float32(parent.Width))
-// height, _ := utils.ConvertToPixels(m.InheritProp(n, "height"), fs, float32(parent.Height))
+ maxWidth = int(utils.Max(float32(x+width), float32(maxWidth)))
+ maxHeight = int(utils.Max(float32(y+height), float32(maxHeight)))
@@ -96 +126,37 @@ func Open(index string) {
-// }
+ x += float64(offsetX)
+ y += float64(offsetY)
+ width += float64(offsetX)
+ height += float64(offsetY)
+
+ if height == 0 {
+ continue
+ }
+
+ bgColor := color.Background(styles)
+ fontColor, _ := color.Font(styles)
+
+ fontFile := font.GetFont(styles)
+
+ var text painter.Text = painter.Text{}
+
+ if len(dom.Children(v.Node)) == 0 {
+ text.Value = dom.InnerText(v.Node)
+ fs, _ := strconv.ParseFloat(styles["fontSize"], 32)
+ if fs == 0 {
+ fs = 16
+ }
+ text.Size = float32(fs)
+ text.Color = rl.NewColor(fontColor.R, fontColor.G, fontColor.B, fontColor.A)
+ text.Font = fontFile
+ }
+
+ node := painter.Rect{
+ Node: rl.NewRectangle(float32(x), float32(y), float32(width), float32(height)),
+ Color: rl.NewColor(bgColor.R, bgColor.G, bgColor.B, bgColor.A),
+ Text: text,
+ }
+
+ wm.AddNode(node)
+ }
+ return screenWidth, int32(maxWidth), screenHeight, int32(maxHeight)
+}
package document
import (
"bufio"
"gui/cstyle"
"gui/painter"
"net/url"
"os"
"path/filepath"
"strings"
rl "github.com/gen2brain/raylib-go/raylib"
"github.com/go-shiori/dom"
"golang.org/x/net/html"
)
type Doc struct {
StyleSheets []string
StyleTags []string
DOM *html.Node
Title string
}
func Open(index string) {
d := Parse(index)
wm := painter.NewWindowManager()
wm.FPS = true
// Initialization
var screenWidth int32 = 800
var screenHeight int32 = 450
// Open the window
wm.OpenWindow(d.Title, screenWidth, screenHeight)
defer wm.CloseWindow()
css := cstyle.CSS{
Width: 800,
Height: 450,
}
css.StyleSheet("./master.css")
for _, v := range d.StyleSheets {
css.StyleSheet(v)
}
for _, v := range d.StyleTags {
css.StyleTag(v)
}
css.Map(d.DOM)
// Main game loop
for !wm.WindowShouldClose() {
rl.BeginDrawing()
rl.ClearBackground(rl.RayWhite)
// Check if the window size has changed
newWidth := int32(rl.GetScreenWidth())
newHeight := int32(rl.GetScreenHeight())
if newWidth != screenWidth || newHeight != screenHeight {
// Window has been resized, handle the event
screenWidth = newWidth
screenHeight = newHeight
css.Width = float32(screenWidth)
css.Height = float32(screenHeight)
// styles := css.Map(d.DOM)
css.Map(d.DOM)
// parent := cstyle.Node{
// X: 0,
// Y: 0,
// Width: float32(screenWidth),
// Height: float32(screenHeight),
// }
// render(styles, styles.Document, wm, parent)
}
// Draw rectangles
wm.Draw()
rl.EndDrawing()
}
}
// func render(m cstyle.Mapped, n *html.Node, wm *painter.WindowManager, parent cstyle.Node) {
// id := dom.GetAttribute(n, "DOMNODEID")
// fs := font.GetFontSize(m.StyleMap[id])
// x, y := m.Position(n, parent)
// width, _ := utils.ConvertToPixels(m.InheritProp(n, "width"), fs, float32(parent.Width))
// height, _ := utils.ConvertToPixels(m.InheritProp(n, "height"), fs, float32(parent.Height))
// }
func Parse(path string) Doc {
file, err := os.Open(path)
check(err)
defer file.Close()
scanner := bufio.NewScanner(file)
var htmlContent string
for scanner.Scan() {
htmlContent += scanner.Text() + "\n"
}
check(scanner.Err())
doc, err := dom.FastParse(strings.NewReader(htmlContent))
check(err)
// Extract stylesheet link tags and style tags
stylesheets := extractStylesheets(doc, filepath.Dir(path))
styleTags := extractStyleTags(doc)
d := Doc{
StyleSheets: stylesheets,
StyleTags: styleTags,
DOM: doc,
Title: dom.InnerText(dom.GetElementsByTagName(doc, "title")[0]),
}
return d
}
func extractStylesheets(n *html.Node, baseDir string) []string {
var stylesheets []string
var dfs func(*html.Node)
dfs = func(node *html.Node) {
if node.Type == html.ElementNode && node.Data == "link" {
var href string
isStylesheet := false
for _, attr := range node.Attr {
if attr.Key == "rel" && attr.Val == "stylesheet" {
isStylesheet = true
} else if attr.Key == "href" {
href = attr.Val
}
}
if isStylesheet {
resolvedHref := localizePath(baseDir, href)
stylesheets = append(stylesheets, resolvedHref)
}
}
for c := node.FirstChild; c != nil; c = c.NextSibling {
dfs(c)
}
}
dfs(n)
return stylesheets
}
func extractStyleTags(n *html.Node) []string {
var styleTags []string
var dfs func(*html.Node)
dfs = func(node *html.Node) {
if node.Type == html.ElementNode && node.Data == "style" {
var styleContent strings.Builder
for c := node.FirstChild; c != nil; c = c.NextSibling {
if c.Type == html.TextNode {
styleContent.WriteString(c.Data)
}
}
styleTags = append(styleTags, styleContent.String())
}
for c := node.FirstChild; c != nil; c = c.NextSibling {
dfs(c)
}
}
dfs(n)
return styleTags
}
func localizePath(rootPath, filePath string) string {
// Check if the file path has a scheme, indicating it's a URL
u, err := url.Parse(filePath)
if err == nil && u.Scheme != "" {
return filePath
}
// Join the root path and the file path to create an absolute path
absPath := filepath.Join(rootPath, filePath)
// If the absolute path is the same as the original path, return it
if absPath == filePath {
return filePath
}
return "./" + absPath
}
func check(e error) {
if e != nil {
panic(e)
}
}