Skip to content

Commit 73cb83c

Browse files
committed
--legacy option
1 parent dd11d1d commit 73cb83c

File tree

8 files changed

+92
-54
lines changed

8 files changed

+92
-54
lines changed

CommandLine/CommandLine.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ Available keys for --format sfsymbol:
9494
--ultralightInsets alignment of ultralight variant: top,left,bottom,right | auto
9595
--black svg file of black variant
9696
--blackInsets alignment of black variant: top,left,bottom,right | auto
97+
--legacy use the original, less precise alignment logic from earlier swiftdraw versions.
9798
9899
99100
""")

DOM/Sources/Parser.XML.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ package struct XMLParser {
4949
self.rawValue = rawValue
5050
}
5151

52-
package static let skipInvalidAttributes = Options(rawValue: 1)
53-
package static let skipInvalidElements = Options(rawValue: 2)
52+
package static let skipInvalidAttributes = Options(rawValue: 1 << 0)
53+
package static let skipInvalidElements = Options(rawValue: 1 << 1)
5454
}
5555

5656
package init(options: Options = [], filename: String? = nil) {

SwiftDraw/Sources/CommandLine/CommandLine+Process.swift

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,19 @@ public extension CommandLine {
5151
precision: config.precision ?? 2)
5252
return code.data(using: .utf8)!
5353
case .sfsymbol:
54-
let renderer = SFSymbolRenderer(options: config.options,
55-
insets: config.insets,
56-
insetsUltralight: config.insetsUltralight ?? config.insets,
57-
insetsBlack: config.insetsBlack ?? config.insets,
58-
precision: config.precision ?? 3)
59-
let svg = try renderer.render(regular: config.input,
60-
ultralight: config.inputUltralight,
61-
black: config.inputBlack)
54+
let renderer = SFSymbolRenderer(
55+
options: config.options,
56+
insets: config.insets,
57+
insetsUltralight: config.insetsUltralight ?? config.insets,
58+
insetsBlack: config.insetsBlack ?? config.insets,
59+
precision: config.precision ?? 3,
60+
isLegacyInsets: config.isLegacyInsetsEnabled
61+
)
62+
let svg = try renderer.render(
63+
regular: config.input,
64+
ultralight: config.inputUltralight,
65+
black: config.inputBlack
66+
)
6267
return svg.data(using: .utf8)!
6368
case .jpeg, .pdf, .png:
6469
#if canImport(CoreGraphics)

SwiftDraw/Sources/CommandLine/CommandLine.Arguments.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,15 @@ extension CommandLine {
4646
case black
4747
case blackInsets
4848
case hideUnsupportedFilters
49+
case legacy
4950

5051
var hasValue: Bool {
51-
self != .hideUnsupportedFilters
52+
switch self {
53+
case .hideUnsupportedFilters, .legacy:
54+
return false
55+
default:
56+
return true
57+
}
5258
}
5359
}
5460

SwiftDraw/Sources/CommandLine/CommandLine.Configuration.swift

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ extension CommandLine {
4848
public var scale: Scale
4949
public var options: SVG.Options
5050
public var precision: Int?
51+
public var isLegacyInsetsEnabled: Bool
5152
}
5253

5354
public enum Format: String {
@@ -119,19 +120,22 @@ extension CommandLine {
119120

120121
let options = try parseOptions(from: modifiers)
121122
let result = source.newURL(for: format, scale: scale)
122-
return Configuration(input: source,
123-
inputUltralight: ultralight,
124-
inputBlack: black,
125-
output: output ?? result,
126-
format: format,
127-
size: size,
128-
api: api,
129-
insets: insets,
130-
insetsUltralight: ultralightInsets,
131-
insetsBlack: blackInsets,
132-
scale: scale,
133-
options: options,
134-
precision: precision)
123+
return Configuration(
124+
input: source,
125+
inputUltralight: ultralight,
126+
inputBlack: black,
127+
output: output ?? result,
128+
format: format,
129+
size: size,
130+
api: api,
131+
insets: insets,
132+
insetsUltralight: ultralightInsets,
133+
insetsBlack: blackInsets,
134+
scale: scale,
135+
options: options,
136+
precision: precision,
137+
isLegacyInsetsEnabled: modifiers.keys.contains(.legacy)
138+
)
135139
}
136140

137141
static func parseFileURL(file: String, within directory: URL) throws -> URL {

SwiftDraw/Sources/LayerTree/LayerTree.CommandOptimizer.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,8 @@ struct OptimizerOptions: OptionSet {
144144
self.rawValue = rawValue
145145
}
146146

147-
static let skipRedundantState = OptimizerOptions(rawValue: 1)
148-
static let skipInitialSaveState = OptimizerOptions(rawValue: 2)
147+
static let skipRedundantState = OptimizerOptions(rawValue: 1 << 0)
148+
static let skipInitialSaveState = OptimizerOptions(rawValue: 1 << 1)
149149
}
150150

151151
extension RendererCommand {

SwiftDraw/Sources/Renderer/Renderer.SFSymbol.swift

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,23 @@ public struct SFSymbolRenderer {
3939
private let insetsUltralight: CommandLine.Insets
4040
private let insetsBlack: CommandLine.Insets
4141
private let formatter: CoordinateFormatter
42+
private let isLegacyInsets: Bool
4243

4344
public init(options: SVG.Options,
4445
insets: CommandLine.Insets,
4546
insetsUltralight: CommandLine.Insets,
4647
insetsBlack: CommandLine.Insets,
47-
precision: Int) {
48+
precision: Int,
49+
isLegacyInsets: Bool) {
4850
self.options = options
4951
self.insets = insets
5052
self.insetsUltralight = insetsUltralight
5153
self.insetsBlack = insetsBlack
52-
self.formatter = CoordinateFormatter(delimeter: .comma,
53-
precision: .capped(max: precision))
54+
self.formatter = CoordinateFormatter(
55+
delimeter: .comma,
56+
precision: .capped(max: precision)
57+
)
58+
self.isLegacyInsets = isLegacyInsets
5459
}
5560

5661
public func render(regular: URL, ultralight: URL?, black: URL?) throws -> String {
@@ -68,25 +73,25 @@ public struct SFSymbolRenderer {
6873

6974
template.svg.styles = image.styles.map(makeSymbolStyleSheet)
7075

71-
let boundsRegular = try makeBounds(svg: image, auto: Self.makeAutoBounds(for: pathsRegular), for: .regular)
72-
template.regular.appendPaths(pathsRegular, from: boundsRegular)
76+
let boundsRegular = try makeBounds(svg: image, auto: Self.makeAutoBounds(for: pathsRegular, isLegacy: isLegacyInsets), for: .regular)
77+
template.regular.appendPaths(pathsRegular, from: boundsRegular, isLegacy: isLegacyInsets)
7378

7479
if let ultralight = ultralight,
7580
let paths = Self.getPaths(for: ultralight) {
76-
let bounds = try makeBounds(svg: ultralight, isRegularSVG: false, auto: Self.makeAutoBounds(for: paths), for: .ultralight)
77-
template.ultralight.appendPaths(paths, from: bounds)
81+
let bounds = try makeBounds(svg: ultralight, isRegularSVG: false, auto: Self.makeAutoBounds(for: paths, isLegacy: isLegacyInsets), for: .ultralight)
82+
template.ultralight.appendPaths(paths, from: bounds, isLegacy: isLegacyInsets)
7883
} else {
79-
let bounds = try makeBounds(svg: image, auto: Self.makeAutoBounds(for: pathsRegular), for: .ultralight)
80-
template.ultralight.appendPaths(pathsRegular, from: bounds)
84+
let bounds = try makeBounds(svg: image, auto: Self.makeAutoBounds(for: pathsRegular, isLegacy: isLegacyInsets), for: .ultralight)
85+
template.ultralight.appendPaths(pathsRegular, from: bounds, isLegacy: isLegacyInsets)
8186
}
8287

8388
if let black = black,
8489
let paths = Self.getPaths(for: black) {
85-
let bounds = try makeBounds(svg: black, isRegularSVG: false, auto: Self.makeAutoBounds(for: paths), for: .black)
86-
template.black.appendPaths(paths, from: bounds)
90+
let bounds = try makeBounds(svg: black, isRegularSVG: false, auto: Self.makeAutoBounds(for: paths, isLegacy: isLegacyInsets), for: .black)
91+
template.black.appendPaths(paths, from: bounds, isLegacy: isLegacyInsets)
8792
} else {
88-
let bounds = try makeBounds(svg: image, auto: Self.makeAutoBounds(for: pathsRegular), for: .black)
89-
template.black.appendPaths(pathsRegular, from: bounds)
93+
let bounds = try makeBounds(svg: image, auto: Self.makeAutoBounds(for: pathsRegular, isLegacy: isLegacyInsets), for: .black)
94+
template.black.appendPaths(pathsRegular, from: bounds, isLegacy: isLegacyInsets)
9095
}
9196

9297
let element = try XML.Formatter.SVG(formatter: formatter).makeElement(from: template.svg)
@@ -257,7 +262,7 @@ extension SFSymbolRenderer {
257262
#endif
258263
}
259264

260-
static func makeAutoBounds(for paths: [SymbolPath]) -> LayerTree.Rect {
265+
static func makeAutoBounds(for paths: [SymbolPath], isLegacy: Bool = false) -> LayerTree.Rect {
261266
var min = LayerTree.Point.maximum
262267
var max = LayerTree.Point.minimum
263268
for p in paths {
@@ -266,8 +271,10 @@ extension SFSymbolRenderer {
266271
max = max.maximum(combining: .init(bounds.maxX, bounds.maxY))
267272
}
268273

269-
min.x -= 10
270-
max.x += 10
274+
if !isLegacy {
275+
min.x -= 10
276+
max.x += 10
277+
}
271278

272279
return LayerTree.Rect(
273280
x: min.x,
@@ -516,7 +523,7 @@ private extension ContainerElement {
516523

517524
private extension SFSymbolTemplate.Variant {
518525

519-
mutating func appendPaths(_ paths: [SFSymbolRenderer.SymbolPath], from source: LayerTree.Rect) {
526+
mutating func appendPaths(_ paths: [SFSymbolRenderer.SymbolPath], from source: LayerTree.Rect, isLegacy: Bool = false) {
520527
let matrix = SFSymbolRenderer.makeTransformation(from: source, to: bounds)
521528
contents.paths = paths
522529
.map {
@@ -527,9 +534,16 @@ private extension SFSymbolTemplate.Variant {
527534
}
528535

529536
let midX = bounds.midX
530-
let newWidth = ((source.width * matrix.a) / 2)
531-
left.x = midX - newWidth
532-
right.x = midX + newWidth
537+
if isLegacy {
538+
// preserve behaviour from earlier SwiftDraw versions with --legacy option
539+
let newWidth = ((source.width * matrix.a) / 2) + 10
540+
left.x = min(left.x, midX - newWidth)
541+
right.x = max(right.x, midX + newWidth)
542+
} else {
543+
let newWidth = ((source.width * matrix.a) / 2)
544+
left.x = midX - newWidth
545+
right.x = midX + newWidth
546+
}
533547
}
534548
}
535549

SwiftDraw/Tests/Renderer/Renderer.SFSymbolTests.swift

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -187,18 +187,26 @@ private extension DOM.SVG {
187187
private extension SFSymbolRenderer {
188188

189189
static func render(fileURL: URL) throws -> String {
190-
let renderer = SFSymbolRenderer(options: [], insets: .init(),
191-
insetsUltralight: .init(),
192-
insetsBlack: .init(),
193-
precision: 3)
190+
let renderer = SFSymbolRenderer(
191+
options: [],
192+
insets: .init(),
193+
insetsUltralight: .init(),
194+
insetsBlack: .init(),
195+
precision: 3,
196+
isLegacyInsets: false
197+
)
194198
return try renderer.render(regular: fileURL, ultralight: nil, black: nil)
195199
}
196200

197201
static func render(svg: DOM.SVG) throws -> String {
198-
let renderer = SFSymbolRenderer(options: [], insets: .init(),
199-
insetsUltralight: .init(),
200-
insetsBlack: .init(),
201-
precision: 3)
202+
let renderer = SFSymbolRenderer(
203+
options: [],
204+
insets: .init(),
205+
insetsUltralight: .init(),
206+
insetsBlack: .init(),
207+
precision: 3,
208+
isLegacyInsets: false
209+
)
202210
return try renderer.render(default: svg, ultralight: nil, black: nil)
203211
}
204212
}

0 commit comments

Comments
 (0)