Class: Idml::TextEngine::FontMetrics
- Inherits:
-
Object
- Object
- Idml::TextEngine::FontMetrics
- Defined in:
- lib/idml/text_engine/font_metrics.rb
Overview
Reads OpenType/TrueType font metrics via Fontisan. Uses Fontisan's FontLoader to open the font file and locate table offsets, then parses the head, hhea, hmtx, and cmap tables directly from the binary. No ttfunk.
Instance Attribute Summary collapse
-
#ascent ⇒ Object
readonly
Returns the value of attribute ascent.
-
#descent ⇒ Object
readonly
Returns the value of attribute descent.
-
#line_gap ⇒ Object
readonly
Returns the value of attribute line_gap.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#units_per_em ⇒ Object
readonly
Returns the value of attribute units_per_em.
Class Method Summary collapse
Instance Method Summary collapse
- #family_name ⇒ Object
- #glyph_width(codepoint) ⇒ Object
-
#initialize(path) ⇒ FontMetrics
constructor
A new instance of FontMetrics.
- #kerning_pair(_left_cp, _right_cp) ⇒ Object
- #measure_text(text, size:) ⇒ Object
- #postscript_name ⇒ Object
- #style_name ⇒ Object
Constructor Details
#initialize(path) ⇒ FontMetrics
Returns a new instance of FontMetrics.
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/idml/text_engine/font_metrics.rb', line 22 def initialize(path) @path = path @font = Fontisan::FontLoader.load(path) @io = File.open(path, "rb") parse_head parse_hhea parse_hmtx parse_cmap @width_cache = {} end |
Instance Attribute Details
#ascent ⇒ Object (readonly)
Returns the value of attribute ascent.
33 34 35 |
# File 'lib/idml/text_engine/font_metrics.rb', line 33 def ascent @ascent end |
#descent ⇒ Object (readonly)
Returns the value of attribute descent.
33 34 35 |
# File 'lib/idml/text_engine/font_metrics.rb', line 33 def descent @descent end |
#line_gap ⇒ Object (readonly)
Returns the value of attribute line_gap.
33 34 35 |
# File 'lib/idml/text_engine/font_metrics.rb', line 33 def line_gap @line_gap end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
33 34 35 |
# File 'lib/idml/text_engine/font_metrics.rb', line 33 def path @path end |
#units_per_em ⇒ Object (readonly)
Returns the value of attribute units_per_em.
33 34 35 |
# File 'lib/idml/text_engine/font_metrics.rb', line 33 def units_per_em @units_per_em end |
Class Method Details
.clear_cache ⇒ Object
18 19 20 |
# File 'lib/idml/text_engine/font_metrics.rb', line 18 def self.clear_cache @cache.clear end |
.open(path) ⇒ Object
14 15 16 |
# File 'lib/idml/text_engine/font_metrics.rb', line 14 def self.open(path) @cache[path] ||= new(path) end |
Instance Method Details
#family_name ⇒ Object
58 59 60 |
# File 'lib/idml/text_engine/font_metrics.rb', line 58 def family_name @family_name ||= parse_name_entry(1) || "Unknown" end |
#glyph_width(codepoint) ⇒ Object
35 36 37 38 39 40 41 42 |
# File 'lib/idml/text_engine/font_metrics.rb', line 35 def glyph_width(codepoint) @width_cache[codepoint] ||= begin glyph_id = @code_map[codepoint] || 0 metric = @advance_widths[glyph_id] metric || @advance_widths.last || 0 end end |
#kerning_pair(_left_cp, _right_cp) ⇒ Object
44 45 46 |
# File 'lib/idml/text_engine/font_metrics.rb', line 44 def kerning_pair(_left_cp, _right_cp) 0 end |
#measure_text(text, size:) ⇒ Object
48 49 50 51 52 |
# File 'lib/idml/text_engine/font_metrics.rb', line 48 def measure_text(text, size:) total = 0 text.each_codepoint { |cp| total += glyph_width(cp) } total.to_f / units_per_em * size end |
#postscript_name ⇒ Object
54 55 56 |
# File 'lib/idml/text_engine/font_metrics.rb', line 54 def postscript_name @postscript_name ||= parse_name_entry(6) || "Unknown" end |
#style_name ⇒ Object
62 63 64 |
# File 'lib/idml/text_engine/font_metrics.rb', line 62 def style_name @style_name ||= parse_name_entry(2) || "Regular" end |