Resolve percent <svg> dimensions against --size and improve error#126
Open
rursache wants to merge 1 commit intoswhitty:mainfrom
Open
Resolve percent <svg> dimensions against --size and improve error#126rursache wants to merge 1 commit intoswhitty:mainfrom
rursache wants to merge 1 commit intoswhitty:mainfrom
Conversation
The root <svg> width/height attributes were rejected with a generic missingAttribute error whenever they used percent values (width="100%") or were omitted entirely without a viewBox. Per the SVG spec these need a viewport to resolve against, but SwiftDraw had no way for callers to provide one. This adds an opt-in viewport that XMLParser can resolve against: - XMLParser.Viewport: a width/height pair callers can set via the new XMLParser.defaultViewport property or threaded through DOM.SVG.parse(fileURL:options:defaultViewport:) / parse(data:options:defaultViewport:). - The CLI now passes --size as the default viewport when loading SVGs for png/jpeg/pdf output, so percent dimensions and missing dimensions resolve against the requested canvas. - A new XMLParser.Error.unresolvableDimension(reason:) replaces the generic missingAttribute for these cases. The reason string explains exactly what is missing (percent + no viewport, missing attribute + no viewBox/viewport) and how to provide it. Public SVG initializers and existing API surface are unchanged. Callers that do not opt in to a viewport now get the clearer error message in place of the previous one. Relates to swhitty#42
29b59bf to
1e8f3a6
Compare
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #126 +/- ##
==========================================
- Coverage 89.80% 89.76% -0.05%
==========================================
Files 159 159
Lines 15868 15961 +93
==========================================
+ Hits 14251 14327 +76
- Misses 1617 1634 +17 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Issue #42 covers SVG canvas units. PR #45 added support for absolute units on the root
<svg>width/height. Two cases still failed at parse time with a genericmissingAttributeerror:width=\"100%\", when no viewBox is presentwidth/heightentirely when no viewBox is presentPer the SVG spec these need a viewport to resolve against. SwiftDraw had no way for callers to supply one, so this PR adds an opt-in viewport and threads
--sizethrough the CLI so the existing flag becomes the user-visible answer.What changes
XMLParsergains anXMLParser.Viewportstruct and adefaultViewportpropertyDOM.SVG.parse(fileURL:options:defaultViewport:)andparse(data:options:defaultViewport:)accept the viewportconfig.sizeas the viewport when loading SVGs forpng/jpeg/pdfoutput, so--size 800x600 responsive.svgresolves percent dimensions and missing dimensions against the requested canvaswidth/heightare missing but a viewBox is declared (already common browser behavior)XMLParser.Error.unresolvableDimension(reason:)replacesmissingAttributefor these cases. The reason string explains exactly what is missing and how to fix it, eg:SVGinitializers and existing API surface are unchanged. Callers that do not opt in to a viewport now get the clearer error in place of the previous oneExamples
Before:
$ swiftdraw responsive.svg --format png --size 800x600 # fails: missingAttribute(\"width\")After:
$ swiftdraw responsive.svg --format png --size 800x600 # resolves width=\"100%\" against 800x600 and rendersSVGs that omit dimensions but provide a viewBox now also work without
--size:Test plan
DOM/Tests/Parser.SVGTests.swiftcovering: percent + viewport, percent without viewport (throws), missing dims + viewport, missing dims + viewBox, missing all (throwsunresolvableDimension), mixed percent/explicitparseSVGMissingHeightInvalidNode/parseSVGMissingWidthInvalidNodestill throw (unchanged behavior when no viewport is provided)swift test)swift buildcleanRelates to #42