From 6653d62480f8ca8b477bda2e4f184ca9b9f47650 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Tue, 24 Feb 2026 10:17:18 -0300 Subject: [PATCH] [test/util] Rename draw suite and tools to vector --- test/meson.build | 2 +- test/vector/README.md | 61 +++ test/vector/data/expected/basic/cff1.svg | 12 + .../data/expected/basic/cff2-var-cntr.svg | 12 + .../data/expected/basic/cff2-var-wght900.svg | 10 + test/vector/data/expected/basic/default.svg | 12 + test/vector/data/expected/basic/flat-cff1.svg | 6 + .../data/expected/basic/flat-precision-0.svg | 6 + .../data/expected/basic/flat-precision-4.svg | 6 + test/vector/data/expected/basic/flat.svg | 6 + .../data/expected/basic/glyf-var-wght900.svg | 12 + .../expected/basic/glyf-vvar-ttb-wght900.svg | 12 + test/vector/data/expected/basic/multiline.svg | 12 + test/vector/data/expected/basic/ttb.svg | 12 + .../data/expected/basic/upem-default.svg | 12 + .../expected/varc/conditional-default.svg | 8 + .../expected/varc/conditional-wght840p3.svg | 8 + test/vector/data/expected/varc/default.svg | 10 + test/vector/data/expected/varc/wght840p3.svg | 10 + test/vector/data/tests/basic.tests | 14 + test/vector/data/tests/varc.tests | 5 + test/vector/hb-svg-compare | 369 +++++++++++++++ test/vector/hb-vector-compare | 431 ++++++++++++++++++ test/vector/meson.build | 20 + test/vector/record-test.sh | 296 ++++++++++++ test/vector/run-tests.py | 254 +++++++++++ util/{hb-draw-all.c => hb-vector-all.c} | 0 util/meson.build | 2 +- 28 files changed, 1618 insertions(+), 2 deletions(-) create mode 100644 test/vector/README.md create mode 100644 test/vector/data/expected/basic/cff1.svg create mode 100644 test/vector/data/expected/basic/cff2-var-cntr.svg create mode 100644 test/vector/data/expected/basic/cff2-var-wght900.svg create mode 100644 test/vector/data/expected/basic/default.svg create mode 100644 test/vector/data/expected/basic/flat-cff1.svg create mode 100644 test/vector/data/expected/basic/flat-precision-0.svg create mode 100644 test/vector/data/expected/basic/flat-precision-4.svg create mode 100644 test/vector/data/expected/basic/flat.svg create mode 100644 test/vector/data/expected/basic/glyf-var-wght900.svg create mode 100644 test/vector/data/expected/basic/glyf-vvar-ttb-wght900.svg create mode 100644 test/vector/data/expected/basic/multiline.svg create mode 100644 test/vector/data/expected/basic/ttb.svg create mode 100644 test/vector/data/expected/basic/upem-default.svg create mode 100644 test/vector/data/expected/varc/conditional-default.svg create mode 100644 test/vector/data/expected/varc/conditional-wght840p3.svg create mode 100644 test/vector/data/expected/varc/default.svg create mode 100644 test/vector/data/expected/varc/wght840p3.svg create mode 100644 test/vector/data/tests/basic.tests create mode 100644 test/vector/data/tests/varc.tests create mode 100755 test/vector/hb-svg-compare create mode 100755 test/vector/hb-vector-compare create mode 100644 test/vector/meson.build create mode 100755 test/vector/record-test.sh create mode 100755 test/vector/run-tests.py rename util/{hb-draw-all.c => hb-vector-all.c} (100%) diff --git a/test/meson.build b/test/meson.build index b537f5a7d..90a265227 100644 --- a/test/meson.build +++ b/test/meson.build @@ -4,5 +4,5 @@ subdir('threads') if not get_option('utilities').disabled() subdir('subset') subdir('shape') -subdir('draw') +subdir('vector') endif diff --git a/test/vector/README.md b/test/vector/README.md new file mode 100644 index 000000000..32502af4d --- /dev/null +++ b/test/vector/README.md @@ -0,0 +1,61 @@ +Adding tests +============ + +`test/vector` validates `hb-vector` CLI output by comparing exact stdout with +golden files. + +Test file format (`data/tests/*.tests`) +--------------------------------------- + +Each non-empty, non-comment line has 4 semicolon-separated fields: + +``` +font-file;options;unicodes;expected-file +``` + +- `font-file`: relative to the `.tests` file (or absolute path). +- `options`: shell-split arguments passed to `hb-vector`. +- `unicodes`: Unicode sequence, using shape-style codepoint format + (for example `U+0061,U+0062,U+0063`). +- `expected-file`: path to golden output, relative to the `.tests` file. + This is also used as the TAP test name. + +Example: + +``` +../../../api/fonts/Roboto-Regular.abc.ttf;--font-size=64;U+0061,U+0062,U+0063;../expected/basic/reuse-svg.svg +``` + +Running vector tests +-------------------- + +In an in-tree build: + +```sh +meson test -C build-default -v --suite vector +``` + +Updating expected output +------------------------ + +Regenerate a golden file with the current `hb-vector` binary and replace the +file under `data/expected/`. + +Recording a new test case +------------------------- + +Use `record-test.sh`: + +```sh +./record-test.sh -o data/tests/basic.tests --test-name reuse-svg ../../build-default/util/hb-vector ../api/fonts/Roboto-Regular.abc.ttf --font-size=64 abc +``` + +It subsets the input font, verifies subset-vs-original rendering with +`hb-view`, stores the subset font under `../fonts/.ttf` (relative to the target +`.tests` file), writes expected output under +`../expected//.` by default (unless +`--expected-dir` or `--expected-file` is provided), and +appends one test line to the target `.tests` file. `hb-shape`, `hb-view`, and +`hb-subset` are resolved as sibling executables next to the provided +`hb-vector` binary. The emitted test line uses Unicode codepoint input in the +`unicodes` field. diff --git a/test/vector/data/expected/basic/cff1.svg b/test/vector/data/expected/basic/cff1.svg new file mode 100644 index 000000000..059070e59 --- /dev/null +++ b/test/vector/data/expected/basic/cff1.svg @@ -0,0 +1,12 @@ + + + + + + + + + + + + diff --git a/test/vector/data/expected/basic/cff2-var-cntr.svg b/test/vector/data/expected/basic/cff2-var-cntr.svg new file mode 100644 index 000000000..8b9fabcd3 --- /dev/null +++ b/test/vector/data/expected/basic/cff2-var-cntr.svg @@ -0,0 +1,12 @@ + + + + + + + + + + + + diff --git a/test/vector/data/expected/basic/cff2-var-wght900.svg b/test/vector/data/expected/basic/cff2-var-wght900.svg new file mode 100644 index 000000000..8d24be661 --- /dev/null +++ b/test/vector/data/expected/basic/cff2-var-wght900.svg @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/test/vector/data/expected/basic/default.svg b/test/vector/data/expected/basic/default.svg new file mode 100644 index 000000000..a80b2ce85 --- /dev/null +++ b/test/vector/data/expected/basic/default.svg @@ -0,0 +1,12 @@ + + + + + + + + + + + + diff --git a/test/vector/data/expected/basic/flat-cff1.svg b/test/vector/data/expected/basic/flat-cff1.svg new file mode 100644 index 000000000..209abd225 --- /dev/null +++ b/test/vector/data/expected/basic/flat-cff1.svg @@ -0,0 +1,6 @@ + + + + + + diff --git a/test/vector/data/expected/basic/flat-precision-0.svg b/test/vector/data/expected/basic/flat-precision-0.svg new file mode 100644 index 000000000..7c52efcc9 --- /dev/null +++ b/test/vector/data/expected/basic/flat-precision-0.svg @@ -0,0 +1,6 @@ + + + + + + diff --git a/test/vector/data/expected/basic/flat-precision-4.svg b/test/vector/data/expected/basic/flat-precision-4.svg new file mode 100644 index 000000000..66e057540 --- /dev/null +++ b/test/vector/data/expected/basic/flat-precision-4.svg @@ -0,0 +1,6 @@ + + + + + + diff --git a/test/vector/data/expected/basic/flat.svg b/test/vector/data/expected/basic/flat.svg new file mode 100644 index 000000000..66e057540 --- /dev/null +++ b/test/vector/data/expected/basic/flat.svg @@ -0,0 +1,6 @@ + + + + + + diff --git a/test/vector/data/expected/basic/glyf-var-wght900.svg b/test/vector/data/expected/basic/glyf-var-wght900.svg new file mode 100644 index 000000000..43588876d --- /dev/null +++ b/test/vector/data/expected/basic/glyf-var-wght900.svg @@ -0,0 +1,12 @@ + + + + + + + + + + + + diff --git a/test/vector/data/expected/basic/glyf-vvar-ttb-wght900.svg b/test/vector/data/expected/basic/glyf-vvar-ttb-wght900.svg new file mode 100644 index 000000000..b55c4257c --- /dev/null +++ b/test/vector/data/expected/basic/glyf-vvar-ttb-wght900.svg @@ -0,0 +1,12 @@ + + + + + + + + + + + + diff --git a/test/vector/data/expected/basic/multiline.svg b/test/vector/data/expected/basic/multiline.svg new file mode 100644 index 000000000..4e0ce6c52 --- /dev/null +++ b/test/vector/data/expected/basic/multiline.svg @@ -0,0 +1,12 @@ + + + + + + + + + + + + diff --git a/test/vector/data/expected/basic/ttb.svg b/test/vector/data/expected/basic/ttb.svg new file mode 100644 index 000000000..094bf6e4d --- /dev/null +++ b/test/vector/data/expected/basic/ttb.svg @@ -0,0 +1,12 @@ + + + + + + + + + + + + diff --git a/test/vector/data/expected/basic/upem-default.svg b/test/vector/data/expected/basic/upem-default.svg new file mode 100644 index 000000000..a80b2ce85 --- /dev/null +++ b/test/vector/data/expected/basic/upem-default.svg @@ -0,0 +1,12 @@ + + + + + + + + + + + + diff --git a/test/vector/data/expected/varc/conditional-default.svg b/test/vector/data/expected/varc/conditional-default.svg new file mode 100644 index 000000000..515a25554 --- /dev/null +++ b/test/vector/data/expected/varc/conditional-default.svg @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/test/vector/data/expected/varc/conditional-wght840p3.svg b/test/vector/data/expected/varc/conditional-wght840p3.svg new file mode 100644 index 000000000..11b23332d --- /dev/null +++ b/test/vector/data/expected/varc/conditional-wght840p3.svg @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/test/vector/data/expected/varc/default.svg b/test/vector/data/expected/varc/default.svg new file mode 100644 index 000000000..61f69a399 --- /dev/null +++ b/test/vector/data/expected/varc/default.svg @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/test/vector/data/expected/varc/wght840p3.svg b/test/vector/data/expected/varc/wght840p3.svg new file mode 100644 index 000000000..63c11f933 --- /dev/null +++ b/test/vector/data/expected/varc/wght840p3.svg @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/test/vector/data/tests/basic.tests b/test/vector/data/tests/basic.tests new file mode 100644 index 000000000..4ef4eafc1 --- /dev/null +++ b/test/vector/data/tests/basic.tests @@ -0,0 +1,14 @@ +# font-file;options;unicodes;expected-file +../../../api/fonts/Roboto-Regular.abc.ttf;;U+0061,U+0062,U+0063;../expected/basic/default.svg +../../../api/fonts/Roboto-Regular.abc.ttf;--flat;U+0061,U+0062,U+0063;../expected/basic/flat.svg +../../../api/fonts/Roboto-Regular.abc.ttf;--font-size=upem;U+0061,U+0062,U+0063;../expected/basic/upem-default.svg +../../../api/fonts/Roboto-Regular.abc.ttf;--direction=ttb;U+0061,U+0062,U+0063;../expected/basic/ttb.svg +../../../api/fonts/Roboto-Regular.abc.ttf;;U+0061,U+000A,U+0062,U+0063;../expected/basic/multiline.svg +../../../api/fonts/Roboto-Regular.abc.ttf;--flat --precision=0;U+0061,U+0062,U+0063;../expected/basic/flat-precision-0.svg +../../../api/fonts/Roboto-Regular.abc.ttf;--flat --precision=4;U+0061,U+0062,U+0063;../expected/basic/flat-precision-4.svg +../../../api/fonts/SourceSansPro-Regular.abc.otf;;U+0061,U+0062,U+0063;../expected/basic/cff1.svg +../../../api/fonts/SourceSansPro-Regular.abc.otf;--flat;U+0061,U+0062,U+0063;../expected/basic/flat-cff1.svg +../../../api/fonts/Roboto-Variable.abc.ttf;--variations=wght=900;U+0061,U+0062,U+0063;../expected/basic/glyf-var-wght900.svg +../../../api/fonts/TestCFF2VF.otf;--variations=wght=900;U+0061,U+0062,U+0063;../expected/basic/cff2-var-wght900.svg +../../../api/fonts/AdobeVFPrototype.abc.otf;--variations=wght=900,CNTR=100;U+0061,U+0062,U+0063;../expected/basic/cff2-var-cntr.svg +../../../api/fonts/SourceSerifVariable-Roman-VVAR.abc.ttf;--direction=ttb --variations=wght=900;U+0061,U+0062,U+0063;../expected/basic/glyf-vvar-ttb-wght900.svg diff --git a/test/vector/data/tests/varc.tests b/test/vector/data/tests/varc.tests new file mode 100644 index 000000000..2ec2a47e6 --- /dev/null +++ b/test/vector/data/tests/varc.tests @@ -0,0 +1,5 @@ +# font-file;options;unicodes;expected-file +../../../api/fonts/varc-ac00-ac01.ttf;;U+AC00,U+AC01;../expected/varc/default.svg +../../../api/fonts/varc-ac00-ac01.ttf;--variations=wght=840.3;U+AC00,U+AC01;../expected/varc/wght840p3.svg +../../../api/fonts/varc-ac01-conditional.ttf;;U+AC01;../expected/varc/conditional-default.svg +../../../api/fonts/varc-ac01-conditional.ttf;--variations=wght=840.3;U+AC01;../expected/varc/conditional-wght840p3.svg diff --git a/test/vector/hb-svg-compare b/test/vector/hb-svg-compare new file mode 100755 index 000000000..13ba90d72 --- /dev/null +++ b/test/vector/hb-svg-compare @@ -0,0 +1,369 @@ +#!/usr/bin/env python3 + +import xml.etree.ElementTree as ET +import re +import math + +def tokenize_path_data(d_attribute): + """ + Splits an SVG path 'd' attribute string into a list of tokens: + - Single-letter commands (M, m, L, l, C, c, etc.) + - Float values (including possible scientific notation) + Returns a list of strings/float, e.g. ['M', 10.0, 20.0, 'L', 30.5, 40.7, 'Z']. + """ + # Regex explanation: + # ([MmZzLlHhVvCcSsQqTtAa]) => one of the valid path command letters + # | => OR + # ([+-]?\d+(\.\d+)?([eE][+-]?\d+)?) => a number that may include optional +/-, + # decimals, and scientific notation + token_pattern = re.compile(r'([MmZzLlHhVvCcSsQqTtAa])|([+-]?\d+(\.\d+)?([eE][+-]?\d+)?)') + raw_tokens = token_pattern.findall(d_attribute) + + # raw_tokens is a list of tuples from the capturing groups. We only need one + # of those fields from each match. We'll filter out empty strings and parse floats. + tokens = [] + for match in raw_tokens: + # match is a tuple: (command_letter, number_string, ..., ...) + command_letter = match[0] + number_string = match[1] + if command_letter: + # It's a path command like 'M', 'L', etc. + tokens.append(command_letter) + else: + # It's a numeric value + tokens.append(float(number_string)) + + return tokens + +def extract_paths_from_svg(svg_data): + """ + Parses an SVG file and extracts the 'd' attribute from + all elements in order. Returns a list of token lists. + """ + root = ET.fromstring(svg_data) + + # Inkscape / Illustrator SVGs often have a default namespace. + # You may need to adjust the namespace if the tags are qualified. + # For example: + # namespace = {'svg': 'http://www.w3.org/2000/svg'} + # for path_elem in root.findall('.//svg:path', namespace): + # ... + # If your SVGs do not use a default namespace, the below should work. + + all_token_lists = [] + for path_elem in root.findall('.//{http://www.w3.org/2000/svg}path'): + d_attribute = path_elem.get('d') + if d_attribute: + tokens = tokenize_path_data(d_attribute) + all_token_lists.append(tokens) + + # If your SVG has no default namespace or if you removed it, you might do: + # for path_elem in root.findall('.//path'): + # d_attribute = path_elem.get('d') + # ... + # + # Adjust as appropriate depending on your actual SVG structure/namespaces. + + return all_token_lists + +def normalize_tokens(tokens): + """ + Normalize path tokens for structural comparison. + Currently this drops degenerate linear segments: + - L/l where endpoint equals current point + - H/h where x stays unchanged + - V/v where y stays unchanged + """ + out = [] + i = 0 + cmd = None + cx = 0.0 + cy = 0.0 + sx = 0.0 + sy = 0.0 + have_current = False + + def gather_numbers(start): + j = start + vals = [] + while j < len(tokens) and not isinstance(tokens[j], str): + vals.append(tokens[j]) + j += 1 + return vals, j + + while i < len(tokens): + token = tokens[i] + if isinstance(token, str): + cmd = token + i += 1 + elif cmd is None: + # Malformed path: no command to apply. + return tokens + + if cmd in ('Z', 'z'): + out.append(cmd) + if have_current: + cx, cy = sx, sy + continue + + values, i = gather_numbers(i) + + if cmd in ('M', 'm'): + if len(values) < 2: + return tokens + + # First pair is moveto and cannot be dropped. + out.extend([cmd, values[0], values[1]]) + if cmd == 'M': + cx, cy = values[0], values[1] + else: + if have_current: + cx += values[0] + cy += values[1] + else: + cx, cy = values[0], values[1] + sx, sy = cx, cy + have_current = True + + # Remaining pairs are implicit lineto. + kept = [] + k = 2 + while k + 1 < len(values): + x = values[k] + y = values[k + 1] + if cmd == 'M': + nx, ny = x, y + else: + nx, ny = cx + x, cy + y + + if nx != cx or ny != cy: + kept.extend([x, y]) + cx, cy = nx, ny + k += 2 + + out.extend(kept) + continue + + if cmd in ('L', 'l'): + kept = [] + k = 0 + while k + 1 < len(values): + x = values[k] + y = values[k + 1] + if cmd == 'L': + nx, ny = x, y + else: + nx, ny = cx + x, cy + y + + if nx != cx or ny != cy: + kept.extend([x, y]) + cx, cy = nx, ny + k += 2 + + if kept: + out.append(cmd) + out.extend(kept) + continue + + if cmd in ('H', 'h'): + kept = [] + for x in values: + nx = x if cmd == 'H' else cx + x + if nx != cx: + kept.append(x) + cx = nx + if kept: + out.append(cmd) + out.extend(kept) + continue + + if cmd in ('V', 'v'): + kept = [] + for y in values: + ny = y if cmd == 'V' else cy + y + if ny != cy: + kept.append(y) + cy = ny + if kept: + out.append(cmd) + out.extend(kept) + continue + + # For other commands, keep original parameters and just track current point. + if cmd in ('C', 'c'): + if values: + out.append(cmd) + out.extend(values) + k = 0 + while k + 5 < len(values): + if cmd == 'C': + cx, cy = values[k + 4], values[k + 5] + else: + cx += values[k + 4] + cy += values[k + 5] + k += 6 + have_current = True + continue + + if cmd in ('S', 's'): + if values: + out.append(cmd) + out.extend(values) + k = 0 + while k + 3 < len(values): + if cmd == 'S': + cx, cy = values[k + 2], values[k + 3] + else: + cx += values[k + 2] + cy += values[k + 3] + k += 4 + have_current = True + continue + + if cmd in ('Q', 'q'): + if values: + out.append(cmd) + out.extend(values) + k = 0 + while k + 3 < len(values): + if cmd == 'Q': + cx, cy = values[k + 2], values[k + 3] + else: + cx += values[k + 2] + cy += values[k + 3] + k += 4 + have_current = True + continue + + if cmd in ('T', 't'): + if values: + out.append(cmd) + out.extend(values) + k = 0 + while k + 1 < len(values): + if cmd == 'T': + cx, cy = values[k], values[k + 1] + else: + cx += values[k] + cy += values[k + 1] + k += 2 + have_current = True + continue + + if cmd in ('A', 'a'): + if values: + out.append(cmd) + out.extend(values) + k = 0 + while k + 6 < len(values): + if cmd == 'A': + cx, cy = values[k + 5], values[k + 6] + else: + cx += values[k + 5] + cy += values[k + 6] + k += 7 + have_current = True + continue + + # Unknown command: keep as-is. + out.append(cmd) + out.extend(values) + + return out + +def compare_token_lists_exact(tokens_a, tokens_b): + """ + Compares two lists of path tokens (commands and floats). + Returns True if they match (same commands in same positions, + numeric values within given tolerance), otherwise False. + """ + if len(tokens_a) != len(tokens_b): + return None # Different lengths => not a match + + max_diff = 0 + for a, b in zip(tokens_a, tokens_b): + if isinstance(a, str) and isinstance(b, str): + # Must match exactly the same command letter + if a != b: + return None + elif isinstance(a, float) and isinstance(b, float): + # Compare numeric values + diff = abs(a - b) + max_diff = max(max_diff, diff) + else: + # One is command, the other is float => mismatch + return None + + return max_diff + +def compare_token_lists(tokens_a, tokens_b): + """ + Compare path token streams with a fallback that ignores degenerate + linear segments (no-op L/l/H/h/V/v) when needed to resolve + structural mismatches. + """ + ret = compare_token_lists_exact(tokens_a, tokens_b) + if ret is not None: + return ret + + return compare_token_lists_exact(normalize_tokens(tokens_a), + normalize_tokens(tokens_b)) + +def compare_svg_files(svg_file_1, svg_file_2): + """ + Compares two SVG files to check if they have the same number of elements, + and each corresponding path has the same structure of commands. + Return max difference between respective numeric values in the paths. + """ + + svg_data_1 = open(svg_file_1).read() + svg_data_2 = open(svg_file_2).read() + + # If contents match exactly, return 0 + if svg_data_1 == svg_data_2: + return 0 + + paths1 = extract_paths_from_svg(svg_data_1) + paths2 = extract_paths_from_svg(svg_data_2) + + # Check that we have the same number of elements + if len(paths1) != len(paths2): + return None + + # Compare each path token list + max_diff = 0 + for tokens1, tokens2 in zip(paths1, paths2): + ret = compare_token_lists(tokens1, tokens2) + if ret is None: + return ret + max_diff = max(max_diff, ret) + + return max_diff + + +if __name__ == "__main__": + import sys + + if '--help' in sys.argv[1:]: + print("Usage: hb-svg-compare TOLERANCE < file_with_svg_pairs.txt") + sys.exit(1) + + tolerance = 0 + if len(sys.argv) > 1: + tolerance = float(sys.argv[1]) + + # Read all lines of two SVG file paths from stdin and compare + for line in sys.stdin: + svg1, svg2 = line.strip().split() + diff = compare_svg_files(svg1, svg2) + + if diff is None: + diff = "DIFF" + elif diff <= tolerance: + continue + else: + diff = f"{diff:g}" + + print(f"{diff}\t{svg1}\t{svg2}") + # Flush stdout to make sure output is immediate + sys.stdout.flush() diff --git a/test/vector/hb-vector-compare b/test/vector/hb-vector-compare new file mode 100755 index 000000000..e6df1e68c --- /dev/null +++ b/test/vector/hb-vector-compare @@ -0,0 +1,431 @@ +#!/bin/bash + +trap "exit" INT + +dirname=`dirname "$0"` + +# Parse options +output_dir= +font_file= +face_index=0 +hb_info=hb-info +hb_vector=hb-vector +hb_svg_compare=$dirname/hb-svg-compare +tolerance=0 +vector_precision= +precision= +font_size=upem +features= +variations= +face_loader=ot +font_funcs1=ot +font_funcs2=fontations +clear1=false +clear2=false +unicodes= +glyphs= +quiet=false +help=false +max_procs= +while test $# -gt 0; do + case "$1" in + -o|--output-dir) + shift + output_dir=$1 + shift + ;; + --font-file) + shift + font_file=$1 + shift + ;; + --face-index) + shift + face_index=$1 + shift + ;; + --hb-vector) + shift + hb_vector=$1 + shift + ;; + --hb-info) + shift + hb_info=$1 + shift + ;; + --hb-svg-compare) + shift + hb_svg_compare=$1 + shift + ;; + --tolerance) + shift + tolerance=$1 + shift + ;; + --precision) + shift + precision=$1 + shift + ;; + --max-procs) + shift + max_procs=$1 + shift + ;; + --font-size) + shift + font_size=$1 + shift + ;; + --features) + shift + features=$1 + shift + ;; + --variations) + shift + variations=$1 + shift + ;; + --unicodes) + shift + unicodes=$1 + shift + ;; + --face-loader) + shift + face_loader=$1 + shift + ;; + --font-funcs1) + shift + font_funcs1=$1 + shift + ;; + --font-funcs2) + shift + font_funcs2=$1 + shift + ;; + --clear1) + shift + clear1=true + ;; + --clear2) + shift + clear2=true + ;; + --quiet) + quiet=true + shift + ;; + --help) + help=true + shift + ;; + *) + if test "x$font_file" == x; then + font_file=$1 + shift + else + glyphs="$glyphs $1" + shift + fi + ;; + esac +done + +if $help; then + cmd=`basename "$0"` + echo "Usage: $cmd [OPTIONS] FONTFILE [GLYPH...]" + echo "Render a font with two font backends and compare the results." + echo + echo "Options:" + echo " -o, --output-dir DIR: Output in DIR" + echo " --font-file FONTFILE: Font file to render" + echo " --hb-vector HB_VECTOR: Path to hb-vector; default $hb_vector" + echo " --hb-info HB_INFO: Path to hb-info; default $hb_info" + echo " --hb-svg-compare HB_SVG_COMPARE: Path to hb-svg-compare; default $hb_svg_compare" + echo " --tolerance TOLERANCE: Tolerance for SVG comparison; default $tolerance" + echo " --precision N: hb-vector precision override; default 5" + echo " --max-procs N: Max parallel processes for render/compare; default auto" + echo " --font-size SIZE: Font size; default $font_size" + echo " --features FEATURES: Font features; default none" + echo " --variations VARIATIONS: Font variations; default none" + echo " --face-loader: Face loader; default $face_loader" + echo " --font-funcs1 font_funcs: First font-funcs; default $font_funcs1" + echo " --font-funcs2 font_funcs: Second font-funcs; default $font_funcs2" + echo " --clear1: Clear first font backend output if exists" + echo " --clear2: Clear second font backend output if exists" + echo " --unicodes CODES: Unicodes to render" + echo " --quiet: Quiet mode" + echo " --help: Print help" + exit 0 +fi + +get_num_jobs() { + n=`getconf _NPROCESSORS_ONLN 2>/dev/null || true` + test "x$n" == x && n=`nproc 2>/dev/null || true` + test "x$n" == x && n=`sysctl -n hw.ncpu 2>/dev/null || true` + case "$n" in + ''|*[!0-9]*) n=1 ;; + esac + test "$n" -lt 1 && n=1 + echo "$n" +} + +if test "x$max_procs" != x; then + echo "$max_procs" | grep -Eq '^[0-9]+$' || { + echo "Invalid max-procs '$max_procs'" >&2 + exit 2 + } + test "$max_procs" -gt 0 || { + echo "Invalid max-procs '$max_procs'" >&2 + exit 2 + } +fi + +if test "x$font_file" == x; then + echo "No font file specified." >&2 + exit 2 +fi +if ! which "$hb_vector" 2>/dev/null >/dev/null; then + echo "'$hb_vector' not found" >&2 + exit 2 +fi +if ! which "$hb_info" 2>/dev/null >/dev/null; then + echo "'$hb_info' not found" >&2 + exit 2 +fi +if ! test -f "$font_file"; then + echo "Font file '$font_file' not found" >&2 + exit 2 +fi + +# Sanity check Unicode values +if test "x$unicodes" != x; then + echo "$unicodes" | + tr ' ' '\n' | + grep -v '^U[+][0-9a-fA-F]\+$' && + { + echo "Invalid Unicode values" >&2 + exit 2 + } +fi + +$quiet || echo "Comparing '$font_file' with '$font_funcs1' and '$font_funcs2' font backends. " >&2 + +if test "x$output_dir" == x; then + output_dir=`mktemp -d` + echo "Output in '$output_dir'" >&2 +fi +mkdir -p "$output_dir" || exit 1 + +if test "x$precision" != x; then + echo "$precision" | grep -Eq '^[0-9]+$' || { + echo "Invalid precision '$precision'" >&2 + exit 2 + } + vector_precision="$precision" +else + vector_precision=5 +fi +$quiet || echo "Using precision=$vector_precision for tolerance=$tolerance." >&2 + +# Populate glyphs file +glyphs_file="$output_dir/glyphs" +> "$glyphs_file" +echo "$unicodes" | +tr ' ' '\n' | +sed 's/^U+//' | +grep . | +while read unicode; do + echo "uni$unicode" +done >> "$glyphs_file" +echo "$glyphs" | +tr ' ' '\n' | +grep . >> "$glyphs_file" +if test "x$unicodes" == x -a "x$glyphs" == x; then + $quiet || echo "No unicodes or glyphs specified. Comparing all glyphs in the font." >&2 + num_glyphs=`$hb_info --quiet --show-glyph-count "$font_file"` + $quiet || echo "Font has $num_glyphs glyphs." >&2 + seq 0 $((num_glyphs - 1)) | + sed 's/^/gid/' >> "$glyphs_file" +fi +num_glyphs=`wc -l < "$glyphs_file" | tr -d '[:space:]'` +if test "$num_glyphs" -lt 1; then + echo "No glyphs to compare." >&2 + exit 2 +fi +base_jobs=`get_num_jobs` +if test "x$max_procs" != x; then + if test "$base_jobs" -gt "$max_procs"; then + base_jobs="$max_procs" + fi +fi +compare_jobs="$base_jobs" +if test "$compare_jobs" -gt "$num_glyphs"; then + compare_jobs="$num_glyphs" +fi +$quiet || test "$base_jobs" -le 1 || echo "Using up to $base_jobs shards." >&2 + +flavor= +if test "$features" != ""; then + flavor="$flavor.features=$features" +fi +if test "$variations" != ""; then + flavor="$flavor.variations=$variations" +fi + +# Render with both font backends +if $clear1; then + funcs_prefix="$output_dir/$font_funcs1$flavor" + $quiet || echo "Clearing '$funcs_prefix'... " >&2 + rm -rf "$funcs_prefix" +fi +if $clear2; then + funcs_prefix="$output_dir/$font_funcs2$flavor" + $quiet || echo "Clearing '$funcs_prefix'... " >&2 + rm -rf "$funcs_prefix" +fi +for font_funcs in "$font_funcs1" "$font_funcs2"; do + test "x$font_funcs" == x && continue + $quiet || echo "Rendering with font backend '$font_funcs'..." >&2 + + funcs_prefix="$output_dir/$font_funcs$flavor" + mkdir -p "$funcs_prefix" + render_cmds="$output_dir/render-cmds.$font_funcs$flavor" + > "$render_cmds" + count=0 + while read glyph; do + dir="$funcs_prefix" + svg="$dir/$glyph.svg" + if test -f "$svg"; then + continue + fi + count=$((count + 1)) + if test $((count % 100)) == 0; then + $quiet || echo -n . >&2 + fi + printf '%s;%s;%s;%s;%s;%s;%s;%s;%s;%s;%s\n' \ + "--font-file=$font_file" \ + "--face-index=$face_index" \ + "--glyphs" \ + "--face-loader=$face_loader" \ + "--font-funcs=$font_funcs" \ + "--features=$features" \ + "--variations=$variations" \ + "--font-size=$font_size" \ + "--precision=$vector_precision" \ + "--output-file=$svg" \ + "$glyph" >> "$render_cmds" + done < "$glyphs_file" + + render_cmd_count=`wc -l < "$render_cmds" | tr -d '[:space:]'` + render_jobs="$base_jobs" + if test "$render_jobs" -gt "$render_cmd_count"; then + render_jobs="$render_cmd_count" + fi + if test "$render_cmd_count" -gt 0; then + render_shard_dir="$output_dir/render-shards.$font_funcs$flavor" + rm -rf "$render_shard_dir" + mkdir -p "$render_shard_dir" || exit 1 + split -d -n l/"$render_jobs" "$render_cmds" "$render_shard_dir/cmds." || exit 1 + pids= + for shard in "$render_shard_dir"/cmds.*; do + "$hb_vector" --batch < "$shard" > "$shard.out" & + pids="$pids $!" + done + for pid in $pids; do + wait "$pid" || exit 1 + done + cat "$render_shard_dir"/cmds.*.out | grep -v '^success$' || true + fi + if test "$count" -ge 100; then + $quiet || echo >&2 + fi +done + +test "x$font_funcs1" == x || test "x$font_funcs2" == x && exit 0 + +diff="$output_dir/diff$flavor" +rm -f "$diff" +> "$diff" +test "x$flavor" == x || ln -f -s "diff$flavor" "$output_dir/diff" +$quiet || echo "Comparing SVGs into '$diff'..." >&2 +funcs1_prefix="$output_dir/$font_funcs1$flavor" +funcs2_prefix="$output_dir/$font_funcs2$flavor" +pairs="$output_dir/pairs$flavor" +> "$pairs" +count=0 +while read glyph; do + count=$((count + 1)) + if test $((count % 100)) == 0; then + $quiet || echo -n . >&2 + fi + + svg1="$funcs1_prefix/$glyph.svg" + svg2="$funcs2_prefix/$glyph.svg" + + if ! test -f "$svg1" || ! test -f "$svg2"; then + echo -e "\n$glyph not rendered." >&2 + exit 1 + fi + + echo -e "$svg1\t$svg2" >> "$pairs" +done < "$glyphs_file" +if test "$count" -ge 100; then + $quiet || echo >&2 +fi + +raw_diff="$output_dir/diff.raw$flavor" +shard_dir="$output_dir/compare-shards$flavor" +rm -rf "$shard_dir" +mkdir -p "$shard_dir" || exit 1 +split -d -n l/"$compare_jobs" "$pairs" "$shard_dir/pairs." || exit 1 +pids= +for shard in "$shard_dir"/pairs.*; do + "$hb_svg_compare" "$tolerance" < "$shard" > "$shard.out" & + pids="$pids $!" +done +for pid in $pids; do + wait "$pid" || exit 1 +done +cat "$shard_dir"/pairs.*.out > "$raw_diff" || exit 1 + +# Sort diff by error +python3 - "$raw_diff" "$diff" <<'PY' || exit 1 +import math +import sys + +src, dst = sys.argv[1], sys.argv[2] +rows = [] + +with open(src, encoding='utf-8') as f: + for idx, line in enumerate(f): + line = line.rstrip('\n') + first = line.split('\t', 1)[0] + try: + value = float(first) + key = (0, value, idx) if math.isfinite(value) else (1, float('inf'), idx) + except ValueError: + key = (1, float('inf'), idx) + rows.append((key, line)) + +rows.sort(key=lambda row: row[0]) + +with open(dst, 'w', encoding='utf-8') as f: + for _, line in rows: + f.write(line + '\n') +PY + +if ! $quiet; then + while IFS= read -r line; do + echo "$line" + done < "$diff" +fi +# Count number of differences +num_diffs=`cat "$diff" | wc -l` +$quiet || echo -e "\nFound $num_diffs differences in $num_glyphs glyphs." >&2 diff --git a/test/vector/meson.build b/test/vector/meson.build new file mode 100644 index 000000000..8247aecfe --- /dev/null +++ b/test/vector/meson.build @@ -0,0 +1,20 @@ +run_test = find_program('run-tests.py') + +tests = [ + 'basic', + 'varc', +] + +foreach t : tests + fname = '@0@.tests'.format(t) + + test(t, run_test, + args: [ + hb_vector, + meson.current_source_dir() / 'data' / 'tests' / fname, + ], + workdir: meson.current_build_dir() / '..' / '..', + protocol: 'tap', + suite: 'vector', + ) +endforeach diff --git a/test/vector/record-test.sh b/test/vector/record-test.sh new file mode 100755 index 000000000..ca6d9311a --- /dev/null +++ b/test/vector/record-test.sh @@ -0,0 +1,296 @@ +#!/bin/bash + +set -euo pipefail + +usage() { + cat </dev/null 2>/dev/null; then + SHA1SUM_KIND=sha1sum +elif which shasum >/dev/null 2>/dev/null; then + SHA1SUM_KIND=shasum +elif which digest >/dev/null 2>/dev/null; then + SHA1SUM_KIND=digest +else + echo "'sha1sum' not found" >&2 + exit 2 +fi + +out=/dev/stdout +test_name= +expected_file= +expected_dir=../expected +expected_dir_explicit=false +subset_dir=../fonts + +while [ $# -gt 0 ]; do + case "$1" in + -o) + out=$2 + shift 2 + ;; + -n|--test-name) + test_name=$2 + shift 2 + ;; + -e|--expected-file) + expected_file=$2 + shift 2 + ;; + --expected-dir) + expected_dir=$2 + expected_dir_explicit=true + shift 2 + ;; + --subset-dir) + subset_dir=$2 + shift 2 + ;; + -h|--help) + usage + exit 0 + ;; + --) + shift + break + ;; + *) + break + ;; + esac +done + +if [ $# -lt 2 ]; then + usage >&2 + exit 1 +fi + +hb_vector=$1 +shift +fontfile=$1 +shift + +if [ "x${fontfile:0:1}" = "x-" ]; then + echo "Specify font file before vector options." >&2 + exit 1 +fi +if [ ! -f "$fontfile" ]; then + echo "Font file not found: '$fontfile'." >&2 + exit 1 +fi +if ! echo "$hb_vector" | grep -q 'vector'; then + echo "Specify hb-vector as first positional argument: got '$hb_vector'." >&2 + exit 1 +fi + +if [[ "$hb_vector" == */* ]]; then + hb_vector_path="$hb_vector" +else + hb_vector_path="$(command -v "$hb_vector" 2>/dev/null || true)" +fi +if [ -z "${hb_vector_path:-}" ] || [ ! -x "$hb_vector_path" ]; then + echo "hb-vector not found or not executable: '$hb_vector'." >&2 + exit 1 +fi +hb_vector="$hb_vector_path" +tool_dir="$(cd "$(dirname "$hb_vector")" && pwd)" +hb_shape="$tool_dir/hb-shape" +hb_view="$tool_dir/hb-view" +hb_subset="$tool_dir/hb-subset" +for tool in "$hb_shape" "$hb_view" "$hb_subset"; do + if [ ! -x "$tool" ]; then + echo "Required sibling tool not found or not executable: '$tool'." >&2 + exit 1 + fi +done + +if [ "x${fontfile:0:1}" = "x/" ]; then + fontfile_path="$fontfile" +else + fontfile_path="$PWD/$fontfile" +fi +fontfile="$fontfile_path" + +options=() +shape_view_options=() +have_text=false +text= +for arg in "$@"; do + if ! $have_text && [ "x${arg:0:1}" = "x-" ]; then + if echo "$arg" | grep -q ' '; then + echo "Space in argument is not supported: '$arg'." >&2 + exit 1 + fi + options+=("$arg") + if ! is_vector_only_option "$arg"; then + shape_view_options+=("$arg") + fi + continue + fi + + if $have_text; then + echo "Too many arguments found. Use '=' notation for options, eg --font-size=64." >&2 + exit 1 + fi + text=$arg + have_text=true +done + +if ! $have_text; then + text=$(cat) +fi + +if [[ "$text" == *';'* ]]; then + echo "Text cannot contain ';' in .tests format." >&2 + exit 1 +fi +if [[ "$text" == *$'\n'* ]]; then + echo "Multiline text is not supported by .tests format." >&2 + exit 1 +fi +if [[ "$test_name" == *';'* ]]; then + echo "Test name cannot contain ';'." >&2 + exit 1 +fi +if [[ "$test_name" == *$'\n'* ]]; then + echo "Test name cannot contain newlines." >&2 + exit 1 +fi +unicodes=$(/usr/bin/env python3 -c 'import sys; s=sys.argv[1]; print(",".join(f"U+{ord(c):04X}" for c in s))' "$text") + +options_str= +for arg in "${options[@]}"; do + options_str="${options_str}${options_str:+ }${arg}" +done + +ext=svg +for arg in "${options[@]}"; do + if [ "x$arg" = "x-q" ] || [ "x$arg" = "x--quiet" ]; then + ext=txt + break + fi +done + +if [ "$out" = "/dev/stdout" ] || [ "$out" = "-" ]; then + out_dir=$PWD +else + mkdir -p "$(dirname "$out")" + out_dir=$(cd "$(dirname "$out")" && pwd) +fi + +tmpdir=$(mktemp -d) + +# Collect gids from shaping result and subset. +glyph_ids=$( + printf '%s\n' "$text" | "$hb_shape" "${shape_view_options[@]}" --no-glyph-names --no-clusters --no-positions "$fontfile" | + sed 's/[][]//g; s/|/,/g' +) +cp "$fontfile" "$tmpdir/font.ttf" +"$hb_subset" \ + --glyph-names \ + --no-hinting \ + --layout-features='*' \ + --gids="$glyph_ids" \ + --text="$text" \ + --output-file="$tmpdir/font.subset.ttf" \ + "$tmpdir/font.ttf" +if ! test -s "$tmpdir/font.subset.ttf"; then + echo "Subsetter did not produce nonempty subset font in $tmpdir/font.subset.ttf." >&2 + exit 2 +fi + +# Verify visual parity via hb-view. +printf '%s\n' "$text" | "$hb_view" "${shape_view_options[@]}" "$fontfile" --output-format=png --output-file="$tmpdir/orig.png" +printf '%s\n' "$text" | "$hb_view" "${shape_view_options[@]}" "$tmpdir/font.subset.ttf" --output-format=png --output-file="$tmpdir/subset.png" +if ! cmp -s "$tmpdir/orig.png" "$tmpdir/subset.png"; then + echo "Subset font produced different rendering. Please inspect $tmpdir/orig.png and $tmpdir/subset.png." >&2 + exit 2 +fi + +if [ "x${subset_dir:0:1}" = "x/" ]; then + subset_dir_path="$subset_dir" +else + subset_dir_path="$out_dir/$subset_dir" +fi +mkdir -p "$subset_dir_path" +subset_hash=$(sha1_file "$tmpdir/font.subset.ttf") +subset_path="$subset_dir_path/$subset_hash.ttf" +if ! test -f "$subset_path"; then + mv "$tmpdir/font.subset.ttf" "$subset_path" +fi + +if [ -z "$expected_file" ]; then + if [ -z "$test_name" ]; then + echo "Specify --test-name NAME (or --expected-file PATH)." >&2 + exit 1 + fi + + if ! $expected_dir_explicit && [ "$out" != "/dev/stdout" ] && [ "$out" != "-" ]; then + tests_base="$(basename "$out")" + tests_stem="${tests_base%.tests}" + if [ -z "$tests_stem" ]; then + tests_stem="$tests_base" + fi + expected_dir="$expected_dir/$tests_stem" + fi + + expected_file="$expected_dir/$test_name.$ext" +fi +if [ "x${expected_file:0:1}" = "x/" ]; then + expected_path="$expected_file" +else + expected_path="$out_dir/$expected_file" +fi + +mkdir -p "$(dirname "$expected_path")" +"$hb_vector" "${options[@]}" "$subset_path" "$text" > "$expected_path" + +if [ "$out" = "/dev/stdout" ] || [ "$out" = "-" ]; then + font_field=$(/usr/bin/env python3 -c 'import os,sys; print(os.path.relpath(sys.argv[1], sys.argv[2]))' "$subset_path" "$PWD") + expected_field=$(/usr/bin/env python3 -c 'import os,sys; print(os.path.relpath(sys.argv[1], sys.argv[2]))' "$expected_path" "$PWD") + out_file=/dev/stdout +else + font_field=$(/usr/bin/env python3 -c 'import os,sys; print(os.path.relpath(sys.argv[1], sys.argv[2]))' "$subset_path" "$out_dir") + expected_field=$(/usr/bin/env python3 -c 'import os,sys; print(os.path.relpath(sys.argv[1], sys.argv[2]))' "$expected_path" "$out_dir") + out_file="$out" +fi + +echo "${font_field};${options_str};${unicodes};${expected_field}" >> "$out_file" + +rm -rf "$tmpdir" diff --git a/test/vector/run-tests.py b/test/vector/run-tests.py new file mode 100755 index 000000000..8436900d9 --- /dev/null +++ b/test/vector/run-tests.py @@ -0,0 +1,254 @@ +#!/usr/bin/env python3 + +import os +import re +import shlex +import subprocess +import sys +import tempfile +from difflib import unified_diff + +print("TAP version 14") + +args = sys.argv[1:] +if ( + not args + or not os.path.exists(args[0]) + or "hb-vector" not in args[0] +): + sys.exit("First argument does not seem to point to usable hb-vector.") +hb_vector, args = args[0], args[1:] + +if not args: + args = ["-"] + +env = os.environ.copy() +env["LC_ALL"] = "C" +EXE_WRAPPER = os.environ.get("MESON_EXE_WRAPPER") + + +def resolve_path(base, path): + if os.path.isabs(path): + return path + return os.path.normpath(os.path.join(base, path)) + + +def open_vector_batch_process(): + cmd = [hb_vector, "--batch"] + if EXE_WRAPPER: + cmd = [EXE_WRAPPER] + cmd + + process = subprocess.Popen( + cmd, + stdin=subprocess.PIPE, + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT, + env=env, + text=True, + ) + return [process] + + +def close_vector_batch_process(vector_process): + process = vector_process[0] + if process is None: + return + + if process.stdin: + process.stdin.close() + if process.stdout: + process.stdout.close() + + process.wait() + vector_process[0] = None + + +vector_process = open_vector_batch_process() + + +def build_vector_command(font_path, options, unicodes, output_path): + command = [] + if options: + command.extend(shlex.split(options)) + command.extend([f"--output-file={output_path}", f"--unicodes={unicodes}", font_path]) + return command + + +def run_vector_single(command): + cmd = [hb_vector] + cmd.extend(command) + if EXE_WRAPPER: + cmd = [EXE_WRAPPER] + cmd + return subprocess.run(cmd, env=env, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) + + +def vector_cmd(command, vector_process): + process = vector_process[0] + + # (Re)start vector tool if it is dead. + if process.poll() is not None: + if process.stdin: + process.stdin.close() + if process.stdout: + process.stdout.close() + vector_process[0] = open_vector_batch_process()[0] + process = vector_process[0] + + process.stdin.write(";".join(command) + "\n") + process.stdin.flush() + + diagnostics = [] + while True: + line = process.stdout.readline() + if line == "": + return None, diagnostics + + line = line.rstrip("\n") + if line in ("success", "failure"): + return line, diagnostics + + diagnostics.append(line) + + +def run_vector(font_path, options, unicodes, output_path): + command = build_vector_command(font_path, options, unicodes, output_path) + status, diagnostics = vector_cmd(command, vector_process) + + if status == "success": + return subprocess.CompletedProcess( + args=command, + returncode=0, + stdout="", + stderr="\n".join(diagnostics), + ) + + # Fall back to one-shot execution for proper diagnostics. + result = run_vector_single(command) + if diagnostics: + stderr = "\n".join(diagnostics) + if result.stderr: + stderr += "\n" + result.stderr + result = subprocess.CompletedProcess( + args=result.args, + returncode=result.returncode, + stdout=result.stdout, + stderr=stderr, + ) + return result + + +def parse_unicodes(s): + # Match test/shape/hb_test_tools.py Unicode.parse semantics. + s = re.sub(r"0[xX]", " ", s) + s = re.sub(r"[<+\->{},;&#\\xXuUnNiI\n\t]", " ", s) + try: + return "".join(chr(int(x, 16)) for x in s.split()) + except ValueError as e: + raise ValueError(f"invalid unicode codepoint list: {s!r}") from e + + +number = 0 +fails = 0 + +try: + for filename in args: + if filename == "-": + f = sys.stdin + base = os.getcwd() + print("# Running tests from standard input") + else: + f = open(filename, encoding="utf-8") + base = os.path.dirname(os.path.abspath(filename)) + print("# Running tests in " + filename) + + for lineno, line in enumerate(f, start=1): + line = line.strip() + if not line or line.startswith("#"): + continue + + number += 1 + fields = line.split(";", 3) + if len(fields) != 4: + fails += 1 + print(f"not ok {number} - parse:{filename}:{lineno}") + print("# malformed test line, expected 4 ';'-separated fields") + continue + + font_file, options, unicodes, expected_file = fields + name = expected_file + font_path = resolve_path(base, font_file) + expected_path = resolve_path(base, expected_file) + + if not os.path.exists(font_path): + fails += 1 + print(f"not ok {number} - {name}") + print(f"# font file not found: {font_path}") + continue + + if not os.path.exists(expected_path): + fails += 1 + print(f"not ok {number} - {name}") + print(f"# expected output file not found: {expected_path}") + continue + + try: + parse_unicodes(unicodes) + except ValueError as e: + fails += 1 + print(f"not ok {number} - {name}") + print("# " + str(e)) + continue + + with tempfile.NamedTemporaryFile(prefix="hb-vector-test-", suffix=".svg", delete=False) as tmp: + output_path = tmp.name + + try: + result = run_vector(font_path, options, unicodes, output_path) + if result.returncode != 0: + fails += 1 + print(f"not ok {number} - {name}") + print(f"# hb-vector exited with code {result.returncode}") + stderr = result.stderr.strip() + if stderr: + for l in stderr.splitlines(): + print("# " + l) + continue + + if not os.path.exists(output_path): + fails += 1 + print(f"not ok {number} - {name}") + print("# hb-vector did not create output file") + continue + + with open(expected_path, encoding="utf-8") as fp: + expected = fp.read() + with open(output_path, encoding="utf-8") as fp: + actual = fp.read() + + if actual == expected: + print(f"ok {number} - {name}") + continue + + fails += 1 + print(f"not ok {number} - {name}") + diff = unified_diff( + expected.splitlines(keepends=True), + actual.splitlines(keepends=True), + fromfile=expected_path, + tofile="actual", + ) + for diff_line in diff: + print("# " + diff_line.rstrip("\n")) + finally: + if os.path.exists(output_path): + os.unlink(output_path) + + if f is not sys.stdin: + f.close() +finally: + close_vector_batch_process(vector_process) + +print(f"1..{number}") + +if fails: + sys.exit(1) diff --git a/util/hb-draw-all.c b/util/hb-vector-all.c similarity index 100% rename from util/hb-draw-all.c rename to util/hb-vector-all.c diff --git a/util/meson.build b/util/meson.build index c37c06d95..67b1fa625 100644 --- a/util/meson.build +++ b/util/meson.build @@ -111,7 +111,7 @@ if conf.get('HAVE_GLIB', 0) == 1 ) endif - hb_draw_all = executable('hb-draw-all', ['hb-draw-all.c'], + hb_vector_all = executable('hb-vector-all', ['hb-vector-all.c'], cpp_args: cpp_args, include_directories: [incconfig, incsrc], link_with: [libharfbuzz],