Class: HexaPDF::Font::TrueTypeWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/hexapdf/font/true_type_wrapper.rb

Overview

This class wraps a generic TrueType font object and provides the methods needed for working with the font in a PDF context.

TrueType fonts can be represented in two ways in PDF: As a simple font with Subtype TrueType or as a composite font using a Type2 CIDFont. The wrapper only supports the composite font case because:

  • By using a composite font more than 256 characters can be encoded with one font object.

  • Fonts for vertical writing can potentially be used.

  • The PDF specification recommends using a composite font (see PDF1.7 s9.9 at the end).

Additionally, TrueType fonts are always embedded.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document, font) ⇒ TrueTypeWrapper

Creates a new object wrapping the TrueType font for the PDF document.



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/hexapdf/font/true_type_wrapper.rb', line 91

def initialize(document, font)
  @document = document
  @wrapped_font = font

  @cmap = font[:cmap].preferred_table
  if @cmap.nil?
    raise HexaPDF::Error, "No mapping table for Unicode characters found for TTF " \
      "font #{font.full_name}"
  end
  @dict = build_font_dict
  @document.register_listener(:complete_objects, &method(:complete_font_dict))

  @id_to_glyph = {}
  @codepoint_to_glyph = {}
  @encoded_glyphs = {}
end

Instance Attribute Details

#dictObject (readonly)

Returns the PDF font dictionary representing the wrapped font.



88
89
90
# File 'lib/hexapdf/font/true_type_wrapper.rb', line 88

def dict
  @dict
end

#wrapped_fontObject (readonly)

Returns the wrapped TrueType font object.



85
86
87
# File 'lib/hexapdf/font/true_type_wrapper.rb', line 85

def wrapped_font
  @wrapped_font
end

Instance Method Details

#decode_utf8(str) ⇒ Object

Returns an array of glyph objects representing the characters in the UTF-8 encoded string.



122
123
124
125
126
127
128
129
130
# File 'lib/hexapdf/font/true_type_wrapper.rb', line 122

def decode_utf8(str)
  str.each_codepoint.map do |c|
    @codepoint_to_glyph[c] ||=
      begin
        gid = @cmap[c] || @document.config['font.on_missing_glyph'].call(c, @wrapped_font)
        glyph(gid)
      end
  end
end

#encode(glyph) ⇒ Object

Encodes the glyph and returns the code string.



133
134
135
# File 'lib/hexapdf/font/true_type_wrapper.rb', line 133

def encode(glyph)
  @encoded_glyphs[glyph] ||= [glyph.id].pack('n')
end

#glyph(id) ⇒ Object

Returns a Glyph object for the given glyph ID.

Note: Although this method is public, it should normally not be used by application code!



111
112
113
114
115
116
117
118
119
# File 'lib/hexapdf/font/true_type_wrapper.rb', line 111

def glyph(id)
  @id_to_glyph[id] ||=
    begin
      if id < 0 || id >= @wrapped_font[:maxp].num_glyphs
        id = @document.config['font.on_missing_glyph'].call(0xFFFD, @wrapped_font)
      end
      Glyph.new(@wrapped_font, id)
    end
end