Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion specs/001-pathway-pdfs/research.md
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,10 @@ synthetic documents, on this host under load.
unmeasured. The viewport writes its scale twice, by attribute and by style to six
figures; the two must agree. Only the pathways' labels (`PATHWAY TEXT`) are read: the
illustrations' other lettering (`LABEL_TEXT`, and paths in groups named `TEXT`) is
about the same size at release 98, as #107's review measured. Release 98's 221 illustrations, every one with labels, none
about the same size at release 98, as #107's review measured. Markup the render service
does not write is read only as far as it fails safe: malformed markup, a title or
metadata leaves a label unread; a style element's rules or `display="none"` are not
looked for (`src/layout/lettering.ts` lists them). Release 98's 221 illustrations, every one with labels, none
transformed:
- every label's cap height is 8.49–8.94 of the file's units, a font of about 12 px,
as the diagrams' labels are;
Expand Down
3 changes: 2 additions & 1 deletion specs/001-pathway-pdfs/tasks.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ for WebsiteAngular to ship it (T042).

**Done** (research R4) — #106. Release 98's 1,170 human diagrams, drawn by the render service from the release's own files: only 80 (7%) can print their 12 px labels at 5 pt on any page, the median at 2.1 pt; today's book, which has no floor, prints a median of about 1.85 pt. Raised with the user, who chose, for the reader on a screen: each figure as large as the page allows, portrait or a landscape page of its own where that is larger (1,060 diagrams and all 221 illustrations landscape), never split, linked to its interactive form on the website (FR-003, amended), and listed in the run report below 1 pt (100 diagrams). FR-016 and the data model amended. The illustrations' lettering, outlines, is read from their `PATHWAY TEXT` paths at the render service's viewport scale (`src/layout/lettering.ts`): every label about a 12 px font, 13.9–14.8 px in the render's frame, one at 7.2 px; the render's SVG and the file agreed. Tests: 11 for the lettering, 6 for the figure's links, the placement tests rewritten for the rule. Each of these mutants made one fail: the tallest line not taken, glyphs off the baseline counted, the viewport ignored or any matrix taken, relative commands read as absolute, subpaths merged, no cap-height ratio, an unreadable label skipped or its count dropped, the lettering ignored or used only without text, never landscape, landscape on a tie, the threshold back at 5 pt, the report at the portrait size, no link, no selection, a reaction to the Browser, the section linking the event's page. The link was printed and read back: one annotation over the figure on its landscape page; without the link's `inline-block`, two. From #105's review: `serve`'s bind names its second caller, the analysis gateway (contracts/cli.md, research R9).
- **From #106's review (#107):** the lettering reader left a letter's holes in, so on a short label a counter could win the baseline vote (BBB read 62% small; release 98's smallest labels, long names, were unaffected, and R4's numbers stand, recounted); read an exponent as a command; joined a subpath begun after a close without a move to the one before; took a command with the wrong count of numbers; and read a label transformed, or in a transformed group, at full size. Holes are now left out, an exponent (either case) is a number, a subpath after a close starts at the last one's start, each command's count is checked, and a transformed label is unread, the groups followed through the markup in one pass of its tags. An ancestor's illustration standing in for an event now links with the event selected. Stale wording of the 5 pt floor is gone from the code's comments and R4. Each was seen failing first.
- **From #107's review (#109, replacing #108 after its review):** a dot or an accent met first could begin a line of its own that the letters beneath it missed, and a mark set high won a tied baseline vote; two identical subpaths each hid the other; the scan of tags read comments as markup and only double-quoted attributes, and missed a transform on a link, by style, a nested `<svg>`, and a label kept in `<defs>`. Lines are now the letters whose heights overlap, directly or through others; a tie goes to the lower bottom; only a smaller box within another is a hole; every element is followed, comments and character data left out, attributes in either quotes, and each of those leaves the label unread. The render service's viewport writes its scale by attribute and again by style, to six figures: the two must agree (found when this PR's first version read the real render as unmeasured; the tests' viewport now has both, as the render's does). FR-003 names an ancestor's illustration standing in. R4's numbers, recounted, stand. Each was seen failing first. From #108's review: two of its tests passed with their own fix undone, each rescued by the other (the line test by the tie-break, the tie test by the order the letters were written in); the line test now has two dotted letters, whose dots outvote the f on a line split off, and the tie test writes the mark first, each now failing with its own fix alone undone. Also from it: the scan of comments, and of tags never closed, was quadratic (40,000 comments took 12 s; each now under 250 ms, tested); a close tag ends the element it names, so a stray one ends nothing; the viewport's class is matched whole; element names are known with a prefix or in capitals.
- **From #107's review (#109, replacing #108 after its review):** a dot or an accent met first could begin a line of its own that the letters beneath it missed, and a mark set high won a tied baseline vote; two identical subpaths each hid the other; the scan of tags read comments as markup and only double-quoted attributes, and missed a transform on a link, by style, a nested `<svg>`, and a label kept in `<defs>`. Lines are now the letters whose heights overlap, directly or through others; a tie goes to the lower bottom; only a smaller box within another is a hole; every element is followed, comments and character data left out, attributes in either quotes, and each of those leaves the label unread. The render service's viewport writes its scale by attribute and again by style, to six figures: the two must agree (found when this PR's first version read the real render as unmeasured; the tests' viewport now has both, as the render's does). FR-003 names an ancestor's illustration standing in. R4's numbers, recounted, stand. Each was seen failing first. From #108's review: two of its tests passed with their own fix undone, each rescued by the other (the line test by the tie-break, the tie test by the order the letters were written in); the line test now has two dotted letters, whose dots outvote the f on a line split off, and the tie test writes the mark first, each now failing with its own fix alone undone. Also from it: the scan of comments, and of tags never closed, was quadratic (40,000 comments took 12 s; both now under 250 ms, tested, though a tag left open by an unclosed quote stayed quadratic until #110); a close tag ends the element it names, so a stray one ends nothing; the viewport's class is matched whole; element names are known with a prefix or in capitals.
- **From #109's review (#110):** a tag left open by an unclosed quote still made the scan quadratic (4 s); XML allows no `<` in a tag, so none is matched now, and every such tag ends at the next `<` (tested). A close tag that skips open elements (malformed markup, which could end the viewport and read later labels at the wrong scale) now leaves every label after it unread. A label in a title, a description or metadata is unread, as one in `<defs>` is. Release 98's 221 illustrations and the sample render read exactly as on main. Not looked for, as the render service writes none of it: a style element's rules, `display="none"`, an unquoted value, an entity in an id, a name in capitals taken as SVG's. Each fix was seen failing first.

