fix(compare): count text ranges inside wrapper elements

text_ranges read only ParagraphStyleRange children of the story and their
CharacterStyleRange children. Ranges inside HyperlinkTextSource,
CrossReferenceSource, XMLElement, Change and EndnoteRange were skipped,
and their text was left out of the offsets, so every later range of the
story started earlier in the reference than in the output. It now walks
through those wrappers at any depth.

Trustworthy pairs (compare.py --exclude own/):
- hyperlink and cross-reference sources alone: reference values
  11,142,835 -> 11,158,348, reproduced 10,501,458 -> 10,505,081,
  extra values 30,237 -> 26,634
- with XMLElement, Change and EndnoteRange (counted as the headline must
  include everything InDesign writes): reference values 11,508,691,
  coverage 94.24 % -> 91.28 %; 348,306 of the 350,343 new values are in
  one document
- story text unchanged (18,517 of 18,610); documents >= 99 %: 18
- three keys reproduce one value fewer (CSR.Underline, PSR.Justification,
  PSR.P.TabList): ranges that matched at a shifted offset before
This commit is contained in:
aiongg committed 2026-10-08 00:51:12 +00:00
1 parent adc397f871
commit 52a32e9b85
2 files changed
+73 -14

No files matched your search

+34 -1
View File
@@ -122,13 +122,46 @@ A **value** is one of these, in the reference IDML:
| Attribute | Attribute other than `Self` | The output's attribute is equal, with numbers compared to 6 significant digits |
| `<Properties>` child | Child of `Properties` (`P.Name`) | Equal text and attributes; a structured child (`PathGeometry`, lists) is compared as a whole |
| Story text | Story | The text, with paragraph breaks, is identical |
| Text range | Start of a paragraph and character style range | The output has a range starting at the same offset |
| Text range | Start of a paragraph and character style range, at any depth inside wrappers (below) | The output has a range starting at the same offset |
| Text range attribute | Attribute or `Properties` child of the range | Equal |
When an element is not produced, its presence and all its values count as
not reproduced. When a story's text differs, its text ranges cannot be
lined up, so all their values count as not reproduced.
#### Text ranges inside wrappers
IDML puts some elements around text ranges or around text inside a range:
`HyperlinkTextSource`, `CrossReferenceSource`, `XMLElement`, `Change` and
`EndnoteRange`. They hold ranges at any level, for example
`ParagraphStyleRange > HyperlinkTextSource > CharacterStyleRange`,
`XMLElement > ParagraphStyleRange` at story level, or
`CharacterStyleRange > Change > ParagraphStyleRange` for a tracked change.
`compare.py` looks through them: every `ParagraphStyleRange` and
`CharacterStyleRange` of the story counts, and the text inside a wrapper
counts towards the offsets. Offsets count `Content` in UTF-16 code units
and every other element (`Br`, `Table`, `Footnote`, an anchored frame) as
one position; `Properties` and `XMLAttribute` count nothing. When a range
nested in a wrapper starts at the same offset as the range around it, the
nested range is the one compared. The wrapper elements themselves have a
`Self` and count as elements.
Counting them changed the trustworthy totals as follows:
| Ranges counted | Reference values | Reproduced | Coverage | Extra values |
|---|---:|---:|---:|---:|
| Only `ParagraphStyleRange` children of the story and their `CharacterStyleRange` children | 11,142,835 | 10,501,458 | 94.24 % | 30,237 |
| Also inside `HyperlinkTextSource` and `CrossReferenceSource` | 11,158,348 | 10,505,081 | 94.15 % | 26,634 |
| Also inside `XMLElement`, `Change` and `EndnoteRange` | 11,508,691 | 10,505,081 | 91.28 % | 26,634 |
Before, the skipped ranges were not counted, and every later range of the
story started at a smaller offset in the reference than in the output: the
converter's ranges were counted as extra and the reference's as missing.
The 350,343 values inside `XMLElement`, `Change` and `EndnoteRange` come
from 23 trustworthy pairs; one document with `XMLElement` around its text
has 348,306 of them. They count although the converter does not reproduce
them yet, because the headline counts everything InDesign writes.
**Value coverage** is reproduced values divided by all values, summed over
pairs, so each value counts once and large documents weigh more. Per-pair
coverage is the same ratio within one pair. `target/compare/pairs.tsv`
+39 -13
View File
@@ -210,23 +210,49 @@ def validate(outs, args):
return errs
# Elements that wrap part of a story's text without being text themselves.
# IDML puts them around ranges (ParagraphStyleRange > HyperlinkTextSource >
# CharacterStyleRange, XMLElement > ParagraphStyleRange) or inside a range
# around text; text_ranges looks through them (docs/measurement.md).
WRAPPERS = {"XMLElement", "HyperlinkTextSource", "CrossReferenceSource", "Change",
"EndnoteRange"}
# Children that are neither text nor a character position.
NOT_TEXT = {"Properties", "XMLAttribute"}
def text_ranges(story):
"""Map text offset -> attributes of the paragraph and character range
starting there, with keys prefixed PSR. and CSR."""
starting there, with keys prefixed PSR. and CSR. Ranges inside wrapper
elements count, at whatever depth. Offsets count Content in UTF-16 code
units and every other element (Br, Table, Footnote, an anchored frame) as
one position."""
out = {}
pos = 0
for psr in story.findall("ParagraphStyleRange"):
pa = {"PSR." + k: v for k, v in props(psr).items()}
for csr in psr.findall("CharacterStyleRange"):
start = pos
for el in csr:
if el.tag == "Content":
pos += len((el.text or "").encode("utf-16-le")) // 2
elif el.tag != "Properties":
pos += 1
if pos > start:
ca = {"CSR." + k: v for k, v in props(csr).items()}
out[start] = {**pa, **ca}
def walk(el, pa):
"""Walk the children of `el` inside the paragraph attributes `pa`."""
nonlocal pos
for ch in el:
if ch.tag in NOT_TEXT:
continue
if ch.tag == "ParagraphStyleRange":
walk(ch, {"PSR." + k: v for k, v in props(ch).items()})
elif ch.tag == "CharacterStyleRange":
start = pos
walk(ch, pa)
if pos > start:
ca = {"CSR." + k: v for k, v in props(ch).items()}
# A range nested at the same offset (Change > range at
# the start of this one) keeps the offset.
out.setdefault(start, {**pa, **ca})
elif ch.tag in WRAPPERS:
walk(ch, pa)
elif ch.tag == "Content":
pos += len((ch.text or "").encode("utf-16-le")) // 2
else:
pos += 1
walk(story, {})
return out