Repository navigation
Expand file tree
/
Copy pathfont.py
More file actions
1267 lines (1134 loc) · 61.1 KB
/
Copy pathfont.py
File metadata and controls
1267 lines (1134 loc) · 61.1 KB
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
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""font.py — Episode II's bitmap fonts (``font0.us`` / ``font1.us``, Japan ``font0.tex`` / ``font1.tex``).
The FILE layer is Episode I's (this module started as a copy of
Xenosaga1PythonExtractor/font.py) and is right for Episode II: both regions'
files round-trip to the byte. The ADDRESSING (which cell a character is drawn
from, which metric pair gives its width) is NOT Episode I's; it was read from
the Episode II executables on 2026-11-10 and is described below. Episode I's
addressing is kept only inside the ``Episode1`` class, and the old module-level
names (``cell_of_code``, ``prop_index``, ``ASCII2EUC``, ...) raise ``FontError``
so they cannot be applied to an Episode II font by accident.
Which face draws what (read from the USA code, 2026-11-20; the Episode I kit's
"font0 is the dialogue face" does not hold here): the engine has ONE font
buffer (0x8DD280). ``FUN_00198ff0`` loads font1 at init, so font1 draws field
dialogue, menus and battle text; font0 is loaded in its place while a movie
plays (``FUN_001e7d08``) and between cfe.init (``FUN_0024c308``) and
cfe.destroy (``FUN_0024e738``), the real-time cutscenes. On the USA discs
cells 0-1023 (sheet 0) and all 384 metric pairs are byte-identical in the two
files (measured), so a new glyph is written into both.
File layout::
0x00000 0x20-byte header
0x00020 block 0 0x3C000 bytes -> glyph sheets 0 and 1
0x3C020 0x20-byte header (identical)
0x3C040 block 1 0x3C000 bytes -> glyph sheets 2 and 3
0x78040 metrics: (left, right) byte pairs, one per cell from cell 0
USA 384 pairs (file 492,352 bytes); Japan and the demo 256 (492,096)
Each block is a PS2 GS upload of a 320x192 PSMCT32 image which the engine
re-reads as a 640x768 PSMT4 texture: 4 bits per pixel, the low two bits
forming one 4-level sheet and the high two bits another. A sheet is a
32x32 grid of 20x24 cells, so one file carries 4 x 1,024 = 4,096 cells.
The GS swizzle walk is the one worked out by poketony for XS1KOR
(https://github.com/poketony/XS1KOR, ``xeno1font_ex.py``).
Episode II addressing (addresses are USA SLUS_208.92; Japan SLPS_253.68 in
brackets; the demo's EXP.ELF code is Japan's apart from addresses):
* **Code -> cell**: ``xs2_cell(lead, trail)``, a port of ``FUN_001979e0``
[0x197880]. Rows A1-A8 and AD go to cells below 608 by per-row rules, kanji
rows B0-CF to ``lead*94 + trail - 0x3D41`` (B0A1 = cell 1024), and lead
bytes D0 and up are looked up in a list of extra kanji (``FUN_00197988``
[0x197828]; cell 3989 + index). The list is in the executable, at the VA
the region profile gives (USA 0x63E870: empty; Japan 0x63A2A0: 87 codes).
Lead A9 is USA only (cell = trail + 0x100, used for Latin-1 below); the
Japanese function has no A9 case and returns the fallback cell 0x7F.
* **(a) USA single bytes** (print function ``FUN_00197bb8``, default case at
0x198660-0x1988dc; width function ``FUN_00195938``):
- 0x20-0x7F with ``$han1;`` in effect: ``FUN_00198b90`` reads a u16 EUC
code from a table in the executable (0x63E770 + 2 * byte; ``A`` = A3C1)
and the code goes through ``xs2_cell`` (``A`` = cell 138). Without
``$han``, the byte is drawn from an 8x8 system font, not from a cell.
- 0x80-0xFF: ``FUN_00194dd0`` accepts only a few two-byte codes (lead A4,
A6 or AD with any trail; A1 with BD DA DB DF F9 FA FB; A2 with A2 A3 A4
F6). Any other high byte is ONE character: lead A9, trail = byte - 0x80,
so cell = byte + 0x80 (0xC9 ``É`` = cell 329). The USA build cannot print
kanji through this function.
* **(b) Metrics**: the pair index is the cell number in both regions
(``FUN_00198b38`` [0x1989a8] reads ``font + 0x78040 + 2 * cell``). With
``$prop1;`` in effect a cell below the limit is drawn proportionally: the
glyph image comes from sheet 0, cell + 576 (18 rows down: 0x1b00 at
0x198894 [0x198704]), ``left - 1`` to ``right + 1``.
=============== ===================== ======================
. USA Japan (and the demo)
=============== ===================== ======================
pairs in file 384 256
proportional if cell < 384 (0x198840) cell < 192 (0x1986b0)
advance right - left + 3 right - left + 4
=============== ===================== ======================
So the Japanese file's pairs 192-255 are never read by this code (they all
hold (8, 11) and cells 768-831 hold a matching placeholder mark). A pair
whose left is 0 means "no width": the advance is then the text's ``$xx1``
value + 1. Everything else is fixed width: cell as is, advance 19.
Checked against the files: the ink of cell + 576 equals the pair exactly
for 379 of 384 USA pairs and 255 of 256 Japanese pairs (the rest are blank).
* **Which cells can be drawn**: the Japanese draw function selects block 0 or
block 1 by bit 1 of cell >> 10 (0x197204). The USA one (``FUN_00197200``,
0x197380-0x1973fc) builds its texture register from bit 0 only and one
texture address, and the USA font setup (``FUN_00198bc0``) uploads one
block; from the code, USA cells 2048-4095 (sheets 2 and 3) are drawn from
the same place as cells 0-2047. Not seen in the running game.
* Not modelled: the half-size mode (control byte 0x18, ``FUN_00198928``),
ruby, and the ``$``-codes; this module does not measure whole strings.
Round trip: ``Font.from_bytes(b).to_bytes() == b`` for font0 and font1 of the
USA discs, the Japanese discs and the demo.
Usage:
python font.py extract font0.us --out DIR # 4 PNG sheets + metrics.csv
python font.py rebuild --ref font0.us --sheets DIR --out new.us
python font.py info font0.us
python font.py table --iso DISC.iso # byte -> cell -> metric pair
python font.py --self-test
"""
from __future__ import annotations
import argparse
import csv
import os
import struct
import sys
import zlib
from dataclasses import dataclass
from pathlib import Path
from typing import List, NamedTuple, Optional, Sequence, Tuple
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
HEADER = 0x20
BLOCK = 0x3C000
SHEET_W, SHEET_H = 640, 768
CELL_W, CELL_H = 20, 24
CELLS_PER_ROW = SHEET_W // CELL_W # 32
CELLS_PER_SHEET = CELLS_PER_ROW * (SHEET_H // CELL_H) # 1024
METRICS_OFF = 2 * (HEADER + BLOCK) # 0x78040
FULLWIDTH = (0, 19) # metrics pair meaning "not proportional"
class FontError(ValueError):
pass
# --- GS swizzle tables (PS2 GS PSMCT32 / PSMT4 page/block/column layout) ---
_BLOCK32 = [0, 1, 4, 5, 16, 17, 20, 21, 2, 3, 6, 7, 18, 19, 22, 23,
8, 9, 12, 13, 24, 25, 28, 29, 10, 11, 14, 15, 26, 27, 30, 31]
_COLWORD32 = [0, 1, 4, 5, 8, 9, 12, 13, 2, 3, 6, 7, 10, 11, 14, 15]
_BLOCK4 = [0, 2, 8, 10, 1, 3, 9, 11, 4, 6, 12, 14, 5, 7, 13, 15,
16, 18, 24, 26, 17, 19, 25, 27, 20, 22, 28, 30, 21, 23, 29, 31]
_COLWORD4 = [
[0, 1, 4, 5, 8, 9, 12, 13, 0, 1, 4, 5, 8, 9, 12, 13,
0, 1, 4, 5, 8, 9, 12, 13, 0, 1, 4, 5, 8, 9, 12, 13,
2, 3, 6, 7, 10, 11, 14, 15, 2, 3, 6, 7, 10, 11, 14, 15,
2, 3, 6, 7, 10, 11, 14, 15, 2, 3, 6, 7, 10, 11, 14, 15,
8, 9, 12, 13, 0, 1, 4, 5, 8, 9, 12, 13, 0, 1, 4, 5,
8, 9, 12, 13, 0, 1, 4, 5, 8, 9, 12, 13, 0, 1, 4, 5,
10, 11, 14, 15, 2, 3, 6, 7, 10, 11, 14, 15, 2, 3, 6, 7,
10, 11, 14, 15, 2, 3, 6, 7, 10, 11, 14, 15, 2, 3, 6, 7],
[8, 9, 12, 13, 0, 1, 4, 5, 8, 9, 12, 13, 0, 1, 4, 5,
8, 9, 12, 13, 0, 1, 4, 5, 8, 9, 12, 13, 0, 1, 4, 5,
10, 11, 14, 15, 2, 3, 6, 7, 10, 11, 14, 15, 2, 3, 6, 7,
10, 11, 14, 15, 2, 3, 6, 7, 10, 11, 14, 15, 2, 3, 6, 7,
0, 1, 4, 5, 8, 9, 12, 13, 0, 1, 4, 5, 8, 9, 12, 13,
0, 1, 4, 5, 8, 9, 12, 13, 0, 1, 4, 5, 8, 9, 12, 13,
2, 3, 6, 7, 10, 11, 14, 15, 2, 3, 6, 7, 10, 11, 14, 15,
2, 3, 6, 7, 10, 11, 14, 15, 2, 3, 6, 7, 10, 11, 14, 15],
]
_COLBYTE4 = ([0] * 8 + [2] * 8 + [4] * 8 + [6] * 8) * 2 + \
([1] * 8 + [3] * 8 + [5] * 8 + [7] * 8) * 2
def _ct32_word(x: int, y: int, dbw: int = 0x140) -> int:
page = ((y >> 5) * (dbw >> 6)) + (x >> 6)
px, py = x & 63, y & 31
block = _BLOCK32[(px >> 3) + (py >> 3) * 8]
bx, by = px & 7, py & 7
return page * 2048 + block * 64 + (by >> 1) * 16 + _COLWORD32[bx + (by & 1) * 8]
def _t4_word(x: int, y: int, dbp: int, dbw: int = 64) -> Tuple[int, int]:
dbw2 = dbw >> 1
page = ((y >> 7) * ((dbw2 + 127) >> 7)) + (x >> 7)
px, py = x & 127, y & 127
block = _BLOCK4[(px >> 5) + (py >> 4) * 4]
bx, by = px & 31, py & 15
col = by >> 2
cx, cy = bx, by & 3
word = dbp * 64 + page * 2048 + block * 64 + col * 16 + _COLWORD4[col & 1][cx + cy * 32]
return word, _COLBYTE4[cx + cy * 32]
_MAP: Optional[Tuple[list, list]] = None
def _layout() -> Tuple[list, list]:
"""Per sheet pixel (row-major 640x768): (byte offset in block, nibble shift).
Same for both blocks. Built once (~1 s, pure Python) and cached.
"""
global _MAP
if _MAP is not None:
return _MAP
# GS word address -> block byte offset of that word, for the CT32 upload
gs_to_byte = {}
for y in range(192):
for x in range(320):
gs_to_byte[_ct32_word(x, y)] = (y * 320 + x) * 4
offs = [0] * (SHEET_W * SHEET_H)
shifts = [0] * (SHEET_W * SHEET_H)
i = 0
for tile in range(SHEET_H // 128):
dbp = (tile * SHEET_W) // 4
for row in range(128):
for col in range(SHEET_W):
word, cb = _t4_word(col, row, dbp)
offs[i] = gs_to_byte[word] + (cb >> 1)
shifts[i] = 4 if (cb & 1) else 0
i += 1
_MAP = (offs, shifts)
return _MAP
def decode_block(block: bytes) -> Tuple[bytearray, bytearray]:
"""One 0x3C000 block -> two 640x768 sheets of values 0..3 (row-major)."""
offs, shifts = _layout()
lo = bytearray(SHEET_W * SHEET_H)
hi = bytearray(SHEET_W * SHEET_H)
for i in range(SHEET_W * SHEET_H):
nib = (block[offs[i]] >> shifts[i]) & 0xF
lo[i] = nib & 3
hi[i] = nib >> 2
return lo, hi
def encode_block(lo: Sequence[int], hi: Sequence[int]) -> bytes:
offs, shifts = _layout()
out = bytearray(BLOCK)
for i in range(SHEET_W * SHEET_H):
nib = (lo[i] & 3) | ((hi[i] & 3) << 2)
out[offs[i]] |= nib << shifts[i]
return bytes(out)
# --- Episode II addressing ----------------------------------------------------
FALLBACK_CELL = 0x7F
EXTRA_KANJI_BASE = 3989 # 0xF95, FUN_00197988
PROP_CELL_SHIFT = 576 # a proportional glyph is drawn from sheet 0, cell + 18 rows
FIXED_ADVANCE = 19
def xs2_cell(lead: int, trail: int, extra: Sequence[Tuple[int, int]] = (), a9: bool = True) -> int:
"""The font cell of a two-byte code: USA ``FUN_001979e0`` (Japan 0x197880).
``extra`` is the executable's extra-kanji list as (lead, trail) pairs;
``a9`` is False for the Japanese and demo builds, whose function has no
case for lead 0xA9."""
lead &= 0xFF
trail &= 0xFF
if lead < 0xB0:
if lead == 0xA1:
return (trail - 0xA1) & 0xFFFF
if lead == 0xA2:
base = {0xA0: 0x5D, 0xE0: 0x6C, 0xF0: 0x75}.get(trail & 0xF0)
return FALLBACK_CELL if base is None else (trail & 0x0F) + base
if lead == 0xA3:
h = trail & 0xE0
if h == 0xA0:
return (trail & 0x0F) + 0x80
if h == 0xC0:
return (trail & 0x1F) + 0x89
if h == 0xE0:
return (trail & 0x1F) + 0xA3
return FALLBACK_CELL
if lead == 0xA4:
return trail + 0x1F
if lead == 0xA5:
return trail + 0x7F
if lead == 0xA6:
return (trail & 0x1F) + (0x17F if trail < 0xC0 else 0x197)
if lead == 0xA7:
return trail + (0x100 if trail > 0xCF else 0x10F)
if lead == 0xA8:
return trail + 0x15F
if lead == 0xA9 and a9:
return trail + 0x100
if lead == 0xAD:
return trail + 0x17F
return FALLBACK_CELL
if lead < 0xD0:
return (lead * 94 + trail - 0x3D41) & 0xFFFF
for i, pair in enumerate(extra):
if tuple(pair) == (lead, trail):
return EXTRA_KANJI_BASE + i
return FALLBACK_CELL
def us_two_byte(lead: int, trail: int) -> bool:
"""USA ``FUN_00194dd0``: the only two-byte codes the USA print and width
functions read as a pair. Any other byte from 0x80 up is one character."""
if trail == 0:
return False
if lead in (0xA4, 0xA6, 0xAD):
return True
if lead == 0xA1:
return trail in (0xBD, 0xDA, 0xDB, 0xDF, 0xF9, 0xFA, 0xFB)
if lead == 0xA2:
return trail in (0xA2, 0xA3, 0xA4, 0xF6)
return False
class Glyph(NamedTuple):
length: int # bytes of text consumed
kind: str # "cell" or "8x8" (the system font: no cell of this file)
code: int # the EUC code looked up (0 for "8x8")
cell: Optional[int]
# what differs between the builds, with the address that shows it (USA / Japan)
_BUILDS = {
# lone high bytes proportional below advance = r-l+pad blocks the draw call reaches
"us": dict(latin1=True, prop_cells=384, prop_pad=3, drawable=2048),
"jp": dict(latin1=False, prop_cells=192, prop_pad=4, drawable=4096),
"demo": dict(latin1=False, prop_cells=192, prop_pad=4, drawable=4096),
}
@dataclass(frozen=True)
class Addressing:
"""Byte -> cell -> metric pair for one build. Make one with ``from_iso`` or
``from_executable``: the ASCII table and the extra-kanji list are read from
the game's executable, at the virtual addresses of the region profile."""
region: str
ascii: Tuple[int, ...] # EUC codes of bytes 0x20..0x7F
extra: Tuple[Tuple[int, int], ...] # the extra-kanji list
latin1: bool
prop_cells: int
prop_pad: int
drawable: int
sys_widths: Tuple[int, ...] = () # USA: width of bytes 0x20..0x7F without $han
@classmethod
def from_executable(cls, elf: bytes, profile) -> "Addressing":
import region
if profile.name not in _BUILDS:
raise FontError(f"no Episode II font addressing is known for region {profile.name!r}")
if profile.ascii_table_va is None or profile.kanji_list_va is None:
raise FontError(f"the {profile.name} profile does not give the font table addresses")
try:
raw = region.read_va(elf, profile.ascii_table_va + 2 * 0x20, 2 * 96)
ascii_codes = struct.unpack("<96H", raw)
extra, va = [], profile.kanji_list_va
while len(extra) <= CELLS_PER_SHEET * 4 - EXTRA_KANJI_BASE:
a, b = region.read_va(elf, va, 2)
if a == 0:
break
extra.append((a, b))
va += 2
else:
raise FontError("the extra-kanji list does not end: wrong executable for this profile?")
except (region.ElfError, struct.error) as e:
raise FontError(f"cannot read the font tables from the executable: {e}")
if ascii_codes[0] != 0xA1A1 or ascii_codes[ord("A") - 0x20] != 0xA3C1 \
or any(not (0xA1 <= c >> 8 <= 0xFE and 0xA1 <= c & 0xFF <= 0xFE) for c in ascii_codes):
raise FontError("the ASCII table is not at the profile's address: wrong executable for this profile?")
if any(a < 0xD0 for a, _ in extra):
raise FontError("the extra-kanji list holds a lead byte below 0xD0: wrong executable?")
sysw: Tuple[int, ...] = ()
if getattr(profile, "sys_width_va", None) is not None:
try:
sysw = struct.unpack("<96i", region.read_va(elf, profile.sys_width_va + 4 * 0x20, 4 * 96))
except region.ElfError as e:
raise FontError(f"cannot read the width table from the executable: {e}")
if any(not 0 < w <= 32 for w in sysw):
raise FontError("the width table is not at the profile's address: wrong executable?")
return cls(profile.name, tuple(ascii_codes), tuple(extra), sys_widths=sysw, **_BUILDS[profile.name])
@classmethod
def from_iso(cls, iso) -> "Addressing":
import region
try:
prof = region.require_executable(iso, "reading the font tables")
_, elf = region.executable(iso)
except (region.UnknownBuild, region.ElfError) as e:
raise FontError(str(e))
return cls.from_executable(elf, prof)
# -- code -> cell
def cell(self, lead: int, trail: int) -> int:
return xs2_cell(lead, trail, self.extra, a9=self.latin1)
def two_byte(self, lead: int, trail: int) -> bool:
"""Does the print function read (lead, trail) as one two-byte code?
USA: ``FUN_00194dd0``. Japan: any byte from 0xA0 up leads a pair."""
if not self.latin1:
return lead >= 0xA0
return us_two_byte(lead, trail)
def glyph(self, text: bytes, i: int = 0, han: bool = True) -> Glyph:
"""The character at ``text[i]`` as the print function reads it. ``han``
is the ``$han1;`` state. Control bytes (< 0x20) and ``$``-codes are the
caller's business: a control byte raises FontError."""
b = text[i]
nxt = text[i + 1] if i + 1 < len(text) else 0
if b < 0x20:
raise FontError(f"control byte {b:#04x}: not a character")
if self.latin1: # FUN_00197bb8, 0x198660-0x1986fc
if b < 0x80:
if not han:
return Glyph(1, "8x8", 0, None)
code = self.ascii[b - 0x20]
return Glyph(1, "cell", code, self.cell(code >> 8, code & 0xFF))
if self.two_byte(b, nxt):
return Glyph(2, "cell", b << 8 | nxt, self.cell(b, nxt))
trail = (b + 0x80) & 0xFF # a lone high byte: Latin-1
return Glyph(1, "cell", 0xA900 | trail, self.cell(0xA9, trail))
if b >= 0xA0: # Japan 0x198508-0x198570
return Glyph(2, "cell", b << 8 | nxt, self.cell(b, nxt))
if not han:
return Glyph(1, "8x8", 0, None)
if b >= 0x80: # a signed char below 0x20 at 0x198a68
return Glyph(1, "cell", 0xA1A1, self.cell(0xA1, 0xA1))
n = 1
if b == 0x23: # '#': Japan 0x198a84-0x198b0c
if nxt == 0x23:
n, code = 2, self.ascii[0x23 - 0x20]
else:
try:
v = int(text[i + 1:i + 3].decode("ascii"), 16) if len(text) >= i + 3 else None
except ValueError:
v = None
if v is None:
raise FontError("'#' in Japanese $han text must be '##' or '#' and two hex digits")
v += 0x20
n, code = 3, 0xD0A1 + ((v // 94) << 8) + v % 94
else:
code = self.ascii[b - 0x20]
return Glyph(n, "cell", code, self.cell(code >> 8, code & 0xFF))
# -- cell -> what is drawn
def metric_slot(self, cell: int, prop: bool = True) -> Optional[int]:
"""The metric pair read for ``cell`` (the cell number itself), or None
when it is drawn fixed width (``$prop0;``, or a cell at or past the limit)."""
return cell if prop and 0 <= cell < self.prop_cells else None
def drawn_cell(self, cell: int, prop: bool = True) -> int:
"""The cell whose image is drawn for ``cell``."""
return cell + PROP_CELL_SHIFT if self.metric_slot(cell, prop) is not None else cell
def advance(self, cell: int, metrics: Sequence[Tuple[int, int]], prop: bool = True) -> Optional[int]:
"""Pixels the pen moves at zoom 16. None when the pair's left is 0: the
width is then the text's own ``$xx1`` value + 1, which this cannot know."""
slot = self.metric_slot(cell, prop)
if slot is None:
return FIXED_ADVANCE
if slot >= len(metrics):
raise FontError(f"cell {cell}: the font has {len(metrics)} metric pairs, "
f"this build reads pairs below {self.prop_cells}")
left, right = metrics[slot]
return None if left == 0 else right - left + self.prop_pad
def measurer(self, metrics: Sequence[Tuple[int, int]], han: bool = False, prop: bool = False,
xx1: int = 4) -> "Measurer":
return Measurer(self, metrics, han, prop, xx1)
def reachable(self, cell: int) -> bool:
"""False for a cell the build's draw call cannot address (USA: sheets 2 and 3)."""
return 0 <= cell < self.drawable
def table(self, metrics: Sequence[Tuple[int, int]] = (), han: bool = True, prop: bool = True) -> List[dict]:
"""One row per single byte 0x20..0xFF: what it is read as, its cell, the
cell drawn, the metric slot, the pair and the advance. A byte that only
starts a two-byte code (Japan 0xA0 up) has kind "lead"."""
rows = []
for b in range(0x20, 0x100):
row = {"byte": f"{b:02X}", "kind": "", "code": "", "cell": "", "drawn_cell": "",
"metric_slot": "", "left": "", "right": "", "advance": ""}
if not self.latin1 and b >= 0xA0:
row["kind"] = "lead"
elif not self.latin1 and han and b == 0x23:
row["kind"] = "escape" # '##' or '#XX'
else:
g = self.glyph(bytes((b, 0)), 0, han)
row["kind"] = g.kind
if g.cell is not None:
slot = self.metric_slot(g.cell, prop)
row.update(code=f"{g.code:04X}", cell=g.cell, drawn_cell=self.drawn_cell(g.cell, prop),
metric_slot="" if slot is None else slot)
if slot is not None and slot < len(metrics):
adv = self.advance(g.cell, metrics, prop)
row.update(left=metrics[slot][0], right=metrics[slot][1],
advance="$xx1+1" if adv is None else adv)
elif slot is None:
row["advance"] = FIXED_ADVANCE
rows.append(row)
return rows
class Measurer:
"""The pixel width of a line, as the game's width function computes it (USA
``FUN_00195938``, which the subtitle packer calls for every line through
``FUN_00195e78``). The ``$prop``, ``$han``, ``$xx1`` and ``$xx3`` states live in
globals in the game, so they carry from one call to the next; here they
carry inside one Measurer.
Read from the USA function: a ``$...;`` code has no width (``$prop``, ``$han``,
``$xx1,N``, ``$xx3,N`` set the state; ``$rubys;`` skips to ``$rubye;``); an ASCII
byte without ``$han`` takes its width from a table in the executable (USA) or
16 (Japan, 0x195bc4); every other character is 19 wide unless ``$prop`` is on
and its cell is below the build's limit, when it is ``right - left + pad``, or
``$xx1 + 1`` for a pair whose left is 0, or 10 when ``$xx3`` is non-zero.
Control bytes below 0x20 are refused (not ported). The Japanese function was
read only where it measures a character (0x195ba0-0x195ca0); its ``$`` handling
is assumed to be the USA one."""
def __init__(self, addressing: "Addressing", metrics, han: bool, prop: bool, xx1: int):
self.a, self.metrics = addressing, metrics
self.han, self.prop, self.xx1, self.xx3 = han, prop, xx1, 0
@staticmethod
def _number(s: bytes, i: int) -> int:
j = i
while j < len(s) and s[j] in b"+-0123456789":
j += 1
try:
return int(s[i:j] or b"0")
except ValueError:
return 0
def width(self, s: bytes) -> int:
w, i, n = 0, 0, len(s)
while i < n and s[i] != 0:
c = s[i]
if c == 0x24: # '$'
t = s[i + 1:i + 8]
if t.startswith(b"prop") and i + 5 < n:
self.prop = bool(s[i + 5] & 1)
elif t.startswith(b"han") and i + 4 < n:
self.han = bool(s[i + 4] & 1)
elif t.startswith(b"xx1,"):
self.xx1 = self._number(s, i + 5) & 0xFF
elif t.startswith(b"xx3,"):
self.xx3 = self._number(s, i + 5) & 0xFF
elif t.startswith(b"rubys;"):
e = s.find(b"$rubye;", i)
if e < 0:
raise FontError("$rubys; without $rubye;")
i = e
e = s.find(b";", i)
if e < 0:
raise FontError("a '$' code without its ';' (the game would read on past the text)")
i = e + 1
continue
if c < 0x20:
raise FontError(f"control byte {c:#04x} in a measured line")
if c < 0x80 and not self.han:
if self.a.latin1:
if not self.a.sys_widths:
raise FontError("the width table for ASCII without $han was not read")
w += self.a.sys_widths[c - 0x20]
else:
w += 16
i += 1
continue
g = self.a.glyph(s, i, han=True)
i += g.length
if not self.prop or g.cell is None or not 0 <= g.cell < self.a.prop_cells:
w += FIXED_ADVANCE
elif self.xx3:
w += 10
else:
if g.cell >= len(self.metrics):
raise FontError(f"cell {g.cell} has no metric pair in this font")
left, right = self.metrics[g.cell]
w += self.xx1 + 1 if left == 0 else right - left + self.a.prop_pad
return w
# --- Episode I addressing: fenced --------------------------------------------
#
# What follows is Xenosaga I's (SLUS_204.69) addressing, kept for reference and
# for comparing the two games. It is WRONG for both Episode II fonts: its kanji
# formula puts B0A1 at cell 752 (Episode II: 1024) and it reserves cells
# 3776-4031 for proportional glyphs, which hold kanji in Episode II. Use
# ``Addressing`` above. The names live only on this class; the old module-level
# names raise FontError (``__getattr__`` below).
class Episode1:
PROP_CELL_BASE = 3744 # cell = PROP_CELL_BASE + n, n = 32..287
PROP_SLOTS = 256
# xglFontAscii2Euc's table for 0x20..0x7F (0x7F -> space). Punctuation was
# moved into the USA-only rows 0xD1/0xD2; '\\' lands on the yen glyph as in
# every JIS font.
ASCII2EUC = [
0xA1A1, 0xD1B5, 0xD1C2, 0xD1E5, 0xD2A9, 0xD1E6, 0xD2A8, 0xD1C0,
0xD1CB, 0xD1CC, 0xD1B4, 0xD1FE, 0xD1BA, 0xD1FD, 0xD1B9, 0xD1D4,
0xA3B0, 0xA3B1, 0xA3B2, 0xA3B3, 0xA3B4, 0xA3B5, 0xA3B6, 0xA3B7,
0xA3B8, 0xA3B9, 0xD1BC, 0xD1BB, 0xD1C7, 0xD1FC, 0xD1C8, 0xD1B7,
0xD1AD, 0xA3C1, 0xA3C2, 0xA3C3, 0xA3C4, 0xA3C5, 0xA3C6, 0xA3C7,
0xA3C8, 0xA3C9, 0xA3CA, 0xA3CB, 0xA3CC, 0xA3CD, 0xA3CE, 0xA3CF,
0xA3D0, 0xA3D1, 0xA3D2, 0xA3D3, 0xA3D4, 0xA3D5, 0xA3D6, 0xA3D7,
0xA3D8, 0xA3D9, 0xA3DA, 0xD1CD, 0xD1A5, 0xD1CE, 0xD1D8, 0xD1D3,
0xD1DA, 0xA3E1, 0xA3E2, 0xA3E3, 0xA3E4, 0xA3E5, 0xA3E6, 0xA3E7,
0xA3E8, 0xA3E9, 0xA3EA, 0xA3EB, 0xA3EC, 0xA3ED, 0xA3EE, 0xA3EF,
0xA3F0, 0xA3F1, 0xA3F2, 0xA3F3, 0xA3F4, 0xA3F5, 0xA3F6, 0xA3F7,
0xA3F8, 0xA3F9, 0xA3FA, 0xD1CF, 0xD1D1, 0xD1D0, 0xD1E8, 0xA1A1,
]
_ROW3_BASE = (-16, 10, 37, 0) # xglFontCheckProportional's table for row 3
# Operand byte count per control byte 0x00..0x1F (xglFontGetSPcodeSize).
# None = variable: 0x08 takes a flag byte whose set bits add 2,2,3,2,2 bytes
# (bits 0-4); 0x15 takes a length byte L and L more bytes.
CONTROL_OPERANDS = [0, 1, 1, 1, 1, 1, 3, 0, None, 0, 0, 0, 3, 1, 5, 3,
5, 6, 2, 2, 0, None, 0, 7, 1, 1, 0, 1, 0, 0, 0, 0]
_CTRL08_BITS = (2, 2, 3, 2, 2, 0, 0, 0)
@staticmethod
def control_length(data: bytes, i: int) -> int:
"""Total byte length of the control sequence starting at data[i] (< 0x20)."""
c = data[i]
n = Episode1.CONTROL_OPERANDS[c]
if n is not None:
return 1 + n
if c == 0x08:
flags = data[i + 1] if i + 1 < len(data) else 0
return 2 + sum(Episode1._CTRL08_BITS[b] for b in range(8) if flags >> b & 1)
return 2 + (data[i + 1] if i + 1 < len(data) else 0) # 0x15
@staticmethod
def ascii_to_code(ch: int) -> int:
"""ASCII byte (0x20..0x7F) -> EUC code as the engine maps it."""
return Episode1.ASCII2EUC[ch - 0x20]
@staticmethod
def cell_of_code(code: int) -> int:
"""Kanji-path cell for an EUC code (xglFontGetKanjiClutUV)."""
hi, lo = code >> 8, code & 0xFF
v = (lo + 94 * hi - 0xA1) & 0xFFFF
v += 0xC4E2 if hi < 0xAD else (0xC30C if hi < 0xB0 else 0xC250)
return v & 0xFFFF
@staticmethod
def prop_index(code: int) -> int:
"""Proportional slot for an EUC code, or -1 (xglFontCheckProportional)."""
hi, lo = code >> 8, code & 0xFF
if hi == 0xA7:
return {0xA1: 0x40, 0xA3: 0x41, 0xA6: 0x42}.get(lo, -1)
if hi == 0xA1:
return {0xA1: 37, 0xA5: 10}.get(lo, -1)
if hi == 0xA3:
return Episode1._ROW3_BASE[(lo - 0xA0) >> 5] + (lo & 0x1F)
if hi < 0xD0:
return -1
v = (94 * hi + lo - 0x4D21) & 0xFFFF
r = (~(v >> 5)) & 7
if r < 2:
r ^= 1
return r * 32 + (v & 0x1F)
@staticmethod
def prop_offset(slot: int) -> int:
"""Linear offset n (32..287) from code 0xD0A1 of the code owning ``slot``."""
r, k = slot >> 5, slot & 0x1F
r0 = r ^ 1 if r < 2 else r
return (7 - r0) * 32 + k + 32
@staticmethod
def code_of_prop(slot: int) -> int:
"""The USA-row EUC code that selects proportional ``slot`` (inverse of
prop_index for the 0xD0.. rows)."""
n = Episode1.prop_offset(slot)
return ((0xD0 + n // 94) << 8) | (0xA1 + n % 94)
@staticmethod
def cell_of_prop(slot: int) -> int:
"""Cell drawn for a proportional slot (the 256-cell block, 3776..4031)."""
return Episode1.PROP_CELL_BASE + Episode1.prop_offset(slot)
@staticmethod
def glyph_cell(code: int) -> int:
"""Cell the dialogue printer draws for ``code`` in proportional mode."""
p = Episode1.prop_index(code)
return Episode1.cell_of_prop(p) if p >= 0 else Episode1.cell_of_code(code)
@staticmethod
def advance(pair: Tuple[int, int]) -> int:
"""Pixels the printer advances for a proportional slot's (left, right)."""
left, right = pair
if left == 0:
return 20 # engine substitutes its default width; retail uses this
return right - left + 3 # xglFontGetProportionalSize, mode 0
_EPISODE1_NAMES = ("ASCII2EUC", "CONTROL_OPERANDS", "PROP_CELL_BASE", "PROP_SLOTS", "control_length",
"ascii_to_code", "cell_of_code", "prop_index", "prop_offset", "code_of_prop",
"cell_of_prop", "glyph_cell", "advance")
def __getattr__(name): # module-level: font.cell_of_code and friends
if name in _EPISODE1_NAMES:
raise FontError(f"font.{name} is Xenosaga I's addressing and is wrong for Episode II fonts; "
f"use font.Addressing (or font.Episode1.{name} if you mean Episode I)")
raise AttributeError(f"module 'font' has no attribute {name!r}")
# --- the file --------------------------------------------------------------
class Font:
def __init__(self, headers: List[bytes], sheets: List[bytearray],
metrics: List[Tuple[int, int]], tail: bytes = b""):
self.headers = headers # two 0x20-byte headers
self.sheets = sheets # four 640*768 bytearrays, values 0..3
self.metrics = metrics # (left, right) per slot
self.tail = tail # bytes after the metrics, if any
@classmethod
def from_bytes(cls, data: bytes) -> "Font":
if len(data) < METRICS_OFF:
raise FontError(f"font file too short ({len(data)} bytes)")
headers, sheets = [], []
for m in range(2):
base = m * (HEADER + BLOCK)
headers.append(bytes(data[base:base + HEADER]))
lo, hi = decode_block(data[base + HEADER:base + HEADER + BLOCK])
sheets += [lo, hi]
rest = data[METRICS_OFF:]
npairs = len(rest) // 2
metrics = [(rest[2 * i], rest[2 * i + 1]) for i in range(npairs)]
return cls(headers, sheets, metrics, bytes(rest[npairs * 2:]))
@classmethod
def load(cls, path) -> "Font":
return cls.from_bytes(Path(path).read_bytes())
def to_bytes(self) -> bytes:
out = bytearray()
for m in range(2):
out += self.headers[m]
out += encode_block(self.sheets[2 * m], self.sheets[2 * m + 1])
for left, right in self.metrics:
out += bytes((left & 0xFF, right & 0xFF))
out += self.tail
return bytes(out)
# cells
@property
def cell_count(self) -> int:
return CELLS_PER_SHEET * len(self.sheets)
def cell(self, index: int) -> List[List[int]]:
"""24 rows of 20 values (0..3) for cell ``index``."""
sheet = self.sheets[index // CELLS_PER_SHEET]
i = index % CELLS_PER_SHEET
x0, y0 = (i % CELLS_PER_ROW) * CELL_W, (i // CELLS_PER_ROW) * CELL_H
return [list(sheet[(y0 + r) * SHEET_W + x0:(y0 + r) * SHEET_W + x0 + CELL_W])
for r in range(CELL_H)]
def set_cell(self, index: int, rows: Sequence[Sequence[int]]) -> None:
if len(rows) != CELL_H or any(len(r) != CELL_W for r in rows):
raise FontError("cell must be 24 rows of 20 values")
sheet = self.sheets[index // CELLS_PER_SHEET]
i = index % CELLS_PER_SHEET
x0, y0 = (i % CELLS_PER_ROW) * CELL_W, (i // CELLS_PER_ROW) * CELL_H
for r in range(CELL_H):
sheet[(y0 + r) * SHEET_W + x0:(y0 + r) * SHEET_W + x0 + CELL_W] = \
bytes(v & 3 for v in rows[r])
def ink_bounds(self, index: int) -> Optional[Tuple[int, int]]:
"""(leftmost, rightmost) inked columns of a cell, or None if blank."""
cols = [c for c in range(CELL_W)
if any(row[c] for row in self.cell(index))]
return (cols[0], cols[-1]) if cols else None
def set_metrics(self, slot: int, left: int, right: int) -> None:
"""Change one pair. The table cannot grow: the game reads the whole file into
a fixed buffer (USA 0x8DD280, set in FUN_00198ff0) and indexes pairs by cell."""
if not 0 <= slot < len(self.metrics):
raise FontError(f"metric slot {slot}: this font has {len(self.metrics)} pairs and cannot grow")
if not (0 <= left <= 255 and 0 <= right <= 255):
raise FontError(f"metric slot {slot}: left and right are bytes")
self.metrics[slot] = (left, right)
# --- PNG (8-bit grayscale, stdlib only) -------------------------------------
def _png_chunk(tag: bytes, payload: bytes) -> bytes:
return (struct.pack(">I", len(payload)) + tag + payload
+ struct.pack(">I", zlib.crc32(tag + payload) & 0xFFFFFFFF))
def sheet_to_png(sheet: Sequence[int], width: int = SHEET_W,
height: int = SHEET_H) -> bytes:
levels = bytes((0, 85, 170, 255))
raw = bytearray()
for y in range(height):
raw.append(0)
raw += bytes(levels[v & 3] for v in sheet[y * width:(y + 1) * width])
return (b"\x89PNG\r\n\x1a\n"
+ _png_chunk(b"IHDR", struct.pack(">IIBBBBB", width, height, 8, 0, 0, 0, 0))
+ _png_chunk(b"IDAT", zlib.compress(bytes(raw), 6))
+ _png_chunk(b"IEND", b""))
def png_to_gray(data: bytes) -> Tuple[int, int, bytearray]:
"""Decode a non-interlaced 8-bit PNG (gray, gray+alpha, RGB, RGBA, or
8-bit palette) to (width, height, gray bytes). Enough for edited sheets.
Transparency counts as background: a pixel's value is multiplied by its
alpha (a transparent pixel is 0, whatever colour is stored under it).
Chunk lengths and CRCs are checked."""
if data[:8] != b"\x89PNG\r\n\x1a\n":
raise FontError("not a PNG file")
pos, idat, plte, trns = 8, bytearray(), b"", b""
width = height = depth = ctype = 0
ended = False
while pos < len(data):
if pos + 12 > len(data):
raise FontError("PNG cut off inside a chunk header")
(ln,) = struct.unpack_from(">I", data, pos)
tag = data[pos + 4:pos + 8]
body = data[pos + 8:pos + 8 + ln]
if pos + 12 + ln > len(data):
raise FontError(f"PNG chunk {tag!r} runs past the end of the file")
if struct.unpack_from(">I", data, pos + 8 + ln)[0] != zlib.crc32(tag + body) & 0xFFFFFFFF:
raise FontError(f"PNG chunk {tag!r} fails its CRC")
if tag == b"tRNS":
trns = body
if tag == b"IHDR":
width, height, depth, ctype, _, _, inter = struct.unpack(">IIBBBBB", body)
if depth != 8 or inter:
raise FontError("PNG must be 8-bit, non-interlaced")
elif tag == b"PLTE":
plte = body
elif tag == b"IDAT":
idat += body
elif tag == b"IEND":
ended = True
break
pos += 12 + ln
if not ended or not width or ctype not in (0, 2, 3, 4, 6):
raise FontError("PNG without IHDR/IEND or of an unsupported colour type")
bpp = {0: 1, 2: 3, 3: 1, 4: 2, 6: 4}[ctype]
try:
raw = zlib.decompress(bytes(idat))
except zlib.error as e:
raise FontError(f"PNG image data does not decompress: {e}")
stride = width * bpp
if len(raw) != height * (stride + 1):
raise FontError("PNG image data has the wrong length")
prev = bytearray(stride)
gray = bytearray(width * height)
p = 0
for y in range(height):
f = raw[p]
line = bytearray(raw[p + 1:p + 1 + stride])
p += 1 + stride
if f == 1:
for i in range(bpp, stride):
line[i] = (line[i] + line[i - bpp]) & 0xFF
elif f == 2:
for i in range(stride):
line[i] = (line[i] + prev[i]) & 0xFF
elif f == 3:
for i in range(stride):
a = line[i - bpp] if i >= bpp else 0
line[i] = (line[i] + ((a + prev[i]) >> 1)) & 0xFF
elif f == 4:
for i in range(stride):
a = line[i - bpp] if i >= bpp else 0
b = prev[i]
c = prev[i - bpp] if i >= bpp else 0
pa, pb, pc = abs(b - c), abs(a - c), abs(a + b - 2 * c)
pred = a if (pa <= pb and pa <= pc) else (b if pb <= pc else c)
line[i] = (line[i] + pred) & 0xFF
elif f != 0:
raise FontError(f"bad PNG filter {f}")
row = y * width
if ctype == 3:
for x in range(width):
i = line[x] * 3
if i + 2 >= len(plte):
raise FontError("PNG palette index out of range")
g = (plte[i] * 299 + plte[i + 1] * 587 + plte[i + 2] * 114) // 1000
a = trns[line[x]] if line[x] < len(trns) else 255
gray[row + x] = g * a // 255
elif bpp == 1:
gray[row:row + width] = line
else:
for x in range(width):
i = x * bpp
if bpp == 2:
gray[row + x] = line[i] * line[i + 1] // 255
else:
g = (line[i] * 299 + line[i + 1] * 587 + line[i + 2] * 114) // 1000
gray[row + x] = g * line[i + 3] // 255 if bpp == 4 else g
prev = line
return width, height, gray
def quantize(gray: Sequence[int]) -> bytearray:
"""8-bit gray -> 4 levels (thresholds between the 0/85/170/255 steps)."""
return bytearray(0 if g <= 42 else 1 if g <= 127 else 2 if g <= 212 else 3
for g in gray)
# --- CLI -------------------------------------------------------------------
def write_table(path: Path, addressing: "Addressing", font: Optional["Font"]) -> None:
"""The byte -> cell -> metric table as CSV, for $han1;$prop1; text (what the menus
and dialogue set) and, in the mode column, for $han1;$prop0; too."""
fields = ["mode", "byte", "kind", "code", "cell", "drawn_cell", "metric_slot", "left", "right", "advance"]
with open(path, "w", newline="", encoding="utf-8") as fh:
w = csv.DictWriter(fh, fieldnames=fields)
w.writeheader()
for prop in (True, False):
for row in addressing.table(font.metrics if font else (), han=True, prop=prop):
w.writerow(dict(row, mode="han1 prop1" if prop else "han1 prop0"))
def cmd_extract(tex: str, out: str) -> int:
font = Font.load(tex)
outp = Path(out)
outp.mkdir(parents=True, exist_ok=True)
stem = Path(tex).stem
for i, sheet in enumerate(font.sheets):
(outp / f"{stem}_sheet{i}.png").write_bytes(sheet_to_png(sheet))
with open(outp / f"{stem}_metrics.csv", "w", newline="", encoding="utf-8") as fh:
w = csv.writer(fh)
# slot = the cell number a character maps to; its proportional image is cell + 576
w.writerow(["slot", "drawn_cell", "left", "right"])
for slot, (l, r) in enumerate(font.metrics):
w.writerow([slot, slot + PROP_CELL_SHIFT, l, r])
print(f"{tex}: 4 sheets + {len(font.metrics)} metrics -> {outp}")
return 0
def cmd_rebuild(ref: str, sheets_dir: str, out: str, metrics_csv: Optional[str],
allow_unchanged: bool = False) -> int:
"""Build a font from the reference file, the sheet PNGs found in ``sheets_dir``
(``<ref stem>_sheet0.png`` .. ``_sheet3.png``) and the metrics CSV. Says what
was applied. With no sheet found and no metrics given there is nothing to
build from, which is refused (a wrong folder, or a renamed reference) unless
``allow_unchanged`` asks for the plain copy. The output may not be the reference."""
if Path(out).exists() and os.path.samefile(ref, out) or Path(out).resolve() == Path(ref).resolve():
raise FontError(f"--out {out} is the reference file itself; give another name")
data = Path(ref).read_bytes()
font = Font.from_bytes(data)
stem = Path(ref).stem
names = [f"{stem}_sheet{i}.png" for i in range(4)]
applied, changed_sheets = [], []
for i, name in enumerate(names):
p = Path(sheets_dir) / name
if not p.exists():
continue
w, h, gray = png_to_gray(p.read_bytes())
if (w, h) != (SHEET_W, SHEET_H):
raise FontError(f"{p}: sheet must be {SHEET_W}x{SHEET_H}, got {w}x{h}")
new = quantize(gray)
if new != font.sheets[i]:
changed_sheets.append(i)
font.sheets[i] = new
applied.append(i)
n_rows = n_changed = 0
if metrics_csv:
seen = set()
with open(metrics_csv, newline="", encoding="utf-8") as fh:
for row in csv.DictReader(fh):
try:
slot, left, right = int(row["slot"]), int(row["left"]), int(row["right"])
except (KeyError, TypeError, ValueError):
raise FontError(f"{metrics_csv}: a row without whole-number slot, left and right: {dict(row)}")
if slot in seen:
raise FontError(f"{metrics_csv}: slot {slot} appears twice")
seen.add(slot)
n_rows += 1
n_changed += font.metrics[slot] != (left, right) if 0 <= slot < len(font.metrics) else 0
font.set_metrics(slot, left, right)
if not applied and not n_rows and not allow_unchanged:
raise FontError(f"nothing to apply: none of {', '.join(names)} is in {sheets_dir} and no metrics were "
f"given. The sheet names follow the reference file's name ({Path(ref).name}). "
f"Pass --allow-unchanged to write a plain copy of the reference")
blob = font.to_bytes()
if len(blob) != len(data): # cannot happen through set_metrics; kept as a guard
raise FontError(f"rebuilt font is {len(blob)} bytes, the original {len(data)}: the size must not change")
Path(out).write_bytes(blob)
print(f"applied: sheets {applied or 'none'} ({len(changed_sheets)} differ from the reference: "
f"{changed_sheets or 'none'}); metrics rows {n_rows} ({n_changed} changed)")
print(f"wrote {out}" + (" (identical to the reference)" if blob == data else ""))
return 0
def cmd_info(tex: str) -> int:
data = Path(tex).read_bytes()
font = Font.from_bytes(data)
ok = font.to_bytes() == data
used = sum(1 for m in font.metrics if m[0] != 0)
match = sum(1 for c, m in enumerate(font.metrics) if font.ink_bounds(c + PROP_CELL_SHIFT) == m)
print(f"{tex}: {len(data)} bytes, {len(font.metrics)} metric pairs ({used} with a width, "
f"{match} equal to the ink of cell + {PROP_CELL_SHIFT}), round-trip {'OK' if ok else 'MISMATCH'}")
return 0 if ok else 1
def cmd_table(iso_path: str, out: Optional[str]) -> int:
import region
from chains import CHAINS, ChainReader
from iso9660 import IsoImage
from toc import parse_toc
iso = IsoImage(iso_path)
prof = region.region_of(iso)
adr = Addressing.from_iso(iso)
reader = ChainReader(iso, 0)
byp = {e.path.lower(): e for e in parse_toc(bytes(reader.toc_bytes()), CHAINS[0][0])}
e = byp[prof.fonts[0]]
font = Font.from_bytes(reader.read(e.byte_offset, e.size))
print(f"{prof.name}: {len(adr.extra)} extra kanji, proportional below cell {adr.prop_cells}, "
f"advance right-left+{adr.prop_pad}, cells below {adr.drawable} reachable")
if out: