Skip to content

Commit 8dfd9cd

Browse files
committed
Localized fix for LinearGradients that reference other LinearGradients.
1 parent f09a8c4 commit 8dfd9cd

File tree

6 files changed

+100
-23
lines changed

6 files changed

+100
-23
lines changed

Examples/Basic/Basic.xcodeproj/project.pbxproj

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
/* Begin PBXBuildFile section */
1010
D90523452217927F00B3B94F /* mystery.svg in Resources */ = {isa = PBXBuildFile; fileRef = D90523442217927F00B3B94F /* mystery.svg */; };
11+
D9126F0E24A6C19300859CC5 /* gradient-apple.svg in Resources */ = {isa = PBXBuildFile; fileRef = D9126F0D24A6C19200859CC5 /* gradient-apple.svg */; };
1112
D918D26D22122A7300097C9A /* arc2.svg in Resources */ = {isa = PBXBuildFile; fileRef = D918D26C22122A7300097C9A /* arc2.svg */; };
1213
D91C3E43224ED9A4001BCDAE /* gradient-gratification.svg in Resources */ = {isa = PBXBuildFile; fileRef = D91C3E42224ED9A4001BCDAE /* gradient-gratification.svg */; };
1314
D945F5AA225220F900FAAAC4 /* mask-composite-2.svg in Resources */ = {isa = PBXBuildFile; fileRef = D945F5A8225220F900FAAAC4 /* mask-composite-2.svg */; };
@@ -55,6 +56,7 @@
5556

