Skip to content

Commit

Permalink
chore: add clang-format rules
Browse files Browse the repository at this point in the history
  • Loading branch information
castdrian committed Nov 16, 2024
1 parent e95baf5 commit 072bfff
Show file tree
Hide file tree
Showing 10 changed files with 556 additions and 604 deletions.
14 changes: 14 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
BasedOnStyle: LLVM
IndentWidth: 4
ObjCBlockIndentWidth: 4
UseTab: Never
ColumnLimit: 100
AccessModifierOffset: -4
AllowShortBlocksOnASingleLine: Empty
AllowShortFunctionsOnASingleLine: Empty
AllowShortCaseLabelsOnASingleLine: false
AllowShortEnumsOnASingleLine: false
AllowShortIfStatementsOnASingleLine: Never
AllowShortLambdasOnASingleLine: Empty
AllowShortLoopsOnASingleLine: false
AlignConsecutiveAssignments: Consecutive
3 changes: 1 addition & 2 deletions Headers/Fonts.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@
#import <UIKit/UIKit.h>

extern NSMutableDictionary<NSString *, NSString *> *fontMap;
void patchFonts(NSDictionary<NSString *, NSString *> *mainFonts,
NSString *fontDefName);
void patchFonts(NSDictionary<NSString *, NSString *> *mainFonts, NSString *fontDefName);
4 changes: 1 addition & 3 deletions Headers/RCTCxxBridge.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
@interface RCTCxxBridge : NSObject
- (void)executeApplicationScript:(NSData *)script
url:(NSURL *)url
async:(BOOL)async;
- (void)executeApplicationScript:(NSData *)script url:(NSURL *)url async:(BOOL)async;
@end
3 changes: 1 addition & 2 deletions Headers/Theme.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

void swizzleDCDThemeColor(
NSDictionary<NSString *, NSArray<NSString *> *> *semanticColors);
void swizzleDCDThemeColor(NSDictionary<NSString *, NSArray<NSString *> *> *semanticColors);
void swizzleUIColor(NSDictionary<NSString *, NSString *> *rawColors);
290 changes: 140 additions & 150 deletions Sources/Fonts.x
Original file line number Diff line number Diff line change
Expand Up @@ -10,178 +10,168 @@ NSMutableDictionary<NSString *, NSString *> *fontMap;
%hook UIFont

