Class: Ucode::Glyphs::EmbeddedFonts::CodepointMapper::TraceStrategy

Inherits:
Strategy
  • Object
show all
Defined in:
lib/ucode/glyphs/embedded_fonts/codepoint_mapper/trace_strategy.rb

Overview

Strategy 3 — auto-detect via mutool trace. Last-resort fallback for CID fonts without /ToUnicode and without a caller-supplied correlator config.

Consumes a PageTraceCache that traces each PDF page exactly once across all CID fonts. The pre-cache path was O(F × P) subprocess invocations; this is O(P).

Correlates per page (Y positions are page-local — clustering across page boundaries would produce false matches).

Instance Method Summary collapse

Constructor Details

#initialize(cache:, indexer:) ⇒ TraceStrategy

Returns a new instance of TraceStrategy.

Parameters:

  • cache (PageTraceCache, nil)

    nil = strategy is a no-op

  • indexer (PdfIndexer)

    for the cheap font_appears? precondition (avoids touching the cache for fonts the PDF doesn't reference at all)



27
28
29
30
31
# File 'lib/ucode/glyphs/embedded_fonts/codepoint_mapper/trace_strategy.rb', line 27

def initialize(cache:, indexer:)
  super()
  @cache = cache
  @indexer = indexer
end

Instance Method Details

#map(descriptor) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/ucode/glyphs/embedded_fonts/codepoint_mapper/trace_strategy.rb', line 45

def map(descriptor)
  return {} unless @cache

  correlator = TraceCorrelator.new(
    specimen_font_name: descriptor.base_font,
  )
  mapping = {}
  @cache.each_page_for(descriptor.base_font) do |_page, glyphs|
    page_mapping = correlator.correlate(glyphs)
    page_mapping.each do |cp, gid|
      mapping[cp] ||= gid
    end
  end
  mapping
end

#positional?Boolean

Returns:

  • (Boolean)

See Also:



41
42
43
# File 'lib/ucode/glyphs/embedded_fonts/codepoint_mapper/trace_strategy.rb', line 41

def positional?
  true
end

#supports?(descriptor) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
36
37
38
# File 'lib/ucode/glyphs/embedded_fonts/codepoint_mapper/trace_strategy.rb', line 33

def supports?(descriptor)
  return false unless @cache
  return false unless descriptor.cid_map_kind == :identity

  @indexer.font_appears?(descriptor.base_font)
end