-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkobofix.py
More file actions
executable file
·1707 lines (1457 loc) · 68.3 KB
/
kobofix.py
File metadata and controls
executable file
·1707 lines (1457 loc) · 68.3 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
#!/usr/bin/env python3
"""
Font processing utility for Kobo e-readers.
Processes TrueType fonts to improve compatibility with Kobo e-readers:
- Converting OTF (CFF) inputs to TTF (glyf) first, so all downstream steps
operate on a true TrueType font with no CFF metadata left behind
- Renaming fonts with a configurable prefix and updating internal metadata
(name table, CFF, post table, PS name)
- Validating and correcting PANOSE metadata based on font style
- Updating font weight metadata (OS/2 usWeightClass)
- Adjusting line spacing via font-line
- Simplifying outlines w/ skia-pathops
- Kerning: extracting GPOS pairs (Format 1, Format 2, and Extension lookups)
into a legacy kern table, prioritized by Unicode range to fit within
format 0 size constraints
- Hinting: optionally stripping hints or applying ttfautohint
Supports --dry-run to preview what would change without modifying files.
Includes NV and KF presets for common workflows, or can be fully
configured via individual flags. Run with -h for usage details.
Requirements:
- fontTools (pip install fonttools)
- font-line (pip install font-line)
- skia-pathops (pip install skia-pathops)
- ttfautohint (optional, for --hint additive/overwrite)
"""
import sys
import os
import re
import shutil
import subprocess
import argparse
import logging
import string
from datetime import date
from pathlib import Path
from collections import defaultdict
from dataclasses import dataclass
from typing import Dict, Tuple, Optional, List
from fontTools.ttLib import TTFont, newTable
from fontTools.ttLib.tables._k_e_r_n import KernTable_format_0
# -------------
# PRESETS
# -------------
#
PRESETS = {
"nv": {
"prefix": "NV",
"line_percent": 20,
"kern": "skip",
"hint": "skip",
"outline": "skip",
},
"kf": {
"prefix": "KF",
"line_percent": 0,
"kern": "add-legacy-kern",
"hint": "skip",
"outline": "apply",
"remove_prefix": "NV",
},
}
# Known prefixes are automatically detected and stripped before applying
# the preset's prefix. This ensures idempotent processing.
KNOWN_PREFIXES = sorted(
{p["prefix"] for p in PRESETS.values() if "prefix" in p},
key=len, reverse=True # longest first to avoid partial matches
)
# -------------
# STYLE MAPPING
# -------------
# Style mapping for filenames and internal font data.
# If a particular style string is found in the font file name, it can be mapped.
# The values are a tuple of (human-readable_style_name, usWeightClass).
#
STYLE_MAP = {
"BoldItalic": ("Bold Italic", 700),
"Bold": ("Bold", 700),
"Italic": ("Italic", 400),
"Regular": ("Regular", 400),
}
# Configure logging for clear output
logging.basicConfig(level=logging.INFO, format='%(message)s')
logger = logging.getLogger(__name__)
ASCII_PRIORITY_CODEPOINTS = {
ord(ch) for ch in (string.ascii_letters + string.digits + string.punctuation)
}
TYPOGRAPHIC_PRIORITY_CODEPOINTS = {
0x00AB, # LEFT-POINTING DOUBLE ANGLE QUOTATION MARK
0x00BB, # RIGHT-POINTING DOUBLE ANGLE QUOTATION MARK
0x2013, # EN DASH
0x2014, # EM DASH
0x2018, # LEFT SINGLE QUOTATION MARK
0x2019, # RIGHT SINGLE QUOTATION MARK
0x201A, # SINGLE LOW-9 QUOTATION MARK
0x201C, # LEFT DOUBLE QUOTATION MARK
0x201D, # RIGHT DOUBLE QUOTATION MARK
0x201E, # DOUBLE LOW-9 QUOTATION MARK
0x2026, # HORIZONTAL ELLIPSIS
0x2039, # SINGLE LEFT-POINTING ANGLE QUOTATION MARK
0x203A, # SINGLE RIGHT-POINTING ANGLE QUOTATION MARK
}
STAMP_URL = "https://github.com/nicoverbruggen/kobo-font-fix"
STAMP_RE = re.compile(
r"\s*Patched with `kobo-font-fix` \([^)]*\) for improved compatibility with Kobo devices on \d{4}-\d{2}-\d{2}\.",
)
def _build_stamped_copyright(current: str) -> str:
"""Return copyright text with a fresh kobo-font-fix stamp appended.
Any prior stamp (same tool, any date/url) is removed first so repeated
runs don't accumulate lines.
"""
cleaned = STAMP_RE.sub("", current or "").rstrip()
today = date.today().isoformat()
suffix = f"Patched with `kobo-font-fix` ({STAMP_URL}) for improved compatibility with Kobo devices on {today}."
return f"{cleaned}\n\n{suffix}" if cleaned else suffix
@dataclass
class FontMetadata:
"""
A simple data class to hold consistent font naming and metadata.
"""
family_name: str
style_name: str
full_name: str
ps_name: str
class FontProcessor:
"""
Main font processing class.
"""
def __init__(self,
prefix: str,
line_percent: int,
):
"""
Initialize the font processor with configurable values.
If the user has not supplied custom arguments, the default values are used.
Args:
prefix: Prefix to add to font names
line_percent: Percentage for baseline adjustment
"""
self.prefix = prefix
self.line_percent = line_percent
@staticmethod
def _find_available_ots() -> Optional[Path]:
"""Return a usable ots-sanitize path if already installed or cached."""
import shutil as sh
binary = "ots-sanitize.exe" if os.name == "nt" else "ots-sanitize"
system_binary = sh.which(binary)
if system_binary:
return Path(system_binary)
tools_dir = Path(__file__).resolve().parent / ".tools"
if not tools_dir.exists():
return None
for path in tools_dir.rglob(binary):
if path.is_file():
return path
return None
@staticmethod
def _validate_font(ots: Path, font_path: Path) -> Tuple[bool, str]:
"""Run ots-sanitize against a font. Returns (ok, combined_output)."""
import subprocess as sp
result = sp.run(
[str(ots), str(font_path), os.devnull],
capture_output=True,
text=True,
)
output = (result.stdout + result.stderr).strip()
return result.returncode == 0, output
@staticmethod
def _validate_output_font(output_path: str) -> bool:
"""Validate the final output font if ots-sanitize is already available."""
ots = FontProcessor._find_available_ots()
if not ots:
logger.warning("WARNING: skipped ots-sanitize step (missing)")
return True
ok, output = FontProcessor._validate_font(ots, Path(output_path))
status = "OK" if ok else "FAIL"
logger.info(f" ots-sanitize: {status}")
if output:
for line in output.splitlines():
logger.info(f" {line}")
return ok
# ============================================================
# Helper methods
# ============================================================
@staticmethod
def _get_style_from_filename(filename: str) -> Tuple[str, int]:
"""
Determine font style and weight from filename.
This function centralizes a critical piece of logic that is used in
multiple places to ensure consistency across the script.
Args:
filename: The font file name.
Returns:
A tuple of (style_name, usWeightClass).
"""
base_filename = os.path.basename(filename)
for key, (style_name, weight) in STYLE_MAP.items():
if key.lower() in base_filename.lower():
return style_name, weight
return "Regular", 400 # Default if no style found
@staticmethod
def _set_name_records(font: TTFont, name_id: int, new_name: str) -> None:
"""
Update a font's name table record for all relevant platforms,
encodings, and languages.
This method has been updated to iterate through all existing records
for the given name_id and update them, ensuring consistent naming
across different platforms like Windows and Macintosh.
"""
name_table = font["name"]
# Find all existing records for the given nameID
names_to_update = [
n for n in name_table.names if n.nameID == name_id
]
# If no records exist, add new ones for standard platforms.
if not names_to_update:
logger.debug(f" Name ID {name_id} not found; adding new records.")
try:
name_table.setName(new_name, name_id, 3, 1, 0x0409) # Windows record
name_table.setName(new_name, name_id, 1, 0, 0) # Macintosh record
logger.debug(f" Name ID {name_id} added as '{new_name}'.")
except Exception as e:
logger.warning(f" Failed to add new name ID {name_id}: {e}")
return
updated_count = 0
for name_record in names_to_update:
try:
# Determine the appropriate encoding for the platform
if name_record.platformID == 1: # Macintosh
encoded_name = new_name.encode('mac-roman', 'ignore')
if name_record.string != encoded_name:
name_record.string = encoded_name
updated_count += 1
elif name_record.platformID in (0, 3): # Unicode and Windows
encoded_name = new_name.encode('utf-16-be', 'ignore')
if name_record.string != encoded_name:
name_record.string = encoded_name
updated_count += 1
else:
# Fallback for other platforms to ensure consistency
encoded_name = new_name.encode('utf-8', 'ignore')
if name_record.string != encoded_name:
name_record.string = encoded_name
updated_count += 1
except Exception as e:
logger.warning(f" Failed to update record for Name ID {name_id} "
f"(Platform {name_record.platformID}, "
f"Encoding {name_record.platEncID}): {e}")
if updated_count > 0:
logger.debug(f" Name ID {name_id} updated for {updated_count} record(s).")
else:
logger.debug(f" Name ID {name_id} is already correct across all records.")
# ============================================================
# Metadata extraction
# ============================================================
def _get_font_metadata(
self,
font: TTFont,
font_path: str,
new_family_name: Optional[str]
) -> Optional[FontMetadata]:
"""
Extract or infer font metadata from the font and arguments.
This function acts as a single point of truth for font metadata,
ensuring consistency throughout the processing pipeline.
"""
if "name" in font:
# Determine family name from user input or best available name from font.
family_name = new_family_name if new_family_name else font["name"].getBestFamilyName()
else:
family_name = new_family_name
if not family_name:
logger.warning(" Could not determine font family name.")
return None
# Centralized logic: Determine style name from filename.
style_name, _ = self._get_style_from_filename(font_path)
# Construct the full name and PS name based on style name logic
full_name = f"{family_name}"
if style_name != "Regular":
full_name += f" {style_name}"
# If prefix is empty, don't add it to the PS name
if self.prefix:
ps_name = f"{self.prefix}_{family_name.replace(' ', '-')}"
else:
ps_name = family_name.replace(' ', '-')
if style_name != "Regular":
ps_name += f"-{style_name.replace(' ', '')}"
logger.debug(f" Constructed metadata: family='{family_name}', style='{style_name}', full='{full_name}', ps='{ps_name}'")
return FontMetadata(
family_name=family_name,
style_name=style_name,
full_name=full_name,
ps_name=ps_name
)
# ============================================================
# Kerning extraction methods
# ============================================================
@staticmethod
def _pair_value_to_kern(value1, value2) -> int:
"""
Compute a legacy kerning value from GPOS PairValue records.
This logic is specific to converting GPOS (OpenType) kerning to
the older 'kern' (TrueType) table format.
Note: Only XAdvance values are used, as they directly map to kern table semantics
(adjusting inter-character spacing). XPlacement values shift glyphs without
affecting spacing and cannot be represented in the legacy kern table. To avoid
potential issues, XPlacement values are now being ignored.
"""
kern_value = 0
if value1 is not None:
kern_value += getattr(value1, "XAdvance", 0) or 0
if value2 is not None:
kern_value += getattr(value2, "XAdvance", 0) or 0
return int(kern_value)
def _extract_format1_pairs(self, subtable) -> Dict[Tuple[str, str], int]:
"""Extract kerning pairs from PairPos Format 1 (per-glyph PairSets)."""
pairs = {}
coverage = getattr(subtable, "Coverage", None)
pair_sets = getattr(subtable, "PairSet", [])
if not coverage or not hasattr(coverage, "glyphs"):
return pairs
for idx, left_glyph in enumerate(coverage.glyphs):
if idx >= len(pair_sets):
break
for record in getattr(pair_sets[idx], "PairValueRecord", []):
right_glyph = record.SecondGlyph
kern_value = self._pair_value_to_kern(record.Value1, record.Value2)
if kern_value:
# Only set if not already present (first value wins)
key = (left_glyph, right_glyph)
if key not in pairs:
pairs[key] = kern_value
return pairs
def _extract_format2_pairs(self, font: TTFont, subtable) -> Dict[Tuple[str, str], int]:
"""Extract kerning pairs from PairPos Format 2 (class-based)."""
pairs = {}
coverage = getattr(subtable, "Coverage", None)
class_def1 = getattr(subtable, "ClassDef1", None)
class_def2 = getattr(subtable, "ClassDef2", None)
class1_records = getattr(subtable, "Class1Record", [])
if not coverage or not hasattr(coverage, "glyphs"):
return pairs
class1_map = getattr(class_def1, "classDefs", {}) if class_def1 else {}
left_by_class = defaultdict(list)
for glyph in coverage.glyphs:
class_idx = class1_map.get(glyph, 0)
left_by_class[class_idx].append(glyph)
class2_map = getattr(class_def2, "classDefs", {}) if class_def2 else {}
right_by_class = defaultdict(list)
for glyph, class_idx in class2_map.items():
right_by_class[class_idx].append(glyph)
for glyph in font.getGlyphOrder():
if glyph not in class2_map:
right_by_class[0].append(glyph)
for class1_idx, class1_record in enumerate(class1_records):
left_glyphs = left_by_class.get(class1_idx, [])
if not left_glyphs:
continue
for class2_idx, class2_record in enumerate(class1_record.Class2Record):
right_glyphs = right_by_class.get(class2_idx, [])
if not right_glyphs:
continue
kern_value = self._pair_value_to_kern(class2_record.Value1, class2_record.Value2)
if not kern_value:
continue
for left in left_glyphs:
for right in right_glyphs:
# Only set if not already present (first value wins)
key = (left, right)
if key not in pairs:
pairs[key] = kern_value
return pairs
def extract_kern_pairs(self, font: TTFont) -> Dict[Tuple[str, str], int]:
"""
Extract kerning pairs from the font.
Prioritizes existing 'kern' table over GPOS data if present.
GPOS (Glyph Positioning) is the modern standard for kerning in OpenType fonts.
"""
pairs = {}
# If a kern table already exists, use it instead of GPOS
if "kern" in font:
kern_table = font["kern"]
for subtable in getattr(kern_table, "kernTables", []):
if hasattr(subtable, "kernTable"):
pairs.update(subtable.kernTable)
return pairs
# Otherwise, extract from GPOS
if "GPOS" in font:
gpos = font["GPOS"].table
lookup_list = getattr(gpos, "LookupList", None)
if lookup_list and lookup_list.Lookup:
for lookup in lookup_list.Lookup:
lookup_type = getattr(lookup, "LookupType", None)
subtables = getattr(lookup, "SubTable", [])
# Unwrap Extension lookups (type 9) to get the inner subtables
if lookup_type == 9:
unwrapped = []
for ext_subtable in subtables:
ext_type = getattr(ext_subtable, "ExtensionLookupType", None)
inner = getattr(ext_subtable, "ExtSubTable", None)
if ext_type == 2 and inner is not None:
unwrapped.append(inner)
subtables = unwrapped
lookup_type = 2 if unwrapped else None
if lookup_type != 2:
continue
for subtable in subtables:
fmt = getattr(subtable, "Format", None)
if fmt == 1:
extracted = self._extract_format1_pairs(subtable)
elif fmt == 2:
extracted = self._extract_format2_pairs(font, subtable)
else:
continue
for key, value in extracted.items():
if key not in pairs:
pairs[key] = value
return pairs
@staticmethod
def _glyph_priority(glyph_name: str, cmap_reverse: Dict[str, int]) -> int:
"""
Assign a priority to a glyph for kern pair sorting.
Lower values = higher priority. Pairs involving common glyphs
are prioritized so they fit within the subtable size limit.
"""
cp = cmap_reverse.get(glyph_name)
if cp is None:
return 5 # unmapped glyphs (ligatures, alternates, etc.)
if cp in ASCII_PRIORITY_CODEPOINTS:
return 0 # ASCII letters, digits, and punctuation
if cp in TYPOGRAPHIC_PRIORITY_CODEPOINTS:
return 1 # smart quotes, dashes, ellipsis, guillemets
if cp <= 0x007F:
return 2 # other Basic Latin codepoints
if cp <= 0x00FF:
return 3 # Latin-1 Supplement (accented chars, common symbols)
if cp <= 0x024F:
return 4 # Latin Extended-A and B
return 5 # everything else
@staticmethod
def add_legacy_kern(font: TTFont, kern_pairs: Dict[Tuple[str, str], int]) -> int:
"""
Create or replace a legacy 'kern' table with the supplied pairs.
The legacy kern table format has strict size constraints:
- Most renderers (including Kobo's WebKit-based engine) only read the
first subtable, so we write exactly one.
- Format 0 subtables have a uint16 length field (max 65,535 bytes).
With a 14-byte header and 6 bytes per pair, this allows at most
(65,535 - 14) / 6 = 10,920 pairs before the length overflows.
When a font has more pairs than this (common with class-based GPOS
kerning, which can expand to 100k+ individual pairs), we prioritize
by Unicode range so the most commonly encountered pairs are kept:
- ASCII letters, digits, and punctuation
- Common typography punctuation (smart quotes, dashes, ellipsis, guillemets)
- Other Basic Latin codepoints (for example control-space block)
- Latin-1 Supplement (accented chars, common symbols)
- Latin Extended-A/B (Central/Eastern European chars)
- Everything else and unmapped glyphs (ligatures, alternates)
This means all English kerning is preserved, most Western European
kerning (French, German, Spanish, etc.) is preserved, and only less
common extended Latin pairings are dropped when truncation is needed.
"""
if not kern_pairs:
return 0
MAX_PAIRS = 10920
items = [(tuple(k), int(v)) for k, v in kern_pairs.items() if v]
if len(items) > MAX_PAIRS:
# Build reverse cmap (glyph name -> codepoint) for prioritization
cmap_reverse = {}
if "cmap" in font:
for table in font["cmap"].tables:
if hasattr(table, "cmap"):
for cp, glyph_name in table.cmap.items():
if glyph_name not in cmap_reverse:
cmap_reverse[glyph_name] = cp
# Sort by priority of both glyphs (lower = more common)
items.sort(key=lambda pair: (
FontProcessor._glyph_priority(pair[0][0], cmap_reverse) +
FontProcessor._glyph_priority(pair[0][1], cmap_reverse)
))
logger.warning(f" Kerning: {len(items)} pairs exceed the subtable limit of {MAX_PAIRS}. "
f"Keeping the {MAX_PAIRS} most common pairs.")
items = items[:MAX_PAIRS]
kern_table = newTable("kern")
kern_table.version = 0
kern_table.kernTables = []
subtable = KernTable_format_0()
subtable.version = 0
subtable.length = None
subtable.coverage = 1
subtable.kernTable = dict(items)
kern_table.kernTables.append(subtable)
# Additional subtables are not created because most renderers
# (including Kobo's WebKit-based engine) only read the first one.
font["kern"] = kern_table
return len(items)
# ============================================================
# OT-layout normalization
# ============================================================
# Map GSUB/GPOS subtable types (LookupType, Format) to the attribute
# name of the array that runs parallel to Coverage.glyphs. Subtables
# not listed here either have no parallel array (e.g. Single Format 1,
# where every covered glyph shares the same value) or use class-based
# indexing that is independent of Coverage order (e.g. Pair Format 2).
_PARALLEL_ARRAYS = {
("GSUB", 1, 2): "Substitute",
("GSUB", 2, 1): "Sequence",
("GSUB", 3, 1): "AlternateSet",
("GSUB", 4, 1): "LigatureSet",
("GPOS", 1, 2): "Value",
("GPOS", 2, 1): "PairSet",
("GPOS", 3, 1): "EntryExitRecord",
}
@classmethod
def _sort_coverage_subtable(cls, sub, lookup_type: int, table_tag: str, font: TTFont) -> bool:
"""Sort Coverage.glyphs on one subtable, permuting its parallel array.
Returns True if the subtable was modified. Extension lookups
(GSUB Type 7, GPOS Type 9) are unwrapped so the inner subtable is
handled instead.
"""
# Unwrap Extension lookups
if lookup_type in (7, 9) and hasattr(sub, "ExtSubTable"):
inner_type = getattr(sub, "ExtensionLookupType", None)
if inner_type:
return cls._sort_coverage_subtable(sub.ExtSubTable, inner_type, table_tag, font)
return False
cov = getattr(sub, "Coverage", None)
if cov is None or not hasattr(cov, "glyphs"):
return False
glyph_ids = [font.getGlyphID(g) for g in cov.glyphs]
if glyph_ids == sorted(glyph_ids):
return False
fmt = getattr(sub, "Format", None)
parallel_attr = cls._PARALLEL_ARRAYS.get((table_tag, lookup_type, fmt))
# Compute permutation that sorts glyph IDs ascending
order = sorted(range(len(cov.glyphs)), key=glyph_ids.__getitem__)
cov.glyphs = [cov.glyphs[i] for i in order]
if parallel_attr and hasattr(sub, parallel_attr):
arr = getattr(sub, parallel_attr)
if arr is not None and len(arr) == len(order):
setattr(sub, parallel_attr, [arr[i] for i in order])
return True
@classmethod
def normalize_coverage(cls, font: TTFont) -> int:
"""Sort every Coverage table in GSUB/GPOS (and permute its parallel
array), so renderers that binary-search Coverage get correct results.
Returns the number of subtables that were fixed. Lenient shapers
(HarfBuzz, WebKit) accept unsorted Coverage, but strict consumers
on desktop or legacy platforms can silently drop kern pairs or
ligatures without this.
"""
fixed = 0
for tag in ("GSUB", "GPOS"):
if tag not in font:
continue
table = font[tag].table
lookup_list = getattr(table, "LookupList", None)
if lookup_list is None:
continue
for lookup in lookup_list.Lookup:
lt = lookup.LookupType
for sub in lookup.SubTable:
if cls._sort_coverage_subtable(sub, lt, tag, font):
fixed += 1
return fixed
# ============================================================
# Name table methods
# ============================================================
def rename_font(self, font: TTFont, metadata: FontMetadata) -> None:
"""
Update the font's name-related metadata.
This method uses the centralized `_set_name_records` helper to update
all relevant name fields.
"""
if "name" not in font:
logger.warning(" No 'name' table found; skipping all name changes")
return
if self.prefix:
logger.info(" Renaming the font to: " + f"{self.prefix} {metadata.full_name}")
adjusted_family_name = f"{self.prefix} {metadata.family_name}"
adjusted_full_name = f"{self.prefix} {metadata.full_name}"
else:
logger.info(" Updating font metadata (no prefix)")
adjusted_family_name = metadata.family_name
adjusted_full_name = metadata.full_name
# Update Family Name
self._set_name_records(font, 1, adjusted_family_name)
# Update Subfamily
self._set_name_records(font, 2, metadata.style_name)
# Update Full Name
self._set_name_records(font, 4, adjusted_full_name)
# Update Typographic Family
self._set_name_records(font, 16, adjusted_family_name)
# Update Typographic Subfamily
self._set_name_records(font, 17, metadata.style_name)
# Update Compatible Full Name
self._set_name_records(font, 18, adjusted_full_name)
# Update Unique ID (ID 3)
try:
current_unique = font["name"].getName(3, 3, 1).toUnicode()
parts = current_unique.split("Version")
version_info = f"Version{parts[1]}" if len(parts) == 2 else "Version 1.000"
if self.prefix:
new_unique_id = f"{self.prefix} {metadata.family_name.strip()}:{version_info}"
else:
new_unique_id = f"{metadata.family_name.strip()}:{version_info}"
if current_unique != new_unique_id:
self._set_name_records(font, 3, new_unique_id)
except Exception as e:
logger.warning(f" Failed to update Unique ID: {e}")
# Update PostScript Name (ID 6)
new_ps_name = metadata.ps_name
self._set_name_records(font, 6, new_ps_name)
# Update PostScript data in CFF (if applicable)
if "CFF " in font:
cff = font["CFF "].cff
cff_topdict = cff.topDictIndex[0]
if self.prefix:
cff_full_name = f"{self.prefix} {metadata.full_name}"
cff_family_name = f"{self.prefix} {metadata.family_name.replace(' ', '_')}"
else:
cff_full_name = metadata.full_name
cff_family_name = metadata.family_name.replace(' ', '_')
name_mapping = {
"FullName": cff_full_name,
"FamilyName": cff_family_name
}
for key, new_value in name_mapping.items():
if key in cff_topdict.rawDict:
current_value = cff_topdict.rawDict[key]
if current_value != new_value:
cff_topdict.rawDict[key] = new_value
logger.debug(f" CFF table '{key}' updated to '{new_value}'.")
else:
logger.debug(f" CFF table '{key}' is already correct.")
logger.warning(" CFF table found. The original font name may persist as part of an indexed `Name INDEX`. (This cannot be easily fixed with this script. If you are encountering issues, I recommend using FontForge.)")
else:
logger.debug(" No 'CFF' table in this font.")
# Update PostScript data if relevant
if "post" in font:
if hasattr(font["post"], "fontName"):
new_ps_name = metadata.ps_name
if font["post"].fontName != new_ps_name:
font["post"].fontName = new_ps_name
logger.debug(f" 'post' table updated with new fontName '{new_ps_name}'.")
else:
logger.debug(" 'post' table fontName is already correct.")
else:
logger.debug(" No 'post' table in this font.")
# ============================================================
# Copyright stamp
# ============================================================
def stamp_copyright(self, font: TTFont) -> None:
"""Append a kobo-font-fix patch stamp to the copyright (name ID 0)."""
if "name" not in font:
return
record = font["name"].getName(0, 3, 1, 0x0409) or font["name"].getName(0, 1, 0, 0)
current = record.toUnicode() if record else ""
new_value = _build_stamped_copyright(current)
if new_value != current:
self._set_name_records(font, 0, new_value)
logger.info(" Notice stamp applied in copyright section")
# ============================================================
# Weight metadata methods
# ============================================================
def update_weight_metadata(self, font: TTFont, filename: str) -> None:
"""
Update font weight metadata based on filename suffix.
This function uses the centralized style lookup, which simplifies
the logic significantly.
"""
style_name, os2_weight = self._get_style_from_filename(filename)
ps_weight = style_name.replace(" ", "")
if "OS/2" in font and hasattr(font["OS/2"], "usWeightClass"):
if font["OS/2"].usWeightClass != os2_weight:
font["OS/2"].usWeightClass = os2_weight
logger.debug(f" OS/2 usWeightClass updated to {os2_weight}.")
else:
logger.debug(" OS/2 usWeightClass is already correct.")
if "CFF " in font and hasattr(font["CFF "].cff.topDictIndex[0], "Weight"):
if getattr(font["CFF "].cff.topDictIndex[0], "Weight", "") != ps_weight:
font["CFF "].cff.topDictIndex[0].Weight = ps_weight
logger.debug(f" PostScript CFF weight updated to '{ps_weight}'.")
elif "post" in font and hasattr(font["post"], "Weight"):
if getattr(font["post"], "Weight", "") != ps_weight:
font["post"].Weight = ps_weight
logger.debug(f" PostScript 'post' weight updated to '{ps_weight}'.")
# ============================================================
# Style flag methods
# ============================================================
@staticmethod
def _style_flags(style_name: str) -> Tuple[bool, bool, bool]:
"""Return (is_bold, is_italic, is_regular) derived from style_name."""
is_italic = "Italic" in style_name
is_bold = "Bold" in style_name
is_regular = style_name == "Regular"
return is_bold, is_italic, is_regular
@staticmethod
def _expected_style_flags(style_name: str) -> Tuple[int, int]:
"""Return the expected (fsSelection, macStyle) bit masks for a style.
Only the style-related bits are returned; callers must merge these
into existing values while clearing the style bits they manage.
"""
is_bold, is_italic, is_regular = FontProcessor._style_flags(style_name)
fs = 0
if is_italic: fs |= 0x01 # ITALIC
if is_bold: fs |= 0x20 # BOLD
if is_regular: fs |= 0x40 # REGULAR
ms = 0
if is_bold: ms |= 0x01
if is_italic: ms |= 0x02
return fs, ms
def update_style_flags(self, font: TTFont, filename: str) -> None:
"""Synchronize OS/2.fsSelection and head.macStyle with the style_name.
Some source fonts ship with style-related bits that disagree with
the name table (for example an italic weight with the italic bits
cleared and the regular bit set). Once the name table is rewritten
to reflect the filename-derived style, the binary flags must be
brought into agreement — otherwise renderers that trust fsSelection
or macStyle instead of the name table will treat the font wrongly.
"""
style_name, _ = self._get_style_from_filename(filename)
expected_fs, expected_ms = self._expected_style_flags(style_name)
style_fs_mask = 0x01 | 0x20 | 0x40 # italic | bold | regular
style_ms_mask = 0x01 | 0x02 # bold | italic
if "OS/2" in font and hasattr(font["OS/2"], "fsSelection"):
current = font["OS/2"].fsSelection
new_value = (current & ~style_fs_mask) | expected_fs
if current != new_value:
font["OS/2"].fsSelection = new_value
logger.debug(f" OS/2 fsSelection: 0x{current:04x} -> 0x{new_value:04x}")
if "head" in font and hasattr(font["head"], "macStyle"):
current = font["head"].macStyle
new_value = (current & ~style_ms_mask) | expected_ms
if current != new_value:
font["head"].macStyle = new_value
logger.debug(f" head macStyle: 0x{current:04x} -> 0x{new_value:04x}")
# ============================================================
# PANOSE methods
# ============================================================
def check_and_fix_panose(self, font: TTFont, filename: str) -> None:
"""
Check and adjust PANOSE values based on filename suffix.
PANOSE is an older classification system for fonts. Correcting these
values ensures better compatibility with legacy systems and font menus.
"""
style_name, _ = self._get_style_from_filename(filename)
# PANOSE expected values for each style
style_specs = {
"Bold Italic": {"weight": 8, "letterform": 3},
"Bold": {"weight": 8, "letterform": 2},
"Italic": {"weight": 5, "letterform": 3},
"Regular": {"weight": 5, "letterform": 2},
}
if "OS/2" not in font or not hasattr(font["OS/2"], "panose") or font["OS/2"].panose is None:
logger.warning(" No OS/2 table or PANOSE information found; skipping check.")
return
panose = font["OS/2"].panose
# bFamilyType 0 means "Any" — the font has no meaningful PANOSE data
if panose.bFamilyType == 0:
logger.info(" No PANOSE classification set (bFamilyType=0); skipping check.")
return
expected = style_specs.get(style_name)
if not expected:
logger.warning(f" No PANOSE specification for style '{style_name}'; skipping.")
return
changes = []
if panose.bWeight != expected["weight"]:
old_weight = panose.bWeight
panose.bWeight = expected["weight"]
changes.append(f"bWeight {old_weight}->{expected['weight']}")
if panose.bLetterForm != expected["letterform"]:
old_letterform = panose.bLetterForm
panose.bLetterForm = expected["letterform"]
changes.append(f"bLetterForm {old_letterform}->{expected['letterform']}")
if changes:
logger.info(f" PANOSE corrected: {', '.join(changes)}")
else:
logger.info(" PANOSE check passed, no modifications required.")
# ============================================================
# Hinting methods
# ============================================================
@staticmethod
def _font_has_hints(font: TTFont) -> bool:
"""Check whether a font contains TrueType hinting data."""
if "fpgm" in font or "prep" in font or "cvt " in font:
return True
if "glyf" in font:
for glyph_name in font.getGlyphOrder():
glyph = font["glyf"][glyph_name]
if hasattr(glyph, 'program') and glyph.program and glyph.program.getAssembly():
return True
return False
@staticmethod
def strip_hints(font: TTFont) -> None:
"""Remove all TrueType hints from the font."""
hints_removed = False
for table in ("fpgm", "prep", "cvt "):
if table in font:
del font[table]
hints_removed = True
if "glyf" in font:
for glyph_name in font.getGlyphOrder():
glyph = font["glyf"][glyph_name]
if hasattr(glyph, 'removeHinting'):
glyph.removeHinting()
hints_removed = True
if hints_removed:
logger.info(" Removed TrueType hints from the font.")
else:
logger.info(" No TrueType hints found to remove.")
def apply_ttfautohint(self, font_path: str) -> bool:
"""Run ttfautohint on a saved font file, replacing it in-place.
Uses natural stem widths (--stem-width-mode=nss) for Kobo's FreeType
grayscale renderer, which produces less distortion than the default
strong grid-fitting.
"""
try:
hinted_path = font_path + ".hinted"
subprocess.run(
[
"ttfautohint",
"--stem-width-mode=nss",
font_path,
hinted_path,
],
check=True, stdout=subprocess.DEVNULL, stderr=subprocess.PIPE
)
os.replace(hinted_path, font_path)
logger.info(" Applied ttfautohint (natural stem widths).")
return True
except subprocess.CalledProcessError as e:
logger.warning(f" ttfautohint failed: {e}")
# Clean up temp file if it exists
hinted_path = font_path + ".hinted"
if os.path.exists(hinted_path):
os.remove(hinted_path)
return False
# ============================================================
# Line adjustment methods
# ============================================================
def apply_line_adjustment(self, font_path: str) -> bool:
"""
Apply font-line baseline adjustment to the font.
This external tool fixes an issue with line spacing on some e-readers.
The function handles the necessary file operations (renaming and cleanup)
after the external utility has run.
"""
try:
subprocess.run(["font-line", "percent", str(self.line_percent), font_path], check=True, stdout=subprocess.DEVNULL, stderr=subprocess.PIPE)
base, ext = os.path.splitext(font_path)
linegap_file = f"{base}-linegap{self.line_percent}{ext}"
if os.path.exists(linegap_file):
os.remove(font_path)
os.rename(linegap_file, font_path)
logger.info(f" Line spacing adjusted ({self.line_percent}% baseline shift).")
return True