| 1 |
"""Regression test for bitmap export in various formats and bitdepths. |
| 2 |
|
| 3 |
Needs: fonts/courB18.sfd (bitmap-only font) and fonts/Caliban.sfd (regular font) |
| 4 |
|
| 5 |
This test generates bitmap exports in multiple formats with different bitdepths: |
| 6 |
- Formats: BMP, PNG, XBM |
| 7 |
- Bitdepths: 1, 8 |
| 8 |
|
| 9 |
It compares generated outputs against reference files and asserts byte-for-byte |
| 10 |
identity. |
| 11 |
|
| 12 |
To (re)generate references intentionally, set: |
| 13 |
FF_UPDATE_BITMAP_REFS=1 |
| 14 |
""" |
| 15 |
|
| 16 |
import filecmp |
| 17 |
import os |
| 18 |
import shutil |
| 19 |
import sys |
| 20 |
import tempfile |
| 21 |
|
| 22 |
import fontforge |
| 23 |
|
| 24 |
|
| 25 |
def _make_deterministic_environment() -> None: |
| 26 |
os.environ["SOURCE_DATE_EPOCH"] = "1700000000" |
| 27 |
os.environ["USER"] = "fontforge-test" |
| 28 |
os.environ["TZ"] = "UTC" |
| 29 |
if hasattr(__import__("time"), "tzset"): |
| 30 |
__import__("time").tzset() |
| 31 |
|
| 32 |
|
| 33 |
def _normalized_text(path: str) -> str: |
| 34 |
with open(path, "r", encoding="utf-8", errors="replace", newline=None) as f: |
| 35 |
return f.read().replace("\r\n", "\n").replace("\r", "\n") |
| 36 |
|
| 37 |
|
| 38 |
def _generate_bitmap_outputs(font_path: str, out_dir: str, font_type_label: str) -> list[str]: |
| 39 |
"""Generate bitmap exports in various formats and bitdepths. |
| 40 |
|
| 41 |
Args: |
| 42 |
font_path: Path to the font file |
| 43 |
out_dir: Output directory for generated bitmaps |
| 44 |
font_type_label: Label to use in filename to distinguish font types (e.g., "bitmap", "regular") |
| 45 |
|
| 46 |
Returns: |
| 47 |
List of generated bitmap file paths |
| 48 |
""" |
| 49 |
font = fontforge.open(font_path) |
| 50 |
|
| 51 |
formats = ["bmp", "png", "xbm"] |
| 52 |
bitdepths = [1, 2, 4, 8] |
| 53 |
test_glyph = "A" |
| 54 |
|
| 55 |
generated = [] |
| 56 |
|
| 57 |
for fmt in formats: |
| 58 |
for bitdepth in bitdepths: |
| 59 |
|
| 60 |
filename = f"{font_type_label}_{fmt}_depth{bitdepth}.{fmt}" |
| 61 |
export_path = os.path.join(out_dir, filename) |
| 62 |
|
| 63 |
try: |
| 64 |
|
| 65 |
glyph = font[test_glyph] |
| 66 |
glyph.export( |
| 67 |
export_path, |
| 68 |
pixelsize=100, |
| 69 |
bitdepth=bitdepth |
| 70 |
) |
| 71 |
generated.append(export_path) |
| 72 |
except Exception as e: |
| 73 |
|
| 74 |
print(f"Warning: Could not export {fmt} at bitdepth {bitdepth}: {e}") |
| 75 |
continue |
| 76 |
|
| 77 |
font.close() |
| 78 |
return generated |
| 79 |
|
| 80 |
|
| 81 |
def _assert_identical_outputs(generated_files: list[str], refs_dir: str) -> None: |
| 82 |
"""Compare generated bitmap files against reference files. |
| 83 |
|
| 84 |
Args: |
| 85 |
generated_files: List of generated bitmap file paths |
| 86 |
refs_dir: Directory containing reference bitmap files |
| 87 |
|
| 88 |
Raises: |
| 89 |
ValueError: If reference files are missing or outputs don't match |
| 90 |
""" |
| 91 |
missing_refs = [] |
| 92 |
mismatches = [] |
| 93 |
|
| 94 |
for generated in generated_files: |
| 95 |
basename = os.path.basename(generated) |
| 96 |
reference = os.path.join(refs_dir, basename) |
| 97 |
|
| 98 |
if not os.path.exists(reference): |
| 99 |
missing_refs.append(reference) |
| 100 |
continue |
| 101 |
|
| 102 |
if generated.lower().endswith(".xbm"): |
| 103 |
if _normalized_text(generated) != _normalized_text(reference): |
| 104 |
mismatches.append((generated, reference)) |
| 105 |
else: |
| 106 |
if not filecmp.cmp(generated, reference, shallow=False): |
| 107 |
mismatches.append((generated, reference)) |
| 108 |
|
| 109 |
if missing_refs: |
| 110 |
raise ValueError( |
| 111 |
"Missing bitmap reference files:\n" |
| 112 |
+ "\n".join(missing_refs) |
| 113 |
+ "\n\n" |
| 114 |
+ "Generate references by setting FF_UPDATE_BITMAP_REFS=1 and rerunning test_bitmap_export.py." |
| 115 |
) |
| 116 |
|
| 117 |
if mismatches: |
| 118 |
details = [f"{gen} != {ref}" for gen, ref in mismatches] |
| 119 |
raise ValueError("Bitmap export output differs from reference files:\n" + "\n".join(details)) |
| 120 |
|
| 121 |
|
| 122 |
def main() -> None: |
| 123 |
if len(sys.argv) < 3: |
| 124 |
raise ValueError("Expected bitmap font and regular font path arguments") |
| 125 |
|
| 126 |
bitmap_font_path = sys.argv[1] |
| 127 |
regular_font_path = sys.argv[2] |
| 128 |
|
| 129 |
|
| 130 |
if not os.path.exists(bitmap_font_path): |
| 131 |
raise ValueError(f"Bitmap font not found: {bitmap_font_path}") |
| 132 |
if not os.path.exists(regular_font_path): |
| 133 |
raise ValueError(f"Regular font not found: {regular_font_path}") |
| 134 |
|
| 135 |
script_dir = os.path.dirname(os.path.abspath(__file__)) |
| 136 |
refs_dir = os.path.join(script_dir, "fonts", "bitmap_refs") |
| 137 |
update_refs = os.environ.get("FF_UPDATE_BITMAP_REFS") == "1" |
| 138 |
|
| 139 |
_make_deterministic_environment() |
| 140 |
|
| 141 |
with tempfile.TemporaryDirectory(prefix="ff-bitmap-export-test-") as temp_dir: |
| 142 |
|
| 143 |
bitmap_generated = _generate_bitmap_outputs(bitmap_font_path, temp_dir, font_type_label="court18") |
| 144 |
|
| 145 |
|
| 146 |
regular_generated = _generate_bitmap_outputs(regular_font_path, temp_dir, font_type_label="caliban") |
| 147 |
|
| 148 |
all_generated = bitmap_generated + regular_generated |
| 149 |
|
| 150 |
if update_refs: |
| 151 |
os.makedirs(refs_dir, exist_ok=True) |
| 152 |
for generated in all_generated: |
| 153 |
if os.path.exists(generated): |
| 154 |
shutil.copyfile(generated, os.path.join(refs_dir, os.path.basename(generated))) |
| 155 |
|
| 156 |
_assert_identical_outputs(all_generated, refs_dir) |
| 157 |
|
| 158 |
|
| 159 |
if __name__ == "__main__": |
| 160 |
main() |