Class: Ucode::CodeChart::Verifier::PageRenderCache

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

Overview

Caches per-page PDF→PNG renders so multiple glyphs on the same page share one mutool draw invocation. The Verifier asks for the rendered page; the cache either reuses the existing PNG or renders it on demand.

PNG path layout: <diff_dir>/.cache/page-<N>-<sha>.png where <sha> is a short hash of the PDF path (so two different PDFs with page 1 don't collide).

Instance Method Summary collapse

Constructor Details

#initialize(diff_dir:, strategy:) ⇒ PageRenderCache

Returns a new instance of PageRenderCache.

Parameters:

  • diff_dir (Pathname, String)
  • strategy (Strategy)


19
20
21
22
23
# File 'lib/ucode/code_chart/verifier/page_render_cache.rb', line 19

def initialize(diff_dir:, strategy:)
  @diff_dir = Pathname.new(diff_dir)
  @strategy = strategy
  @cache = {}
end

Instance Method Details

#render_page(pdf_path, page, scale: 2.0) ⇒ Pathname

Returns the rendered page PNG path.

Parameters:

  • pdf_path (Pathname, String)
  • page (Integer)

    1-based page number

  • scale (Float) (defaults to: 2.0)

Returns:

  • (Pathname)

    the rendered page PNG path



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ucode/code_chart/verifier/page_render_cache.rb', line 29

def render_page(pdf_path, page, scale: 2.0)
  key = [pdf_path.to_s, page, scale]
  return @cache[key] if @cache.key?(key)

  @diff_dir.mkpath
  png = @diff_dir.join("page-#{page}-#{short_hash(pdf_path)}.png")
  unless png.exist?
    @strategy.render_pdf_region(
      pdf_path, page,
      full_page_rect, png,
      scale: scale,
    )
  end
  @cache[key] = png
  png
end