diff options
| author | bptato <nincsnevem662@gmail.com> | 2022-12-11 21:24:01 +0100 |
|---|---|---|
| committer | bptato <nincsnevem662@gmail.com> | 2022-12-11 21:47:20 +0100 |
| commit | 339a167f6bf8f4bd8c22c1bdfad645aad0732371 (patch) | |
| tree | 014e7a5ff87ef34fd9869fdbd579d1edb91a0367 /src | |
| parent | fca4ef9b8fabebb3a9173293908ee9b56acc9cc5 (diff) | |
| download | chawan-339a167f6bf8f4bd8c22c1bdfad645aad0732371.tar.gz | |
Add border-collapse, border-spacing; fix fd sending hack
Note that they don't actually work correctly because of rounding errors.
The fd sending hack now emits C directly. Let's hope this actually
works.
Diffstat (limited to 'src')
| -rw-r--r-- | src/css/values.nim | 68 | ||||
| -rw-r--r-- | src/ips/socketstream.nim | 7 | ||||
| -rw-r--r-- | src/layout/box.nim | 3 | ||||
| -rw-r--r-- | src/layout/engine.nim | 31 |
4 files changed, 90 insertions, 19 deletions
diff --git a/src/css/values.nim b/src/css/values.nim index 5bf9c2d1..cfd1fa89 100644 --- a/src/css/values.nim +++ b/src/css/values.nim @@ -31,14 +31,15 @@ type PROPERTY_PADDING_BOTTOM, PROPERTY_WORD_SPACING, PROPERTY_VERTICAL_ALIGN, PROPERTY_LINE_HEIGHT, PROPERTY_TEXT_ALIGN, PROPERTY_LIST_STYLE_POSITION, PROPERTY_BACKGROUND_COLOR, PROPERTY_POSITION, PROPERTY_LEFT, - PROPERTY_RIGHT, PROPERTY_TOP, PROPERTY_BOTTOM, PROPERTY_CAPTION_SIDE + PROPERTY_RIGHT, PROPERTY_TOP, PROPERTY_BOTTOM, PROPERTY_CAPTION_SIDE, + PROPERTY_BORDER_SPACING, PROPERTY_BORDER_COLLAPSE CSSValueType* = enum VALUE_NONE, VALUE_LENGTH, VALUE_COLOR, VALUE_CONTENT, VALUE_DISPLAY, VALUE_FONT_STYLE, VALUE_WHITE_SPACE, VALUE_INTEGER, VALUE_TEXT_DECORATION, VALUE_WORD_BREAK, VALUE_LIST_STYLE_TYPE, VALUE_VERTICAL_ALIGN, VALUE_TEXT_ALIGN, VALUE_LIST_STYLE_POSITION, VALUE_POSITION, - VALUE_CAPTION_SIDE + VALUE_CAPTION_SIDE, VALUE_LENGTH2, VALUE_BORDER_COLLAPSE CSSGlobalValueType* = enum VALUE_NOGLOBAL, VALUE_INITIAL, VALUE_INHERIT, VALUE_REVERT, VALUE_UNSET @@ -92,6 +93,9 @@ type CAPTION_SIDE_RIGHT, CAPTION_SIDE_BLOCK_START, CAPTION_SIDE_BLOCK_END, CAPTION_SIDE_INLINE_START, CAPTION_SIDE_INLINE_END + CSSBorderCollapse* = enum + BORDER_COLLAPSE_SEPARATE, BORDER_COLLAPSE_COLLAPSE + const RowGroupBox* = {DISPLAY_TABLE_ROW_GROUP, DISPLAY_TABLE_HEADER_GROUP, DISPLAY_TABLE_FOOTER_GROUP} const ProperTableChild* = {DISPLAY_TABLE_ROW, DISPLAY_TABLE_COLUMN, @@ -146,6 +150,10 @@ type position*: CSSPosition of VALUE_CAPTION_SIDE: captionside*: CSSCaptionSide + of VALUE_LENGTH2: + length2*: tuple[a, b: CSSLength] + of VALUE_BORDER_COLLAPSE: + bordercollapse*: CSSBorderCollapse of VALUE_NONE: discard CSSComputedValues* = ref array[CSSPropertyType, CSSComputedValue] @@ -207,7 +215,9 @@ const PropertyNames = { "right": PROPERTY_RIGHT, "top": PROPERTY_TOP, "bottom": PROPERTY_BOTTOM, - "caption-side": PROPERTY_CAPTION_SIDE + "caption-side": PROPERTY_CAPTION_SIDE, + "border-spacing": PROPERTY_BORDER_SPACING, + "border-collapse": PROPERTY_BORDER_COLLAPSE }.toTable() const ValueTypes* = [ @@ -242,7 +252,9 @@ const ValueTypes* = [ PROPERTY_RIGHT: VALUE_LENGTH, PROPERTY_TOP: VALUE_LENGTH, PROPERTY_BOTTOM: VALUE_LENGTH, - PROPERTY_CAPTION_SIDE: VALUE_CAPTION_SIDE + PROPERTY_CAPTION_SIDE: VALUE_CAPTION_SIDE, + PROPERTY_BORDER_SPACING: VALUE_LENGTH2, + PROPERTY_BORDER_COLLAPSE: VALUE_BORDER_COLLAPSE ] const InheritedProperties = { @@ -250,7 +262,7 @@ const InheritedProperties = { PROPERTY_FONT_WEIGHT, PROPERTY_TEXT_DECORATION, PROPERTY_WORD_BREAK, PROPERTY_LIST_STYLE_TYPE, PROPERTY_WORD_SPACING, PROPERTY_LINE_HEIGHT, PROPERTY_TEXT_ALIGN, PROPERTY_LIST_STYLE_POSITION, PROPERTY_BACKGROUND_COLOR, - PROPERTY_CAPTION_SIDE + PROPERTY_CAPTION_SIDE, PROPERTY_BORDER_SPACING, PROPERTY_BORDER_COLLAPSE } func getPropInheritedArray(): array[CSSPropertyType, bool] = @@ -451,6 +463,19 @@ func cssLength(val: CSSComponentValue): CSSLength = else: discard raise newException(CSSValueError, "Invalid length") +func cssAbsoluteLength(val: CSSComponentValue): CSSLength = + if val of CSSToken: + let tok = CSSToken(val) + case tok.tokenType + of CSS_NUMBER_TOKEN: + if tok.nvalue == 0: + return CSSLength(num: 0, unit: UNIT_PX) + of CSS_DIMENSION_TOKEN: + if tok.nvalue >= 0: + return cssLength(tok.nvalue, tok.unit) + else: discard + raise newException(CSSValueError, "Invalid length") + func cssWordSpacing(cval: CSSComponentValue): CSSLength = if cval of CSSToken: let tok = CSSToken(cval) @@ -666,14 +691,26 @@ func cssCaptionSide(cval: CSSComponentValue): CSSCaptionSide = of "inline-end": return CAPTION_SIDE_INLINE_END raise newException(CSSValueError, "Invalid caption side") +func cssBorderCollapse(cval: CSSComponentValue): CSSBorderCollapse = + if cval of CSSToken: + let tok = CSSToken(cval) + if tok.tokenType == CSS_IDENT_TOKEN: + case tok.value + of "collapse": return BORDER_COLLAPSE_COLLAPSE + of "separate": return BORDER_COLLAPSE_SEPARATE + raise newException(CSSValueError, "Invalid border collapse") + proc getValueFromDecl(val: CSSComputedValue, d: CSSDeclaration, vtype: CSSValueType, ptype: CSSPropertyType) = + template skip_whitespace = + while i < d.value.len: + if d.value[i] != CSS_WHITESPACE_TOKEN: break + inc i var i = 0 - while i < d.value.len: - if d.value[i] != CSS_WHITESPACE_TOKEN: break - inc i + skip_whitespace if i >= d.value.len: raise newException(CSSValueError, "Empty value") let cval = d.value[i] + inc i case vtype of VALUE_COLOR: val.color = cssColor(cval) @@ -700,6 +737,15 @@ proc getValueFromDecl(val: CSSComputedValue, d: CSSDeclaration, vtype: CSSValueT of VALUE_LIST_STYLE_POSITION: val.liststyleposition = cssListStylePosition(cval) of VALUE_POSITION: val.position = cssPosition(cval) of VALUE_CAPTION_SIDE: val.captionside = cssCaptionSide(cval) + of VALUE_BORDER_COLLAPSE: val.bordercollapse = cssBorderCollapse(cval) + of VALUE_LENGTH2: + val.length2.a = cssAbsoluteLength(cval) + skip_whitespace + if i >= d.value.len: + val.length2.b = val.length2.a + else: + let cval = d.value[i] + val.length2.b = cssAbsoluteLength(cval) of VALUE_NONE: discard func getInitialColor(t: CSSPropertyType): RGBAColor = @@ -848,7 +894,9 @@ func equals*(a, b: CSSComputedValue): bool = return true if a == nil or b == nil: return false - case valueType(a.t) + if a.v != b.v: + return false + case a.v of VALUE_COLOR: return a.color == b.color of VALUE_LENGTH: return a.length == b.length of VALUE_FONT_STYLE: return a.fontstyle == b.fontstyle @@ -864,6 +912,8 @@ func equals*(a, b: CSSComputedValue): bool = of VALUE_LIST_STYLE_POSITION: return a.liststyleposition == b.liststyleposition of VALUE_POSITION: return a.position == b.position of VALUE_CAPTION_SIDE: return a.captionside == b.captionside + of VALUE_LENGTH2: return a.length2 == b.length2 + of VALUE_BORDER_COLLAPSE: return a.bordercollapse == b.bordercollapse of VALUE_NONE: return true return false diff --git a/src/ips/socketstream.nim b/src/ips/socketstream.nim index e411956c..4f98517c 100644 --- a/src/ips/socketstream.nim +++ b/src/ips/socketstream.nim @@ -69,9 +69,10 @@ proc sendFileHandle*(s: SocketStream, fd: FileHandle) = hdr.msg_control = cmsgbuf hdr.msg_controllen = CMSG_LEN(csize_t(sizeof(FileHandle))) let cmsg = CMSG_FIRSTHDR(addr hdr) - # ...this is stupid. - var xl = CMSG_LEN(csize_t(sizeof(FileHandle))) - cast[ptr uint16](addr cmsg.cmsg_len)[] = cast[ptr uint16](addr xl)[] + # ...sigh + {.emit: [ + cmsg.cmsg_len, """ = CMSG_LEN(sizeof(int));""" + ].} cmsg.cmsg_level = SOL_SOCKET cmsg.cmsg_type = SCM_RIGHTS cast[ptr FileHandle](CMSG_DATA(cmsg))[] = fd diff --git a/src/layout/box.nim b/src/layout/box.nim index 708275d4..c79c52e1 100644 --- a/src/layout/box.nim +++ b/src/layout/box.nim @@ -158,6 +158,9 @@ type rows*: seq[RowContext] cols*: seq[ColumnContext] maxwidth*: int + blockspacing*: int + inlinespacing*: int + collapse*: bool InlineBlockBox* = ref object of InlineAtom innerbox*: BlockBox diff --git a/src/layout/engine.nim b/src/layout/engine.nim index bb1e160c..88d87bb1 100644 --- a/src/layout/engine.nim +++ b/src/layout/engine.nim @@ -682,7 +682,6 @@ proc positionBlocks(box: BlockBox) = x += box.compwidth div 2 of TEXT_ALIGN_CHA_LEFT: discard of TEXT_ALIGN_CHA_RIGHT: - eprint "cha-right" x += box.compwidth else: discard @@ -807,6 +806,7 @@ proc preBuildTableRow(pctx: var TableContext, box: TableRowBoxBuilder, parent: B let minw = cell.minwidth div cellbuilder.colspan let w = cell.width div cellbuilder.colspan for i in n ..< n + cellbuilder.colspan: + ctx.width += pctx.inlinespacing pctx.cols[i].maxwidth = w if pctx.cols[i].width < w: pctx.cols[i].width = w @@ -830,6 +830,7 @@ proc preBuildTableRow(pctx: var TableContext, box: TableRowBoxBuilder, parent: B pctx.cols[i].width = minw ctx.reflow[i] = true ctx.width += pctx.cols[i].width + ctx.width += pctx.inlinespacing n += cellbuilder.colspan inc i ctx.ncols = n @@ -846,9 +847,11 @@ proc buildTableRow(pctx: TableContext, ctx: RowContext, parent: BlockBox, builde for i in n ..< n + cellw.colspan: w += pctx.cols[i].width if cellw.reflow: - cell = parent.viewport.buildTableCell(cellw.builder, w, none(int), false) #TODO height? + cell = parent.viewport.buildTableCell(cellw.builder, w, none(int), false) w = max(w, cell.width) + x += pctx.inlinespacing cell.offset.x += x + x += pctx.inlinespacing x += w n += cellw.colspan if cell.computed{"vertical-align"}.keyword notin {VERTICAL_ALIGN_TOP, VERTICAL_ALIGN_MIDDLE, VERTICAL_ALIGN_BOTTOM}: # baseline @@ -856,6 +859,8 @@ proc buildTableRow(pctx: TableContext, ctx: RowContext, parent: BlockBox, builde row.nested.add(cell) row.height = max(row.height, cell.height) for cell in row.nested: + cell.height = max(cell.height, row.height) + for cell in row.nested: case cell.computed{"vertical-align"}.keyword of VERTICAL_ALIGN_TOP: cell.offset.y = 0 @@ -903,7 +908,7 @@ iterator rows(builder: TableBoxBuilder): BoxBuilder {.inline.} = for child in footer: yield child -proc calcUnspecifiedColIndices(ctx: var TableContext, dw: var int, weight: var float64): seq[int] = +proc calcUnspecifiedColIndices(ctx: var TableContext, dw: int, weight: var float64): seq[int] = var avail = newSeqUninitialized[int](ctx.cols.len) var i = 0 var j = 0 @@ -932,7 +937,12 @@ proc calcUnspecifiedColIndices(ctx: var TableContext, dw: var int, weight: var f # difference too. proc buildTable(box: TableBoxBuilder, parent: BlockBox): BlockBox = let table = parent.newBlockBox(box) - var ctx: TableContext + var ctx = TableContext( + collapse: table.computed{"border-collapse"} == BORDER_COLLAPSE_COLLAPSE + ) + if not ctx.collapse: + ctx.inlinespacing = table.computed{"border-spacing"}.a.px(parent.viewport) + ctx.blockspacing = table.computed{"border-spacing"}.b.px(parent.viewport) for row in box.rows: if unlikely(row.computed{"display"} == DISPLAY_TABLE_CAPTION): ctx.caption = TableCaptionBoxBuilder(row) @@ -946,7 +956,7 @@ proc buildTable(box: TableBoxBuilder, parent: BlockBox): BlockBox = forceresize = true var reflow = newSeq[bool](ctx.cols.len) if table.compwidth > ctx.maxwidth and (not table.shrink or forceresize): - var dw = (table.compwidth - ctx.maxwidth) + let dw = (table.compwidth - ctx.maxwidth) var weight: float64 var avail = ctx.calcUnspecifiedColIndices(dw, weight) let unit = float64(dw) / weight @@ -989,12 +999,19 @@ proc buildTable(box: TableBoxBuilder, parent: BlockBox): BlockBox = if n < row.reflow.len and row.reflow[n]: reflow[n] = true dec n + var y = 0 for roww in ctx.rows: + y += ctx.blockspacing let row = ctx.buildTableRow(roww, table, roww.builder) - row.offset.y += table.height - table.height += row.height + row.offset.y += y + row.offset.x += table.padding_left + row.width += table.padding_left + row.width += table.padding_right + y += ctx.blockspacing + y += row.height table.nested.add(row) table.width = max(row.width, table.width) + table.height = table.compheight.get(y) if ctx.caption != nil: case ctx.caption.computed{"caption-side"} of CAPTION_SIDE_TOP, CAPTION_SIDE_BLOCK_START: |