class HexaPDF::Type::Font

Represents a generic font object.

This class is the base class for all font objects, be it simple fonts or composite fonts.

Public Instance Methods

bounding_box() click to toggle source

Returns the bounding box of the font or nil if it is not found.

# File lib/hexapdf/type/font.rb, line 81
def bounding_box
  if key?(:FontDescriptor) && self[:FontDescriptor].key?(:FontBBox)
    self[:FontDescriptor][:FontBBox].value
  else
    nil
  end
end
embedded?() click to toggle source

Returns true if the font is embedded.

# File lib/hexapdf/type/font.rb, line 90
def embedded?
  dict = self[:FontDescriptor]
  dict && (dict[:FontFile] || dict[:FontFile2] || dict[:FontFile3])
end
font_file() click to toggle source

Returns the embeeded font file object or nil if the font is not embedded.

# File lib/hexapdf/type/font.rb, line 96
def font_file
  embedded?
end
font_wrapper() click to toggle source

Retrieves the font wrapper that is needed when this font is used for text output. Returns nil if this font can't be used for text output.

Note: For internal use only!

See: HexaPDF::Font

# File lib/hexapdf/type/font.rb, line 58
def font_wrapper
  @font_wrapper ||= nil
end
font_wrapper=(font) click to toggle source

Sets the font wrapper.

See: font_wrapper

# File lib/hexapdf/type/font.rb, line 65
def font_wrapper=(font)
  @font_wrapper = font
end
must_be_indirect?() click to toggle source

Font objects must always be indirect.

# File lib/hexapdf/type/font.rb, line 70
def must_be_indirect?
  true
end
to_utf8(code) click to toggle source

Returns the UTF-8 string for the given character code, or calls the configuration option 'font.on_missing_unicode_mapping' if no mapping was found.

# File lib/hexapdf/type/font.rb, line 76
def to_utf8(code)
  to_unicode_cmap&.to_unicode(code) || missing_unicode_mapping(code)
end

Private Instance Methods

missing_unicode_mapping(code) click to toggle source

Calls the configured proc for handling missing unicode mappings.

# File lib/hexapdf/type/font.rb, line 114
def missing_unicode_mapping(code)
  @document.config['font.on_missing_unicode_mapping'].call(code, self)
end
to_unicode_cmap() click to toggle source

Parses and caches the ToUnicode CMap.

# File lib/hexapdf/type/font.rb, line 103
def to_unicode_cmap
  cache(:to_unicode_cmap) do
    if key?(:ToUnicode)
      HexaPDF::Font::CMap.parse(self[:ToUnicode].stream)
    else
      nil
    end
  end
end