freepdfeditor/spike/B1_glyph_unicode/main.cpp

50 lines
1.9 KiB
C++

// SPDX-License-Identifier: GPL-3.0-or-later
// SPDX-FileCopyrightText: 2025 ai-ad4 and the FreePDFEditor contributors
//
// Spike B step 1 main: run the glyph→Unicode ladder on a real PDF's embedded
// font and report coverage. Exit 0 if the ToUnicode path maps at least one
// glyph (proving the priority ladder's step 1 works on a real font); 1
// otherwise. The coverage and confidence numbers go in the JSON report's notes.
//
// Usage:
// spike_b1_glyph_unicode <pdf_path>
//
// Defaults to a system PDF if none given.
#include "GlyphUnicode.h"
#include "../common/SpikeRunner.h"
#include <cstdio>
#include <cstdlib>
#include <string>
int main(int argc, char** argv)
{
const std::string pdf_path = (argc >= 2) ? argv[1]
: "/usr/share/doc/shared-mime-info/shared-mime-info-spec.pdf";
auto r = freepdfeditor::spike::b1::map_glyphs(pdf_path);
freepdfeditor::spike::SpikeResult sr{};
sr.spike = "B1";
sr.name = "glyph→Unicode via ToUnicode + cmap fallback";
sr.total = r.mappings.size();
sr.passed = r.mapped_via_tounicode;
sr.failed = r.mapped_via_cmap;
sr.errored = r.ok ? 0 : 1;
sr.metric_name = "tounicode_coverage";
sr.metric_value = r.coverage;
sr.target = 0.0; // any ToUnicode mapping proves the ladder; no fixed gate
sr.gate_met = r.ok && r.mapped_via_tounicode > 0;
char buf[512];
std::snprintf(buf, sizeof(buf),
"pdf=%s glyphs=%zu via_tounicode=%zu via_cmap=%zu unmapped=%zu "
"coverage=%.3f avg_confidence=%.3f; §4.1 step 1 ladder (step 1 ToUnicode, "
"step 4 cmap fallback) exercised on a real embedded font",
pdf_path.c_str(), r.mappings.size(),
r.mapped_via_tounicode, r.mapped_via_cmap, r.unmapped,
r.coverage, r.avg_confidence);
sr.notes = buf;
if (!r.ok) sr.notes += "; " + r.error;
return freepdfeditor::spike::emit_json_report(sr);
}