1.0.2: converter searches no subfolders, safer filters, forms, links and watermark

A document at the root of a checkout of 68377 folders needed 56 minutes,
because the converter named the suite, the document folder and the build folder
with `//` in TEXINPUTS; it now names them without recursion and the same
document converts in 11.5 seconds. A file below the document no longer replaces
a package of TeX Live.

The security review of the pending changes found four defects, each fixed with
a test proven red against the old files and green against the new ones twice:
an HTML table read in an empty sandbox, so an iframe opens no address; form
labels written through the LaTeX writer, so they are set as text; the form
pattern scans a line once instead of quadratically; a kept build folder no
longer lays the links of an earlier drawing over a new one. The form tests
remove the PDF of an earlier run before each conversion.

Also carried: forms out of Markdown, link colours from the company, clickable
links in prose, HTML tables and SVG drawings, the watermark, and the further
changes of 1.0.1 and 1.0.2 in the CHANGELOG.

npm test: 1232 of 1232 checks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Marc WäckerlinandClaude Opus 5.5 committed 2026-11-14 12:14:55 +01:00
1 parent adc58e19d7
commit d7aac52ac7
28 files changed
+3918 -137

No files matched your search

+303
View File
@@ -0,0 +1,303 @@
#!/usr/bin/env python3
"""What a document somebody fills in has to be, measured in the finished PDF.
A form is only a form where a reader can tick a box, type an entry of any
length and save the file. Nothing of that is visible on a rendered page, so it
is read out of the PDF itself: the field dictionaries, their names, their
flags, and the rectangles they stand in.
python3 tests/forms.py
"""
import os
import re
import sys
import zlib
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import support
from support import Check
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
OUT = os.path.join(ROOT, "tests", "build", "form")
CONVERTER = os.path.join(ROOT, "bin", "md2pdf.py")
support.name_example_company()
# The two shapes the platforms already render, and nothing else: a task list
# item and a line of a label with underscores. The third item carries no
# identifier, the second arrives ticked, and the last two lines are a
# signature block.
FORM_MD = """---
title: Sign-off
---
## Changes to approve
- [ ] **S1 First condition.** What is confirmed, in one sentence.
Remarks: ________________________________
- [x] **S2 Second condition.** Delivered ticked.
Remarks: ________________________________
- [ ] An item without an identifier.
Remarks: ________________________________
## General remarks
General remarks: ________________________________
## Confirmation
Name: ________________________________
Date: ________________________________
"""
# The same document without the two shapes: a list that is no task list, and a
# line that carries underscores without a label. Neither may make a field, and
# the underscores alone stay what every platform makes of them, a rule.
PLAIN_MD = """---
title: Report
---
## Findings
- First finding of the report.
- Second finding of the report.
The line under this one is a rule and no field.
________________________________
Closing sentence of the report.
"""
# What the two flags mean, out of the PDF specification: bit 13 is the
# multi-line field, bit 24 forbids scrolling. A field that scrolls takes an
# entry longer than its box, which is what «any length» asks for.
MULTILINE = 1 << 12
NO_SCROLL = 1 << 23
def convert(check, name, markdown):
"""One document through the converter, as a reader receives it."""
os.makedirs(OUT, exist_ok=True)
path = os.path.join(OUT, name + ".md")
with open(path, "w", encoding="utf-8") as handle:
handle.write(markdown)
# `--keep-tex` so the build log stays: the measurement of the logs reads
# every log of this directory, and a conversion that throws its log away
# leaves the one an earlier build wrote to be measured instead — a log of a
# state nobody has any more. The PDF of an earlier run goes first, so a
# failed build is not measured on it.
pdf = os.path.join(OUT, name + ".pdf")
if os.path.exists(pdf):
os.remove(pdf)
result = support.run(["python3", CONVERTER, "--identity", "nordwind",
"--keep-tex", "--out-dir", OUT, path])
check.that(os.path.exists(pdf),
f"{name}.md did not build:\n{result.stdout[-1000:]}")
return pdf if os.path.exists(pdf) else None
def objects(pdf):
"""The PDF with every stream it carries unpacked, as one string of bytes."""
with open(pdf, "rb") as handle:
data = handle.read()
chunks = [data]
for match in re.finditer(rb"stream\r?\n(.*?)endstream", data, re.S):
try:
chunks.append(zlib.decompress(match.group(1)))
except zlib.error:
pass
return b"\n".join(chunks)
def names(whole):
"""Every field name, in the order the document writes them."""
found = []
for match in re.finditer(rb"/T\s*(?:<([0-9a-fA-F]+)>|\(([^)]*)\))", whole):
if match.group(1):
raw = bytes.fromhex(match.group(1).decode())
found.append(raw.decode("utf-16-be", "replace").lstrip(""))
else:
found.append(match.group(2).decode("latin-1"))
return found
def flags(whole):
"""The flag word of every field that carries one."""
return [int(value) for value in re.findall(rb"/Ff\s*(\d+)", whole)]
def rectangles(whole):
"""Every annotation rectangle: left, bottom, right, top."""
return [tuple(float(value) for value in match.groups())
for match in re.finditer(
rb"/Rect\s*\[\s*([\d.]+)\s+([\d.]+)\s+([\d.]+)\s+([\d.]+)\s*\]",
whole)]
# A chapter long enough to run over a page, with items of different length, so
# that one of them meets the foot of a page wherever the page happens to end.
SPILL_MD = ("---\ntitle: Sign-off\n---\n\n## Changes to approve\n\n"
+ "".join(
"- [ ] **S%d Condition %d.** %s\n\n Remarks: ____________\n\n"
% (number, number,
("What is confirmed here, in a sentence that carries "
"enough words to fill a line or two of the page. ")
* (1 + number % 3))
for number in range(1, 13)))
def together(check):
"""An item, its label and its field stand on one page.
A box, the text beside it and the field under it are one thing to read and
one thing to fill in. The page used to break between them: the text of an
item stood at the foot of a page and its «Remarks» label and frame began
the next one, so the reader ticks on one page and writes on the other.
"""
pdf = convert(check, "spill", SPILL_MD)
if not pdf:
return
pages = support.pages(pdf)
check.that(pages > 1,
f"the fixture of twelve items fits on {pages} page and proves "
"nothing about a page break")
seen = {}
for page in range(1, pages + 1):
for word, left, top, right, bottom in support.words(pdf, page):
if re.fullmatch(r"S\d+", word):
seen.setdefault(word, []).append((page, bottom))
elif word == "Remarks":
seen.setdefault("remarks", []).append((page, top))
check.that(len(seen) > 12,
f"the fixture carries {len(seen) - 1} identifiers of twelve")
remarks = seen.pop("remarks", [])
for name, places in sorted(seen.items()):
page, bottom = places[0]
following = [spot for spot in remarks
if spot[0] > page or (spot[0] == page and spot[1] > bottom)]
check.that(following and following[0][0] == page,
f"the item {name} stands on page {page} and its label on "
f"page {following[0][0] if following else 0}")
def fields(check):
"""Every field of the form, by name, by kind and by flag."""
pdf = convert(check, "form", FORM_MD)
if not pdf:
return
whole = objects(pdf)
found = names(whole)
expected = ["S1", "S2", "q1", "S1-remarks", "S2-remarks", "q1-remarks",
"General-remarks", "Name", "Date"]
for name in expected:
check.that(name in found, f"the form carries no field «{name}»")
check.that(len(found) == len(set(found)),
f"a field name is used twice: {sorted(found)}")
boxes = whole.count(b"/FT/Btn") + whole.count(b"/FT /Btn")
texts = whole.count(b"/FT/Tx") + whole.count(b"/FT /Tx")
check.that(boxes == 3, f"the form carries {boxes} checkboxes and not three")
check.that(texts == 6, f"the form carries {texts} text fields and not six")
# An entry of any length: no limit, and the field scrolls.
check.that(b"/MaxLen" not in whole,
"a field limits how much may be typed into it")
written = [value for value in flags(whole) if value & MULTILINE]
check.that(len(written) == texts,
f"{len(written)} of {texts} text fields take several lines")
check.that(not [value for value in flags(whole) if value & NO_SCROLL],
"a field forbids scrolling, so a long entry is cut off")
# What the reader needs to draw what somebody types.
check.that(b"/NeedAppearances true" in whole,
"the form does not ask the reader to draw what is typed")
# The ticked item arrives ticked. The on state of a checkbox carries the
# name of its appearance, and hyperref calls that one `Yes`: the box holds
# `/V/Yes` as its value and `/AS/Yes` as what is drawn, while an unticked
# one holds `/Off` in both. Read out of the three dictionaries of this
# document, where exactly one carries it.
on = len(re.findall(rb"/V\s*/Yes", whole))
check.that(on == 1,
f"{on} of the three boxes arrive ticked, and one was written "
"ticked")
# The large field at the end of a form takes what somebody writes: it is
# clearly taller than the remark of an item, which is three lines.
boxes = rectangles(whole)
heights = sorted(box[3] - box[1] for box in boxes)
check.that(heights[-1] > 3 * heights[len(heights) // 2],
f"the large field is {heights[-1]:.0f} points tall and the "
f"others {heights[len(heights) // 2]:.0f}")
# The lines of a signature block begin on one edge, whatever their labels
# are called: «Name» and «Date» are four letters and not the same width, so
# each label pushed its own frame to a different place.
lines = sorted(box[0] for box in boxes if 10 < box[3] - box[1] < 25)
check.that(lines and max(lines) - min(lines) < 0.5,
f"the lines of the signature block begin at {lines}")
# Nothing stands outside the text block, which is where the page ends for
# ink as much as for a field.
edge = 566.93
over = [box for box in rectangles(whole) if box[2] > edge + 0.2]
check.that(not over,
f"{len(over)} field(s) reach past the text block, the widest to "
f"{max((box[2] for box in over), default=0):.1f} points")
SECRET = "Zebra marker of the file the label asked for"
def labels(check):
"""The label of a field is set as the text it is, never read as LaTeX.
A label closed the brace of the command it is written into and asked for a
file with `\\input`, which put that file into the PDF, and a `%`, `&` or `#`
in a label stopped the build.
"""
os.makedirs(OUT, exist_ok=True)
secret = os.path.join(OUT, "label-secret.tex")
with open(secret, "w", encoding="utf-8") as handle:
handle.write(SECRET + "\n")
markdown = ("---\ntitle: Labels\n---\n\n## Entries\n\n"
"Notes}\\input{" + secret + "}\\iffalse: ________________\n\n"
"Cost % & # Ref_no: ________________\n\n"
"## Remarks\n\n"
"Say \\textbf{bold}: ________________\n")
pdf = convert(check, "labels", markdown)
if not pdf:
return
text = " ".join(support.text(pdf).split())
check.that(SECRET not in text, "a label read a file into the PDF")
for label in ("Notes}\\input{", "Cost % & # Ref_no", "Say \\textbf{bold}"):
check.that(label in text, f"the label «{label}» is not on the page as written")
def untouched(check):
"""A document that fills in nothing carries no form at all.
Every other document of every company goes through the same converter, and
the one thing a feature like this may not do is change them.
"""
pdf = convert(check, "plain", PLAIN_MD)
if not pdf:
return
whole = objects(pdf)
check.that(b"/AcroForm" not in whole,
"a document without a single field carries a form dictionary")
check.that(not names(whole),
f"a document without a single field carries fields: "
f"{names(whole)}")
def main():
check = Check("forms")
fields(check)
together(check)
labels(check)
untouched(check)
return check.done()
if __name__ == "__main__":
sys.exit(main())
+910
View File
@@ -7,7 +7,9 @@ the page is a property of the page, so it is measured there.
"""
import os
import re
import shutil
import sys
import zlib
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import support
@@ -34,6 +36,10 @@ OWN_VALUE = re.compile(r"^\s*(\\definecolor|\\colorlet|\\setmainfont"
# document is then built without what it does. Measured in a sister package on
# 2026-09-21, where the head height was measured by code that never ran.
ENDINPUT = re.compile(r"^\\endinput\s*$", re.MULTILINE)
# An option that stores its value in a macro of the template: the declaration,
# the option and at least one line that reads it are three mentions.
OPTION = re.compile(r"\\DeclareOptionX\{([a-z]+)\}\{\\(?:renewcommand\{|def)"
r"(\\business@[a-z]+)")
DOCUMENT = r"""\documentclass[a4paper,10pt]{article}
\usepackage{nordwind}
@@ -263,6 +269,18 @@ def source_checks(check):
used -= {"identity", "suite", "brief", "beamer"}
missing = sorted(used - declared)
check.that(not missing, f"roles read but never declared: {missing}")
# Every option of a template stores a value that a line of the template
# reads. An option that only stores takes what a company writes, raises no
# error and changes nothing: `titlelogoheight` did that from the day the
# logo left the title block until 2026-10-05.
for name in TEMPLATES:
with open(os.path.join(support.SUITE, name), encoding="utf-8") as handle:
text = handle.read()
for option, macro in OPTION.findall(text):
uses = len(re.findall(re.escape(macro) + r"(?![@a-zA-Z])", text))
check.that(uses > 2,
f"{name}: the option `{option}` stores `{macro}` and no "
"line of the template reads it")
def page_checks(check):
@@ -512,15 +530,907 @@ def tagline_colour(check):
"a colour named for the tagline changed the colour of the name")
BODY_DOCUMENT = r"""\documentclass[a4paper,10pt]{article}
\usepackage{nordwind}
%s
\begin{document}
Consetetur sadipscing elitr sed diam nonumy eirmod tempor invidunt.
\end{document}
"""
BODY_ROLE = BODY_DOCUMENT % r"\businesscolours{text=black!55}"
BODY_DEFAULT = BODY_DOCUMENT % ""
def body_colour(check):
"""The body text is set in the text role of the company.
LaTeX writes in its own pure black until it is told otherwise, and a brand
whose dark is not that black then carries a colour nobody chose on every
page of running text. Measured at the darkest pixel of each word, which is
the core of a stem and therefore the colour the type is set in: a company
that sets its text role to a 55 percent grey gets that grey, and one that
leaves it at black gets black, which is the control that proves the
measurement sees the colour at all.
"""
tone = {}
for name, fixture in (("role", BODY_ROLE), ("default", BODY_DEFAULT)):
source = support.write("bodycolour%s.tex" % name, fixture)
pdf, _ = build(source, EXAMPLES)
page = Page(pdf, 1)
found = {word[0]: word for word in support.words(pdf, 1)}
check.that("Consetetur" in found and "invidunt." in found,
f"the body text of the {name} fixture is not on the page")
if "Consetetur" not in found or "invidunt." not in found:
return
tone[name] = max(darkest(page, found["Consetetur"]),
darkest(page, found["invidunt."]))
check.that(tone["default"] < 40,
f"black body text comes out at {tone['default']}, so the "
"measurement does not see the colour of the type")
check.that(tone["role"] > 80,
f"a text role of 55 percent grey comes out at {tone['role']}: "
"the body text is not set in the text role of the company")
SVG_COMPANY = r"""\ProvidesPackage{svgcompany}[2026/10/05 A company with an SVG logo]
\RequirePackage{business-identity}
\setmainfont{Latin Modern Roman}
\businessfonts{default = rm, smallcaps = false}
\businessidentity{name = Svgcompany, lockup = banner, logo = svgmark}
\DeclareOption*{\PassOptionsToPackage{\CurrentOption}{business-suite}}
\ProcessOptions\relax
\RequirePackage{business-suite}
\endinput
"""
SVG_DOCUMENT = r"""\documentclass[a4paper,10pt]{article}
\usepackage[nologo]{svgcompany}
\begin{document}
\pagestyle{empty}
\noindent\logo[height=2cm]
\end{document}
"""
SVG_MARK = ('<svg xmlns="http://www.w3.org/2000/svg" width="40" height="20">'
'<rect width="40" height="20" fill="#202020"/></svg>\n')
LOGO = os.path.join(support.ROOT, "bin", "logo.py")
def logo_from_svg(check):
"""A logo that exists as an SVG is a logo the company does not convert by hand.
No TeX engine reads an SVG, so `bin/logo.py` puts a PDF of the same name
beside it, and the identity keeps naming the logo without a format. A logo
the company has as an SVG alone stops the build with the command that
converts it: graphicx says «File not found» about a file the company can see
in its directory. Until 2026-10-05 the identity named `bin/logo.py` in a
comment, and the file did not exist.
"""
folder = os.path.join(support.BUILD, "svglogo")
shutil.rmtree(folder, ignore_errors=True)
os.makedirs(folder)
for name, text in (("svgcompany.sty", SVG_COMPANY),
("svgmark.svg", SVG_MARK),
("svglogo.tex", SVG_DOCUMENT)):
with open(os.path.join(folder, name), "w", encoding="utf-8") as handle:
handle.write(text)
check.that(support.run(["python3", LOGO, "--check", folder]).returncode == 1,
"logo.py --check calls an SVG without its PDF current")
# The build has to stop here, and `build` reports a stop as an exception;
# what it said stands in the log of the run either way.
try:
build(os.path.join(folder, "svglogo.tex"), folder)
except AssertionError:
pass
said = ""
log = os.path.join(support.BUILD, "svglogo", "svglogo.log")
if os.path.exists(log):
with open(log, encoding="utf-8", errors="replace") as handle:
said = handle.read()
check.that("exists as an SVG only" in said,
"a logo that exists as an SVG alone does not say how to convert "
"it")
converted = support.run(["python3", LOGO, folder])
check.that(converted.returncode == 0
and os.path.exists(os.path.join(folder, "svgmark.pdf")),
f"logo.py did not convert the SVG:\n{converted.stdout[-800:]}")
check.that(support.run(["python3", LOGO, "--check", folder]).returncode == 0,
"logo.py --check calls a freshly converted PDF out of date")
try:
pdf, _ = build(os.path.join(folder, "svglogo.tex"), folder)
except AssertionError:
pdf = ""
check.that(os.path.exists(pdf) and Page(pdf, 1).ink_rows(),
"the converted logo did not reach the page")
# Five addresses of the lengths a company really writes, each at three heights,
# one lockup to a page so that nothing else carries ink there.
ADDRESSES = ("https://nordwind.example",
"Nordwind AG, Hafenstrasse 4, 8000 Zurich",
"nordwind.ch",
"post@nordwind.example",
"Nordwind AG -- Lorem ipsum dolor sit amet consetetur sadipscing")
HEIGHTS = ("1cm", "2cm", "4cm")
ADDRESS_DOCUMENT = r"""\documentclass[a4paper,10pt]{article}
\usepackage[nologo]{nordwind}
\begin{document}
\pagestyle{empty}
""" + "\n\\newpage\n".join(
"\\businessidentity{address={%s}}\\noindent\\logo[address, nolink, "
"height=%s]" % (address, height)
for address in ADDRESSES for height in HEIGHTS) + r"""
\end{document}
"""
def address_on_logo(check):
"""The address line is as wide as the logo above it, at every height and for
every address, and the whole block scales with the height it is asked for.
The size of the line is derived from the two widths, never a factor of the
height: a factor fits one address and leaves the next one short of the logo
or over its edge. Five addresses, from one letter to a sentence, each at
1cm, 2cm and 4cm. The left edge of the logo is the ink of the icon, which
fills its own box, and the right edge is the box of the name.
And the block at twice the height is twice as large: its height from the top
of the icon to the foot of the address line, the width of the logo and the
width of the address line. A length read as a number drops its unit, and
that shows at one height and not at another. These are two of the measurements the package the suite
came out of carries and the suite did not, named as the condition for moving
that package onto the suite.
"""
source = support.write("addresses.tex", ADDRESS_DOCUMENT)
pdf, _ = build(source, EXAMPLES)
measured = {}
for index, (address, height) in enumerate(
(address, height) for address in ADDRESSES for height in HEIGHTS):
number = index + 1
found = support.words(pdf, number)
names = [word for word in found if word[0] == "Nordwind"]
check.that(names, f"page {number}: the lockup did not reach the page")
if not names:
continue
name = min(names, key=lambda word: word[2])
line = [word for word in found if word[2] > name[4] - 0.5]
check.that(line, f"page {number}: the address line is missing")
if not line:
continue
page = Page(pdf, number)
block = page.box()
# The left edge of the LOGO is read above the address line: the ink of
# the whole page begins with the address wherever the address reaches
# past the icon, and the comparison would then measure the address
# against itself. Measured on 2026-10-05, when a space in front of every
# icon put the address a word space left of it and this check stayed
# green.
top = min(word[2] for word in line)
logo_left = page.box(0, int(top * page.dpi / 72.0))[0]
logo_right = name[3]
left = min(word[1] for word in line)
right = max(word[3] for word in line)
check.that(abs(left - logo_left) < 1.5 and abs(right - logo_right) < 1.5,
f"«{address}» at {height}: the address runs from {left:.1f} to "
f"{right:.1f} and the logo from {logo_left:.1f} to "
f"{logo_right:.1f}")
measured[(address, height)] = (block[3] - block[1],
logo_right - logo_left, right - left)
for address in ADDRESSES:
for small, large in (("1cm", "2cm"), ("2cm", "4cm")):
one, two = measured.get((address, small)), measured.get((address,
large))
if not one or not two:
continue
for what, a, b in zip(("block", "logo", "address line"), one, two):
ratio = b / a if a else 0
check.that(1.94 < ratio < 2.06,
f"«{address}»: the {what} at {large} is {ratio:.3f} "
f"times the one at {small}, not twice")
MARKS_DOCUMENT = r"""\documentclass[a4paper,10pt]{article}
\usepackage[nologo]{nordwind}
\begin{document}
\pagestyle{empty}
\noindent{\fontsize{60}{72}\selectfont\success}
\newpage
\noindent{\fontsize{60}{72}\selectfont\fail}
\end{document}
"""
def marks_fill(check):
"""Each of the two marks is a white sign that fills a share of its box.
The page shows the box in any case; what it shows inside is the question. A
sign the symbol face does not carry leaves the box empty, a sign of another
face fills it in another share, and neither says a word in the log. The share
of white inside the box of each mark is read at 60 points: measured on
2026-10-05 at 0.12 for the check and 0.21 for the cross with DejaVu Sans, so
a window of 0.08 to 0.30 holds both and catches an empty box, which is no
white at all, and a box that lost its colour, which is nearly all white.
This is the last of the measurements the package the suite came out of
carried and the suite did not.
"""
source = support.write("marksfill.tex", MARKS_DOCUMENT)
pdf, _ = build(source, EXAMPLES)
for number, name in ((1, "success"), (2, "fail")):
page = Page(pdf, number)
box = page.box()
check.that(box, f"the {name} mark did not reach the page")
if not box:
continue
scale = page.dpi / 72.0
left, top = int(box[0] * scale), int(box[1] * scale)
right, bottom = int(box[2] * scale), int(box[3] * scale)
inside = [page.pixels[y * page.width + x]
for y in range(top, bottom + 1)
for x in range(left, right + 1)]
white = sum(1 for value in inside if value > 230) / max(1, len(inside))
check.that(0.08 < white < 0.30,
f"the white sign fills {white:.2f} of the box of the {name} "
"mark")
DECK = r"""\documentclass{beamer}
\usetheme{nordwind}
%s
\begin{document}
\begin{frame}{Lorem Ipsum}Consetetur sadipscing elitr.\end{frame}
\begin{frame}{Dolor Sit}At vero eos et accusam.\end{frame}
\end{document}
"""
DECK_PLAIN = DECK % ""
DECK_GROUND = DECK % (r"\businesscolours{canvas-top=red, canvas-bottom=blue,"
r" sidebar-top=green, sidebar-bottom=yellow}")
def deck_pixels(pdf, page):
"""The colour of one rendered slide, as a function of x and y in points."""
stem = os.path.join(support.BUILD, "deck")
result = support.run(["pdftoppm", "-r", "36", "-f", str(page), "-l",
str(page), "-singlefile", pdf, stem])
with open(stem + ".ppm", "rb") as handle:
data = handle.read()
fields, offset = [], 0
while len(fields) < 4:
end = data.index(b"\n", offset)
line = data[offset:end]
offset = end + 1
if not line.startswith(b"#"):
fields += line.split()
width = int(fields[1])
pixels = data[offset:]
def at(x, y):
index = 3 * (int(y / 2) * width + int(x / 2))
return tuple(pixels[index:index + 3])
return at, result.stdout
def deck_ground(check):
"""The ground of a slide and its sidebar take the roles of the company.
A company that names no colour for them keeps the deck of the family: white
ground, the sidebar in the brand colour. One that names two colours gets a
ground from the first at the top to the second at the bottom. The ground is
drawn as bands and not as a shading of beamer: xdvipdfmx writes such a
shading into the first page alone, and a reader of the PDF reported «XObject
'Fm2' is unknown» on every later slide. So the reader is asked as well.
"""
for name, fixture in (("plain", DECK_PLAIN), ("ground", DECK_GROUND)):
source = support.write("deck%s.tex" % name, fixture)
pdf, _ = build(source, EXAMPLES)
at, said = deck_pixels(pdf, 2)
check.that("Error" not in said,
f"the {name} deck makes the PDF reader report: {said[:200]}")
# A point of the ground at the right edge, top and bottom, away from
# the text; and a point of the sidebar at the left edge.
top, bottom = at(350, 60), at(350, 255)
if name == "plain":
check.that(min(top) > 240 and min(bottom) > 240,
f"a company without a ground of its own got {top} at "
f"the top and {bottom} at the bottom instead of white")
else:
# The two points lie a fifth of the way in from either end, so each
# carries some of the other colour; what they have to show is which
# of the two dominates.
check.that(top[0] > 150 and top[0] > 3 * top[2],
f"the top of the ground is {top}, not mostly the red "
"named for it")
check.that(bottom[2] > 150 and bottom[2] > 3 * bottom[0],
f"the bottom of the ground is {bottom}, not mostly the "
"blue named for it")
side_top, side_bottom = at(10, 60), at(10, 255)
check.that(side_top[1] > 100 and side_top[1] > 2 * side_top[0],
f"the top of the sidebar is {side_top}, not mostly the "
"green named for it")
check.that(side_bottom[0] > 150 and side_bottom[1] > 150
and side_bottom[2] < 100,
f"the bottom of the sidebar is {side_bottom}, not mostly "
"the yellow named for it")
NAMED_DOCUMENT = r"""\documentclass[a4paper,10pt]{article}
\usepackage{nordwind}
\begin{document}
\section{Lorem Ipsum}\label{lorem-ipsum}
Consetetur sadipscing elitr.
\end{document}
"""
def destinations(pdf):
"""The names of the places of a PDF that a link from outside can reach.
The names stand in the destination tree, partly in compressed object
streams, so the file is decompressed before it is read.
"""
with open(pdf, "rb") as handle:
data = handle.read()
chunks = [data]
for match in re.finditer(rb"stream\r?\n(.*?)endstream", data, re.S):
try:
chunks.append(zlib.decompress(match.group(1)))
except zlib.error:
continue
found = set()
for chunk in chunks:
for hit in re.finditer(rb"\(([A-Za-z0-9._-]+)\)\s*(?:\[|<<|\d+ \d+ R)",
chunk):
found.add(hit.group(1).decode("latin-1"))
return found
def named_destinations(check):
"""A section is reachable from another document by the name of its label.
A link from one document into a section of another carries the name of the
label there, and hyperref numbers its places unless it loads with
`destlabel`. The template passed that option AFTER the identity had loaded
hyperref, so it reached no company: measured on 2026-10-05, when a section
of a converted report carried no named place and a link into it opened the
report on page one.
"""
source = support.write("named.tex", NAMED_DOCUMENT)
pdf, _ = build(source, EXAMPLES)
places = destinations(pdf)
check.that("lorem-ipsum" in places,
"the section is not reachable by the name of its label; the "
"places are: " + ", ".join(sorted(places)[:12]))
RULE_DOCUMENT = r"""\documentclass[a4paper,10pt]{article}
\usepackage{nordwind}
%s
\begin{document}
\begin{tabular}{ll}
\toprule
Consetetur & sadipscing \\
\midrule
Elitr & sed \\ \businessrowrule
Diam & nonumy \\
\bottomrule
\end{tabular}
\end{document}
"""
RULE_ROLE = RULE_DOCUMENT % r"\businesscolours{text=black!55}"
RULE_DEFAULT = RULE_DOCUMENT % ""
def rule_tone(page):
"""The darkest pixel of the rules of a table, 0 to 255.
A rule is the one thing on the page that is dark over a long unbroken run of
a pixel row: a line of type has white between its letters. The darkest pixel
of those rows is the core of the heaviest rule, and that is its colour.
"""
darkest_found = 255
for y in range(page.height):
row = page.pixels[y * page.width:(y + 1) * page.width]
run = longest = 0
for value in row:
run = run + 1 if value < 200 else 0
longest = max(longest, run)
if longest > 100:
darkest_found = min(darkest_found, min(row))
return darkest_found
def rule_colour(check):
"""The rules of a table are drawn in the text role of the company.
colortbl draws in its own pure black until it is told otherwise, and the row
rule of this package reset the rules to exactly that black after every row:
a company whose text is a dark of its own then carried pure black in every
table. Measured on 2026-10-05 in a company whose palette ink is a black of
its own. A text role of 55 percent grey has to reach the rules, and black has
to stay black, which is the control that proves the measurement sees them.
"""
tone = {}
for name, fixture in (("role", RULE_ROLE), ("default", RULE_DEFAULT)):
source = support.write("rulecolour%s.tex" % name, fixture)
pdf, _ = build(source, EXAMPLES)
tone[name] = rule_tone(Page(pdf, 1))
check.that(tone["default"] < 40,
f"black rules come out at {tone['default']}, so the measurement "
"does not see the rules of the table")
check.that(tone["role"] > 80,
f"under a text role of 55 percent grey the rules come out at "
f"{tone['role']}: they are not drawn in the text role")
# A page of the furniture and nothing else: the title block, the running head,
# both heading levels, the two marks, a table with its rules, the foot. No
# picture, because a photograph carries the colours of whatever it shows and
# says nothing about the template that places it.
PALETTE_DOCUMENT = r"""\documentclass[a4paper,10pt]{article}
\usepackage{nordwind}
\title{Lorem Ipsum}
\author{Erika Muster}
\date{21 September 2026}
\begin{document}
\maketitle
\section{Consetetur}
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore. Done: \success{} Open: \fail{}
\subsection{Dolor Sit}
Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
\begin{tabular}{lr}
\toprule
\businesstablehead{Lorem} & \businesstablehead{Ipsum} \\
\midrule
Consetetur sadipscing & 41 \\ \businessrowrule
Sed diam nonumy & 17 \\
\bottomrule
\end{tabular}
\end{document}
"""
def role_names():
"""Every colour role the package declares, read off the package."""
with open(os.path.join(support.SUITE, "business-identity.sty"),
encoding="utf-8") as handle:
text = handle.read()
return re.findall(r"^\\business@role\{([^}]+)\}", text, re.MULTILINE)
def role_inks(check, names):
"""What each role really comes out as, asked of xcolor in a probe.
The company assigns a role to a palette entry or to a mixture of two, and
only xcolor knows what that mixture is. A list of values written here would
be the palette of one company, which is the one thing this suite may not
carry.
"""
lines = "".join(
"\\convertcolorspec{named}{business-%s}{RGB}\\business@ink"
"\\typeout{ROLE %s \\business@ink}\n" % (name, name) for name in names)
source = support.write("paletteprobe.tex",
"\\documentclass[a4paper,10pt]{article}\n"
"\\usepackage{nordwind}\n\\makeatletter\n"
"\\AtBeginDocument{%%\n%s}\n\\makeatother\n"
"\\begin{document}Lorem.\\end{document}\n" % lines)
_, log = build(source, EXAMPLES)
with open(log, encoding="utf-8") as handle:
found = re.findall(r"^ROLE (\S+) ([\d.]+),([\d.]+),([\d.]+)\s*$",
handle.read(), re.MULTILINE)
inks = {name: tuple(round(float(value)) for value in triple)
for name, *triple in found}
check.that(len(inks) == len(names),
f"the probe reported {len(inks)} of the {len(names)} roles the "
"package declares")
return inks
def between(colour, first, second):
"""How far a pixel lies from the line between two colours of the palette.
A renderer draws the edge of a letter in the colours between the ink and
what lies under it, so every pixel of a page sits on the line between two
colours the page uses. The distance to that line says whether the pixel
belongs there, and a colour past both ends — a black darker than the one the
company declared — lies off it and is found.
"""
along = sum((one - other) * (one - other)
for one, other in zip(second, first))
if not along:
return max(abs(one - other) for one, other in zip(colour, first))
share = sum((end - start) * (pixel - start)
for end, start, pixel in zip(second, first, colour)) / along
share = min(1.0, max(0.0, share))
return max(abs(start + share * (end - start) - pixel)
for end, start, pixel in zip(second, first, colour))
# The same page twice: a company that says what its links look like, and one
# that says nothing. The word of the link stands alone in its paragraph, so its
# pixels are easy to find, and the fixture asks xcolor what the role really is,
# because a value written here would be the palette of one company.
LINKSTYLE = r"""\documentclass[a4paper,10pt]{article}
\usepackage{nordwind}
%s\makeatletter
\AtBeginDocument{\convertcolorspec{named}{business-link}{RGB}\business@ink
\typeout{ROLE link \business@ink}}
\makeatother
\begin{document}
\pagestyle{empty}
\noindent\href{https://nordwind.example}{Verweis}
\end{document}
"""
LINKSTYLE_SET = LINKSTYLE % ("\\businesscolours{link = nordwind-deep}\n"
"\\businesslinks{colour = link, "
"underline = true}\n")
LINKSTYLE_PLAIN = LINKSTYLE % ""
# The same setting with a table of contents: every entry of it is an internal
# link, so it takes the colour of the role, and a line under each of them would
# turn the contents page into a grid.
LINKSTYLE_CONTENTS = r"""\documentclass[a4paper,10pt]{article}
\usepackage{nordwind}
\businesscolours{link = nordwind-deep}
\businesslinks{colour = link, underline = true}
\begin{document}
\tableofcontents
\newpage
\section{Erster Abschnitt}
\noindent\href{https://nordwind.example}{Verweis}
\end{document}
"""
def painted(pdf, word=None):
"""Colours around one word, or on the page, with the rows they cover."""
stem = os.path.join(support.BUILD, "linkpage")
support.run(["pdftoppm", "-r", "150", "-f", "1", "-l", "1", "-singlefile",
pdf, stem])
with open(stem + ".ppm", "rb") as handle:
data = handle.read()
fields, offset = [], 0
while len(fields) < 4:
end = data.index(b"\n", offset)
line = data[offset:end]
offset = end + 1
if not line.startswith(b"#"):
fields += line.split()
width = int(fields[1])
pixels = data[offset:]
top, bottom = 0, len(pixels) // 3 // width
if word:
found = [item for item in support.words(pdf, 1) if item[0] == word]
if found:
scale = 150 / 72.0
top = max(0, int(found[0][2] * scale) - 3)
bottom = int(found[0][4] * scale) + 4
rows = {}
for index in range(0, len(pixels) - 2, 3):
row = index // 3 // width
if row < top or row > bottom:
continue
colour = tuple(pixels[index:index + 3])
if colour == (255, 255, 255):
continue
rows.setdefault(colour, set()).add(row)
return rows
def asked_role(log, name):
"""What the build reported for a role, out of its own log."""
with open(log, encoding="utf-8", errors="replace") as handle:
found = re.search(r"^ROLE %s ([\d.]+),([\d.]+),([\d.]+)\s*$" % name,
handle.read(), re.MULTILINE)
return tuple(round(float(value)) for value in found.groups()) if found \
else None
def link_style(check):
"""A link looks the way the company says, and nothing says it by default.
A reader of a PDF and a reader of the website of the same company see one
design, so the colour of a link is a role of the palette and the line under
it is a setting, both taken from the stylesheet of that company. Measured on
the rendered page: the word of the link stands in the colour of the role,
and the line under it in the same colour. What a PDF cannot carry is the
state of a link — there is no hover and no visited link on paper.
Without the setting a link keeps the colour of the text and carries no
line, which is what this package did before and what a printed page wants.
"""
styled, log = build(support.write("linkstyle.tex", LINKSTYLE_SET), EXAMPLES)
wanted = asked_role(log, "link")
check.that(wanted, "the build reported no colour for the role link")
if not wanted:
return
rows = painted(styled, "Verweis")
near = {colour: places for colour, places in rows.items()
if max(abs(colour[band] - wanted[band]) for band in range(3)) < 20}
check.that(near,
f"no pixel of the page stands in the link colour {wanted}")
if not near:
return
lines = set()
for places in near.values():
lines |= places
# The word and the line under it: the word covers a band of rows and the
# line adds its own under it, at the distance of the underline.
check.that(max(lines) - min(lines) > 12,
"the link carries no line under it: its colour covers "
f"{max(lines) - min(lines)} rows of the page")
plain, plain_log = build(support.write("linkplain.tex", LINKSTYLE_PLAIN),
EXAMPLES)
quiet = painted(plain, "Verweis")
coloured = [colour for colour in quiet
if max(abs(colour[band] - wanted[band])
for band in range(3)) < 20]
check.that(not coloured,
"a document that sets nothing carries the link colour all the "
f"same: {sorted(coloured)[:3]}")
contents_without_lines(check, wanted)
heading_link(check)
def heading_link(check):
"""A link inside a section title builds, and keeps its destination.
A title is written out three times over: into the contents file, into the
running head and into the bookmark of the PDF. A replacement of `\\hyperref`
that is not robust is expanded in those places while its argument is still
unread, and the title then ends in the middle of itself. Reported from the
PLIM documents on 2026-10-23, where exactly one of nineteen documents
carried such a title and stopped building.
"""
pdf, _ = build(support.write("linkheading.tex", LINKSTYLE_HEADING),
EXAMPLES)
names = support.run(["pdfinfo", "-dests", pdf]).stdout
check.that("marke" in names,
"the destination of the link in the title is missing from the "
"built document")
# A link inside a section title, written the way pandoc writes a Markdown
# heading that carries one: the title travels into the contents, the running
# head and the bookmark, and a replacement of `\hyperref` that is not robust
# tears the title apart on the way.
LINKSTYLE_HEADING = r"""\documentclass[a4paper,10pt]{article}
\usepackage{nordwind}
\businesscolours{link = nordwind-deep}
\businesslinks{colour = link, underline = true}
\begin{document}
\tableofcontents
\newpage
\section{\texorpdfstring{Der \hyperref[marke]{Verweis} im Titel}%
{Der Verweis im Titel}}
\label{marke}
\noindent Rumpf.
\end{document}
"""
def ruled_rows(pdf, page, wanted, word):
"""Rows under a word that are a long run of one colour: a drawn line.
Only the band of the word is read. A page of this suite carries lines of its
own — the rule under the head stands in a colour of the same palette, 194
pixels long, and a measurement over the whole page reports it as an
underline.
"""
stem = os.path.join(support.BUILD, "linkcontents")
support.run(["pdftoppm", "-r", "150", "-f", str(page), "-l", str(page),
"-singlefile", pdf, stem])
with open(stem + ".ppm", "rb") as handle:
data = handle.read()
fields, offset = [], 0
while len(fields) < 4:
end = data.index(b"\n", offset)
line = data[offset:end]
offset = end + 1
if not line.startswith(b"#"):
fields += line.split()
width, height = int(fields[1]), int(fields[2])
pixels = data[offset:]
found = [item for item in support.words(pdf, page) if item[0] == word]
if not found:
return [("the word %s stands on no page %d" % (word, page), 0)], width
scale = 150 / 72.0
top = max(0, int(found[0][2] * scale) - 3)
height = min(height, int(found[0][4] * scale) + 7)
ruled = []
for row in range(top, height):
run = longest = 0
for column in range(width):
index = (row * width + column) * 3
colour = tuple(pixels[index:index + 3])
if max(abs(colour[band] - wanted[band]) for band in range(3)) < 40:
run += 1
longest = max(longest, run)
else:
run = 0
# A drawn line runs without a gap under the whole word, 40 pixels at
# 150 dpi for a word of seven letters at ten points. A row of glyphs
# never does: the horizontal stroke of a letter is a few pixels wide,
# and the leaders of a contents entry are dots with white between them.
if longest > 40:
ruled.append((row, longest))
return ruled, width
def contents_without_lines(check, wanted):
"""The line under a link belongs to a link in the text, not to a contents page.
Every entry of a table of contents is an internal link, so it takes the
colour of the role like any other. A line under each of them would draw a
grid over the page, and nobody reads a contents page that way: the line is
drawn around `\\href` and `\\hyperref`, the two commands a text writes, and
`\\contentsline` builds its entries with neither.
"""
pdf, _ = build(support.write("linkcontents.tex", LINKSTYLE_CONTENTS),
EXAMPLES)
contents, _ = ruled_rows(pdf, 1, wanted, "Erster")
check.that(not contents,
"the contents page carries a drawn line in the link colour, "
f"row and length: {contents}")
text, width = ruled_rows(pdf, 2, wanted, "Verweis")
check.that(text,
"the link in the text carries no line under it on its page")
def palette(check):
"""No ink on the page outside the colours the company declared.
LaTeX writes in its own pure black until it is told otherwise, and a company
whose text is a warm black never sees the difference by eye. The check reads
the RENDERED page and not the content stream: a stream sets a colour for
what follows, and the writer of a PDF sets pure black at the head of every
page whether anything is painted in it or not. What a reader sees is the
pixel.
Taken over from the design of a company of this family on 2026-10-06, where
it stood with that company's four colours written into the test. Here the
roles are read off the package and their values out of xcolor, so it holds
for every company and grows with every role the family adds.
"""
names = role_names()
check.that(len(names) > 10,
f"the package declares {len(names)} colour roles, which is fewer "
"than this family has")
inks = role_inks(check, names)
if not inks:
return
# The paper is no role and belongs to every page; every letter of every page
# is drawn on it, so every edge lies on a line that ends there.
colours = list(inks.values()) + [(255, 255, 255)]
source = support.write("palettedoc.tex", PALETTE_DOCUMENT)
pdf, _ = build(source, EXAMPLES)
stem = os.path.join(support.BUILD, "palettepage")
support.run(["pdftoppm", "-r", "100", "-f", "1", "-l", "1", "-singlefile",
pdf, stem])
with open(stem + ".ppm", "rb") as handle:
data = handle.read()
fields, offset = [], 0
while len(fields) < 4:
end = data.index(b"\n", offset)
line = data[offset:end]
offset = end + 1
if not line.startswith(b"#"):
fields += line.split()
pixels = data[offset:]
strange = {}
for index in range(0, len(pixels) - 2, 3):
colour = tuple(pixels[index:index + 3])
if colour in strange:
strange[colour] += 1
continue
# Eight steps of 255: a blend of two colours sits on its line by
# construction, and the window covers the rounding of the renderer and
# the corner where three colours meet in one pixel.
if min(between(colour, first, second)
for number, first in enumerate(colours)
for second in colours[number:]) > 8:
strange[colour] = 1
worst = sorted(strange.items(), key=lambda pair: -pair[1])[:3]
check.that(not strange,
"the page carries ink the company never declared: "
+ ", ".join("#%02X%02X%02X (%d pixels)" % (colour + (count,))
for colour, count in worst))
# A company that builds on another: it loads that identity and names a mark of
# its own. `nordwind` DRAWS its icon, so the drawing has to give way to the file.
DAUGHTER = r"""\documentclass[a4paper,10pt]{article}
\usepackage[nologo]{nordwind}
\businessidentity{icon = example-image}
\begin{document}
\pagestyle{empty}
\noindent\logo[height=2cm]
\end{document}
"""
def daughter_mark(check):
"""A mark named after another identity was loaded replaces the one it drew.
A file and a drawing answer the same question, and the package preferred the
drawing wherever one stood. A company that loads the identity of another and
names its own `icon` therefore carried the mark of that other company, on
every page, with nothing in any log to say so. The measurement is the shape
on the page: the drawn mark of the example company is a disc and as wide as
it is high, the test image of TeX Live is half again as wide as it is high.
"""
source = support.write("daughter.tex", DAUGHTER)
pdf, _ = build(source, EXAMPLES)
page = Page(pdf, 1)
rows = page.ink_rows()
check.that(rows, "the mark of the daughter did not reach the page")
if not rows:
return
columns = page.ink_columns(rows[0], rows[-1] + 1)
factor = 72.0 / page.dpi
high = (rows[-1] - rows[0] + 1) * factor
wide = (columns[-1] - columns[0] + 1) * factor
check.that(wide > 1.2 * high,
f"the mark is {wide:.1f} points wide and {high:.1f} high, so the "
"drawing of the identity that was loaded first is still there")
# A document that brings the PDF management of the kernel with it: it writes
# the XMP stream itself, and a package that would write a second one is turned
# away. The two curricula vitae of the family are built this way, for PDF/UA.
MANAGED = r"""\DocumentMetadata{}
\documentclass[a4paper,10pt]{article}
\usepackage{nordwind}
\title{Gemessen}
\author{Erika Musterfrau}
\begin{document}
\maketitle
Dolor sit amet.
\end{document}
"""
def managed(check):
"""Where the kernel manages the PDF, the suite asks for nothing beside it.
`hyperxmp` writes the stream a reader and an archive look at, and the
template loads it. Where a document declares `\\DocumentMetadata`, the
kernel writes that stream itself and turns the package away with a warning —
and a warning is an error in the projects that build such documents, so the
template must not ask for it there. Measured in the build log, and the
title still has to reach the file.
"""
source = support.write("managed.tex", MANAGED)
pdf, log = build(source, EXAMPLES)
with open(log, encoding="utf-8", errors="replace") as handle:
text = handle.read()
turned = [line for line in text.splitlines()
if "hyperxmp" in line and ("disabled" in line
or "Warning" in line)]
check.that(not turned,
"the template asks for hyperxmp where the kernel manages the "
"PDF: " + "; ".join(turned[:2]))
properties = support.run(["pdfinfo", pdf]).stdout
check.that("Gemessen" in properties,
"the title of a managed document did not reach its properties")
def main():
check = Check("identity")
source_checks(check)
plain(check)
page_checks(check)
logo_from_file(check)
logo_from_svg(check)
tagline(check)
tagline_colour(check)
body_colour(check)
rule_colour(check)
named_destinations(check)
deck_ground(check)
address_on_logo(check)
marks_fill(check)
lockup_sizes(check)
palette(check)
daughter_mark(check)
managed(check)
link_style(check)
return check.done()
+265
View File
@@ -86,6 +86,27 @@ def pair_document():
return "".join(parts)
def level_document():
"""The fourth and the fifth heading level, over every distance to the foot.
Both keep the shape their class gives them and run into the paragraph they
open, and both go through the same reservation as the three levels above:
a `####` out of Markdown is the fourth, and a level that misses the
reservation leaves its heading at the foot of a page while what it
announces begins on the next. Only `\\section` and `\\subsection` were ever
built by a test, so the claim about these two rested on the code alone.
"""
parts = [HEAD]
for level in ("paragraph", "subparagraph"):
for case in range(CASES):
parts.append("\\newpage\n")
parts.append("Filler line.\\par\n" * case)
parts.append("\\%s{%s case %d}\n" % (level, level.title(), case))
parts.append(long_table("%s cell case %d" % (level.title(), case)))
parts.append("\\end{document}\n")
return "".join(parts)
def sweep_document():
parts = [HEAD]
for case in range(CASES):
@@ -131,6 +152,16 @@ Dolor sit amet.
\end{document}
"""
HEADLINE_ONE = HEADLINE % ""
# A company whose lockup is wide: a long name, a long address and a logo of four
# ems. The head used to divide its line by a fixed share, 0.45 of it for the
# logo, and this block needs 0.57 — the logo then stood 65.11 points past the
# right edge of the paper and the annotation of its link lay outside the page
# box, measured on 2026-10-06.
HEADLINE_WIDE = HEADLINE.replace(
"\\usepackage{nordwind}",
"\\usepackage[logoheight=4em]{nordwind}") % (
"\\businessidentity{name={Nordwind Industriebeteiligungen},\n"
" address={https://nordwind-industriebeteiligungen.example}}\n")
# The same head with the lockup of two lines. The heading on the left has to
# stand on the line of the NAME, the first of the two, and not on the middle of
# a block that has grown a second line.
@@ -154,6 +185,47 @@ PICTURES = r"""\documentclass[a4paper,10pt]{article}
"""
def list_document():
"""Three headings, each followed by a list, and a table under the last.
A list sets an `\\everypar` of its own, so the forgetting that the first
paragraph after a heading does never runs. Where a heading does not ask
whether the announcement above it is still the one directly above, every
following heading takes over the number of the first, and the table at the
end writes its measurement under that number: the room for everything in
between is then reserved in front of the first heading.
"""
parts = [HEAD, "\\section{Ergebnisse}\n"]
for number in (1, 2):
parts.append("\\begin{itemize}\n\\item Erster Punkt der Liste\n"
"\\item Zweiter Punkt der Liste\n\\end{itemize}\n")
parts.append("\\section{Abschnitt %d}\n" % number)
parts.append(table("Letzte Zeile"))
parts.append("\\end{document}\n")
return "".join(parts)
# A picture with its caption, once short enough for one line and once long
# enough for two. The caption belongs to the picture over it, and a picture that
# stands alone is centred on the line, so the caption is centred too.
CAPTIONS = r"""\documentclass[a4paper,10pt]{article}
\usepackage{nordwind}
\begin{document}
\pagestyle{empty}
\begin{figure}[h]
\businessgraphic{example-image-16x9}
\caption{Kurze Legende}
\end{figure}
\end{document}
"""
CAPTIONS_LONG = CAPTIONS.replace(
"Kurze Legende",
"Eine Legende, die so lang ist, dass sie über die ganze Zeile reicht und "
"auf einer zweiten weitergeht, weil eine Legende aus mehreren Zeilen "
"anders ausgerichtet werden kann als eine aus einer einzigen, und beide "
"Fälle hier gemessen werden sollen")
# The same raster picture in a document that allows it a tenth of enlargement
# and no more: 401.5 points wide by itself, so 441.6 with the option below,
# where the line would otherwise give it 540.6.
@@ -342,6 +414,27 @@ def together(check):
f"{sentence} and its table on page {cell}")
def levels(check):
"""The fourth and the fifth level hold their table like the three above.
A heading that runs into its paragraph is still an announcement, and the
reservation reaches it through the same wrapper as every other level. The
fixture pushes each of the two down the page line by line over sixteen
distances to the foot, each with a table under it that asks for far more
room than a heading does by itself.
"""
source = support.write("levels.tex", level_document())
pdf, _ = build(source, EXAMPLES)
count = support.pages(pdf)
for level in ("Paragraph", "Subparagraph"):
for case in range(CASES):
heading = page_of(pdf, "%s case %d" % (level, case), count)
cell = page_of(pdf, "%s cell case %d" % (level, case), count)
check.that(heading and cell and heading == cell,
f"{level.lower()} case {case}: the heading stands on "
f"page {heading} and its table on page {cell}")
def foot(check):
"""What a document puts in the inner corner of the foot stays in the foot."""
source = support.write("foot.tex", FOOT)
@@ -590,6 +683,173 @@ def head_line(check):
"not on one line")
def wide_logo(check):
"""The logo takes the room it needs, and the running head takes the rest.
How much of the line a logo needs follows from the name, the address and the
height the document asks for, and no share of the line fits every company.
What is measured is the rendered page: the ink of the head ends inside the
text block, the build reports no box over the edge, and the heading on the
left still arrives.
"""
source = support.write("headlinewide.tex", HEADLINE_WIDE)
pdf, log = build(source, EXAMPLES)
with open(log, encoding="utf-8", errors="replace") as handle:
complaints = [line for line in handle.read().splitlines()
if line.startswith("Overfull \\hbox")]
check.that(not complaints,
"a wide lockup makes the head run over: "
+ "; ".join(complaints[:2]))
page = Page(pdf, 1)
blocks = bands(page)
check.that(blocks, "the page with the wide lockup carries no ink")
if not blocks:
return
factor = 72.0 / page.dpi
right = max(x for y in blocks[0] for x in range(page.width)
if page.dark(x, y)) * factor
# The text block ends one centimetre from the right edge of A4, and the
# glyphs may reach their own side bearing beyond the ink of the drawing.
edge = 595.28 - 28.45
check.that(right <= edge + 1,
f"the head ends at {right:.1f} points and the text block at "
f"{edge:.1f}")
heading = first_glyph(page, blocks[0][0], blocks[0][-1] + 1, 0,
page.width // 2)
check.that(heading, "the running head lost its room beside the wide logo")
def after_list(check):
"""A heading after a list announces for itself, not for the one above it.
Two headings in a row are one announcement, and the lower one takes over the
number of the upper. Between them, though, stands a list, so they are not in
a row at all. Measured in the aux file, where every reservation stands with
the number it belongs to, and on the page: the first heading has to stay on
the page it begins, and the table has to reserve its own room, a few lines,
instead of the whole distance from the first heading down to itself.
"""
source = support.write("afterlist.tex", list_document())
pdf, _ = build(source, EXAMPLES)
aux = os.path.join(os.path.dirname(pdf), "afterlist.aux")
with open(aux, encoding="utf-8", errors="replace") as handle:
reserved = [float(points) for points in re.findall(
r"\\business@need\{\d+\}\{([0-9.]+)pt\}", handle.read())]
check.that(reserved, "the table recorded no reservation at all")
if reserved:
check.that(max(reserved) < 200,
f"the table reserves {max(reserved):.0f} points, the "
"distance from a heading several lists further up")
check.that(support.pages(pdf) == 1,
f"the document of three headings runs over "
f"{support.pages(pdf)} pages")
first = support.words(pdf, 1)
check.that(any(word[0] == "Ergebnisse" for word in first),
"the first heading was pushed off page 1 by the reservation")
# The same page twice: once with the word across it, once without. The word is
# long enough to be measured as a band of ink and short enough to be read.
WATERMARK = r"""\documentclass[a4paper,10pt]{article}
\usepackage[%s]{nordwind}
\begin{document}
\section{Lorem}
Dolor sit amet, consetetur sadipscing elitr.
\end{document}
"""
def watermark(check):
"""The word of a draft lies across the page, and only where it is asked for.
It is set once and scaled to four fifths of the diagonal of the paper in
use, turned by the angle of that diagonal, so it lies along the page at
every paper size; a size in centimetres fits one paper and one word. What
is measured is the ink: the page with the word carries it over the whole
height and width of the text block, the page without it carries only its
own lines, and the word is light enough to read the page through it.
"""
plain, _ = build(support.write("nowatermark.tex", WATERMARK % ""),
EXAMPLES)
marked, _ = build(support.write("watermark.tex", WATERMARK % "draft"),
EXAMPLES)
quiet, loud = Page(plain, 1), Page(marked, 1)
# The word is as quiet as the paper allows, so the reader of the raster has
# to be told what counts as ink here: anything that is not the white of the
# page. The ordinary threshold of a layout test sees type and would report
# the page with the word as empty of it.
def tinted(page):
return [(x, y) for y in range(page.height)
for x in range(page.width)
if page.pixels[y * page.width + x] < 250]
light = tinted(loud)
check.that(len(light) > 20 * len(tinted(quiet)),
f"the page with the word carries {len(light)} tinted pixels and "
f"the page without it {len(tinted(quiet))}")
if not light:
return
factor = 72.0 / loud.dpi
width = (max(x for x, _ in light) - min(x for x, _ in light)) * factor
height = (max(y for _, y in light) - min(y for _, y in light)) * factor
check.that(width > 400,
f"the word spans {width:.0f} points across the page")
check.that(height > 400,
f"the word reaches over {height:.0f} points of the height")
# And it stays on the paper: a word too large for the sheet is cut at the
# edge, which is what a size written in centimetres does to a long word.
check.that(min(x for x, _ in light) > 2 and min(y for _, y in light) > 2,
"the word is cut at the top or the left edge of the sheet")
check.that(max(x for x, _ in light) < loud.width - 3
and max(y for _, y in light) < loud.height - 3,
"the word is cut at the bottom or the right edge of the sheet")
# That a page without the option carries no word is the first check above:
# the tinted pixels of the two pages are a factor of twenty apart, and an
# absolute number would measure the type and the logo of the page, which
# carry grey edges of their own.
def caption(check):
"""The caption stands centred under its picture, over one line and over two.
A picture that stands alone is centred on the line, and its caption used to
begin at the left edge of the text: the two then read as two things. What is
measured is the rendered page — the ink of the caption against the ink of the
picture over it.
"""
for what, fixture, lines in (("one line", CAPTIONS, 1),
("two lines", CAPTIONS_LONG, 2)):
source = support.write("caption%d.tex" % lines, fixture)
pdf, _ = build(source, EXAMPLES)
page = Page(pdf, 1)
blocks = bands(page)
# The picture is the tallest band of the page; the head and the folio
# stand on it too, so neither its place nor its number is assumed.
tallest = max(range(len(blocks)),
key=lambda index: len(blocks[index]))
picture = blocks[tallest]
columns = page.ink_columns(picture[0], picture[-1] + 1)
middle = (columns[0] + columns[-1]) / 2
# What follows the picture within a line of it is its caption; what
# stands further down belongs to the page.
step = int(round(2 * page.dpi / 72.0 * 10))
found, previous = [], picture
for band in blocks[tallest + 1:]:
if band[0] - previous[-1] > step:
break
found.append(band)
previous = band
check.that(len(found) == lines,
f"the caption of {what} came out in {len(found)} lines")
for number, band in enumerate(found, 1):
text = page.ink_columns(band[0], band[-1] + 1)
centre = (text[0] + text[-1]) / 2
apart = abs(centre - middle) * 72.0 / page.dpi
check.that(apart < 3,
f"line {number} of the caption of {what} stands "
f"{apart:.1f} points beside the middle of its picture")
def opening(check):
"""The title block brings the distance under it, and nothing adds to it.
@@ -734,12 +994,17 @@ def main():
pairs(check)
pictures(check)
head_line(check)
wide_logo(check)
caption(check)
watermark(check)
after_list(check)
opening(check)
edges(check)
headroom(check)
foot(check)
left_edge(check)
together(check)
levels(check)
return check.done()
+825 -17
View File
@@ -5,10 +5,15 @@ The converter is the second road into the same design, and the two have to end
in the same page: the same font, the same logo, the same table that fits the
line. What is measured here is the result and not the call.
"""
import http.server
import importlib.util
import os
import re
import shutil
import sys
import tempfile
import threading
import time
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import support
@@ -17,6 +22,7 @@ from support import Check, Page
EXAMPLES = os.path.join(support.ROOT, "examples")
CONVERTER = os.path.join(support.ROOT, "bin", "md2pdf.py")
OUT = os.path.join(support.BUILD, "md2pdf")
support.name_example_company()
def runnable(check):
@@ -91,6 +97,104 @@ def inside(check, pdf):
"margin of the page")
# A paragraph of many short code spans in a row, the shape a style guide writes
# when it lists the names of a convention. Each span is shorter than a path and
# carries its boundaries inside itself, and between two of them stands nothing a
# line may break at but one space.
CODELINE_MD = """---
title: Inline code
---
## Variables
- **Name CSS custom properties consistently.** Use `--<property>[-<role>][-<context>]`, omitting unnecessary segments and placing the context last. Examples of Base Variables: `--font-size-base`, `--font-size-large`, `--color-dark`, `--size-medium`. Examples of Semantic Variables: `--color-fg`, `--color-bg-popup`, `--color-fg-error`, `--padding-box`, `--margin-box`, `--border-box`, `--gap-heading`. Base names identify a palette entry, scale step or universal HTML default; Semantic names identify a purpose or context.
"""
# What a break may never fall into: the hyphens a name begins with. `--` alone
# at the end of a line, or `-` with the rest of the name on the next, leaves a
# reader who copies the name with two pieces, neither of which is the name.
def convert_file(check, name, markdown):
"""One document of our own through the converter, as a reader receives it."""
source = support.write(name + ".md", markdown)
result = support.run(["python3", CONVERTER, "--identity", "nordwind",
"--keep-tex", "--out-dir", OUT, source])
pdf = os.path.join(OUT, name + ".pdf")
check.that(os.path.exists(pdf),
f"{name}.md did not build:\n{result.stdout[-1000:]}")
return pdf if os.path.exists(pdf) else None
def leading_hyphens(check):
"""A name that begins with hyphens keeps them, whatever the line does.
A custom property is written `--size-medium`, and the break the converter
offers after every structuring character fell between the two hyphens and
behind them: one line ended with `--` and the next began with `size-medium`,
another ended with `-` and the next began with `-margin-box`. The break
after an inner hyphen stays, which is where a reader reads a boundary
anyway.
"""
# Measured on the PAGE of the document whose lines really break, the same
# sentence the overflow was found in: the break this forbids is one TeX
# takes by itself after a hyphen, so a file carrying the right break points
# can still break in the wrong place. The first version of this check read
# the LaTeX, passed, and the page still ended a line with `--`.
pdf = convert_file(check, "codeline", CODELINE_MD)
if not pdf:
return
lines = [line.strip() for line
in support.run(["pdftotext", "-layout", pdf, "-"]).stdout
.splitlines() if line.strip()]
# A line that BEGINS with a name is right: the whole name moved to the next
# line. What is wrong is a line that ends with the hyphens alone and leaves
# the rest of the name behind.
ends = [line for line in lines if re.search(r"(^|\s)-+$", line)]
check.that(not ends,
"a line ends with the hyphens a name begins with: "
+ "; ".join(ends))
text = " ".join(lines)
for name in ("--size-medium", "--margin-box", "--color-fg-error"):
check.that(name.replace("-", "") in text.replace("-", "")
.replace(" ", ""),
f"the name {name} did not arrive on the page")
def code_line(check):
"""A line of inline code spans stays inside the type area.
A span of a typewriter face is one word to LaTeX: it carries no space and
hyphenation patterns do not reach it, so a line that ends in one runs past
the right edge. The converter offers a break at the characters that
structure such a name, and it did so only from sixteen characters up: a
paragraph that lists `--color-dark` and `--size-medium`, twelve and thirteen
characters, then had two lines over the edge, 3.5 and 7.9 points, while
`--color-fg-error` beside them broke after its hyphen. Measured on the
rendered page and in the build log.
"""
path = support.write("codeline.md", CODELINE_MD)
result = support.run(["python3", CONVERTER, "--identity", "nordwind",
"--keep-tex", "--out-dir", OUT, path])
pdf = os.path.join(OUT, "codeline.pdf")
check.that(os.path.exists(pdf),
f"the document of code spans did not build:\n"
f"{result.stdout[-1000:]}")
if not os.path.exists(pdf):
return
page = Page(pdf, 1)
factor = 72.0 / page.dpi
right = page.ink_columns()[-1] * factor
check.that(right < 568,
f"a line of code spans reaches {right:.1f} points, past the "
"right margin of the page")
log = os.path.join(OUT, "build", "codeline.log")
with open(log, encoding="utf-8", errors="replace") as handle:
over = [line for line in handle.read().splitlines()
if line.startswith("Overfull \\hbox")]
check.that(not over,
"the build reports a line over the edge: " + "; ".join(over[:2]))
def content(check, pdf):
text = support.text(pdf)
check.that("Lorem Ipsum" in text, "the title block did not arrive")
@@ -239,9 +343,9 @@ def pictures(check):
"""A picture out of Markdown takes the size of the family.
The converter hands every picture to pandoc's own bound, and that is where
the rules of the template hang: the lying picture on the first page takes
the whole line, the raster one on the second is the one the converter marks
as pixels, and both stand centred.
the rules of the template hang: the lying picture takes the whole line, the
raster one is the one the converter marks as pixels, both stand centred, and
both carry the caption the document gave them.
"""
path = support.write("picture.md", PICTURE_MD)
result = support.run(["python3", CONVERTER, "--identity", "nordwind",
@@ -252,31 +356,128 @@ def pictures(check):
f"{result.stdout[-1000:]}")
if not os.path.exists(pdf):
return
for number, name in ((1, "lying"), (2, "raster")):
# Both captions reach the page. A raster picture carries the mark of its
# kind in front of it, and the paragraph was then two things and no longer
# a picture alone, so it lost its caption: measured on 2026-10-05.
text = support.run(["pdftotext", pdf, "-"]).stdout
for caption in ("Lorem ipsum dolor sit amet", "Consetetur sadipscing elitr"):
check.that(caption in text,
f"the caption «{caption}» did not reach the page")
# A picture that moves to the next page leaves the room it did not take
# EMPTY, at the foot of the page. The page used to be stretched over that
# room instead: the author stood 240 points under the title, and the heading
# over the first picture ended up inside it. Measured on 2026-10-05.
first = support.words(pdf, 1)
title = [word for word in first if word[0] == "Lorem" and word[2] < 120]
author = [word for word in first if word[0] == "Erika"]
if title and author:
gap = author[0][2] - max(word[4] for word in title)
check.that(gap < 30,
f"the author stands {gap:.0f} points under the title: the "
"page was stretched over the room a picture left")
# And the raster picture with its caption fits into what the first page has
# left, which is more than the quarter the template may leave empty: it is
# made smaller and stays. Inside a figure the caption under the picture was
# not counted, so the picture made to fit the room did not fit with its
# caption and moved after all.
check.that(support.pages(pdf) == 1,
f"the raster picture moved to page {support.pages(pdf)} although "
"the first page had more than a quarter of its height left for it")
heading = [word for word in first if word[0] == "Consetetur"
and word[2] > 100]
figure = [word for word in first if word[0] == "16×9"]
if heading and figure:
check.that(heading[0][4] < figure[0][2] - 60,
"the heading over the first picture stands inside it")
# The pictures are the two tallest bands of ink, on whatever page each one
# stands. The page was once assumed: the raster picture was read off page
# 2 of a document of one page, which the old reader of the rendered page
# answered with a file an earlier test had left behind.
bands_found = []
for number in range(1, support.pages(pdf) + 1):
page = Page(pdf, number)
factor = 72.0 / page.dpi
rows = page.ink_rows()
check.that(rows, f"the {name} picture is not on page {number}")
if not rows:
continue
grouped, current = [], []
for row in rows:
for row in page.ink_rows():
if current and row - current[-1] > 1:
grouped.append(current)
current = []
current.append(row)
grouped.append(current)
tallest = max(grouped, key=lambda band: band[-1] - band[0])
columns = page.ink_columns(tallest[0], tallest[-1] + 1)
if current:
grouped.append(current)
for band in grouped:
columns = page.ink_columns(band[0], band[-1] + 1)
bands_found.append((band[-1] - band[0], number, columns, page.width,
factor))
pictures_found = sorted(bands_found, reverse=True)[:2]
check.that(len(pictures_found) == 2, "the two pictures did not reach the PDF")
for height, number, columns, page_width, factor in pictures_found:
width = (columns[-1] - columns[0] + 1) * factor
left = columns[0] * factor
right = page.width * factor - columns[-1] * factor
right = page_width * factor - columns[-1] * factor
check.that(width <= 545,
f"the {name} picture is {width:.1f} points wide, more than "
"the 540.6 of the line")
f"a picture on page {number} is {width:.1f} points wide, "
"more than the 540.6 of the line")
check.that(abs(left - right) < 4,
f"the {name} picture stands {left:.1f} points from the left "
f"edge and {right:.1f} from the right, so it is not centred")
f"a picture on page {number} stands {left:.1f} points from "
f"the left edge and {right:.1f} from the right, so it is not "
"centred")
# A page whose content has fixed heights, so that what is left for the picture
# is known: the title block, one sentence, a block of 62 percent of the text
# height, and then a heading with a lying picture that needs more than the
# quarter of a page that remains. The picture stands under a heading, which
# holds it, so the two move together and the page ends after the block; the
# spaces of the page are few, so their play cannot cover the room left.
SHORT_TEX = r"""\documentclass[a4paper,10pt]{article}
\usepackage{nordwind}
\title{Lorem Ipsum}
\author{Erika Muster}
\begin{document}
\maketitle
Lorem ipsum dolor sit amet.
\noindent\rule{0.5\linewidth}{0.62\textheight}
\subsection{Dolor Sit}
\businessgraphic{example-image-16x9}
Consetetur sadipscing elitr.
\end{document}
"""
def short_page(check):
"""A page that a picture leaves early keeps the rest empty at its foot.
A picture that does not fit into the last quarter of a page moves to the
next one, and the room it leaves stays empty. The page used to be stretched
over that room instead, every space past the half line it may grow by: the
author then stood 240 points under the title. The distance between the
title and the author is read off the first page.
And the picture does move: a lying picture used to take whatever height was
left, so with a strip of 60 points it came out 60 points high on the first
page, and the quarter the template may leave empty never applied.
"""
pdf, _ = support.build(support.write("shortpage.tex", SHORT_TEX), EXAMPLES)
# The lying picture moves to the next page and does not shrink into the
# strip that is left: less than a quarter of the page remains for it.
first = support.run(["pdftotext", "-f", "1", "-l", "1", pdf, "-"]).stdout
check.that("16×9" not in first and support.pages(pdf) == 2,
"the lying picture shrank into the last strip of the first page "
"instead of moving to the next one")
found = support.words(pdf, 1)
title = [word for word in found if word[0] == "Lorem" and word[2] < 120]
author = [word for word in found if word[0] == "Erika"]
check.that(title and author, "the title block did not reach the page")
if not title or not author:
return
gap = author[0][2] - max(word[4] for word in title)
check.that(gap < 30,
f"the author stands {gap:.0f} points under the title: the page "
"was stretched over the room the picture left")
def twins(check):
@@ -321,6 +522,599 @@ def missing(check):
"the converter produced a PDF for a file that does not exist")
def foreign_rc(check):
"""A latexmkrc beside a document is not executed by the converter.
latexmk runs the Perl code of a `latexmkrc` in its working directory, and
the converter builds inside the directory of the document, so a document
that arrives with such a file would run its code on the machine that
converts it. The rc file here writes a marker file; the marker must not
appear, and the document must still come out.
"""
folder = os.path.join(support.BUILD, "foreign-rc")
shutil.rmtree(folder, ignore_errors=True)
os.makedirs(folder)
with open(os.path.join(folder, "doc.md"), "w", encoding="utf-8") as handle:
handle.write("# Lorem Ipsum\n\nConsetetur sadipscing elitr.\n")
with open(os.path.join(folder, "latexmkrc"), "w", encoding="utf-8") as handle:
handle.write("open(my $f, '>', 'rc-ran.txt'); close($f);\n")
result = support.run(["python3", CONVERTER, "--identity", "nordwind",
os.path.join(folder, "doc.md")], cwd=folder)
check.that(os.path.exists(os.path.join(folder, "doc.pdf")),
f"the document beside a latexmkrc did not build:\n"
f"{result.stdout[-1000:]}")
check.that(not os.path.exists(os.path.join(folder, "rc-ran.txt")),
"latexmk executed the latexmkrc lying beside the document")
SHELL_MARKER = "shell-ran.ind"
# An identity that lies beside the document and starts a program as it loads.
# The converter puts the directory of the document on the search path, so the
# probe and the build both read it from there.
SHELL_IDENTITY = (r"\ProvidesPackage{shellcompany}" "\n"
r"\immediate\write18{makeindex -o " + SHELL_MARKER
+ r" entries.idx}" "\n"
r"\RequirePackage{nordwind}" "\n")
SHELL_TEX = (r"\documentclass[a4paper,10pt]{article}" "\n"
r"\usepackage{shellcompany}" "\n"
r"\begin{document}Consetetur.\end{document}" "\n")
def shell_escape(check):
"""A document cannot start a program through the converter.
A raw LaTeX block out of the Markdown comes out as verbatim, a header field
and the label of a form field as escaped text; math reaches LaTeX as it is
written, which is why a document is converted only when it is trusted. What
reaches
LaTeX is every package file in the directory of the document, because the
converter searches there, and TeX Live allows a restricted set of programs
by default, `makeindex` among them. The package here asks for `makeindex`
through `\\write18` to write a marker file, and the document is converted
with that package as its identity: the marker must not appear. A control
run builds a document with the same package without the converter: the
marker must appear there, which proves that the program is allowed and
that this check can see it.
"""
folder = os.path.join(support.BUILD, "shell-escape")
shutil.rmtree(folder, ignore_errors=True)
os.makedirs(folder)
for name, text in (("shellcompany.sty", SHELL_IDENTITY),
("doc.md", "# Lorem Ipsum\n\nConsetetur sadipscing.\n"),
("entries.idx", "\\indexentry{a}{1}\n"),
("control.tex", SHELL_TEX)):
with open(os.path.join(folder, name), "w", encoding="utf-8") as handle:
handle.write(text)
result = support.run(["python3", CONVERTER, "--identity", "shellcompany",
os.path.join(folder, "doc.md")], cwd=folder)
check.that(os.path.exists(os.path.join(folder, "doc.pdf")),
f"the document with a package of its own did not build:\n"
f"{result.stdout[-1000:]}")
check.that(not os.path.exists(os.path.join(folder, SHELL_MARKER)),
"xelatex started a program for a package beside the document")
support.run(["latexmk", "-norc", "-xelatex", "-interaction=nonstopmode",
"-outdir=" + os.path.join(folder, "control"), "control.tex"],
cwd=folder,
environment={"TEXINPUTS": support.searchpath(EXAMPLES, folder)})
check.that(os.path.exists(os.path.join(folder, SHELL_MARKER)),
"the control run without the converter did not start the "
"program, so this check proves nothing")
# A company whose identity lies outside the TeX tree, and a file of the same
# name beside the document that claims to be it.
ELSEWHERE_IDENTITY = (r"\ProvidesPackage{elsewhere}" "\n"
r"\RequirePackage{nordwind}" "\n"
r"\businessidentity{name=Fernwald}" "\n")
DECOY_IDENTITY = (r"\ProvidesPackage{elsewhere}" "\n"
r"\RequirePackage{nordwind}" "\n"
r"\businessidentity{name=Attrappe}" "\n")
def inherited_search(check):
"""A company names the directory of its identity, and that one is used.
A company whose identity lies outside the TeX tree calls the converter with
its directory in `TEXINPUTS`. The converter replaced that search path with
its own until 2026-10-05, so such a company did not build at all. The
directory of the caller comes before the folder of the document: a file
beside a document must not stand in for the identity of the company, so the
fixture puts one there that carries another name.
The fixture lies OUTSIDE this repository. Until 2026-11-14 the converter
searched the suite first and through every folder of it, so a fixture under
`tests/build` was found there before anything else, and the check measured
the order of two files in one tree instead of the order of the search path.
"""
folder = tempfile.mkdtemp(prefix="business-inherited-")
try:
identity = os.path.join(folder, "identity")
document = os.path.join(folder, "document")
os.makedirs(identity)
os.makedirs(document)
for path, text in (
(os.path.join(identity, "elsewhere.sty"), ELSEWHERE_IDENTITY),
(os.path.join(document, "elsewhere.sty"), DECOY_IDENTITY),
(os.path.join(document, "doc.md"),
"# Lorem Ipsum\n\nConsetetur sadipscing elitr.\n")):
with open(path, "w", encoding="utf-8") as handle:
handle.write(text)
result = support.run(["python3", CONVERTER, "--identity", "elsewhere",
os.path.join(document, "doc.md")], cwd=document,
environment={"TEXINPUTS": os.pathsep.join(
[identity + "//", EXAMPLES, ""])})
pdf = os.path.join(document, "doc.pdf")
check.that(os.path.exists(pdf),
f"a company named in TEXINPUTS did not build:\n"
f"{result.stdout[-1000:]}")
if not os.path.exists(pdf):
return
text = support.run(["pdftotext", pdf, "-"]).stdout
check.that("Fernwald" in text and "Attrappe" not in text,
"the identity came from the folder of the document and not "
"from the directory the caller named")
finally:
shutil.rmtree(folder, ignore_errors=True)
# A probe of the same name as the one the converter writes, on the search path
# of the caller, which measures a line one point wide.
DECOY_PROBE = (r"\documentclass{article}\begin{document}" "\n"
r"\newwrite\businessmetrics" "\n"
r"\immediate\openout\businessmetrics=metrics.txt" "\n"
r"\immediate\write\businessmetrics{linewidth 1.0pt}" "\n"
r"\immediate\write\businessmetrics{reference 1.0pt}" "\n"
r"\immediate\closeout\businessmetrics" "\n"
r"\end{document}" "\n")
def own_probe(check):
"""The line is measured in the probe the converter wrote, and in no other.
The probe is a LaTeX file in the build directory, and xelatex looks up a
file name without a directory through the search path, where the suite and
the directories of the caller come first. A `metrics.tex` left in the suite
by an earlier conversion was found there and measured the face of the
example company for every company: Pacta built its tables with the width of
a character of TeX Gyre Pagella, 4.55 points instead of 5.24, and a word of
a head cell stood 7 points past the right edge of the page. Measured on
2026-10-05. The decoy here lies outside this repository, on the search path
the caller hands over.
"""
folder = tempfile.mkdtemp(prefix="business-probe-")
try:
decoy = os.path.join(folder, "decoy")
document = os.path.join(folder, "document")
os.makedirs(decoy)
os.makedirs(document)
for path, text in ((os.path.join(decoy, "metrics.tex"), DECOY_PROBE),
(os.path.join(document, "doc.md"),
"# Lorem Ipsum\n\nConsetetur sadipscing elitr.\n")):
with open(path, "w", encoding="utf-8") as handle:
handle.write(text)
result = support.run(["python3", CONVERTER, "--identity", "nordwind",
"--keep-tex", os.path.join(document, "doc.md")],
cwd=document,
environment={"TEXINPUTS": os.pathsep.join(
[decoy + "//", EXAMPLES, ""])})
metrics = os.path.join(document, "build", "metrics.txt")
check.that(os.path.exists(metrics),
f"the converter wrote no measurement of the line:\n"
f"{result.stdout[-1000:]}")
if not os.path.exists(metrics):
return
# Which file xelatex really ran, out of its log. The width alone does
# not tell: a probe of the example company left in this repository
# measures the same line as the right one, and it is found before the
# decoy. The log breaks its lines at 79 characters, so they are joined.
log = os.path.join(document, "build", "metrics.log")
ran = ""
if os.path.exists(log):
with open(log, encoding="utf-8", errors="replace") as handle:
ran = handle.read().replace("\n", "")
own = os.path.join(document, "build", "metrics.tex")
check.that("(" + own in ran,
"xelatex did not run the probe the converter wrote into its "
"build directory, but a file of the same name from the "
"search path")
with open(metrics, encoding="utf-8") as handle:
found = dict(re.findall(r"^(\w+) ([0-9.]+)pt$", handle.read(),
re.MULTILINE))
width = float(found.get("linewidth", 0))
check.that(530 < width < 545,
f"the measured line is {width} points wide: the converter "
"ran a file called metrics.tex from the search path instead "
"of its own probe")
finally:
shutil.rmtree(folder, ignore_errors=True)
# A package named like one of the TeX tree, which stops the build where it is
# read, and an identity of a name nothing else of the suite carries.
DECOY_PACKAGE = (r"\ProvidesPackage{fontspec}" "\n"
r"\errmessage{a package from a subfolder of the document "
r"was loaded}" "\n")
SUBFOLDER_IDENTITY = (r"\ProvidesPackage{businesssubfolder}" "\n"
r"\RequirePackage{nordwind}" "\n")
def subfolders(check):
"""The folder of the document and the suite are searched, no folder below.
kpathsea expands a search path entry that ends in `//` into every folder
below it, and the TeX tree comes last: a document at the root of a checkout
of 68377 folders took 257 seconds for one lookup of `fontspec.sty` instead
of 0.15, and its probe ran for twelve and a half minutes, measured on
2026-11-14. A file named like a package of the TeX tree anywhere below the
document replaced that package, and a package left in a test build of this
repository could serve as the identity of a company.
The folder of the first document carries a `fontspec.sty` two folders down
that stops LaTeX: the document must build. A package of a name only this
check uses lies two folders down in the test builds of the suite: asked for
as the identity of the second document, it must not be found.
"""
folder = tempfile.mkdtemp(prefix="business-subfolders-")
suite = os.path.join(support.BUILD, "subfolders")
shutil.rmtree(suite, ignore_errors=True)
try:
decoyed = os.path.join(folder, "decoyed")
plain = os.path.join(folder, "plain")
hidden = os.path.join(suite, "notes", "drafts")
os.makedirs(os.path.join(decoyed, "notes", "drafts"))
os.makedirs(plain)
os.makedirs(hidden)
text = "# Lorem Ipsum\n\nConsetetur sadipscing elitr.\n"
for path, content in (
(os.path.join(decoyed, "notes", "drafts", "fontspec.sty"),
DECOY_PACKAGE),
(os.path.join(hidden, "businesssubfolder.sty"),
SUBFOLDER_IDENTITY),
(os.path.join(decoyed, "doc.md"), text),
(os.path.join(plain, "doc.md"), text)):
with open(path, "w", encoding="utf-8") as handle:
handle.write(content)
result = support.run(["python3", CONVERTER, "--identity", "nordwind",
"--keep-tex", os.path.join(decoyed, "doc.md")],
cwd=decoyed)
check.that(os.path.exists(os.path.join(decoyed, "doc.pdf")),
"a fontspec.sty in a subfolder of the document replaced "
f"the package of the TeX tree:\n{result.stdout[-1000:]}")
result = support.run(["python3", CONVERTER, "--identity",
"businesssubfolder", "--keep-tex",
os.path.join(plain, "doc.md")], cwd=plain)
# The log breaks its lines at 79 characters, so they are joined.
log = os.path.join(plain, "build", "doc.log")
written = ""
if os.path.exists(log):
with open(log, encoding="utf-8", errors="replace") as handle:
written = handle.read().replace("\n", "")
check.that(result.returncode != 0
and "File `businesssubfolder.sty' not found" in written,
"a package in a subfolder of the suite was found as the "
f"identity of a company:\n{result.stdout[-1000:]}")
finally:
shutil.rmtree(folder, ignore_errors=True)
shutil.rmtree(suite, ignore_errors=True)
# A document with a title whose first section runs over the first page, so that
# the second page begins inside it; the second section opens further down.
HEAD_PARAGRAPH = ("Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed "
"diam nonumy eirmod tempor invidunt ut labore et dolore magna "
"aliquyam erat, sed diam voluptua. ") * 5
HEAD_MD = ("---\ntitle: Nordwind Quartal\nauthor: Erika Muster\n---\n\n"
"# Consetetur\n\n" + "\n\n".join([HEAD_PARAGRAPH] * 9)
+ "\n\n# Dolor Sit\n\n" + HEAD_PARAGRAPH + "\n")
# The same title, and a section that the document opens on a page of its own.
HEAD_TEX = r"""\documentclass[a4paper,10pt]{article}
\usepackage{nordwind}
\title{Nordwind Quartal}
\author{Erika Muster}
\begin{document}
\maketitle
Lorem ipsum dolor sit amet.
\newpage
\section{Dolor Sit}
Consetetur sadipscing elitr.
\end{document}
"""
def head_words(pdf, page):
"""The words of the running head: the upper band of the left half."""
return [word for word in support.words(pdf, page)
if word[4] < 60 and word[1] < 298]
def running_head(check):
"""The head of a page after the title names the section, not the title.
The title stands in the head of the page with the title block only, the page
that begins before any section. Whether a page begins before any section
was asked of the marks a heading leaves at the top of a page, and a section
that begins in the middle of a page leaves none there: every page of a
document out of Markdown with a title then carried the title in its head.
Measured on 2026-10-05 on the second page of a Pacta document that began
inside its first section. A section that opens a page of its own is the
other half of the same question.
"""
md = support.write("head.md", HEAD_MD)
result = support.run(["python3", CONVERTER, "--identity", "nordwind",
"--out-dir", OUT, md])
pdf = os.path.join(OUT, "head.pdf")
check.that(os.path.exists(pdf),
f"the document with two sections did not build:\n"
f"{result.stdout[-1000:]}")
if os.path.exists(pdf):
check.that(support.pages(pdf) >= 2,
"the first section does not reach the second page, so the "
"head of a page inside it cannot be measured")
first = [word[0] for word in head_words(pdf, 1)]
second = [word[0] for word in head_words(pdf, 2)]
check.that("Quartal" in first,
f"the head of the page with the title block says {first}, "
"without the title")
check.that("Consetetur" in second and "Quartal" not in second,
f"the second page begins inside «Consetetur» and its head "
f"says {second}")
written, _ = support.build(support.write("headpage.tex", HEAD_TEX), EXAMPLES)
second = [word[0] for word in head_words(written, 2)]
check.that("Dolor" in second and "Quartal" not in second,
f"the second page opens with «Dolor Sit» and its head says "
f"{second}")
SAME_NAME_MD = """# Lorem Ipsum
![Consetetur](a/mark.svg)
![Sadipscing](b/mark.svg)
![Consetetur again](a/mark.svg)
"""
LINKED_SVG = """<svg xmlns="http://www.w3.org/2000/svg" width="200" height="80"
viewBox="0 0 200 80">
<a href="#external-payment-service"><text x="10" y="25" font-size="12"
textLength="100">Payment service</text></a>
<a href="#term-pat"><text x="10" y="55" font-size="12"
textLength="24">PAT</text></a>
</svg>
"""
LINKED_MD = """# Links
[Payment service](#external-payment-service) and [PAT](#term-pat) are defined below.
![Functions](linked.svg)
## External functions
<table>
<tr><th>Logical designation</th><th>Description</th><th>UK</th><th>Switzerland</th></tr>
<tr><td><a id="external-payment-service"></a>Payment service</td><td>Payments</td><td colspan="2">Stripe</td></tr>
</table>
## Terms
| Term | Meaning |
| --- | --- |
| <a id="term-pat"></a>PAT | Pre-approval estimate |
"""
def annotations(pdf, page=1):
"""The link annotations a reader can operate on one finished PDF page."""
shown = support.run(["mutool", "show", pdf, f"pages/{page}"]).stdout
found = re.search(r"/Annots (\d+) 0 R", shown)
if not found:
return []
array = support.run(["mutool", "show", pdf, found.group(1)]).stdout
return [support.run(["mutool", "show", pdf, number]).stdout
for number in re.findall(r"(\d+) 0 R", array)]
def linked_content(check):
"""Links in prose and in an SVG reach definitions in the finished PDF.
Markdown links became coloured text without an annotation because the raw
HTML anchors of tables were dropped by the LaTeX writer. SVG links existed
in the drawing and disappeared when the drawing was converted and embedded.
This fixture uses both paths and reads the final PDF objects a reader clicks.
"""
folder = os.path.join(support.BUILD, "linked-content")
shutil.rmtree(folder, ignore_errors=True)
os.makedirs(folder)
for name, content in (("linked.svg", LINKED_SVG), ("linked.md", LINKED_MD)):
with open(os.path.join(folder, name), "w", encoding="utf-8") as handle:
handle.write(content)
result = support.run(["python3", CONVERTER, "--identity", "nordwind",
"--keep-tex", os.path.join(folder, "linked.md")],
cwd=folder)
pdf = os.path.join(folder, "linked.pdf")
check.that(os.path.exists(pdf),
"the linked document did not build:\n" + result.stdout[-1000:])
if not os.path.exists(pdf):
return
text = support.text(pdf)
check.that("Payment service" in text and "Stripe" in text
and "Pre-approval estimate" in text,
"the HTML provider table or Markdown glossary did not reach "
"the finished PDF")
destinations = support.run(["pdfinfo", "-dests", pdf]).stdout
for target in ("external-payment-service", "term-pat"):
check.that(f'"{target}"' in destinations,
f"the definition {target} is not a destination in the PDF")
links = "\n".join(annotations(pdf))
for target in ("external-payment-service", "term-pat"):
count = links.count(f"/D ({target})")
check.that(count == 2,
f"{target} has {count} clickable links instead of one in "
"prose and one in the drawing")
def same_name_drawings(check):
"""Two drawings of one file name in two folders stay two drawings.
The drawings are converted to PDF before LaTeX runs, into one build
directory. Named after the file alone, `b/mark.svg` overwrote `a/mark.svg`
and the document showed the second drawing twice. A drawing shown twice is
converted once.
"""
folder = os.path.join(support.BUILD, "same-name")
shutil.rmtree(folder, ignore_errors=True)
for name, colour in (("a", "red"), ("b", "blue")):
os.makedirs(os.path.join(folder, name))
with open(os.path.join(folder, name, "mark.svg"), "w",
encoding="utf-8") as handle:
handle.write('<svg xmlns="http://www.w3.org/2000/svg" width="20" '
'height="20"><rect width="20" height="20" fill="'
+ colour + '"/></svg>\n')
with open(os.path.join(folder, "doc.md"), "w", encoding="utf-8") as handle:
handle.write(SAME_NAME_MD)
result = support.run(["python3", CONVERTER, "--identity", "nordwind",
"--keep-tex", os.path.join(folder, "doc.md")],
cwd=folder)
build_dir = os.path.join(folder, "build")
made = sorted(name for name in os.listdir(build_dir)
if name.endswith("mark.pdf")) \
if os.path.isdir(build_dir) else []
check.that(len(made) == 2,
f"three pictures of two drawings called mark.svg gave "
f"{len(made)} converted files instead of 2:\n"
f"{result.stdout[-1000:]}")
if len(made) != 2:
return
contents = []
for name in made:
with open(os.path.join(build_dir, name), "rb") as handle:
contents.append(handle.read())
check.that(contents[0] != contents[1],
"the two converted drawings are identical, so one drawing "
"replaced the other")
def html_sandbox(check):
"""A raw HTML table opens no address while it is read.
The HTML reader of pandoc fetches the page an `<iframe>` names and pastes it
into the document. The table is read in an empty sandbox, so a page lying
beside the document, an internal service or a tracking address never reaches
the PDF and is never asked.
"""
folder = os.path.join(support.BUILD, "html-sandbox")
shutil.rmtree(folder, ignore_errors=True)
os.makedirs(folder)
asked = []
class Page(http.server.BaseHTTPRequestHandler):
def do_GET(self):
asked.append(self.path)
body = b"<p>Zebra marker of the fetched page</p>"
self.send_response(200)
self.send_header("Content-Type", "text/html")
self.send_header("Content-Length", str(len(body)))
self.end_headers()
self.wfile.write(body)
def log_message(self, *arguments):
pass
server = http.server.ThreadingHTTPServer(("127.0.0.1", 0), Page)
threading.Thread(target=server.serve_forever, daemon=True).start()
try:
with open(os.path.join(folder, "doc.md"), "w", encoding="utf-8") as handle:
handle.write("# Sandbox\n\n<table>\n<tr><th>Cell</th></tr>\n"
"<tr><td>Kept text <iframe src=\"http://127.0.0.1:"
f"{server.server_port}/page.html\"></iframe></td></tr>\n"
"</table>\n")
result = support.run(["python3", CONVERTER, "--identity", "nordwind",
os.path.join(folder, "doc.md")], cwd=folder)
finally:
server.shutdown()
server.server_close()
check.that(not asked, f"reading the HTML table asked {asked} of a server")
pdf = os.path.join(folder, "doc.pdf")
check.that(os.path.exists(pdf),
"the document with an HTML table did not build:\n"
+ result.stdout[-1000:])
if not os.path.exists(pdf):
return
text = support.text(pdf)
check.that("Kept text" in text, "the HTML table did not reach the PDF")
check.that("Zebra marker" not in text,
"the page an iframe of the HTML table names was fetched into the PDF")
def form_pattern(check):
"""The pattern that recognises a form reads a line once.
`^.*\\S.*:` tried every split of a line between its two `.*`: 0.29 seconds
for a line of 20000 characters and 1.01 for 40000, measured on 2026-11-14,
so a megabyte of inline data on one line held the build for minutes. The
limit lies far below the 7 seconds the old pattern needs for 100000.
"""
spec = importlib.util.spec_from_file_location("converter_under_test", CONVERTER)
converter = importlib.util.module_from_spec(spec)
spec.loader.exec_module(converter)
for line, form in (("Name: ________", True), (" Remarks: ____ ", True),
("- [ ] An item", True), (": ________", False),
("No label ________", False), ("Two: colons: ____", True)):
check.that(bool(converter.FORM.search(line)) == form,
f"«{line}» is {'not ' if form else ''}read as a form")
start = time.monotonic()
converter.FORM.search("a" + "b" * 100000 + "\n")
seconds = time.monotonic() - start
check.that(seconds < 1, f"a line of 100000 characters took {seconds:.2f} s")
UNLINKED_SVG = re.sub(r"</?a[^>]*>", "", LINKED_SVG)
STALE_MD = """# Drawing
![Functions](drawing.svg)
## Terms
| Term | Meaning |
| --- | --- |
| <a id="external-payment-service"></a>Payment service | Payments |
| <a id="term-pat"></a>PAT | Pre-approval estimate |
"""
def stale_links(check):
"""A kept build folder lays no links of an earlier drawing over a new one.
The drawings are numbered from one in every run, and a drawing without links
wrote no link file, so with `--keep-tex` the file of the earlier drawing of
that number stayed and the template laid its links over the new picture.
"""
folder = os.path.join(support.BUILD, "stale-links")
shutil.rmtree(folder, ignore_errors=True)
os.makedirs(folder)
source = os.path.join(folder, "doc.md")
with open(source, "w", encoding="utf-8") as handle:
handle.write(STALE_MD)
pdf = os.path.join(folder, "doc.pdf")
for drawing in (LINKED_SVG, UNLINKED_SVG):
with open(os.path.join(folder, "drawing.svg"), "w", encoding="utf-8") as handle:
handle.write(drawing)
result = support.run(["python3", CONVERTER, "--identity", "nordwind",
"--keep-tex", source], cwd=folder)
check.that(os.path.exists(pdf), "the document with a drawing did not "
"build:\n" + result.stdout[-1000:])
if not os.path.exists(pdf):
return
links = "\n".join(annotations(pdf)).count("/D (term-pat)")
if drawing is LINKED_SVG:
check.that(links == 1, f"the drawing with links carries {links} "
"links to its term, so this check proves nothing")
check.that(links == 0, f"the drawing without links carries {links} links "
"of the drawing that stood there before")
def main():
check = Check("md2pdf")
runnable(check)
@@ -333,6 +1127,20 @@ def main():
pictures(check)
twins(check)
missing(check)
foreign_rc(check)
shell_escape(check)
linked_content(check)
html_sandbox(check)
form_pattern(check)
stale_links(check)
same_name_drawings(check)
inherited_search(check)
own_probe(check)
subfolders(check)
running_head(check)
code_line(check)
leading_hyphens(check)
short_page(check)
return check.done()
+17 -3
View File
@@ -34,9 +34,11 @@ HEIGHTS = (
("logoheight", r"\\newcommand\{\\pacta@logoheight\}\{([^}]*)\}",
r"\\newcommand\{\\business@logoheight\}\{([^}]*)\}",
"the logo of the page head"),
("titlelogoheight", r"\\newcommand\{\\pacta@titlelogoheight\}\{([^}]*)\}",
r"\\newcommand\{\\business@titlelogoheight\}\{([^}]*)\}",
"the logo of the title block"),
)
FAMILY_HEIGHTS = (
("logoheight", "2em",
r"\\newcommand\{\\business@logoheight\}\{([^}]*)\}",
"the logo of the page head"),
)
# The three proportions of the lockup, as the origin writes them: the name at
# 0.8 of the height, the gap beside it at 0.1, the gap over the address at 0.1.
@@ -73,11 +75,23 @@ def main():
check.that(mine is not None and abs(float(mine) - value) < 0.0001,
f"{what}: this suite says {mine}, the family {value}")
# The height of the page head is a value of the family as well, and it
# stands here as one: since 2026-10-05 the origin is a configuration of
# this suite and carries no height of its own to read.
for name, value, pattern, what in FAMILY_HEIGHTS:
mine = first(pattern, ours)
check.that(mine == value,
f"{what}: this suite says {mine}, the family {value}")
origin = support.run(["kpsewhich", "pacta.sty"]).stdout.strip()
if not origin or not os.path.exists(origin):
print(" pacta.sty is not installed; the heights were not compared")
return check.done()
theirs = read(origin)
if "\\RequirePackage{business-identity}" in theirs:
print(" pacta.sty is a configuration of this suite; its heights are "
"the ones of this suite and were not compared again")
return check.done()
for name, there, here, what in HEIGHTS:
want = first(there, theirs)
mine = first(here, ours)
+23 -4
View File
@@ -23,6 +23,18 @@ def run(command, cwd=ROOT, environment=None):
encoding="utf-8", errors="replace")
def name_example_company():
"""The directory of the example company in the TEXINPUTS of every call.
The example company lies in examples/, outside the TeX tree, and the
converter searches no subfolder of this repository: a test of the converter
names that directory the way every company outside the TeX tree names its
own.
"""
os.environ["TEXINPUTS"] = os.pathsep.join(
[os.path.join(ROOT, "examples"), os.environ.get("TEXINPUTS", "")])
def searchpath(*directories):
return os.pathsep.join([directory + "//" for directory in
(ROOT,) + directories] + [""])
@@ -79,12 +91,19 @@ class Page:
"""One rendered page, as pixels, with the questions a layout test asks."""
def __init__(self, pdf, page=1, dpi=150):
# One file under one name, written fresh for every page. pdftoppm names
# a page after the number of digits of the LAST page of the document, so
# page 1 of a document of ten pages is `page-01` and of one of nine
# `page-1`; the name was guessed in that order, and the guess found the
# `page-1` an earlier test had left behind. Measured on 2026-10-05, when
# three pages of a fifteen-page fixture were read as the pixels of a
# document of two pages built before it.
stem = os.path.join(BUILD, "page")
name = stem + ".pgm"
if os.path.exists(name):
os.remove(name)
run(["pdftoppm", "-r", str(dpi), "-f", str(page), "-l", str(page),
"-gray", pdf, stem])
name = f"{stem}-{page}.pgm"
if not os.path.exists(name):
name = f"{stem}-{page:02d}.pgm"
"-gray", "-singlefile", pdf, stem])
with open(name, "rb") as handle:
data = handle.read()
fields, offset = [], 0