Class: Ucode::Glyphs::EmbeddedFonts::PageTraceCache

Inherits:
Object
  • Object
show all
Defined in:
lib/ucode/glyphs/embedded_fonts/page_trace_cache.rb

Overview

Per-PDF trace cache. Runs mutool trace once per page (1..page_count), parses each result, and stores glyphs grouped by page so each TraceStrategy can correlate positionally without re-tracing.

Replaces the O(F × P) explosion in the pre-cache TraceStrategy, which spawned mutool trace once per page per CID font. For Code Charts PDFs with F trace-needing fonts and P pages, that was F×P subprocess invocations; this cache makes it exactly P.

Why per-page grouping: TraceCorrelator's algorithm clusters label glyphs by Y bucket, and Y positions are page-local.

Lazy: glyphs are only fetched when a strategy first asks for them. PDFs where every font has /ToUnicode never trigger the trace at all.

Instance Method Summary collapse

Constructor Details

#initialize(pdf:, page_count:, mutool: Mutool::Trace.new) ⇒ PageTraceCache

Returns a new instance of PageTraceCache.

Parameters:

  • pdf (Pathname, String)
  • page_count (Integer)

    total pages in the PDF

  • mutool (Mutool::Trace) (defaults to: Mutool::Trace.new)


31
32
33
34
35
# File 'lib/ucode/glyphs/embedded_fonts/page_trace_cache.rb', line 31

def initialize(pdf:, page_count:, mutool: Mutool::Trace.new)
  @pdf = Pathname.new(pdf)
  @page_count = page_count
  @mutool = mutool
end

Instance Method Details

#each_page_for(base_font) {|page, glyphs| ... } ⇒ Boolean, Enumerator

Parameters:

  • base_font (String)

    specimen font BaseFont name

Yield Parameters:

  • page (Integer)

    1-based page number

  • glyphs (Array<TraceGlyph>)

    every glyph on that page (all fonts — TraceCorrelator filters internally)

Returns:

  • (Boolean)

    true if at least one page references the font; false otherwise

  • (Enumerator)

    if no block given



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/ucode/glyphs/embedded_fonts/page_trace_cache.rb', line 51

def each_page_for(base_font)
  return enum_for(:each_page_for, base_font) unless block_given?

  present_in_any = false
  glyphs_by_page.each_with_index do |glyphs, idx|
    next if idx.zero?

    present = glyphs.any? { |g| g.font_name == base_font }
    next unless present

    present_in_any = true
    yield idx, glyphs
  end
  present_in_any
end

#glyphs_by_pageArray<Array<TraceGlyph>>

Returns one Array per page, 1-indexed (index 0 is unused). Each inner array holds every glyph emitted by mutool trace on that page.

Returns:

  • (Array<Array<TraceGlyph>>)

    one Array per page, 1-indexed (index 0 is unused). Each inner array holds every glyph emitted by mutool trace on that page.



40
41
42
# File 'lib/ucode/glyphs/embedded_fonts/page_trace_cache.rb', line 40

def glyphs_by_page
  @glyphs_by_page ||= fetch_glyphs_by_page
end

#references_font?(base_font) ⇒ Boolean

Returns true if any page references this font.

Parameters:

  • base_font (String)

Returns:

  • (Boolean)

    true if any page references this font



69
70
71
72
73
# File 'lib/ucode/glyphs/embedded_fonts/page_trace_cache.rb', line 69

def references_font?(base_font)
  glyphs_by_page.any? do |page_glyphs|
    page_glyphs.any? { |g| g.font_name == base_font }
  end
end