Permalink
Browse files

Unify width_of

  • Loading branch information...
practicingruby committed Jan 23, 2009
1 parent a4bb071 commit 6155bd0bcc5ad2aa6defeb48949379ddba61cb94
Showing with 15 additions and 19 deletions.
  1. +14 −1 lib/prawn/font.rb
  2. +1 −18 lib/prawn/font/afm.rb
View
@@ -226,6 +226,10 @@ def inspect
"#{self.class.name}< #{name}: #{size} >"
end
+ def unscaled_width_of(string)
+ raise NotImplementedError, "subclasses of Prawn::Font must implement #width_of"
+ end
+
# Returns the width of the given string using the given font. If :size is not
# specified as one of the options, the string is measured using the current
# font size. You can also pass :kerning as an option to indicate whether
@@ -235,8 +239,17 @@ def inspect
# For AFM fonts, this is WinAnsi. For TTF, make sure the font is encoded as
# UTF-8. You can use the #normalize_encoding method to make sure strings
# are in an encoding appropriate for the font.
+ #
def width_of(string, options={})
- raise NotImplementedError, "subclasses of Prawn::Font must implement #width_of"
+ scale = (options[:size] || size) / 1000.0
+
+ if options[:kerning]
+ strings, numbers = kern(string).partition { |e| e.is_a?(String) }
+ total_kerning_offset = numbers.inject(0.0) { |s,r| s + r }
+ (unscaled_width_of(strings.join) - total_kerning_offset) * scale
+ else
+ unscaled_width_of(string) * scale
+ end
end
# Normalizes the encoding of the string to an encoding supported by the font.
View
@@ -49,22 +49,6 @@ def bbox
@bbox ||= @attributes['fontbbox'].split(/\s+/).map { |e| Integer(e) }
end
- # calculates the width of the supplied string.
- #
- # String *must* be encoded as WinAnsi
- #
- def width_of(string, options={})
- scale = (options[:size] || size) / 1000.0
-
- if options[:kerning]
- strings, numbers = kern(string).partition { |e| e.is_a?(String) }
- total_kerning_offset = numbers.inject(0.0) { |s,r| s + r }
- (unscaled_width_of(strings.join) - total_kerning_offset) * scale
- else
- unscaled_width_of(string) * scale
- end
- end
-
def has_kerning_data?
@kern_pairs.any?
end
@@ -177,8 +161,7 @@ def kern(string)
def latin_kern_pairs_table
@kern_pairs_table ||= @kern_pairs.inject({}) do |h,p|
- h[p[0].map { |n| Encoding::WinAnsi::CHARACTERS.index(n) }] = p[1]
- h
+ h[p[0].map { |n| Encoding::WinAnsi::CHARACTERS.index(n) }] = p[1]; h
end
end

0 comments on commit 6155bd0

Please sign in to comment.