Skip to content

Commit

Permalink
Merge pull request #38 from ginsudev/feature/batch-import
Browse files Browse the repository at this point in the history
Feature/batch import
  • Loading branch information
ginsudev authored Jan 4, 2023
2 parents 875ad86 + 0700676 commit 28f70a1
Show file tree
Hide file tree
Showing 8 changed files with 421 additions and 143 deletions.
30 changes: 25 additions & 5 deletions WDBFontOverwrite.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
4F4E64A7295F9AB600D4F04D /* ContentView.ViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4F4E64A6295F9AB600D4F04D /* ContentView.ViewModel.swift */; };
4FE5EF312963E460003384EC /* NoticeView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4FE5EF302963E460003384EC /* NoticeView.swift */; };
4FE5EF3329640075003384EC /* WDBImportCustomFontPickerViewControllerDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4FE5EF3229640075003384EC /* WDBImportCustomFontPickerViewControllerDelegate.swift */; };
4FE5EF3529653188003384EC /* FontMap.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4FE5EF3429653188003384EC /* FontMap.swift */; };
4FE5EF38296561A5003384EC /* FileEditorView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4FE5EF37296561A5003384EC /* FileEditorView.swift */; };
4FE5EF3A296561B2003384EC /* FileEditorView.ViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4FE5EF39296561B2003384EC /* FileEditorView.ViewModel.swift */; };
C55CF774295BA37D000DE71C /* woff2_wrapper.cpp in Sources */ = {isa = PBXBuildFile; fileRef = C55CF772295BA37D000DE71C /* woff2_wrapper.cpp */; };
C55CF776295BA9B1000DE71C /* BrotliPadding.swift in Sources */ = {isa = PBXBuildFile; fileRef = C55CF775295BA9B1000DE71C /* BrotliPadding.swift */; };
C55CF778295BAF42000DE71C /* libwoff2enc.a in Frameworks */ = {isa = PBXBuildFile; fileRef = C55CF76E295BA346000DE71C /* libwoff2enc.a */; };
Expand All @@ -30,6 +33,9 @@
4F4E64A6295F9AB600D4F04D /* ContentView.ViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.ViewModel.swift; sourceTree = "<group>"; };
4FE5EF302963E460003384EC /* NoticeView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NoticeView.swift; sourceTree = "<group>"; };
4FE5EF3229640075003384EC /* WDBImportCustomFontPickerViewControllerDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WDBImportCustomFontPickerViewControllerDelegate.swift; sourceTree = "<group>"; };
4FE5EF3429653188003384EC /* FontMap.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FontMap.swift; sourceTree = "<group>"; };
4FE5EF37296561A5003384EC /* FileEditorView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FileEditorView.swift; sourceTree = "<group>"; };
4FE5EF39296561B2003384EC /* FileEditorView.ViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FileEditorView.ViewModel.swift; sourceTree = "<group>"; };
C55CF76E295BA346000DE71C /* libwoff2enc.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; path = libwoff2enc.a; sourceTree = "<group>"; };
C55CF76F295BA346000DE71C /* libwoff2common.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; path = libwoff2common.a; sourceTree = "<group>"; };
C55CF772295BA37D000DE71C /* woff2_wrapper.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = woff2_wrapper.cpp; sourceTree = "<group>"; };
Expand Down Expand Up @@ -66,6 +72,15 @@
/* End PBXFrameworksBuildPhase section */