5657
/* Begin PBXFileReference section */
5758
D90523442217927F00B3B94F /* mystery.svg */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = mystery.svg; sourceTree = "<group>"; };
59+
D9126F0D24A6C19200859CC5 /* gradient-apple.svg */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = "gradient-apple.svg"; sourceTree = "<group>"; };
5860
D918D26C22122A7300097C9A /* arc2.svg */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = arc2.svg; sourceTree = "<group>"; };
5961
D91C3E42224ED9A4001BCDAE /* gradient-gratification.svg */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = "gradient-gratification.svg"; sourceTree = "<group>"; };
6062
D945F5A8225220F900FAAAC4 /* mask-composite-2.svg */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = "mask-composite-2.svg"; sourceTree = "<group>"; };
@@ -133,31 +135,32 @@
133135
D9ACD782220FDDE0009717CF /* Samples */ = {
134136
isa = PBXGroup;
135137
children = (
138+
D9ACD78D220FDDE0009717CF /* arc.svg */,
139+
D918D26C22122A7300097C9A /* arc2.svg */,
136140
D945F5AC225241A100FAAAC4 /* blend.svg */,
137-
D945F5A8225220F900FAAAC4 /* mask-composite-2.svg */,
138-
D945F5A9225220F900FAAAC4 /* mask-composite-3.svg */,
139-
D98375B522500FCC00974D71 /* mask-composite.svg */,
141+
D9ACD788220FDDE0009717CF /* curves.svg */,
142+
D9ACD78F220FDDE0009717CF /* display.svg */,
143+
D9126F0D24A6C19200859CC5 /* gradient-apple.svg */,
140144
D91C3E42224ED9A4001BCDAE /* gradient-gratification.svg */,
141145
D9494F45224C99950015ACE1 /* gradient.svg */,
142146
D9FD69D7224CD4960046A926 /* gradient2.svg */,
143-
D9494F43224C846D0015ACE1 /* thats-no-moon.svg */,
144-
D90523442217927F00B3B94F /* mystery.svg */,
145-
D9ACD783220FDDE0009717CF /* use.svg */,
146-
D9ACD784220FDDE0009717CF /* path.svg */,
147-
D9ACD785220FDDE0009717CF /* viewbox.svg */,
148-
D9ACD786220FDDE0009717CF /* transform.svg */,
149-
D9ACD788220FDDE0009717CF /* curves.svg */,
150147
D956E1B022103A0C00C5ADA2 /* identity.svg */,
151-
D9ACD789220FDDE0009717CF /* shapes.svg */,
148+
D9ACD790220FDDE0009717CF /* invalid.svg */,
152149
D9ACD78A220FDDE0009717CF /* lines.svg */,
153-
D9ACD78B220FDDE0009717CF /* starry.svg */,
154-
D9D9EB2C224A3BCA006CF83C /* pattern.svg */,
150+
D945F5A8225220F900FAAAC4 /* mask-composite-2.svg */,
151+
D945F5A9225220F900FAAAC4 /* mask-composite-3.svg */,
152+
D98375B522500FCC00974D71 /* mask-composite.svg */,
155153
D9ACD78C220FDDE0009717CF /* mask.svg */,
156-
D9ACD78D220FDDE0009717CF /* arc.svg */,
157-
D918D26C22122A7300097C9A /* arc2.svg */,
154+
D90523442217927F00B3B94F /* mystery.svg */,
155+
D9ACD784220FDDE0009717CF /* path.svg */,
156+
D9D9EB2C224A3BCA006CF83C /* pattern.svg */,
158157
D9ACD78E220FDDE0009717CF /* quad.svg */,
159-
D9ACD78F220FDDE0009717CF /* display.svg */,
160-
D9ACD790220FDDE0009717CF /* invalid.svg */,
158+
D9ACD789220FDDE0009717CF /* shapes.svg */,
159+
D9ACD78B220FDDE0009717CF /* starry.svg */,
160+
D9494F43224C846D0015ACE1 /* thats-no-moon.svg */,
161+
D9ACD786220FDDE0009717CF /* transform.svg */,
162+
D9ACD783220FDDE0009717CF /* use.svg */,
163+
D9ACD785220FDDE0009717CF /* viewbox.svg */,
161164
);
162165
name = Samples;
163166
path = ../../Samples;
@@ -231,6 +234,7 @@
231234
buildActionMask = 2147483647;
232235
files = (
233236
D9ACD79E220FDDE0009717CF /* invalid.svg in Resources */,
237+
D9126F0E24A6C19300859CC5 /* gradient-apple.svg in Resources */,
234238
D98375B622500FCC00974D71 /* mask-composite.svg in Resources */,
235239
D9ACD796220FDDE0009717CF /* curves.svg in Resources */,
236240
D9ACD79C220FDDE0009717CF /* quad.svg in Resources */,

Examples/Basic/Sources/ViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class ViewController: UIViewController {
6565

6666
override func loadView() {
6767
let imageView = UIImageView(frame: UIScreen.main.bounds)
68-
imageView.image = Image(named: "arc.svg")?.pdfImage()
68+
imageView.image = Image(named: "gradient-apple.svg")?.pdfImage()
6969
imageView.contentMode = .scaleAspectFit
7070
imageView.backgroundColor = .white
7171
self.view = imageView

Samples/gradient-apple.svg

Lines changed: 58 additions & 0 deletions
Loading

SwiftDraw/DOM.Gradient.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ extension DOM {
4040
var y2: Coordinate?
4141

4242
var stops: [Stop]
43-
43+
44+
//references another LinearGradient element id within defs
45+
var href: URL?
46+
4447
init(id: String) {
4548
self.id = id
4649
self.stops = []

SwiftDraw/LayerTree.Builder.swift

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,14 +206,24 @@ extension LayerTree.Builder {
206206
}
207207

208208
let gradient = LayerTree.Gradient(start: Point(x1, y1), end: Point(x2, y2))
209-
gradient.stops = element.stops.map {
210-
return LayerTree.Gradient.Stop(offset: $0.offset,
211-
color: LayerTree.Color($0.color),
212-
opacity: $0.opacity)
209+
if let id = element.href?.fragment,
210+
let reference = svg.defs.linearGradients.first(where: { $0.id == id }) {
211+
gradient.stops = makeGradientStops(for: reference)
212+
} else {
213+
gradient.stops = makeGradientStops(for: element)
213214
}
215+
214216
return gradient
215217
}
216218

219+
func makeGradientStops(for element: DOM.LinearGradient) -> [LayerTree.Gradient.Stop] {
220+
return element.stops.map {
221+
LayerTree.Gradient.Stop(offset: $0.offset,
222+
color: LayerTree.Color($0.color),
223+
opacity: $0.opacity)
224+
}
225+
}
226+
217227
//current state of the render tree, updated as builder traverses child nodes
218228
struct State {
219229
var opacity: DOM.Float

SwiftDraw/Parser.XML.Gradient.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ extension XMLParser {
6060
let att: AttributeParser = try parseAttributes(n)
6161
node.stops.append(try parseLinearGradientStop(att))
6262
}
63+
64+
node.href = try? nodeAtt.parseUrl("xlink:href")
6365

6466
return node
6567
}

0 commit comments

Comments
 (0)