about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2022-01-23 12:55:41 +0100
committerbptato <nincsnevem662@gmail.com>2022-01-23 12:55:41 +0100
commit0b19885b33c53e2250a3a91326e5f146ccfa1492 (patch)
treeb97c011ec31561f43b60df0abc89e2dcaf19d5ed
parent7a2cda0e992da40684c193791b5865bb643df95e (diff)
downloadchawan-0b19885b33c53e2250a3a91326e5f146ccfa1492.tar.gz
Consider cell ratio when calculating lengths
-rw-r--r--src/css/values.nim19
-rw-r--r--src/io/term.nim7
-rw-r--r--src/layout/engine.nim2
3 files changed, 17 insertions, 11 deletions
diff --git a/src/css/values.nim b/src/css/values.nim
index 93f40cd1..436ef95e 100644
--- a/src/css/values.nim
+++ b/src/css/values.nim
@@ -6,10 +6,11 @@ import options
 import macros
 import strutils
 
-import utils/twtstr
 import css/parser
 import css/selparser
+import io/term
 import types/color
+import utils/twtstr
 
 export selparser.PseudoElem
 
@@ -200,23 +201,25 @@ macro `{}=`*(vals: CSSSpecifiedValues, s: string, v: typed): untyped =
 func inherited(t: CSSPropertyType): bool =
   return InheritedArray[t]
 
-func px(n: float64, d: int): int =
+func px(n: float64, d: int): int {.inline.} =
   return int(n / float(d))
 
-func cells*(l: CSSLength, d, w, h: int, p: Option[int], o: bool): int =
+func cells*(l: CSSLength, d: int, term: TermAttributes, p: Option[int], o: bool): int =
+  let w = term.width_px
+  let h = term.height_px
   case l.unit
   of UNIT_EM, UNIT_REM:
-    if o: int(l.num * 2) #horizontal
+    if o: int(l.num * term.cell_ratio) #horizontal
     else: int(l.num) #vertical
   of UNIT_CH:
     if o: int(l.num) #horizontal
-    else: int(l.num / 2) #vertical
+    else: int(l.num / term.cell_ratio) #vertical
   of UNIT_IC:
-    if o: int(l.num * 2) #horizontal
+    if o: int(l.num * term.cell_ratio) #horizontal
     else: int(l.num) #vertical
-  of UNIT_EX:
+  of UNIT_EX: # x-letter height, we assume it's em/2
     if o: int(l.num / 2) #horizontal
-    else: int(l.num / 4) #vertical
+    else: int(l.num / term.cell_ratio / 2) #vertical
   of UNIT_PERC: int(p.get / 100 * l.num)
   of UNIT_PX: px(l.num, d)
   of UNIT_CM: px(l.num * 37.8, d)
diff --git a/src/io/term.nim b/src/io/term.nim
index 2e859d66..d1cacf7c 100644
--- a/src/io/term.nim
+++ b/src/io/term.nim
@@ -6,8 +6,9 @@ type
   TermAttributes* = object
     width*: int
     height*: int
-    ppc*: int
-    ppl*: int
+    ppc*: int # cell width
+    ppl*: int # cell height
+    cell_ratio*: float64 # ppl / ppc
     width_px*: int
     height_px*: int
 
@@ -22,6 +23,7 @@ proc getTermAttributes*(): TermAttributes =
         result.height_px = int(win.ws_ypixel)
         result.ppc = int(win.ws_xpixel) div int(win.ws_col)
         result.ppl = int(win.ws_ypixel) div int(win.ws_row)
+        result.cell_ratio = result.ppl / result.ppc
         return
   #fail
   result.width = terminalWidth() - 1
@@ -30,5 +32,6 @@ proc getTermAttributes*(): TermAttributes =
     result.height = 24
   result.ppc = 9
   result.ppl = 18
+  result.cell_ratio = result.ppl / result.ppc
   result.width_px = result.ppc * result.width
   result.height_px = result.ppl * result.height
diff --git a/src/layout/engine.nim b/src/layout/engine.nim
index cfd2a1dd..3f16c8ac 100644
--- a/src/layout/engine.nim
+++ b/src/layout/engine.nim
@@ -9,7 +9,7 @@ import utils/twtstr
 import io/term
 
 func cells_in(l: CSSLength, state: Viewport, d: int, p: Option[int], o: bool): int =
-  return cells(l, d, state.term.width_px, state.term.height_px, p, o)
+  return cells(l, d, state.term, p, o)
 
 func cells_w(l: CSSLength, state: Viewport, p: int): int =
   return l.cells_in(state, state.term.ppc, p.some, true)