Skip to content

Commit 7eff956

Browse files
alecthomasclaude
andcommitted
fix: correct SVG @font-face CSS syntax and MIME types
A stray trailing apostrophe after the src declaration made the embedded-font CSS invalid, and the data URI used the obsolete application/x-font-* MIME types. fontFormats now pairs each format's modern MIME type (font/woff, font/woff2, font/ttf) with its CSS format() hint, which differ for TrueType. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent 180daf7 commit 7eff956

2 files changed

Lines changed: 35 additions & 6 deletions

File tree

formatters/svg/svg.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -157,21 +157,22 @@ const (
157157
TRUETYPE
158158
)
159159

160-
var fontFormats = [...]string{
161-
"woff",
162-
"woff2",
163-
"truetype",
160+
// The CSS format() hint differs from the MIME type for TrueType.
161+
var fontFormats = [...]struct{ mime, format string }{
162+
{"font/woff", "woff"},
163+
{"font/woff2", "woff2"},
164+
{"font/ttf", "truetype"},
164165
}
165166

166167
func (f *Formatter) writeFontStyle(w io.Writer) {
167168
fmt.Fprintf(w, `<style>
168169
@font-face {
169170
font-family: '%s';
170-
src: url(data:application/x-font-%s;charset=utf-8;base64,%s) format('%s');'
171+
src: url(data:%s;charset=utf-8;base64,%s) format('%s');
171172
font-weight: normal;
172173
font-style: normal;
173174
}
174-
</style>`, f.fontFamily, fontFormats[f.fontFormat], f.embeddedFont, fontFormats[f.fontFormat])
175+
</style>`, f.fontFamily, fontFormats[f.fontFormat].mime, f.embeddedFont, fontFormats[f.fontFormat].format)
175176
}
176177

177178
func (f *Formatter) styleAttr(styles map[chroma.TokenType]string, tt chroma.TokenType) string {

formatters/svg/svg_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package svg
2+
3+
import (
4+
"strings"
5+
"testing"
6+
7+
assert "github.com/alecthomas/assert/v2"
8+
)
9+
10+
func TestWriteFontStyle(t *testing.T) {
11+
tests := []struct {
12+
name string
13+
format FontFormat
14+
expected string
15+
}{
16+
{"WOFF", WOFF, "src: url(data:font/woff;charset=utf-8;base64,AAAA) format('woff');\n"},
17+
{"WOFF2", WOFF2, "src: url(data:font/woff2;charset=utf-8;base64,AAAA) format('woff2');\n"},
18+
{"TrueType", TRUETYPE, "src: url(data:font/ttf;charset=utf-8;base64,AAAA) format('truetype');\n"},
19+
}
20+
for _, test := range tests {
21+
t.Run(test.name, func(t *testing.T) {
22+
f := New(EmbedFont("Test", "AAAA", test.format))
23+
w := &strings.Builder{}
24+
f.writeFontStyle(w)
25+
assert.Contains(t, w.String(), test.expected)
26+
})
27+
}
28+
}

0 commit comments

Comments
 (0)