#!/usr/bin/env python3 """ Simple font validation script to replace fontbakery check-universal. Validates TTF/OTF fonts without requiring fontbakery installation. """ import sys import os from fontTools.ttLib import TTFont from pathlib import Path def validate_font(font_path): """ Validate a single font file. Args: font_path (str): Path to the font file Returns: tuple: (success: bool, message: str) """ if not os.path.exists(font_path): return False, f"File not found: {font_path}" if not os.path.isfile(font_path): return False, f"Not a file: {font_path}" try: font = TTFont(font_path) # Check required tables required_tables = ['head', 'hhea', 'maxp', 'name', 'cmap', 'glyf' if font_path.endswith('.ttf') else 'CFF '] missing_tables = [t for t in required_tables if t not in font] if missing_tables: return False, f"Missing required tables: {', '.join(missing_tables)}" # Check glyph count num_glyphs = font['maxp'].numGlyphs if num_glyphs == 0: return False, "No glyphs in font" # Check name table if 'name' not in font: return False, "Missing name table" # Check cmap table if 'cmap' not in font or not font['cmap'].tables: return False, "Missing or empty cmap table" # Basic checks passed file_size = os.path.getsize(font_path) / 1024 return True, f"✓ Valid font with {num_glyphs} glyphs ({file_size:.1f}KB)" except Exception as e: return False, f"Error reading font: {e}" def validate_fonts_in_directory(directory): """ Validate all TTF and OTF files in a directory and subdirectories. Args: directory (str): Path to directory to scan Returns: tuple: (success_count: int, failure_count: int, total: int) """ font_extensions = ('.ttf', '.otf') font_files = [] # Recursively find all font files for root, dirs, files in os.walk(directory): for file in files: if file.lower().endswith(font_extensions): font_files.append(os.path.join(root, file)) if not font_files: print(f"No font files found in {directory}") return 0, 0, 0 success_count = 0 failure_count = 0 print(f"Validating {len(font_files)} font file(s)...\n") for font_path in sorted(font_files): rel_path = os.path.relpath(font_path) success, message = validate_font(font_path) if success: print(f" {rel_path}") print(f" {message}") success_count += 1 else: print(f" {rel_path}") print(f" ✗ {message}") failure_count += 1 return success_count, failure_count, len(font_files) def main(): if not sys.argv[1:]: print("Usage: validate_fonts.py [directory_or_file ...]") print("Validates all TTF/OTF fonts in the given directory/file.") sys.exit(1) total_success = 0 total_failure = 0 for path in sys.argv[1:]: if os.path.isdir(path): success, failure, total = validate_fonts_in_directory(path) total_success += success total_failure += failure elif os.path.isfile(path): success, message = validate_font(path) if success: print(f"✓ {path}: {message}") total_success += 1 else: print(f"✗ {path}: {message}") total_failure += 1 else: print(f"Warning: {path} not found") # Print summary print(f"\n{'='*60}") print(f"Validation Summary:") print(f" Passed: {total_success}") print(f" Failed: {total_failure}") print(f" Total: {total_success + total_failure}") print(f"{'='*60}") if total_failure > 0: sys.exit(1) sys.exit(0) if __name__ == "__main__": main()