feat: finish font impl
This commit is contained in:
parent
c568969333
commit
bdf3792be9
4 changed files with 72 additions and 8 deletions
4
Makefile
4
Makefile
|
|
@ -7,9 +7,9 @@ include $(THEOS)/makefiles/common.mk
|
||||||
TWEAK_NAME = Bunny
|
TWEAK_NAME = Bunny
|
||||||
BUNDLE_NAME = BunnyResources
|
BUNDLE_NAME = BunnyResources
|
||||||
|
|
||||||
Bunny_FILES = $(shell find Sources -name "*.x" && find Sources -name "*.m")
|
Bunny_FILES = $(wildcard Sources/*.x Sources/*.m Sources/**/*.x Sources/**/*.m)
|
||||||
Bunny_CFLAGS = -fobjc-arc -DPACKAGE_VERSION='@"$(THEOS_PACKAGE_BASE_VERSION)"' -I$(THEOS_PROJECT_DIR)/Headers
|
Bunny_CFLAGS = -fobjc-arc -DPACKAGE_VERSION='@"$(THEOS_PACKAGE_BASE_VERSION)"' -I$(THEOS_PROJECT_DIR)/Headers
|
||||||
Bunny_FRAMEWORKS = Foundation UIKit CoreGraphics
|
Bunny_FRAMEWORKS = Foundation UIKit CoreGraphics CoreText CoreFoundation
|
||||||
|
|
||||||
BunnyResources_INSTALL_PATH = "/Library/Application\ Support/"
|
BunnyResources_INSTALL_PATH = "/Library/Application\ Support/"
|
||||||
BunnyResources_RESOURCE_DIRS = Resources
|
BunnyResources_RESOURCE_DIRS = Resources
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,9 @@
|
||||||
#import "Font.h"
|
#import "Fonts.h"
|
||||||
#import "Utils.h"
|
#import "Utils.h"
|
||||||
#import "Logger.h"
|
#import "Logger.h"
|
||||||
#import <CoreText/CoreText.h>
|
#import <CoreText/CoreText.h>
|
||||||
#import <CoreGraphics/CoreGraphics.h>
|
#import <CoreGraphics/CoreGraphics.h>
|
||||||
|
#import <CoreFoundation/CoreFoundation.h>
|
||||||
|
|
||||||
NSMutableDictionary<NSString *, NSString *> *fontMap;
|
NSMutableDictionary<NSString *, NSString *> *fontMap;
|
||||||
|
|
||||||
|
|
@ -11,7 +12,11 @@ NSMutableDictionary<NSString *, NSString *> *fontMap;
|
||||||
+ (UIFont *)fontWithName:(NSString *)name size:(CGFloat)size {
|
+ (UIFont *)fontWithName:(NSString *)name size:(CGFloat)size {
|
||||||
NSString *replacementName = fontMap[name];
|
NSString *replacementName = fontMap[name];
|
||||||
if (replacementName) {
|
if (replacementName) {
|
||||||
return %orig(replacementName, size);
|
UIFontDescriptor *replacementDescriptor = [UIFontDescriptor fontDescriptorWithName:replacementName size:size];
|
||||||
|
UIFontDescriptor *fallbackDescriptor = [replacementDescriptor fontDescriptorByAddingAttributes:@{UIFontDescriptorNameAttribute: @[name]}];
|
||||||
|
UIFontDescriptor *finalDescriptor = [replacementDescriptor fontDescriptorByAddingAttributes:@{UIFontDescriptorCascadeListAttribute: @[fallbackDescriptor]}];
|
||||||
|
|
||||||
|
return [UIFont fontWithDescriptor:finalDescriptor size:size];
|
||||||
}
|
}
|
||||||
return %orig;
|
return %orig;
|
||||||
}
|
}
|
||||||
|
|
@ -27,13 +32,39 @@ NSMutableDictionary<NSString *, NSString *> *fontMap;
|
||||||
return %orig;
|
return %orig;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
+ (UIFont *)systemFontOfSize:(CGFloat)size {
|
||||||
|
NSString *replacementName = fontMap[@"systemFont"];
|
||||||
|
if (replacementName) {
|
||||||
|
return [UIFont fontWithName:replacementName size:size];
|
||||||
|
}
|
||||||
|
return %orig;
|
||||||
|
}
|
||||||
|
|
||||||
|
+ (UIFont *)preferredFontForTextStyle:(UIFontTextStyle)style {
|
||||||
|
NSString *replacementName = fontMap[@"systemFont"];
|
||||||
|
if (replacementName) {
|
||||||
|
return [UIFont fontWithName:replacementName size:[UIFont systemFontSize]];
|
||||||
|
}
|
||||||
|
return %orig;
|
||||||
|
}
|
||||||
|
|
||||||
%end
|
%end
|
||||||
|
|
||||||
void patchFonts(NSDictionary<NSString *, NSString *> *mainFonts, NSString *fontDefName) {
|
void patchFonts(NSDictionary<NSString *, NSString *> *mainFonts, NSString *fontDefName) {
|
||||||
|
BunnyLog(@"patchFonts called with fonts: %@ and def name: %@", mainFonts, fontDefName);
|
||||||
|
|
||||||
if (!fontMap) {
|
if (!fontMap) {
|
||||||
|
BunnyLog(@"Creating new fontMap");
|
||||||
fontMap = [NSMutableDictionary dictionary];
|
fontMap = [NSMutableDictionary dictionary];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
NSString *fontJson = [NSString stringWithContentsOfURL:[getPyoncordDirectory() URLByAppendingPathComponent:@"fonts.json"]
|
||||||
|
encoding:NSUTF8StringEncoding
|
||||||
|
error:nil];
|
||||||
|
if (fontJson) {
|
||||||
|
BunnyLog(@"Found existing fonts.json: %@", fontJson);
|
||||||
|
}
|
||||||
|
|
||||||
for (NSString *fontName in mainFonts) {
|
for (NSString *fontName in mainFonts) {
|
||||||
NSString *url = mainFonts[fontName];
|
NSString *url = mainFonts[fontName];
|
||||||
BunnyLog(@"Replacing font %@ with URL: %@", fontName, url);
|
BunnyLog(@"Replacing font %@ with URL: %@", fontName, url);
|
||||||
|
|
@ -75,16 +106,31 @@ void patchFonts(NSDictionary<NSString *, NSString *> *mainFonts, NSString *fontD
|
||||||
if (font) {
|
if (font) {
|
||||||
CFStringRef postScriptName = CGFontCopyPostScriptName(font);
|
CFStringRef postScriptName = CGFontCopyPostScriptName(font);
|
||||||
|
|
||||||
|
CTFontRef existingFont = CTFontCreateWithName(postScriptName, 0, NULL);
|
||||||
|
if (existingFont) {
|
||||||
|
CFErrorRef unregisterError = NULL;
|
||||||
|
if (!CTFontManagerUnregisterGraphicsFont(font, &unregisterError)) {
|
||||||
|
BunnyLog(@"Failed to deregister font %@: %@", (__bridge NSString *)postScriptName,
|
||||||
|
unregisterError ? (__bridge NSString *)CFErrorCopyDescription(unregisterError) : @"Unknown error");
|
||||||
|
if (unregisterError) CFRelease(unregisterError);
|
||||||
|
}
|
||||||
|
CFRelease(existingFont);
|
||||||
|
}
|
||||||
|
|
||||||
CFErrorRef error = NULL;
|
CFErrorRef error = NULL;
|
||||||
if (CTFontManagerRegisterGraphicsFont(font, &error)) {
|
if (CTFontManagerRegisterGraphicsFont(font, &error)) {
|
||||||
fontMap[fontName] = (__bridge NSString *)postScriptName;
|
fontMap[fontName] = (__bridge NSString *)postScriptName;
|
||||||
BunnyLog(@"Successfully registered font %@ to %@", fontName, (__bridge NSString *)postScriptName);
|
BunnyLog(@"Successfully registered font %@ to %@", fontName, (__bridge NSString *)postScriptName);
|
||||||
|
|
||||||
|
NSError *jsonError;
|
||||||
|
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:fontMap options:0 error:&jsonError];
|
||||||
|
if (!jsonError) {
|
||||||
|
[jsonData writeToURL:[getPyoncordDirectory() URLByAppendingPathComponent:@"fontMap.json"] atomically:YES];
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
NSString *errorDesc = error ? (__bridge NSString *)CFErrorCopyDescription(error) : @"Unknown error";
|
NSString *errorDesc = error ? (__bridge NSString *)CFErrorCopyDescription(error) : @"Unknown error";
|
||||||
BunnyLog(@"Failed to register font %@: %@", fontName, errorDesc);
|
BunnyLog(@"Failed to register font %@: %@", fontName, errorDesc);
|
||||||
if (error) {
|
if (error) CFRelease(error);
|
||||||
CFRelease(error);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
CFRelease(postScriptName);
|
CFRelease(postScriptName);
|
||||||
|
|
@ -93,4 +139,12 @@ void patchFonts(NSDictionary<NSString *, NSString *> *mainFonts, NSString *fontD
|
||||||
CGDataProviderRelease(provider);
|
CGDataProviderRelease(provider);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
%ctor {
|
||||||
|
@autoreleasepool {
|
||||||
|
fontMap = [NSMutableDictionary dictionary];
|
||||||
|
BunnyLog(@"Font hooks initialized");
|
||||||
|
%init;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -3,7 +3,7 @@
|
||||||
#import "Utils.h"
|
#import "Utils.h"
|
||||||
#import "Logger.h"
|
#import "Logger.h"
|
||||||
#import "Theme.h"
|
#import "Theme.h"
|
||||||
#import "Font.h"
|
#import "Fonts.h"
|
||||||
#import "LoaderConfig.h"
|
#import "LoaderConfig.h"
|
||||||
|
|
||||||
static NSURL *source;
|
static NSURL *source;
|
||||||
|
|
@ -109,6 +109,16 @@ static LoaderConfig *loaderConfig;
|
||||||
%orig([jsCode dataUsingEncoding:NSUTF8StringEncoding], source, async);
|
%orig([jsCode dataUsingEncoding:NSUTF8StringEncoding], source, async);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
NSData *fontData = [NSData dataWithContentsOfURL:[pyoncordDirectory URLByAppendingPathComponent:@"fonts.json"]];
|
||||||
|
if (fontData) {
|
||||||
|
NSError *jsonError;
|
||||||
|
NSDictionary *fontDict = [NSJSONSerialization JSONObjectWithData:fontData options:0 error:&jsonError];
|
||||||
|
if (!jsonError && fontDict[@"main"]) {
|
||||||
|
BunnyLog(@"Found font configuration, applying...");
|
||||||
|
patchFonts(fontDict[@"main"], fontDict[@"name"]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (bundle) {
|
if (bundle) {
|
||||||
BunnyLog(@"Executing JS bundle");
|
BunnyLog(@"Executing JS bundle");
|
||||||
%orig(bundle, source, async);
|
%orig(bundle, source, async);
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue