expo-font 12.0.8 → 12.0.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -10,6 +10,18 @@
10
10
 
11
11
  ### 💡 Others
12
12
 
13
+ ## 12.0.10 — 2024-09-03
14
+
15
+ ### 🐛 Bug fixes
16
+
17
+ - Fix support for macOS. ([#31307](https://github.com/expo/expo/pull/31307) by [@gabrieldonadel](https://github.com/gabrieldonadel))
18
+
19
+ ## 12.0.9 — 2024-07-16
20
+
21
+ ### 🐛 Bug fixes
22
+
23
+ - On `iOS`, fix issues where fonts were removed when the app is backgrounded. ([#30400](https://github.com/expo/expo/pull/30400) by [@alanjhughes](https://github.com/alanjhughes))
24
+
13
25
  ## 12.0.8 — 2024-07-11
14
26
 
15
27
  ### 🎉 New features
@@ -1,7 +1,7 @@
1
1
  apply plugin: 'com.android.library'
2
2
 
3
3
  group = 'host.exp.exponent'
4
- version = '12.0.8'
4
+ version = '12.0.10'
5
5
 
6
6
  def expoModulesCorePlugin = new File(project(":expo-modules-core").projectDir.absolutePath, "ExpoModulesCorePlugin.gradle")
7
7
  apply from: expoModulesCorePlugin
@@ -14,6 +14,6 @@ android {
14
14
  namespace "expo.modules.font"
15
15
  defaultConfig {
16
16
  versionCode 29
17
- versionName "12.0.8"
17
+ versionName "12.0.10"
18
18
  }
19
19
  }
@@ -17,3 +17,9 @@ internal final class FontRegistrationFailedException: GenericException<CFError>
17
17
  "Registering '\(param)' font failed with message: '\(param.localizedDescription)'"
18
18
  }
19
19
  }
20
+
21
+ internal final class UnregisteringFontFailedException: GenericException<CFError> {
22
+ override var reason: String {
23
+ "Unregistering '\(param)' font failed with message: '\(param.localizedDescription)'"
24
+ }
25
+ }
@@ -25,9 +25,9 @@ internal struct FontFamilyAliasManager {
25
25
  Sets the alias for the given family name.
26
26
  If the alias has already been set, its family name will be overriden.
27
27
  */
28
- internal static func setAlias(_ familyNameAlias: String, forFamilyName familyName: String) {
28
+ internal static func setAlias(_ familyNameAlias: String, forFont font: String) {
29
29
  maybeSwizzleUIFont()
30
- fontFamilyAliases[familyNameAlias] = familyName
30
+ fontFamilyAliases[familyNameAlias] = font
31
31
  }
32
32
 
33
33
  /**
@@ -47,6 +47,7 @@ private func maybeSwizzleUIFont() {
47
47
  if hasSwizzled {
48
48
  return
49
49
  }
50
+ #if !os(macOS)
50
51
  let originalFontNamesMethod = class_getClassMethod(UIFont.self, #selector(UIFont.fontNames(forFamilyName:)))
51
52
  let newFontNamesMethod = class_getClassMethod(UIFont.self, #selector(UIFont._expo_fontNames(forFamilyName:)))
52
53
 
@@ -55,5 +56,6 @@ private func maybeSwizzleUIFont() {
55
56
  } else {
56
57
  log.error("expo-font is unable to swizzle `UIFont.fontNames(forFamilyName:)`")
57
58
  }
59
+ #endif
58
60
  hasSwizzled = true
59
61
  }
@@ -9,24 +9,23 @@ public final class FontLoaderModule: Module {
9
9
  }
10
10
 
11
11
  AsyncFunction("loadAsync") { (fontFamilyAlias: String, localUri: URL) in
12
- // If the font was already registered, unregister it first. Otherwise CTFontManagerRegisterGraphicsFont
12
+ let fontUrl = localUri as CFURL
13
+ // If the font was already registered, unregister it first. Otherwise CTFontManagerRegisterFontsForURL
13
14
  // would fail because of a duplicated font name when the app reloads or someone wants to override a font.
14
- if let familyName = FontFamilyAliasManager.familyName(forAlias: fontFamilyAlias) {
15
- guard try unregisterFont(named: familyName) else {
15
+ if FontFamilyAliasManager.familyName(forAlias: fontFamilyAlias) != nil {
16
+ guard try unregisterFont(url: fontUrl) else {
16
17
  return
17
18
  }
18
19
  }
19
20
 
20
- // Create a font object from the given file
21
- let font = try loadFont(fromPath: localUri.path, alias: fontFamilyAlias)
22
-
23
21
  // Register the font
24
- try registerFont(font)
22
+ try registerFont(fontUrl)
23
+
24
+ // Create a font object from the given URL
25
+ let font = try loadFont(fromUrl: fontUrl, alias: fontFamilyAlias)
25
26
 
26
- // The real font name might be different than it's been requested by the user,
27
- // so we save the provided name as an alias.
28
27
  if let postScriptName = font.postScriptName as? String {
29
- FontFamilyAliasManager.setAlias(fontFamilyAlias, forFamilyName: postScriptName)
28
+ FontFamilyAliasManager.setAlias(fontFamilyAlias, forFont: postScriptName)
30
29
  }
31
30
  }
32
31
  }
@@ -25,31 +25,34 @@ internal func queryCustomNativeFonts() -> [String] {
25
25
  // [2] Retrieve font names by family names
26
26
  return fontFamilies.flatMap { fontFamilyNames in
27
27
  return fontFamilyNames.flatMap { fontFamilyName in
28
+ #if os(iOS) || os(tvOS)
28
29
  return UIFont.fontNames(forFamilyName: fontFamilyName)
30
+ #elseif os(macOS)
31
+ return NSFontManager.shared.availableMembers(ofFontFamily: fontFamilyName)?.compactMap { $0[0] as? String } ?? []
32
+ #endif
29
33
  }
30
34
  }
31
35
  }
32
36
 
33
37
  /**
34
- Loads the font from the given path and returns it as ``CGFont``.
38
+ Loads the font from the given url and returns it as ``CGFont``.
35
39
  */
36
- internal func loadFont(fromPath path: String, alias: String) throws -> CGFont {
37
- guard let data = FileManager.default.contents(atPath: path) else {
38
- throw FontFileNotFoundException(path)
39
- }
40
- guard let provider = CGDataProvider(data: data as CFData), let font = CGFont(provider) else {
40
+ internal func loadFont(fromUrl url: CFURL, alias: String) throws -> CGFont {
41
+ guard let provider = CGDataProvider(url: url),
42
+ let cgFont = CGFont(provider) else {
41
43
  throw FontCreationFailedException(alias)
42
44
  }
43
- return font
45
+
46
+ return cgFont
44
47
  }
45
48
 
46
49
  /**
47
50
  Registers the given font to make it discoverable through font descriptor matching.
48
51
  */
49
- internal func registerFont(_ font: CGFont) throws {
52
+ internal func registerFont(_ fontUrl: CFURL) throws {
50
53
  var error: Unmanaged<CFError>?
51
54
 
52
- if !CTFontManagerRegisterGraphicsFont(font, &error), let error = error?.takeRetainedValue() {
55
+ if !CTFontManagerRegisterFontsForURL(fontUrl, .process, &error), let error = error?.takeRetainedValue() {
53
56
  let fontError = CTFontManagerError(rawValue: CFErrorGetCode(error))
54
57
 
55
58
  switch fontError {
@@ -68,10 +71,10 @@ internal func registerFont(_ font: CGFont) throws {
68
71
  Unregisters the given font, so the app will no longer be able to render it.
69
72
  Returns a boolean indicating if the font is successfully unregistered after this function completes.
70
73
  */
71
- internal func unregisterFont(_ font: CGFont) throws -> Bool {
74
+ internal func unregisterFont(url: CFURL) throws -> Bool {
72
75
  var error: Unmanaged<CFError>?
73
76
 
74
- if !CTFontManagerUnregisterGraphicsFont(font, &error), let error = error?.takeRetainedValue() {
77
+ if !CTFontManagerUnregisterFontsForURL(url, .process, &error), let error = error?.takeRetainedValue() {
75
78
  if let ctFontManagerError = CTFontManagerError(rawValue: CFErrorGetCode(error as CFError)) {
76
79
  switch ctFontManagerError {
77
80
  case .systemRequired, .inUse:
@@ -79,20 +82,9 @@ internal func unregisterFont(_ font: CGFont) throws -> Bool {
79
82
  case .notRegistered:
80
83
  return true
81
84
  default:
82
- throw FontRegistrationFailedException(error)
85
+ throw UnregisteringFontFailedException(error)
83
86
  }
84
87
  }
85
88
  }
86
89
  return true
87
90
  }
88
-
89
- /**
90
- Unregisters a font with the given name, so the app will no longer be able to render it.
91
- Returns a boolean indicating if the font is successfully unregistered after this function completes.
92
- */
93
- internal func unregisterFont(named fontName: String) throws -> Bool {
94
- if let font = CGFont(fontName as CFString) {
95
- return try unregisterFont(font)
96
- }
97
- return true
98
- }
@@ -1,3 +1,4 @@
1
+ #if !os(macOS)
1
2
  /**
2
3
  An extension to ``UIFont`` that adds a custom implementation of `fontNames(forFamilyName:)` that supports aliasing font families.
3
4
  */
@@ -11,16 +12,18 @@ public extension UIFont {
11
12
  let fontNames = UIFont._expo_fontNames(forFamilyName: familyName)
12
13
 
13
14
  // If no font names were found, let's try with the alias.
14
- if fontNames.isEmpty, let aliasedFamilyName = FontFamilyAliasManager.familyName(forAlias: familyName) {
15
- let fontNames = UIFont._expo_fontNames(forFamilyName: aliasedFamilyName)
15
+ if fontNames.isEmpty, let postScriptName = FontFamilyAliasManager.familyName(forAlias: familyName) {
16
+ let fontNames = UIFont._expo_fontNames(forFamilyName: postScriptName)
16
17
 
17
18
  // If we still don't find any font names, we can assume it was not a family name but a font name.
18
19
  // In that case we can safely return the original font name.
19
20
  if fontNames.isEmpty {
20
- return [aliasedFamilyName]
21
+ return [postScriptName]
21
22
  }
22
23
  return fontNames
23
24
  }
25
+
24
26
  return fontNames
25
27
  }
26
28
  }
29
+ #endif
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "expo-font",
3
- "version": "12.0.8",
3
+ "version": "12.0.10",
4
4
  "description": "Load fonts at runtime and use them in React Native components.",
5
5
  "main": "build/index.js",
6
6
  "types": "build/index.d.ts",
@@ -42,5 +42,5 @@
42
42
  "peerDependencies": {
43
43
  "expo": "*"
44
44
  },
45
- "gitHead": "48f1ec9d91c5c38c7684cb98a314e443cbeb2185"
45
+ "gitHead": "de377512cb1707aee70e78cc8fd875d2ba9c3ab4"
46
46
  }