Class: Prawn::Fonts::TTF

Inherits:
Prawn::Font show all
Defined in:
lib/prawn/fonts/ttf.rb

Overview

Note:

You shouldn’t use this class directly.

TrueType font.

Direct Known Subclasses

DFont, OTF, TTC

Defined Under Namespace

Classes: Error, NoPostscriptName, NoUnicodeCMap

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document, name, options = {}) ⇒ TTF

Returns a new instance of TTF.

Parameters:

  • document (Prawn::Document)
  • name (String)

    font file path

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :family (String)
  • :style (Symbol)
Source Code
lib/prawn/fonts/ttf.rb, line 134
134
def initialize(document, name, options = {})
135
  super
136
137
  @ttf = read_ttf_file
138
  @subsets =
139
    if full_font_embedding
140
      FullFontSubsetsCollection.new(@ttf)
141
    else
142
      TTFunk::SubsetCollection.new(@ttf)
143
    end
144
  @italic_angle = nil
145
146
  @attributes = {}
147
  @bounding_boxes = {}
148
  @char_widths = {}
149
  @has_kerning_data = @ttf.kerning.exists? && @ttf.kerning.tables.any?
150
151
  @ascender = Integer(@ttf.ascent * scale_factor)
152
  @descender = Integer(@ttf.descent * scale_factor)
153
  @line_gap = Integer(@ttf.line_gap * scale_factor)
154
end

Instance Attribute Details

#subsetsObject (readonly)

Returns the value of attribute subsets.

Source Code
lib/prawn/fonts/ttf.rb, line 58
58
def subsets
59
  @subsets
60
end

#ttfTTFunk::File (readonly)

TTFunk font.

Returns:

  • (TTFunk::File)
Source Code
lib/prawn/fonts/ttf.rb, line 57
57
def ttf
58
  @ttf
59
end

Instance Method Details

#basenameString

Base name of the font.

Returns:

  • (String)
Source Code
lib/prawn/fonts/ttf.rb, line 240
240
def basename
241
  @basename ||= @ttf.name.postscript_name
242
end

#bboxArray(Number, Number, Number, Number)

The font bbox.

Returns:

  • (Array(Number, Number, Number, Number))
Source Code
lib/prawn/fonts/ttf.rb, line 184
184
def bbox
185
  @bbox ||= @ttf.bbox.map { |i| Integer(i * scale_factor) }
186
end

#character_count(str) ⇒ Integer

Returns the number of characters in str (a UTF-8-encoded string).

Parameters:

  • str (String)

Returns:

  • (Integer)
Source Code
lib/prawn/fonts/ttf.rb, line 354
354
def character_count(str)
355
  str.length
356
end

#compute_width_of(string, options = {}) ⇒ Number

Compute width of a string at the specified size, optionally with kerning applied.

Parameters:

  • string (String)

    must be encoded as UTF-8

  • options (Hash{Symbol => any}) (defaults to: {})

Options Hash (options):

  • :size (Number)
  • :kerning (Boolean) — default: false

Returns:

  • (Number)
Source Code
lib/prawn/fonts/ttf.rb, line 164
164
def compute_width_of(string, options = {})
165
  scale = (options[:size] || size) / 1000.0
166
  if options[:kerning]
167
    kern(string).reduce(0) { |s, r|
168
      if r.is_a?(Numeric)
169
        s - r
170
      else
171
        r.reduce(s) { |a, e| a + character_width_by_code(e) }
172
      end
173
    } * scale
174
  else
175
    string.codepoints.reduce(0) { |s, r|
176
      s + character_width_by_code(r)
177
    } * scale
178
  end
179
end

#encode_text(text, options = {}) ⇒ Array<Array(0, (String, Array)>]

Perform any changes to the string that need to happen before it is rendered to the canvas. Returns an array of subset “chunks”, where the even-numbered indices are the font subset number, and the following entry element is either a string or an array (for kerned text).

Parameters:

  • text (String)

    must be in UTF-8 encoding

  • options (Hash{Symbol => any}) (defaults to: {})

Options Hash (options):

  • :kerning (Boolean)

Returns:

  • (Array<Array(0, (String, Array)>])

    Array<Array(0, (String, Array)>]

Source Code
lib/prawn/fonts/ttf.rb, line 204
204
def encode_text(text, options = {})
205
  text = text.chomp
206
207
  if options[:kerning]
208
    last_subset = nil
209
    kern(text).reduce([]) do |result, element|
210
      if element.is_a?(Numeric)
211
        unless result.last[1].is_a?(Array)
212
          result.last[1] = [result.last[1]]
213
        end
214
        result.last[1] << element
215
        result
216
      else
217
        encoded = @subsets.encode(element)
218
219
        if encoded.first[0] == last_subset
220
          result.last[1] << encoded.first[1]
221
          encoded.shift
222
        end
223
224
        if encoded.any?
225
          last_subset = encoded.last[0]
226
          result + encoded
227
        else
228
          result
229
        end
230
      end
231
    end
232
  else
233
    @subsets.encode(text.unpack('U*'))
234
  end
235
end

#glyph_present?(char) ⇒ Boolean

Does this font has a glyph for the character?

Parameters:

  • char (String)

Returns:

  • (Boolean)
Source Code
lib/prawn/fonts/ttf.rb, line 345
345
def glyph_present?(char)
346
  code = char.codepoints.first
347
  cmap[code].positive?
348
end

#has_kerning_data?Boolean

Does this font contain kerning data.

Returns:

  • (Boolean)
Source Code
lib/prawn/fonts/ttf.rb, line 191
191
def has_kerning_data? # rubocop: disable Naming/PredicateName
192
  @has_kerning_data
193
end

#normalize_encoding(text) ⇒ String

Normlize text to a compatible encoding.

Parameters:

  • text (String)

Returns:

  • (String)
Source Code
lib/prawn/fonts/ttf.rb, line 325
325
def normalize_encoding(text)
326
  text.encode(::Encoding::UTF_8)
327
rescue StandardError
328
  raise Prawn::Errors::IncompatibleStringEncoding,
329
    "Encoding #{text.encoding} can not be transparently converted to UTF-8. " \
330
      'Please ensure the encoding of the string you are attempting to use is set correctly'
331
end

#to_utf8(text) ⇒ String

Encode text to UTF-8.

Parameters:

  • text (String)

Returns:

  • (String)
Source Code
lib/prawn/fonts/ttf.rb, line 337
337
def to_utf8(text)
338
  text.encode('UTF-8')
339
end

#unicode?true

Does this font support Unicode?

Returns:

  • (true)
Source Code
lib/prawn/fonts/ttf.rb, line 63
63
def unicode?
64
  true
65
end