diff --git a/CLAUDE.md b/CLAUDE.md index b01b91f..6734024 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -67,10 +67,11 @@ separately (rule and evidence: `docs/measurement.md`). measure of progress; run it after every change. It reports all pairs and the trustworthy ones (`--stale N` lists stale pairs, `--trusted` limits the tables to trustworthy pairs). -- Headline numbers (value coverage, document scores, ranked gaps; defined - in `docs/measurement.md`): `cargo build --release && python3 -I - tools/compare.py --exclude own/`. It prints them last and writes - `pairs.tsv`, `gaps.tsv` and `gaps-all.tsv` to `target/compare/`. +- Headline numbers (value coverage, extra values, document scores, ranked + gaps; defined in `docs/measurement.md`): `cargo build --release && + python3 -I tools/compare.py --exclude own/`. It prints them last and + writes `pairs.tsv`, `gaps*.tsv`, `values*.tsv` and `extras*.tsv` to + `target/compare/`. - `python3 -I tools/diff_outputs.py [OLD [NEW]]`: build two revisions (default `HEAD` and the working tree `.`) under `~/.cache/indd-diff/`, convert every corpus file with both and report which outputs differ diff --git a/docs/measurement.md b/docs/measurement.md index 9f575d4..a26c588 100644 --- a/docs/measurement.md +++ b/docs/measurement.md @@ -27,6 +27,7 @@ A pair is **stale** if any of these holds. The others are **trustworthy**. | INDD saved over an hour after the IDML | Same dates | The INDD's date is more than 1 hour after the IDML's | | No ModifyDate | Same dates | Either file has no `xmp:ModifyDate` | | IDML objects not in the INDD | `Self="u…"` identifiers in the IDML; UIDs in the INDD database (`indd uids`) | The IDML names a UID that the INDD database does not have | +| IDML objects deleted in the INDD | Same identifiers; UIDs whose INDD slot has no class (`indd uids` prints class `-`) | The IDML names such a UID | | INDD stories not in the IDML | The INDD's story list, as converted; the IDML's stories | The INDD has a story that the IDML does not | Pairs whose IDML `DOMVersion` has a different major version from the INDD @@ -39,6 +40,13 @@ in the INDD database. In the other 23, between 1 and 1,361 IDML UIDs are missing from the INDD, so the IDML describes objects that the INDD no longer (or never) had. +**Deleted slots.** A UID can be in the INDD database without a class (the +third tree; `database.md`). In 25 of 654 pairs the IDML names such a +UID: the IDML has an object that the INDD has deleted. In 6 of these 25 +no other signal fires. Their IDMLs name 1 to 185 such objects each, 218 +in all: 116 `PDF`, 54 `Image`, 23 `EPS`, 5 `TextFrame`, 5 `Story`, 2 +`Rectangle`, 2 `Link` and 1 `Page`. + **ModifyDate.** InDesign writes `xmp:ModifyDate` both when it saves an INDD and when it exports an IDML. A document exported and then saved within a few minutes (as when packaging) has a small positive gap. The table counts @@ -46,7 +54,7 @@ pairs by gap (INDD date minus IDML date), split by whether their content visibly differs: the presence signals above fire, or a story's text is less than 90 % similar to the converter's text for the same story (`difflib` ratio, ignoring U+FEFF, which the converter writes for -anchors). +anchors). It was measured before the deleted-slot signal was added. | Gap | Content same | Content differs | |---|---:|---:| @@ -76,13 +84,13 @@ whose minor version is lower than the INDD header's. ### Result -The rule flags 159 of 654 pairs; 495 are trustworthy. +The rule flags 165 of 654 pairs; 489 are trustworthy. | Group | Pairs | Stories with identical text | Pairs with a differing story | |---|---:|---:|---:| -| Trustworthy | 495 | 18,823 of 18,929 (99.4 %) | 49 (10 %) | -| Stale by a presence signal | 33 | 724 of 1,209 (59.9 %) | 22 (67 %) | -| Stale by ModifyDate only (INDD over 1 hour later) | 125 | 5,626 of 5,802 (97.0 %) | 40 (32 %) | +| Trustworthy | 489 | 18,509 of 18,610 (99.5 %) | 47 (10 %) | +| Stale by a presence signal | 51 | 2,000 of 2,547 (78.5 %) | 29 (57 %) | +| Stale by ModifyDate only (INDD over 1 hour later) | 113 | 4,664 of 4,783 (97.5 %) | 35 (31 %) | | Stale by ModifyDate only (IDML newer) | 1 | 12 of 12 | 0 | Spot checks of the pairs flagged by ModifyDate alone show edits made after @@ -130,6 +138,27 @@ the directory). **Document score** is the share of trustworthy pairs whose coverage is at least 99 %, and at least 99.9 %. +### Extra values + +Coverage counts only values that the reference has, so writing more never +lowers it. **Extra values** count the other direction: values the output +has and the reference does not, with the same definitions as above: + +| Extra value | Counted as | +|---|---| +| Attribute or `` child of an element both have | 1, under (element, attribute) | +| Element (or child element, or preference) that only the output has | 1 plus its values, under (element, `(element not in reference)`) | +| Text range that only the output has, in a story whose text is identical | 1 plus its attributes, under (`TextRange`, `(element not in reference)`) | +| Story that only the output has | 1 for its text, plus its text ranges | + +An extra value is not always wrong: IDML leaves out some values that +equal a default, and the converter may write them. It is still output +that the reference does not confirm. The headline prints the number of +extra values and their share of the reference values; +`extras.tsv` (trustworthy pairs) and `extras-all.tsv` list them by key +with the number of documents affected, and `pairs.tsv` has a column for +each pair. Values left out (below) are left out of both counts. + ### Values left out Some values describe the computer that exported the IDML or the IDML @@ -142,6 +171,25 @@ are left out of the counts (`EXCLUDED` in `compare.py`). | `idPkg:Story`, `idPkg:Spread` and the other part references in `designmap.xml` | `src` | The file names of the parts inside the IDML package. The IDML writer chooses them; they are not document content. | | `Assignment` | `Name` | The INDD stores the name of the assignment that holds unassigned InCopy content in the language of the computer that made the document (`Unassigned InCopy Content`, `Contenu InCopy non affecté`, …). IDML writes `$ID/UnassignedInCopy` when the exporting InDesign recognises it (476 of 495 trustworthy pairs) and the stored name otherwise (`objects.md`, assignments). | | `DocumentUser` | `UserColor` | The colour of each user of the document as the exporting InDesign shows it. The INDD stores a colour for each user (`objects.md`, document users), but the IDML colour does not follow from it: users stored with the same colour get different IDML colours in different files. | +| `Link` | `LinkImportTime`, `LinkImportModificationTime` | Local time of the exporting computer. The INDD stores each time as a count of 100 ns intervals since 1601-01-01 (the `LinkImportStamp` text `file ` repeats the first one in 4,686 of 4,686 links). Read as UTC, it differs from the IDML time by a whole number of hours in 9,009 of 9,385 time values. The offset differs between documents (−8 to +13 hours) and, in 71 of 284 documents, between links of one document with the season of the date: the daylight-saving rule of the exporting computer's time zone. The INDD does not store the time zone. | +| `PrintPreference`, `PrintBookletPrintPreference` | `BitmapPrinting` | No byte of the print settings, the preferences object or the document object follows it. It follows the platform of the stored print record (Windows record: 102 `false`, 13 `true`; Cocoa: 213 `true`, 4 `false`; Carbon: 57 `true`, 19 `false`; none: 84 `true`, 2 `false`), so the exporting InDesign most likely evaluates it. | + +Two more rules adjust the comparison for values that depend on the +exporting computer: + +- **Links resolved to another file at export.** When InDesign exports, + it looks for each linked file and writes what it finds. If the IDML's + `LinkResourceURI` differs from the converter's (which comes from the + INDD), the file was found in another folder or replaced by a newer one, + and the link's `LinkResource…` and `LinkImport…` attributes describe + that file, not the INDD. They are left out for that link. The headline + prints how many links this affects (329 in trustworthy pairs). +- **Font technology suffix.** IDML adds ` (OTF)`, ` (TT)` or ` (T1)` to + some font family names, depending on the fonts installed where it was + exported (`fonts.md`; the same family has the suffix in some documents + and not in others, and the strings occur in no INDD file). Family names + (`FontFamily` `Name`, `Font` `FontFamily`, `AppliedFont`, + `BulletsFont`) are compared with that suffix removed from both values. A value enters this list only with a reason of this kind. Values that are hard to decode, or that the converter does not write yet, stay in the @@ -157,6 +205,9 @@ number of documents affected and the number of values wrong and missing. `compare.py` prints the top 30 keys by documents affected (`--gaps N`) and the top 15 by values. +`extras.tsv` and `extras-all.tsv` list the extra values by key the same +way, and `compare.py` prints the top keys by documents affected. + `values.tsv` and `values-all.tsv` list every key with its number of values and how many are reproduced, wrong and missing. Comparing them between two runs shows whether a change reproduces fewer values of any diff --git a/tools/compare.py b/tools/compare.py index e69929a..93476b8 100644 --- a/tools/compare.py +++ b/tools/compare.py @@ -33,9 +33,11 @@ shortfalls and details to trustworthy pairs, --stale N lists stale pairs. Last comes the headline (docs/measurement.md): value coverage, the share of reference values the conversion reproduces, over trustworthy pairs and over all pairs; document scores (pairs with coverage >= 99 % and >= 99.9 %); -and the biggest gaps (--gaps N). pairs.tsv, gaps.tsv and gaps-all.tsv go to ---out (default target/compare), with values.tsv and values-all.tsv, which -list every key with its values and how many are reproduced. +extra values (values the output has and the reference does not); and the +biggest gaps (--gaps N). pairs.tsv, gaps.tsv and gaps-all.tsv go to --out +(default target/compare), with values.tsv and values-all.tsv, which list +every key with its values and how many are reproduced, and extras.tsv and +extras-all.tsv, which list the extra values by key. --shortfalls N lists the N element types and attributes that fall short (missing elements; wrong or missing attribute values; differing story @@ -169,7 +171,9 @@ def idml_modify_date(idml): def stale_reasons(indd_date, idml_date, indd_uids, ref_el, ref_st, our_st): """Why a pair's IDML may not show the same save as its INDD (empty if - there is no sign of that). See docs/measurement.md for the evidence.""" + there is no sign of that). `indd_uids` maps each UID in the INDD + database to its class (None for a slot without a class). See + docs/measurement.md for the evidence.""" out = [] if indd_date is None or idml_date is None: out.append("no ModifyDate") @@ -178,8 +182,10 @@ def stale_reasons(indd_date, idml_date, indd_uids, ref_el, ref_st, our_st): elif indd_date - idml_date > STALE_AFTER: out.append("INDD saved over an hour after the IDML") ref_uids = {int(s[1:], 16) for _, s in ref_el if re.fullmatch(r"u[0-9a-f]+", s)} - if indd_uids is not None and ref_uids - indd_uids: + if indd_uids is not None and ref_uids - indd_uids.keys(): out.append("IDML objects not in the INDD") + if indd_uids is not None and any(indd_uids.get(u, 0) is None for u in ref_uids): + out.append("IDML objects deleted in the INDD") if set(our_st) - set(ref_st): out.append("INDD stories not in the IDML") return out @@ -324,8 +330,32 @@ EXCLUDED = [ "colour the exporting InDesign gives each user, not the colour in the INDD"), ("Assignment", "Name", "the exporting InDesign's name for unassigned content, which depends on its language"), + ("Link", "LinkImportTime", "local time of the exporting computer; the INDD stores UTC"), + ("Link", "LinkImportModificationTime", + "local time of the exporting computer; the INDD stores UTC"), + ("*", "BitmapPrinting", "evaluated by the exporting computer; not stored in the INDD"), ] +# Attributes of a Link that describe the linked file as InDesign found it +# when it exported the IDML. When the IDML's LinkResourceURI differs from +# the converter's (which comes from the INDD), the link was resolved to +# another file on the exporting computer, and these values of that link +# are left out (docs/measurement.md). +RELINKED = re.compile(r"LinkResource.*|LinkImport.*") + +# Font technology suffix that IDML adds to some family names, depending on +# the fonts installed on the exporting computer (docs/measurement.md). +FONT_SUFFIX = re.compile(r" \((OTF|TT|T1)\)$") + + +def font_name(tag, key, v): + """A value with the font technology suffix removed if it names a font + family: FontFamily Name, Font FontFamily, AppliedFont and BulletsFont.""" + if (tag, key) in (("FontFamily", "Name"), ("Font", "FontFamily")) or \ + key.endswith(("P.AppliedFont", "P.BulletsFont")): + return FONT_SUFFIX.sub("", v) + return v + @functools.cache def excluded(tag, key): @@ -356,6 +386,7 @@ def values(tag, el): # Gap keys for values that could not be compared one by one. NO_ELEMENT = "(element not produced)" NO_TEXT = "(story text differs)" +NOT_IN_REFERENCE = "(element not in reference)" ELEMENT = "(element)" @@ -367,11 +398,18 @@ class Stats: # Value coverage: reference values, and those reproduced exactly. self.values = 0 self.reproduced = 0 + # Links whose resource values are left out (RELINKED). + self.relinked = 0 # (tag, key) -> values wrong / missing, and documents affected. self.gap_wrong = Counter() self.gap_missing = Counter() self.gap_docs = Counter() self.gap_total = Counter() + # Values the output has and the reference does not, by (tag, key), + # and documents affected. + self.extra = 0 + self.extra_values = Counter() + self.extra_docs = Counter() # (pair, reproduced, values) for each pair. self.pairs = [] self.found = Counter() @@ -384,7 +422,10 @@ class Stats: self.docs += other.docs self.values += other.values self.reproduced += other.reproduced - for c in ("gap_wrong", "gap_missing", "gap_docs", "gap_total"): + self.extra += other.extra + self.relinked += other.relinked + for c in ("gap_wrong", "gap_missing", "gap_docs", "gap_total", "extra_values", + "extra_docs"): getattr(self, c).update(getattr(other, c)) self.pairs += other.pairs self.found.update(other.found) @@ -412,6 +453,11 @@ class Stats: rows = [(k, d, self.gap_wrong[k], self.gap_missing[k]) for k, d in self.gap_docs.items()] return sorted(rows, key=lambda r: (-r[1], -(r[2] + r[3]), r[0])) + def extras(self): + """(tag, key), documents, extra values; most documents first.""" + rows = [(k, d, self.extra_values[k]) for k, d in self.extra_docs.items()] + return sorted(rows, key=lambda r: (-r[1], -r[2], r[0])) + def story_line(self): st = sum(self.story_ok.values()) return (f"{self.story_ok['ok']}/{st} exact, {self.story_ok['wrong']} differ, " @@ -488,12 +534,14 @@ def compare_pair(name, ref, ours, examples, show): def value_coverage(p, ref, ours): """Count reference values and the reproduced ones into `p`: element presence, attribute and values, story text, and the start - and attributes of each text range.""" + and attributes of each text range. Also count the values the output has + and the reference does not (extra values), with the same keys.""" ref_el, ref_st, ref_rg = ref our_el, our_st, our_rg = ours total = Counter() wrong = Counter() missing = Counter() + extra = Counter() def compare(tag, theirs, mine): for k, v in theirs.items(): @@ -502,8 +550,10 @@ def value_coverage(p, ref, ours): o = mine.get(k) if o is None: missing[key] += 1 - elif norm(o) != norm(v): + elif norm(font_name(tag, k, o)) != norm(font_name(tag, k, v)): wrong[key] += 1 + for k in mine.keys() - theirs.keys(): + extra[(tag, k)] += 1 for (tag, s), el in ref_el.items(): theirs = values(tag, el) @@ -514,7 +564,15 @@ def value_coverage(p, ref, ours): missing[(tag, NO_ELEMENT)] += n continue total[(tag, ELEMENT)] += 1 - compare(tag, theirs, values(tag, mine)) + mine = values(tag, mine) + if tag == "Link" and mine.get("LinkResourceURI") not in (None, theirs.get("LinkResourceURI")): + theirs = {k: v for k, v in theirs.items() if not RELINKED.fullmatch(k)} + mine = {k: v for k, v in mine.items() if not RELINKED.fullmatch(k)} + p.relinked += 1 + compare(tag, theirs, mine) + for (tag, s), el in our_el.items(): + if (tag, s) not in ref_el: + extra[(tag, NOT_IN_REFERENCE)] += 1 + len(values(tag, el)) for sid, text in ref_st.items(): key = ("Story", "(text)") total[key] += 1 @@ -522,6 +580,10 @@ def value_coverage(p, ref, ours): missing[key] += 1 elif our_st[sid] != text: wrong[key] += 1 + for sid in our_st.keys() - ref_st.keys(): + extra[("Story", "(text)")] += 1 + extra[("TextRange", NOT_IN_REFERENCE)] += sum(1 + len(a) for a in + our_rg.get(sid, {}).values()) for sid, rgs in ref_rg.items(): if our_st.get(sid) != ref_st.get(sid): n = sum(1 + len(a) for a in rgs.values()) @@ -537,6 +599,8 @@ def value_coverage(p, ref, ours): continue total[("TextRange", ELEMENT)] += 1 compare("TextRange", attrs, mine[start]) + for start in mine.keys() - rgs.keys(): + extra[("TextRange", NOT_IN_REFERENCE)] += 1 + len(mine[start]) n = sum(total.values()) bad = sum(wrong.values()) + sum(missing.values()) p.values += n @@ -545,6 +609,9 @@ def value_coverage(p, ref, ours): p.gap_wrong.update(wrong) p.gap_missing.update(missing) p.gap_docs.update(k for k in total if wrong[k] or missing[k]) + p.extra += sum(extra.values()) + p.extra_values.update(extra) + p.extra_docs.update(k for k, v in extra.items() if v) def main(): @@ -603,8 +670,12 @@ def main(): r = convert(indd, out) x = subprocess.run([args.bin, "xmp", indd], capture_output=True) u = subprocess.run([args.bin, "uids", indd], capture_output=True, text=True) - uids = ({int(line.split("\t")[0]) for line in u.stdout.splitlines()} - if u.returncode == 0 else None) + uids = None + if u.returncode == 0: + uids = {} + for line in u.stdout.splitlines(): + uid, cls = line.split("\t") + uids[int(uid)] = None if cls == "-" else int(cls, 16) dates = (modify_date(x.stdout) if x.returncode == 0 else None, idml_modify_date(idml)) return r, dates, uids @@ -751,6 +822,13 @@ def write_gaps(path, st): f.write(f"{key[0]}\t{key[1]}\t{docs}\t{w}\t{m}\t{st.gap_total[key]}\n") +def write_extras(path, st): + with open(path, "w") as f: + f.write("tag\tkey\tdocuments\textra\n") + for key, docs, n in st.extras(): + f.write(f"{key[0]}\t{key[1]}\t{docs}\t{n}\n") + + def write_values(path, st): """Every key with its values and how many are reproduced, for checking that a change reproduces no fewer values of any key.""" @@ -768,12 +846,16 @@ def headline(args, every, trusted, pair_rows): out = Path(args.out) out.mkdir(parents=True, exist_ok=True) with open(out / "pairs.tsv", "w") as f: - f.write("path\ttrusted\tcoverage\treproduced\tvalues\tstale reasons\n") + f.write("path\ttrusted\tcoverage\treproduced\tvalues\textra\tstories" + "\tstories_identical\tstale reasons\n") for name, p, reasons in sorted(pair_rows, key=lambda r: r[1].coverage()): f.write(f"{name}\t{not reasons}\t{p.coverage():.5f}\t{p.reproduced}\t{p.values}" + f"\t{p.extra}\t{sum(p.story_ok.values())}\t{p.story_ok['ok']}" f"\t{'; '.join(reasons)}\n") write_gaps(out / "gaps.tsv", trusted) write_gaps(out / "gaps-all.tsv", every) + write_extras(out / "extras.tsv", trusted) + write_extras(out / "extras-all.tsv", every) write_values(out / "values.tsv", trusted) write_values(out / "values-all.tsv", every) print("\nheadline (docs/measurement.md):") @@ -784,6 +866,9 @@ def headline(args, every, trusted, pair_rows): f"{st.document_score(0.99)}/{st.docs} ({st.document_score(0.99) / n:.1%}), " f">= 99.9 %: {st.document_score(0.999)}/{st.docs} " f"({st.document_score(0.999) / n:.1%})") + print(f" extra values (in the output, not in the reference): {st.extra} " + f"({st.extra / (st.values or 1):.2%} of reference values); " + f"links resolved to another file at export: {st.relinked}") print(f" per pair: lowest {st.quantile(0):.1%}, 10th percentile {st.quantile(0.1):.1%}, " f"median {st.quantile(0.5):.1%}, 90th percentile {st.quantile(0.9):.1%}, " f"highest {st.quantile(1):.1%}") @@ -795,6 +880,11 @@ def headline(args, every, trusted, pair_rows): print("\nvalue gaps, trustworthy pairs, by values (documents, values wrong, missing, of values):") for key, docs, w, m in sorted(rows, key=lambda r: -(r[2] + r[3]))[:args.gaps // 2]: print(f" {docs:5} {w:7} {m:7} {trusted.gap_total[key]:8} {gap_name(key)[:90]}") + rows = trusted.extras() + print(f"\nextra values, trustworthy pairs, by documents affected " + f"(documents, extra values; all in {out / 'extras.tsv'}):") + for key, docs, n in rows[:args.gaps // 2]: + print(f" {docs:5} {n:7} {gap_name(key)[:90]}") if __name__ == "__main__":