| •1 |
|
| 2 |
""" |
| 3 |
Simple font validation script to replace fontbakery check-universal. |
| 4 |
Validates TTF/OTF fonts without requiring fontbakery installation. |
| 5 |
""" |
| 6 |
|
| 7 |
import sys |
| 8 |
import os |
| 9 |
from fontTools.ttLib import TTFont |
| 10 |
from pathlib import Path |
| 11 |
|
| 12 |
|
| 13 |
def validate_font(font_path): |
| 14 |
""" |
| 15 |
Validate a single font file. |
| 16 |
|
| 17 |
Args: |
| 18 |
font_path (str): Path to the font file |
| 19 |
|
| 20 |
Returns: |
| 21 |
tuple: (success: bool, message: str) |
| 22 |
""" |
| 23 |
if not os.path.exists(font_path): |
| 24 |
return False, f"File not found: {font_path}" |
| 25 |
|
| 26 |
if not os.path.isfile(font_path): |
| 27 |
return False, f"Not a file: {font_path}" |
| 28 |
|
| 29 |
try: |
| 30 |
font = TTFont(font_path) |
| 31 |
|
| 32 |
|
| 33 |
required_tables = ['head', 'hhea', 'maxp', 'name', 'cmap', 'glyf' if font_path.endswith('.ttf') else 'CFF '] |
| 34 |
missing_tables = [t for t in required_tables if t not in font] |
| 35 |
|
| 36 |
if missing_tables: |
| 37 |
return False, f"Missing required tables: {', '.join(missing_tables)}" |
| 38 |
|
| 39 |
|
| 40 |
num_glyphs = font['maxp'].numGlyphs |
| 41 |
if num_glyphs == 0: |
| 42 |
return False, "No glyphs in font" |
| 43 |
|
| 44 |
|
| 45 |
if 'name' not in font: |
| 46 |
return False, "Missing name table" |
| 47 |
|
| 48 |
|
| 49 |
if 'cmap' not in font or not font['cmap'].tables: |
| 50 |
return False, "Missing or empty cmap table" |
| 51 |
|
| 52 |
|
| 53 |
file_size = os.path.getsize(font_path) / 1024 |
| 54 |
return True, f"✓ Valid font with {num_glyphs} glyphs ({file_size:.1f}KB)" |
| 55 |
|
| 56 |
except Exception as e: |
| 57 |
return False, f"Error reading font: {e}" |
| 58 |
|
| 59 |
|
| 60 |
def validate_fonts_in_directory(directory): |
| 61 |
""" |
| 62 |
Validate all TTF and OTF files in a directory and subdirectories. |
| 63 |
|
| 64 |
Args: |
| 65 |
directory (str): Path to directory to scan |
| 66 |
|
| 67 |
Returns: |
| 68 |
tuple: (success_count: int, failure_count: int, total: int) |
| 69 |
""" |
| 70 |
font_extensions = ('.ttf', '.otf') |
| 71 |
font_files = [] |
| 72 |
|
| 73 |
|
| 74 |
for root, dirs, files in os.walk(directory): |
| 75 |
for file in files: |
| 76 |
if file.lower().endswith(font_extensions): |
| 77 |
font_files.append(os.path.join(root, file)) |
| 78 |
|
| 79 |
if not font_files: |
| 80 |
print(f"No font files found in {directory}") |
| 81 |
return 0, 0, 0 |
| 82 |
|
| 83 |
success_count = 0 |
| 84 |
failure_count = 0 |
| 85 |
|
| 86 |
print(f"Validating {len(font_files)} font file(s)...\n") |
| 87 |
|
| 88 |
for font_path in sorted(font_files): |
| 89 |
rel_path = os.path.relpath(font_path) |
| 90 |
success, message = validate_font(font_path) |
| 91 |
|
| 92 |
if success: |
| 93 |
print(f" {rel_path}") |
| 94 |
print(f" {message}") |
| 95 |
success_count += 1 |
| 96 |
else: |
| 97 |
print(f" {rel_path}") |
| 98 |
print(f" ✗ {message}") |
| 99 |
failure_count += 1 |
| 100 |
|
| 101 |
return success_count, failure_count, len(font_files) |
| 102 |
|
| 103 |
|
| 104 |
def main(): |
| 105 |
if not sys.argv[1:]: |
| 106 |
print("Usage: validate_fonts.py <directory_or_file> [directory_or_file ...]") |
| 107 |
print("Validates all TTF/OTF fonts in the given directory/file.") |
| 108 |
sys.exit(1) |
| 109 |
|
| 110 |
total_success = 0 |
| 111 |
total_failure = 0 |
| 112 |
|
| 113 |
for path in sys.argv[1:]: |
| 114 |
if os.path.isdir(path): |
| 115 |
success, failure, total = validate_fonts_in_directory(path) |
| 116 |
total_success += success |
| 117 |
total_failure += failure |
| 118 |
elif os.path.isfile(path): |
| 119 |
success, message = validate_font(path) |
| 120 |
if success: |
| 121 |
print(f"✓ {path}: {message}") |
| 122 |
total_success += 1 |
| 123 |
else: |
| 124 |
print(f"✗ {path}: {message}") |
| 125 |
total_failure += 1 |
| 126 |
else: |
| 127 |
print(f"Warning: {path} not found") |
| 128 |
|
| 129 |
|
| 130 |
print(f"\n{'='*60}") |
| 131 |
print(f"Validation Summary:") |
| 132 |
print(f" Passed: {total_success}") |
| 133 |
print(f" Failed: {total_failure}") |
| 134 |
print(f" Total: {total_success + total_failure}") |
| 135 |
print(f"{'='*60}") |
| 136 |
|
| 137 |
if total_failure > 0: |
| 138 |
sys.exit(1) |
| 139 |
sys.exit(0) |
| 140 |
|
| 141 |
|
| 142 |
if __name__ == "__main__": |
| 143 |
main() |