ImguiManager: Only rasterize font awesome glyphs which we need

This commit is contained in:
Connor McLaughlin 2022-08-26 23:16:15 +10:00
parent f9bd2db2dc
commit ea8d779962
2 changed files with 91 additions and 1 deletions

View File

@ -0,0 +1,80 @@
import sys
import os
import glob
import re
#src_file = "src/duckstation-qt/qttranslations.cpp"
src_dir = os.path.join(os.path.dirname(__file__), "..", "src")
fa_file = os.path.join(os.path.dirname(__file__), "..", "dep", "imgui", "include", "IconsFontAwesome5.h")
dst_file = os.path.join(os.path.dirname(__file__), "..", "src", "frontend-common", "imgui_manager.cpp")
all_source_files = glob.glob(os.path.join(src_dir, "**", "*.cpp"), recursive=True) + \
glob.glob(os.path.join(src_dir, "**", "*.h"), recursive=True) + \
glob.glob(os.path.join(src_dir, "**", "*.inl"), recursive=True)
tokens = set()
for filename in all_source_files:
data = None
with open(filename, "r") as f:
try:
data = f.read()
except:
continue
tokens = tokens.union(set(re.findall("(ICON_FA_[a-zA-Z0-9_]+)", data)))
print("{} tokens found.".format(len(tokens)))
if len(tokens) == 0:
sys.exit(0)
u8_encodings = {}
with open(fa_file, "r") as f:
for line in f.readlines():
match = re.match("#define (ICON_FA_[^ ]+) \"([^\"]+)\"", line)
if match is None:
continue
u8_encodings[match[1]] = bytes.fromhex(match[2].replace("\\x", ""))
out_pattern = "(static constexpr ImWchar range_fa\[\] = \{)[0-9A-Z_a-z, \n]+(\};)"
codepoints = list()
for token in tokens:
u8_bytes = u8_encodings[token]
u8 = str(u8_bytes, "utf-8")
u16 = u8.encode("utf-16le")
if len(u16) > 2:
raise ValueError("{} too long".format(u8_bytes))
codepoint = int.from_bytes(u16, byteorder="little", signed=False)
codepoints.append(codepoint)
codepoints.sort()
codepoints.append(0) # null terminator
startc = None
endc = None
pairs = []
for codepoint in codepoints:
if endc is not None and (endc + 1) != codepoint:
if startc is not None:
pairs.append(startc)
pairs.append(endc)
startc = codepoint
endc = codepoint
else:
endc = codepoint
if startc is not None:
pairs.append(startc)
pairs.append(endc)
pairs_str = ",".join(list(map(lambda x: "0x{:x}".format(x), pairs)))
with open(dst_file, "r") as f:
original = f.read()
updated = re.sub(out_pattern, "\\1 " + pairs_str + " \\2", original)
if original != updated:
with open(dst_file, "w") as f:
f.write(updated)
print("Updated {}".format(dst_file))
else:
print("Skipping updating {}".format(dst_file))

View File

@ -458,7 +458,17 @@ ImFont* ImGuiManager::AddFixedFont(float size)
bool ImGuiManager::AddIconFonts(float size)
{
static const ImWchar range_fa[] = {ICON_MIN_FA, ICON_MAX_FA, 0};
static constexpr ImWchar range_fa[] = {
0xf005, 0xf005, 0xf007, 0xf007, 0xf00c, 0xf00e, 0xf011, 0xf011, 0xf013, 0xf013, 0xf017, 0xf017, 0xf019, 0xf019,
0xf021, 0xf021, 0xf025, 0xf025, 0xf027, 0xf028, 0xf02d, 0xf02e, 0xf030, 0xf030, 0xf03a, 0xf03a, 0xf049, 0xf04c,
0xf050, 0xf050, 0xf059, 0xf059, 0xf05e, 0xf05e, 0xf065, 0xf065, 0xf067, 0xf067, 0xf071, 0xf071, 0xf075, 0xf075,
0xf077, 0xf078, 0xf07b, 0xf07c, 0xf084, 0xf084, 0xf091, 0xf091, 0xf0a0, 0xf0a0, 0xf0ac, 0xf0ad, 0xf0c5, 0xf0c5,
0xf0c7, 0xf0c8, 0xf0cb, 0xf0cb, 0xf0d0, 0xf0d0, 0xf0e2, 0xf0e2, 0xf0eb, 0xf0eb, 0xf0f1, 0xf0f1, 0xf0f3, 0xf0f3,
0xf0fe, 0xf0fe, 0xf119, 0xf119, 0xf11b, 0xf11c, 0xf140, 0xf140, 0xf144, 0xf144, 0xf14a, 0xf14a, 0xf15b, 0xf15b,
0xf188, 0xf188, 0xf191, 0xf192, 0xf1dd, 0xf1de, 0xf1e6, 0xf1e6, 0xf1eb, 0xf1eb, 0xf1f8, 0xf1f8, 0xf26c, 0xf26c,
0xf279, 0xf279, 0xf2d0, 0xf2d0, 0xf2db, 0xf2db, 0xf2f2, 0xf2f2, 0xf2f5, 0xf2f5, 0xf410, 0xf410, 0xf466, 0xf466,
0xf500, 0xf500, 0xf51f, 0xf51f, 0xf552, 0xf552, 0xf57a, 0xf57a, 0xf5a2, 0xf5a2, 0xf65d, 0xf65e, 0xf6a9, 0xf6a9,
0xf7c2, 0xf7c2, 0xf807, 0xf807, 0xf815, 0xf815, 0xf818, 0xf818, 0x0, 0x0};
ImFontConfig cfg;
cfg.MergeMode = true;