Class: Prawn::Font
- Inherits:
-
Object
- Object
- Prawn::Font
- 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.
Defined Under Namespace
Instance Attribute Summary collapse
-
#family ⇒ Object
readonly
The current font family.
-
#name ⇒ Object
readonly
The current font name.
-
#options ⇒ Object
readonly
The options hash used to initialize the font.
Class Method Summary collapse
-
.load(document, name, options = {}) ⇒ Object
Shortcut interface for constructing a font object.
Instance Method Summary collapse
-
#add_to_current_page(subset) ⇒ Object
Registers the given subset of the current font with the current PDF page.
-
#ascender ⇒ Object
The size of the font ascender in PDF points.
-
#descender ⇒ Object
The size of the font descender in PDF points.
-
#height ⇒ Object
Gets height of current font in PDF points at current font size.
-
#height_at(size) ⇒ Object
Gets height of current font in PDF points at the given font size.
-
#identifier_for(subset) ⇒ Object
:nodoc:.
-
#initialize(document, name, options = {}) ⇒ Font
constructor
:nodoc:.
-
#inspect ⇒ Object
:nodoc:.
-
#line_gap ⇒ Object
The size of the recommended gap between lines of text in PDF points.
-
#normalize_encoding(string) ⇒ Object
Normalizes the encoding of the string to an encoding supported by the font.
-
#normalize_encoding!(str) ⇒ Object
Destructive version of normalize_encoding; normalizes the encoding of a string in place.
Constructor Details
#initialize(document, name, options = {}) ⇒ Font
:nodoc:
251 252 253 254 255 256 257 258 259 260 261 |
# File 'lib/prawn/font.rb', line 251 def initialize(document,name,={}) #:nodoc: @document = document @name = name @options = @family = [:family] @identifier = generate_unique_id @references = {} end |
Instance Attribute Details
#family ⇒ Object (readonly)
The current font family
233 234 235 |
# File 'lib/prawn/font.rb', line 233 def family @family end |
#name ⇒ Object (readonly)
The current font name
230 231 232 |
# File 'lib/prawn/font.rb', line 230 def name @name end |
#options ⇒ Object (readonly)
The options hash used to initialize the font
236 237 238 |
# File 'lib/prawn/font.rb', line 236 def @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()
242 243 244 245 246 247 248 249 |
# File 'lib/prawn/font.rb', line 242 def self.load(document,name,={}) case name when /\.ttf$/i then TTF.new(document, name, ) when /\.dfont$/i then DFont.new(document, name, ) when /\.afm$/i then AFM.new(document, name, ) else AFM.new(document, name, ) 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.
321 322 323 324 |
# File 'lib/prawn/font.rb', line 321 def add_to_current_page(subset) @references[subset] ||= register(subset) @document.state.page.fonts.merge!(identifier_for(subset) => @references[subset]) end |
#ascender ⇒ Object
The size of the font ascender in PDF points
265 266 267 |
# File 'lib/prawn/font.rb', line 265 def ascender @ascender / 1000.0 * size end |
#descender ⇒ Object
The size of the font descender in PDF points
271 272 273 |
# File 'lib/prawn/font.rb', line 271 def descender -@descender / 1000.0 * size end |
#height ⇒ Object
Gets height of current font in PDF points at current font size
313 314 315 |
# File 'lib/prawn/font.rb', line 313 def height height_at(size) end |
#height_at(size) ⇒ Object
Gets height of current font in PDF points at the given font size
306 307 308 309 |
# File 'lib/prawn/font.rb', line 306 def height_at(size) @normalized_height ||= (@ascender - @descender + @line_gap) / 1000.0 @normalized_height * size end |
#identifier_for(subset) ⇒ Object
:nodoc:
326 327 328 |
# File 'lib/prawn/font.rb', line 326 def identifier_for(subset) "#{@identifier}.#{subset}".to_sym end |
#inspect ⇒ Object
:nodoc:
330 331 332 |
# File 'lib/prawn/font.rb', line 330 def inspect "#{self.class.name}< #{name}: #{size} >" end |
#line_gap ⇒ Object
The size of the recommended gap between lines of text in PDF points
277 278 279 |
# File 'lib/prawn/font.rb', line 277 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!.
293 294 295 |
# File 'lib/prawn/font.rb', line 293 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.
300 301 302 |
# File 'lib/prawn/font.rb', line 300 def normalize_encoding!(str) str.replace(normalize_encoding(str)) end |