Provides font information and helper functions.
The current font family
The current font name
The options hash used to initialize the font
Shortcut interface for constructing a font object. Filenames of the form *.ttf will call Prawn::Font::TTF.new, *.dfont Font::DFont.new, and anything else will be passed through to Font::AFM.new()
# File lib/prawn/font.rb, line 258 def self.load(document,name,options={}) case name.to_s 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
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.
# File lib/prawn/font.rb, line 337 def add_to_current_page(subset) @references[subset] ||= register(subset) @document.state.page.fonts.merge!(identifier_for(subset) => @references[subset]) end
The size of the font ascender in PDF points
# File lib/prawn/font.rb, line 281 def ascender @ascender / 1000.0 * size end
The size of the font descender in PDF points
# File lib/prawn/font.rb, line 287 def descender -@descender / 1000.0 * size end
Gets height of current font in PDF points at current font size
# File lib/prawn/font.rb, line 329 def height height_at(size) end
Gets height of current font in PDF points at the given font size
# File lib/prawn/font.rb, line 322 def height_at(size) @normalized_height ||= (@ascender - @descender + @line_gap) / 1000.0 @normalized_height * size end
# File lib/prawn/font.rb, line 297 def identifier_for(subset) "#{@identifier}.#{subset}".to_sym end
# File lib/prawn/font.rb, line 301 def inspect "#{self.class.name}< #{name}: #{size} >" end
The size of the recommended gap between lines of text in PDF points
# File lib/prawn/font.rb, line 293 def line_gap @line_gap / 1000.0 * size 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!.
# File lib/prawn/font.rb, line 309 def normalize_encoding(string) raise NotImplementedError, "subclasses of Prawn::Font must implement #normalize_encoding" end
Destructive version of #normalize_encoding; normalizes the encoding of a string in place.
# File lib/prawn/font.rb, line 316 def normalize_encoding!(str) str.replace(normalize_encoding(str)) end
generate a font identifier that hasn't been used on the curretn page yet
# File lib/prawn/font.rb, line 354 def generate_unique_id offset, id = 0, nil while id.nil? || page_contains_font_id?(id) offset += 1 id = :"F#{@document.font_registry.size + offset}" end id end
Returns true if the provided font identifier already exists in the document. This is used when adding new fonts to a document to ensure we don't step on fonts imported from a template.
page_contains_font_id?("F1") => true
# File lib/prawn/font.rb, line 372 def page_contains_font_id?(id) id = id.to_s @document.state.page.fonts.keys.any? { |exist_id| exist_id.to_s[0,id.size] == id } end
# File lib/prawn/font.rb, line 379 def size @document.font_size end