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, subset: true) ⇒ TrueTypeWrapper

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

If subset is true, the font is subset.



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

def initialize(document, font, subset: true)
  @document = document
  @wrapped_font = font
  @subsetter = (subset ? HexaPDF::Font::TrueType::Subsetter.new(font) : nil)

  @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.



130
131
132
133
134
135
136
137
138
# File 'lib/hexapdf/font/true_type_wrapper.rb', line 130

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.



141
142
143
144
145
146
147
148
149
150
# File 'lib/hexapdf/font/true_type_wrapper.rb', line 141

def encode(glyph)
  @encoded_glyphs[glyph] ||=
    begin
      if @subsetter
        [@subsetter.use_glyph(glyph.id)].pack('n')
      else
        [glyph.id].pack('n')
      end
    end
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!



119
120
121
122
123
124
125
126
127
# File 'lib/hexapdf/font/true_type_wrapper.rb', line 119

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

#subset?Boolean

Returns true if the wrapped TrueType font will be subset.

Returns:

  • (Boolean)


112
113
114
# File 'lib/hexapdf/font/true_type_wrapper.rb', line 112

def subset?
  !@subsetter.nil?
end