Skip to content

Commit

Permalink
Merge pull request #139 from sst311212/main
Browse files Browse the repository at this point in the history
Fix build error & fonts script tweak
Rate limit · GitHub

Whoa there!

You have triggered an abuse detection mechanism.

Please wait a few minutes before you try again;
in some cases this may take up to an hour.

deividAlfa authored Nov 28, 2024
2 parents da06db8 + c74e707 commit 44bfa4f
Showing 4 changed files with 121 additions and 248 deletions.
1 change: 1 addition & 0 deletions Core/Inc/user_main.h
Original file line number Diff line number Diff line change
@@ -56,6 +56,7 @@ extern struct mallinfo mi;

#define WAKE_input() HAL_GPIO_ReadPin(WAKE_GPIO_Port, WAKE_Pin)
#define BUTTON_input() HAL_GPIO_ReadPin(ENC_SW_GPIO_Port, ENC_SW_Pin)
#define STAND_input() HAL_GPIO_ReadPin(STAND_GPIO_Port, STAND_Pin)

void Program_Handler(void);
void initBeforeMCUConfiguration(void);
235 changes: 106 additions & 129 deletions Drivers/graphics/u8g2/tools/font/generate_fonts/generate_fonts.py
Original file line number Diff line number Diff line change
@@ -1,129 +1,106 @@
import re, subprocess, os, shutil, time

# Universal file reading function
def read_file(file_path):
"""Read the content of a file."""
if os.path.exists(file_path):
with open(file_path, 'r', encoding='utf-8') as file:
return file.read()

# Universal file writing function
def write_file(file_path, content):
"""Write content to a file."""
with open(file_path, "w", encoding="utf-8") as file:
file.write(content)

# Function to extract specific characters
def extract_chars(code, filter_condition):
"""Extract specific variables and their values from the code based on a filter condition."""
regex_string = r'(\b[a-zA-Z_][a-zA-Z0-9_]*)\s*=\s*(.*)'
matches = re.findall(regex_string, code)
filtered_assignments = [(var, val) for var, val in matches if filter_condition(var)]
chars = "".join(value for var, value in filtered_assignments)
filtered_chars = set(filter(lambda c: ord(c) >= 19968, chars))
return filtered_chars

# Function to generate a mapping file
def make_map_file(file_path):
"""Generate a mapping file from the content of a text file."""
if os.path.exists(file_path):
data = read_file(file_path)
unicode_text = [hex(ord(c)).upper() + "\t" + hex(ord(c)).upper() for c in data]
unicode_text.sort()
return "\n".join(unicode_text)

# Function to run external commands
def run_command(command):
"""Execute an external command in the shell."""
subprocess.run(command, shell=True)

# Function to replace pixels in a file
def replace_pixel(file_path, ustr, nstr):
"""Replace a specific string with another in a file."""
if os.path.exists(file_path):
with open(file_path, 'r', encoding='utf-8') as file:
lines = file.readlines()
lines[7] = lines[7].replace(ustr, nstr)
with open(file_path, 'w', encoding='utf-8') as file:
file.writelines(lines)

# Function to merge mapping files
def mux_map_file(eng_path, ch_path, out_path):
"""Merge two mapping files into one."""
eng_data = read_file(eng_path)
start = eng_data.find("CHARS ") + 6
end = eng_data.find("STARTCHAR")
eng_chars = int(eng_data[start:end])
ch_data = read_file(ch_path)
start = ch_data.find("CHARS ") + 6
end = ch_data.find("STARTCHAR")
ch_chars = int(ch_data[start:end])
index = eng_data.find("ENDFONT")
start = ch_data.find("STARTCHAR")
end = ch_data.find("ENDFONT")
out_comt = "COMMENT Traditional Chinese Fonts start from here!!!\n"
out_data = eng_data[:index] + out_comt + ch_data[start:end] + eng_data[index:]
out_data = out_data.replace(f"CHARS {eng_chars}", f"CHARS {eng_chars + ch_chars}")
write_file(out_path, out_data)

# Main function
def main():
"""Main function to organize the flow of operations."""
gui_strings_path = os.path.join("..", "..", "..", "..", "gui", "screens", "gui_strings.c")
output_dir = "output"

# Ensure the output directory exists
if not os.path.exists(output_dir):
os.makedirs(output_dir)

# Generate menu files and mapping files
menu_chars = extract_chars(read_file(gui_strings_path), lambda var: 'main_mode_' not in var and 'main_error_' not in var)
small_chars = extract_chars(read_file(gui_strings_path), lambda var: 'main_mode' in var or 'main_error' in var)

write_file(os.path.join(output_dir, "font_menu_ch.txt"), "".join(menu_chars) + f"简繁└")
write_file(os.path.join(output_dir, "font_small_ch.txt"), "".join(small_chars))

map_file_data_menu = make_map_file(os.path.join(output_dir, "font_menu_ch.txt"))
map_file_data_small = make_map_file(os.path.join(output_dir, "font_small_ch.txt"))

