about summary refs log tree commit diff stats
path: root/src/css/style.nim
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2021-11-19 21:42:39 +0100
committerbptato <nincsnevem662@gmail.com>2021-11-19 21:52:31 +0100
commit8bbff1f79920fa8175da7425f8f23ad08b97f79e (patch)
tree830abcc6918f8c96bce5538eff5384518876e378 /src/css/style.nim
parent42aacf6bf1a52a8ebad902d8ee5adeef57f8822a (diff)
downloadchawan-8bbff1f79920fa8175da7425f8f23ad08b97f79e.tar.gz
User stylesheets and applyStylesheets optimizations
Diffstat (limited to 'src/css/style.nim')
-rw-r--r--src/css/style.nim38
1 files changed, 24 insertions, 14 deletions
diff --git a/src/css/style.nim b/src/css/style.nim
index 9611f862..f018f077 100644
--- a/src/css/style.nim
+++ b/src/css/style.nim
@@ -12,11 +12,9 @@ type
     unit*: CSSUnit
     auto*: bool
 
-  CSSComputedValues* = array[low(CSSPropertyType)..high(CSSPropertyType), CSSComputedValue]
-
   CSSColor* = tuple[r: uint8, g: uint8, b: uint8, a: uint8]
   
-  CSSComputedValue* = object of RootObj
+  CSSComputedValue* = ref object of RootObj
     t*: CSSPropertyType
     case v*: CSSValueType
     of VALUE_COLOR:
@@ -37,6 +35,8 @@ type
       textdecoration*: CSSTextDecoration
     of VALUE_NONE: discard
 
+  CSSComputedValues* = array[low(CSSPropertyType)..high(CSSPropertyType), CSSComputedValue]
+
   CSSSpecifiedValue* = object of CSSComputedValue
     globalValue: CSSGlobalValueType
 
@@ -59,22 +59,22 @@ const PropertyNames = {
   "text-decoration": PROPERTY_TEXT_DECORATION,
 }.toTable()
 
-const ValueTypes = {
+const ValueTypes = [
   PROPERTY_NONE: VALUE_NONE,
   PROPERTY_ALL: VALUE_NONE,
   PROPERTY_COLOR: VALUE_COLOR,
   PROPERTY_MARGIN: VALUE_LENGTH,
   PROPERTY_MARGIN_TOP: VALUE_LENGTH,
-  PROPERTY_MARGIN_BOTTOM: VALUE_LENGTH,
   PROPERTY_MARGIN_LEFT: VALUE_LENGTH,
   PROPERTY_MARGIN_RIGHT: VALUE_LENGTH,
+  PROPERTY_MARGIN_BOTTOM: VALUE_LENGTH,
   PROPERTY_FONT_STYLE: VALUE_FONT_STYLE,
   PROPERTY_DISPLAY: VALUE_DISPLAY,
   PROPERTY_CONTENT: VALUE_CONTENT,
   PROPERTY_WHITE_SPACE: VALUE_WHITE_SPACE,
   PROPERTY_FONT_WEIGHT: VALUE_INTEGER,
   PROPERTY_TEXT_DECORATION: VALUE_TEXT_DECORATION,
-}.toTable()
+]
 
 const InheritedProperties = {
   PROPERTY_COLOR, PROPERTY_FONT_STYLE, PROPERTY_WHITE_SPACE,
@@ -365,7 +365,7 @@ func getInitialColor*(t: CSSPropertyType): CSSColor =
   else:
     return (r: 0u8, g: 0u8, b: 0u8, a: 255u8)
 
-func getDefault(t: CSSPropertyType): CSSComputedValue =
+func calcDefault(t: CSSPropertyType): CSSComputedValue =
   let v = valueType(t)
   var nv: CSSComputedValue
   case v
@@ -377,6 +377,16 @@ func getDefault(t: CSSPropertyType): CSSComputedValue =
     nv = CSSComputedValue(t: t, v: v)
   return nv
 
+func getDefaultTable(): array[low(CSSPropertyType)..high(CSSPropertyType), CSSComputedValue] =
+  for i in low(result)..high(result):
+    result[i] = calcDefault(i)
+
+let defaultTable = getDefaultTable()
+
+func getDefault(t: CSSPropertyType): CSSComputedValue = {.cast(noSideEffect).}:
+  assert defaultTable[t] != nil
+  return defaultTable[t]
+
 func getComputedValue*(prop: CSSSpecifiedValue, current: CSSComputedValues): CSSComputedValue =
   case prop.globalValue
   of VALUE_INHERIT:
@@ -415,13 +425,13 @@ func getComputedValue*(prop: CSSSpecifiedValue, current: CSSComputedValues): CSS
 func getComputedValue*(d: CSSDeclaration, current: CSSComputedValues): CSSComputedValue =
   return getComputedValue(getSpecifiedValue(d), current)
 
-func inheritProperties*(parent: CSSComputedValues): CSSComputedValues =
+proc inheritProperties*(vals: var CSSComputedValues, parent: CSSComputedValues) =
   for prop in low(CSSPropertyType)..high(CSSPropertyType):
-    if inherited(prop):
-      result[prop] = parent[prop]
-    else:
-      result[prop] = getDefault(prop)
+    if vals[prop] == nil:
+      vals[prop] = getDefault(prop)
+    if inherited(prop) and parent[prop] != nil and vals[prop] == getDefault(prop):
+      vals[prop] = parent[prop]
 
-func rootProperties*(): CSSComputedValues =
+proc rootProperties*(vals: var CSSComputedValues) =
   for prop in low(CSSPropertyType)..high(CSSPropertyType):
-    result[prop] = getDefault(prop)
+    vals[prop] = getDefault(prop)