Class: Ucode::Glyphs::EmbeddedFonts::FontEntry

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

Overview

Value object describing one Type0 font discovered in the Code Charts PDF, plus lazy accessors for its outline data.

A FontEntry is constructed by Catalog during the PDF walk and is the unit of work for the renderer. Each entry owns:

* identity — `base_font` name, font dict object number
* stream refs — object numbers of the FontDescriptor's
FontFile2 (TrueType) / FontFile3 (CFF) and the ToUnicode CMap
* `cid_to_gid_map` — `:identity` (gid == cid) or `:stream`
(we'd need to parse a separate map; not currently supported)
* `codepoint_to_gid` — the per-font map built from the parsed
ToUnicode CMap. Frozen.

The fontisan accessor is built lazily on first #accessor call, and the font program is extracted to the Source cache directory at the same point. Subsequent calls reuse the cached file unless the PDF is newer than the cache.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_font:, font_obj_id:, fontfile_obj_id:, fontfile_kind:, tounicode_obj_id:, cid_to_gid_map:, codepoint_to_gid:, source:) ⇒ FontEntry

Returns a new instance of FontEntry.

Parameters:

  • base_font (String)

    e.g. "CIAIIP+Uni2000Generalpunctuation"

  • font_obj_id (Integer)

    Type0 font dict object number

  • fontfile_obj_id (Integer)

    FontFile2/3 stream object number

  • fontfile_kind (Symbol)

    :ttf (FontFile2) or :cff (FontFile3)

  • tounicode_obj_id (Integer)

    ToUnicode CMap stream object number

  • cid_to_gid_map (Symbol)

    :identity (we only support this)

  • codepoint_to_gid (Hash{Integer=>Integer})

    frozen cp → gid

  • source (Source)

    for cache path + pdf path



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/ucode/glyphs/embedded_fonts/font_entry.rb', line 43

def initialize(base_font:, font_obj_id:, fontfile_obj_id:,
               fontfile_kind:, tounicode_obj_id:, cid_to_gid_map:,
               codepoint_to_gid:, source:)
  @base_font = base_font
  @font_obj_id = font_obj_id
  @fontfile_obj_id = fontfile_obj_id
  @fontfile_kind = fontfile_kind
  @tounicode_obj_id = tounicode_obj_id
  @cid_to_gid_map = cid_to_gid_map
  @codepoint_to_gid = codepoint_to_gid
  @source = source
  @accessor = nil
end

Instance Attribute Details

#base_fontObject (readonly)

Returns the value of attribute base_font.



31
32
33
# File 'lib/ucode/glyphs/embedded_fonts/font_entry.rb', line 31

def base_font
  @base_font
end

#cid_to_gid_mapObject (readonly)

Returns the value of attribute cid_to_gid_map.



31
32
33
# File 'lib/ucode/glyphs/embedded_fonts/font_entry.rb', line 31

def cid_to_gid_map
  @cid_to_gid_map
end

#codepoint_to_gidObject (readonly)

Returns the value of attribute codepoint_to_gid.



31
32
33
# File 'lib/ucode/glyphs/embedded_fonts/font_entry.rb', line 31

def codepoint_to_gid
  @codepoint_to_gid
end

#font_obj_idObject (readonly)

Returns the value of attribute font_obj_id.



31
32
33
# File 'lib/ucode/glyphs/embedded_fonts/font_entry.rb', line 31

def font_obj_id
  @font_obj_id
end

#fontfile_kindObject (readonly)

Returns the value of attribute fontfile_kind.



31
32
33
# File 'lib/ucode/glyphs/embedded_fonts/font_entry.rb', line 31

def fontfile_kind
  @fontfile_kind
end

#fontfile_obj_idObject (readonly)

Returns the value of attribute fontfile_obj_id.



31
32
33
# File 'lib/ucode/glyphs/embedded_fonts/font_entry.rb', line 31

def fontfile_obj_id
  @fontfile_obj_id
end

#sourceObject (readonly)

Returns the value of attribute source.



31
32
33
# File 'lib/ucode/glyphs/embedded_fonts/font_entry.rb', line 31

def source
  @source
end

#tounicode_obj_idObject (readonly)

Returns the value of attribute tounicode_obj_id.



31
32
33
# File 'lib/ucode/glyphs/embedded_fonts/font_entry.rb', line 31

def tounicode_obj_id
  @tounicode_obj_id
end

Instance Method Details

#accessorFontisan::GlyphAccessor

Lazy: extracts the font program to the cache (if missing or stale) and loads it via fontisan. Memoized per FontEntry.

Returns:

  • (Fontisan::GlyphAccessor)


83
84
85
# File 'lib/ucode/glyphs/embedded_fonts/font_entry.rb', line 83

def accessor
  @accessor ||= build_accessor
end

#cache_pathPathname

Returns where the extracted font stream is cached.

Returns:

  • (Pathname)

    where the extracted font stream is cached



75
76
77
# File 'lib/ucode/glyphs/embedded_fonts/font_entry.rb', line 75

def cache_path
  @source.font_cache_path(@base_font, fontfile_extension)
end

#codepointsArray<Integer>

Returns codepoints covered by this font.

Returns:

  • (Array<Integer>)

    codepoints covered by this font



65
66
67
# File 'lib/ucode/glyphs/embedded_fonts/font_entry.rb', line 65

def codepoints
  @codepoint_to_gid.keys
end

#fontfile_extensionString

Returns ".ttf" or ".cff" — cache file extension.

Returns:

  • (String)

    ".ttf" or ".cff" — cache file extension



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

def fontfile_extension
  @fontfile_kind == :ttf ? ".ttf" : ".cff"
end

#gid_for(codepoint) ⇒ Integer?

Returns GID for the codepoint in this font, or nil if the codepoint isn't covered.

Parameters:

  • codepoint (Integer)

Returns:

  • (Integer, nil)

    GID for the codepoint in this font, or nil if the codepoint isn't covered



60
61
62
# File 'lib/ucode/glyphs/embedded_fonts/font_entry.rb', line 60

def gid_for(codepoint)
  @codepoint_to_gid[codepoint]
end

#reset_accessor!void

This method returns an undefined value.

Force-clear the cached accessor and fontisan state. Useful in long-running processes that walk many fonts.



91
92
93
# File 'lib/ucode/glyphs/embedded_fonts/font_entry.rb', line 91

def reset_accessor!
  @accessor = nil
end