@private
# File lib/prawn/font/ttf.rb, line 23 def initialize(document, name, options={}) super @ttf = read_ttf_file @subsets = TTFunk::SubsetCollection.new(@ttf) @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
# File lib/prawn/font/ttf.rb, line 107 def basename @basename ||= @ttf.name.postscript_name end
The font bbox, as an array of integers
# File lib/prawn/font/ttf.rb, line 59 def bbox @bbox ||= @ttf.bbox.map { |i| Integer(i * scale_factor) } end
# File lib/prawn/font/ttf.rb, line 127 def cap_height @cap_height ||= begin height = @ttf.os2.exists? && @ttf.os2.cap_height || 0 height == 0 ? @ascender : height end end
Returns the number of characters in str (a UTF-8-encoded string).
# File lib/prawn/font/ttf.rb, line 182 def character_count(str) str.length 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).
The text parameter must be UTF8-encoded.
# File lib/prawn/font/ttf.rb, line 76 def encode_text(text,options={}) text = text.chomp if options[:kerning] last_subset = nil kern(text).inject([]) do |result, element| if element.is_a?(Numeric) result.last[1] = [result.last[1]] unless result.last[1].is_a?(Array) 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
# File lib/prawn/font/ttf.rb, line 140 def family_class @family_class ||= (@ttf.os2.exists? && @ttf.os2.family_class || 0) >> 8 end
# File lib/prawn/font/ttf.rb, line 175 def glyph_present?(char) code = char.codepoints.first cmap[code] > 0 end
Returns true if the font has kerning data, false otherwise
# File lib/prawn/font/ttf.rb, line 64 def has_kerning_data? @has_kerning_data end
# File lib/prawn/font/ttf.rb, line 116 def italic_angle @italic_angle ||= if @ttf.postscript.exists? raw = @ttf.postscript.italic_angle hi, low = raw >> 16, raw & 0xFF hi = -((hi ^ 0xFFFF) + 1) if hi & 0x8000 != 0 "#{hi}.#{low}".to_f else 0 end end
# File lib/prawn/font/ttf.rb, line 163 def normalize_encoding(text) begin text.encode(::Encoding::UTF_8) rescue => e puts e 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 end
# File lib/prawn/font/ttf.rb, line 152 def pdf_flags @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 flags |= 0x0004 # assume the font contains at least some non-latin characters end end
# File lib/prawn/font/ttf.rb, line 148 def script? @script ||= family_class == 10 end
# File lib/prawn/font/ttf.rb, line 144 def serif? @serif ||= [1,2,3,4,5,7].include?(family_class) end
Generated with the Darkfish Rdoc Generator 2.