diff --git a/document/main.go b/document/main.go
index 7575c98..4734a66 100644
--- a/document/main.go
+++ b/document/main.go
@@ -9 +8,0 @@ import (
- "gui/utils"
@@ -56,14 +55 @@ func Open(index string) {
- // // 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()
+ render(css, d, wm)
@@ -73,2 +58,0 @@ func Open(index string) {
- rl.BeginDrawing()
- rl.ClearBackground(rl.RayWhite)
@@ -79,3 +62,0 @@ func Open(index string) {
- // vO1 := verticalScrollBar.Update(screenHeight, int32(maxHeight))
- // hO1 := horizontalScrollBar.Update(screenWidth, int32(maxWidth))
-
@@ -83 +63,0 @@ func Open(index string) {
- // if newWidth != screenWidth || newHeight != screenHeight || vO1 != vO || hO1 != hO {
@@ -90,2 +70 @@ func Open(index string) {
- // screenWidth, maxWidth, screenHeight, maxHeight = render(css, d, wm, screenWidth, screenHeight, vO, hO)
- render(css, d, wm, screenWidth, screenHeight, vO, hO)
+ render(css, d, wm)
@@ -94,3 +72,0 @@ func Open(index string) {
- // vO = vO1
- // hO = hO1
-
@@ -98,8 +74 @@ func Open(index string) {
- wm.Draw()
- // Draw vertical scrollbar
- // verticalScrollBar.Draw()
-
- // Draw horizontal scrollbar
- // horizontalScrollBar.Draw()
-
- rl.EndDrawing()
+ wm.DrawRectangles()
@@ -109 +78 @@ func Open(index string) {
-func render(css cstyle.CSS, d Doc, wm *painter.WindowManager, screenWidth, screenHeight int32, offsetY, offsetX float32) (int32, int32, int32, int32) {
+func render(css cstyle.CSS, d Doc, wm *painter.WindowManager) {
@@ -112,3 +80,0 @@ func render(css cstyle.CSS, d Doc, wm *painter.WindowManager, screenWidth, scree
- maxWidth := 0
- maxHeight := 0
-
@@ -123,8 +88,0 @@ func render(css cstyle.CSS, d Doc, wm *painter.WindowManager, screenWidth, scree
- maxWidth = int(utils.Max(float32(x+width), float32(maxWidth)))
- maxHeight = int(utils.Max(float32(y+height), float32(maxHeight)))
-
- x += float64(offsetX)
- y += float64(offsetY)
- width += float64(offsetX)
- height += float64(offsetY)
-
@@ -159 +117 @@ func render(css cstyle.CSS, d Doc, wm *painter.WindowManager, screenWidth, scree
- wm.AddNode(node)
+ wm.AddRectangle(node)
@@ -161 +118,0 @@ func render(css cstyle.CSS, d Doc, wm *painter.WindowManager, screenWidth, scree
- return screenWidth, int32(maxWidth), screenHeight, int32(maxHeight)
package document
import (
"bufio"
"gui/color"
"gui/cstyle"
"gui/font"
"gui/painter"
"gui/utils"
"net/url"
"os"
"path/filepath"
"strconv"
"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)
}
// // 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()
// 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())
// vO1 := verticalScrollBar.Update(screenHeight, int32(maxHeight))
// hO1 := horizontalScrollBar.Update(screenWidth, int32(maxWidth))
if newWidth != screenWidth || newHeight != screenHeight {
// if newWidth != screenWidth || newHeight != screenHeight || vO1 != vO || hO1 != hO {
// Window has been resized, handle the event
screenWidth = newWidth
screenHeight = newHeight
css.Width = float32(screenWidth)
css.Height = float32(screenHeight)
// screenWidth, maxWidth, screenHeight, maxHeight = render(css, d, wm, screenWidth, screenHeight, vO, hO)
render(css, d, wm, screenWidth, screenHeight, vO, hO)
}
// vO = vO1
// hO = hO1
// Draw rectangles
wm.Draw()
// Draw vertical scrollbar
// verticalScrollBar.Draw()
// Draw horizontal scrollbar
// horizontalScrollBar.Draw()
rl.EndDrawing()
}
}
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]
x, _ := strconv.ParseFloat(styles["x"], 32)
y, _ := strconv.ParseFloat(styles["y"], 32)
width, _ := strconv.ParseFloat(styles["width"], 32)
height, _ := strconv.ParseFloat(styles["height"], 32)
maxWidth = int(utils.Max(float32(x+width), float32(maxWidth)))
maxHeight = int(utils.Max(float32(y+height), float32(maxHeight)))
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)
}
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)
}
}