Class: Ucode::CodeChart::Verifier

Inherits:
Object
  • Object
show all
Defined in:
lib/ucode/code_chart/verifier.rb,
lib/ucode/code_chart/verifier/result.rb,
lib/ucode/code_chart/verifier/builder.rb,
lib/ucode/code_chart/verifier/strategy.rb,
lib/ucode/code_chart/verifier/resvg_strategy.rb,
lib/ucode/code_chart/verifier/mutool_strategy.rb,
lib/ucode/code_chart/verifier/page_render_cache.rb

Overview

Pixel-diff verification for extracted SVG glyphs against the source PDF cell. REQ R4.

Strategy chain (OCP)

One renderer strategy per external tool. The strategy exposes three primitive operations: render an SVG to PNG, render a PDF page region to PNG, and compute a pixel-diff percentage between two PNGs. Verifier orchestrates these primitives; it has no knowledge of any specific CLI tool.

Adding a new renderer (cairo, imagemagick, …) = one Strategy subclass + one entry in Builder.pick. Verifier core never changes.

Result types (see Verifier::Result)

* `Pass` — diff < FAIL_THRESHOLD (default 1.0%).
* `Fail` — diff ≥ threshold; carries the percent + diff
artifact path for inspection.
* `Skipped` — extractor produced no `source_page`/`source_cell`
(e.g. ToUnicode-only path, or Last Resort placeholder), so no
honest cell-diff is possible. Surfaces as a warning, NOT a
pass.

Defined Under Namespace

Modules: Builder, Result Classes: MutoolStrategy, PageRenderCache, ResvgStrategy, Strategy

Instance Method Summary collapse

Constructor Details

#initialize(diff_dir:, strategy: nil, threshold: nil) ⇒ Verifier

Returns a new instance of Verifier.

Parameters:



46
47
48
49
50
51
52
53
54
# File 'lib/ucode/code_chart/verifier.rb', line 46

def initialize(diff_dir:, strategy: nil, threshold: nil)
  @strategy = strategy || Builder.pick
  @diff_dir = Pathname.new(diff_dir)
  @threshold = threshold || (Strategy::FAIL_THRESHOLD if @strategy)
  if @strategy
    @page_cache = PageRenderCache.new(diff_dir: @diff_dir.join(".cache"),
                                      strategy: @strategy)
  end
end

Instance Method Details

#available?Boolean

Returns true iff a usable strategy was found.

Returns:

  • (Boolean)

    true iff a usable strategy was found



57
58
59
# File 'lib/ucode/code_chart/verifier.rb', line 57

def available?
  !@strategy.nil? && @strategy.available?
end

#verify(result, pdf_path:) ⇒ Result::Pass, ...

Parameters:

Returns:



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/ucode/code_chart/verifier.rb', line 64

def verify(result, pdf_path:)
  unless @strategy
    return Result::Skipped.new(codepoint: result.codepoint,
                               reason: :no_strategy)
  end

  loc = extract_location(result)
  unless loc
    return Result::Skipped.new(codepoint: result.codepoint,
                               reason: :no_location)
  end

  path = Pathname.new(pdf_path)
  unless path.exist?
    return Result::Skipped.new(codepoint: result.codepoint,
                               reason: :no_pdf)
  end

  verify_with_location(result, path, loc)
end