Class: Ucode::CodeChart::Verifier::MutoolStrategy

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

Overview

Strategy backed by mutool draw. Slower than resvg but always available on a system that has the trace pipeline working (mutool is already required elsewhere).

Pixel diff: byte-wise comparison of the two rendered PNGs. Crude (real pixel diff needs ChunkyPNG or vips), but sufficient to catch the regression classes the REQ lists: clipped glyphs, missing paths, wrong-cell content, coordinate-flip errors. Returns the percentage of differing bytes — a useful relative signal even when not a true pixel-perfect diff.

Constant Summary

Constants inherited from Strategy

Strategy::FAIL_THRESHOLD

Instance Method Summary collapse

Constructor Details

#initialize(runner: Ucode::Glyphs::EmbeddedFonts::Mutool::SystemRunner.new) ⇒ MutoolStrategy

Returns a new instance of MutoolStrategy.

Parameters:



23
24
25
26
# File 'lib/ucode/code_chart/verifier/mutool_strategy.rb', line 23

def initialize(runner: Ucode::Glyphs::EmbeddedFonts::Mutool::SystemRunner.new)
  super()
  @runner = runner
end

Instance Method Details

#available?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/ucode/code_chart/verifier/mutool_strategy.rb', line 28

def available?
  system("which mutool >/dev/null 2>&1")
end

#diff(png_a, png_b) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/ucode/code_chart/verifier/mutool_strategy.rb', line 48

def diff(png_a, png_b)
  bytes_a = File.binread(png_a)
  bytes_b = File.binread(png_b)
  return 100.0 if bytes_a.bytesize != bytes_b.bytesize

  same = bytes_a.each_byte.with_index.count do |byte, idx|
    byte == bytes_b.getbyte(idx)
  end
  (100.0 * (1.0 - same.to_f / bytes_a.bytesize)).round(2)
end

#render_pdf_region(pdf_path, page, rect, png_path, scale: 2.0) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/ucode/code_chart/verifier/mutool_strategy.rb', line 39

def render_pdf_region(pdf_path, page, rect, png_path, scale: 2.0)
  dpi = (72 * scale).round
  @runner.run("mutool", "draw", "-o", png_path.to_s,
              "-r", dpi.to_s,
              "-R", format_rect(rect, scale),
              pdf_path.to_s, page.to_s)
  Pathname.new(png_path)
end

#render_svg(svg_path, png_path, scale: 2.0) ⇒ Object



32
33
34
35
36
37
# File 'lib/ucode/code_chart/verifier/mutool_strategy.rb', line 32

def render_svg(svg_path, png_path, scale: 2.0)
  dpi = (72 * scale).round
  @runner.run("mutool", "draw", "-o", png_path.to_s,
              "-r", dpi.to_s, svg_path.to_s)
  Pathname.new(png_path)
end

#write_diff_artifact(png_a, png_b, dest) ⇒ Object



59
60
61
62
63
64
# File 'lib/ucode/code_chart/verifier/mutool_strategy.rb', line 59

def write_diff_artifact(png_a, png_b, dest)
  data_a = File.binread(png_a)
  data_b = File.binread(png_b)
  Pathname.new(dest).binwrite(data_a + data_b)
  Pathname.new(dest)
end