Skip to content

Commit 0e1f34f

Browse files
committed
Fixed font name parsing
1 parent 4387a9e commit 0e1f34f

File tree

1 file changed

+10
-29
lines changed

1 file changed

+10
-29
lines changed

Sources/Silica/CGFont.swift

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,10 @@ public struct CGFont {
7070
return nil
7171
}
7272

73-
guard name == scaledFont.fullName
73+
// Default font is Verdana, make sure the name is correct
74+
let defaultFontName = "Verdana"
75+
76+
guard name == defaultFontName || scaledFont.fullName != defaultFontName
7477
else { return nil }
7578

7679
// cache
@@ -170,29 +173,18 @@ internal func FcPattern(name: String) -> (pointer: OpaquePointer, family: String
170173

171174
let cleanup = ErrorCleanup(cleanup: { FcPatternDestroy(pattern) })
172175

173-
let separator = "-".withCString { (pointer) in return pointer[0] }
176+
let separator: Character = "-"
174177

175178
let traits: String?
176179

177180
let family: String
178181

179-
if let traitsCString = strchr(name, CInt(separator)) {
180-
181-
let trimmedCString = traitsCString.advanced(by: 1)
182-
183-
// should free memory, but crashes
184-
// defer { free(traitsCString) }
185-
186-
let traitsString = String(cString: trimmedCString)
187-
188-
let familyLength = name.utf8.count - traitsString.utf8.count - 1 // for separator
189-
190-
family = name.substring(range: 0 ..< familyLength)!
191-
192-
traits = traitsString
193-
182+
let components = name.split(separator: separator, maxSplits: 2, omittingEmptySubsequences: true)
183+
184+
if components.count == 2 {
185+
family = String(components[0])
186+
traits = String(components[1])
194187
} else {
195-
196188
family = name
197189
traits = nil
198190
}
@@ -245,14 +237,3 @@ internal func FcPattern(name: String) -> (pointer: OpaquePointer, family: String
245237

246238
return (pattern, family)
247239
}
248-
249-
// MARK: - String Extensions
250-
251-
internal extension String {
252-
253-
func substring(range: Range<Int>) -> String? {
254-
let indexRange = utf8.index(utf8.startIndex, offsetBy: range.lowerBound) ..< utf8.index(utf8.startIndex, offsetBy: range.upperBound)
255-
let substring = String(utf8[indexRange])
256-
return substring
257-
}
258-
}

0 commit comments

Comments
 (0)