Class: Prawn::Font

Inherits:
Object
  • Object
show all
Defined in:
lib/prawn/font.rb,
lib/prawn/font/afm.rb,
lib/prawn/font/ttf.rb,
lib/prawn/font/dfont.rb

Overview

Provides font information and helper functions.

Direct Known Subclasses

AFM, TTF

Defined Under Namespace

Classes: AFM, DFont, TTF

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Font) initialize(document, name, options = {})

:nodoc:



268
269
270
271
272
273
274
275
276
277
278
# File 'lib/prawn/font.rb', line 268

def initialize(document,name,options={}) #:nodoc:
  @document   = document
  @name       = name
  @options    = options

  @family     = options[:family]

  @identifier = generate_unique_id

  @references = {}
end

Instance Attribute Details

- (Object) family (readonly)

The current font family



250
251
252
# File 'lib/prawn/font.rb', line 250

def family
  @family
end

- (Object) name (readonly)

The current font name



247
248
249
# File 'lib/prawn/font.rb', line 247

def name
  @name
end

- (Object) options (readonly)

The options hash used to initialize the font



253
254
255
# File 'lib/prawn/font.rb', line 253

def options
  @options
end

Class Method Details

+ (Object) load(document, name, options = {})

Shortcut interface for constructing a font object. Filenames of the form *.ttf will call Font::TTF.new, *.dfont Font::DFont.new, and anything else will be passed through to Font::AFM.new()



259
260
261
262
263
264
265
266
# File 'lib/prawn/font.rb', line 259

def self.load(document,name,options={})
  case name.to_s
  when /\.ttf$/i   then TTF.new(document, name, options)
  when /\.dfont$/i then DFont.new(document, name, options)
  when /\.afm$/i   then AFM.new(document, name, options)
  else                  AFM.new(document, name, options)
  end
end

Instance Method Details

- (Object) add_to_current_page(subset)

Registers the given subset of the current font with the current PDF page. This is safe to call multiple times for a given font and subset, as it will only add the font the first time it is called.



338
339
340
341
# File 'lib/prawn/font.rb', line 338

def add_to_current_page(subset)
  @references[subset] ||= register(subset)
  @document.state.page.fonts.merge!(identifier_for(subset) => @references[subset])
end

- (Object) ascender

The size of the font ascender in PDF points



282
283
284
# File 'lib/prawn/font.rb', line 282

def ascender
  @ascender / 1000.0 * size
end

- (Object) descender

The size of the font descender in PDF points



288
289
290
# File 'lib/prawn/font.rb', line 288

def descender
  -@descender / 1000.0 * size
end

- (Object) height

Gets height of current font in PDF points at current font size



330
331
332
# File 'lib/prawn/font.rb', line 330

def height
  height_at(size)
end

- (Object) height_at(size)

Gets height of current font in PDF points at the given font size



323
324
325
326
# File 'lib/prawn/font.rb', line 323

def height_at(size)
  @normalized_height ||= (@ascender - @descender + @line_gap) / 1000.0
  @normalized_height * size
end

- (Object) identifier_for(subset)

:nodoc:



343
344
345
# File 'lib/prawn/font.rb', line 343

def identifier_for(subset)
  "#{@identifier}.#{subset}".to_sym
end

- (Object) inspect

:nodoc:



347
348
349
# File 'lib/prawn/font.rb', line 347

def inspect
  "#{self.class.name}< #{name}: #{size} >"
end

- (Object) line_gap

The size of the recommended gap between lines of text in PDF points



294
295
296
# File 'lib/prawn/font.rb', line 294

def line_gap
  @line_gap / 1000.0 * size
end

- (Object) normalize_encoding(string)

Normalizes the encoding of the string to an encoding supported by the font. The string is expected to be UTF-8 going in. It will be re-encoded and the new string will be returned. For an in-place (destructive) version, see normalize_encoding!.

Raises:

  • (NotImplementedError)


310
311
312
# File 'lib/prawn/font.rb', line 310

def normalize_encoding(string)
  raise NotImplementedError, "subclasses of Prawn::Font must implement #normalize_encoding"
end

- (Object) normalize_encoding!(str)

Destructive version of normalize_encoding; normalizes the encoding of a string in place.



317
318
319
# File 'lib/prawn/font.rb', line 317

def normalize_encoding!(str)
  str.replace(normalize_encoding(str))
end