1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884
| """ Convert TTF font to M5ReadPaper binary format Based on M5ReadPaper's font structure """
from PIL import Image, ImageDraw, ImageFont import struct import sys import os from fontTools.ttLib import TTFont
if not getattr(sys, 'frozen', False): os.chdir(os.path.join(os.path.dirname(os.path.abspath(__file__)), '..'))
def get_common_chinese_chars(): """Get common Traditional Chinese characters for e-books""" chars = set() chars.update(chr(i) for i in range(32, 127)) common_ranges = [ (0x4E00, 0x9FFF), (0x3000, 0x303F), (0xFF00, 0xFFEF), (0xFE10, 0xFE1F), (0xFE30, 0xFE4F), (0x2000, 0x206F), (0x00A0, 0x00FF), (0x2100, 0x214F), (0x25A0, 0x25FF), ] for start, end in common_ranges: chars.update(chr(i) for i in range(start, end + 1)) return sorted(list(chars), key=lambda x: ord(x))
def render_glyph(font, char, font_size): """Render a single character to bitmap, returning bearing offsets""" canvas_size = font_size * 3 img = Image.new('1', (canvas_size, canvas_size), 1) draw = ImageDraw.Draw(img) origin = font_size try: bbox = draw.textbbox((origin, origin), char, font=font) text_width = bbox[2] - bbox[0] text_height = bbox[3] - bbox[1] if text_width <= 0 or text_height <= 0: return None, 0, 0, 0, 0 draw.text((origin, origin), char, font=font, fill=0) img_crop = img.crop(bbox) bearing_x = bbox[0] - origin bearing_y = bbox[1] - origin return img_crop, text_width, text_height, bearing_x, bearing_y except Exception as e: print(f"Error rendering '{char}' (U+{ord(char):04X}): {e}") return None, 0, 0, 0, 0
def bitmap_to_bytes(img): """Convert PIL 1-bit image to packed bytes""" pixels = img.getdata() bits = [1 if p == 0 else 0 for p in pixels] pad = (8 - len(bits) % 8) % 8 bits.extend([0] * pad) bytes_data = bytearray() for i in range(0, len(bits), 8): byte = 0 for j in range(8): byte |= (bits[i + j] << (7 - j)) bytes_data.append(byte) return bytes(bytes_data)
VERT_BRACKETS_TO_HORIZ = { 0xFE41: 0x300C, 0xFE42: 0x300D, 0xFE43: 0x300E, 0xFE44: 0x300F, 0xFE3F: 0x3008, 0xFE40: 0x3009, 0xFE3D: 0x300A, 0xFE3E: 0x300B, 0xFE3B: 0x3010, 0xFE3C: 0x3011, 0xFE35: 0xFF08, 0xFE36: 0xFF09, 0xFE17: 0x3016, 0xFE18: 0x3017, 0xFE39: 0x3014, 0xFE3A: 0x3015, 0xFE37: 0xFF5B, 0xFE38: 0xFF5D, 0xFE47: 0xFF3B, 0xFE48: 0xFF3D, }
def render_rotated_glyph(font, horiz_char, font_size): """Render a horizontal character and rotate 90° clockwise to create vertical form.""" canvas_size = font_size * 3 img = Image.new('L', (canvas_size, canvas_size), 255) draw = ImageDraw.Draw(img) try: bbox = draw.textbbox((0, 0), horiz_char, font=font) text_width = bbox[2] - bbox[0] text_height = bbox[3] - bbox[1] if text_width <= 0 or text_height <= 0: return None, 0, 0, 0, 0 draw.text((0, 0), horiz_char, font=font, fill=0) img_crop = img.crop(bbox) img_rot = img_crop.rotate(-90, expand=True) img_1bit = img_rot.point(lambda p: 0 if p < 128 else 1, '1') rot_w, rot_h = img_1bit.size bearing_x = (font_size - rot_w) // 2 bearing_y = (font_size - rot_h) // 2 return img_1bit, rot_w, rot_h, bearing_x, bearing_y except Exception as e: return None, 0, 0, 0, 0
PUNCT_RANGES = [ (0x2000, 0x206F), (0x3000, 0x303F), (0xFE10, 0xFE1F), (0xFE30, 0xFE4F), (0xFF00, 0xFFEF), (0x00A0, 0x00BF), ]
def _is_punct(cp): """Return True if codepoint falls in a punctuation range.""" return any(s <= cp < e for s, e in PUNCT_RANGES)
def get_font_display_name(font_path): """Extract display name from a font file, preferring Traditional Chinese.""" try: tt = TTFont(font_path, fontNumber=0) name_table = tt['name'] font_family = None font_family_en = None tc_lang_ids = {1028, 3076} cjk_lang_ids = {1028, 2052, 3076, 1041, 1042} for record in name_table.names: if record.nameID == 1: try: decoded = record.toUnicode() if decoded: if record.platformID == 3 and record.langID in tc_lang_ids: font_family = decoded break elif record.platformID == 3 and record.langID in cjk_lang_ids: if not font_family: font_family = decoded elif record.platformID == 1 and record.platEncID == 2: if not font_family: font_family = decoded elif font_family_en is None: font_family_en = decoded except Exception: pass tt.close() if font_family: return font_family if font_family_en: _NAME_OVERRIDES = { 'Noto Sans TC': '思源黑體', 'Noto Serif TC': '思源宋體', } return _NAME_OVERRIDES.get(font_family_en, font_family_en) except Exception: pass return os.path.splitext(os.path.basename(font_path))[0]
def convert_ttf_to_bin(ttf_path, output_path, font_size=30, fallback_path=None, render_size=None, target_size=None, force_fallback_punct=False): """Convert TTF to M5ReadPaper binary format. If fallback_path is given, borrow missing glyphs from the fallback font. If render_size is given, render glyphs at that size but store render_size in header. target_size is the nominal/equivalent size (e.g. 36 for Silver) for fallback sizing. If force_fallback_punct is True, all punctuation glyphs are taken from the fallback font.""" if render_size is None: render_size = font_size if target_size is None: target_size = font_size header_font_size = render_size print(f"Loading font: {ttf_path}") print(f"Font size: {font_size}pt (render at {render_size}pt)") if fallback_path: print(f"Fallback font: {fallback_path}") font_family = get_font_display_name(ttf_path) primary_cmap = set() try: tt = TTFont(ttf_path, fontNumber=0) for table in tt['cmap'].tables: primary_cmap.update(table.cmap.keys()) tt.close() print(f"Font family: {font_family}") print(f"Primary font cmap: {len(primary_cmap)} codepoints") except Exception as e: print(f"Warning: Could not read cmap: {e}") try: font = ImageFont.truetype(ttf_path, render_size) except Exception as e: print(f"Error loading font: {e}") return False fallback_render_size = font_size if render_size != font_size: sample_chars = '盡陀人心世郡第一大是國中不為' sample_img = Image.new('1', (render_size * 3, render_size * 3), 1) sample_draw = ImageDraw.Draw(sample_img) sample_widths = [] for sc in sample_chars: sb = sample_draw.textbbox((0, 0), sc, font=font) sw = sb[2] - sb[0] if sw > 0: sample_widths.append(sw) if sample_widths: avg_glyph = sum(sample_widths) / len(sample_widths) fallback_render_size = int(avg_glyph + 0.5) print(f"Silver mode: primary avg glyph={avg_glyph:.1f}px, fallback will render at {fallback_render_size}pt")
fallback_font = None if fallback_path: try: fallback_font = ImageFont.truetype(fallback_path, fallback_render_size) except Exception as e: print(f"Warning: Could not load fallback font: {e}") auto_fallback_path = None if not fallback_font: auto_fallback = 'sd_card/fonts/GenYoMinTW-Regular.ttf' if os.path.exists(auto_fallback) and os.path.abspath(auto_fallback) != os.path.abspath(ttf_path): try: fallback_font = ImageFont.truetype(auto_fallback, fallback_render_size) auto_fallback_path = auto_fallback print(f"Auto-detected fallback font: {auto_fallback}") except Exception: pass fallback_cmap = set() fb_cmap_path = fallback_path or auto_fallback_path if fallback_font and fb_cmap_path: try: tt_fb = TTFont(fb_cmap_path, fontNumber=0) for table in tt_fb['cmap'].tables: fallback_cmap.update(table.cmap.keys()) tt_fb.close() print(f"Fallback font cmap: {len(fallback_cmap)} codepoints") except Exception: pass print("Building character set...") chars = get_common_chinese_chars() print(f"Total characters: {len(chars)}") index_entries = [] bitmap_data = bytearray()
keep_native_bearing = is_bpmfzihi_font(ttf_path, font_family) if keep_native_bearing: print("Bpmf Zihi mode: preserving native glyph bearings for primary glyphs") print("Rendering glyphs...") fallback_count = 0 rotated_count = 0 for i, char in enumerate(chars): if i % 100 == 0: print(f" Progress: {i}/{len(chars)} ({100*i//len(chars)}%)") cp = ord(char) use_fallback_first = force_fallback_punct and fallback_font and _is_punct(cp) in_primary = cp in primary_cmap in_fallback = cp in fallback_cmap
img = None glyph_from_primary = False if use_fallback_first and in_fallback: img, width, height, bearing_x, bearing_y = render_glyph(fallback_font, char, fallback_render_size) if img is not None: fallback_count += 1
if img is None and in_primary: img, width, height, bearing_x, bearing_y = render_glyph(font, char, render_size) if img is not None: glyph_from_primary = True if img is None and cp in VERT_BRACKETS_TO_HORIZ: horiz_cp = VERT_BRACKETS_TO_HORIZ[cp] if horiz_cp in primary_cmap: img, width, height, bearing_x, bearing_y = render_rotated_glyph(font, chr(horiz_cp), render_size) if img is not None: rotated_count += 1 glyph_from_primary = True if img is None and fallback_font and not use_fallback_first and in_fallback: img, width, height, bearing_x, bearing_y = render_glyph(fallback_font, char, fallback_render_size) if img is not None: fallback_count += 1 if img is None: continue bitmap_bytes = bitmap_to_bytes(img) unicode_val = ord(char) bitmap_size = len(bitmap_bytes) index_entries.append({ 'unicode': unicode_val, 'width': width, 'height': height, 'size': bitmap_size, 'bearing_x': bearing_x, 'bearing_y': bearing_y, 'bitmap': bitmap_bytes }) print(f"Successfully rendered {len(index_entries)} glyphs ({rotated_count} rotated, {fallback_count} from fallback)") if fallback_count > len(index_entries) * 0.5: print(f"⚠ WARNING: {fallback_count}/{len(index_entries)} glyphs ({100*fallback_count//len(index_entries)}%) came from fallback font!") print(f" This usually means the primary font's cmap could not be read (e.g. TTC without fontNumber).") print("Calculating offsets...") header_size = 137 index_size = len(index_entries) * 20 current_offset = 0 for entry in index_entries: entry['offset'] = header_size + index_size + current_offset bitmap_data.extend(entry['bitmap']) current_offset += entry['size'] del entry['bitmap'] print(f"Writing binary font: {output_path}") with open(output_path, 'wb') as f: char_count = len(index_entries) version = 2 family_name = font_family.encode('utf-8')[:64].ljust(64, b'\x00') style_name = b"Regular".ljust(64, b'\x00') f.write(struct.pack('<I', char_count)) f.write(struct.pack('<B', header_font_size)) f.write(struct.pack('<I', version)) f.write(family_name) f.write(style_name) for entry in index_entries: f.write(struct.pack('<I', entry['unicode'])) f.write(struct.pack('<H', entry['width'])) f.write(struct.pack('<H', entry['height'])) f.write(struct.pack('<I', entry['offset'])) f.write(struct.pack('<I', entry['size'])) f.write(struct.pack('<h', entry['bearing_x'])) f.write(struct.pack('<h', entry['bearing_y'])) f.write(bitmap_data) file_size = os.path.getsize(output_path) print(f"✓ Binary font created: {file_size:,} bytes") print(f" Characters: {char_count}") print(f" Index size: {char_count * 20:,} bytes") print(f" Bitmap size: {len(bitmap_data):,} bytes") print(f" Fallback glyphs: {fallback_count}") return (True, fallback_count, char_count)
SILVER_SCALE_TABLE = { 16: 23, 18: 25, 20: 27, 22: 29, 24: 32, 26: 35, 28: 39, 32: 44, 34: 47, 36: 49, 38: 53, 40: 58, 64: 90 } SILVER_SCALE_RATIO = 1.38
def silver_scaled_size(nominal): """Look up scaled render size for Silver font, matching device-side silverScaledSize().""" if nominal in SILVER_SCALE_TABLE: return SILVER_SCALE_TABLE[nominal] return int(nominal * SILVER_SCALE_RATIO + 0.5)
def is_silver_font(path): return 'silver' in os.path.basename(path).lower()
def is_bpmfzihi_font(path, family_name=None): """Detect Bpmf Zihi Kai Std font for font-specific glyph placement.""" base = os.path.basename(path).lower() if 'bpmf' in base and 'zihi' in base: return True if family_name: fam = family_name.lower() if 'bpmf' in fam and 'zihi' in fam: return True return False
if __name__ == '__main__': import argparse parser = argparse.ArgumentParser(description='Convert TTF font to M5ReadPaper binary format') parser.add_argument('ttf', nargs='?', help='Input TTF/TTC/OTF font file') parser.add_argument('-o', '--output', help='Output .bin file path (default: <font>_<size>pt.bin)') parser.add_argument('-s', '--size', type=int, default=30, help='Target font size in pt (default: 30). For Silver fonts, this is the nominal readingFontSize; the script auto-computes the actual render size using the built-in ratio.') parser.add_argument('-f', '--fallback', help='Fallback font for missing glyphs (auto-detects GenYoMinTW)') parser.add_argument('-r', '--render-size', type=int, help='Override render size (normally auto-calculated for Silver)') parser.add_argument('--gui', action='store_true', help='Launch graphical interface') args = parser.parse_args()
if args.gui or args.ttf is None: import threading import tkinter as tk from tkinter import ttk, filedialog, messagebox
FONTS_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', 'sd_card', 'fonts') if not getattr(sys, 'frozen', False) else os.path.expanduser('~/Desktop') GUI_SIZES = [36, 44, 52]
_STRINGS = { 'en': { 'title': 'Convert TTF → BIN', 'folders': 'Folders', 'source': 'Source:', 'target': 'Target:', 'fallback': 'Fallback:', 'browse': 'Browse…', 'options': 'Options', 'force_punct': 'Replace all punctuation from fallback font', 'fonts': 'Fonts', 'sizes': 'Sizes (pt)', 'select_all': 'Select All', 'deselect_all': 'Deselect All', 'add_font': 'Add Font…', 'convert': 'Convert', 'log': 'Log', 'lang_switch': '中文', 'browse_source': 'Select source fonts folder', 'browse_target': 'Select target folder', 'browse_fallback': 'Select fallback font', 'browse_add': 'Select a font file', 'warn_no_fonts': 'No fonts', 'warn_no_fonts_msg': 'Select at least one font.', 'warn_no_sizes': 'No sizes', 'warn_no_sizes_msg': 'Select at least one size.', 'done': 'Done: {success} succeeded, {failed} failed out of {total} jobs.', }, 'zh': { 'title': '轉換 TTF → BIN', 'folders': '資料夾', 'source': '來源:', 'target': '目標:', 'fallback': '備用字型:', 'browse': '瀏覽…', 'options': '選項', 'force_punct': '以備用字型取代所有標點', 'fonts': '字型', 'sizes': '字級 (pt)', 'select_all': '全選', 'deselect_all': '取消全選', 'add_font': '新增字型…', 'convert': '轉換', 'log': '紀錄', 'lang_switch': 'English', 'browse_source': '選擇來源字型資料夾', 'browse_target': '選擇目標資料夾', 'browse_fallback': '選擇備用字型', 'browse_add': '選擇字型檔案', 'warn_no_fonts': '未選字型', 'warn_no_fonts_msg': '請至少選擇一個字型。', 'warn_no_sizes': '未選字級', 'warn_no_sizes_msg': '請至少選擇一個字級。', 'done': '完成:{success} 成功、{failed} 失敗,共 {total} 個任務。', }, }
class _LogRedirector: """Redirects print() to the GUI log.""" def __init__(self, callback): self.callback = callback self._buf = "" def write(self, text): self._buf += text while "\n" in self._buf: line, self._buf = self._buf.split("\n", 1) if line: self.callback(line) def flush(self): if self._buf: self.callback(self._buf) self._buf = ""
class ConvertGUI: def __init__(self, root): self.root = root self.lang = 'en' root.resizable(False, False)
self.input_dir = tk.StringVar(value=FONTS_DIR) self.input_dir.trace_add('write', self._on_input_dir_changed) self.output_dir = tk.StringVar(value=FONTS_DIR) default_fallback = os.path.join(FONTS_DIR, 'GenYoMinTW-Regular.ttf') self.fallback_path = tk.StringVar(value=default_fallback if os.path.exists(default_fallback) else '') self.force_fallback_punct = tk.BooleanVar(value=False) self.font_vars = {} self.size_vars = {}
self._build_ui()
def _t(self, key): return _STRINGS[self.lang][key]
def _build_ui(self): for w in self.root.winfo_children(): w.destroy()
self.root.title(self._t('title'))
frame_lang = ttk.Frame(self.root, padding=2) frame_lang.pack(fill="x", padx=10, pady=(4, 0)) ttk.Button(frame_lang, text=self._t('lang_switch'), command=self._toggle_lang).pack(side="right")
frame_dirs = ttk.LabelFrame(self.root, text=self._t('folders'), padding=8) frame_dirs.pack(fill="x", padx=10, pady=(4, 4)) frame_dirs.columnconfigure(1, weight=1)
ttk.Label(frame_dirs, text=self._t('source')).grid(row=0, column=0, sticky="w", padx=(0, 4)) ttk.Entry(frame_dirs, textvariable=self.input_dir, width=50).grid(row=0, column=1, sticky="ew", padx=2) ttk.Button(frame_dirs, text=self._t('browse'), command=self._browse_input).grid(row=0, column=2, padx=(4, 0))
ttk.Label(frame_dirs, text=self._t('target')).grid(row=1, column=0, sticky="w", padx=(0, 4), pady=(4, 0)) ttk.Entry(frame_dirs, textvariable=self.output_dir, width=50).grid(row=1, column=1, sticky="ew", padx=2, pady=(4, 0)) ttk.Button(frame_dirs, text=self._t('browse'), command=self._browse_output).grid(row=1, column=2, padx=(4, 0), pady=(4, 0))
ttk.Label(frame_dirs, text=self._t('fallback')).grid(row=2, column=0, sticky="w", padx=(0, 4), pady=(4, 0)) ttk.Entry(frame_dirs, textvariable=self.fallback_path, width=50).grid(row=2, column=1, sticky="ew", padx=2, pady=(4, 0)) ttk.Button(frame_dirs, text=self._t('browse'), command=self._browse_fallback).grid(row=2, column=2, padx=(4, 0), pady=(4, 0))
frame_opts = ttk.LabelFrame(self.root, text=self._t('options'), padding=8) frame_opts.pack(fill="x", padx=10, pady=4) ttk.Checkbutton(frame_opts, text=self._t('force_punct'), variable=self.force_fallback_punct).pack(anchor="w")
self.frame_fonts = ttk.LabelFrame(self.root, text=self._t('fonts'), padding=4) self.frame_fonts.pack(fill="x", padx=10, pady=4) self._font_canvas = tk.Canvas(self.frame_fonts, height=200, highlightthickness=0) self._font_scrollbar = ttk.Scrollbar(self.frame_fonts, orient="vertical", command=self._font_canvas.yview) self._font_inner = ttk.Frame(self._font_canvas) self._font_inner.bind("<Configure>", lambda e: self._font_canvas.configure(scrollregion=self._font_canvas.bbox("all"))) self._font_canvas.create_window((0, 0), window=self._font_inner, anchor="nw") self._font_canvas.configure(yscrollcommand=self._font_scrollbar.set) self._font_canvas.pack(side="left", fill="both", expand=True) self._font_scrollbar.pack(side="right", fill="y") def _on_mousewheel(event): w = event.widget while w: if w is self._font_canvas or w is self._font_inner: self._font_canvas.yview_scroll(-1 * (event.delta // 120 or (-1 if event.delta < 0 else 1)), "units") break w = w.master self.root.bind_all("<MouseWheel>", _on_mousewheel) self._build_font_list(self._font_inner)
frame_sizes = ttk.LabelFrame(self.root, text=self._t('sizes'), padding=8) frame_sizes.pack(fill="x", padx=10, pady=4) old_selections = {s: v.get() for s, v in self.size_vars.items()} self.size_vars = {} for i, s in enumerate(GUI_SIZES): var = tk.BooleanVar(value=old_selections.get(s, True)) ttk.Checkbutton(frame_sizes, text=str(s), variable=var).grid(row=0, column=i, padx=8) self.size_vars[s] = var
frame_btns = ttk.Frame(self.root, padding=4) frame_btns.pack(fill="x", padx=10, pady=4) ttk.Button(frame_btns, text=self._t('select_all'), command=self._select_all).pack(side="left", padx=4) ttk.Button(frame_btns, text=self._t('deselect_all'), command=self._deselect_all).pack(side="left", padx=4) ttk.Button(frame_btns, text=self._t('add_font'), command=self._add_font).pack(side="left", padx=4) self.btn_convert = ttk.Button(frame_btns, text=self._t('convert'), command=self._start_convert) self.btn_convert.pack(side="right", padx=4)
self.progress = ttk.Progressbar(self.root, mode="determinate") self.progress.pack(fill="x", padx=10, pady=4)
frame_log = ttk.LabelFrame(self.root, text=self._t('log'), padding=4) frame_log.pack(fill="both", expand=True, padx=10, pady=(4, 10)) self.log_text = tk.Text(frame_log, height=14, width=80, state="disabled", font=("Menlo", 11) if sys.platform == "darwin" else ("Consolas", 10)) scrollbar = ttk.Scrollbar(frame_log, orient="vertical", command=self.log_text.yview) self.log_text.configure(yscrollcommand=scrollbar.set) self.log_text.pack(side="left", fill="both", expand=True) scrollbar.pack(side="right", fill="y")
def _toggle_lang(self): self.lang = 'zh' if self.lang == 'en' else 'en' self._build_ui()
def _on_input_dir_changed(self, *_args): self.output_dir.set(self.input_dir.get())
def _browse_input(self): d = filedialog.askdirectory(title=self._t('browse_source'), initialdir=self.input_dir.get()) if d: self.input_dir.set(d) self._build_font_list(self._font_inner)
def _browse_output(self): d = filedialog.askdirectory(title=self._t('browse_target'), initialdir=self.output_dir.get()) if d: self.output_dir.set(d)
def _browse_fallback(self): path = filedialog.askopenfilename( title=self._t('browse_fallback'), initialdir=os.path.dirname(self.fallback_path.get()) or FONTS_DIR, filetypes=[("Font files", "*.ttf *.ttc *.otf"), ("All files", "*.*")]) if path: self.fallback_path.set(path)
def _build_font_list(self, parent): for widget in parent.winfo_children(): widget.destroy() self.font_vars.clear() fonts_dir = self.input_dir.get() if not os.path.isdir(fonts_dir): return ttf_files = sorted( f for f in os.listdir(fonts_dir) if f.lower().endswith(('.ttf', '.ttc', '.otf')) ) cjk_fonts = [] for f in ttf_files: path = os.path.join(fonts_dir, f) try: tt = TTFont(path, fontNumber=0) has_cjk = any( 0x4E00 <= cp <= 0x9FFF for table in tt['cmap'].tables for cp in table.cmap.keys() ) if not has_cjk: tt.close() continue display = None display_en = None name_table = tt['name'] tc_lang_ids = {1028, 3076} cjk_lang_ids = {1028, 2052, 3076, 1041, 1042} for record in name_table.names: if record.nameID == 1: try: decoded = record.toUnicode() if decoded: if record.platformID == 3 and record.langID in tc_lang_ids: display = decoded break elif record.platformID == 3 and record.langID in cjk_lang_ids: if not display: display = decoded elif record.platformID == 1 and record.platEncID == 2: if not display: display = decoded elif display_en is None: display_en = decoded except Exception: pass tt.close() if not display: _NAME_OVERRIDES = {'Noto Sans TC': '思源黑體', 'Noto Serif TC': '思源宋體'} display = _NAME_OVERRIDES.get(display_en, display_en) if display_en else os.path.splitext(f)[0] cjk_fonts.append((f, display)) except Exception: pass for i, (fname, display) in enumerate(cjk_fonts): var = tk.BooleanVar(value=False) path = os.path.join(fonts_dir, fname) label = f"{display} ({fname})" ttk.Checkbutton(parent, text=label, variable=var).grid( row=i, column=0, sticky="w", padx=4, pady=1) self.font_vars[path] = var
def _select_all(self): for v in self.font_vars.values(): v.set(True)
def _deselect_all(self): for v in self.font_vars.values(): v.set(False)
def _add_font(self): path = filedialog.askopenfilename( title=self._t('browse_add'), filetypes=[("Font files", "*.ttf *.ttc *.otf"), ("All files", "*.*")]) if not path: return var = tk.BooleanVar(value=True) row = len(self.font_vars) self.font_vars[path] = var display = get_font_display_name(path) label = f"{display} ({os.path.basename(path)})" ttk.Checkbutton(self._font_inner, text=label, variable=var).grid( row=row, column=0, sticky="w", padx=4, pady=1)
def _log(self, msg): self.log_text.configure(state="normal") self.log_text.insert("end", msg + "\n") self.log_text.see("end") self.log_text.configure(state="disabled") self.root.update_idletasks()
def _log_from_thread(self, msg): self.root.after(0, self._log, msg)
def _update_progress(self, value): self.progress["value"] = value
def _start_convert(self): selected_fonts = [p for p, v in self.font_vars.items() if v.get()] selected_sizes = [s for s, v in self.size_vars.items() if v.get()] if not selected_fonts: messagebox.showwarning(self._t('warn_no_fonts'), self._t('warn_no_fonts_msg')) return if not selected_sizes: messagebox.showwarning(self._t('warn_no_sizes'), self._t('warn_no_sizes_msg')) return fallback = self.fallback_path.get().strip() or None punct_opt = self.force_fallback_punct.get() self.btn_convert.configure(state="disabled") self.log_text.configure(state="normal") self.log_text.delete("1.0", "end") self.log_text.configure(state="disabled") threading.Thread(target=self._run_convert, args=(selected_fonts, selected_sizes, fallback, punct_opt), daemon=True).start()
def _run_convert(self, fonts, sizes, fallback, force_punct): jobs = [(f, s) for f in fonts for s in sizes] total = len(jobs) self.progress["maximum"] = total self.progress["value"] = 0 success = failed = 0 old_stdout = sys.stdout sys.stdout = _LogRedirector(self._log_from_thread) for i, (font_path, size) in enumerate(jobs, 1): base = os.path.splitext(os.path.basename(font_path))[0] target_size = size render_size = silver_scaled_size(size) if is_silver_font(font_path) else None output_path = os.path.join(self.output_dir.get(), f"{base}_{target_size}pt.bin") self._log_from_thread(f"\n{'='*60}") self._log_from_thread(f"[{i}/{total}] {os.path.basename(font_path)} @ {size}pt → {os.path.basename(output_path)}") self._log_from_thread(f"{'='*60}") try: result = convert_ttf_to_bin(font_path, output_path, size, fallback, render_size, target_size, force_fallback_punct=force_punct) ok = result[0] if isinstance(result, tuple) else result if ok: success += 1 if isinstance(result, tuple) and len(result) >= 3: fb_count, total_glyphs = result[1], result[2] if total_glyphs > 0 and fb_count > total_glyphs * 0.5: self._log_from_thread(f"⚠ WARNING: {fb_count}/{total_glyphs} glyphs ({100*fb_count//total_glyphs}%) from fallback — font may not look correct!") else: failed += 1 self._log_from_thread(f"✗ Failed: {os.path.basename(font_path)} @ {size}pt") except Exception as e: failed += 1 self._log_from_thread(f"✗ Error: {e}") self.root.after(0, self._update_progress, i) sys.stdout = old_stdout self._log_from_thread('\n' + self._t('done').format(success=success, failed=failed, total=total)) self.root.after(0, lambda: self.btn_convert.configure(state="normal"))
root = tk.Tk() ConvertGUI(root) root.mainloop() else: ttf_path = args.ttf font_size = args.size fallback_path = args.fallback render_size = args.render_size
target_size = font_size if is_silver_font(ttf_path) and render_size is None: render_size = silver_scaled_size(font_size) print(f"Silver font detected: nominal {target_size}pt → render {render_size}pt (ratio {render_size/target_size:.2f})")
base = os.path.splitext(ttf_path)[0] output_path = args.output if args.output else f"{base}_{target_size}pt.bin" if not os.path.exists(ttf_path): print(f"Error: Font file not found: {ttf_path}") sys.exit(1) result = convert_ttf_to_bin(ttf_path, output_path, font_size, fallback_path, render_size, target_size) ok = result[0] if isinstance(result, tuple) else result sys.exit(0 if ok else 1)
|