@@ -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
517524private 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
0 commit comments