Skip to content
This repository has been archived by the owner on Sep 6, 2018. It is now read-only.

Add "path" to the fonts contexts #25

Merged
merged 8 commits into from
May 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ _None_
* The `strings`, `structuredStrings` and `tableName` have been replaced by `tables`, which is an array of string tables, each with a `name` and a `strings` property.
* For each string, the `params` variable and it's subvariables (such as `names`, `count`, ...) have been replaced by `types`, which is an array of types.
* `enumName`, `sceneEnumName` and `segueEnumName` have been replaced by `param.enumName`, `param.sceneEnumName` and `param.segueEnumName` respectively. Templates should provide a default value for these in case the variables are empty.

* Added the `path` variable to the fonts context (so that we can use it to genrate `Info.plist` entries and such).
[Olivier Halligon](https://github.com/AliGator)
[#25](https://github.com/SwiftGen/SwiftGenKit/pull/25)

### Internal Changes

* Switch from Travis CI to Circle CI, clean up the Rakefile in the process.
Expand Down
61 changes: 37 additions & 24 deletions Sources/Parsers/FontsFileParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ import PathKit
// MARK: Font

public struct Font {
let filePath: String
let familyName: String
let style: String
let postScriptName: String

init(familyName: String, style: String, postScriptName: String) {
init(filePath: String, familyName: String, style: String, postScriptName: String) {
self.filePath = filePath
self.familyName = familyName
self.style = style
self.postScriptName = postScriptName
Expand All @@ -33,11 +35,24 @@ public func == (lhs: Font, rhs: Font) -> Bool {
return lhs.postScriptName == rhs.postScriptName
}

extension PathKit.Path {
/// Returns the Path relative to a parent directory.
/// If the argument passed as parent isn't a prefix of `self`, returns `nil`.
///
/// - Parameter parent: The parent Path to get the relative path against
/// - Returns: The relative Path, or nil if parent was not a parent dir of self
func relative(to parent: Path) -> Path? {
let pc = parent.components
let fc = self.components
return fc.starts(with: pc) ? Path(components: fc.dropFirst(pc.count)) : nil
}
}

// MARK: CTFont

extension CTFont {
static func parseFonts(at url: URL) -> [Font] {
let descs = CTFontManagerCreateFontDescriptorsFromURL(url as CFURL) as NSArray?
static func parse(file: Path, relativeTo parent: Path? = nil) -> [Font] {
let descs = CTFontManagerCreateFontDescriptorsFromURL(file.url as CFURL) as NSArray?
guard let descRefs = (descs as? [CTFontDescriptor]) else { return [] }

return descRefs.flatMap { (desc) -> Font? in
Expand All @@ -46,7 +61,13 @@ extension CTFont {
guard let familyName = CTFontCopyAttribute(font, kCTFontFamilyNameAttribute) as? String,
let style = CTFontCopyAttribute(font, kCTFontStyleNameAttribute) as? String else { return nil }

return Font(familyName: familyName, style: style, postScriptName: postScriptName)
let relPath = parent.flatMap({ file.relative(to: $0) }) ?? file
return Font(
filePath: relPath.string,
familyName: familyName,
style: style,
postScriptName: postScriptName
)
}
}
}
Expand All @@ -59,26 +80,18 @@ public final class FontsFileParser {
public init() {}

public func parseFile(at path: Path) {
// PathKit does not support support enumeration with options yet
// see: https://github.com/kylef/PathKit/pull/25
let url = URL(fileURLWithPath: path.description)

if let dirEnum = FileManager.default.enumerator(at: url,
includingPropertiesForKeys: [],
options: [.skipsHiddenFiles, .skipsPackageDescendants],
errorHandler: nil) {
var value: AnyObject? = nil
while let file = dirEnum.nextObject() as? URL {
guard let _ = try? (file as NSURL).getResourceValue(&value, forKey: URLResourceKey.typeIdentifierKey),
let uti = value as? String else {
print("Unable to determine the Universal Type Identifier for file \(file)")
continue
}
guard UTTypeConformsTo(uti as CFString, "public.font" as CFString) else { continue }
let fonts = CTFont.parseFonts(at: file)

fonts.forEach { addFont($0) }
}
let dirChildren = path.iterateChildren(options: [.skipsHiddenFiles, .skipsPackageDescendants])
for file in dirChildren {
var value: AnyObject? = nil
let url = file.url as NSURL
guard let _ = try? url.getResourceValue(&value, forKey: URLResourceKey.typeIdentifierKey),
let uti = value as? String else {
print("Unable to determine the Universal Type Identifier for file \(file)")
continue
}
guard UTTypeConformsTo(uti as CFString, "public.font" as CFString) else { continue }
let fonts = CTFont.parse(file: file, relativeTo: path)
fonts.forEach { addFont($0) }
}
}

Expand Down
8 changes: 5 additions & 3 deletions Sources/Stencil/FontsContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import Foundation
- `families`: `Array` — list of font families
- `name` : `String` — name of family
- `fonts`: `Array` — list of fonts in family
- `style`: `String` — font style
- `name` : `String` — font postscript name
- `name` : `String` — the font's postscript name
- `path` : `String` — the path to the font, relative to the folder being scanned
- `style`: `String` — the designer's description of the font's style, like bold, oblique, …
*/

extension FontsFileParser {
Expand All @@ -21,8 +22,9 @@ extension FontsFileParser {
let fonts = family.map { (font: Font) -> [String: String] in
// Font
return [
"style": font.style,
"name": font.postScriptName,
"path": font.filePath,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't forget to add this new variable to the context docs.

"style": font.style,

// NOTE: This is a deprecated variable
"fontName": font.postScriptName
Expand Down
2 changes: 1 addition & 1 deletion Tests/Resources