Skip to content

Commit 4c6257f

Browse files
committed
Font as common GraphicsElement
1 parent 417629a commit 4c6257f

File tree

8 files changed

+49
-18
lines changed

8 files changed

+49
-18
lines changed

Examples/Basic.xcodeproj/project.pbxproj

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

99
/* Begin PBXBuildFile section */
1010
01BA4DA826820DE8001FF7B5 /* gradient-gratification-p3.svg in Resources */ = {isa = PBXBuildFile; fileRef = 01BA4DA726820DE8001FF7B5 /* gradient-gratification-p3.svg */; };
11+
01F290A027D7839900743E2F /* transform-path.svg in Resources */ = {isa = PBXBuildFile; fileRef = 01F2909E27D7839800743E2F /* transform-path.svg */; };
12+
01F290A127D7839900743E2F /* symbol-test.svg in Resources */ = {isa = PBXBuildFile; fileRef = 01F2909F27D7839900743E2F /* symbol-test.svg */; };
1113
D90523452217927F00B3B94F /* mystery.svg in Resources */ = {isa = PBXBuildFile; fileRef = D90523442217927F00B3B94F /* mystery.svg */; };
1214
D9126F0E24A6C19300859CC5 /* gradient-apple.svg in Resources */ = {isa = PBXBuildFile; fileRef = D9126F0D24A6C19200859CC5 /* gradient-apple.svg */; };
1315
D918D26D22122A7300097C9A /* arc2.svg in Resources */ = {isa = PBXBuildFile; fileRef = D918D26C22122A7300097C9A /* arc2.svg */; };
@@ -60,6 +62,8 @@
6062

