Class: Idml::TextEngine::PdfrbFontMetrics
- Inherits:
-
Object
- Object
- Idml::TextEngine::PdfrbFontMetrics
- Defined in:
- lib/idml/text_engine/pdfrb_font_metrics.rb
Overview
Adapter that exposes the same measurement surface as
FontMetrics (the Fontisan-based parser) but delegates to
pdfrb's Fonts collection + a registered font resource Symbol.
Once a font is registered with the pdfrb document via
document.fonts.add(path), pdfrb parses the TTF tables (cmap,
hmtx, head, hhea) and exposes real per-glyph advance widths
through Fonts#glyph_width(resource, codepoint) and
Fonts#metrics_for(resource). This adapter wraps that pair so
the text engine (Shaper, LineBreaker) can use real metrics
without any Fontisan dependency.
Instance Attribute Summary collapse
-
#fonts ⇒ Object
readonly
Returns the value of attribute fonts.
-
#resource ⇒ Object
readonly
Returns the value of attribute resource.
Instance Method Summary collapse
- #ascent ⇒ Object
- #descent ⇒ Object
- #family_name ⇒ Object
-
#glyph_width(codepoint) ⇒ Object
Returns raw advance width in font units (not scaled by size).
-
#initialize(fonts_collection, resource) ⇒ PdfrbFontMetrics
constructor
A new instance of PdfrbFontMetrics.
- #kerning_pair(_left_cp, _right_cp) ⇒ Object
- #line_gap ⇒ Object
- #measure_text(text, size:) ⇒ Object
- #path ⇒ Object
- #postscript_name ⇒ Object
- #style_name ⇒ Object
- #units_per_em ⇒ Object
Constructor Details
#initialize(fonts_collection, resource) ⇒ PdfrbFontMetrics
Returns a new instance of PdfrbFontMetrics.
19 20 21 22 |
# File 'lib/idml/text_engine/pdfrb_font_metrics.rb', line 19 def initialize(fonts_collection, resource) @fonts = fonts_collection @resource = resource end |
Instance Attribute Details
#fonts ⇒ Object (readonly)
Returns the value of attribute fonts.
17 18 19 |
# File 'lib/idml/text_engine/pdfrb_font_metrics.rb', line 17 def fonts @fonts end |
#resource ⇒ Object (readonly)
Returns the value of attribute resource.
17 18 19 |
# File 'lib/idml/text_engine/pdfrb_font_metrics.rb', line 17 def resource @resource end |
Instance Method Details
#ascent ⇒ Object
28 29 30 |
# File 'lib/idml/text_engine/pdfrb_font_metrics.rb', line 28 def ascent metrics_data[:ascent] || 800 end |
#descent ⇒ Object
32 33 34 |
# File 'lib/idml/text_engine/pdfrb_font_metrics.rb', line 32 def descent metrics_data[:descent] || -200 end |
#family_name ⇒ Object
62 63 64 |
# File 'lib/idml/text_engine/pdfrb_font_metrics.rb', line 62 def family_name postscript_name end |
#glyph_width(codepoint) ⇒ Object
Returns raw advance width in font units (not scaled by size).
41 42 43 44 |
# File 'lib/idml/text_engine/pdfrb_font_metrics.rb', line 41 def glyph_width(codepoint) cp = codepoint.is_a?(String) ? codepoint.each_codepoint.first : codepoint @fonts.glyph_width(@resource, cp).to_i end |
#kerning_pair(_left_cp, _right_cp) ⇒ Object
54 55 56 |
# File 'lib/idml/text_engine/pdfrb_font_metrics.rb', line 54 def kerning_pair(_left_cp, _right_cp) 0 end |
#line_gap ⇒ Object
36 37 38 |
# File 'lib/idml/text_engine/pdfrb_font_metrics.rb', line 36 def line_gap 0 end |
#measure_text(text, size:) ⇒ Object
46 47 48 49 50 51 52 |
# File 'lib/idml/text_engine/pdfrb_font_metrics.rb', line 46 def measure_text(text, size:) return 0.0 if text.nil? || text.empty? text.each_codepoint.sum do |cp| glyph_width(cp) * size.to_f / units_per_em end end |
#path ⇒ Object
70 71 72 |
# File 'lib/idml/text_engine/pdfrb_font_metrics.rb', line 70 def path nil end |
#postscript_name ⇒ Object
58 59 60 |
# File 'lib/idml/text_engine/pdfrb_font_metrics.rb', line 58 def postscript_name @resource.to_s end |
#style_name ⇒ Object
66 67 68 |
# File 'lib/idml/text_engine/pdfrb_font_metrics.rb', line 66 def style_name "Regular" end |
#units_per_em ⇒ Object
24 25 26 |
# File 'lib/idml/text_engine/pdfrb_font_metrics.rb', line 24 def units_per_em metrics_data[:units_per_em] || 1000 end |