Class: Ucode::CodeChart::Verifier::Strategy

Inherits:
Object
  • Object
show all
Defined in:
lib/ucode/code_chart/verifier/strategy.rb

Overview

Abstract base for one verification-renderer strategy. Each subclass owns ONE external CLI tool (resvg, mutool, etc.) and implements the three primitives the Ucode::CodeChart::Verifier needs.

Subclasses must implement:

* {#available?} — is the underlying tool installed?
* {#render_svg} — render an SVG to a PNG at the given scale.
* {#render_pdf_region} — render a rectangular region of one
PDF page to a PNG at the given scale.
* {#diff} — pixel-diff percentage between two PNGs.

Adding a new renderer = one subclass + one entry in Builder. No edit to Ucode::CodeChart::Verifier.

Direct Known Subclasses

MutoolStrategy, ResvgStrategy

Constant Summary collapse

FAIL_THRESHOLD =

Diff percentage at or above which a glyph is considered visually different from the source. Tuned to absorb anti-aliasing differences across renderers; callers can override via the Ucode::CodeChart::Verifier constructor.

1.0

Instance Method Summary collapse

Instance Method Details

#available?Boolean

Returns true iff the underlying tool is on PATH.

Returns:

  • (Boolean)

    true iff the underlying tool is on PATH

Raises:

  • (NotImplementedError)


29
30
31
# File 'lib/ucode/code_chart/verifier/strategy.rb', line 29

def available?
  raise NotImplementedError
end

#diff(_png_a, _png_b) ⇒ Float

Returns percentage of differing pixels (0.0–100.0).

Parameters:

  • png_a (Pathname, String)
  • png_b (Pathname, String)

Returns:

  • (Float)

    percentage of differing pixels (0.0–100.0)

Raises:

  • (NotImplementedError)


55
56
57
# File 'lib/ucode/code_chart/verifier/strategy.rb', line 55

def diff(_png_a, _png_b)
  raise NotImplementedError
end

#render_pdf_region(_pdf_path, _page, _rect, _png_path, scale: 2.0) ⇒ Pathname

Parameters:

  • pdf_path (Pathname, String)

    source PDF

  • page (Integer)

    1-based page number

  • rect (Hash{Symbol=>Float})

    {x:, y:, w:, h:} in PDF user space (origin bottom-left) of the region to render.

  • png_path (Pathname, String)

    destination PNG

  • scale (Float) (defaults to: 2.0)

    zoom factor

Returns:

  • (Pathname)

Raises:

  • (NotImplementedError)


48
49
50
# File 'lib/ucode/code_chart/verifier/strategy.rb', line 48

def render_pdf_region(_pdf_path, _page, _rect, _png_path, scale: 2.0)
  raise NotImplementedError
end

#render_svg(_svg_path, _png_path, scale: 2.0) ⇒ Pathname

Returns the written PNG path.

Parameters:

  • svg_path (Pathname, String)

    source SVG

  • png_path (Pathname, String)

    destination PNG

  • scale (Float) (defaults to: 2.0)

    zoom factor (2.0 = 200%)

Returns:

  • (Pathname)

    the written PNG path

Raises:

  • (NotImplementedError)


37
38
39
# File 'lib/ucode/code_chart/verifier/strategy.rb', line 37

def render_svg(_svg_path, _png_path, scale: 2.0)
  raise NotImplementedError
end

#write_diff_artifact(_png_a, _png_b, _dest) ⇒ Pathname?

Optional: produce a visual diff artifact (e.g. side-by-side or red/blue overlay). When unsupported, returns nil and the Ucode::CodeChart::Verifier skips writing the artifact.

Parameters:

  • png_a (Pathname, String)
  • png_b (Pathname, String)
  • dest (Pathname, String)

Returns:

  • (Pathname, nil)


67
68
69
# File 'lib/ucode/code_chart/verifier/strategy.rb', line 67

def write_diff_artifact(_png_a, _png_b, _dest)
  nil
end