diff options
| author | bptato <nincsnevem662@gmail.com> | 2022-05-13 11:56:59 +0200 |
|---|---|---|
| committer | bptato <nincsnevem662@gmail.com> | 2022-05-13 11:56:59 +0200 |
| commit | 7e65add5c43063c1f0fc2ece2f895aef7ff560e2 (patch) | |
| tree | 06c8a869cf7b2688477449156036ebb271925857 /src | |
| parent | 4c959684a4bd4705b4176140092e833dfdab7b7a (diff) | |
| download | chawan-7e65add5c43063c1f0fc2ece2f895aef7ff560e2.tar.gz | |
SpecifiedValue -> ComputedValue
Actually it's neither but ComputedValue is more accurate.
Diffstat (limited to 'src')
| -rw-r--r-- | src/css/cascade.nim | 4 | ||||
| -rw-r--r-- | src/css/values.nim | 64 | ||||
| -rw-r--r-- | src/html/dom.nim | 4 | ||||
| -rw-r--r-- | src/layout/box.nim | 4 | ||||
| -rw-r--r-- | src/layout/engine.nim | 56 |
5 files changed, 66 insertions, 66 deletions
diff --git a/src/css/cascade.nim b/src/css/cascade.nim index abf8ba73..93131637 100644 --- a/src/css/cascade.nim +++ b/src/css/cascade.nim @@ -17,7 +17,7 @@ type DeclarationList* = array[PseudoElem, seq[CSSDeclaration]] proc applyProperty(elem: Element, d: CSSDeclaration, pseudo: PseudoElem) = - var parent: CSSSpecifiedValues + var parent: CSSComputedValues if elem.parentElement != nil: parent = elem.parentElement.css else: @@ -96,7 +96,7 @@ proc applyImportant(ares: var ApplyResult, decls: seq[CSSDeclaration]) = if decl.important: ares.important.add(decl) -proc checkRendered(element: Element, prev: CSSSpecifiedValues, ppseudo: array[PSEUDO_BEFORE..PSEUDO_AFTER, CSSSpecifiedValues]) = +proc checkRendered(element: Element, prev: CSSComputedValues, ppseudo: array[PSEUDO_BEFORE..PSEUDO_AFTER, CSSComputedValues]) = if element.rendered: for p in PSEUDO_BEFORE..PSEUDO_AFTER: if ppseudo[p] != element.pseudo[p] and ppseudo[p] == nil: diff --git a/src/css/values.nim b/src/css/values.nim index 0e8923aa..73299f9e 100644 --- a/src/css/values.nim +++ b/src/css/values.nim @@ -98,7 +98,7 @@ type length*: CSSLength keyword*: CSSVerticalAlign2 - CSSSpecifiedValue* = ref object + CSSComputedValue* = ref object t*: CSSPropertyType case v*: CSSValueType of VALUE_COLOR: @@ -129,7 +129,7 @@ type liststyleposition*: CSSListStylePosition of VALUE_NONE: discard - CSSSpecifiedValues* = ref array[CSSPropertyType, CSSSpecifiedValue] + CSSComputedValues* = ref array[CSSPropertyType, CSSComputedValue] CSSValueError* = object of ValueError @@ -218,13 +218,13 @@ func propertyType(s: string): CSSPropertyType = func valueType(prop: CSSPropertyType): CSSValueType = return ValueTypes[prop] -macro `{}`*(vals: CSSSpecifiedValues, s: string): untyped = +macro `{}`*(vals: CSSComputedValues, s: string): untyped = let t = propertyType($s) let vs = $valueType(t) let s = vs.split(Rune('_'))[1..^1].join("_").tolower() result = newDotExpr(newTree(nnkBracketExpr, vals, newLit(t)), newIdentNode(s)) -macro `{}=`*(vals: CSSSpecifiedValues, s: string, v: typed): untyped = +macro `{}=`*(vals: CSSComputedValues, s: string, v: typed): untyped = let t = propertyType($s) let vs = $valueType(t) let s = vs.split(Rune('_'))[1..^1].join("_").tolower() @@ -272,7 +272,7 @@ func em_to_px(em: float64, term: TermAttributes): int = func ch_to_px(ch: float64, term: TermAttributes): int = int(ch * float64(term.ppc)) -# 水 width, we assume it's 2 chars +# 水 width, we assume it's 2 chars (TODO make width() work at comptime) func ic_to_px(ic: float64, term: TermAttributes): int = int(ic * float64(term.ppc) * 2) @@ -768,7 +768,7 @@ func cssListStylePosition(d: CSSDeclaration): CSSListStylePosition = else: raise newException(CSSValueError, "Invalid list style position") raise newException(CSSValueError, "Invalid list style position") -proc getValueFromDecl(val: CSSSpecifiedValue, d: CSSDeclaration, vtype: CSSValueType, ptype: CSSPropertyType) = +proc getValueFromDecl(val: CSSComputedValue, d: CSSDeclaration, vtype: CSSValueType, ptype: CSSPropertyType) = case vtype of VALUE_COLOR: val.color = cssColor(d) of VALUE_LENGTH: @@ -810,38 +810,38 @@ func getInitialLength(t: CSSPropertyType): CSSLength = else: return CSSLength() -func calcInitial(t: CSSPropertyType): CSSSpecifiedValue = +func calcInitial(t: CSSPropertyType): CSSComputedValue = let v = valueType(t) - var nv: CSSSpecifiedValue + var nv: CSSComputedValue case v of VALUE_COLOR: - nv = CSSSpecifiedValue(t: t, v: v, color: getInitialColor(t)) + nv = CSSComputedValue(t: t, v: v, color: getInitialColor(t)) of VALUE_DISPLAY: - nv = CSSSpecifiedValue(t: t, v: v, display: DISPLAY_INLINE) + nv = CSSComputedValue(t: t, v: v, display: DISPLAY_INLINE) of VALUE_WORD_BREAK: - nv = CSSSpecifiedValue(t: t, v: v, wordbreak: WORD_BREAK_NORMAL) + nv = CSSComputedValue(t: t, v: v, wordbreak: WORD_BREAK_NORMAL) of VALUE_LENGTH: - nv = CSSSpecifiedValue(t: t, v: v, length: getInitialLength(t)) + nv = CSSComputedValue(t: t, v: v, length: getInitialLength(t)) else: - nv = CSSSpecifiedValue(t: t, v: v) + nv = CSSComputedValue(t: t, v: v) return nv -func getInitialTable(): array[CSSPropertyType, CSSSpecifiedValue] = +func getInitialTable(): array[CSSPropertyType, CSSComputedValue] = for i in low(result)..high(result): result[i] = calcInitial(i) let defaultTable = getInitialTable() -func getDefault(t: CSSPropertyType): CSSSpecifiedValue = {.cast(noSideEffect).}: +func getDefault(t: CSSPropertyType): CSSComputedValue = {.cast(noSideEffect).}: assert defaultTable[t] != nil return defaultTable[t] -func getSpecifiedValue(d: CSSDeclaration, parent: CSSSpecifiedValues): tuple[a:CSSSpecifiedValue,b:CSSGlobalValueType] = +func getComputedValue(d: CSSDeclaration, parent: CSSComputedValues): tuple[a:CSSComputedValue,b:CSSGlobalValueType] = let name = $d.name let ptype = propertyType(name) let vtype = valueType(ptype) - var val = CSSSpecifiedValue(t: ptype, v: vtype) + var val = CSSComputedValue(t: ptype, v: vtype) try: val.getValueFromDecl(d, vtype, ptype) except CSSValueError: @@ -849,7 +849,7 @@ func getSpecifiedValue(d: CSSDeclaration, parent: CSSSpecifiedValues): tuple[a:C return (val, cssGlobal(d)) -func equals*(a, b: CSSSpecifiedValue): bool = +func equals*(a, b: CSSComputedValue): bool = if a == b: return true if a == nil or b == nil: @@ -871,7 +871,7 @@ func equals*(a, b: CSSSpecifiedValue): bool = of VALUE_NONE: return true return false -proc applyValue(vals, parent: CSSSpecifiedValues, t: CSSPropertyType, val: CSSSpecifiedValue, global: CSSGlobalValueType) = +proc applyValue(vals, parent: CSSComputedValues, t: CSSPropertyType, val: CSSComputedValue, global: CSSGlobalValueType) = case global of VALUE_INHERIT, VALUE_UNSET: if inherited(t): @@ -885,8 +885,8 @@ proc applyValue(vals, parent: CSSSpecifiedValues, t: CSSPropertyType, val: CSSSp of VALUE_NOGLOBAL: vals[t] = val -proc applyValue*(vals, parent: CSSSpecifiedValues, d: CSSDeclaration) = - let vv = getSpecifiedValue(d, parent) +proc applyValue*(vals, parent: CSSComputedValues, d: CSSDeclaration) = + let vv = getComputedValue(d, parent) let val = vv.a case val.t of PROPERTY_ALL: @@ -895,23 +895,23 @@ proc applyValue*(vals, parent: CSSSpecifiedValues, d: CSSDeclaration) = for t in CSSPropertyType: vals.applyValue(parent, t, nil, global) of PROPERTY_MARGIN: - let left = CSSSpecifiedValue(t: PROPERTY_MARGIN_LEFT, v: VALUE_LENGTH, length: val.length) - let right = CSSSpecifiedValue(t: PROPERTY_MARGIN_RIGHT, v: VALUE_LENGTH, length: val.length) - let top = CSSSpecifiedValue(t: PROPERTY_MARGIN_TOP, v: VALUE_LENGTH, length: val.length) - let bottom = CSSSpecifiedValue(t: PROPERTY_MARGIN_BOTTOM, v: VALUE_LENGTH, length: val.length) + let left = CSSComputedValue(t: PROPERTY_MARGIN_LEFT, v: VALUE_LENGTH, length: val.length) + let right = CSSComputedValue(t: PROPERTY_MARGIN_RIGHT, v: VALUE_LENGTH, length: val.length) + let top = CSSComputedValue(t: PROPERTY_MARGIN_TOP, v: VALUE_LENGTH, length: val.length) + let bottom = CSSComputedValue(t: PROPERTY_MARGIN_BOTTOM, v: VALUE_LENGTH, length: val.length) for val in [left, right, top, bottom]: vals.applyValue(parent, val.t, val, vv.b) of PROPERTY_PADDING: - let left = CSSSpecifiedValue(t: PROPERTY_PADDING_LEFT, v: VALUE_LENGTH, length: val.length) - let right = CSSSpecifiedValue(t: PROPERTY_PADDING_RIGHT, v: VALUE_LENGTH, length: val.length) - let top = CSSSpecifiedValue(t: PROPERTY_PADDING_TOP, v: VALUE_LENGTH, length: val.length) - let bottom = CSSSpecifiedValue(t: PROPERTY_PADDING_BOTTOM, v: VALUE_LENGTH, length: val.length) + let left = CSSComputedValue(t: PROPERTY_PADDING_LEFT, v: VALUE_LENGTH, length: val.length) + let right = CSSComputedValue(t: PROPERTY_PADDING_RIGHT, v: VALUE_LENGTH, length: val.length) + let top = CSSComputedValue(t: PROPERTY_PADDING_TOP, v: VALUE_LENGTH, length: val.length) + let bottom = CSSComputedValue(t: PROPERTY_PADDING_BOTTOM, v: VALUE_LENGTH, length: val.length) for val in [left, right, top, bottom]: vals.applyValue(parent, val.t, val, vv.b) else: vals.applyValue(parent, val.t, vv.a, vv.b) -func inheritProperties*(parent: CSSSpecifiedValues): CSSSpecifiedValues = +func inheritProperties*(parent: CSSComputedValues): CSSComputedValues = new(result) for prop in CSSPropertyType: if inherited(prop) and parent[prop] != nil: @@ -919,12 +919,12 @@ func inheritProperties*(parent: CSSSpecifiedValues): CSSSpecifiedValues = else: result[prop] = getDefault(prop) -func copyProperties*(parent: CSSSpecifiedValues): CSSSpecifiedValues = +func copyProperties*(parent: CSSComputedValues): CSSComputedValues = new(result) for prop in CSSPropertyType: result[prop] = parent[prop] -func rootProperties*(): CSSSpecifiedValues = +func rootProperties*(): CSSComputedValues = new(result) for prop in CSSPropertyType: result[prop] = getDefault(prop) diff --git a/src/html/dom.nim b/src/html/dom.nim index 924fa7a9..d50c1c8e 100644 --- a/src/html/dom.nim +++ b/src/html/dom.nim @@ -78,8 +78,8 @@ type id*: string classList*: seq[string] attributes*: Table[string, string] - css*: CSSSpecifiedValues - pseudo*: array[PSEUDO_BEFORE..PSEUDO_AFTER, CSSSpecifiedValues] + css*: CSSComputedValues + pseudo*: array[PSEUDO_BEFORE..PSEUDO_AFTER, CSSComputedValues] hover*: bool cssapplied*: bool rendered*: bool diff --git a/src/layout/box.nim b/src/layout/box.nim index 0a1a4cc0..9ff94f53 100644 --- a/src/layout/box.nim +++ b/src/layout/box.nim @@ -12,7 +12,7 @@ type BoxBuilder* = ref object of RootObj children*: seq[BoxBuilder] inlinelayout*: bool - specified*: CSSSpecifiedValues + specified*: CSSComputedValues node*: Node element*: Element @@ -77,7 +77,7 @@ type BlockContext* = ref object inline*: InlineContext nested*: seq[BlockContext] - specified*: CSSSpecifiedValues + specified*: CSSComputedValues viewport*: Viewport relx*: int rely*: int diff --git a/src/layout/engine.nim b/src/layout/engine.nim index a2fb517a..3ca1c114 100644 --- a/src/layout/engine.nim +++ b/src/layout/engine.nim @@ -32,9 +32,9 @@ type InlineState = object node: Node word: InlineWord maxwidth: int - specified: CSSSpecifiedValues + specified: CSSComputedValues -func whitespacepre(specified: CSSSpecifiedValues): bool {.inline.} = +func whitespacepre(specified: CSSComputedValues): bool {.inline.} = specified{"white-space"} in {WHITESPACE_PRE, WHITESPACE_PRE_WRAP} func cellwidth(viewport: Viewport): int {.inline.} = @@ -50,7 +50,7 @@ func cellheight(ictx: InlineContext): int {.inline.} = ictx.viewport.cellheight # Whitespace between words -func computeShift(ictx: InlineContext, specified: CSSSpecifiedValues): int = +func computeShift(ictx: InlineContext, specified: CSSComputedValues): int = if ictx.whitespacenum > 0: if ictx.thisrow.atoms.len > 0 or specified.whitespacepre: let spacing = specified{"word-spacing"} @@ -60,7 +60,7 @@ func computeShift(ictx: InlineContext, specified: CSSSpecifiedValues): int = return spacing.px(ictx.viewport) * ictx.whitespacenum return 0 -func computeLineHeight(viewport: Viewport, specified: CSSSpecifiedValues): int = +func computeLineHeight(viewport: Viewport, specified: CSSComputedValues): int = if specified{"line-height"}.auto: return viewport.cellheight return specified{"line-height"}.px(viewport, viewport.cellheight) @@ -78,7 +78,7 @@ proc newWord(state: var InlineState) = state.ictx.format = format state.word = word -proc horizontalAlignRow(ictx: InlineContext, row: InlineRow, specified: CSSSpecifiedValues, maxwidth: int, last = false) = +proc horizontalAlignRow(ictx: InlineContext, row: InlineRow, specified: CSSComputedValues, maxwidth: int, last = false) = let maxwidth = if ictx.shrink: ictx.maxwidth else: @@ -165,13 +165,13 @@ proc addSpacing(row: InlineRow, width, height: int, format: ComputedFormat) {.in row.width += spacing.width row.atoms.add(spacing) -proc flushWhitespace(ictx: InlineContext, specified: CSSSpecifiedValues) = +proc flushWhitespace(ictx: InlineContext, specified: CSSComputedValues) = let shift = ictx.computeShift(specified) ictx.whitespacenum = 0 if shift > 0: ictx.thisrow.addSpacing(shift, ictx.cellheight, ictx.format) -proc finishRow(ictx: InlineContext, specified: CSSSpecifiedValues, maxwidth: int, force = false) = +proc finishRow(ictx: InlineContext, specified: CSSComputedValues, maxwidth: int, force = false) = if ictx.thisrow.atoms.len != 0 or force: ictx.flushWhitespace(specified) ictx.verticalAlignRow() @@ -182,12 +182,12 @@ proc finishRow(ictx: InlineContext, specified: CSSSpecifiedValues, maxwidth: int ictx.maxwidth = max(ictx.maxwidth, oldrow.width) ictx.thisrow = InlineRow(rely: oldrow.rely + oldrow.height) -proc finish(ictx: InlineContext, specified: CSSSpecifiedValues, maxwidth: int) = +proc finish(ictx: InlineContext, specified: CSSComputedValues, maxwidth: int) = ictx.finishRow(specified, maxwidth) for row in ictx.rows: ictx.horizontalAlignRow(row, specified, maxwidth, row == ictx.rows[^1]) -proc addAtom(ictx: InlineContext, atom: InlineAtom, maxwidth: int, specified: CSSSpecifiedValues) = +proc addAtom(ictx: InlineContext, atom: InlineAtom, maxwidth: int, specified: CSSComputedValues) = var shift = ictx.computeShift(specified) ictx.whitespacenum = 0 # Line wrapping @@ -221,7 +221,7 @@ proc addWord(state: var InlineState) = state.newWord() # Start a new line, even if the previous one is empty -proc flushLine(ictx: InlineContext, specified: CSSSpecifiedValues, maxwidth: int) = +proc flushLine(ictx: InlineContext, specified: CSSComputedValues, maxwidth: int) = ictx.thisrow.lineheight = computeLineHeight(ictx.viewport, specified) ictx.finishRow(specified, maxwidth, true) @@ -252,7 +252,7 @@ proc processWhitespace(state: var InlineState, c: char) = else: inc state.ictx.whitespacenum -proc renderText*(ictx: InlineContext, str: string, maxwidth: int, specified: CSSSpecifiedValues, node: Node) = +proc renderText*(ictx: InlineContext, str: string, maxwidth: int, specified: CSSComputedValues, node: Node) = var state: InlineState state.specified = specified state.ictx = ictx @@ -348,9 +348,9 @@ proc newInlineContext(bctx: BlockContext): InlineContext = result.shrink = bctx.shrink bctx.inline = result -# Blocks' positions do not have to be arranged if buildBlocks is called with +# Blocks' positions do not have to be positiond if buildBlocks is called with # children, whence the separate procedure. -proc arrangeBlocks(bctx: BlockContext, selfcontained: bool) = +proc positionBlocks(bctx: BlockContext, selfcontained: bool) = var y = 0 var x = 0 var margin_todo = 0 @@ -416,7 +416,7 @@ proc arrangeBlocks(bctx: BlockContext, selfcontained: bool) = bctx.width += bctx.padding_left bctx.width += bctx.padding_right -proc arrangeInlines(bctx: BlockContext, selfcontained: bool) = +proc positionInlines(bctx: BlockContext, selfcontained: bool) = bctx.margin_top = bctx.specified{"margin-top"}.px(bctx.viewport, bctx.compwidth) bctx.margin_bottom = bctx.specified{"margin-bottom"}.px(bctx.viewport, bctx.compwidth) @@ -551,11 +551,11 @@ proc buildBlock(box: BlockBoxBuilder, parent: BlockContext, selfcontained = fals if box.inlinelayout: # Builder only contains inline boxes. result.buildInlines(box.children) - result.arrangeInlines(selfcontained) + result.positionInlines(selfcontained) else: # Builder only contains block boxes. result.buildBlocks(box.children, box.node) - result.arrangeBlocks(selfcontained) + result.positionBlocks(selfcontained) # Build a block box whose parent is the viewport, based on a builder. proc buildBlock(box: BlockBoxBuilder, viewport: Viewport, selfcontained = false): BlockContext = @@ -563,34 +563,34 @@ proc buildBlock(box: BlockBoxBuilder, viewport: Viewport, selfcontained = false) if box.inlinelayout: # Builder only contains inline boxes. result.buildInlines(box.children) - result.arrangeInlines(selfcontained) + result.positionInlines(selfcontained) else: # Builder only contains block boxes. result.buildBlocks(box.children, box.node) - result.arrangeBlocks(selfcontained) + result.positionBlocks(selfcontained) # Generation phase -proc getInlineBlockBox(specified: CSSSpecifiedValues): InlineBlockBoxBuilder = +proc getInlineBlockBox(specified: CSSComputedValues): InlineBlockBoxBuilder = assert specified{"display"} == DISPLAY_INLINE_BLOCK new(result) result.specified = specified # Returns a block box, disregarding the specified value of display -proc getBlockBox(specified: CSSSpecifiedValues): BlockBoxBuilder = +proc getBlockBox(specified: CSSComputedValues): BlockBoxBuilder = new(result) result.specified = specified.copyProperties() #WARNING yes there is a {}= macro but that modifies the specified value # reference itself and those are copied across arrays... #TODO figure something out here - result.specified[PROPERTY_DISPLAY] = CSSSpecifiedValue(t: PROPERTY_DISPLAY, v: VALUE_DISPLAY, display: DISPLAY_BLOCK) + result.specified[PROPERTY_DISPLAY] = CSSComputedValue(t: PROPERTY_DISPLAY, v: VALUE_DISPLAY, display: DISPLAY_BLOCK) proc getTextBox(box: BoxBuilder): InlineBoxBuilder = new(result) result.inlinelayout = true result.specified = box.specified.inheritProperties() -proc getTextBox(specified: CSSSpecifiedValues): InlineBoxBuilder = +proc getTextBox(specified: CSSComputedValues): InlineBoxBuilder = new(result) result.inlinelayout = true result.specified = specified.inheritProperties() @@ -607,21 +607,21 @@ func getInputBox(parent: BoxBuilder, input: HTMLInputElement, viewport: Viewport textbox.text.add(input.inputString()) return textbox -func getInputBox(specified: CSSSpecifiedValues, input: HTMLInputElement, viewport: Viewport): InlineBoxBuilder = +func getInputBox(specified: CSSComputedValues, input: HTMLInputElement, viewport: Viewport): InlineBoxBuilder = let textbox = specified.getTextBox() textbox.node = input textbox.text.add(input.inputString()) return textbox # Don't generate empty anonymous inline blocks between block boxes -func canGenerateAnonymousInline(blockgroup: seq[BoxBuilder], specified: CSSSpecifiedValues, text: Text): bool = +func canGenerateAnonymousInline(blockgroup: seq[BoxBuilder], specified: CSSComputedValues, text: Text): bool = return blockgroup.len > 0 and blockgroup[^1].specified{"display"} == DISPLAY_INLINE or specified{"white-space"} in {WHITESPACE_PRE_LINE, WHITESPACE_PRE, WHITESPACE_PRE_WRAP} or not text.data.onlyWhitespace() proc generateBlockBox(elem: Element, viewport: Viewport): BlockBoxBuilder -template flush_block_group3(specified: CSSSpecifiedValues) = +template flush_block_group3(specified: CSSComputedValues) = if blockgroup.len > 0: let bbox = getBlockBox(specified.inheritProperties()) bbox.inlinelayout = true @@ -659,7 +659,7 @@ template generate_from_elem(child: Element) = else: discard #TODO -proc generateInlinePseudoBox(box: BlockBoxBuilder, specified: CSSSpecifiedValues, blockgroup: var seq[BoxBuilder], viewport: Viewport) = +proc generateInlinePseudoBox(box: BlockBoxBuilder, specified: CSSComputedValues, blockgroup: var seq[BoxBuilder], viewport: Viewport) = var ibox: InlineBoxBuilder = nil if specified{"content"}.len > 0: @@ -668,7 +668,7 @@ proc generateInlinePseudoBox(box: BlockBoxBuilder, specified: CSSSpecifiedValues flush_ibox -proc generateBlockPseudoBox(specified: CSSSpecifiedValues, viewport: Viewport): BlockBoxBuilder = +proc generateBlockPseudoBox(specified: CSSComputedValues, viewport: Viewport): BlockBoxBuilder = let box = getBlockBox(specified) var blockgroup: seq[BoxBuilder] var ibox: InlineBoxBuilder = nil @@ -681,7 +681,7 @@ proc generateBlockPseudoBox(specified: CSSSpecifiedValues, viewport: Viewport): return box -template generate_pseudo(specified: CSSSpecifiedValues) = +template generate_pseudo(specified: CSSComputedValues) = case specified{"display"} of DISPLAY_BLOCK, DISPLAY_LIST_ITEM: flush_block_group3(elem.css) |