oxedyne/fe2o3/fe2o3_austenite/tools/gen_linebreak_fixtures.py
7.5 KiB, 1 run
created by r1870400018:65170, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | #!/usr/bin/env python3 |
| 2 | """Writes the U6a line-break corpus, tests/fixtures/eval/linebreak/*.typ. |
| 3 | |
| 4 | Each fixture is one paragraph on a page as wide as its measure, with no margin and an automatic height, so |
| 5 | the lines Typst sets are the lines a paragraph of that measure breaks into. The prose is original to this |
| 6 | corpus, or Typst's own lorem(). Rerun after editing and commit the output; the expected lines are never |
| 7 | stored, since the test asks typst for them each time. |
| 8 | """ |
| 9 | |
| 10 | import os |
| 11 | |
| 12 | HERE = os.path.dirname(os.path.abspath(__file__)) |
| 13 | OUT = os.path.join(HERE, "..", "tests", "fixtures", "eval", "linebreak") |
| 14 | |
| 15 | EN = { |
| 16 | "craft": ( |
| 17 | "Printing began as a craft of patience. A compositor set each line by hand, choosing where to " |
| 18 | "break a word and how much space to leave between the others, and the quality of a page depended " |
| 19 | "on judgements made thousands of times a day. When machines took over the setting of type, the " |
| 20 | "judgements did not disappear; they moved into rules. Some rules were simple: never end a line " |
| 21 | "with a single short word, avoid two hyphens in a row, keep the spaces even. Others weighed one " |
| 22 | "line against the next, looking ahead to find a set of breaks that made the whole paragraph " |
| 23 | "comfortable to read. The best of these methods, published in 1981, treated the paragraph as a " |
| 24 | "single problem rather than a sequence of separate choices." |
| 25 | ), |
| 26 | "config": ( |
| 27 | "Configuration files often mix several kinds of values: numbers such as 1024 or 3.75, identifiers " |
| 28 | "like max-connections, quoted strings, and paths (for example /usr/local/share). A well-behaved " |
| 29 | "parser reports the line and column of every error, explains what it expected, and suggests a fix " |
| 30 | "where one is obvious. It should never guess silently. Long, unbroken tokens -- hash digests, " |
| 31 | "base64 blobs, or addresses -- are the hardest case for any layout engine, because they offer few " |
| 32 | "places to break and tend to leave the line before them loose. Engineers who write documentation " |
| 33 | "learn to wrap such tokens in code blocks, where a horizontal scroll is acceptable, instead of " |
| 34 | "letting them stretch a paragraph." |
| 35 | ), |
| 36 | "ferry": ( |
| 37 | "The ferry left at seven, though nobody on the pier seemed to believe it. \"It's always late,\" " |
| 38 | "said the woman selling coffee, handing over a paper cup that was too hot to hold. The harbour " |
| 39 | "smelled of diesel and salt; gulls argued over a crust near the ticket office. When the horn " |
| 40 | "finally sounded, a dozen passengers who had been sheltering under the awning hurried down the " |
| 41 | "ramp, shoes clattering on the wet steel. Out beyond the breakwater the water turned from grey to " |
| 42 | "green, and the island appeared as a low, dark line that grew, over forty minutes, into cliffs, " |
| 43 | "fields, a church spire, and a row of white houses facing the sea." |
| 44 | ), |
| 45 | "long": ( |
| 46 | "Comprehensive documentation of internationalisation requirements, accessibility considerations, " |
| 47 | "and interoperability constraints distinguishes professional engineering organisations from their " |
| 48 | "less disciplined counterparts. Responsibilities are allocated unambiguously; characteristics of " |
| 49 | "the underlying infrastructure are recorded systematically; and incompatibilities between " |
| 50 | "successive implementations are identified before deployment rather than afterwards. " |
| 51 | "Nevertheless, overcomplicated specifications can themselves become counterproductive, " |
| 52 | "discouraging contributors and encouraging undocumented workarounds that undermine " |
| 53 | "maintainability." |
| 54 | ), |
| 55 | "lorem": None, |
| 56 | } |
| 57 | |
| 58 | DE = { |
| 59 | "faehre": ( |
| 60 | "Die Fähre legte um sieben Uhr ab, obwohl niemand am Anleger so recht daran glauben wollte. Der " |
| 61 | "Hafen roch nach Diesel und Salz, und die Möwen stritten sich um ein Stück Brot neben dem " |
| 62 | "Fahrkartenschalter. Als endlich das Horn ertönte, eilten ein Dutzend Fahrgäste die Rampe hinunter, " |
| 63 | "die Schuhe klapperten auf dem nassen Stahl. Hinter der Mole wurde das Wasser grün, und die Insel " |
| 64 | "erschien als niedrige, dunkle Linie, die in vierzig Minuten zu Klippen, Feldern, einem Kirchturm " |
| 65 | "und einer Reihe weißer Häuser wurde." |
| 66 | ), |
| 67 | "doku": ( |
| 68 | "Eine sorgfältige Dokumentation der Anforderungen an Barrierefreiheit, Internationalisierung und " |
| 69 | "Zusammenarbeit unterscheidet professionelle Entwicklungsabteilungen von weniger disziplinierten. " |
| 70 | "Zuständigkeiten werden eindeutig verteilt, Eigenschaften der Infrastruktur werden systematisch " |
| 71 | "erfasst, und Unverträglichkeiten zwischen aufeinanderfolgenden Versionen werden vor der " |
| 72 | "Auslieferung erkannt." |
| 73 | ), |
| 74 | } |
| 75 | |
| 76 | MEASURES = [180, 205, 230, 255, 280, 305, 330, 355, 380, 405, 430, 460] |
| 77 | SIZES = [10, 11, 12] |
| 78 | |
| 79 | |
| 80 | def body(src, text, n): |
| 81 | if text is None: |
| 82 | return "#lorem(%d)" % n |
| 83 | return text |
| 84 | |
| 85 | |
| 86 | def write(name, width, size, lang, par, text_args, content): |
| 87 | lines = ["// U6a line-break corpus, written by tools/gen_linebreak_fixtures.py.", |
| 88 | "#set page(width: %dpt, height: auto, margin: 0pt)" % width] |
| 89 | if par: |
| 90 | lines.append("#set par(%s)" % par) |
| 91 | args = ["size: %dpt" % size, 'lang: "%s"' % lang] + text_args |
| 92 | lines.append("#set text(%s)" % ", ".join(args)) |
| 93 | lines.append(content) |
| 94 | with open(os.path.join(OUT, name + ".typ"), "w") as f: |
| 95 | f.write("\n".join(lines) + "\n") |
| 96 | |
| 97 | |
| 98 | def main(): |
| 99 | os.makedirs(OUT, exist_ok=True) |
| 100 | for f in os.listdir(OUT): |
| 101 | if f.endswith(".typ"): |
| 102 | os.remove(os.path.join(OUT, f)) |
| 103 | en_keys = list(EN.keys()) |
| 104 | count = 0 |
| 105 | for i, w in enumerate(MEASURES): |
| 106 | size = SIZES[i % 3] |
| 107 | variants = [ |
| 108 | ("j", "justify: true", []), |
| 109 | ("r", "justify: false", []), |
| 110 | ("jn", "justify: true", ["hyphenate: false"]), |
| 111 | ("rh", "justify: false", ["hyphenate: true"]), |
| 112 | ] |
| 113 | for v, (tag, par, targs) in enumerate(variants): |
| 114 | key = en_keys[(i + v) % len(en_keys)] |
| 115 | write("en-%s-%d-%s" % (key, w, tag), w, size, "en", par, targs, body(key, EN[key], 90 + 7 * i)) |
| 116 | count += 1 |
| 117 | for i, w in enumerate(MEASURES[::2]): |
| 118 | size = SIZES[(i + 1) % 3] |
| 119 | for key in DE: |
| 120 | for tag, par in [("r", "justify: false"), ("jn", "justify: true")]: |
| 121 | write("de-%s-%d-%s" % (key, w, tag), w, size, "de", par, ["hyphenate: false"], DE[key]) |
| 122 | count += 1 |
| 123 | costs = [ |
| 124 | ("hyph50", "costs: (hyphenation: 50%)"), |
| 125 | ("hyph200", "costs: (hyphenation: 200%)"), |
| 126 | ("runt0", "costs: (runt: 0%)"), |
| 127 | ("runt300", "costs: (runt: 300%)"), |
| 128 | ("both", "costs: (hyphenation: 20%, runt: 400%)"), |
| 129 | ("hyph0", "costs: (hyphenation: 0%)"), |
| 130 | ] |
| 131 | for i, (tag, c) in enumerate(costs): |
| 132 | w = MEASURES[(2 * i + 1) % len(MEASURES)] |
| 133 | key = en_keys[i % len(en_keys)] |
| 134 | write("en-%s-%d-cost-%s" % (key, w, tag), w, 11, "en", "justify: true", [c], body(key, EN[key], 120)) |
| 135 | count += 1 |
| 136 | modes = [ |
| 137 | ("js", 'justify: true, linebreaks: "simple"'), |
| 138 | ("ro", 'justify: false, linebreaks: "optimized"'), |
| 139 | ] |
| 140 | for i, w in enumerate([200, 290, 390]): |
| 141 | for tag, par in modes: |
| 142 | key = en_keys[(i + 2) % len(en_keys)] |
| 143 | write("en-%s-%d-%s" % (key, w, tag), w, 11, "en", par, [], body(key, EN[key], 110)) |
| 144 | count += 1 |
| 145 | print("wrote %d fixtures to %s" % (count, os.path.normpath(OUT))) |
| 146 | |
| 147 | |
| 148 | if __name__ == "__main__": |
| 149 | main() |