From 1c6ce0892f5ca63682fa2c4dfe9d6a4e81558eb4 Mon Sep 17 00:00:00 2001 From: bptato Date: Mon, 3 Jul 2023 23:50:50 +0200 Subject: Use LayoutUnit in layout Reduces ugly rendering caused by rounding errors. --- src/css/cascade.nim | 9 +++++---- src/css/values.nim | 43 +++++++++++++++++++++++-------------------- 2 files changed, 28 insertions(+), 24 deletions(-) (limited to 'src/css') diff --git a/src/css/cascade.nim b/src/css/cascade.nim index 2582763e..7996f96e 100644 --- a/src/css/cascade.nim +++ b/src/css/cascade.nim @@ -12,6 +12,7 @@ import css/stylednode import css/values import html/dom import html/tags +import layout/layoutunit import types/color type @@ -33,12 +34,12 @@ func applies(feature: MediaFeature, window: Window): bool = of FEATURE_PREFERS_COLOR_SCHEME: return feature.b of FEATURE_WIDTH: - let a = px(feature.lengthrange.a, window.attrs, 0) - let b = px(feature.lengthrange.b, window.attrs, 0) + let a = toInt(px(feature.lengthrange.a, window.attrs, 0)) + let b = toInt(px(feature.lengthrange.b, window.attrs, 0)) return window.attrs.ppc * window.attrs.width in a .. b of FEATURE_HEIGHT: - let a = px(feature.lengthrange.a, window.attrs, 0) - let b = px(feature.lengthrange.b, window.attrs, 0) + let a = toInt(px(feature.lengthrange.a, window.attrs, 0)) + let b = toInt(px(feature.lengthrange.b, window.attrs, 0)) return window.attrs.ppl * window.attrs.height in a .. b func applies(mq: MediaQuery, window: Window): bool = diff --git a/src/css/values.nim b/src/css/values.nim index 6d664a25..e988333d 100644 --- a/src/css/values.nim +++ b/src/css/values.nim @@ -8,6 +8,7 @@ import css/cssparser import css/selectorparser import img/bitmap import io/window +import layout/layoutunit import types/color import utils/opt import utils/twtstr @@ -387,37 +388,39 @@ macro `{}=`*(vals: CSSComputedValues, s: string, val: typed) = func inherited(t: CSSPropertyType): bool = return InheritedArray[t] -func em_to_px(em: float64, window: WindowAttributes): int = - int(em * float64(window.ppl)) +func em_to_px(em: float64, window: WindowAttributes): LayoutUnit = + em * toLayoutUnit(window.ppl) -func ch_to_px(ch: float64, window: WindowAttributes): int = - int(ch * float64(window.ppc)) +func ch_to_px(ch: float64, window: WindowAttributes): LayoutUnit = + ch * toLayoutUnit(window.ppc) # 水 width, we assume it's 2 chars -func ic_to_px(ic: float64, window: WindowAttributes): int = - int(ic * float64(window.ppc) * 2) +func ic_to_px(ic: float64, window: WindowAttributes): LayoutUnit = + ic * toLayoutUnit(window.ppc) * 2 # x-letter height, we assume it's em/2 -func ex_to_px(ex: float64, window: WindowAttributes): int = - int(ex * float64(window.ppc) / 2) +func ex_to_px(ex: float64, window: WindowAttributes): LayoutUnit = + ex * toLayoutUnit(window.ppc) / 2 -func px*(l: CSSLength, window: WindowAttributes, p: int): int {.inline.} = +func px*(l: CSSLength, window: WindowAttributes, p: LayoutUnit): LayoutUnit {.inline.} = case l.unit of UNIT_EM, UNIT_REM: em_to_px(l.num, window) of UNIT_CH: ch_to_px(l.num, window) of UNIT_IC: ic_to_px(l.num, window) of UNIT_EX: ex_to_px(l.num, window) - of UNIT_PERC: int(p / 100 * l.num) - of UNIT_PX: int(l.num) - of UNIT_CM: int(l.num * 37.8) - of UNIT_MM: int(l.num * 3.78) - of UNIT_IN: int(l.num * 96) - of UNIT_PC: int(l.num * 96 / 6) - of UNIT_PT: int(l.num * 96 / 72) - of UNIT_VW: int(window.width_px / 100 * l.num) - of UNIT_VH: int(window.height_px / 100 * l.num) - of UNIT_VMIN: int(min(window.width_px, window.width_px) / 100 * l.num) - of UNIT_VMAX: int(max(window.width_px, window.width_px) / 100 * l.num) + of UNIT_PERC: p * l.num / 100 + of UNIT_PX: toLayoutUnit(l.num) + of UNIT_CM: toLayoutUnit(l.num * 37.8) + of UNIT_MM: toLayoutUnit(l.num * 3.78) + of UNIT_IN: toLayoutUnit(l.num * 96) + of UNIT_PC: toLayoutUnit(l.num * 16) + of UNIT_PT: toLayoutUnit(l.num * 4 / 3) + of UNIT_VW: toLayoutUnit(float64(window.width_px) * l.num / 100) + of UNIT_VH: toLayoutUnit(float64(window.height_px) * l.num / 100) + of UNIT_VMIN: + toLayoutUnit(min(window.width_px, window.width_px) / 100 * l.num) + of UNIT_VMAX: + toLayoutUnit(max(window.width_px, window.width_px) / 100 * l.num) func listMarker*(t: CSSListStyleType, i: int): string = case t -- cgit