update leading/trailing accounting

This commit is contained in:
Gavin Nishizawa 2023-06-19 15:25:14 -07:00
parent 14afd8c433
commit 14300dc9f8
No known key found for this signature in database
GPG key ID: AE3B177777CE55CD
3 changed files with 13 additions and 13 deletions

View file

@ -1314,29 +1314,28 @@ func GetTextDimensions(mtexts []*d2target.MText, ruler *textmeasure.Ruler, t *d2
var h int
if t.Language != "" {
originalLineHeight := ruler.LineHeightFactor
ruler.LineHeightFactor = 1.3
ruler.LineHeightFactor = textmeasure.CODE_LINE_HEIGHT
w, h = ruler.MeasureMono(d2fonts.SourceCodePro.Font(t.FontSize, d2fonts.FONT_STYLE_REGULAR), t.Text)
ruler.LineHeightFactor = originalLineHeight
// count empty leading and trailing lines since ruler will not be able to measure it
lines := strings.Split(t.Text, "\n")
leadingLines := 0
for _, line := range lines {
if strings.TrimSpace(line) == "" {
leadingLines++
} else {
break
}
hasLeading := false
if len(lines) > 0 && strings.TrimSpace(lines[0]) == "" {
hasLeading = true
}
trailingLines := 0
numTrailing := 0
for i := len(lines) - 1; i >= 0; i-- {
if strings.TrimSpace(lines[i]) == "" {
trailingLines++
numTrailing++
} else {
break
}
}
h += t.FontSize * (leadingLines + trailingLines)
if hasLeading && numTrailing < len(lines) {
h += t.FontSize
}
h += int(math.Ceil(textmeasure.CODE_LINE_HEIGHT * float64(t.FontSize*numTrailing)))
} else {
style := d2fonts.FONT_STYLE_REGULAR
if t.IsBold {

View file

@ -1272,9 +1272,9 @@ func drawShape(writer io.Writer, diagramHash string, targetShape d2target.Shape,
padding := float64(targetShape.FontSize) / 2.
fmt.Fprintf(writer, `<g transform="translate(%f %f)">`, padding, padding)
lineHeight := 1.3
lineHeight := textmeasure.CODE_LINE_HEIGHT
for index, tokens := range chroma.SplitTokensIntoLines(iterator.Tokens()) {
fmt.Fprintf(writer, "<text class=\"text-mono\" x=\"0\" y=\"%fem\">", 1+lineHeight*float64(index))
fmt.Fprintf(writer, "<text class=\"text-mono\" x=\"0\" y=\"%fem\">", 1+float64(index)*lineHeight)
for _, token := range tokens {
text := svgEscaper.Replace(token.String())
attr := styleAttr(svgStyles, token.Type)

View file

@ -18,6 +18,7 @@ import (
const TAB_SIZE = 4
const SIZELESS_FONT_SIZE = 0
const CODE_LINE_HEIGHT = 1.3
// ASCII is a set of all ASCII runes. These runes are codepoints from 32 to 127 inclusive.
var ASCII []rune