diff --git a/d2renderers/d2svg/github-markdown.css b/d2renderers/d2svg/github-markdown.css index c5adbeae5..49e1dfaa8 100644 --- a/d2renderers/d2svg/github-markdown.css +++ b/d2renderers/d2svg/github-markdown.css @@ -639,7 +639,7 @@ .md tt { padding: 0.2em 0.4em; margin: 0; - /* font-size: 85%; */ + font-size: 85%; background-color: var(--color-neutral-muted); border-radius: 6px; } @@ -679,8 +679,8 @@ .md pre { padding: 16px; overflow: auto; - /* font-size: 85%; - line-height: 1.45; */ + font-size: 85%; + line-height: 1.45; background-color: var(--color-canvas-subtle); border-radius: 6px; } diff --git a/d2renderers/textmeasure/markdown.go b/d2renderers/textmeasure/markdown.go index 2f1829280..fb2145767 100644 --- a/d2renderers/textmeasure/markdown.go +++ b/d2renderers/textmeasure/markdown.go @@ -37,8 +37,10 @@ const ( Height_hr = 4 MarginTopBottom_hr = 24 - Padding_pre = 16 - MarginBottom_pre = 16 + Padding_pre = 16 + MarginBottom_pre = 16 + LineHeight_pre = 1.45 + FontSize_pre_code_em = 0.85 PaddingTopBottom_code_em = 0.2 PaddingLeftRight_code_em = 0.4 @@ -100,9 +102,11 @@ func MeasureMarkdown(mdText string, ruler *Ruler) (width, height int, err error) { originalLineHeight := ruler.LineHeightFactor + ruler.boundsWithDot = true ruler.LineHeightFactor = MarkdownLineHeight defer func() { ruler.LineHeightFactor = originalLineHeight + ruler.boundsWithDot = false }() } @@ -256,14 +260,20 @@ func (ruler *Ruler) measureNode(depth int, n *html.Node, font d2fonts.Font) (wid str = str[startIndex : endIndex+1] } + } - if isCode { - ruler.boundsWithDot = true + if parentElementType == "pre" { + originalLineHeight := ruler.LineHeightFactor + ruler.LineHeightFactor = LineHeight_pre + defer func() { + ruler.LineHeightFactor = originalLineHeight + }() } w, h := ruler.MeasurePrecise(font, str) if isCode { - ruler.boundsWithDot = false + w *= FontSize_pre_code_em + h *= FontSize_pre_code_em } // fmt.Printf("%d:'%s' width %v (%v) height %v fontStyle %s fontSize %v family %v\n", depth, n.Data, w, w+spaceWidths, h, font.Style, font.Size, font.Family) diff --git a/d2renderers/textmeasure/textmeasure_test.go b/d2renderers/textmeasure/textmeasure_test.go index e7a2cb9f2..616c20c5c 100644 --- a/d2renderers/textmeasure/textmeasure_test.go +++ b/d2renderers/textmeasure/textmeasure_test.go @@ -88,19 +88,19 @@ _italics are all measured correctly_ `: {214, 24}, ` **bold is measured correctly** -`: {187, 24}, +`: {188, 24}, ` **Note:** This document -`: {141, 24}, +`: {143, 24}, ` **Note:** -`: {37, 24}, - `a`: {7, 24}, +`: {39, 24}, + `a`: {9, 24}, `w`: {12, 24}, `ww`: {24, 24}, - "`inline code`": {119, 27}, - "`code`": {52, 27}, - "`a`": {23, 27}, + "`inline code`": {103, 24}, + "`code`": {46, 24}, + "`a`": {21, 24}, } func TestTextMeasureMarkdown(t *testing.T) { diff --git a/e2etests/testdata/stable/chaos2/board.exp.json b/e2etests/testdata/stable/chaos2/board.exp.json index be553467a..8caeb8bd9 100644 --- a/e2etests/testdata/stable/chaos2/board.exp.json +++ b/e2etests/testdata/stable/chaos2/board.exp.json @@ -8,7 +8,7 @@ "x": 0, "y": 0 }, - "width": 1115, + "width": 1117, "height": 1654, "level": 1, "opacity": 1, @@ -421,7 +421,7 @@ "id": "aa.ll", "type": "", "pos": { - "x": 914, + "x": 915, "y": 652 }, "width": 114, @@ -459,7 +459,7 @@ "id": "aa.mm", "type": "cylinder", "pos": { - "x": 901, + "x": 902, "y": 426 }, "width": 131, @@ -500,7 +500,7 @@ "x": 867, "y": 1429 }, - "width": 16, + "width": 18, "height": 24, "level": 2, "opacity": 1, @@ -527,14 +527,14 @@ "italic": false, "bold": true, "underline": false, - "labelWidth": 16, + "labelWidth": 18, "labelHeight": 24 }, { "id": "aa.oo", "type": "", "pos": { - "x": 943, + "x": 945, "y": 1378 }, "width": 123, @@ -785,20 +785,20 @@ "labelPercentage": 0, "route": [ { - "x": 913.5, - "y": 723.8172484599589 + "x": 914.5, + "y": 723.8051948051948 }, { - "x": 835.9, - "y": 735.8172484599589 + "x": 836.1, + "y": 735.8051948051948 }, { - "x": 894.1, - "y": 726.8172484599589 + "x": 894.9, + "y": 726.8051948051948 }, { "x": 816.5, - "y": 738.8172484599589 + "y": 738.8051948051948 } ], "isCurve": true, @@ -832,11 +832,11 @@ "labelPercentage": 0, "route": [ { - "x": 901, + "x": 902, "y": 500 }, { - "x": 404.2, + "x": 404.4, "y": 581.6 }, { @@ -891,19 +891,19 @@ "labelPercentage": 0, "route": [ { - "x": 969, + "x": 970, "y": 552 }, { - "x": 970.2, + "x": 971.2, "y": 592 }, { - "x": 970.5, + "x": 971.5, "y": 612 }, { - "x": 970.5, + "x": 971.5, "y": 652 } ], @@ -938,20 +938,20 @@ "labelPercentage": 0, "route": [ { - "x": 901, + "x": 902, "y": 500 }, { - "x": 833.6, - "y": 512.3623984526112 + "x": 833.8, + "y": 512.3482425646968 }, { - "x": 884.15, - "y": 503.0905996131528 + "x": 884.95, + "y": 503.08706064117416 }, { "x": 816.75, - "y": 515.452998065764 + "y": 515.4353032058709 } ], "isCurve": true, @@ -985,12 +985,12 @@ "labelPercentage": 0, "route": [ { - "x": 913.5, - "y": 729.1405049396268 + "x": 914.5, + "y": 729.1095290251917 }, { - "x": 594.7, - "y": 808.2281009879254 + "x": 594.9, + "y": 808.2219058050383 }, { "x": 515, @@ -1080,11 +1080,11 @@ "labelPercentage": 0, "route": [ { - "x": 901, + "x": 902, "y": 476 }, { - "x": 500.2, + "x": 500.4, "y": 396 }, { @@ -1135,12 +1135,12 @@ "y": 838 }, { - "x": 484.7, - "y": 807.5723440134907 + "x": 484.9, + "y": 807.5686868686869 }, { - "x": 913.5, - "y": 725.8617200674536 + "x": 914.5, + "y": 725.8434343434343 } ], "isCurve": true, @@ -1194,12 +1194,12 @@ "y": 466.4 }, { - "x": 470.7, - "y": 622.4899262899263 + "x": 470.9, + "y": 622.493376941946 }, { - "x": 913.5, - "y": 704.4496314496314 + "x": 914.5, + "y": 704.4668847097302 } ], "isCurve": true, diff --git a/e2etests/testdata/stable/chaos2/sketch.exp.svg b/e2etests/testdata/stable/chaos2/sketch.exp.svg index 306355ef4..baabef123 100644 --- a/e2etests/testdata/stable/chaos2/sketch.exp.svg +++ b/e2etests/testdata/stable/chaos2/sketch.exp.svg @@ -2,7 +2,7 @@