Auto-generate SF Symbol weight variants from stroke-width scaling#125
Open
rursache wants to merge 1 commit intoswhitty:mainfrom
Open
Auto-generate SF Symbol weight variants from stroke-width scaling#125rursache wants to merge 1 commit intoswhitty:mainfrom
rursache wants to merge 1 commit intoswhitty:mainfrom
Conversation
Adds --ultralight-stroke-width and --black-stroke-width flags to the sfsymbol format. When set without an explicit variant SVG, SwiftDraw clones the regular DOM, multiplies every stroke-width value by the scale (decimal or percent syntax), then runs that mutated DOM through the existing variant pipeline. Explicit --ultralight or --black always wins, with a warning when both are supplied. Fixes swhitty#38
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #125 +/- ##
==========================================
+ Coverage 89.80% 89.96% +0.16%
==========================================
Files 159 161 +2
Lines 15868 16181 +313
==========================================
+ Hits 14251 14558 +307
- Misses 1617 1623 +6 ☔ 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.
Fixes #38
Summary
Adds two new flags to the
sfsymbolformat that synthesize the ultralight and black weight variants directly from the regular SVG by scaling everystroke-widthvalue before strokes get expanded into outlines:--ultralight-stroke-width <scale>— typically0.5or50%--black-stroke-width <scale>— typically2.0or200%Both decimal (
0.5) and percent (50%) syntax are accepted. The existing--ultralight <file>and--black <file>flags still work and take precedence; a warning is printed when both are supplied.Implementation
SFSymbolRenderer.StrokeWidthScale: small value type with a permissive parser (rejects0, negative, malformed, empty)StrokeWidthScaler: walks aDOM.SVGand multiplies stroke-width onattributesstyle<style>rules in everyStyleSheetRecurses through groups, clipPath/mask/pattern containers in
defs, and thedefs.elementsdictionary. Returns the count of values mutated so the caller can warn when no strokes were foundSFSymbolRenderer.render(regular:ultralight:black:): when a scale is supplied without an explicit variant URL, re-parses the regular file (DOM nodes are reference types so a fresh parse is the cleanest deep clone), runs the scaler, and feeds the mutated DOM into the existing variant pipeline. No path/layer/codegen changes were neededModifiercases,parseStrokeScalehelper, threading throughConfigurationandprocessImage, updated--helptextstroke-widthis scaled today (future work could cover dash arrays and similar)Output emits these warnings to
stderr:--ultralight-stroke-width has no effect: source SVG has no stroke-width values.explicit --ultralight overrides --ultralight-stroke-width.Tests
RendererSFSymbolStrokeScaleTests(18 tests) covers the parser, the walker against each of the three stroke-width sources individually plus a mixed fixture, nested group recursion, clipPath children, the no-strokes case, and end-to-end runs throughSFSymbolRendererconfirming the variant paths actually differ from regular when scaled.CommandLineConfigurationTestsadds three tests for the new flags (valid decimal, valid percent, invalid value rejection).Five new fixtures cover the stroke sources independently:
stroke-attribute.svgstroke-inline-style.svgstyle=stroke-stylesheet.svg<style>selectorstroke-mixed.svgstroke-none.svgFull suite:
swift test— 217 tests pass, 0 failures.Test plan
swift testpasses locallyswiftdraw key.svg --format sfsymbol --ultralight-stroke-width 50% --black-stroke-width 2.0produces a valid template--ultralightand--ultralight-stroke-widthare both supplied