Class: Krill::Formatter
- Inherits:
-
Struct
- Object
- Struct
- Krill::Formatter
- Defined in:
- lib/krill/formatter.rb
Instance Attribute Summary collapse
-
#character_spacing ⇒ Object
Returns the value of attribute character_spacing.
-
#font ⇒ Object
Returns the value of attribute font.
-
#size ⇒ Object
Returns the value of attribute size.
-
#subscript ⇒ Object
(also: #subscript?)
Returns the value of attribute subscript.
-
#superscript ⇒ Object
(also: #superscript?)
Returns the value of attribute superscript.
Instance Method Summary collapse
- #ascender ⇒ Object
-
#compute_width_of(string, kerning: true) ⇒ Object
NOTE:
stringmust be UTF8-encoded. - #descender ⇒ Object
- #family ⇒ Object
- #height ⇒ Object
-
#initialize(font, size, character_spacing: 0, superscript: false, subscript: false) ⇒ Formatter
constructor
A new instance of Formatter.
- #line_gap ⇒ Object
- #name ⇒ Object
- #normalize_encoding(text) ⇒ Object
- #unicode? ⇒ Boolean
- #width_of(string, kerning: true) ⇒ Object
Constructor Details
#initialize(font, size, character_spacing: 0, superscript: false, subscript: false) ⇒ Formatter
Returns a new instance of Formatter.
6 7 8 |
# File 'lib/krill/formatter.rb', line 6 def initialize(font, size, character_spacing: 0, superscript: false, subscript: false) super font, size, character_spacing, superscript, subscript end |
Instance Attribute Details
#character_spacing ⇒ Object
Returns the value of attribute character_spacing
4 5 6 |
# File 'lib/krill/formatter.rb', line 4 def character_spacing @character_spacing end |
#font ⇒ Object
Returns the value of attribute font
4 5 6 |
# File 'lib/krill/formatter.rb', line 4 def font @font end |
#size ⇒ Object
Returns the value of attribute size
4 5 6 |
# File 'lib/krill/formatter.rb', line 4 def size @size end |
#subscript ⇒ Object Also known as: subscript?
Returns the value of attribute subscript
4 5 6 |
# File 'lib/krill/formatter.rb', line 4 def subscript @subscript end |
#superscript ⇒ Object Also known as: superscript?
Returns the value of attribute superscript
4 5 6 |
# File 'lib/krill/formatter.rb', line 4 def superscript @superscript end |
Instance Method Details
#ascender ⇒ Object
41 42 43 |
# File 'lib/krill/formatter.rb', line 41 def ascender font.ascender * size end |
#compute_width_of(string, kerning: true) ⇒ Object
NOTE: string must be UTF8-encoded.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/krill/formatter.rb', line 25 def compute_width_of(string, kerning: true) size_adjustment = superscript? || subscript? ? SUPERSCRIPT_SCALE : 1 if kerning kern(string).inject(0.0) do |width, r| if r.is_a?(Numeric) width - r else r.inject(width) { |width2, char| width2 + width_of_char(char) } end end else string.chars.inject(0.0) { |width, char| width + width_of_char(char) } end * size * size_adjustment end |
#descender ⇒ Object
45 46 47 |
# File 'lib/krill/formatter.rb', line 45 def descender -font.descender * size end |
#family ⇒ Object
14 15 16 |
# File 'lib/krill/formatter.rb', line 14 def family font.family end |
#height ⇒ Object
53 54 55 |
# File 'lib/krill/formatter.rb', line 53 def height normalized_height * size end |
#line_gap ⇒ Object
49 50 51 |
# File 'lib/krill/formatter.rb', line 49 def line_gap font.line_gap * size end |
#name ⇒ Object
10 11 12 |
# File 'lib/krill/formatter.rb', line 10 def name font.name end |
#normalize_encoding(text) ⇒ Object
66 67 68 |
# File 'lib/krill/formatter.rb', line 66 def normalize_encoding(text) text.encode(::Encoding::UTF_8) end |
#unicode? ⇒ Boolean
62 63 64 |
# File 'lib/krill/formatter.rb', line 62 def unicode? true end |
#width_of(string, kerning: true) ⇒ Object
19 20 21 22 |
# File 'lib/krill/formatter.rb', line 19 def width_of(string, kerning: true) length = compute_width_of(string, kerning: kerning) length + (character_spacing * character_count(string)) end |