From fbfdb9752cd9c9b6faf45cfc162e07486078e7ce Mon Sep 17 00:00:00 2001 From: bptato Date: Mon, 3 Feb 2025 20:50:26 +0100 Subject: layout: eliminate InlineAtom * For boxes, we now use the box in InlineBox. * For images, wrap bmp in an InlineImage type that stores offset + size. * For text, wrap strings into TextRun objects (an offset + a string). (Ideally, TextRuns shouldn't copy at all, but that's a problem for another time.) --- src/css/box.nim | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) (limited to 'src/css/box.nim') diff --git a/src/css/box.nim b/src/css/box.nim index 6ec56c38..f8dca8a0 100644 --- a/src/css/box.nim +++ b/src/css/box.nim @@ -11,19 +11,17 @@ type Size* = array[DimensionType, LUnit] - InlineAtomType* = enum - iatWord, iatInlineBlock, iatImage - - InlineAtom* = ref object + InlineImageState* = object offset*: Offset size*: Size - case t*: InlineAtomType - of iatWord: - str*: string - of iatInlineBlock: - innerbox*: BlockBox - of iatImage: - bmp*: NetworkBitmap + + InlineImage* = ref object + state*: InlineImageState + bmp*: NetworkBitmap + + TextRun* = ref object + offset*: Offset + str*: string BoxLayoutState* = object # offset relative to parent @@ -46,7 +44,7 @@ type InlineBoxState* = object startOffset*: Offset # offset of the first word, for position: absolute areas*: seq[Area] # background that should be painted by box - atoms*: seq[InlineAtom] + runs: seq[TextRun] InlineBoxType* = enum ibtParent, ibtText, ibtNewline, ibtBitmap, ibtBox @@ -64,7 +62,7 @@ type of ibtNewline: discard of ibtBitmap: - bmp*: NetworkBitmap + image*: InlineImage of ibtBox: box*: BlockBox @@ -112,6 +110,13 @@ type inline*: InlineBox children*: seq[BlockBox] +# We store runs in state as a private field, so that we can both check +# if the box type is correct and reset them on relayout by zeroing out +# state. +template runs*(ibox: InlineBox): seq[TextRun] = + assert ibox.t == ibtText + ibox.state.runs + func offset*(x, y: LUnit): Offset = return [dtHorizontal: x, dtVertical: y] -- cgit