Class: Ucode::Glyphs::EmbeddedFonts::PdfIndexer

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

Overview

Walks the Code Charts PDF once via mutool info + mutool show and builds an Array of RawFontDescriptor — one per Type0 font that has the required descendant CIDFont, FontDescriptor, and FontFile2/3 + Identity CIDToGIDMap.

Pure subprocess + parsing concern. Does NOT resolve codepoint → GID (that's CodepointMapper's job). The descriptor carries every ref the mapper needs to do its work.

Instance Method Summary collapse

Constructor Details

#initialize(source:) ⇒ PdfIndexer

Returns a new instance of PdfIndexer.

Parameters:



19
20
21
# File 'lib/ucode/glyphs/embedded_fonts/pdf_indexer.rb', line 19

def initialize(source:)
  @source = source
end

Instance Method Details

#collect_ref(dict_value, acc) ⇒ Object



47
48
49
50
# File 'lib/ucode/glyphs/embedded_fonts/pdf_indexer.rb', line 47

def collect_ref(dict_value, acc)
  ref = first_ref(dict_value)
  acc << ref if ref
end

#collect_refs(type0_refs, type0_dicts) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/ucode/glyphs/embedded_fonts/pdf_indexer.rb', line 36

def collect_refs(type0_refs, type0_dicts)
  descendant_refs = []
  tounicode_refs = []
  type0_refs.each_key do |font_obj_id|
    d = type0_dicts[font_obj_id] || {}
    collect_ref(d["DescendantFonts"], descendant_refs)
    collect_ref(d["ToUnicode"], tounicode_refs)
  end
  [descendant_refs, tounicode_refs]
end

#fetch_fontdescs(descendant_dicts) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/ucode/glyphs/embedded_fonts/pdf_indexer.rb', line 52

def fetch_fontdescs(descendant_dicts)
  fontdesc_refs = []
  descendant_dicts.each_value do |d|
    collect_ref(d["FontDescriptor"], fontdesc_refs)
  end
  fetch_objects(fontdesc_refs)
end

#font_appears?(base_font) ⇒ Boolean

Returns true if this font appears on any page.

Parameters:

  • base_font (String)

    e.g. "GPJAHB+WolofGaraySansSerif"

Returns:

  • (Boolean)

    true if this font appears on any page



70
71
72
# File 'lib/ucode/glyphs/embedded_fonts/pdf_indexer.rb', line 70

def font_appears?(base_font)
  font_entries_cache.key?(base_font)
end

#page_countInteger

Returns total pages in the PDF.

Returns:

  • (Integer)

    total pages in the PDF



61
62
63
64
65
66
# File 'lib/ucode/glyphs/embedded_fonts/pdf_indexer.rb', line 61

def page_count
  @page_count ||= begin
    m = mutool_info_text.match(/^Pages:\s+(\d+)/)
    m ? m[1].to_i : 1
  end
end

#raw_descriptorsArray<RawFontDescriptor>

Returns:



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ucode/glyphs/embedded_fonts/pdf_indexer.rb', line 24

def raw_descriptors
  type0_refs = discover_type0_fonts
  return [] if type0_refs.empty?

  type0_dicts = fetch_objects(type0_refs.keys)
  descendant_refs, = collect_refs(type0_refs, type0_dicts)
  descendant_dicts = fetch_objects(descendant_refs)
  fontdesc_dicts = fetch_fontdescs(descendant_dicts)

  build_descriptors(type0_refs, type0_dicts, descendant_dicts, fontdesc_dicts)
end