Class: Krill::Formatter

Inherits:
Struct
  • Object
show all
Defined in:
lib/krill/formatter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_spacingObject

Returns the value of attribute character_spacing

Returns:

  • (Object)

    the current value of character_spacing



4
5
6
# File 'lib/krill/formatter.rb', line 4

def character_spacing
  @character_spacing
end

#fontObject

Returns the value of attribute font

Returns:

  • (Object)

    the current value of font



4
5
6
# File 'lib/krill/formatter.rb', line 4

def font
  @font
end

#sizeObject

Returns the value of attribute size

Returns:

  • (Object)

    the current value of size



4
5
6
# File 'lib/krill/formatter.rb', line 4

def size
  @size
end

#subscriptObject Also known as: subscript?

Returns the value of attribute subscript

Returns:

  • (Object)

    the current value of subscript



4
5
6
# File 'lib/krill/formatter.rb', line 4

def subscript
  @subscript
end

#superscriptObject Also known as: superscript?

Returns the value of attribute superscript

Returns:

  • (Object)

    the current value of superscript



4
5
6
# File 'lib/krill/formatter.rb', line 4

def superscript
  @superscript
end

Instance Method Details

#ascenderObject



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

#descenderObject



45
46
47
# File 'lib/krill/formatter.rb', line 45

def descender
  -font.descender * size
end

#familyObject



14
15
16
# File 'lib/krill/formatter.rb', line 14

def family
  font.family
end

#heightObject



53
54
55
# File 'lib/krill/formatter.rb', line 53

def height
  normalized_height * size
end

#line_gapObject



49
50
51
# File 'lib/krill/formatter.rb', line 49

def line_gap
  font.line_gap * size
end

#nameObject



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

Returns:

  • (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