class Prawn::Font
Provides font information and helper functions.
Constants
- AFM
-
@deprecated
- DFont
- TTC
- TTF
Attributes
The current font family
The current font name
The options hash used to initialize the font
Public Class Methods
Source
# File lib/prawn/font.rb, line 317 def self.font_format(src, options) return options.fetch(:format, 'ttf') if src.respond_to? :read case src.to_s when /\.ttf$/i then 'ttf' when /\.otf$/i then 'otf' when /\.dfont$/i then 'dfont' when /\.ttc$/i then 'ttc' else 'afm' end end
Source
# File lib/prawn/font.rb, line 307 def self.load(document, src, options = {}) case font_format(src, options) when 'ttf' then TTF.new(document, src, options) when 'otf' then Fonts::OTF.new(document, src, options) when 'dfont' then DFont.new(document, src, options) when 'ttc' then TTC.new(document, src, options) else AFM.new(document, src, options) end end
Shortcut interface for constructing a font object. Filenames of the form *.ttf will call Fonts::TTF.new
, *.dfont Fonts::DFont.new, *.ttc goes to Fonts::TTC.new, and anything else will be passed through to Fonts::AFM.new()
Public Instance Methods
Source
# File lib/prawn/font.rb, line 395 def add_to_current_page(subset) @references[subset] ||= register(subset) @document.state.page.fonts.merge!( identifier_for(subset) => @references[subset] ) end
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.
Source
# File lib/prawn/font.rb, line 343 def ascender @ascender / 1000.0 * size end
The size of the font ascender in PDF
points
Source
# File lib/prawn/font.rb, line 349 def descender -@descender / 1000.0 * size end
The size of the font descender in PDF
points
Source
# File lib/prawn/font.rb, line 387 def height height_at(size) end
Gets height of current font in PDF
points at current font size
Source
# File lib/prawn/font.rb, line 380 def height_at(size) @normalized_height ||= (@ascender - @descender + @line_gap) / 1000.0 @normalized_height * size end
Gets height of current font in PDF
points at the given font size
Source
# File lib/prawn/font.rb, line 355 def line_gap @line_gap / 1000.0 * size end
The size of the recommended gap between lines of text in PDF
points
Source
# File lib/prawn/font.rb, line 363 def normalize_encoding(_string) raise NotImplementedError, 'subclasses of Prawn::Font must implement #normalize_encoding' end
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
!.
Source
# File lib/prawn/font.rb, line 372 def normalize_encoding!(str) warn 'Font#normalize_encoding! is deprecated. ' \ 'Please use non-mutating version Font#normalize_encoding instead.' str.dup.replace(normalize_encoding(str)) end
Destructive version of normalize_encoding
; normalizes the encoding of a string in place.
@deprecated
Protected Instance Methods
Private Instance Methods
Source
# File lib/prawn/font.rb, line 430 def generate_unique_id key = nil font_count = @document.font_registry.size + 1 loop do key = :"F#{font_count}" break if key_is_unique?(key) font_count += 1 end key end
generate a font identifier that hasn’t been used on the current page yet
Source
# File lib/prawn/font.rb, line 442 def key_is_unique?(test_key) @document.state.page.fonts.keys.none? do |key| key.to_s.start_with?("#{test_key}.") end end