+ (UIFont *)fontWithName:(NSString *)name size:(CGFloat)size {
NSString *replacementName = fontMap[name];
if (replacementName) {
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;
NSString *replacementName = fontMap[name];
if (replacementName) {
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;
}

+ (UIFont *)fontWithDescriptor:(UIFontDescriptor *)descriptor
size:(CGFloat)size {
NSString *replacementName = fontMap[descriptor.postscriptName];
if (replacementName) {
UIFontDescriptor *replacementDescriptor =
[UIFontDescriptor fontDescriptorWithName:replacementName size:size];
UIFontDescriptor *finalDescriptor =
[replacementDescriptor fontDescriptorByAddingAttributes:@{
UIFontDescriptorCascadeListAttribute : @[ descriptor ]
}];

return [UIFont fontWithDescriptor:finalDescriptor size:size];
}
return %orig;
+ (UIFont *)fontWithDescriptor:(UIFontDescriptor *)descriptor size:(CGFloat)size {
NSString *replacementName = fontMap[descriptor.postscriptName];
if (replacementName) {
UIFontDescriptor *replacementDescriptor =
[UIFontDescriptor fontDescriptorWithName:replacementName size:size];
UIFontDescriptor *finalDescriptor =
[replacementDescriptor fontDescriptorByAddingAttributes:@{
UIFontDescriptorCascadeListAttribute : @[ descriptor ]
}];

return [UIFont fontWithDescriptor:finalDescriptor size:size];
}
return %orig;
}

+ (UIFont *)systemFontOfSize:(CGFloat)size {
NSString *replacementName = fontMap[@"systemFont"];
if (replacementName) {
return [UIFont fontWithName:replacementName size:size];
}
return %orig;
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;
NSString *replacementName = fontMap[@"systemFont"];
if (replacementName) {
return [UIFont fontWithName:replacementName size:[UIFont systemFontSize]];
}
return %orig;
}

%end

void patchFonts(NSDictionary<NSString *, NSString *> *mainFonts,
NSString *fontDefName) {
BunnyLog(@"patchFonts called with fonts: %@ and def name: %@", mainFonts,
fontDefName);

if (!fontMap) {
BunnyLog(@"Creating new fontMap");
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) {
NSString *url = mainFonts[fontName];
BunnyLog(@"Replacing font %@ with URL: %@", fontName, url);

NSURL *fontURL = [NSURL URLWithString:url];
NSString *fontExtension = fontURL.pathExtension;

NSURL *fontCachePath = [[[getPyoncordDirectory()
URLByAppendingPathComponent:@"downloads"
isDirectory:YES] URLByAppendingPathComponent:@"fonts"
isDirectory:YES]
URLByAppendingPathComponent:fontDefName
isDirectory:YES];

fontCachePath = [fontCachePath
URLByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@",
fontName,
fontExtension]];

NSURL *parentDir = [fontCachePath URLByDeletingLastPathComponent];
if (![[NSFileManager defaultManager] fileExistsAtPath:parentDir.path]) {
BunnyLog(@"Creating parent directory: %@", parentDir.path);
[[NSFileManager defaultManager] createDirectoryAtURL:parentDir
withIntermediateDirectories:YES
attributes:nil
error:nil];
void patchFonts(NSDictionary<NSString *, NSString *> *mainFonts, NSString *fontDefName) {
BunnyLog(@"patchFonts called with fonts: %@ and def name: %@", mainFonts, fontDefName);

if (!fontMap) {
BunnyLog(@"Creating new fontMap");
fontMap = [NSMutableDictionary dictionary];
}

if (![[NSFileManager defaultManager] fileExistsAtPath:fontCachePath.path]) {
BunnyLog(@"Downloading font %@ from %@", fontName, url);
NSData *data = [NSData dataWithContentsOfURL:fontURL];
if (data) {
BunnyLog(@"Writing font data to: %@", fontCachePath.path);
[data writeToURL:fontCachePath atomically:YES];
}
NSString *fontJson = [NSString
stringWithContentsOfURL:[getPyoncordDirectory() URLByAppendingPathComponent:@"fonts.json"]
encoding:NSUTF8StringEncoding
error:nil];
if (fontJson) {
BunnyLog(@"Found existing fonts.json: %@", fontJson);
}

NSData *fontData = [NSData dataWithContentsOfURL:fontCachePath];
if (fontData) {
BunnyLog(@"Registering font %@ with provider", fontName);
CGDataProviderRef provider =
CGDataProviderCreateWithCFData((__bridge CFDataRef)fontData);
CGFontRef font = CGFontCreateWithDataProvider(provider);

if (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);
for (NSString *fontName in mainFonts) {
NSString *url = mainFonts[fontName];
BunnyLog(@"Replacing font %@ with URL: %@", fontName, url);

NSURL *fontURL = [NSURL URLWithString:url];
NSString *fontExtension = fontURL.pathExtension;

NSURL *fontCachePath = [[[getPyoncordDirectory() URLByAppendingPathComponent:@"downloads"
isDirectory:YES]
URLByAppendingPathComponent:@"fonts"
isDirectory:YES] URLByAppendingPathComponent:fontDefName
isDirectory:YES];

fontCachePath = [fontCachePath
URLByAppendingPathComponent:[NSString
stringWithFormat:@"%@.%@", fontName, fontExtension]];

NSURL *parentDir = [fontCachePath URLByDeletingLastPathComponent];
if (![[NSFileManager defaultManager] fileExistsAtPath:parentDir.path]) {
BunnyLog(@"Creating parent directory: %@", parentDir.path);
[[NSFileManager defaultManager] createDirectoryAtURL:parentDir
withIntermediateDirectories:YES
attributes:nil
error:nil];
}

CFErrorRef error = NULL;
if (CTFontManagerRegisterGraphicsFont(font, &error)) {
fontMap[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 {
NSString *errorDesc =
error ? (__bridge NSString *)CFErrorCopyDescription(error)
: @"Unknown error";
BunnyLog(@"Failed to register font %@: %@", fontName, errorDesc);
if (error)
CFRelease(error);
if (![[NSFileManager defaultManager] fileExistsAtPath:fontCachePath.path]) {
BunnyLog(@"Downloading font %@ from %@", fontName, url);
NSData *data = [NSData dataWithContentsOfURL:fontURL];
if (data) {
BunnyLog(@"Writing font data to: %@", fontCachePath.path);
[data writeToURL:fontCachePath atomically:YES];
}
}

CFRelease(postScriptName);
CFRelease(font);
}
CGDataProviderRelease(provider);
NSData *fontData = [NSData dataWithContentsOfURL:fontCachePath];
if (fontData) {
BunnyLog(@"Registering font %@ with provider", fontName);
CGDataProviderRef provider =
CGDataProviderCreateWithCFData((__bridge CFDataRef)fontData);
CGFontRef font = CGFontCreateWithDataProvider(provider);

if (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;
if (CTFontManagerRegisterGraphicsFont(font, &error)) {
fontMap[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 {
NSString *errorDesc = error ? (__bridge NSString *)CFErrorCopyDescription(error)
: @"Unknown error";
BunnyLog(@"Failed to register font %@: %@", fontName, errorDesc);
if (error)
CFRelease(error);
}

CFRelease(postScriptName);
CFRelease(font);
}
CGDataProviderRelease(provider);
}
}
}
}

%ctor {
@autoreleasepool {
fontMap = [NSMutableDictionary dictionary];
BunnyLog(@"Font hooks initialized");
%init;
}
@autoreleasepool {
fontMap = [NSMutableDictionary dictionary];
BunnyLog(@"Font hooks initialized");
%init;
}
}
Loading

0 comments on commit 072bfff

Please sign in to comment.