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

Fonts: fix code which checks if a font is already registered #77

Merged
merged 4 commits into from
Aug 22, 2017
Merged
Show file tree
Hide file tree
Changes from 3 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
20 changes: 20 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,26 @@

---

## Master

### Bug Fixes

* Fonts: fix code which checks if a font is already registered.
[Vladimir Burdukov](https://github.com/chipp)
[#77](https://github.com/SwiftGen/templates/pull/77)

### Breaking Changes

_None_

### New Features

_None_

### Internal Changes

_None_

## 2.1.1

### Bug Fixes
Expand Down
17 changes: 9 additions & 8 deletions Tests/Expected/Fonts/swift2-context-defaults-customname.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,26 @@ struct FontConvertible {
}

func register() {
let bundle = NSBundle(forClass: BundleToken.self)

guard let url = bundle.URLForResource(path, withExtension: nil) else {
return
}

guard let url = url else { return }
var errorRef: Unmanaged<CFError>?
CTFontManagerRegisterFontsForURL(url as CFURL, .Process, &errorRef)
}

fileprivate var url: NSURL? {
let bundle = NSBundle(forClass: BundleToken.self)
guard let url = bundle.URLForResource(path, withExtension: nil) else { return nil }
Copy link
Collaborator

@AliSoftware AliSoftware Aug 22, 2017

Choose a reason for hiding this comment

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

Wait, given than you guard let url … else return nil and do nothing afterwards but directly return url, why not directly return bundle.URLForResource(path, withExtension: nil) there? That would already directly return nil if URLForResource return nil, so…

Copy link
Member Author

Choose a reason for hiding this comment

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

yeah, you right..

return url
}
}

extension Font {
convenience init!(font: FontConvertible, size: CGFloat) {
#if os(iOS) || os(tvOS) || os(watchOS)
if UIFont.fontNamesForFamilyName(font.family).isEmpty {
if !UIFont.fontNamesForFamilyName(font.family).contains(font.name) {
font.register()
}
#elseif os(OSX)
if NSFontManager.sharedFontManager().availableMembersOfFontFamily(font.family) == nil {
if let url = font.url, CTFontManagerGetScopeForURL(url as CFURL) == .None {
font.register()
}
#endif
Expand Down
17 changes: 9 additions & 8 deletions Tests/Expected/Fonts/swift2-context-defaults-preservepath.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,26 @@ struct FontConvertible {
}

func register() {
let bundle = NSBundle(forClass: BundleToken.self)

guard let url = bundle.URLForResource(path, withExtension: nil) else {
return
}

guard let url = url else { return }
var errorRef: Unmanaged<CFError>?
CTFontManagerRegisterFontsForURL(url as CFURL, .Process, &errorRef)
}

fileprivate var url: NSURL? {
let bundle = NSBundle(forClass: BundleToken.self)
guard let url = bundle.URLForResource(path, withExtension: nil) else { return nil }
return url
}
}

extension Font {
convenience init!(font: FontConvertible, size: CGFloat) {
#if os(iOS) || os(tvOS) || os(watchOS)
if UIFont.fontNamesForFamilyName(font.family).isEmpty {
if !UIFont.fontNamesForFamilyName(font.family).contains(font.name) {
font.register()
}
#elseif os(OSX)
if NSFontManager.sharedFontManager().availableMembersOfFontFamily(font.family) == nil {
if let url = font.url, CTFontManagerGetScopeForURL(url as CFURL) == .None {
font.register()
}
#endif
Expand Down
17 changes: 9 additions & 8 deletions Tests/Expected/Fonts/swift2-context-defaults.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,26 @@ struct FontConvertible {
}

func register() {
let bundle = NSBundle(forClass: BundleToken.self)

guard let url = bundle.URLForResource(path, withExtension: nil) else {
return
}

guard let url = url else { return }
var errorRef: Unmanaged<CFError>?
CTFontManagerRegisterFontsForURL(url as CFURL, .Process, &errorRef)
}

fileprivate var url: NSURL? {
let bundle = NSBundle(forClass: BundleToken.self)
guard let url = bundle.URLForResource(path, withExtension: nil) else { return nil }
return url
}
}

extension Font {
convenience init!(font: FontConvertible, size: CGFloat) {
#if os(iOS) || os(tvOS) || os(watchOS)
if UIFont.fontNamesForFamilyName(font.family).isEmpty {
if !UIFont.fontNamesForFamilyName(font.family).contains(font.name) {
font.register()
}
#elseif os(OSX)
if NSFontManager.sharedFontManager().availableMembersOfFontFamily(font.family) == nil {
if let url = font.url, CTFontManagerGetScopeForURL(url as CFURL) == .None {
font.register()
}
#endif
Expand Down
17 changes: 9 additions & 8 deletions Tests/Expected/Fonts/swift3-context-defaults-customname.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,26 @@ struct FontConvertible {
}

func register() {
let bundle = Bundle(for: BundleToken.self)

guard let url = bundle.url(forResource: path, withExtension: nil) else {
return
}

guard let url = url else { return }
var errorRef: Unmanaged<CFError>?
CTFontManagerRegisterFontsForURL(url as CFURL, .process, &errorRef)
}

fileprivate var url: URL? {
let bundle = Bundle(for: BundleToken.self)
guard let url = bundle.url(forResource: path, withExtension: nil) else { return nil }
return url
}
}

extension Font {
convenience init!(font: FontConvertible, size: CGFloat) {
#if os(iOS) || os(tvOS) || os(watchOS)
if UIFont.fontNames(forFamilyName: font.family).isEmpty {
if !UIFont.fontNames(forFamilyName: font.family).contains(font.name) {
font.register()
}
#elseif os(OSX)
if NSFontManager.shared().availableMembers(ofFontFamily: font.family) == nil {
if let url = font.url, CTFontManagerGetScopeForURL(url as CFURL) == .none {
font.register()
}
#endif
Expand Down
17 changes: 9 additions & 8 deletions Tests/Expected/Fonts/swift3-context-defaults-preservepath.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,26 @@ struct FontConvertible {
}

func register() {
let bundle = Bundle(for: BundleToken.self)

guard let url = bundle.url(forResource: path, withExtension: nil) else {
return
}

guard let url = url else { return }
var errorRef: Unmanaged<CFError>?
CTFontManagerRegisterFontsForURL(url as CFURL, .process, &errorRef)
}

fileprivate var url: URL? {
let bundle = Bundle(for: BundleToken.self)
guard let url = bundle.url(forResource: path, withExtension: nil) else { return nil }
return url
}
}

extension Font {
convenience init!(font: FontConvertible, size: CGFloat) {
#if os(iOS) || os(tvOS) || os(watchOS)
if UIFont.fontNames(forFamilyName: font.family).isEmpty {
if !UIFont.fontNames(forFamilyName: font.family).contains(font.name) {
font.register()
}
#elseif os(OSX)
if NSFontManager.shared().availableMembers(ofFontFamily: font.family) == nil {
if let url = font.url, CTFontManagerGetScopeForURL(url as CFURL) == .none {
font.register()
}
#endif
Expand Down
17 changes: 9 additions & 8 deletions Tests/Expected/Fonts/swift3-context-defaults.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,26 @@ struct FontConvertible {
}

func register() {
let bundle = Bundle(for: BundleToken.self)

guard let url = bundle.url(forResource: path, withExtension: nil) else {
return
}

guard let url = url else { return }
var errorRef: Unmanaged<CFError>?
CTFontManagerRegisterFontsForURL(url as CFURL, .process, &errorRef)
}

fileprivate var url: URL? {
let bundle = Bundle(for: BundleToken.self)
guard let url = bundle.url(forResource: path, withExtension: nil) else { return nil }
return url
}
}

extension Font {
convenience init!(font: FontConvertible, size: CGFloat) {
#if os(iOS) || os(tvOS) || os(watchOS)
if UIFont.fontNames(forFamilyName: font.family).isEmpty {
if !UIFont.fontNames(forFamilyName: font.family).contains(font.name) {
font.register()
}
#elseif os(OSX)
if NSFontManager.shared().availableMembers(ofFontFamily: font.family) == nil {
if let url = font.url, CTFontManagerGetScopeForURL(url as CFURL) == .none {
font.register()
}
#endif
Expand Down
17 changes: 9 additions & 8 deletions Tests/Expected/Fonts/swift4-context-defaults-customname.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,26 @@ struct FontConvertible {
}

func register() {
let bundle = Bundle(for: BundleToken.self)

guard let url = bundle.url(forResource: path, withExtension: nil) else {
return
}

guard let url = url else { return }
var errorRef: Unmanaged<CFError>?
CTFontManagerRegisterFontsForURL(url as CFURL, .process, &errorRef)
}

fileprivate var url: URL? {
let bundle = Bundle(for: BundleToken.self)
guard let url = bundle.url(forResource: path, withExtension: nil) else { return nil }
return url
}
}

extension Font {
convenience init!(font: FontConvertible, size: CGFloat) {
#if os(iOS) || os(tvOS) || os(watchOS)
if UIFont.fontNames(forFamilyName: font.family).isEmpty {
if !UIFont.fontNames(forFamilyName: font.family).contains(font.name) {
font.register()
}
#elseif os(OSX)
if NSFontManager.shared.availableMembers(ofFontFamily: font.family) == nil {
if let url = font.url, CTFontManagerGetScopeForURL(url as CFURL) == .none {
font.register()
}
#endif
Expand Down
17 changes: 9 additions & 8 deletions Tests/Expected/Fonts/swift4-context-defaults-preservepath.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,26 @@ struct FontConvertible {
}

func register() {
let bundle = Bundle(for: BundleToken.self)

guard let url = bundle.url(forResource: path, withExtension: nil) else {
return
}

guard let url = url else { return }
var errorRef: Unmanaged<CFError>?
CTFontManagerRegisterFontsForURL(url as CFURL, .process, &errorRef)
}

fileprivate var url: URL? {
let bundle = Bundle(for: BundleToken.self)
guard let url = bundle.url(forResource: path, withExtension: nil) else { return nil }
return url
}
}

extension Font {
convenience init!(font: FontConvertible, size: CGFloat) {
#if os(iOS) || os(tvOS) || os(watchOS)
if UIFont.fontNames(forFamilyName: font.family).isEmpty {
if !UIFont.fontNames(forFamilyName: font.family).contains(font.name) {
font.register()
}
#elseif os(OSX)
if NSFontManager.shared.availableMembers(ofFontFamily: font.family) == nil {
if let url = font.url, CTFontManagerGetScopeForURL(url as CFURL) == .none {
font.register()
}
#endif
Expand Down
17 changes: 9 additions & 8 deletions Tests/Expected/Fonts/swift4-context-defaults.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,26 @@ struct FontConvertible {
}

func register() {
let bundle = Bundle(for: BundleToken.self)

guard let url = bundle.url(forResource: path, withExtension: nil) else {
return
}

guard let url = url else { return }
var errorRef: Unmanaged<CFError>?
CTFontManagerRegisterFontsForURL(url as CFURL, .process, &errorRef)
}

fileprivate var url: URL? {
let bundle = Bundle(for: BundleToken.self)
guard let url = bundle.url(forResource: path, withExtension: nil) else { return nil }
return url
}
}

extension Font {
convenience init!(font: FontConvertible, size: CGFloat) {
#if os(iOS) || os(tvOS) || os(watchOS)
if UIFont.fontNames(forFamilyName: font.family).isEmpty {
if !UIFont.fontNames(forFamilyName: font.family).contains(font.name) {
font.register()
}
#elseif os(OSX)
if NSFontManager.shared.availableMembers(ofFontFamily: font.family) == nil {
if let url = font.url, CTFontManagerGetScopeForURL(url as CFURL) == .none {
font.register()
}
#endif
Expand Down
17 changes: 9 additions & 8 deletions templates/fonts/swift2.stencil
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,26 @@ struct FontConvertible {
}

func register() {
let bundle = NSBundle(forClass: BundleToken.self)

guard let url = bundle.URLForResource(path, withExtension: nil) else {
return
}

guard let url = url else { return }
var errorRef: Unmanaged<CFError>?
CTFontManagerRegisterFontsForURL(url as CFURL, .Process, &errorRef)
}

fileprivate var url: NSURL? {
let bundle = NSBundle(forClass: BundleToken.self)
guard let url = bundle.URLForResource(path, withExtension: nil) else { return nil }
return url
}
}

extension Font {
convenience init!(font: FontConvertible, size: CGFloat) {
#if os(iOS) || os(tvOS) || os(watchOS)
if UIFont.fontNamesForFamilyName(font.family).isEmpty {
if !UIFont.fontNamesForFamilyName(font.family).contains(font.name) {
font.register()
}
#elseif os(OSX)
if NSFontManager.sharedFontManager().availableMembersOfFontFamily(font.family) == nil {
if let url = font.url, CTFontManagerGetScopeForURL(url as CFURL) == .None {
font.register()
}
#endif
Expand Down
Loading