- [ ] T009 [P] Measure the analysis service's answers (research R6), on a private instance with a submitted analysis, never production. For each case, record the status and body in research R6:
- a good token;
Expand Down
38 changes: 30 additions & 8 deletions src/layout/lettering.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,25 @@ function attribute(attributes: string, name: string): string | undefined {
return match ? (match[1] ?? match[2]) : undefined;
}

/** A tag: its name and attributes, a quoted value holding no `>` of its own. */
const TAG = /<(\/?)([A-Za-z][\w:.-]*)((?:[^>"']|"[^"]*"|'[^']*')*)>/g;
/**
* A tag: its name and attributes, a quoted value holding no `>` of its own. XML allows no `<`
* in a tag, quoted or not, so none is matched: a tag left open, or a quote, ends at the next
* `<`, and the scan stays linear.
*/
const TAG = /<(\/?)([A-Za-z][\w:.-]*)((?:[^<>"']|"[^"<]*"|'[^'<]*')*)>/g;

/** Elements whose content is not drawn where it stands: a label there is unread. */
const UNDRAWN = new Set(['defs', 'symbol', 'clippath', 'mask', 'pattern', 'marker']);
const UNDRAWN = new Set([
'defs',
'symbol',
'clippath',
'mask',
'pattern',
'marker',
'title',
'desc',
'metadata',
]);

/** An element's name without its namespace prefix, in lower case. */
const local = (name: string) => name.slice(name.lastIndexOf(':') + 1).toLowerCase();
Expand Down Expand Up @@ -192,11 +206,13 @@ function withoutComments(svg: string): string {
* style), other than the render service's viewport, whose plain scale fits the illustration
* to the figure's frame and is applied (a viewport that is not a plain scale leaves every
* label inside it unread); or when it is in a nested `<svg>`, or in content not drawn where
* it stands (`<defs>` and the like). The elements are followed through the markup in one
* it stands (`<defs>`, `<symbol>`, a clip path, mask, pattern or marker, a title, a
* description or metadata), or after a close tag that skipped open elements. The elements are followed through the markup in one
* pass of its tags, its comments and character data left out, names known with a prefix or
* in capitals, and a close tag ending the element it names. Markup this does not read as a
* browser would (a style element's rules, an unquoted value, an entity in an id) is not
* looked for: the render service writes none of it.
* browser would (a style element's rules, `display="none"`, an unquoted value, an entity in
* an id, a name in capitals taken as SVG's) is not looked for: the render service writes
* none of it.
*/
export function lettering(svg: string): { smallest: number | null; unread: number } {
let smallest: number | null = null;
Expand All @@ -219,6 +235,9 @@ export function lettering(svg: string): { smallest: number | null; unread: numbe
};
// for each open element: its name, and the scale it applies, or null where it is not known
const open: { name: string; scale: number | null }[] = [];
// a close tag that skipped open elements: the markup is not what it seems, so no label after
// it is read
let malformed = false;
const markup = withoutComments(svg);
// up to the last `>`: past it no tag can end, and a tail of `<` would be searched from each
for (const [, close, name = '', attributes = ''] of markup
Expand All @@ -227,7 +246,10 @@ export function lettering(svg: string): { smallest: number | null; unread: numbe
if (close) {
// it ends the element it names, and any left open inside it; a stray one, nothing
const at = open.map((e) => e.name).lastIndexOf(local(name));
if (at >= 0) open.length = at;
if (at >= 0) {
if (at !== open.length - 1) malformed = true;
open.length = at;
}
continue;
}
const selfClosing = attributes.trimEnd().endsWith('/');
Expand All @@ -238,7 +260,7 @@ export function lettering(svg: string): { smallest: number | null; unread: numbe
const d = attribute(attributes, 'd');
const boxes = d === undefined ? null : outlineBoxes(d);
const cap = boxes === null ? null : capHeight(boxes);
if (scale === null || cap === null || !(cap > 0)) unread += 1;
if (malformed || scale === null || cap === null || !(cap > 0)) unread += 1;
else {
const size = (cap * scale) / CAP_HEIGHT;
if (smallest === null || size < smallest) smallest = size;
Expand Down
18 changes: 18 additions & 0 deletions test/layout/lettering.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ describe("an illustration's lettering", () => {
test('many comments, or tags never closed, are read in linear time', () => {
const label = `<path d="${word(0, 20, 10)}" id="PATHWAY TEXT"/>`;
for (const filler of [
'<a "' + '<a '.repeat(20_000) + "'>",
'<!-- x -->'.repeat(40_000),
'<![CDATA[x]]>'.repeat(40_000),
'<a '.repeat(20_000),
Expand All @@ -277,6 +278,23 @@ describe("an illustration's lettering", () => {
}
});

test('a close tag that skips open elements leaves the labels after it unread', () => {
const d = word(0, 20, 10);
const svg = illustration([]).replace(
'</g></svg>',
`<g></svg><path d="${d}" id="PATHWAY TEXT"/></g></svg>`
);
assert.deepEqual(lettering(svg), { smallest: null, unread: 1 });
});

test('a label in a title, a description or metadata is unread', () => {
const label = `<path d="${word(0, 20, 10)}" id="PATHWAY TEXT"/>`;
for (const name of ['title', 'desc', 'metadata']) {
const svg = illustration([]).replace('</g></svg>', `<${name}>${label}</${name}></g></svg>`);
assert.deepEqual(lettering(svg), { smallest: null, unread: 1 }, name);
}
});

test('an illustration with no label path is still unmeasured', () => {
assert.equal(place(measure(illustration([]))).unmeasured, true);
});
Expand Down
Loading