-
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 all commits
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
Some comments aren't visible on the classic Files Changed page.
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
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,70 @@ | ||
| // SPDX-License-Identifier: Unlicense OR BSD-3-Clause | ||
|
|
||
| package font | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| tu "github.com/go-text/typesetting/testutils" | ||
| ) | ||
|
|
||
| func TestSVGViewBox(t *testing.T) { | ||
| const upem = 1000 | ||
| for _, test := range []struct { | ||
| doc string | ||
| expected SVGViewBox | ||
| }{ | ||
| // no viewport information: default to the em square | ||
| {`<svg xmlns="http://www.w3.org/2000/svg" id="glyph1"></svg>`, SVGViewBox{0, 0, upem, upem}}, | ||
| {``, SVGViewBox{0, 0, upem, upem}}, | ||
| {`not xml`, SVGViewBox{0, 0, upem, upem}}, | ||
| {`<svg`, SVGViewBox{0, 0, upem, upem}}, // malformed | ||
| // the root element must be an svg element, in the SVG namespace or none | ||
| {`<html viewBox="0 0 128 128"></html>`, SVGViewBox{0, 0, upem, upem}}, | ||
| {`<svg xmlns="http://example.com/wrong" viewBox="0 0 128 128"></svg>`, SVGViewBox{0, 0, upem, upem}}, | ||
| {`<w:svg xmlns:w="http://example.com/wrong" viewBox="0 0 128 128"></w:svg>`, SVGViewBox{0, 0, upem, upem}}, | ||
| {`<svg:svg xmlns:svg="http://www.w3.org/2000/svg" viewBox="0 0 128 128"></svg:svg>`, SVGViewBox{0, 0, 128, 128}}, | ||
| // viewBox attribute (a missing namespace is tolerated) | ||
| {`<svg viewBox="0 0 128 128"></svg>`, SVGViewBox{0, 0, 128, 128}}, | ||
| {`<svg viewBox="-10,-20 30,40"></svg>`, SVGViewBox{-10, -20, 30, 40}}, | ||
| {`<svg viewBox=" 0 , 0 , 1e2 , 50.5 "></svg>`, SVGViewBox{0, 0, 100, 50.5}}, | ||
| {`<svg viewBox='0 0 128 64'/>`, SVGViewBox{0, 0, 128, 64}}, | ||
| // invalid viewBox attributes | ||
| {`<svg viewBox="0 0 128"></svg>`, SVGViewBox{0, 0, upem, upem}}, | ||
| {`<svg viewBox="0 0 0 128"></svg>`, SVGViewBox{0, 0, upem, upem}}, | ||
| {`<svg viewBox="0 0 128 -1"></svg>`, SVGViewBox{0, 0, upem, upem}}, | ||
| {`<svg viewBox="a b c d"></svg>`, SVGViewBox{0, 0, upem, upem}}, | ||
| {`<svg viewBox="NaN 0 128 128"></svg>`, SVGViewBox{0, 0, upem, upem}}, | ||
| {`<svg viewBox="0 0 Inf 128"></svg>`, SVGViewBox{0, 0, upem, upem}}, | ||
| {`<svg viewBox="0 0 128 +infinity"></svg>`, SVGViewBox{0, 0, upem, upem}}, | ||
| // width and height attributes | ||
| {`<svg width="100" height="200"></svg>`, SVGViewBox{0, 0, 100, 200}}, | ||
| {`<svg width="100px" height="50px"></svg>`, SVGViewBox{0, 0, 100, 50}}, | ||
| {`<svg width="100"></svg>`, SVGViewBox{0, 0, upem, upem}}, | ||
| {`<svg width="100%" height="100%"></svg>`, SVGViewBox{0, 0, upem, upem}}, | ||
| {`<svg width="12pt" height="12pt"></svg>`, SVGViewBox{0, 0, upem, upem}}, | ||
| {`<svg width="NaN" height="NaN"></svg>`, SVGViewBox{0, 0, upem, upem}}, | ||
| {`<svg width="Inf" height="Inf"></svg>`, SVGViewBox{0, 0, upem, upem}}, | ||
| // viewBox has precedence over width and height | ||
| {`<svg width="100" height="200" viewBox="0 0 128 128"></svg>`, SVGViewBox{0, 0, 128, 128}}, | ||
| {`<svg viewBox="invalid" width="100" height="200"></svg>`, SVGViewBox{0, 0, 100, 200}}, | ||
| // content before the root element is skipped | ||
| {`<?xml version="1.0"?><!-- comment --><!DOCTYPE svg><svg viewBox="0 0 128 128"></svg>`, SVGViewBox{0, 0, 128, 128}}, | ||
| } { | ||
| got := svgViewBox([]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 | ||
| font := loadFont(t, "toys/chromacheck-svg.ttf") | ||
| face := Face{Font: font} | ||
|
|
||
| glyph, ok := face.GlyphDataSVG(1) | ||
| tu.Assert(t, ok) | ||
| tu.Assert(t, glyph.ViewBox == 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.