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.
Experimental API 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.
Experimental API collapse
- .font_format(src, options) ⇒ Object
-
.load(document, src, options = {}) ⇒ Object
Shortcut interface for constructing a font object.
-
#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.
-
#eql?(other) ⇒ Boolean
Compliments the #hash implementation above.
-
#hash ⇒ Object
Return a hash (as in Object#hash) for the font based on the output of #inspect.
-
#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:
308 309 310 311 312 313 314 315 316 317 318 |
# File 'lib/prawn/font.rb', line 308 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
282 283 284 |
# File 'lib/prawn/font.rb', line 282 def family @family end |
#name ⇒ Object (readonly)
The current font name
279 280 281 |
# File 'lib/prawn/font.rb', line 279 def name @name end |
#options ⇒ Object (readonly)
The options hash used to initialize the font
285 286 287 |
# File 'lib/prawn/font.rb', line 285 def @options end |
Class Method Details
.font_format(src, options) ⇒ Object
298 299 300 301 302 303 304 305 306 |
# File 'lib/prawn/font.rb', line 298 def self.font_format(src, ) return .fetch(:format, 'ttf') if src.respond_to? :read case src.to_s when /\.ttf$/i then return 'ttf' when /\.dfont$/i then return 'dfont' else return 'afm' end end |
.load(document, src, 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()
290 291 292 293 294 295 296 |
# File 'lib/prawn/font.rb', line 290 def self.load(document, src, ={}) case font_format(src, ) when 'ttf' then TTF.new(document, src, ) when 'dfont' then DFont.new(document, src, ) else AFM.new(document, src, ) 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.
370 371 372 373 |
# File 'lib/prawn/font.rb', line 370 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
322 323 324 |
# File 'lib/prawn/font.rb', line 322 def ascender @ascender / 1000.0 * size end |
#descender ⇒ Object
The size of the font descender in PDF points
328 329 330 |
# File 'lib/prawn/font.rb', line 328 def descender -@descender / 1000.0 * size end |
#eql?(other) ⇒ Boolean
Compliments the #hash implementation above
394 395 396 397 |
# File 'lib/prawn/font.rb', line 394 def eql?( other ) #:nodoc: self.class == other.class && self.name == other.name && self.family == other.family && size == other.send(:size) end |
#hash ⇒ Object
Return a hash (as in Object#hash) for the font based on the output of #inspect. This is required since font objects are used as keys in hashes that cache certain values (See Prawn::Table::Text#styled_with_of_single_character)
388 389 390 |
# File 'lib/prawn/font.rb', line 388 def hash #:nodoc: [ self.class, self.name, self.family, size ].hash end |
#height ⇒ Object
Gets height of current font in PDF points at current font size
362 363 364 |
# File 'lib/prawn/font.rb', line 362 def height height_at(size) end |
#height_at(size) ⇒ Object
Gets height of current font in PDF points at the given font size
355 356 357 358 |
# File 'lib/prawn/font.rb', line 355 def height_at(size) @normalized_height ||= (@ascender - @descender + @line_gap) / 1000.0 @normalized_height * size end |
#identifier_for(subset) ⇒ Object
:nodoc:
375 376 377 |
# File 'lib/prawn/font.rb', line 375 def identifier_for(subset) #:nodoc: "#{@identifier}.#{subset}" end |
#inspect ⇒ Object
:nodoc:
379 380 381 |
# File 'lib/prawn/font.rb', line 379 def inspect #:nodoc: "#{self.class.name}< #{name}: #{size} >" end |
#line_gap ⇒ Object
The size of the recommended gap between lines of text in PDF points
334 335 336 |
# File 'lib/prawn/font.rb', line 334 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!.
342 343 344 |
# File 'lib/prawn/font.rb', line 342 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.
349 350 351 |
# File 'lib/prawn/font.rb', line 349 def normalize_encoding!(str) str.replace(normalize_encoding(str)) end |