Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[expo-font][iOS] Fix font registration failing when font was in use. #28989

Merged
merged 3 commits into from
May 22, 2024

Conversation

aleqsio
Copy link
Contributor

@aleqsio aleqsio commented May 20, 2024

Why

Fixes #28948

How

When unregistering a font, we sometimes bump into a failure.

Now, if the failure code indicates that the font will still be available afterwards, we let the registerFont function complete instead of rethrowing this error.

Test Plan

Tested that this works in standalone app.

Checklist

@aleqsio aleqsio requested a review from tsapeta as a code owner May 20, 2024 12:01
@expo-bot expo-bot added the bot: suggestions ExpoBot has some suggestions label May 20, 2024
@expo-bot
Copy link
Collaborator

expo-bot commented May 20, 2024

The Pull Request introduced fingerprint changes against the base commit: 56a028d

Fingerprint diff
[
  {
    "type": "dir",
    "filePath": "../../packages/expo-font/ios",
    "reasons": [
      "expoAutolinkingIos"
    ],
    "hash": "eeb1766aaef499afccd13cb634d09e754ec7b445"
  }
]

Generated by PR labeler 🤖

Co-authored-by: Expo Bot <34669131+expo-bot@users.noreply.github.com>
@expo-bot expo-bot added bot: passed checks ExpoBot has nothing to complain about and removed bot: suggestions ExpoBot has some suggestions labels May 20, 2024
@anstapol
Copy link

hey @aleqsio how long does it usually takes after the PR is merged for this to get into a release?

@aleqsio
Copy link
Contributor Author

aleqsio commented May 20, 2024

For a CNG flow (not expo-go), usually a week or so :)

@anstapol
Copy link

@aleqsio roger that. Thank you for the fix!

packages/expo-font/ios/FontLoaderModule.swift Outdated Show resolved Hide resolved
@aleqsio aleqsio merged commit d34b738 into main May 22, 2024
13 checks passed
@aleqsio aleqsio deleted the @aleqsio/fix-expo-font-issue branch May 22, 2024 14:04
@pera14
Copy link

pera14 commented May 23, 2024

when will this update be live in npm?

@hyochan
Copy link
Contributor

hyochan commented May 24, 2024

Also waiting for this!

@Lurtroxx
Copy link

Waiting this as well. Fonts do not work in my app from latest expo upgrade. Any Expo team member able to comment on when this will be released to the live version? What is the last stable version of Expo that we can use.

@Lurtroxx
Copy link

@aleqsio What can be done to test the pull request or integrate into the package. I can install the standalone build from the pull request (or current main branch, since the PR is merged, but not published to NPM), but I do not see how to use it with EAS build. Any advice?

@hyochan
Copy link
Contributor

hyochan commented May 26, 2024

Here is a patch package for expo-font. This works great!

diff --git a/node_modules/expo-font/ios/FontLoaderModule.swift b/node_modules/expo-font/ios/FontLoaderModule.swift
index 0cce788..f03aa4f 100644
--- a/node_modules/expo-font/ios/FontLoaderModule.swift
+++ b/node_modules/expo-font/ios/FontLoaderModule.swift
@@ -12,7 +12,9 @@ public final class FontLoaderModule: Module {
       // If the font was already registered, unregister it first. Otherwise CTFontManagerRegisterGraphicsFont
       // would fail because of a duplicated font name when the app reloads or someone wants to override a font.
       if let familyName = FontFamilyAliasManager.familyName(forAlias: fontFamilyAlias) {
-        try unregisterFont(named: familyName)
+        guard try unregisterFont(named: familyName) else {
+          return
+        }
       }
 
       // Create a font object from the given file
diff --git a/node_modules/expo-font/ios/FontUtils.swift b/node_modules/expo-font/ios/FontUtils.swift
index 48968db..2e179f4 100644
--- a/node_modules/expo-font/ios/FontUtils.swift
+++ b/node_modules/expo-font/ios/FontUtils.swift
@@ -67,19 +67,30 @@ internal func registerFont(_ font: CGFont) throws {
 /**
  Unregisters the given font, so the app will no longer be able to render it.
  */
-internal func unregisterFont(_ font: CGFont) throws {
+internal func unregisterFont(_ font: CGFont) throws -> Bool {
   var error: Unmanaged<CFError>?
 
   if !CTFontManagerUnregisterGraphicsFont(font, &error), let error = error?.takeRetainedValue() {
-    throw FontRegistrationFailedException(error)
+    if let ctFontManagerError = CTFontManagerError(rawValue: CFErrorGetCode(error as CFError)) {
+      switch ctFontManagerError {
+      case .systemRequired, .inUse:
+        return false
+      case .notRegistered:
+        return true
+      default:
+        throw FontRegistrationFailedException(error)
+      }
+    }
   }
+  return true
 }
 
 /**
  Unregisters a font with the given name, so the app will no longer be able to render it.
  */
-internal func unregisterFont(named fontName: String) throws {
+internal func unregisterFont(named fontName: String) throws -> Bool {
   if let font = CGFont(fontName as CFString) {
-    try unregisterFont(font)
+    return try unregisterFont(font)
   }
+  return true
 }
\ No newline at end of file

@Lurtroxx
Copy link

@hyochan Unfortunately, this did not work.

aleqsio added a commit that referenced this pull request May 28, 2024
…28989)

Co-authored-by: Expo Bot <34669131+expo-bot@users.noreply.github.com>
@diceros-soojungkim
Copy link

also waiting for this!

@brentvatne brentvatne added the published Changes from the PR have been published to npm label May 29, 2024
@pera14
Copy link

pera14 commented May 31, 2024

@aleqsio what's going on with this? Is there any solution for the ongoing issue or it's sorted? If that's the case, can you please tell us how to apply that

@aleqsio
Copy link
Contributor Author

aleqsio commented May 31, 2024

@pera14 Make sure to update to the latest expo-font package version - it should be fixed there.

@pera14
Copy link

pera14 commented May 31, 2024

@aleqsio I installed the latest expo-font@12.0.6 and expo@51.0.9 and I still have an issue when I build the app, and reopen it, after pressing any button, the app will crash.

@aleqsio
Copy link
Contributor Author

aleqsio commented May 31, 2024

This sounds like an unrelated issue - this one was happening on an app reload.

You can try to open an issue with a fresh reproduction - I'm pretty sure the issue in the repro linked here was fixed already.

@pera14
Copy link

pera14 commented May 31, 2024

Can you try to open a new issue then since I have had this issue for a long time and I thought it was related to this because it was crashing the app on reload as well

@hyochan
Copy link
Contributor

hyochan commented Jun 1, 2024

I've just realized that this patch doesn't work in release mode while it is fine in development.
When I remove the newArch, it works again.

Let me investigate further and come back.

@Lurtroxx
Copy link

Lurtroxx commented Jun 1, 2024

@hyochan no need to use patch package anymore, new version of Expo fixes this.

@hyochan
Copy link
Contributor

hyochan commented Jun 1, 2024

Disabling newArch works fine with this updates. With newArch, it only works in dev client and not production mode (tested in android device).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bot: fingerprint changed bot: passed checks ExpoBot has nothing to complain about published Changes from the PR have been published to npm
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[expo-font] Could not unregister the CGFont
9 participants