class Prawn::Fonts::TTF
TrueType font.
@note You shouldn’t use this class directly.
Attributes
TTFunk font. @return [TTFunk::File]
Public Class Methods
Source
# File lib/prawn/fonts/ttf.rb, line 134 def initialize(document, name, options = {}) super @ttf = read_ttf_file @subsets = if full_font_embedding FullFontSubsetsCollection.new(@ttf) else TTFunk::SubsetCollection.new(@ttf) end @italic_angle = nil @attributes = {} @bounding_boxes = {} @char_widths = {} @has_kerning_data = @ttf.kerning.exists? && @ttf.kerning.tables.any? @ascender = Integer(@ttf.ascent * scale_factor) @descender = Integer(@ttf.descent * scale_factor) @line_gap = Integer(@ttf.line_gap * scale_factor) end
@param document [Prawn::Document] @param name [String] font file path @param options [Hash] @option options :family [String] @option options :style [Symbol]
Prawn::Font::new
Public Instance Methods
Source
# File lib/prawn/fonts/ttf.rb, line 240 def basename @basename ||= @ttf.name.postscript_name end
Base name of the font.
@return [String]
Source
# File lib/prawn/fonts/ttf.rb, line 184 def bbox @bbox ||= @ttf.bbox.map { |i| Integer(i * scale_factor) } end
The font bbox.
@return [Array(Number, Number, Number, Number)]
Source
# File lib/prawn/fonts/ttf.rb, line 272 def cap_height @cap_height ||= begin height = (@ttf.os2.exists? && @ttf.os2.cap_height) || 0 height.zero? ? @ascender : height end end
@private @return [Number]
Source
# File lib/prawn/fonts/ttf.rb, line 354 def character_count(str) str.length end
Returns the number of characters in ‘str` (a UTF-8-encoded string).
@param str [String] @return [Integer]
Source
# File lib/prawn/fonts/ttf.rb, line 164 def compute_width_of(string, options = {}) scale = (options[:size] || size) / 1000.0 if options[:kerning] kern(string).reduce(0) { |s, r| if r.is_a?(Numeric) s - r else r.reduce(s) { |a, e| a + character_width_by_code(e) } end } * scale else string.codepoints.reduce(0) { |s, r| s + character_width_by_code(r) } * scale end end
Compute width of a string at the specified size, optionally with kerning applied.
@param string [String] must be encoded as UTF-8 @param options [Hash{Symbol => any}] @option options :size [Number] @option options :kerning [Boolean] (false) @return [Number]
Source
# File lib/prawn/fonts/ttf.rb, line 204 def encode_text(text, options = {}) text = text.chomp if options[:kerning] last_subset = nil kern(text).reduce([]) do |result, element| if element.is_a?(Numeric) unless result.last[1].is_a?(Array) result.last[1] = [result.last[1]] end result.last[1] << element result else encoded = @subsets.encode(element) if encoded.first[0] == last_subset result.last[1] << encoded.first[1] encoded.shift end if encoded.any? last_subset = encoded.last[0] result + encoded else result end end end else @subsets.encode(text.unpack('U*')) end end
Perform any changes to the string that need to happen before it is rendered to the canvas. Returns an array of subset “chunks”, where the even-numbered indices are the font subset number, and the following entry element is either a string or an array (for kerned text).
@param text [String] must be in UTF-8 encoding @param options [Hash{Symbol => any}] @option options :kerning [Boolean] @return [Array<Array(0, (String, Array)>]
Source
# File lib/prawn/fonts/ttf.rb, line 290 def family_class @family_class ||= ((@ttf.os2.exists? && @ttf.os2.family_class) || 0) >> 8 end
@private @return [Number]
Source
# File lib/prawn/fonts/ttf.rb, line 345 def glyph_present?(char) code = char.codepoints.first cmap[code].positive? end
Does this font has a glyph for the character?
@param char [String] @return [Boolean]
Source
# File lib/prawn/fonts/ttf.rb, line 191 def has_kerning_data? # rubocop: disable Naming/PredicateName @has_kerning_data end
Does this font contain kerning data.
@return [Boolean]
Source
# File lib/prawn/fonts/ttf.rb, line 254 def italic_angle return @italic_angle if @italic_angle if @ttf.postscript.exists? raw = @ttf.postscript.italic_angle hi = raw >> 16 low = raw & 0xFF hi = -((hi ^ 0xFFFF) + 1) if hi & 0x8000 != 0 @italic_angle = Float("#{hi}.#{low}") else @italic_angle = 0 end @italic_angle end
@private @return [Number]
Source
# File lib/prawn/fonts/ttf.rb, line 325 def normalize_encoding(text) text.encode(::Encoding::UTF_8) rescue StandardError raise Prawn::Errors::IncompatibleStringEncoding, "Encoding #{text.encoding} can not be transparently converted to UTF-8. " \ 'Please ensure the encoding of the string you are attempting to use is set correctly' end
Normlize text to a compatible encoding.
@param text [String] @return [String]
Source
# File lib/prawn/fonts/ttf.rb, line 308 def pdf_flags @pdf_flags ||= begin flags = 0 flags |= 0x0001 if @ttf.postscript.fixed_pitch? flags |= 0x0002 if serif? flags |= 0x0008 if script? flags |= 0x0040 if italic_angle != 0 # Assume the font contains at least some non-latin characters flags | 0x0004 end end
@private @return [Integer]
Source
# File lib/prawn/fonts/ttf.rb, line 302 def script? @script ||= family_class == 10 end
@private @return [Boolean]
Source
# File lib/prawn/fonts/ttf.rb, line 296 def serif? @serif ||= [1, 2, 3, 4, 5, 7].include?(family_class) end
@private @return [Boolean]
Source
# File lib/prawn/fonts/ttf.rb, line 248 def stem_v 0 end
@devnote not sure how to compute this for true-type fonts…
@private @return [Number]
Source
# File lib/prawn/fonts/ttf.rb, line 337 def to_utf8(text) text.encode('UTF-8') end
Encode text to UTF-8.
@param text [String] @return [String]
Source
# File lib/prawn/fonts/ttf.rb, line 63 def unicode? true end
Does this font support Unicode?
@return [true]
Source
# File lib/prawn/fonts/ttf.rb, line 282 def x_height # FIXME: seems like if os2 table doesn't exist, we could # just find the height of the lower-case 'x' glyph? (@ttf.os2.exists? && @ttf.os2.x_height) || 0 end
@private @return [number]