Class: Arrolio::FontMetrics::AfmMetrics
- Inherits:
-
Object
- Object
- Arrolio::FontMetrics::AfmMetrics
- Defined in:
- lib/arrolio/font_metrics/afm_metrics.rb
Overview
Glyph-width lookup against Pdfrb's AFM data for one of the 14 PDF standard Type1 fonts.
Constant Summary collapse
- UNITS_PER_EM =
1000
Instance Attribute Summary collapse
-
#afm ⇒ Object
readonly
Returns the value of attribute afm.
-
#font_name ⇒ Object
readonly
Returns the value of attribute font_name.
Class Method Summary collapse
Instance Method Summary collapse
- #advance_width(char) ⇒ Object
- #ascender(font_size: 12) ⇒ Object
- #cap_height(font_size: 12) ⇒ Object
- #descender(font_size: 12) ⇒ Object
-
#initialize(font_name, afm) ⇒ AfmMetrics
constructor
A new instance of AfmMetrics.
- #line_height(font_size:, line_spacing: 1.2) ⇒ Object
- #units_per_em ⇒ Object
- #width_of_string(str, font_size:) ⇒ Object
Constructor Details
#initialize(font_name, afm) ⇒ AfmMetrics
Returns a new instance of AfmMetrics.
12 13 14 15 |
# File 'lib/arrolio/font_metrics/afm_metrics.rb', line 12 def initialize(font_name, afm) @font_name = font_name.to_s @afm = afm end |
Instance Attribute Details
#afm ⇒ Object (readonly)
Returns the value of attribute afm.
10 11 12 |
# File 'lib/arrolio/font_metrics/afm_metrics.rb', line 10 def afm @afm end |
#font_name ⇒ Object (readonly)
Returns the value of attribute font_name.
10 11 12 |
# File 'lib/arrolio/font_metrics/afm_metrics.rb', line 10 def font_name @font_name end |
Class Method Details
.for_name(name) ⇒ Object
17 18 19 20 21 22 |
# File 'lib/arrolio/font_metrics/afm_metrics.rb', line 17 def self.for_name(name) afm = Pdfrb::Font::Type1::FontMetrics.for_standard_font(name.to_s) return nil unless afm new(name, afm) end |
Instance Method Details
#advance_width(char) ⇒ Object
28 29 30 31 |
# File 'lib/arrolio/font_metrics/afm_metrics.rb', line 28 def advance_width(char) metric = metric_for(char) metric ? metric[:width] : 0 end |
#ascender(font_size: 12) ⇒ Object
38 39 40 |
# File 'lib/arrolio/font_metrics/afm_metrics.rb', line 38 def ascender(font_size: 12) (afm.ascender || 800) * font_size.to_f / units_per_em end |
#cap_height(font_size: 12) ⇒ Object
46 47 48 |
# File 'lib/arrolio/font_metrics/afm_metrics.rb', line 46 def cap_height(font_size: 12) (afm.cap_height || 700) * font_size.to_f / units_per_em end |
#descender(font_size: 12) ⇒ Object
42 43 44 |
# File 'lib/arrolio/font_metrics/afm_metrics.rb', line 42 def descender(font_size: 12) (afm.descender || -200) * font_size.to_f / units_per_em end |
#line_height(font_size:, line_spacing: 1.2) ⇒ Object
50 51 52 53 |
# File 'lib/arrolio/font_metrics/afm_metrics.rb', line 50 def line_height(font_size:, line_spacing: 1.2) ((afm.ascender || 800).to_f - (afm.descender || -200).to_f) * font_size.to_f / units_per_em * line_spacing.to_f end |
#units_per_em ⇒ Object
24 25 26 |
# File 'lib/arrolio/font_metrics/afm_metrics.rb', line 24 def units_per_em UNITS_PER_EM end |
#width_of_string(str, font_size:) ⇒ Object
33 34 35 36 |
# File 'lib/arrolio/font_metrics/afm_metrics.rb', line 33 def width_of_string(str, font_size:) scale = font_size.to_f / units_per_em str.each_char.sum { |c| advance_width(c) * scale } end |