d2renderers: d2fonts: add bold + italic styles for Source Code Pro

👋 I've been playing around with changing the entire font of a diagram to something fixed-width, starting by hand-editing `main.go`

I noticed that if I switched over the family to Source Code Pro, d2 crashed with the following stack:

```
[23:38:30] info: compiling GetUniqueColumnName-fix.d2...
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x1c4 pc=0x16b562c]

goroutine 43 [running]:
github.com/golang/freetype/truetype.(*Font).Bounds(0xc000669670?, 0x0?)
        /Users/kevin/dev/go/pkg/mod/github.com/golang/freetype@v0.0.0-20170609003504-e2365dfdc4a0/truetype/truetype.go:378 +0xc
github.com/golang/freetype/truetype.NewFace(0x0, 0xc0003276c0)
        /Users/kevin/dev/go/pkg/mod/github.com/golang/freetype@v0.0.0-20170609003504-e2365dfdc4a0/truetype/face.go:199 +0x325
oss.terrastruct.com/d2/lib/textmeasure.(*Ruler).addFontSize(0xc00037c000, {{0x1bb9a34, 0xd}, {0x1b82732, 0x4}, 0x1f})
        /Users/kevin/dev/d2/lib/textmeasure/textmeasure.go:141 +0x112
oss.terrastruct.com/d2/lib/textmeasure.(*Ruler).MeasurePrecise(0xc00037c000, {{0x1bb9a34, 0xd}, {0x1b82732, 0x4}, 0x1f}, {0xc000582100, 0x1f})
        /Users/kevin/dev/d2/lib/textmeasure/textmeasure.go:157 +0xde
oss.terrastruct.com/d2/lib/textmeasure.(*Ruler).Measure(...)
        /Users/kevin/dev/d2/lib/textmeasure/textmeasure.go:151
oss.terrastruct.com/d2/d2graph.GetTextDimensions({0x0, 0x0, 0xc000657860?}, 0xc00037c000, 0xc000620280, 0xc000092020)
```

Which I tracked down to these missing styles, which appear to matter even if they aren't used.

I acquired the `ttf` files from https://fonts.google.com/specimen/Source+Code+Pro

I created the encoded fonts on my Mac with:
```
base64 -i SourceCodePro-Italic.ttf -o ../encoded/SourceCodePro-Italic.txt
```

Hopefully this is correct! Open to all feedback, especially since I think this is the first change I've ever made to a go program :)

Signed-off-by: Kevin David <kevin-david@github.com>
This commit is contained in:
Kevin David 2023-01-23 00:13:08 -05:00
parent 13d202b84b
commit bd7e18395e
No known key found for this signature in database
6 changed files with 39 additions and 1 deletions

View file

@ -78,6 +78,12 @@ var sourceSansProItalicBase64 string
//go:embed encoded/SourceCodePro-Regular.txt
var sourceCodeProRegularBase64 string
//go:embed encoded/SourceCodePro-Bold.txt
var sourceCodeProBoldBase64 string
//go:embed encoded/SourceCodePro-Italic.txt
var sourceCodeProItalicBase64 string
//go:embed encoded/ArchitectsDaughter-Regular.txt
var architectsDaughterRegularBase64 string
@ -108,6 +114,14 @@ func init() {
Family: SourceCodePro,
Style: FONT_STYLE_REGULAR,
}: sourceCodeProRegularBase64,
{
Family: SourceCodePro,
Style: FONT_STYLE_BOLD,
}: sourceCodeProBoldBase64,
{
Family: SourceCodePro,
Style: FONT_STYLE_ITALIC,
}: sourceCodeProItalicBase64,
{
Family: HandDrawn,
Style: FONT_STYLE_REGULAR,
@ -144,6 +158,22 @@ func init() {
Family: SourceCodePro,
Style: FONT_STYLE_REGULAR,
}] = b
b, err = fontFacesFS.ReadFile("ttf/SourceCodePro-Bold.ttf")
if err != nil {
panic(err)
}
FontFaces[Font{
Family: SourceCodePro,
Style: FONT_STYLE_BOLD,
}] = b
b, err = fontFacesFS.ReadFile("ttf/SourceCodePro-Italic.ttf")
if err != nil {
panic(err)
}
FontFaces[Font{
Family: SourceCodePro,
Style: FONT_STYLE_ITALIC,
}] = b
b, err = fontFacesFS.ReadFile("ttf/SourceSansPro-Bold.ttf")
if err != nil {
panic(err)

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

Binary file not shown.

Binary file not shown.

View file

@ -4,6 +4,7 @@
package textmeasure
import (
"fmt"
"math"
"unicode"
"unicode/utf8"
@ -138,7 +139,12 @@ func NewRuler() (*Ruler, error) {
func (r *Ruler) addFontSize(font d2fonts.Font) {
sizeless := font
sizeless.Size = 0
face := truetype.NewFace(r.ttfs[sizeless], &truetype.Options{
ttf := r.ttfs[sizeless]
if ttf == nil {
panic(fmt.Errorf("sizeless font %s not found with style %s", font.Family, font.Style))
}
face := truetype.NewFace(ttf, &truetype.Options{
Size: float64(font.Size),
})
atlas := NewAtlas(face, ASCII)