Class: Prawn::Fonts::TTF
- Inherits:
-
Prawn::Font
- Object
- Prawn::Font
- Prawn::Fonts::TTF
- Defined in:
- lib/prawn/fonts/ttf.rb
Overview
You shouldn’t use this class directly.
TrueType font.
Defined Under Namespace
Classes: Error, NoPostscriptName, NoUnicodeCMap
Instance Attribute Summary collapse
-
#subsets ⇒ Object
readonly
Returns the value of attribute subsets.
-
#ttf ⇒ TTFunk::File
readonly
TTFunk font.
Instance Method Summary collapse
-
#basename ⇒ String
Base name of the font.
-
#bbox ⇒ Array(Number, Number, Number, Number)
The font bbox.
-
#character_count(str) ⇒ Integer
Returns the number of characters in
str
(a UTF-8-encoded string). -
#compute_width_of(string, options = {}) ⇒ Number
Compute width of a string at the specified size, optionally with kerning applied.
-
#encode_text(text, options = {}) ⇒ Array<Array(0, (String, Array)>]
Perform any changes to the string that need to happen before it is rendered to the canvas.
-
#glyph_present?(char) ⇒ Boolean
Does this font has a glyph for the character?.
-
#has_kerning_data? ⇒ Boolean
Does this font contain kerning data.
-
#initialize(document, name, options = {}) ⇒ TTF
constructor
A new instance of TTF.
-
#normalize_encoding(text) ⇒ String
Normlize text to a compatible encoding.
-
#to_utf8(text) ⇒ String
Encode text to UTF-8.
-
#unicode? ⇒ true
Does this font support Unicode?.
Constructor Details
#initialize(document, name, options = {}) ⇒ TTF
Returns a new instance of TTF.
Source Code
134 | def initialize(document, name, options = {}) |
135 | super
|
136 | |
137 | @ttf = read_ttf_file |
138 | @subsets = |
139 | if full_font_embedding |
140 | FullFontSubsetsCollection.new(@ttf) |
141 | else
|
142 | TTFunk::SubsetCollection.new(@ttf) |
143 | end
|
144 | @italic_angle = nil |
145 | |
146 | @attributes = {} |
147 | @bounding_boxes = {} |
148 | @char_widths = {} |
149 | @has_kerning_data = @ttf.kerning.exists? && @ttf.kerning.tables.any? |
150 | |
151 | @ascender = Integer(@ttf.ascent * scale_factor) |
152 | @descender = Integer(@ttf.descent * scale_factor) |
153 | @line_gap = Integer(@ttf.line_gap * scale_factor) |
154 | end
|
Instance Attribute Details
#subsets ⇒ Object (readonly)
Returns the value of attribute subsets.
Source Code
58 | def subsets |
59 | @subsets
|
60 | end
|
#ttf ⇒ TTFunk::File (readonly)
TTFunk font.
Source Code
57 | def ttf |
58 | @ttf
|
59 | end
|
Instance Method Details
#basename ⇒ String
Base name of the font.
Source Code
240 | def basename |
241 | @basename ||= @ttf.name.postscript_name |
242 | end
|
#bbox ⇒ Array(Number, Number, Number, Number)
The font bbox.
Source Code
184 | def bbox |
185 | @bbox ||= @ttf.bbox.map { |i| Integer(i * scale_factor) } |
186 | end
|
#character_count(str) ⇒ Integer
Returns the number of characters in str
(a UTF-8-encoded string).
Source Code
354 | def character_count(str) |
355 | str.length |
356 | end
|
#compute_width_of(string, options = {}) ⇒ Number
Compute width of a string at the specified size, optionally with kerning applied.
Source Code
164 | def compute_width_of(string, options = {}) |
165 | scale = (options[:size] || size) / 1000.0 |
166 | if options[:kerning] |
167 | kern(string).reduce(0) { |s, r| |
168 | if r.is_a?(Numeric) |
169 | s - r |
170 | else
|
171 | r.reduce(s) { |a, e| a + character_width_by_code(e) } |
172 | end
|
173 | } * scale |
174 | else
|
175 | string.codepoints.reduce(0) { |s, r| |
176 | s + character_width_by_code(r) |
177 | } * scale |
178 | end
|
179 | end
|
#encode_text(text, options = {}) ⇒ Array<Array(0, (String, Array)>]
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).
Source Code
204 | def encode_text(text, options = {}) |
205 | text = text.chomp |
206 | |
207 | if options[:kerning] |
208 | last_subset = nil |
209 | kern(text).reduce([]) do |result, element| |
210 | if element.is_a?(Numeric) |
211 | unless result.last[1].is_a?(Array) |
212 | result.last[1] = [result.last[1]] |
213 | end
|
214 | result.last[1] << element |
215 | result
|
216 | else
|
217 | encoded = @subsets.encode(element) |
218 | |
219 | if encoded.first[0] == last_subset |
220 | result.last[1] << encoded.first[1] |
221 | encoded.shift |
222 | end
|
223 | |
224 | if encoded.any? |
225 | last_subset = encoded.last[0] |
226 | result + encoded |
227 | else
|
228 | result
|
229 | end
|
230 | end
|
231 | end
|
232 | else
|
233 | @subsets.encode(text.unpack('U*')) |
234 | end
|
235 | end
|
#glyph_present?(char) ⇒ Boolean
Does this font has a glyph for the character?
Source Code
345 | def glyph_present?(char) |
346 | code = char.codepoints.first |
347 | cmap[code].positive? |
348 | end
|
#has_kerning_data? ⇒ Boolean
Does this font contain kerning data.
Source Code
191 | def has_kerning_data? # rubocop: disable Naming/PredicateName |
192 | @has_kerning_data
|
193 | end
|
#normalize_encoding(text) ⇒ String
Normlize text to a compatible encoding.
Source Code
325 | def normalize_encoding(text) |
326 | text.encode(::Encoding::UTF_8) |
327 | rescue StandardError |
328 | raise Prawn::Errors::IncompatibleStringEncoding, |
329 | "Encoding #{text.encoding} can not be transparently converted to UTF-8. " \ |
330 | 'Please ensure the encoding of the string you are attempting to use is set correctly'
|
331 | end
|
#to_utf8(text) ⇒ String
Encode text to UTF-8.
Source Code
337 | def to_utf8(text) |
338 | text.encode('UTF-8') |
339 | end
|
#unicode? ⇒ true
Does this font support Unicode?
Source Code
63 | def unicode? |
64 | true
|
65 | end
|