diff options
| author | bptato <nincsnevem662@gmail.com> | 2022-12-16 19:01:56 +0100 |
|---|---|---|
| committer | bptato <nincsnevem662@gmail.com> | 2022-12-16 19:13:53 +0100 |
| commit | 7146419b06c87eb00a51b16d5bec3e22b7d0a949 (patch) | |
| tree | e34dde3ed3887aa21c77cd2444e463c12458f429 /src | |
| parent | ffee0d78c9bcbf04b41892dee613ebdc7ae0b95d (diff) | |
| download | chawan-7146419b06c87eb00a51b16d5bec3e22b7d0a949.tar.gz | |
Display text for img tag, background-image
Diffstat (limited to 'src')
| -rw-r--r-- | src/css/cascade.nim | 18 | ||||
| -rw-r--r-- | src/css/selectorparser.nim | 2 | ||||
| -rw-r--r-- | src/css/stylednode.nim | 4 | ||||
| -rw-r--r-- | src/css/values.nim | 48 | ||||
| -rw-r--r-- | src/layout/box.nim | 3 | ||||
| -rw-r--r-- | src/layout/engine.nim | 88 | ||||
| -rw-r--r-- | src/render/renderdocument.nim | 12 |
7 files changed, 116 insertions, 59 deletions
diff --git a/src/css/cascade.nim b/src/css/cascade.nim index 4c782ded..eb9e87b8 100644 --- a/src/css/cascade.nim +++ b/src/css/cascade.nim @@ -134,6 +134,9 @@ func calcPresentationalHints(element: Element): CSSComputedValues = map_align of TAG_COL: map_width + of TAG_IMG: + map_width + map_height of TAG_BODY: map_bgcolor map_text @@ -262,6 +265,8 @@ proc applyRules(document: Document, ua, user: CSSStylesheet, cachedTree: StyledN let content = HTMLInputElement(styledParent.node).inputString() if content.len > 0: let styledText = styledParent.newStyledText(content) + # Note: some pseudo-elements (like input text) generate text nodes + # directly, so we have to cache them like this. styledText.pseudo = pseudo styledParent.children.add(styledText) of PSEUDO_TEXTAREA_TEXT: @@ -270,6 +275,11 @@ proc applyRules(document: Document, ua, user: CSSStylesheet, cachedTree: StyledN let styledText = styledParent.newStyledText(content) styledText.pseudo = pseudo styledParent.children.add(styledText) + of PSEUDO_IMAGE: + let content = CSSContent(t: CONTENT_IMAGE, s: "[img]") + let styledText = styledParent.newStyledReplacement(content) + styledText.pseudo = pseudo + styledParent.children.add(styledText) of PSEUDO_NONE: discard else: assert child != nil @@ -344,14 +354,16 @@ proc applyRules(document: Document, ua, user: CSSStylesheet, cachedTree: StyledN stack_append styledChild, PSEUDO_AFTER - if elem.tagType != TAG_TEXTAREA: + if elem.tagType == TAG_TEXTAREA: + stack_append styledChild, PSEUDO_TEXTAREA_TEXT + elif elem.tagType == TAG_IMG or elem.tagType == TAG_IMAGE: + stack_append styledChild, PSEUDO_IMAGE + else: for i in countdown(elem.childNodes.high, 0): if elem.childNodes[i].nodeType in {ELEMENT_NODE, TEXT_NODE}: stack_append styledChild, elem.childNodes[i] if elem.tagType == TAG_INPUT: stack_append styledChild, PSEUDO_INPUT_TEXT - else: - stack_append styledChild, PSEUDO_TEXTAREA_TEXT stack_append styledChild, PSEUDO_BEFORE diff --git a/src/css/selectorparser.nim b/src/css/selectorparser.nim index 926606da..8a38292b 100644 --- a/src/css/selectorparser.nim +++ b/src/css/selectorparser.nim @@ -19,7 +19,7 @@ type PseudoElem* = enum PSEUDO_NONE, PSEUDO_BEFORE, PSEUDO_AFTER, # internal - PSEUDO_INPUT_TEXT, PSEUDO_TEXTAREA_TEXT + PSEUDO_INPUT_TEXT, PSEUDO_TEXTAREA_TEXT, PSEUDO_IMAGE PseudoClass* = enum PSEUDO_FIRST_CHILD, PSEUDO_LAST_CHILD, PSEUDO_ONLY_CHILD, PSEUDO_HOVER, diff --git a/src/css/stylednode.nim b/src/css/stylednode.nim index 28537abd..bc0d1b5c 100644 --- a/src/css/stylednode.nim +++ b/src/css/stylednode.nim @@ -51,7 +51,7 @@ type StyledNode* = ref object parent*: StyledNode node*: Node - pseudo*: PseudoElem #TODO this should be in element only + pseudo*: PseudoElem case t*: StyledType of STYLED_TEXT: text*: string @@ -83,6 +83,8 @@ func checked(element: Element): bool = func isValid*(styledNode: StyledNode): bool = if styledNode.t == STYLED_TEXT: return true + if styledNode.t == STYLED_REPLACEMENT: + return true if styledNode.node != nil and Element(styledNode.node).invalid: return false for d in DependencyType: diff --git a/src/css/values.nim b/src/css/values.nim index c40ffa4e..c79a5d9e 100644 --- a/src/css/values.nim +++ b/src/css/values.nim @@ -34,7 +34,7 @@ type PROPERTY_RIGHT, PROPERTY_TOP, PROPERTY_BOTTOM, PROPERTY_CAPTION_SIDE, PROPERTY_BORDER_SPACING, PROPERTY_BORDER_COLLAPSE, PROPERTY_QUOTES, PROPERTY_COUNTER_RESET, PROPERTY_MAX_WIDTH, PROPERTY_MAX_HEIGHT, - PROPERTY_MIN_WIDTH, PROPERTY_MIN_HEIGHT + PROPERTY_MIN_WIDTH, PROPERTY_MIN_HEIGHT, PROPERTY_BACKGROUND_IMAGE CSSValueType* = enum VALUE_NONE, VALUE_LENGTH, VALUE_COLOR, VALUE_CONTENT, VALUE_DISPLAY, @@ -42,7 +42,7 @@ type VALUE_WORD_BREAK, VALUE_LIST_STYLE_TYPE, VALUE_VERTICAL_ALIGN, VALUE_TEXT_ALIGN, VALUE_LIST_STYLE_POSITION, VALUE_POSITION, VALUE_CAPTION_SIDE, VALUE_LENGTH2, VALUE_BORDER_COLLAPSE, VALUE_QUOTES, - VALUE_COUNTER_RESET + VALUE_COUNTER_RESET, VALUE_IMAGE CSSGlobalValueType* = enum VALUE_NOGLOBAL, VALUE_INITIAL, VALUE_INHERIT, VALUE_REVERT, VALUE_UNSET @@ -101,7 +101,7 @@ type CSSContentType* = enum CONTENT_STRING, CONTENT_OPEN_QUOTE, CONTENT_CLOSE_QUOTE, - CONTENT_NO_OPEN_QUOTE, CONTENT_NO_CLOSE_QUOTE + CONTENT_NO_OPEN_QUOTE, CONTENT_NO_CLOSE_QUOTE, CONTENT_IMAGE const RowGroupBox* = {DISPLAY_TABLE_ROW_GROUP, DISPLAY_TABLE_HEADER_GROUP, DISPLAY_TABLE_FOOTER_GROUP} @@ -177,6 +177,8 @@ type bordercollapse*: CSSBorderCollapse of VALUE_COUNTER_RESET: counterreset*: seq[CSSCounterReset] + of VALUE_IMAGE: + image*: CSSContent of VALUE_NONE: discard CSSComputedValues* = ref array[CSSPropertyType, CSSComputedValue] @@ -246,7 +248,8 @@ const PropertyNames = { "max-width": PROPERTY_MAX_WIDTH, "max-height": PROPERTY_MAX_HEIGHT, "min-width": PROPERTY_MIN_WIDTH, - "min-height": PROPERTY_MIN_HEIGHT + "min-height": PROPERTY_MIN_HEIGHT, + "background-image": PROPERTY_BACKGROUND_IMAGE }.toTable() const ValueTypes* = [ @@ -290,6 +293,7 @@ const ValueTypes* = [ PROPERTY_MAX_HEIGHT: VALUE_LENGTH, PROPERTY_MIN_WIDTH: VALUE_LENGTH, PROPERTY_MIN_HEIGHT: VALUE_LENGTH, + PROPERTY_BACKGROUND_IMAGE: VALUE_IMAGE ] const InheritedProperties = { @@ -815,6 +819,13 @@ func cssMaxMinSize(cval: CSSComponentValue): Option[CSSLength] = return some(cssLength(tok)) else: discard +#TODO this should be a separate type +func cssImage(cval: CSSComponentValue): Option[CSSContent] = + if isToken(cval): + let tok = getToken(cval) + if tok.tokenType == CSS_URL_TOKEN or tok.tokenType == CSS_BAD_URL_TOKEN: + return some(CSSContent(t: CONTENT_IMAGE, s: "[img]")) + proc getValueFromDecl(val: CSSComputedValue, d: CSSDeclaration, vtype: CSSValueType, ptype: CSSPropertyType) = template skip_whitespace = while i < d.value.len: @@ -880,6 +891,12 @@ proc getValueFromDecl(val: CSSComputedValue, d: CSSDeclaration, vtype: CSSValueT val.counterreset = res.get else: raise newException(CSSValueError, "Invalid counter reset") + of VALUE_IMAGE: + let res = cssImage(d) + if res.isSome: + val.image = res.get + else: + raise newException(CSSValueError, "Invalid image") of VALUE_NONE: discard func getInitialColor(t: CSSPropertyType): RGBAColor = @@ -1015,16 +1032,22 @@ proc getComputedValues(d: CSSDeclaration): seq[(CSSComputedValue, CSSGlobalValue of SHORTHAND_BACKGROUND: let global = cssGlobal(d) let bgcolorptype = PROPERTY_BACKGROUND_COLOR - let bgcolorvtype = valueType(bgcolorptype) - let bgcolorval = CSSComputedValue(t: bgcolorptype, v: bgcolorvtype) + let bgcolorval = CSSComputedValue(t: bgcolorptype, v: valueType(bgcolorptype)) + let bgimageptype = PROPERTY_BACKGROUND_IMAGE + let bgimageval = CSSComputedValue(t: bgimageptype, v: valueType(bgimageptype)) if global == VALUE_NOGLOBAL: for tok in d.value: - try: - bgcolorval.color = cssColor(tok) - result.add((bgcolorval, global)) - break - except CSSValueError: - discard + let img = cssImage(tok) + if img.isSome: + bgimageval.image = img.get + result.add((bgimageval, global)) + else: + try: + bgcolorval.color = cssColor(tok) + result.add((bgcolorval, global)) + break + except CSSValueError: + discard else: result.add((bgcolorval, global)) @@ -1055,6 +1078,7 @@ func equals*(a, b: CSSComputedValue): bool = of VALUE_BORDER_COLLAPSE: return a.bordercollapse == b.bordercollapse of VALUE_QUOTES: return a.quotes == b.quotes of VALUE_COUNTER_RESET: return a.counterreset == b.counterreset + of VALUE_IMAGE: return a.image == b.image of VALUE_NONE: return true return false diff --git a/src/layout/box.nim b/src/layout/box.nim index d84cecf2..acf995ca 100644 --- a/src/layout/box.nim +++ b/src/layout/box.nim @@ -2,7 +2,6 @@ import options import css/stylednode import css/values -import html/dom import io/window import types/color @@ -99,12 +98,12 @@ type minwidth*: int maxwidth*: int viewport*: Viewport - node*: Node shrink*: bool format*: ComputedFormat BlockBox* = ref object of RootObj inline*: InlineContext + node*: StyledNode nested*: seq[BlockBox] computed*: CSSComputedValues viewport*: Viewport diff --git a/src/layout/engine.nim b/src/layout/engine.nim index e6e73899..479e8f3a 100644 --- a/src/layout/engine.nim +++ b/src/layout/engine.nim @@ -323,6 +323,8 @@ proc renderText*(ictx: InlineContext, str: string, maxwidth: int, computed: CSSC state.addWord() type PreferredDimensions = object + # warning: this is actually the available space, not the computed width. + # TODO rename this compwidth: int compheight: Option[int] margin_top: int @@ -357,9 +359,9 @@ proc preferredDimensions(computed: CSSComputedValues, viewport: Viewport, width: result.min_width = some(computed{"min-width"}.px(viewport, width)) if not computed{"min-height"}.auto: if computed{"min-height"}.unit != UNIT_PERC: - result.compheight = computed{"min-height"}.px(viewport).some + result.max_height = computed{"min-height"}.px(viewport).some elif height.issome: - result.compheight = computed{"min-height"}.px(viewport, height.get).some + result.max_height = computed{"min-height"}.px(viewport, height.get).some result.margin_top = computed{"margin-top"}.px(viewport, width) result.margin_bottom = computed{"margin-top"}.px(viewport, width) @@ -439,11 +441,12 @@ func isShrink(box: BlockBox, parent: BlockBox = nil, override = false): bool = return parent.shrink and no_specified_width else: discard -proc newFlowRootBox(viewport: Viewport, box: BoxBuilder, parentWidth: int, parentHeight = none(int), shrink = true): BlockBox {.inline.} = +proc newFlowRootBox(viewport: Viewport, builder: BoxBuilder, parentWidth: int, parentHeight = none(int), shrink = true): BlockBox {.inline.} = new(result) result.viewport = viewport - result.computed = box.computed + result.computed = builder.computed result.setPreferredDimensions(parentWidth, parentHeight) + result.node = builder.node result.shrink = result.isShrink(nil, shrink) proc newBlockBox(parent: BlockBox, builder: BoxBuilder): BlockBox = @@ -451,6 +454,7 @@ proc newBlockBox(parent: BlockBox, builder: BoxBuilder): BlockBox = result.viewport = parent.viewport result.computed = builder.computed result.setPreferredDimensions(parent.compwidth, parent.compheight) + result.node = builder.node result.shrink = result.isShrink(parent) proc newListItem(parent: BlockBox, builder: ListItemBoxBuilder): ListItemBox = @@ -458,6 +462,7 @@ proc newListItem(parent: BlockBox, builder: ListItemBoxBuilder): ListItemBox = result.viewport = parent.viewport result.computed = builder.content.computed result.setPreferredDimensions(parent.compwidth, parent.compheight) + result.node = builder.node result.shrink = result.isShrink(parent) proc newInlineBlock(viewport: Viewport, builder: BoxBuilder, parentWidth: int, parentHeight = none(int)): InlineBlockBox = @@ -470,6 +475,18 @@ proc newInlineContext(parent: BlockBox): InlineContext = result.viewport = parent.viewport result.shrink = parent.shrink +proc buildBlock(box: BlockBoxBuilder, parent: BlockBox): BlockBox +proc buildInlines(parent: BlockBox, inlines: seq[BoxBuilder]): InlineContext +proc buildBlocks(parent: BlockBox, blocks: seq[BoxBuilder], node: StyledNode) +proc buildTable(box: TableBoxBuilder, parent: BlockBox): BlockBox + +proc applyInlineDimensions(parent: BlockBox) = + parent.height += parent.inline.height + if parent.compheight.issome: + parent.height = parent.compheight.get + parent.width = max(parent.width, parent.inline.maxwidth) + parent.xminwidth = max(parent.xminwidth, parent.inline.minwidth) + proc positionInlines(parent: BlockBox) = parent.width += parent.padding_left parent.inline.offset.x += parent.padding_left @@ -481,8 +498,6 @@ proc positionInlines(parent: BlockBox) = parent.width += parent.padding_right - parent.xminwidth = max(parent.xminwidth, parent.inline.minwidth) - if parent.computed{"width"}.auto: if parent.shrink: parent.width = min(parent.width, parent.compwidth) @@ -499,17 +514,6 @@ proc positionInlines(parent: BlockBox) = if parent.min_height.isSome and parent.height < parent.min_height.get: parent.height = parent.min_height.get -proc buildBlock(box: BlockBoxBuilder, parent: BlockBox): BlockBox -proc buildInlines(parent: BlockBox, inlines: seq[BoxBuilder]): InlineContext -proc buildBlocks(parent: BlockBox, blocks: seq[BoxBuilder], node: StyledNode) -proc buildTable(box: TableBoxBuilder, parent: BlockBox): BlockBox - -proc applyInlineDimensions(parent: BlockBox) = - parent.height += parent.inline.height - if parent.compheight.issome: - parent.height = parent.compheight.get - parent.width = max(parent.width, parent.inline.maxwidth) - # Builder only contains inline boxes. proc buildInlineLayout(parent: BlockBox, children: seq[BoxBuilder]) = parent.inline = parent.buildInlines(children) @@ -525,27 +529,27 @@ proc buildBlockLayout(parent: BlockBox, children: seq[BoxBuilder], node: StyledN discard parent.viewport.absolutes.pop() #TODO this is horribly inefficient -func firstBaseline(bctx: BlockBox): int = - if bctx.inline != nil: - if bctx.inline.lines.len > 0: - return bctx.offset.y + bctx.inline.lines[0].baseline - return bctx.offset.y - if bctx.nested.len > 0: - return bctx.offset.y + bctx.nested[^1].firstBaseline - bctx.offset.y +func firstBaseline(box: BlockBox): int = + if box.inline != nil: + if box.inline.lines.len > 0: + return box.offset.y + box.inline.lines[0].baseline + return box.offset.y + if box.nested.len > 0: + return box.offset.y + box.nested[^1].firstBaseline + box.offset.y #TODO ditto -func baseline(bctx: BlockBox): int = - if bctx.inline != nil: +func baseline(box: BlockBox): int = + if box.inline != nil: var y = 0 - for line in bctx.inline.lines: - if line == bctx.inline.lines[^1]: - return bctx.offset.y + y + line.baseline + for line in box.inline.lines: + if line == box.inline.lines[^1]: + return box.offset.y + y + line.baseline y += line.height - return bctx.offset.y - if bctx.nested.len > 0: - return bctx.offset.y + bctx.nested[^1].baseline - bctx.offset.y + return box.offset.y + box.height + if box.nested.len > 0: + return box.offset.y + box.nested[^1].baseline + box.offset.y proc buildInlineBlock(builder: BlockBoxBuilder, parent: InlineContext, parentWidth: int, parentHeight = none(int)): InlineBlockBox = result = newInlineBlock(parent.viewport, builder, parentWidth) @@ -1382,6 +1386,9 @@ proc generateReplacement(ctx: var InnerBlockContext, child, parent: StyledNode) of CONTENT_STRING: #TODO canGenerateAnonymousInline? ctx.generateInlineText(child.content.s, parent) + of CONTENT_IMAGE: + #TODO idk + ctx.generateInlineText(child.content.s, parent) proc generateInlineBoxes(ctx: var InnerBlockContext, styledNode: StyledNode) = for child in styledNode.children: @@ -1426,6 +1433,7 @@ proc generateInnerBlockBox(ctx: var InnerBlockContext) = proc generateBlockBox(styledNode: StyledNode, viewport: Viewport, marker = none(MarkerBoxBuilder), parent: ptr InnerBlockContext = nil): BlockBoxBuilder = let box = getBlockBox(styledNode.computed) + box.node = styledNode var ctx = newInnerBlockContext(styledNode, box, viewport, parent) if marker.issome: @@ -1434,12 +1442,12 @@ proc generateBlockBox(styledNode: StyledNode, viewport: Viewport, marker = none( ctx.generateInnerBlockBox() - if ctx.blockgroup.boxes.len > 0: - # Avoid unnecessary anonymous block boxes - if box.children.len == 0: - box.children = ctx.blockgroup.boxes - box.inlinelayout = true - ctx.blockgroup.boxes.setLen(0) + # Avoid unnecessary anonymous block boxes. This also helps set our layout to + # inline even if no inner anonymous block was generated. + if box.children.len == 0: + box.children = ctx.blockgroup.boxes + box.inlinelayout = true + ctx.blockgroup.boxes.setLen(0) ctx.flush() return box diff --git a/src/render/renderdocument.nim b/src/render/renderdocument.nim index 914ada85..bdcf4df8 100644 --- a/src/render/renderdocument.nim +++ b/src/render/renderdocument.nim @@ -300,6 +300,18 @@ proc renderBlockContext(grid: var FlexibleGrid, ctx: BlockBox, x, y: int, window if ctx.computed{"background-color"}.a != 0: #TODO color blending grid.paintBackground(ctx.computed{"background-color"}, x, y, x + ctx.width, y + ctx.height, window) + if ctx.computed{"background-image"}.t == CONTENT_IMAGE: + # ugly hack for background-image display... TODO actually implement images + let s = ctx.computed{"background-image"}.s # [img] + let w = s.len * window.ppc + var x = x + if ctx.width < w: + # text is larger than image; center it to minimize error + x -= w div 2 + x += ctx.width div 2 + x = x div window.ppc + y = y div window.ppl + grid.setText(s, ComputedFormat(node: ctx.node), x, y) if ctx of ListItemBox: let ctx = ListItemBox(ctx) |