6163
/* Begin PBXFileReference section */
6264
01BA4DA726820DE8001FF7B5 /* gradient-gratification-p3.svg */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = "gradient-gratification-p3.svg"; sourceTree = "<group>"; };
65+
01F2909E27D7839800743E2F /* transform-path.svg */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = "transform-path.svg"; sourceTree = "<group>"; };
66+
01F2909F27D7839900743E2F /* symbol-test.svg */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = "symbol-test.svg"; sourceTree = "<group>"; };
6367
D90523442217927F00B3B94F /* mystery.svg */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = mystery.svg; sourceTree = "<group>"; };
6468
D9126F0D24A6C19200859CC5 /* gradient-apple.svg */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = "gradient-apple.svg"; sourceTree = "<group>"; };
6569
D918D26C22122A7300097C9A /* arc2.svg */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = arc2.svg; sourceTree = "<group>"; };
@@ -147,6 +151,8 @@
147151
D9ACD782220FDDE0009717CF /* Samples */ = {
148152
isa = PBXGroup;
149153
children = (
154+
01F2909F27D7839900743E2F /* symbol-test.svg */,
155+
01F2909E27D7839800743E2F /* transform-path.svg */,
150156
D9C6E95827D4859C008194E5 /* symbol.refresh.svg */,
151157
D9ACD78D220FDDE0009717CF /* arc.svg */,
152158
D918D26C22122A7300097C9A /* arc2.svg */,
@@ -254,6 +260,7 @@
254260
isa = PBXResourcesBuildPhase;
255261
buildActionMask = 2147483647;
256262
files = (
263+
01F290A127D7839900743E2F /* symbol-test.svg in Resources */,
257264
D9ACD79E220FDDE0009717CF /* invalid.svg in Resources */,
258265
D9126F0E24A6C19300859CC5 /* gradient-apple.svg in Resources */,
259266
D98375B622500FCC00974D71 /* mask-composite.svg in Resources */,
@@ -275,6 +282,7 @@
275282
D918D26D22122A7300097C9A /* arc2.svg in Resources */,
276283
D9ACD79B220FDDE0009717CF /* arc.svg in Resources */,
277284
D92A71FE27818B860001630A /* simple.svg in Resources */,
285+
01F290A027D7839900743E2F /* transform-path.svg in Resources */,
278286
D961846D220FDD1200C59D9B /* Assets.xcassets in Resources */,
279287
D938A203268307670051E18B /* pattern-rotate.svg in Resources */,
280288
D9ACD79D220FDDE0009717CF /* display.svg in Resources */,

Examples/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: "symbol.refresh.svg")?.rasterize()
68+
imageView.image = Image(named: "symbol-test.svg")?.rasterize()
6969
imageView.contentMode = .scaleAspectFit
7070
imageView.backgroundColor = .white
7171
self.view = imageView

SwiftDraw/DOM.Element.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,10 @@ protocol PresentationAttributes {
4949
var fill: DOM.Fill? { get set }
5050
var fillOpacity: DOM.Float? { get set }
5151
var fillRule: DOM.FillRule? { get set }
52-
52+
53+
var fontFamily: String? { get set }
54+
var fontSize: Float? { get set }
55+
5356
var transform: [DOM.Transform]? { get set }
5457
var clipPath: DOM.URL? { get set }
5558
var mask: DOM.URL? { get set }
@@ -75,7 +78,10 @@ extension DOM {
7578
var fill: DOM.Fill?
7679
var fillOpacity: DOM.Float?
7780
var fillRule: DOM.FillRule?
78-
81+
82+
var fontFamily: String?
83+
var fontSize: Float?
84+
7985
var transform: [DOM.Transform]?
8086
var clipPath: URL?
8187
var mask: URL?

SwiftDraw/DOM.Text.swift

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,7 @@ extension DOM {
3535
var x: Coordinate?
3636
var y: Coordinate?
3737
var value: String
38-
39-
var fontFamily: String?
40-
var fontSize: Float?
41-
42-
// var textLength: Coordinate
43-
// var text: [TSpan] child nodes
44-
38+
4539
init(x: Coordinate? = nil, y: Coordinate? = nil, value: String) {
4640
self.x = x
4741
self.y = y

SwiftDraw/LayerTree.Builder.swift

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,12 @@ extension LayerTree.Builder {
186186
}
187187

188188
static func makeTextAttributes(with state: State) -> LayerTree.TextAttributes {
189-
return .normal
189+
let fill = LayerTree.Color.create(from: state.fill.makeColor()).withAlpha(state.fillOpacity).maybeNone()
190+
return LayerTree.TextAttributes(
191+
color: fill,
192+
fontName: state.fontFamily,
193+
size: state.fontSize
194+
)
190195
}
191196

192197
func makePattern(for element: DOM.Pattern) -> LayerTree.Pattern {
@@ -249,6 +254,9 @@ extension LayerTree.Builder {
249254
var fillOpacity: DOM.Float
250255
var fillRule: DOM.FillRule
251256

257+
var fontFamily: String
258+
var fontSize: DOM.Float
259+
252260
init() {
253261
//default root SVG element state
254262
opacity = 1.0
@@ -265,6 +273,9 @@ extension LayerTree.Builder {
265273
fill = .color(.keyword(.black))
266274
fillOpacity = 1.0
267275
fillRule = .evenodd
276+
277+
fontFamily = "Helvetica"
278+
fontSize = 12.0
268279
}
269280
}
270281

@@ -285,6 +296,9 @@ extension LayerTree.Builder {
285296
state.fillOpacity = attributes.fillOpacity ?? existing.fillOpacity
286297
state.fillRule = attributes.fillRule ?? existing.fillRule
287298

299+
state.fontFamily = attributes.fontFamily ?? existing.fontFamily
300+
state.fontSize = attributes.fontSize ?? existing.fontSize
301+
288302
return state
289303
}
290304
}

SwiftDraw/LayerTree.Layer.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,5 @@ extension LayerTree {
118118
var color: Color
119119
var fontName: String
120120
var size: Float
121-
122-
static var normal: TextAttributes {
123-
return TextAttributes(color: .black, fontName: "Helvetica", size: 12.0)
124-
}
125121
}
126122
}

SwiftDraw/Parser.XML.Element.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,11 +237,13 @@ extension XMLParser {
237237
} catch {
238238
print(error)
239239
}
240-
241-
240+
242241
el.fillOpacity = try att.parsePercentage("fill-opacity")
243242
el.fillRule = try att.parseRaw("fill-rule")
244-
243+
244+
el.fontFamily = (try att.parseString("font-family"))?.trimmingCharacters(in: .whitespacesAndNewlines)
245+
el.fontSize = try att.parseFloat("font-size")
246+
245247
if let val = try? att.parseString("transform") {
246248
el.transform = try parseTransform(val)
247249
}
@@ -269,6 +271,8 @@ extension PresentationAttributes {
269271
fill = attributes.fill
270272
fillOpacity = attributes.fillOpacity
271273
fillRule = attributes.fillRule
274+
fontFamily = attributes.fontFamily
275+
fontSize = attributes.fontSize
272276
transform = attributes.transform
273277
clipPath = attributes.clipPath
274278
mask = attributes.mask

SwiftDrawTests/LayerTree.LayerTests.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,5 +94,14 @@ private extension LayerTree.Layer.Contents {
9494
let image = LayerTree.Image(mimeType: "image/png", data: Data(base64Encoded: "f00d")!)!
9595
return .image(image)
9696
}
97+
}
9798

99+
extension LayerTree.TextAttributes {
100+
static var normal: Self {
101+
LayerTree.TextAttributes(
102+
color: .black,
103+
fontName: "Helvetica",
104+
size: 12.0
105+
)
106+
}
98107
}

0 commit comments

Comments
 (0)