/* Begin PBXGroup section */
4FE5EF362965617D003384EC /* FileEditor */ = {
isa = PBXGroup;
children = (
4FE5EF37296561A5003384EC /* FileEditorView.swift */,
4FE5EF39296561B2003384EC /* FileEditorView.ViewModel.swift */,
);
path = FileEditor;
sourceTree = "<group>";
};
C55CF777295BAF42000DE71C /* Frameworks */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -96,12 +111,14 @@
C5C9A7A72959351A00466D87 /* Info.plist */,
C5A95F45295964AE00C58FDB /* PreviewFonts */,
C5C9A7A22959341600466D87 /* RepackedFonts */,
C5C9A7922959261000466D87 /* WDBFontOverwriteApp.swift */,
4FE5EF362965617D003384EC /* FileEditor */,
C55CF775295BA9B1000DE71C /* BrotliPadding.swift */,
C5C9A7942959261000466D87 /* ContentView.swift */,
4F4E64A6295F9AB600D4F04D /* ContentView.ViewModel.swift */,
C5C9A7A02959263A00466D87 /* OverwriteFontImpl.swift */,
C55CF775295BA9B1000DE71C /* BrotliPadding.swift */,
4FE5EF3429653188003384EC /* FontMap.swift */,
4FE5EF302963E460003384EC /* NoticeView.swift */,
C5C9A7A02959263A00466D87 /* OverwriteFontImpl.swift */,
C5C9A7922959261000466D87 /* WDBFontOverwriteApp.swift */,
4FE5EF3229640075003384EC /* WDBImportCustomFontPickerViewControllerDelegate.swift */,
C5C9A7962959261200466D87 /* Assets.xcassets */,
C5C9A7A92959417100466D87 /* vm_unaligned_copy_switch_race.c */,
Expand Down Expand Up @@ -202,10 +219,13 @@
buildActionMask = 2147483647;
files = (
C5999BFF295E769700BBBE1F /* stripttc.c in Sources */,
4FE5EF3A296561B2003384EC /* FileEditorView.ViewModel.swift in Sources */,
C5C9A7952959261000466D87 /* ContentView.swift in Sources */,
C55CF776295BA9B1000DE71C /* BrotliPadding.swift in Sources */,
C5C9A7932959261000466D87 /* WDBFontOverwriteApp.swift in Sources */,
4FE5EF38296561A5003384EC /* FileEditorView.swift in Sources */,
C5C9A7AA2959417100466D87 /* vm_unaligned_copy_switch_race.c in Sources */,
4FE5EF3529653188003384EC /* FontMap.swift in Sources */,
C5999BFC295E69C200BBBE1F /* ttcpad.m in Sources */,
4FE5EF312963E460003384EC /* NoticeView.swift in Sources */,
4FE5EF3329640075003384EC /* WDBImportCustomFontPickerViewControllerDelegate.swift in Sources */,
Expand Down Expand Up @@ -359,7 +379,7 @@
"$(inherited)",
"$(PROJECT_DIR)/WDBFontOverwrite",
);
MARKETING_VERSION = 1.9.1;
MARKETING_VERSION = 1.10.0;
PRODUCT_BUNDLE_IDENTIFIER = com.ginsudev.WDBFontOverwrite;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_EMIT_LOC_STRINGS = YES;
Expand Down Expand Up @@ -397,7 +417,7 @@
"$(inherited)",
"$(PROJECT_DIR)/WDBFontOverwrite",
);
MARKETING_VERSION = 1.9.1;
MARKETING_VERSION = 1.10.0;
PRODUCT_BUNDLE_IDENTIFIER = com.ginsudev.WDBFontOverwrite;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_EMIT_LOC_STRINGS = YES;
Expand Down
132 changes: 65 additions & 67 deletions WDBFontOverwrite/ContentView.ViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,39 @@ enum Notice: String {
case keyboard = "Keyboard fonts may not be applied immediately due to iOS caching issues. IF POSSIBLE, remove the folder /var/mobile/Library/Caches/com.apple.keyboards/ if you wish for changes to take effect immediately."
}

struct CustomFont {
var name: String
var targetPath: PathType?
var localPath: String
var alternativeTTCRepackMode: TTCRepackMode?
var notice: Notice?
}

struct FontToReplace {
var name: String
var postScriptName: String
var repackedPath: String
}

enum CustomFontType: String {
case font = "font"
case emoji = "emoji"
}

