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

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

:nodoc:



247
248
249
250
251
252
253
254
255
256
257
# File 'lib/prawn/font.rb', line 247

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

  @family     = options[:family]

  @identifier = :"F#{@document.font_registry.size + 1}"

  @references = {}
end

Instance Attribute Details

#familyObject (readonly)

The current font family



229
230
231
# File 'lib/prawn/font.rb', line 229

def family
  @family
end

#nameObject (readonly)

The current font name



226
227
228
# File 'lib/prawn/font.rb', line 226

def name
  @name
end

#optionsObject (readonly)

The options hash used to initialize the font



232
233
234
# File 'lib/prawn/font.rb', line 232

def options
  @options
end

Class Method Details

.load(document, name, options = {}) ⇒ Object

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()



238
239
240
241
242
243
244
245
# File 'lib/prawn/font.rb', line 238

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

Instance Method Details

#add_to_current_page(subset) ⇒ Object

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.



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

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

#ascenderObject

The size of the font ascender in PDF points



261
262
263
# File 'lib/prawn/font.rb', line 261

def ascender
  @ascender / 1000.0 * size
end

#descenderObject

The size of the font descender in PDF points



267
268
269
# File 'lib/prawn/font.rb', line 267

def descender
  -@descender / 1000.0 * size
end

#heightObject

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



302
303
304
# File 'lib/prawn/font.rb', line 302

def height
  height_at(size)
end

#height_at(size) ⇒ Object

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



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

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

#identifier_for(subset) ⇒ Object

:nodoc:



315
316
317
# File 'lib/prawn/font.rb', line 315

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

#inspectObject

:nodoc:



319
320
321
# File 'lib/prawn/font.rb', line 319

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

#line_gapObject

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



273
274
275
# File 'lib/prawn/font.rb', line 273

def line_gap
  @line_gap / 1000.0 * size
end

#normalize_encoding(string) ⇒ Object

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)


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

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

#normalize_encoding!(str) ⇒ Object

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



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

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