Class: Ucode::Glyphs::EmbeddedFonts::PageTraceCache
- Inherits:
-
Object
- Object
- Ucode::Glyphs::EmbeddedFonts::PageTraceCache
- 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
- #each_page_for(base_font) {|page, glyphs| ... } ⇒ Boolean, Enumerator
-
#glyphs_by_page ⇒ Array<Array<TraceGlyph>>
One Array per page, 1-indexed (index 0 is unused).
-
#initialize(pdf:, page_count:, mutool: Mutool::Trace.new) ⇒ PageTraceCache
constructor
A new instance of PageTraceCache.
-
#references_font?(base_font) ⇒ Boolean
True if any page references this font.
Constructor Details
#initialize(pdf:, page_count:, mutool: Mutool::Trace.new) ⇒ PageTraceCache
Returns a new instance of PageTraceCache.
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
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_page ⇒ Array<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.
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.
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 |