Class: Ucode::Glyphs::EmbeddedFonts::Catalog

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

Overview

Walks the Code Charts PDF once and builds a global ‘=> FontEntry` index.

Discovery uses ‘mutool info` for the font list (one line per page-font), then `mutool show -g` to fetch the Type0 font dicts, their descendant CIDFont dicts, and the FontDescriptors — all in a handful of batched subprocess calls rather than one per font.

For each Type0 font we then fetch its ToUnicode CMap stream (one ‘mutool show -b -o <tmpfile>` per font — these can’t be batched because each is a separate stream) and parse it into a ‘=> codepoint` map. With `/CIDToGIDMap /Identity` (the only form we currently support), `gid == cid`, so the per-font map is directly `=> gid`.

When multiple fonts cover the same codepoint (which happens for a handful of codepoints that appear in multiple blocks), the first font discovered wins. The discovery order follows the ‘mutool info` listing, which is page-major, so the earlier block’s font wins — the expected behavior for the Code Charts.

Instance Method Summary collapse

Constructor Details

#initialize(source, correlator_configs: {}) ⇒ Catalog

Returns a new instance of Catalog.

Parameters:

  • source (Source)
  • correlator_configs (Hash{Integer=>ContentStreamCorrelator::Config}) (defaults to: {})

    maps a Type0 font’s PDF object ID to the pillar-2 config to use when the font has no /ToUnicode CMap. Empty by default — fonts without ToUnicode and without a config are skipped (the v0.1 behavior).



40
41
42
43
44
# File 'lib/ucode/glyphs/embedded_fonts/catalog.rb', line 40

def initialize(source, correlator_configs: {})
  @source = source
  @correlator_configs = correlator_configs
  @index = nil
end

Instance Method Details

#codepointsArray<Integer>

Returns every codepoint this PDF covers.

Returns:

  • (Array<Integer>)

    every codepoint this PDF covers



58
59
60
# File 'lib/ucode/glyphs/embedded_fonts/catalog.rb', line 58

def codepoints
  index.keys
end

#font_countInteger

Returns number of Type0 fonts discovered.

Returns:

  • (Integer)

    number of Type0 fonts discovered



68
69
70
# File 'lib/ucode/glyphs/embedded_fonts/catalog.rb', line 68

def font_count
  font_entries.size
end

#font_entriesArray<FontEntry>

Returns every font entry (one per Type0 font).

Returns:

  • (Array<FontEntry>)

    every font entry (one per Type0 font)



73
74
75
# File 'lib/ucode/glyphs/embedded_fonts/catalog.rb', line 73

def font_entries
  @font_entries ||= build_font_entries
end

#indexHash{Integer=>FontEntry}

Returns frozen codepoint → entry map.

Returns:

  • (Hash{Integer=>FontEntry})

    frozen codepoint → entry map



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

def index
  @index ||= build_index.freeze
end

#lookup(codepoint) ⇒ FontEntry?

Parameters:

  • codepoint (Integer)

Returns:



53
54
55
# File 'lib/ucode/glyphs/embedded_fonts/catalog.rb', line 53

def lookup(codepoint)
  index[codepoint]
end

#sizeInteger

Returns number of codepoints covered.

Returns:

  • (Integer)

    number of codepoints covered



63
64
65
# File 'lib/ucode/glyphs/embedded_fonts/catalog.rb', line 63

def size
  index.size
end