Skip to content

Commit 63f6a94

Browse files
committed
Fix Nerd Font Bundle not persisting after restart. Issue 12744
Fonts were downloaded to a temp directory and registered with CTFontManagerRegisterFontDescriptors using persistent scope. However, persistent scope only stores a reference to the font file location - it doesn't copy the font data. When the temp directory was cleaned up, the registration pointed to non-existent files. Now copy fonts to ~/Library/Application Support/iTerm2/Fonts/ before registering them, so they persist across sessions.
1 parent 17f3789 commit 63f6a94

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

sources/NSFileManager+iTerm.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ extern NSNotificationName iTermScriptsFolderDidChange;
5858
- (NSString *)scriptsPathWithoutSpaces;
5959
- (BOOL)customScriptsFolderIsValid:(NSString *)candidate;
6060

61+
// Directory where fonts installed by iTerm2 live (e.g., Nerd Font Bundle).
62+
- (NSString *)fontsDirectory;
63+
6164
// Path to special auto-launch script that is run at startup.
6265
- (NSString *)legacyAutolaunchScriptPath; // applescript
6366
- (NSString *)autolaunchScriptPath; // scripting API

sources/NSFileManager+iTerm.m

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,15 @@ - (NSString *)scriptsPathWithoutSpacesCreatingLink {
259259
return modernPath;
260260
}
261261

262+
- (NSString *)fontsDirectory {
263+
NSString *path = [[self applicationSupportDirectory] stringByAppendingPathComponent:@"Fonts"];
264+
NSError *error = nil;
265+
if (![self fileExistsAtPath:path]) {
266+
[self createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:&error];
267+
}
268+
return path;
269+
}
270+
262271
- (NSString *)legacyAutolaunchScriptPath {
263272
return [[self scriptsPath] stringByAppendingPathComponent:@"AutoLaunch.scpt"];
264273
}

sources/NerdFontInstaller.swift

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,10 +200,27 @@ class NerdFontInstaller {
200200
return Set(Array(fontCollection))
201201
}
202202

203+
private static var fontsDirectory: URL {
204+
URL(fileURLWithPath: FileManager.default.fontsDirectory())
205+
}
206+
203207
private func install(from tempDir: String) {
204208
let fileManager = FileManager.default
209+
let fontsDir = Self.fontsDirectory
210+
211+
// Copy fonts to permanent location and create descriptors from there
205212
let fontDescriptors = fileManager.flatMapRegularFiles(in: tempDir) { itemURL in
206-
if let descriptors = CTFontManagerCreateFontDescriptorsFromURL(itemURL as CFURL) {
213+
let destURL = fontsDir.appendingPathComponent(itemURL.lastPathComponent)
214+
do {
215+
if fileManager.fileExists(atPath: destURL.path) {
216+
try fileManager.removeItem(at: destURL)
217+
}
218+
try fileManager.copyItem(at: itemURL, to: destURL)
219+
} catch {
220+
DLog("Failed to copy font \(itemURL.lastPathComponent): \(error)")
221+
return [CTFontDescriptor]()
222+
}
223+
if let descriptors = CTFontManagerCreateFontDescriptorsFromURL(destURL as CFURL) {
207224
return Array<CTFontDescriptor>(descriptors)
208225
}
209226
return []

0 commit comments

Comments
 (0)