Commits
Diff
diff --git a/cstyle/main.go b/cstyle/main.go
index b16ae74..39c08ac 100644
--- a/cstyle/main.go
+++ b/cstyle/main.go
@@ -8 +7,0 @@ import (
- "grim/canvas"
@@ -57,0 +57 @@ func (c *CSS) ComputeNodeStyle(n *element.Node) element.State {
+ shelf := c.Adapter.Library
@@ -229 +229 @@ func (c *CSS) ComputeNodeStyle(n *element.Node) element.State {
- m, exists := c.Adapter.Textures[n.Properties.Id]["text"]
+ exists := c.Adapter.Library.Check(key)
@@ -231 +231 @@ func (c *CSS) ComputeNodeStyle(n *element.Node) element.State {
- if exists && m == key {
+ if exists {
@@ -242,4 +241,0 @@ func (c *CSS) ComputeNodeStyle(n *element.Node) element.State {
- if exists {
- c.Adapter.UnloadTexture(n.Properties.Id, "text")
- }
- fmt.Println(n.Properties.Id, key, exists)
@@ -248,2 +244 @@ func (c *CSS) ComputeNodeStyle(n *element.Node) element.State {
- c.Adapter.LoadTexture(n.Properties.Id, "text", key, data)
- self.Textures = append(self.Textures, key)
+ self.Textures = append(self.Textures, c.Adapter.Library.Set(key, data))
@@ -282 +277 @@ func (c *CSS) ComputeNodeStyle(n *element.Node) element.State {
- c.Adapter.LoadTexture(n.Properties.Id, "canvas", key, img)
+ can := shelf.Set(key, img)
@@ -284 +279 @@ func (c *CSS) ComputeNodeStyle(n *element.Node) element.State {
- self.Textures = append(self.Textures, key)
+ self.Textures = append(self.Textures, can)
@@ -343,38 +338 @@ func (c *CSS) ComputeNodeStyle(n *element.Node) element.State {
- border.Draw(&self, c.Adapter, n.Properties.Id)
- // Render bg
- wbw := int(self.Width + self.Border.Left.Width + self.Border.Right.Width)
- hbw := int(self.Height + self.Border.Top.Width + self.Border.Bottom.Width)
-
- key := strconv.Itoa(wbw) + strconv.Itoa(hbw) + utils.RGBAtoString(self.Background)
-
- match, exists := c.Adapter.Textures[n.Properties.Id]["background"]
-
- if exists && match == key {
- lookup := make(map[string]struct{}, len(self.Textures))
- for _, v := range self.Textures {
- lookup[v] = struct{}{}
- }
-
- if _, found := lookup[key]; !found {
- self.Textures = append([]string{key}, self.Textures...)
- }
- } else {
- if exists {
- c.Adapter.UnloadTexture(n.Properties.Id, "background")
- }
- if self.Background.A > 0 {
- fmt.Println("miss", n.Properties.Id, wh.Width, m, p, match, key, exists)
- // Only make the drawing if it's not found
- can := canvas.NewCanvas(wbw, hbw)
- can.BeginPath()
- can.SetFillStyle(self.Background.R, self.Background.G, self.Background.B, self.Background.A)
- can.SetLineWidth(10)
- can.RoundedRect(0, 0, float64(wbw), float64(hbw),
- []float64{float64(self.Border.Radius.TopLeft), float64(self.Border.Radius.TopRight), float64(self.Border.Radius.BottomRight), float64(self.Border.Radius.BottomLeft)})
- can.Fill()
- can.ClosePath()
-
- c.Adapter.LoadTexture(n.Properties.Id, "background", key, can.Context.Image())
- self.Textures = append([]string{key}, self.Textures...)
- }
- }
+ border.Draw(&self, shelf)
package cstyle
import (
"fmt"
"github.com/golang/freetype/truetype"
adapter "grim/adapters"
"grim/border"
"grim/canvas"
"grim/color"
"grim/element"
"grim/font"
"grim/utils"
"image"
"strconv"
"strings"
"golang.org/x/image/draw"
)
type Plugin struct {
Selector func(*element.Node, *CSS) bool
Handler func(*element.Node, *CSS)
}
type Transformer struct {
Selector func(*element.Node, *CSS) bool
Handler func(*element.Node, *CSS) *element.Node
}
type CSS struct {
Width float32
Height float32
Plugins []Plugin
Transformers []Transformer
Fonts map[string]*truetype.Font
Adapter *adapter.Adapter
Path string
State map[string]element.State
}
func (c *CSS) AddPlugin(plugin Plugin) {
c.Plugins = append(c.Plugins, plugin)
}
func (c *CSS) AddTransformer(transformer Transformer) {
c.Transformers = append(c.Transformers, transformer)
}
var nonRenderTags = map[string]bool{
"head": true,
"meta": true,
"link": true,
"title": true,
"style": true,
}
func (c *CSS) ComputeNodeStyle(n *element.Node) element.State {
// Head is not renderable
s := c.State
self := s[n.Properties.Id]
if nonRenderTags[n.TagName] {
return self
}
for _, v := range c.Transformers {
if v.Selector(n, c) {
v.Handler(n, c)
}
}
plugins := c.Plugins
parent := s[n.Parent.Properties.Id]
// Cache the style map
style := n.ComputedStyle
for k, v := range n.Styles() {
style[k] = v
}
self.Background, _ = color.ParseRGBA(style["background-color"])
self.Border, _ = border.Parse(style, self, parent)
if style["font-size"] == "" {
n.ComputedStyle["font-size"] = "1em"
}
fs := utils.ConvertToPixels(n.ComputedStyle["font-size"], parent.EM, parent.Width)
self.EM = fs
if style["display"] == "none" {
self.X, self.Y, self.Width, self.Height = 0, 0, 0, 0
c.State[n.Properties.Id] = self
return self
}
// Set Z index value to be sorted in window
if zIndex, err := strconv.Atoi(style["z-index"]); err == nil {
self.Z = float32(zIndex)
}
if self.Z > 0 {
self.Z = parent.Z + 1
}
c.State[n.Properties.Id] = self
c.State[n.Properties.Id] = self
wh, m, p := utils.FindBounds(*n, style, &c.State)
self.Margin = m
self.Padding = p
self.Width = wh.Width
self.Height = wh.Height
self.Cursor = style["cursor"]
c.State[n.Properties.Id] = self
x, y := parent.X, parent.Y
offsetX, offsetY := utils.GetXY(*n, c.State)
x += offsetX
y += offsetY
var top, left, right, bottom bool
if style["position"] == "absolute" {
// !DEVMAN: Properties.Id is the ancestory of an element with colons seperating them
// + if we split them up we can check the parents without recursion or a while (for true) loop
// + NOTE: See utils.GenerateUnqineId to see how they are made
ancestors := strings.Split(n.Properties.Id, ":")
offsetNode := n
// Should skip the current element and the ROOT
for i := len(ancestors) - 2; i > 0; i-- {
offsetNode = offsetNode.Parent
pos := offsetNode.ComputedStyle["position"]
if pos == "relative" || pos == "absolute" {
break
}
}
base := s[offsetNode.Properties.Id]
if topVal := style["top"]; topVal != "" {
y = utils.ConvertToPixels(topVal, self.EM, parent.Width) + base.Y
top = true
}
if leftVal := style["left"]; leftVal != "" {
x = utils.ConvertToPixels(leftVal, self.EM, parent.Width) + base.X
left = true
}
if rightVal := style["right"]; rightVal != "" {
x = base.X + ((base.Width - self.Width) - utils.ConvertToPixels(rightVal, self.EM, parent.Width))
right = true
}
if bottomVal := style["bottom"]; bottomVal != "" {
y = base.Y + ((base.Height - self.Height) - utils.ConvertToPixels(bottomVal, self.EM, parent.Width))
bottom = true
}
} else {
for i, v := range n.Parent.Children {
if v.ComputedStyle["position"] != "absolute" {
if v.Properties.Id == n.Properties.Id {
if i > 0 {
sib := n.Parent.Children[i-1]
sibling := s[sib.Properties.Id]
if sib.ComputedStyle["position"] != "absolute" {
if style["display"] == "inline" {
y = sibling.Y
if sib.ComputedStyle["display"] != "inline" {
y += sibling.Height
}
} else {
y = sibling.Y + sibling.Height + sibling.Border.Top.Width + sibling.Border.Bottom.Width + sibling.Margin.Bottom
}
}
}
break
} else if style["display"] != "inline" {
vState := s[v.Properties.Id]
y += vState.Margin.Top + vState.Margin.Bottom + vState.Padding.Top + vState.Padding.Bottom + vState.Height + self.Border.Top.Width
}
}
}
}
relPos := !top && !left && !right && !bottom
if left || relPos {
x += m.Left
}
if top || relPos {
y += m.Top
}
if right {
x -= m.Right
}
if bottom {
y -= m.Bottom
}
self.X = x
self.Y = y
self.ContentEditable = n.ContentEditable
c.State[n.Properties.Id] = self
if !element.ChildrenHaveText(n) && len(n.InnerText) > 0 {
n.InnerText = strings.TrimSpace(n.InnerText)
italic := false
if style["font-style"] == "italic" {
italic = true
}
if c.Fonts == nil {
c.Fonts = map[string]*truetype.Font{}
}
fid := style["font-family"] + fmt.Sprint(style["font-weight"], italic)
fnt, ok := c.Fonts[fid]
if !ok {
f, err := font.LoadFont(style["font-family"], int(self.EM), style["font-weight"], italic, &c.Adapter.FileSystem)
if err != nil {
panic(err)
}
c.Fonts[fid] = f
fnt = f
}
metadata := font.GetMetaData(n, style, &c.State, fnt)
key := font.Key(metadata)
m, exists := c.Adapter.Textures[n.Properties.Id]["text"]
var width int
if exists && m == key {
lookup := make(map[string]struct{}, len(self.Textures))
for _, v := range self.Textures {
lookup[v] = struct{}{}
}
if _, found := lookup[key]; !found {
self.Textures = append(self.Textures, key)
}
width = font.MeasureText(metadata, metadata.Text+" ")
} else {
if exists {
c.Adapter.UnloadTexture(n.Properties.Id, "text")
}
fmt.Println(n.Properties.Id, key, exists)
var data image.Image
data, width = font.Render(metadata)
c.Adapter.LoadTexture(n.Properties.Id, "text", key, data)
self.Textures = append(self.Textures, key)
}
if (style["height"] == "" && style["min-height"] == "") || n.TagName == "text" {
self.Height = float32(metadata.LineHeight)
n.ComputedStyle["height"] = strconv.Itoa(int(self.Height)) + "px"
}
if style["width"] == "" && style["min-width"] == "" {
self.Width = float32(width)
n.ComputedStyle["width"] = strconv.Itoa(int(self.Width)) + "px"
}
}
// Load canvas into textures
if n.TagName == "canvas" {
if n.Canvas != nil {
found := false
key := n.Properties.Id + "canvas"
for _, v := range self.Textures {
if v == key {
found = true
}
}
img := n.Canvas.Context.Image()
b := img.Bounds()
if b.Dx() != int(self.Width) || b.Dy() != int(self.Height) {
resized := image.NewRGBA(image.Rect(0, 0, int(self.Width), int(self.Height)))
draw.CatmullRom.Scale(resized, resized.Bounds(), img, img.Bounds(), draw.Over, &draw.Options{})
// n.Canvas.RGBA = resized
}
c.Adapter.LoadTexture(n.Properties.Id, "canvas", key, img)
if !found {
self.Textures = append(self.Textures, key)
}
}
}
self.Value = n.InnerText
self.TabIndex = n.TabIndex
c.State[n.Properties.Id] = self
c.State[n.Parent.Properties.Id] = parent
self.ScrollHeight = 0
self.ScrollWidth = 0
var childYOffset float32
for i := 0; i < len(n.Children); i++ {
v := n.Children[i]
v.Parent = n
cState := c.ComputeNodeStyle(v)
if style["height"] == "" && style["max-height"] == "" {
if v.ComputedStyle["position"] != "absolute" && cState.Y+cState.Height > childYOffset {
childYOffset = cState.Y + cState.Height
self.Height = cState.Y - self.Border.Top.Width - self.Y + cState.Height
self.Height += cState.Margin.Top + cState.Margin.Bottom + cState.Padding.Top + cState.Padding.Bottom + cState.Border.Top.Width + cState.Border.Bottom.Width
}
}
sh := int((cState.Y + cState.Height) - self.Y)
if self.ScrollHeight < sh {
if n.Children[i].TagName != "grim-track" {
self.ScrollHeight = sh
self.ScrollHeight += int(cState.Margin.Top + cState.Margin.Bottom + cState.Padding.Top + cState.Padding.Bottom + cState.Border.Top.Width + cState.Border.Bottom.Width)
}
}
sw := int((cState.X + cState.Width) - self.X)
if self.ScrollWidth < sw {
if n.Children[i].TagName != "grim-track" {
self.ScrollWidth = sw
}
}
if cState.ScrollWidth > self.ScrollWidth {
self.ScrollWidth = cState.ScrollWidth
}
if cState.Width > self.Width && style["width"] == "" {
self.Width = cState.Width
}
}
self.ScrollHeight += int(self.Padding.Bottom + self.Padding.Top)
self.ScrollWidth += int(self.Padding.Right)
if style["height"] == "" {
self.Height += self.Padding.Bottom
}
c.State[n.Properties.Id] = self
border.Draw(&self, c.Adapter, n.Properties.Id)
// Render bg
wbw := int(self.Width + self.Border.Left.Width + self.Border.Right.Width)
hbw := int(self.Height + self.Border.Top.Width + self.Border.Bottom.Width)
key := strconv.Itoa(wbw) + strconv.Itoa(hbw) + utils.RGBAtoString(self.Background)
match, exists := c.Adapter.Textures[n.Properties.Id]["background"]
if exists && match == key {
lookup := make(map[string]struct{}, len(self.Textures))
for _, v := range self.Textures {
lookup[v] = struct{}{}
}
if _, found := lookup[key]; !found {
self.Textures = append([]string{key}, self.Textures...)
}
} else {
if exists {
c.Adapter.UnloadTexture(n.Properties.Id, "background")
}
if self.Background.A > 0 {
fmt.Println("miss", n.Properties.Id, wh.Width, m, p, match, key, exists)
// Only make the drawing if it's not found
can := canvas.NewCanvas(wbw, hbw)
can.BeginPath()
can.SetFillStyle(self.Background.R, self.Background.G, self.Background.B, self.Background.A)
can.SetLineWidth(10)
can.RoundedRect(0, 0, float64(wbw), float64(hbw),
[]float64{float64(self.Border.Radius.TopLeft), float64(self.Border.Radius.TopRight), float64(self.Border.Radius.BottomRight), float64(self.Border.Radius.BottomLeft)})
can.Fill()
can.ClosePath()
c.Adapter.LoadTexture(n.Properties.Id, "background", key, can.Context.Image())
self.Textures = append([]string{key}, self.Textures...)
}
}
c.State[n.Properties.Id] = self
for _, v := range plugins {
if v.Selector(n, c) {
v.Handler(n, c)
}
}
return self
}