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

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

Defined Under Namespace

Classes: Type0Entry

Instance Method Summary collapse

Constructor Details

#initialize(source:, mutool_info: Mutool::Info.new, mutool_show: Mutool::Show.new) ⇒ PdfIndexer

Returns a new instance of PdfIndexer.

Parameters:

  • source (PdfSource)
  • mutool_info (Mutool::Info) (defaults to: Mutool::Info.new)

    injectable for tests

  • mutool_show (Mutool::Show) (defaults to: Mutool::Show.new)

    injectable for tests



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

def initialize(source:, mutool_info: Mutool::Info.new,
               mutool_show: Mutool::Show.new)
  @source = source
  @mutool_info = mutool_info
  @mutool_show = mutool_show
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)
  type0_base_fonts.include?(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