extension ContentView {
struct FontToReplace {
var name: String
var postScriptName: String
var repackedPath: String
}

struct CustomFont {
var name: String
var targetPath: PathType?
var localPath: String
var alternativeTTCRepackMode: TTCRepackMode?
var notice: Notice?
}

final class ViewModel: ObservableObject {
@Published var fontListSelection: Int = 0
@Published var customFontPickerSelection: Int = 0
@Published var message = "Choose a font."
@Published var progress: Progress!
@Published var importPresented: Bool = false
@Published var importName: String = ""
@Published var isPresentedFileEditor: Bool = false
@Published var importTTCRepackMode: TTCRepackMode = .woff2
@Published var allowsMultipleSelection: Bool = true
@Published var importType: CustomFontType = .font

var selectedCustomFont: CustomFont {
return customFonts[customFontPickerSelection]
var selectedCustomFontType: CustomFontType {
return customFontPickerSelection == 0 ? .font : .emoji
}

let fonts = [
Expand Down Expand Up @@ -93,55 +100,46 @@ extension ContentView {
repackedPath: "Chococooky.woff2"
),
]

let customFonts = [
CustomFont(
name: "SFUI.ttf",
targetPath: .single("/System/Library/Fonts/CoreUI/SFUI.ttf"),
localPath: "CustomSFUI.woff2",
alternativeTTCRepackMode: .ttcpad
),
CustomFont(
name: "Emoji",
targetPath: .many([
"/System/Library/Fonts/CoreAddition/AppleColorEmoji-160px.ttc",
"/System/Library/Fonts/Core/AppleColorEmoji.ttc",
]),
localPath: "CustomAppleColorEmoji.woff2"
),
CustomFont(
name: "SFUISoft.ttc",
targetPath: .single("/System/Library/Fonts/CoreUI/SFUISoft.ttc"),
localPath: "CustomSFUISoft.woff2",
alternativeTTCRepackMode: .ttcpad
),
CustomFont(
name: "PingFang.ttc",
targetPath: .single("/System/Library/Fonts/LanguageSupport/PingFang.ttc"),
localPath: "CustomPingFang.woff2",
alternativeTTCRepackMode: .ttcpad
),
CustomFont(
name: "Keycaps.ttc",
targetPath: .single("/System/Library/Fonts/CoreAddition/Keycaps.ttc"),
localPath: "CustomKeycaps.woff2",
alternativeTTCRepackMode: .ttcpad,
notice: .keyboard
),
CustomFont(
name: "KeycapsPad.ttc",
targetPath: .single("/System/Library/Fonts/CoreAddition/KeycapsPad.ttc"),
localPath: "CustomKeycapsPad.woff2",
alternativeTTCRepackMode: .ttcpad,
notice: .keyboard
),
CustomFont(
name: "PhoneKeyCaps.ttf",
targetPath: .single("/System/Library/Fonts/CoreAddition/PhoneKeyCaps.ttf"),
localPath: "CustomPhoneKeyCaps.woff2",
alternativeTTCRepackMode: .ttcpad,
notice: .keyboard
)
]

func batchOverwriteFonts() async {
guard selectedCustomFontType == .font else {
// Overwrite emoji
let emojiFont = FontMap.emojiCustomFont
overwriteWithCustomFont(
name: emojiFont.localPath,
targetPath: emojiFont.targetPath,
progress: progress
) {
self.progress = nil
self.message = $0
}
return
}

let fileManager = FileManager.default
let documentsDirectory = fileManager.urls(
for: .documentDirectory,
in: .userDomainMask
)[0]
do {
let fonts = try fileManager.contentsOfDirectory(atPath: documentsDirectory.relativePath).filter({!$0.contains("AppleColorEmoji")})
for font in fonts {
let key = FontMap.key(forFont: font)
if let customFont = FontMap.fontMap[key] {
overwriteWithCustomFont(
name: customFont.localPath,
targetPath: customFont.targetPath,
progress: progress
) {
self.progress = nil
self.message = $0
}
}
}
} catch {
print(error)
message = "Failed to read imported fonts."
}
}
}
}
Loading

0 comments on commit 28f70a1

Please sign in to comment.