Liquid Glass and Apple Music touches for the Spotify iOS app. A GPL-3.0 fork of spoti.pw 0.21.1.
C
feat: Font imports a custom font file from Files
My-Name-Is-Jeff2472954dbf08
diff --git a/tweak/Makefile b/tweak/Makefileindex d8a67fe1..e32bf4e1 100644--- a/tweak/Makefile+++ b/tweak/Makefile@@ -16,7 +16,7 @@ TWEAK_NAME := spotifyglass# is generated from the IPA by scripts/extract-flags.py.spotifyglass_FILES := $(shell find Sources -name '*.m' -o -name '*.x' -o -name '*.swift' | sort)spotifyglass_CFLAGS := -fobjc-arc -ISources -DSG_VERSION=\"$(SG_VERSION)\"-spotifyglass_FRAMEWORKS := UIKit QuartzCore UniformTypeIdentifiers MediaPlayer CoreImage AudioToolbox AVFoundation CoreHaptics Accelerate CoreMotion CoreML+spotifyglass_FRAMEWORKS := UIKit QuartzCore UniformTypeIdentifiers MediaPlayer CoreImage AudioToolbox AVFoundation CoreHaptics Accelerate CoreMotion CoreML CoreText# zlib unpacks KuGou's KRC lyrics (Shared/LyricsSources/KuGou.m).spotifyglass_LIBRARIES := z# Runtime swizzling instead of Substrate so the dylib has no extra dependency inside the IPA.diff --git a/tweak/Sources/App/Pages.m b/tweak/Sources/App/Pages.mindex e5b862ce..90f23a58 100644--- a/tweak/Sources/App/Pages.m+++ b/tweak/Sources/App/Pages.m@@ -68,7 +68,7 @@ UIViewController *SGAppearancePage(void) {};look = SGWithSymbol(redesign, @"sparkles");}- NSMutableArray<SGModRow *> *everywhere = [NSMutableArray arrayWithObject:SGWithSymbol(SGChoiceRow(@"Font", nil, SGKeyAppFont, SGAppFontNames(), SGAppFontSpotify), @"textformat")];+ NSMutableArray<SGModRow *> *everywhere = [NSMutableArray arrayWithArray:SGAppFontRows()];SGModRow *icon = SGAppIconRow();if (icon) [everywhere addObject:icon];return [[SGModPage alloc] initWithTitle:@"Appearance" intro:SGRestartNote sections:@[diff --git a/tweak/Sources/Shared/Fonts/FontImport.m b/tweak/Sources/Shared/Fonts/FontImport.mnew file mode 100644index 00000000..03226fe1--- /dev/null+++ b/tweak/Sources/Shared/Fonts/FontImport.m@@ -0,0 +1,124 @@+// A font of the user's own for Fonts.x: a .ttf or .otf picked in Files is copied into Application Support,+// one file at a time, registered with Core Text for this process and named by the PostScript name read from+// it. Like every other font choice it takes over when Spotify starts again, and Fonts.x registers the file+// again on each launch.+#import <CoreText/CoreText.h>+#import <UniformTypeIdentifiers/UniformTypeIdentifiers.h>+#import "Core/SGCore.h"+#import "Settings/SGModPage.h"+#import "Settings/SGPageStyle.h"+#import "Shared/Fonts/Fonts.h"++static NSURL *fontFolder(void) {+ NSURL *support = [NSFileManager.defaultManager URLsForDirectory:NSApplicationSupportDirectory inDomains:NSUserDomainMask].firstObject;+ return [[support URLByAppendingPathComponent:@"Vitrine" isDirectory:YES] URLByAppendingPathComponent:@"Font" isDirectory:YES];+}++static NSURL *storedFile(void) {+ NSString *file = [NSUserDefaults.standardUserDefaults stringForKey:SGKeyAppFontFile];+ return file.length ? [fontFolder() URLByAppendingPathComponent:file.lastPathComponent] : nil;+}++// The first face's PostScript name, or nil when Core Text finds no font in the file. A collection or a+// family in one file gives its first face.+static NSString *faceName(NSURL *url) {+ NSArray *faces = CFBridgingRelease(CTFontManagerCreateFontDescriptorsFromURL((__bridge CFURLRef)url));+ if (!faces.count) return nil;+ return CFBridgingRelease(CTFontDescriptorCopyAttribute((__bridge CTFontDescriptorRef)faces.firstObject, kCTFontNameAttribute));+}++// A face iOS already carries under the same name (Chalkduster, say) is refused as a duplicate and is there+// all the same, so what counts is whether the name makes a font afterwards.+static NSString *registerFile(NSURL *url) {+ NSString *name = url ? faceName(url) : nil;+ if (!name) return nil;+ CFErrorRef error = NULL;+ if (!CTFontManagerRegisterFontsForURL((__bridge CFURLRef)url, kCTFontManagerScopeProcess, &error)) {+ NSError *failure = CFBridgingRelease(error);+ SGLog(@"fonts: %@ not registered: %@", url.lastPathComponent, failure.localizedDescription);+ }+ return [UIFont fontWithName:name size:12] ? name : nil;+}++NSString *SGRegisterCustomFont(void) {+ NSString *name = registerFile(storedFile());+ if (name) [NSUserDefaults.standardUserDefaults setObject:name forKey:SGKeyAppFontName];+ return name;+}++static void tell(NSString *title, NSString *message, BOOL restart) {+ UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];+ if (restart) {+ [alert addAction:[UIAlertAction actionWithTitle:@"Later" style:UIAlertActionStyleCancel handler:nil]];+ [alert addAction:[UIAlertAction actionWithTitle:@"Restart now" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { SGRestartSpotify(); }]];+ } else {+ [alert addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel handler:nil]];+ }+ [SGTopController() presentViewController:alert animated:YES completion:nil];+}++// The file is checked before the one in use is touched, so a file Core Text will not read changes nothing.+static void import(NSURL *picked) {+ BOOL scoped = [picked startAccessingSecurityScopedResource];+ NSString *name = faceName(picked);+ NSURL *folder = fontFolder(), *old = storedFile();+ NSString *file = [@"Custom" stringByAppendingPathExtension:picked.pathExtension.length ? picked.pathExtension.lowercaseString : @"ttf"];+ NSURL *target = [folder URLByAppendingPathComponent:file];+ NSFileManager *files = NSFileManager.defaultManager;+ BOOL copied = NO;+ if (name) {+ if (old) {+ CTFontManagerUnregisterFontsForURL((__bridge CFURLRef)old, kCTFontManagerScopeProcess, NULL);+ [files removeItemAtURL:old error:nil];+ }+ [files removeItemAtURL:target error:nil];+ [files createDirectoryAtURL:folder withIntermediateDirectories:YES attributes:nil error:nil];+ copied = [files copyItemAtURL:picked toURL:target error:nil];+ }+ if (scoped) [picked stopAccessingSecurityScopedResource];+ name = copied ? registerFile(target) : nil;+ if (!name) {+ tell(@"Font not imported", @"Choose a valid .ttf or .otf font file.", NO);+ return;+ }+ NSUserDefaults *store = NSUserDefaults.standardUserDefaults;+ [store setObject:file forKey:SGKeyAppFontFile];+ [store setObject:name forKey:SGKeyAppFontName];+ SGSetInt(SGKeyAppFont, SGAppFontCustom);+ tell(@"Font imported", [NSString stringWithFormat:@"%@ becomes the app's font when Spotify starts again.", name], YES);+}++@interface SGFontPicker : NSObject <UIDocumentPickerDelegate>+@end++@implementation SGFontPicker++- (void)documentPicker:(UIDocumentPickerViewController *)picker didPickDocumentsAtURLs:(NSArray<NSURL *> *)urls {+ NSURL *picked = urls.firstObject;+ // The alert waits for the picker to be gone, or it has nothing to present over.+ dispatch_async(dispatch_get_main_queue(), ^{ if (picked) import(picked); });+}++@end++static void pickFont(void) {+ static SGFontPicker *delegate;+ if (!delegate) delegate = [SGFontPicker new];+ UIDocumentPickerViewController *picker = [[UIDocumentPickerViewController alloc] initForOpeningContentTypes:@[UTTypeFont] asCopy:YES];+ picker.delegate = delegate;+ picker.allowsMultipleSelection = NO;+ [SGTopController() presentViewController:picker animated:YES completion:nil];+}++NSArray<SGModRow *> *SGAppFontRows(void) {+ SGModRow *font = SGChoiceRow(@"Font", nil, SGKeyAppFont, SGAppFontNames(), SGAppFontSpotify);+ NSString *(^choice)(void) = font.value;+ font.value = ^NSString *{+ NSString *name = [NSUserDefaults.standardUserDefaults stringForKey:SGKeyAppFontName];+ return SGAppFontChosen() == SGAppFontCustom && name.length ? name : choice();+ };+ font.choiceFooter = @"Custom font uses a .ttf or .otf file of your own, imported from Files. It has one weight, so bold text comes out regular, and text Spotify draws itself may keep its own font.";+ SGModRow *import = SGActionRow(@"Import custom font", nil, ^{ pickFont(); });+ import.visible = ^BOOL { return SGAppFontChosen() == SGAppFontCustom; };+ return @[SGWithSymbol(font, @"textformat"), SGWithSymbol(import, @"square.and.arrow.down")];+}diff --git a/tweak/Sources/Shared/Fonts/Fonts.h b/tweak/Sources/Shared/Fonts/Fonts.hindex 29f6ff22..9bffac2d 100644--- a/tweak/Sources/Shared/Fonts/Fonts.h+++ b/tweak/Sources/Shared/Fonts/Fonts.h@@ -1,7 +1,11 @@-// One font for the whole app (Fonts.x), chosen on the Appearance card under either look.+// One font for the whole app (Fonts.x), chosen on the Appearance page under either look, and a font file+// of the user's own as one of the choices (FontImport.m).#import <UIKit/UIKit.h>#define SGKeyAppFont @"spotifyglass.font"+// The imported font: its file's name in Application Support/Vitrine/Font, and the PostScript name read from it.+#define SGKeyAppFontFile @"spotifyglass.font.file"+#define SGKeyAppFontName @"spotifyglass.font.name"typedef NS_ENUM(NSInteger, SGAppFont) {SGAppFontSpotify, // Spotify's own, untouched@@ -9,7 +13,14 @@ typedef NS_ENUM(NSInteger, SGAppFont) {SGAppFontRounded, // SF RoundedSGAppFontSerif, // New YorkSGAppFontMono, // SF Mono+ SGAppFontCustom, // a .ttf or .otf imported from Files};SGAppFont SGAppFontChosen(void);NSArray<NSString *> *SGAppFontNames(void);+// Registers the imported font with Core Text for this process and answers its PostScript name, or nil when+// there is no file or Core Text will not take it.+NSString *SGRegisterCustomFont(void);++@class SGModRow;+NSArray<SGModRow *> *SGAppFontRows(void); // the Font choice and, while Custom font is chosen, the importdiff --git a/tweak/Sources/Shared/Fonts/Fonts.x b/tweak/Sources/Shared/Fonts/Fonts.xindex 9e5fdaad..c18f18db 100644--- a/tweak/Sources/Shared/Fonts/Fonts.x+++ b/tweak/Sources/Shared/Fonts/Fonts.x@@ -1,12 +1,14 @@// One font for the whole app, under either look: Spotify's own (Circular and Spotify Mix, a file per-// script in SpotifyShared's Fonts.bundle), or the system's in one of its designs. Spotify asks for its-// fonts by name, so a name from its two families comes back as the system font at the same size, with-// the weight its name ends in. The rounded, serif and monospaced designs reach the system font too, so-// the mod's own text follows. Read at launch.+// script in SpotifyShared's Fonts.bundle), the system's in one of its designs, or a font file imported from+// Files (FontImport.m). Spotify asks for its fonts by name, so a name from its two families comes back as the+// system font at the same size, with the weight its name ends in. The rounded, serif and monospaced designs+// reach the system font too, so the mod's own text follows. An imported font takes the system font's place+// at the same size; it is one face, so every weight comes out in it. Read at launch.#import "Core/SGCore.h"#import "Shared/Fonts/Fonts.h"static SGAppFont sg_font;+static NSString *sg_custom; // the imported font's PostScript name, once Core Text has it for this launchstatic BOOL spotifys(NSString *name) {return [name hasPrefix:@"CircularSp"] || [name hasPrefix:@"SpotifyMix"] || [name hasPrefix:@"Circular"];@@ -31,26 +33,34 @@ static UIFontDescriptorSystemDesign designOf(SGAppFont font) {}}+// Nil if the imported font has gone since launch, so whoever asked gets the font they would have had.+static UIFont *custom(CGFloat size) {+ return [UIFont fontWithName:sg_custom size:size];+}+static UIFont *designed(UIFont *font) {if (sg_font < SGAppFontRounded || !font) return font;+ if (sg_font == SGAppFontCustom) return custom(font.pointSize) ?: font;UIFontDescriptor *descriptor = [font.fontDescriptor fontDescriptorWithDesign:designOf(sg_font)];return descriptor ? [UIFont fontWithDescriptor:descriptor size:font.pointSize] : font;}+// Nil only for an imported font that has gone, which leaves Spotify's own in place.static UIFont *systemFor(NSString *name, CGFloat size) {+ if (sg_font == SGAppFontCustom) return custom(size);return designed([UIFont systemFontOfSize:size weight:weightOf(name)]);}%hook UIFont+ (UIFont *)fontWithName:(NSString *)name size:(CGFloat)size {- if (spotifys(name)) return systemFor(name, size);- return %orig;+ UIFont *font = spotifys(name) ? systemFor(name, size) : nil;+ return font ?: %orig;}+ (UIFont *)fontWithDescriptor:(UIFontDescriptor *)descriptor size:(CGFloat)size {NSString *name = descriptor.fontAttributes[UIFontDescriptorNameAttribute] ?: descriptor.fontAttributes[UIFontDescriptorFamilyAttribute];- if (spotifys(name)) return systemFor(name, size > 0 ? size : descriptor.pointSize);- return %orig;+ UIFont *font = spotifys(name) ? systemFor(name, size > 0 ? size : descriptor.pointSize) : nil;+ return font ?: %orig;}%end@@ -75,17 +85,27 @@ static UIFont *systemFor(NSString *name, CGFloat size) {SGAppFont SGAppFontChosen(void) {NSInteger font = SGInt(SGKeyAppFont, SGAppFontSpotify);- return font >= SGAppFontSpotify && font <= SGAppFontMono ? font : SGAppFontSpotify;+ return font >= SGAppFontSpotify && font <= SGAppFontCustom ? font : SGAppFontSpotify;}NSArray<NSString *> *SGAppFontNames(void) {- return @[@"Spotify", @"System", @"Rounded", @"Serif", @"Mono"];+ return @[@"Spotify", @"System", @"Rounded", @"Serif", @"Mono", @"Custom font"];}%ctor {sg_font = SGAppFontChosen();if (sg_font == SGAppFontSpotify) return;+ // A process-wide registration ends with the process, so the file is registered again before any hook+ // runs. A file that is gone (a restored backup, a reinstall) leaves Spotify's font and keeps the choice,+ // so importing again is all it takes; Circular imported as a file of its own needs nothing done either.+ if (sg_font == SGAppFontCustom) {+ sg_custom = SGRegisterCustomFont();+ if (!sg_custom || spotifys(sg_custom)) {+ SGLog(@"fonts: the imported font is not there, Spotify's stays");+ return;+ }+ }%init;if (sg_font >= SGAppFontRounded) %init(Designs);- SGLog(@"fonts: %@ in place of Spotify's", SGAppFontNames()[sg_font]);+ SGLog(@"fonts: %@ in place of Spotify's", sg_custom ?: SGAppFontNames()[sg_font]);}