write_file(os.path.join(output_dir, "font_menu_ch.map"), map_file_data_menu)
write_file(os.path.join(output_dir, "font_small_ch.map"), map_file_data_small)

# Call external programs to generate BDF files
run_command(f"..\\otf2bdf\\otf2bdf.exe -m {os.path.join(output_dir, 'font_menu_ch.map')} -p 9 fireflysung.ttf -o {os.path.join(output_dir, 'font_menu_ch.bdf')}")
run_command(f"..\\otf2bdf\\otf2bdf.exe -m {os.path.join(output_dir, 'font_small_ch.map')} -p 8 fireflysung.ttf -o {os.path.join(output_dir, 'font_small_ch.bdf')}")

# Merge mapping files
mux_map_file(os.path.join("..", "bdf", "font_menu.bdf"), os.path.join(output_dir, "font_menu_ch.bdf"), os.path.join(output_dir, "font_menu_all.bdf"))
mux_map_file(os.path.join("..", "bdf", "font_small.bdf"), os.path.join(output_dir, "font_small_ch.bdf"), os.path.join(output_dir, "font_small_all.bdf"))

# Call external programs to generate C files
run_command(f"..\\bdfconv\\bdfconv.exe -v -b 0 -f 1 -m \"32-126,160-255,268-271,282-283,286,287,304,305,327,328,344,345,350-353,356,357,366,367,381,382,937,1040-1103,5000-65535\" -o {os.path.join(output_dir, 'font_menu.c')} -n u8g2_font_menu {os.path.join(output_dir, 'font_menu_all.bdf')}")
run_command(f"..\\bdfconv\\bdfconv.exe -v -b 0 -f 1 -m \"32-126,160-255,268-271,282-283,286,287,304,305,327,328,344,345,350-353,356,357,366,367,381,382,937,1040-1103,5000-65535\" -o {os.path.join(output_dir, 'font_small.c')} -n u8g2_font_small {os.path.join(output_dir, 'font_small_all.bdf')}")
run_command(f"..\\bdfconv\\bdfconv.exe -v -b 0 -f 1 -m \"45,48-57,67,70,176\" \"../bdf/ITC Avant Garde Gothic Medium_31.bdf\" -o {os.path.join(output_dir, 'font_iron_temp.c')} -n u8g2_font_iron_temp")

# Replace specific characters
replace_pixel(os.path.join(output_dir, "font_iron_temp.c"), "\\42\\", "\\45\\")
replace_pixel(os.path.join(output_dir, "font_menu.c"), "\\21\\", "\\17\\")
replace_pixel(os.path.join(output_dir, "font_small.c"), "\\16\\", "\\12\\")

# Merge C files
font_files = [os.path.join(output_dir, "font_iron_temp.c"),
os.path.join(output_dir, "font_menu.c"),
os.path.join(output_dir, "font_small.c")]
combined_font_file = "u8g2_aio.c"
with open(combined_font_file, "w") as combined_file:
for font_file in font_files:
with open(font_file, "r") as f:
combined_file.write(f.read())

# Clean up temporary files
shutil.rmtree(output_dir)

if __name__ == '__main__':
start_time = time.perf_counter()
main()
end_time = time.perf_counter()
execution_time = end_time - start_time
print(f"u8g2_aio.c Generation Finished!\nExecution Time {execution_time:.3f} seconds!")
os.system("pause")
import os
import subprocess
import traceback

def hasChineseChar(data: str):
chr_list = set(data)
truth_table = map(lambda elm : ord(elm) > 5000, chr_list)
return any(truth_table)

def makeBDFMapFile(data: list, file: str):
map_list = map(lambda elm : f"0x{ord(elm):02X}\t0x{ord(elm):02X}\n", data)
open(file, "w", encoding="utf-8").write("".join(map_list))
return

def exec_command(parameters: str | list):
subprocess.run(parameters, shell=True)
return

def muxBDFMapFile(engFile: str, chiFile: str, outFile: str):
eng_data = open(engFile, encoding="utf-8").read()
i = eng_data.find("CHARS ") + 6
j = eng_data.find("STARTCHAR")
eng_nums = int(eng_data[i:j])

chi_data = open(chiFile, encoding="utf-8").read()
i = chi_data.find("CHARS ") + 6
j = chi_data.find("STARTCHAR")
cht_nums = int(chi_data[i:j])

ei = eng_data.rfind("ENDFONT")
ci = chi_data.find("STARTCHAR")
cj = chi_data.rfind("ENDFONT")

out_comt = "COMMENT Chinese Fonts start from here!!!\n"
# out_comt = out_comt + "COMMENT Please add fonts above for better maintenance\n"
out_data = eng_data[:ei] + out_comt + chi_data[ci:cj] + eng_data[ei:]
out_data = out_data.replace(f"CHARS {eng_nums}", f"CHARS {eng_nums + cht_nums}")

open(outFile, "w", encoding="utf-8").write(out_data)
return

def replaceFileString(file: str, old: str, new: str):
data = open(file, encoding="utf-8").read()
data = data.replace(old, new, 1)
open(file, "w", encoding="utf-8").write(data)
return

