module NSWTopo::Font::Generic

Constants

WIDTHS

Public Instance Methods

glyph_length(string, attributes) click to toggle source
# File lib/nswtopo/font/generic.rb, line 15
def glyph_length(string, attributes)
  font_size, letter_spacing, word_spacing = attributes.values_at("font-size", "letter-spacing", "word-spacing").map(&:to_f)
  string.chars.each_cons(2).inject(WIDTHS[string[0]] * font_size) do |sum, pair|
    next sum + WIDTHS[pair[1]] * font_size + letter_spacing                unless pair[0] == ?\s
    next sum + WIDTHS[pair[1]] * font_size + letter_spacing + word_spacing unless pair[1] == ?\s
    sum
  end
end