From 7862eb840ad1964484352cdfa5458edda413fe0d Mon Sep 17 00:00:00 2001 From: bptato Date: Thu, 23 Jan 2025 00:23:45 +0100 Subject: cssvalues: quick hack to make simple calc() expressions work Most calc calls I've seen look something like "calc(100% - 2em)", i.e. just a percentage value offset by a pixel value. As it happens, we also had 24 bits available in CSSLength objects because of padding. Just enough to smuggle in that offset :) Very limited of course, but it does fix layout on a couple of websites. --- src/css/cssparser.nim | 1 + src/css/cssvalues.nim | 37 +++++++++++++++++++++++++++++++++++++ src/css/layout.nim | 4 ++-- 3 files changed, 40 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/css/cssparser.nim b/src/css/cssparser.nim index 621f81c1..d5a4a558 100644 --- a/src/css/cssparser.nim +++ b/src/css/cssparser.nim @@ -67,6 +67,7 @@ type cftVar = "var" cftHsl = "hsl" cftHsla = "hsla" + cftCalc = "calc" CSSFunction* = ref object of CSSComponentValue name*: CSSFunctionType diff --git a/src/css/cssvalues.nim b/src/css/cssvalues.nim index 95a741c5..ba0b4300 100644 --- a/src/css/cssvalues.nim +++ b/src/css/cssvalues.nim @@ -343,6 +343,8 @@ type CSSLength* = object u*: CSSLengthType + # hack to support simple function calls like calc(100% - 10px). + addpx*: int16 num*: float32 CSSVerticalAlign* = object @@ -1167,6 +1169,41 @@ func parseLength*(val: CSSComponentValue; attrs: WindowAttributes; if hasAuto and tok.value.equalsIgnoreCase("auto"): return ok(CSSLengthAuto) else: discard + elif val of CSSFunction: + #TODO obviously this is a horrible solution... + let fun = CSSFunction(val) + if fun.name == cftCalc and allowNegative: + var i = fun.value.skipBlanks(0) + if i >= fun.value.len: + return err() + var length = ?parseLength(fun.value[i], attrs, hasAuto, allowNegative) + i = fun.value.skipBlanks(i + 1) + if i >= fun.value.len or fun.value[i] != cttDelim: + return err() + let dtok = CSSToken(fun.value[i]) + let sign = if dtok.cvalue == '+': + 1f32 + elif dtok.cvalue == '-': + -1f32 + else: + return err() + i = fun.value.skipBlanks(i + 1) + var length2 = ?parseLength(fun.value[i], attrs, hasAuto, allowNegative) + length2.num *= sign + if length2.u == clAuto or fun.value.skipBlanks(i + 1) < fun.value.len: + return err() + if length.u == length2.u: + return ok(CSSLength(u: length.u, num: length.num + length2.num)) + if length2.u == clPerc: + swap(length, length2) + length2.num += float32(length.addpx) + if length2.num notin float32(int16.low)..float32(int16.high): + return err() + return ok(CSSLength( + u: clPerc, + num: length.num, + addpx: int16(length2.num) + )) return err() func cssAbsoluteLength(val: CSSComponentValue; attrs: WindowAttributes): diff --git a/src/css/layout.nim b/src/css/layout.nim index 3cc65578..84b11c48 100644 --- a/src/css/layout.nim +++ b/src/css/layout.nim @@ -123,13 +123,13 @@ func canpx(l: CSSLength; sc: SizeConstraint): bool = func px(l: CSSLength; p: LUnit): LUnit {.inline.} = if l.u != clPerc: return l.num.toLUnit() - return (p.toFloat32() * l.num / 100).toLUnit() + return (p.toFloat32() * l.num / 100 + float32(l.addpx)).toLUnit() func px(l: CSSLength; p: SizeConstraint): LUnit {.inline.} = if l.u != clPerc: return l.num.toLUnit() if p.t == scStretch: - return (p.u.toFloat32() * l.num / 100).toLUnit() + return (p.u.toFloat32() * l.num / 100 + float32(l.addpx)).toLUnit() return 0 func stretchOrMaxContent(l: CSSLength; sc: SizeConstraint): SizeConstraint = -- cgit