From 8140f97c25e60318df9d2b09b79b9d07ade6380c Mon Sep 17 00:00:00 2001 From: bptato Date: Tue, 10 Dec 2024 21:13:21 +0100 Subject: layout: small cleanup --- src/css/layout.nim | 120 ++++++++++++++++++++++++++--------------------------- 1 file changed, 60 insertions(+), 60 deletions(-) (limited to 'src/css') diff --git a/src/css/layout.nim b/src/css/layout.nim index a3869662..8be6dc9a 100644 --- a/src/css/layout.nim +++ b/src/css/layout.nim @@ -934,7 +934,7 @@ proc resolveContentWidth(sizes: var ResolvedSizes; widthpx: LayoutUnit; sizes.margin[dtHorizontal].start = underflow div 2 sizes.margin[dtHorizontal].send = underflow div 2 -proc resolveMargins(availableWidth: SizeConstraint; lctx: LayoutContext; +proc resolveMargins(lctx: LayoutContext; availableWidth: SizeConstraint; computed: CSSComputedValues): RelativeRect = # Note: we use availableWidth for percentage resolution intentionally. return [ @@ -948,7 +948,7 @@ proc resolveMargins(availableWidth: SizeConstraint; lctx: LayoutContext; ) ] -proc resolvePadding(availableWidth: SizeConstraint; lctx: LayoutContext; +proc resolvePadding(lctx: LayoutContext; availableWidth: SizeConstraint; computed: CSSComputedValues): RelativeRect = # Note: we use availableWidth for percentage resolution intentionally. return [ @@ -1007,55 +1007,6 @@ func resolveBounds(lctx: LayoutContext; space: AvailableSpace; padding: Size; res.a[dtVertical].send = px return res -proc resolveBlockWidth(sizes: var ResolvedSizes; parentWidth: SizeConstraint; - inlinePadding: LayoutUnit; computed: CSSComputedValues; - lctx: LayoutContext) = - let width = computed{"width"} - var widthpx: LayoutUnit = 0 - if width.canpx(parentWidth): - widthpx = width.spx(lctx, parentWidth, computed, inlinePadding) - sizes.space.w = stretch(widthpx) - sizes.bounds.minClamp[dtHorizontal] = widthpx - sizes.resolveContentWidth(widthpx, parentWidth, computed, width.u == cuAuto) - if sizes.space.w.isDefinite() and sizes.maxWidth < sizes.space.w.u or - sizes.maxWidth < LayoutUnit.high and sizes.space.w.t == scMaxContent: - if sizes.space.w.t == scStretch: - # available width would stretch over max-width - sizes.space.w = stretch(sizes.maxWidth) - else: # scFitContent - # available width could be higher than max-width (but not necessarily) - sizes.space.w = fitContent(sizes.maxWidth) - sizes.resolveContentWidth(sizes.maxWidth, parentWidth, computed) - if sizes.space.w.isDefinite() and sizes.minWidth > sizes.space.w.u or - sizes.minWidth > 0 and sizes.space.w.t == scMinContent: - # two cases: - # * available width is stretched under min-width. in this case, - # stretch to min-width instead. - # * available width is fit under min-width. in this case, stretch to - # min-width as well (as we must satisfy min-width >= width). - sizes.space.w = stretch(sizes.minWidth) - sizes.resolveContentWidth(sizes.minWidth, parentWidth, computed) - -proc resolveBlockHeight(sizes: var ResolvedSizes; parentHeight: SizeConstraint; - blockPadding: LayoutUnit; computed: CSSComputedValues; - lctx: LayoutContext) = - let height = computed{"height"} - if height.canpx(parentHeight): - let heightpx = height.spx(lctx, parentHeight, computed, blockPadding) - sizes.space.h = stretch(heightpx) - sizes.bounds.minClamp[dtVertical] = heightpx - if sizes.space.h.isDefinite() and sizes.maxHeight < sizes.space.h.u or - sizes.maxHeight < LayoutUnit.high and sizes.space.h.t == scMaxContent: - # same reasoning as for width. - if sizes.space.h.t == scStretch: - sizes.space.h = stretch(sizes.maxHeight) - else: # scFitContent - sizes.space.h = fitContent(sizes.maxHeight) - if sizes.space.h.isDefinite() and sizes.minHeight > sizes.space.h.u or - sizes.minHeight > 0 and sizes.space.h.t == scMinContent: - # same reasoning as for width. - sizes.space.h = stretch(sizes.minHeight) - const CvalSizeMap = [dtHorizontal: cptWidth, dtVertical: cptHeight] proc resolveAbsoluteWidth(sizes: var ResolvedSizes; size: Size; @@ -1097,8 +1048,8 @@ proc resolveAbsoluteSizes(lctx: LayoutContext; size: Size; positioned: var RelativeRect; computed: CSSComputedValues): ResolvedSizes = positioned = lctx.resolvePositioned(size, computed) var sizes = ResolvedSizes( - margin: resolveMargins(stretch(size.w), lctx, computed), - padding: resolvePadding(stretch(size.w), lctx, computed), + margin: lctx.resolveMargins(stretch(size.w), computed), + padding: lctx.resolvePadding(stretch(size.w), computed), bounds: DefaultBounds ) sizes.resolveAbsoluteWidth(size, positioned, computed, lctx) @@ -1108,10 +1059,10 @@ proc resolveAbsoluteSizes(lctx: LayoutContext; size: Size; # Calculate and resolve available width & height for floating boxes. proc resolveFloatSizes(lctx: LayoutContext; space: AvailableSpace; computed: CSSComputedValues): ResolvedSizes = - let padding = resolvePadding(space.w, lctx, computed) + let padding = lctx.resolvePadding(space.w, computed) let paddingSum = padding.sum() var sizes = ResolvedSizes( - margin: resolveMargins(space.w, lctx, computed), + margin: lctx.resolveMargins(space.w, computed), padding: padding, space: space, bounds: lctx.resolveBounds(space, paddingSum, computed) @@ -1129,10 +1080,10 @@ proc resolveFloatSizes(lctx: LayoutContext; space: AvailableSpace; proc resolveFlexItemSizes(lctx: LayoutContext; space: AvailableSpace; dim: DimensionType; computed: CSSComputedValues): ResolvedSizes = - let padding = resolvePadding(space.w, lctx, computed) + let padding = lctx.resolvePadding(space.w, computed) let paddingSum = padding.sum() var sizes = ResolvedSizes( - margin: resolveMargins(space.w, lctx, computed), + margin: lctx.resolveMargins(space.w, computed), padding: padding, space: space, bounds: lctx.resolveBounds(space, paddingSum, computed) @@ -1171,12 +1122,61 @@ proc resolveFlexItemSizes(lctx: LayoutContext; space: AvailableSpace; sizes.space[odim] = fitContent(sizes.bounds.a[odim].max()) return sizes +proc resolveBlockWidth(sizes: var ResolvedSizes; parentWidth: SizeConstraint; + inlinePadding: LayoutUnit; computed: CSSComputedValues; + lctx: LayoutContext) = + let width = computed{"width"} + var widthpx: LayoutUnit = 0 + if width.canpx(parentWidth): + widthpx = width.spx(lctx, parentWidth, computed, inlinePadding) + sizes.space.w = stretch(widthpx) + sizes.bounds.minClamp[dtHorizontal] = widthpx + sizes.resolveContentWidth(widthpx, parentWidth, computed, width.u == cuAuto) + if sizes.space.w.isDefinite() and sizes.maxWidth < sizes.space.w.u or + sizes.maxWidth < LayoutUnit.high and sizes.space.w.t == scMaxContent: + if sizes.space.w.t == scStretch: + # available width would stretch over max-width + sizes.space.w = stretch(sizes.maxWidth) + else: # scFitContent + # available width could be higher than max-width (but not necessarily) + sizes.space.w = fitContent(sizes.maxWidth) + sizes.resolveContentWidth(sizes.maxWidth, parentWidth, computed) + if sizes.space.w.isDefinite() and sizes.minWidth > sizes.space.w.u or + sizes.minWidth > 0 and sizes.space.w.t == scMinContent: + # two cases: + # * available width is stretched under min-width. in this case, + # stretch to min-width instead. + # * available width is fit under min-width. in this case, stretch to + # min-width as well (as we must satisfy min-width >= width). + sizes.space.w = stretch(sizes.minWidth) + sizes.resolveContentWidth(sizes.minWidth, parentWidth, computed) + +proc resolveBlockHeight(sizes: var ResolvedSizes; parentHeight: SizeConstraint; + blockPadding: LayoutUnit; computed: CSSComputedValues; + lctx: LayoutContext) = + let height = computed{"height"} + if height.canpx(parentHeight): + let heightpx = height.spx(lctx, parentHeight, computed, blockPadding) + sizes.space.h = stretch(heightpx) + sizes.bounds.minClamp[dtVertical] = heightpx + if sizes.space.h.isDefinite() and sizes.maxHeight < sizes.space.h.u or + sizes.maxHeight < LayoutUnit.high and sizes.space.h.t == scMaxContent: + # same reasoning as for width. + if sizes.space.h.t == scStretch: + sizes.space.h = stretch(sizes.maxHeight) + else: # scFitContent + sizes.space.h = fitContent(sizes.maxHeight) + if sizes.space.h.isDefinite() and sizes.minHeight > sizes.space.h.u or + sizes.minHeight > 0 and sizes.space.h.t == scMinContent: + # same reasoning as for width. + sizes.space.h = stretch(sizes.minHeight) + proc resolveBlockSizes(lctx: LayoutContext; space: AvailableSpace; computed: CSSComputedValues): ResolvedSizes = - let padding = resolvePadding(space.w, lctx, computed) + let padding = lctx.resolvePadding(space.w, computed) let paddingSum = padding.sum() var sizes = ResolvedSizes( - margin: resolveMargins(space.w, lctx, computed), + margin: lctx.resolveMargins(space.w, computed), padding: padding, space: space, bounds: lctx.resolveBounds(space, paddingSum, computed) @@ -1795,7 +1795,7 @@ type proc layoutTableCell(lctx: LayoutContext; box: BlockBox; space: AvailableSpace) = var sizes = ResolvedSizes( - padding: resolvePadding(space.w, lctx, box.computed), + padding: lctx.resolvePadding(space.w, box.computed), space: space, bounds: DefaultBounds ) -- cgit