Class: Prawn::Font::TTF
- Inherits:
-
Prawn::Font
- Object
- Prawn::Font
- Prawn::Font::TTF
- Defined in:
- lib/prawn/font/ttf.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#subsets ⇒ Object
readonly
Returns the value of attribute subsets.
-
#ttf ⇒ Object
readonly
Returns the value of attribute ttf.
Attributes inherited from Prawn::Font
Instance Method Summary collapse
- #basename ⇒ Object
-
#bbox ⇒ Object
The font bbox, as an array of integers.
- #cap_height ⇒ Object
-
#compute_width_of(string, options = {}) ⇒ Object
NOTE:
string
must be UTF8-encoded. -
#encode_text(text, options = {}) ⇒ Object
Perform any changes to the string that need to happen before it is rendered to the canvas.
- #family_class ⇒ Object
-
#has_kerning_data? ⇒ Boolean
Returns true if the font has kerning data, false otherwise.
-
#initialize(document, name, options = {}) ⇒ TTF
constructor
A new instance of TTF.
- #italic_angle ⇒ Object
- #normalize_encoding(text) ⇒ Object
- #pdf_flags ⇒ Object
- #script? ⇒ Boolean
- #serif? ⇒ Boolean
-
#stemV ⇒ Object
not sure how to compute this for true-type fonts…
- #x_height ⇒ Object
Methods inherited from Prawn::Font
#add_to_current_page, #ascender, #descender, #height, #height_at, #identifier_for, #inspect, #line_gap, load, #normalize_encoding!
Constructor Details
#initialize(document, name, options = {}) ⇒ TTF
Returns a new instance of TTF.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/prawn/font/ttf.rb', line 9 def initialize(document, name, ={}) 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 |
Instance Attribute Details
#subsets ⇒ Object (readonly)
Returns the value of attribute subsets.
7 8 9 |
# File 'lib/prawn/font/ttf.rb', line 7 def subsets @subsets end |
#ttf ⇒ Object (readonly)
Returns the value of attribute ttf.
7 8 9 |
# File 'lib/prawn/font/ttf.rb', line 7 def ttf @ttf end |
Instance Method Details
#basename ⇒ Object
93 94 95 |
# File 'lib/prawn/font/ttf.rb', line 93 def basename @basename ||= @ttf.name.postscript_name end |
#bbox ⇒ Object
The font bbox, as an array of integers
45 46 47 |
# File 'lib/prawn/font/ttf.rb', line 45 def bbox @bbox ||= @ttf.bbox.map { |i| Integer(i * scale_factor) } end |
#cap_height ⇒ Object
113 114 115 116 117 118 |
# File 'lib/prawn/font/ttf.rb', line 113 def cap_height @cap_height ||= begin height = @ttf.os2.exists? && @ttf.os2.cap_height || 0 height == 0 ? ascender : height end end |
#compute_width_of(string, options = {}) ⇒ Object
NOTE: string
must be UTF8-encoded.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/prawn/font/ttf.rb', line 26 def compute_width_of(string, ={}) #:nodoc: scale = ([:size] || size) / 1000.0 if [:kerning] kern(string).inject(0) do |s,r| if r.is_a?(Numeric) s - r else r.inject(s) { |s2, u| s2 + character_width_by_code(u) } end end * scale else string.unpack("U*").inject(0) do |s,r| s + character_width_by_code(r) end * scale end end |
#encode_text(text, options = {}) ⇒ Object
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.
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/prawn/font/ttf.rb', line 62 def encode_text(text,={}) text = text.chomp if [: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 |
#family_class ⇒ Object
126 127 128 |
# File 'lib/prawn/font/ttf.rb', line 126 def family_class @family_class ||= (@ttf.os2.exists? && @ttf.os2.family_class || 0) >> 8 end |
#has_kerning_data? ⇒ Boolean
Returns true if the font has kerning data, false otherwise
50 51 52 |
# File 'lib/prawn/font/ttf.rb', line 50 def has_kerning_data? @has_kerning_data end |
#italic_angle ⇒ Object
102 103 104 105 106 107 108 109 110 111 |
# File 'lib/prawn/font/ttf.rb', line 102 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 |
#normalize_encoding(text) ⇒ Object
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/prawn/font/ttf.rb', line 149 def normalize_encoding(text) if text.respond_to?(:encode) # if we're running under a M17n aware VM, ensure the string provided is # UTF-8 (by converting it if necessary) begin text.encode("UTF-8") rescue 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 else # on a non M17N aware VM, use unpack as a hackish way to verify the # string is valid utf-8. I thought it was better than loading iconv # though. begin text.unpack("U*") return text.dup rescue raise Prawn::Errors::IncompatibleStringEncoding, "The string you " + "are attempting to render is not encoded in valid UTF-8." end end end |
#pdf_flags ⇒ Object
138 139 140 141 142 143 144 145 146 147 |
# File 'lib/prawn/font/ttf.rb', line 138 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 |
#script? ⇒ Boolean
134 135 136 |
# File 'lib/prawn/font/ttf.rb', line 134 def script? @script ||= family_class == 10 end |
#serif? ⇒ Boolean
130 131 132 |
# File 'lib/prawn/font/ttf.rb', line 130 def serif? @serif ||= [1,2,3,4,5,7].include?(family_class) end |
#stemV ⇒ Object
not sure how to compute this for true-type fonts…
98 99 100 |
# File 'lib/prawn/font/ttf.rb', line 98 def stemV 0 end |
#x_height ⇒ Object
120 121 122 123 124 |
# File 'lib/prawn/font/ttf.rb', line 120 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 |