-
Notifications
You must be signed in to change notification settings - Fork 27
[font] resolve the initial viewport of SVG glyphs #265
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
benoitkugler
merged 4 commits into
go-text:main
from
hajimehoshi:claude/stoic-goldberg-f9b94d
Jul 10, 2026
+191
−3
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9097d7e
[font] resolve the initial viewport of SVG glyphs
hajimehoshi c0a32f6
[font] address review feedback on the SVG viewport parsing
hajimehoshi ab9e1a2
[font] parse SVG glyph documents with encoding/xml
hajimehoshi 7d46363
[font] make the SVG viewport test internal to the package
hajimehoshi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| // SPDX-License-Identifier: Unlicense OR BSD-3-Clause | ||
|
|
||
| package font | ||
|
|
||
| // SVGDocViewBox exposes [svgViewBox] for tests. | ||
| func SVGDocViewBox(doc []byte, upem uint16) SVGViewBox { return svgViewBox(doc, upem) } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| // SPDX-License-Identifier: Unlicense OR BSD-3-Clause | ||
|
|
||
| package font_test | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "testing" | ||
|
|
||
| td "github.com/go-text/typesetting-utils/opentype" | ||
| "github.com/go-text/typesetting/font" | ||
| tu "github.com/go-text/typesetting/testutils" | ||
| ) | ||
|
|
||
| func TestSVGViewBox(t *testing.T) { | ||
| const upem = 1000 | ||
| for _, test := range []struct { | ||
| doc string | ||
| expected font.SVGViewBox | ||
| }{ | ||
| // no viewport information: default to the em square | ||
| {`<svg xmlns="http://www.w3.org/2000/svg" id="glyph1"></svg>`, font.SVGViewBox{0, 0, upem, upem}}, | ||
| {``, font.SVGViewBox{0, 0, upem, upem}}, | ||
| {`not xml`, font.SVGViewBox{0, 0, upem, upem}}, | ||
| {`<html></html>`, font.SVGViewBox{0, 0, upem, upem}}, | ||
| {`<svg`, font.SVGViewBox{0, 0, upem, upem}}, | ||
| // viewBox attribute | ||
| {`<svg viewBox="0 0 128 128"></svg>`, font.SVGViewBox{0, 0, 128, 128}}, | ||
| {`<svg viewBox="-10,-20 30,40"></svg>`, font.SVGViewBox{-10, -20, 30, 40}}, | ||
| {`<svg viewBox=" 0 , 0 , 1e2 , 50.5 "></svg>`, font.SVGViewBox{0, 0, 100, 50.5}}, | ||
| {`<svg viewBox='0 0 128 64'/>`, font.SVGViewBox{0, 0, 128, 64}}, | ||
| {"<svg\tviewBox\t=\r\n\"0 0 128 128\"></svg>", font.SVGViewBox{0, 0, 128, 128}}, | ||
| // invalid viewBox attributes | ||
| {`<svg viewBox="0 0 128"></svg>`, font.SVGViewBox{0, 0, upem, upem}}, | ||
| {`<svg viewBox="0 0 0 128"></svg>`, font.SVGViewBox{0, 0, upem, upem}}, | ||
| {`<svg viewBox="0 0 128 -1"></svg>`, font.SVGViewBox{0, 0, upem, upem}}, | ||
| {`<svg viewBox="a b c d"></svg>`, font.SVGViewBox{0, 0, upem, upem}}, | ||
| // width and height attributes | ||
|
hajimehoshi marked this conversation as resolved.
|
||
| {`<svg width="100" height="200"></svg>`, font.SVGViewBox{0, 0, 100, 200}}, | ||
| {`<svg width="100px" height="50px"></svg>`, font.SVGViewBox{0, 0, 100, 50}}, | ||
| {`<svg width="100"></svg>`, font.SVGViewBox{0, 0, upem, upem}}, | ||
| {`<svg width="100%" height="100%"></svg>`, font.SVGViewBox{0, 0, upem, upem}}, | ||
| {`<svg width="12pt" height="12pt"></svg>`, font.SVGViewBox{0, 0, upem, upem}}, | ||
|
hajimehoshi marked this conversation as resolved.
Outdated
|
||
| // viewBox has precedence over width and height | ||
| {`<svg width="100" height="200" viewBox="0 0 128 128"></svg>`, font.SVGViewBox{0, 0, 128, 128}}, | ||
| {`<svg viewBox="invalid" width="100" height="200"></svg>`, font.SVGViewBox{0, 0, 100, 200}}, | ||
| // prolog, comments and doctype before the root element | ||
| {"\uFEFF" + `<svg viewBox="0 0 128 128"></svg>`, font.SVGViewBox{0, 0, 128, 128}}, | ||
| {`<?xml version="1.0" encoding="UTF-8"?><svg viewBox="0 0 128 128"></svg>`, font.SVGViewBox{0, 0, 128, 128}}, | ||
| {`<?xml version="1.0"?> | ||
| <!-- comment with <svg viewBox="0 0 1 1"> inside --> | ||
| <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"> | ||
| <svg viewBox="0 0 128 128"></svg>`, font.SVGViewBox{0, 0, 128, 128}}, | ||
| {`<!DOCTYPE svg [ <!ENTITY foo "bar"> ]><svg viewBox="0 0 128 128"></svg>`, font.SVGViewBox{0, 0, 128, 128}}, | ||
| // namespace prefix on the root element | ||
| {`<svg:svg xmlns:svg="http://www.w3.org/2000/svg" viewBox="0 0 128 128"></svg:svg>`, font.SVGViewBox{0, 0, 128, 128}}, | ||
| // other attributes, possibly with tricky values | ||
| {`<svg xmlns="http://www.w3.org/2000/svg" title="a>b" viewBox="0 0 128 128" enable-background="new"></svg>`, font.SVGViewBox{0, 0, 128, 128}}, | ||
| } { | ||
| got := font.SVGDocViewBox([]byte(test.doc), upem) | ||
| if got != test.expected { | ||
| t.Fatalf("document %q: expected %v, got %v", test.doc, test.expected, got) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func TestGlyphDataSVGViewBox(t *testing.T) { | ||
| // this font has no viewBox attribute in its SVG document: | ||
| // the viewport defaults to the em square | ||
| data, err := td.Files.ReadFile("toys/chromacheck-svg.ttf") | ||
| tu.AssertNoErr(t, err) | ||
|
|
||
| face, err := font.ParseTTF(bytes.NewReader(data)) | ||
| tu.AssertNoErr(t, err) | ||
|
|
||
| glyph, ok := face.GlyphDataSVG(1) | ||
| tu.Assert(t, ok) | ||
| tu.Assert(t, glyph.ViewBox == font.SVGViewBox{0, 0, 1024, 1024}) | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.