diff --git a/AGENTS.md b/AGENTS.md index ef34d083..77f78fb5 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -70,7 +70,7 @@ swiftdraw-diff runs three renderers and diffs their PNGs: To score one revision without comparing, drop `compare`: `swiftdraw-diff Samples.bundle --resvg` scores the working copy, `--head 0.30.0` a tag or branch. -A directory is searched recursively, and each file is named by its path inside it (`text/text-anchor/simple-case`), as are its outputs. So the whole [resvg test suite](https://github.com/linebender/resvg/tree/main/crates/resvg/tests/tests) is one run: `swiftdraw-diff /crates/resvg/tests/tests --resvg`. To score against the suite's own PNGs instead, pass `--resvg-png sibling --scale 2` (they are 300×300, and WebKit sizes the 200×200 viewBox-only tests at 150×150 points). Add `--resvg` as well to render the files whose PNG is missing or a different size with the resvg CLI. +A directory is searched recursively, and each file is named by its path inside it (`text/text-anchor/simple-case`), as are its outputs. So the whole [resvg test suite](https://github.com/linebender/resvg/tree/main/crates/resvg/tests/tests) is one run: `swiftdraw-diff /crates/resvg/tests/tests --resvg`. To score against the suite's own PNGs instead, pass `--resvg-png sibling --scale 2` (they are 300×300, and WebKit sizes the 200×200 viewBox-only tests at 150×150 points). Add `--resvg` as well (recommended): the resvg CLI then renders the files whose PNG is missing or a different size, and every file containing ``. The suite's PNGs were drawn with its own Noto fonts, while the CLI is given SwiftDraw's fallback fonts and a 16px default size. With `--resvg-png` alone, text is compared against those PNGs and the summary says so. Each file's resvg source is shown in the summary (`resvg CLI`) and recorded as `resvgSource` (`png` or `cli`) in `scores.json` and `compare.json`. ### Reading the result diff --git a/Tools/swiftdraw-diff/Sources/SwiftDrawDiffCore/Models.swift b/Tools/swiftdraw-diff/Sources/SwiftDrawDiffCore/Models.swift index 8e502801..ccfb82e1 100644 --- a/Tools/swiftdraw-diff/Sources/SwiftDrawDiffCore/Models.swift +++ b/Tools/swiftdraw-diff/Sources/SwiftDrawDiffCore/Models.swift @@ -49,9 +49,17 @@ public struct RenderResult: Codable, Equatable, Sendable { public var rendered: Bool /// Why it did not render. public var error: String? + /// For resvg: where the image came from, "png" (pre-rendered) or "cli". + public var source: String? public static let ok = RenderResult(rendered: true, error: nil) public static func failed(_ error: String) -> RenderResult { RenderResult(rendered: false, error: error) } + + public func from(_ source: String) -> RenderResult { + var r = self + r.source = source + return r + } } /// `scores.json`, written by report mode. @@ -96,6 +104,8 @@ public struct ScoresReport: Codable, Sendable { public var verdict: Verdict? /// True when the file contains text, which is judged by resvg. public var text: Bool? + /// Where the resvg image came from: "png" (pre-rendered) or "cli". + public var resvgSource: String? /// Points, as rendered. public var size: PointSize? public var errors: [String: String]? @@ -167,6 +177,8 @@ public struct CompareReport: Codable, Sendable { public var text: Bool? /// The references the category was decided by ("webkit", "resvg"); the others are for information. public var judgedBy: [String]? + /// Where the resvg image came from: "png" (pre-rendered) or "cli". + public var resvgSource: String? /// Pixels that differ between base and head (exact, unblurred). public var pixelsChanged: Int? /// "rendered" or "failed". diff --git a/Tools/swiftdraw-diff/Sources/SwiftDrawDiffCore/Options.swift b/Tools/swiftdraw-diff/Sources/SwiftDrawDiffCore/Options.swift index f56d6e18..e2bd59ac 100644 --- a/Tools/swiftdraw-diff/Sources/SwiftDrawDiffCore/Options.swift +++ b/Tools/swiftdraw-diff/Sources/SwiftDrawDiffCore/Options.swift @@ -42,10 +42,18 @@ public struct Options: Equatable, Sendable { public var fontSmoothing = false public var html = false public var out: URL? - /// Run the resvg CLI (`--resvg`): the reference, or the fallback for files without a usable `--resvg-png`. + /// Run the resvg CLI (`--resvg`): the reference, or, with `--resvg-png`, the reference for text and + /// for files without a usable PNG. public var resvgCLI = false /// Pre-rendered resvg PNGs (`--resvg-png`). public var resvgPNG: ResvgPNGSource? + /// Whether a file should be scored against its pre-rendered resvg PNG (when the PNG is usable) rather + /// than the CLI. Text goes to the CLI whenever it can: pre-rendered PNGs, like resvg's test suite with + /// its Noto fonts, use other fonts than SwiftDraw's fallbacks, which the CLI is given. + public static func prefersResvgPNG(hasPNGs: Bool, hasCLI: Bool, text: Bool) -> Bool { + hasPNGs && !(text && hasCLI) + } + /// Whether resvg is a reference at all; WebKit only when false. public var usesResvg: Bool { resvgCLI || resvgPNG != nil } public var swiftdrawPath: URL? @@ -153,8 +161,11 @@ public struct Options: Equatable, Sendable { WebKit always (Tools/webkit-render, built on first use). It also decides each SVG's size. --resvg also score against resvg (on PATH; brew install resvg) --resvg-png sibling|DIR also score against pre-rendered PNGs: foo.png beside foo.svg, or in DIR. - A PNG must be the compare size; with --resvg too, files whose PNG is - missing or another size are rendered with the resvg CLI instead. + A PNG must be the compare size. With --resvg too, the resvg CLI renders + files whose PNG is missing or another size, and every file with : + pre-rendered PNGs use their own fonts (resvg's suite uses Noto), while the + CLI uses SwiftDraw's fallback fonts. Without --resvg, text is compared + against the PNGs, and the summary says so. resvg's bundled test PNGs are 300×300 and WebKit sizes its 200×200 viewBox-only tests at 150×150 pt, so they need --scale 2 --match 1 percent below which two images count as the same in verdicts diff --git a/Tools/swiftdraw-diff/Sources/swiftdraw-diff/Compare.swift b/Tools/swiftdraw-diff/Sources/swiftdraw-diff/Compare.swift index a0ca92f8..cda97dd8 100644 --- a/Tools/swiftdraw-diff/Sources/swiftdraw-diff/Compare.swift +++ b/Tools/swiftdraw-diff/Sources/swiftdraw-diff/Compare.swift @@ -128,6 +128,7 @@ struct Comparer { // the wrong size, or WebKit failed): moving away from the one left isn't proof of a regression. let missingReference = session.resvg != nil && judgedBy.count < 2 && !(text && e.deltaResvg != nil) e.judgedBy = judgedBy.isEmpty ? nil : judgedBy.map(\.0) + e.resvgSource = r.resvg?.source e.category = CompareCategory.of( baseRendered: baseImage != nil, headRendered: headImage != nil, deltas: judgedBy.map(\.1), pixelsChanged: e.pixelsChanged ?? 0, tolerance: options.tolerance, @@ -149,7 +150,7 @@ struct Comparer { print("\nReferences: \(refs) · \(report.os) · font smoothing \(report.fontSmoothing ? "on" : "off") · tolerance \(String(format: "%g", report.tolerance)) pts") let textCount = report.results.filter { $0.text == true }.count if report.textReference == "resvg" { - print("Text: judged by resvg only (\(textCount) file\(textCount == 1 ? "" : "s") with ); WebKit offsets middle- and end-anchored text, so its score is for information") + printTextSummary(sources: report.results.filter { $0.text == true }.map(\.resvgSource), pngs: options.resvgPNG != nil) } print("") let width = max(10, report.results.map(\.name.count).max() ?? 10) @@ -169,7 +170,10 @@ struct Comparer { if let b = e.baseVerdict, let h = e.headVerdict, b != h { s += " \(b.label) → \(h.label)" } } if let px = e.pixelsChanged, px > 0 { s += " \(px) px changed" } - if e.text == true { s += " [text: resvg]" } + if e.text == true || e.resvgSource == "cli" && options.resvgPNG != nil { + s += " [" + [e.text == true ? "text" : nil, options.resvgPNG == nil ? nil : e.resvgSource.map { "resvg \($0.uppercased())" }] + .compactMap { $0 }.joined(separator: ", ") + "]" + } if [.error, .regressed, .noSecondReference].contains(e.category), let errors = e.errors { s += " (" + errors.sorted { $0.key < $1.key }.map { "\($0.key): \($0.value)" }.joined(separator: "; ") + ")" } diff --git a/Tools/swiftdraw-diff/Sources/swiftdraw-diff/Renderers.swift b/Tools/swiftdraw-diff/Sources/swiftdraw-diff/Renderers.swift index e3dabf28..a5bc3d6a 100644 --- a/Tools/swiftdraw-diff/Sources/swiftdraw-diff/Renderers.swift +++ b/Tools/swiftdraw-diff/Sources/swiftdraw-diff/Renderers.swift @@ -254,8 +254,8 @@ struct ResvgRenderer: Sendable { let binary: URL if let path = options.resvgPath { binary = path } else { binary = try await resolve("resvg") } let id = RenderCache.hash("resvg", version, fontArguments.joined(separator: " ")) - labels.append("resvg \(version)") - return ResvgRenderer(png: options.resvgPNG, cli: binary, label: labels.joined(separator: ", else "), cacheID: "resvg-\(id)") + labels.append(labels.isEmpty ? "resvg \(version)" : "resvg \(version) for text and unusable PNGs") + return ResvgRenderer(png: options.resvgPNG, cli: binary, label: labels.joined(separator: ", "), cacheID: "resvg-\(id)") } private static func resolve(_ name: String) async throws -> URL { @@ -265,10 +265,12 @@ struct ResvgRenderer: Sendable { /// Renders (or copies) one file's image to `png` at `pixels`. A pre-rendered PNG is used only when it /// is exactly `pixels`; otherwise the CLI renders it, or, without the CLI, the file has no resvg image. + /// With the CLI, text is always rendered by it: pre-rendered PNGs (such as resvg's test suite, drawn + /// with its own Noto fonts) use other fonts than SwiftDraw's fallbacks. func render(_ file: InputFile, pixels: (width: Int, height: Int), scale: Int, cache: RenderCache, to png: URL) async -> RenderResult { let fm = FileManager.default var unusable: String? - if let source = self.png { + if let source = self.png, Options.prefersResvgPNG(hasPNGs: true, hasCLI: cli != nil, text: file.text) { let src: URL switch source { case let .directory(dir): src = dir.appendingPathComponent("\(file.name).png") @@ -280,12 +282,12 @@ struct ResvgRenderer: Sendable { unusable = "\(src.lastPathComponent) is \(size.width)×\(size.height), expected \(pixels.width)×\(pixels.height)" + Self.scaleHint(png: size, pixels: pixels, scale: scale) } else { - do { try fm.copyItem(at: src, to: png) } catch { return .failed("\(error)") } - return .ok + do { try fm.copyItem(at: src, to: png) } catch { return .failed("\(error)").from("png") } + return RenderResult.ok.from("png") } } - guard let cli else { return .failed(unusable ?? "no resvg image") } - return await render(file, cli: cli, pixels: pixels, cache: cache, to: png) + guard let cli else { return .failed(unusable ?? "no resvg image").from("png") } + return await render(file, cli: cli, pixels: pixels, cache: cache, to: png).from("cli") } /// The `--scale` that would make a pre-rendered PNG fit, when there is one. diff --git a/Tools/swiftdraw-diff/Sources/swiftdraw-diff/Report.swift b/Tools/swiftdraw-diff/Sources/swiftdraw-diff/Report.swift index 2d0113cd..a7bcbe06 100644 --- a/Tools/swiftdraw-diff/Sources/swiftdraw-diff/Report.swift +++ b/Tools/swiftdraw-diff/Sources/swiftdraw-diff/Report.swift @@ -104,6 +104,7 @@ struct Reporter { // With resvg, text is judged by resvg alone: WebKit's path offsets middle- and end-anchored text. let text = session.resvg != nil && r.file.text if text { score.text = true } + score.resvgSource = r.resvg?.source if let w = score.score, let s = score.resvg, let wr = score.webkitResvg { score.verdict = Verdict(webkit: w, resvg: s, webkitResvg: wr, match: options.match / 100, text: text) } @@ -126,9 +127,8 @@ struct Reporter { let width = max(8, results.map(\.name.count).max() ?? 8) let refs = [report.engine, report.resvg].compactMap { $0 }.joined(separator: " and ") print("\nDifference from \(refs) · \(report.os) · font smoothing \(report.fontSmoothing ? "on" : "off")") - let textCount = report.results.filter { $0.text == true }.count if report.textReference == "resvg" { - print("Text: judged by resvg only (\(textCount) file\(textCount == 1 ? "" : "s") with , marked *); WebKit offsets middle- and end-anchored text, so its score is for information") + printTextSummary(sources: report.results.filter { $0.text == true }.map(\.resvgSource), pngs: options.resvgPNG != nil, marked: true) } print("") var header = "file".padded(width) + " WebKit" @@ -140,6 +140,7 @@ struct Reporter { if hasResvg { line += " " + percent(s.resvg, s.errors?["resvg"] != nil) + " " + percent(s.webkitResvg, false).leftPadded(12) line += " " + (s.verdict?.label ?? "") + (s.text == true ? " *" : "") + if options.resvgPNG != nil, s.resvgSource == "cli" { line += " (resvg CLI)" } } if s.status == "swiftdraw-failed" { line = s.name.padded(width) + " SwiftDraw failed: " + (s.errors?["swiftdraw"] ?? "?") @@ -176,6 +177,21 @@ struct Reporter { } } +/// The text rule, and where text files' resvg images came from. `sources` holds each text file's resvg +/// source ("png", "cli" or nil); `pngs` is whether pre-rendered PNGs were given. +func printTextSummary(sources: [String?], pngs: Bool, marked: Bool = false) { + func files(_ n: Int) -> String { "\(n) file\(n == 1 ? "" : "s")" } + print("Text: judged by resvg only (\(files(sources.count)) with \(marked ? ", marked *" : "")); WebKit offsets middle- and end-anchored text, so its score is for information") + guard pngs else { return } + let cli = sources.filter { $0 == "cli" }.count, png = sources.filter { $0 == "png" }.count + if cli > 0 { + print(" \(files(cli)) with text rendered by the resvg CLI with SwiftDraw's fallback fonts, not from their PNG") + } + if png > 0 { + print(" \(files(png)) with text compared against pre-rendered PNGs, which may use other fonts (resvg's suite uses Noto); add --resvg to render text with the CLI") + } +} + func percent(_ value: Double?, _ failed: Bool) -> String { if let value { return String(format: "%11.2f%%", value * 100) } return (failed ? "failed" : "–").leftPadded(12) diff --git a/Tools/swiftdraw-diff/Tests/SwiftDrawDiffCoreTests/OptionsTests.swift b/Tools/swiftdraw-diff/Tests/SwiftDrawDiffCoreTests/OptionsTests.swift index 7bf94eca..5ca79abf 100644 --- a/Tools/swiftdraw-diff/Tests/SwiftDrawDiffCoreTests/OptionsTests.swift +++ b/Tools/swiftdraw-diff/Tests/SwiftDrawDiffCoreTests/OptionsTests.swift @@ -63,6 +63,18 @@ struct OptionsTests { #expect(!o.useCache) } + @Test(arguments: [ + // hasPNGs, hasCLI, text, expected + (true, true, false, true), // PNG when usable, the CLI otherwise + (true, true, true, false), // text always goes to the CLI: the PNGs use other fonts + (true, false, true, true), // no CLI: text is compared against the PNG, and the summary says so + (false, true, false, false), + (false, true, true, false), + ]) + func resvgPNGOrCLI(hasPNGs: Bool, hasCLI: Bool, text: Bool, expected: Bool) { + #expect(Options.prefersResvgPNG(hasPNGs: hasPNGs, hasCLI: hasCLI, text: text) == expected) + } + @Test(arguments: [ [], // no inputs ["missing.svg"], // no such file (and not a command)