update li measurement + h1 border-bottom

This commit is contained in:
Gavin Nishizawa 2023-01-13 20:55:10 -08:00
parent 2727c58610
commit 1a07492517
No known key found for this signature in database
GPG key ID: AE3B177777CE55CD

View file

@ -2,6 +2,7 @@ package textmeasure
import ( import (
"bytes" "bytes"
"fmt"
"math" "math"
"strings" "strings"
@ -33,6 +34,7 @@ const (
MarginTop_h = 24 MarginTop_h = 24
MarginBottom_h = 16 MarginBottom_h = 16
PaddingBottom_h1_h2_em = 0.3 PaddingBottom_h1_h2_em = 0.3
BorderBottom_h1_h2 = 1
Height_hr = 4 Height_hr = 4
MarginTopBottom_hr = 24 MarginTopBottom_hr = 24
@ -203,6 +205,18 @@ func (ruler *Ruler) measureNode(depth int, n *html.Node, fontFamily *d2fonts.Fon
parentElementType = n.Parent.Data parentElementType = n.Parent.Data
} }
debugMeasure := false
var depthStr string
if debugMeasure {
if depth == 0 {
fmt.Println()
}
depthStr = "┌"
for i := 0; i < depth; i++ {
depthStr += "-"
}
}
switch n.Type { switch n.Type {
case html.TextNode: case html.TextNode:
if strings.TrimSpace(n.Data) == "" { if strings.TrimSpace(n.Data) == "" {
@ -244,6 +258,9 @@ func (ruler *Ruler) measureNode(depth int, n *html.Node, fontFamily *d2fonts.Fon
w *= FontSize_pre_code_em w *= FontSize_pre_code_em
h *= FontSize_pre_code_em h *= FontSize_pre_code_em
} }
if debugMeasure {
fmt.Printf("%stext(%v,%v)\n", depthStr, w, h)
}
return blockAttrs{w + spaceWidths, h, 0, 0} return blockAttrs{w + spaceWidths, h, 0, 0}
case html.ElementNode: case html.ElementNode:
isCode := false isCode := false
@ -273,58 +290,58 @@ func (ruler *Ruler) measureNode(depth int, n *html.Node, fontFamily *d2fonts.Fon
last := getPrev(n.LastChild) last := getPrev(n.LastChild)
var blocks []blockAttrs var blocks []blockAttrs
var current *blockAttrs var inlineBlock *blockAttrs
// first create blocks from combined inline elements, then combine all blocks // first create blocks from combined inline elements, then combine all blocks
// current will be non-nil while inline elements are being combined into a block // inlineBlock will be non-nil while inline elements are being combined into a block
endInlineBlock := func() {
if !isCode && inlineBlock.height > 0 && inlineBlock.height < MarkdownLineHeightPx {
inlineBlock.height = MarkdownLineHeightPx
}
blocks = append(blocks, *inlineBlock)
inlineBlock = nil
}
for child := n.FirstChild; child != nil; child = child.NextSibling { for child := n.FirstChild; child != nil; child = child.NextSibling {
childBlock := ruler.measureNode(depth+1, child, fontFamily, fontSize, fontStyle) childBlock := ruler.measureNode(depth+1, child, fontFamily, fontSize, fontStyle)
if child.Type == html.ElementNode && isBlockElement(child.Data) { if child.Type == html.ElementNode && isBlockElement(child.Data) {
if current != nil { if inlineBlock != nil {
blocks = append(blocks, *current) endInlineBlock()
} }
current = &blockAttrs{} newBlock := &blockAttrs{}
newBlock.width = childBlock.width
newBlock.height = childBlock.height
if child == first && n.Data == "blockquote" { if child == first && n.Data == "blockquote" {
current.marginTop = 0. newBlock.marginTop = 0.
} else { } else {
current.marginTop = childBlock.marginTop newBlock.marginTop = childBlock.marginTop
} }
if child == last && n.Data == "blockquote" { if child == last && n.Data == "blockquote" {
current.marginBottom = 0. newBlock.marginBottom = 0.
} else { } else {
current.marginBottom = childBlock.marginBottom newBlock.marginBottom = childBlock.marginBottom
} }
current.width = childBlock.width blocks = append(blocks, *newBlock)
current.height = childBlock.height
blocks = append(blocks, *current)
current = nil
} else if child.Type == html.ElementNode && child.Data == "br" { } else if child.Type == html.ElementNode && child.Data == "br" {
if current != nil { if inlineBlock != nil {
if !isCode && current.height > 0 && current.height < MarkdownLineHeightPx { endInlineBlock()
current.height = MarkdownLineHeightPx
}
blocks = append(blocks, *current)
current = nil
} }
} else if childBlock.isNotEmpty() { } else if childBlock.isNotEmpty() {
if current == nil { if inlineBlock == nil {
current = &childBlock // start inline block with child
inlineBlock = &childBlock
} else { } else {
current.marginTop = go2.Max(current.marginTop, childBlock.marginTop) // stack inline element dimensions horizontally
current.marginBottom = go2.Max(current.marginBottom, childBlock.marginBottom) inlineBlock.width += childBlock.width
inlineBlock.height = go2.Max(inlineBlock.height, childBlock.height)
current.width += childBlock.width inlineBlock.marginTop = go2.Max(inlineBlock.marginTop, childBlock.marginTop)
current.height = go2.Max(current.height, childBlock.height) inlineBlock.marginBottom = go2.Max(inlineBlock.marginBottom, childBlock.marginBottom)
} }
} }
} }
if current != nil { if inlineBlock != nil {
if !isCode && current.height > 0 && current.height < MarkdownLineHeightPx { endInlineBlock()
current.height = MarkdownLineHeightPx
}
blocks = append(blocks, *current)
current = nil
} }
var prevMarginBottom float64 var prevMarginBottom float64
@ -363,7 +380,7 @@ func (ruler *Ruler) measureNode(depth int, n *html.Node, fontFamily *d2fonts.Fon
block.marginBottom = go2.Max(block.marginBottom, MarginBottom_h) block.marginBottom = go2.Max(block.marginBottom, MarginBottom_h)
switch n.Data { switch n.Data {
case "h1", "h2": case "h1", "h2":
block.height += PaddingBottom_h1_h2_em * float64(fontSize) block.height += PaddingBottom_h1_h2_em*float64(fontSize) + BorderBottom_h1_h2
} }
case "li": case "li":
block.width += PaddingLeft_ul_ol block.width += PaddingLeft_ul_ol
@ -394,6 +411,9 @@ func (ruler *Ruler) measureNode(depth int, n *html.Node, fontFamily *d2fonts.Fon
if block.height > 0 && block.height < MarkdownLineHeightPx { if block.height > 0 && block.height < MarkdownLineHeightPx {
block.height = MarkdownLineHeightPx block.height = MarkdownLineHeightPx
} }
if debugMeasure {
fmt.Printf("%s%s(%v,%v) mt:%v mb:%v\n", depthStr, n.Data, block.width, block.height, block.marginTop, block.marginBottom)
}
return block return block
} }
return blockAttrs{} return blockAttrs{}