class Prawn::Font
Provides font information and helper functions.
@abstract
Constants
- AFM
-
@deprecated
- DFont
-
@deprecated
- TTC
-
@deprecated
- TTF
-
@deprecated
Attributes
The font family. @return [String]
The font name. @return [String]
The options hash used to initialize the font. @return [Hash]
Public Class Methods
Source
# File lib/prawn/font.rb, line 394 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
Guesses font format.
@private @param src [String, IO] @param options [Hash] @option options :format [String] @return [String]
Source
# File lib/prawn/font.rb, line 377 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#initialize TTF.new}, `*.otf` calls {Fonts::OTF#initialize OTF.new}, `*.dfont` calls {Fonts::DFont#initialize DFont.new}, `*.ttc` goes to {Fonts::TTC#initialize TTC.new}, and anything else will be passed through to {Prawn::Fonts::AFM#initialize AFM.new}.
@param document [Prawn::Document] owning document @param src [String] font file path @param options [Hash] @option options :family [String] @option options :style [Symbol] @return [Prawn::Fonts::Font]
Source
# File lib/prawn/font.rb, line 412 def initialize(document, name, options = {}) @document = document @name = name @options = options @family = options[:family] @identifier = generate_unique_id @references = {} @subset_name_cache = {} @full_font_embedding = options.key?(:subset) && !options[:subset] end
@private @param document [Prawn::Document] @param name [String] @param options [Hash{Symbol => any}] @option options :family [String] @option options :subset [Boolean] (true)
Public Instance Methods
Source
# File lib/prawn/font.rb, line 496 def add_to_current_page(subset) @references[subset] ||= register(subset) @document.state.page.fonts[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.
@param subset [Integer] @return [void]
Source
# File lib/prawn/font.rb, line 430 def ascender @ascender / 1000.0 * size end
The size of the font ascender in PDF points.
@return [Number]
Source
# File lib/prawn/font.rb, line 437 def descender -@descender / 1000.0 * size end
The size of the font descender in PDF points.
@return [Number]
Source
# File lib/prawn/font.rb, line 532 def eql?(other) self.class == other.class && name == other.name && family == other.family && size == other.size end
Compliments the {#hash} implementation.
@param other [Object] @return [Boolean]
Source
# File lib/prawn/font.rb, line 524 def hash [self.class, name, family].hash end
Return a hash (as in ‘Object#hash`) for the font. This is required since font objects are used as keys in hashes that cache certain values.
@return [Integer]
Source
# File lib/prawn/font.rb, line 486 def height height_at(size) end
Gets height of current font in PDF points at current font size.
@return [Number]
Source
# File lib/prawn/font.rb, line 478 def height_at(size) @normalized_height ||= (@ascender - @descender + @line_gap) / 1000.0 @normalized_height * size end
Gets height of font in PDF points at the given font size.
@param size [Number] @return [Number]
Source
# File lib/prawn/font.rb, line 504 def identifier_for(subset) @subset_name_cache[subset] ||= if full_font_embedding @identifier.to_sym else :"#{@identifier}.#{subset}" end end
@private @param subset [Integer] @return [Symbol]
Source
# File lib/prawn/font.rb, line 516 def inspect "#{self.class.name}< #{name}: #{size} >" end
Returns a string containing a human-readable representation of this font.
@return [String]
Source
# File lib/prawn/font.rb, line 444 def line_gap @line_gap / 1000.0 * size end
The size of the recommended gap between lines of text in PDF points
@return [Number]
Source
# File lib/prawn/font.rb, line 456 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.
@abstract @!parse def normalize_encoding(string); end @param string [String] @return [String]
Source
# File lib/prawn/font.rb, line 469 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.
@note This method doesn’t mutate its argument any more.
@deprecated @param str [String] @return [String]