if __name__ == "__main__":

try:
os.chdir(os.path.dirname(__file__))
os.makedirs("temp", exist_ok=True)

# Read all chinese lines from gui_strings.c
gui_lines = open(r"..\..\..\..\gui\screens\gui_strings.c", encoding="utf-8").readlines()
chi_lines = list(filter(lambda elm : hasChineseChar(elm), gui_lines))

# Separate menu and small lines from all chinese lines
chi_menu_lines = list(filter(lambda elm : ".main_error_" not in elm and ".main_mode_" not in elm, chi_lines))
chi_smol_lines = list(filter(lambda elm : elm not in chi_menu_lines, chi_lines))

# Extract only chinese characters from lines
chi_menu_chars = sorted(set(filter(lambda elm : ord(elm) > 5000, "".join(chi_menu_lines))))
chi_smol_chars = sorted(set(filter(lambda elm : ord(elm) > 5000, "".join(chi_smol_lines))))
open(r"temp\font_menu_chi.txt", "w", encoding="utf-8").write("\n".join(chi_menu_chars))
open(r"temp\font_smol_chi.txt", "w", encoding="utf-8").write("\n".join(chi_smol_chars))

# Generate chinese BDF files
makeBDFMapFile(chi_menu_chars, r"temp\font_menu_chi.map")
makeBDFMapFile(chi_smol_chars, r"temp\font_smol_chi.map")
exec_command([r"..\otf2bdf\otf2bdf.exe", "-m", r"temp\font_menu_chi.map", "-p", "9", "-o", r"temp\font_menu_chi.bdf", "fireflysung.ttf"])
exec_command([r"..\otf2bdf\otf2bdf.exe", "-m", r"temp\font_smol_chi.map", "-p", "8", "-o", r"temp\font_smol_chi.bdf", "fireflysung.ttf"])

# Merge english and chinese BDF files
muxBDFMapFile(r"..\bdf\font_menu.bdf", r"temp\font_menu_chi.bdf", r"temp\font_menu_all.bdf")
muxBDFMapFile(r"..\bdf\font_small.bdf", r"temp\font_smol_chi.bdf", r"temp\font_smol_all.bdf")

# Generate C language font source
uni_mapset = "32-126,160-255,268-382,937,1040-1103"
chi_mapset = "5000-65535"
all_mapset = ",".join([uni_mapset,chi_mapset])
exec_command([r"..\bdfconv\bdfconv.exe", "-v", "-b", "0", "-f", "1", "-m", all_mapset, "-o", r"temp\font_menu.c", "-n", "u8g2_font_menu", r"temp\font_menu_all.bdf"])
exec_command([r"..\bdfconv\bdfconv.exe", "-v", "-b", "0", "-f", "1", "-m", all_mapset, "-o", r"temp\font_smol.c", "-n", "u8g2_font_small", r"temp\font_smol_all.bdf"])
exec_command([r"..\bdfconv\bdfconv.exe", "-v", "-b", "0", "-f", "1", "-m", uni_mapset, "-o", r"temp\font_menu_no_CN.c", "-n", "u8g2_font_menu_no_CN", r"temp\font_menu_all.bdf"])
exec_command([r"..\bdfconv\bdfconv.exe", "-v", "-b", "0", "-f", "1", "-m", uni_mapset, "-o", r"temp\font_smol_no_CN.c", "-n", "u8g2_font_small_no_CN", r"temp\font_smol_all.bdf"])
exec_command([r"..\bdfconv\bdfconv.exe", "-v", "-b", "0", "-f", "1", "-m", "45,48-57,67,70,176", "-o", r"temp\font_iron_temp.c", "-n", "u8g2_font_iron_temp", r"..\bdf\ITC Avant Garde Gothic Medium_31.bdf"])

# Patch incorrect font height
replaceFileString(r"temp\font_iron_temp.c", "\\42\\0", "\\45\\0")
replaceFileString(r"temp\font_menu.c", "\\21\\0", "\\17\\0")
replaceFileString(r"temp\font_smol.c", "\\16\\0", "\\12\\0")
replaceFileString(r"temp\font_menu_no_CN.c", "\\21\\0", "\\17\\0")
replaceFileString(r"temp\font_smol_no_CN.c", "\\16\\0", "\\12\\0")

# Combine all font sources into one file
exec_command(["copy", "/b", r"temp\font_iron_temp.c", "+", r"temp\font_menu.c", "+", r"temp\font_smol.c", "u8g2_aio.c"])
exec_command(["copy", "/b", "u8g2_aio.c", "+", r"temp\font_menu_no_CN.c", "+", r"temp\font_smol_no_CN.c", "u8g2_aio.c"])

except Exception:
# Print error message
traceback.print_exc()

finally:
# Cleanup unnecessary files
exec_command("rmdir /s /q temp")
os.system("timeout 3")

This file was deleted.

Rate limit · GitHub

Whoa there!

You have triggered an abuse detection mechanism.

Please wait a few minutes before you try again;
in some cases this may take up to an hour.

0 comments on commit 44bfa4f

Please sign in to comment.