Class: Fontisan::Tables::HmtxTable
- Defined in:
- lib/fontisan/tables/hmtx_table.rb
Overview
OOP representation of the 'hmtx' (Horizontal Metrics) table
The hmtx table contains horizontal metrics for each glyph in the font, providing advance width and left sidebearing values needed for proper glyph positioning and text layout.
This class extends SfntTable to provide hmtx-specific convenience methods for accessing glyph metrics. The hmtx table requires context from the hhea and maxp tables to function properly.
Instance Attribute Summary collapse
-
#hhea_table ⇒ Object
readonly
Cache for context tables.
-
#maxp_table ⇒ Object
readonly
Cache for context tables.
Attributes inherited from SfntTable
Instance Method Summary collapse
-
#advance_width_for(glyph_id) ⇒ Integer?
Get advance width for a glyph.
-
#all_advance_widths ⇒ Array<Integer>
Get all advance widths.
-
#all_lsbs ⇒ Array<Integer>
Get all left sidebearings.
-
#has_context? ⇒ Boolean
Check if table has been parsed with context.
-
#has_unique_metrics?(glyph_id) ⇒ Boolean
Check if a glyph has its own metrics.
-
#lsb_for(glyph_id) ⇒ Integer?
Get left sidebearing for a glyph.
-
#metric_for(glyph_id) ⇒ Hash?
Get horizontal metrics for a glyph.
-
#num_glyphs ⇒ Integer?
Get total number of glyphs.
-
#number_of_h_metrics ⇒ Integer?
Get number of horizontal metrics.
-
#parse_with_context(number_of_h_metrics, num_glyphs) ⇒ self
Parse the hmtx table with required context.
-
#statistics ⇒ Hash
Get metrics statistics.
Methods inherited from SfntTable
#available?, #calculate_checksum, #checksum, #data_loaded?, #human_name, #initialize, #inspect, #length, #load_data!, #offset, #parse, #parsed?, #required?, #tag, #to_s, #validate!
Constructor Details
This class inherits a constructor from Fontisan::SfntTable
Instance Attribute Details
#hhea_table ⇒ Object (readonly)
Cache for context tables
26 27 28 |
# File 'lib/fontisan/tables/hmtx_table.rb', line 26 def hhea_table @hhea_table end |
#maxp_table ⇒ Object (readonly)
Cache for context tables
26 27 28 |
# File 'lib/fontisan/tables/hmtx_table.rb', line 26 def maxp_table @maxp_table end |
Instance Method Details
#advance_width_for(glyph_id) ⇒ Integer?
Get advance width for a glyph
77 78 79 80 |
# File 'lib/fontisan/tables/hmtx_table.rb', line 77 def advance_width_for(glyph_id) metric = metric_for(glyph_id) metric&.dig(:advance_width) end |
#all_advance_widths ⇒ Array<Integer>
Get all advance widths
127 128 129 130 131 |
# File 'lib/fontisan/tables/hmtx_table.rb', line 127 def all_advance_widths return [] unless has_context? (0...num_glyphs).map { |gid| advance_width_for(gid) || 0 } end |
#all_lsbs ⇒ Array<Integer>
Get all left sidebearings
136 137 138 139 140 |
# File 'lib/fontisan/tables/hmtx_table.rb', line 136 def all_lsbs return [] unless has_context? (0...num_glyphs).map { |gid| lsb_for(gid) || 0 } end |
#has_context? ⇒ Boolean
Check if table has been parsed with context
57 58 59 |
# File 'lib/fontisan/tables/hmtx_table.rb', line 57 def has_context? !@number_of_h_metrics.nil? && !@num_glyphs.nil? end |
#has_unique_metrics?(glyph_id) ⇒ Boolean
Check if a glyph has its own metrics
Glyphs with ID < numberOfHMetrics have their own advance width
117 118 119 120 121 122 |
# File 'lib/fontisan/tables/hmtx_table.rb', line 117 def has_unique_metrics?(glyph_id) num = number_of_h_metrics return false if num.nil? glyph_id < num end |
#lsb_for(glyph_id) ⇒ Integer?
Get left sidebearing for a glyph
86 87 88 89 |
# File 'lib/fontisan/tables/hmtx_table.rb', line 86 def lsb_for(glyph_id) metric = metric_for(glyph_id) metric&.dig(:lsb) end |
#metric_for(glyph_id) ⇒ Hash?
Get horizontal metrics for a glyph
66 67 68 69 70 71 |
# File 'lib/fontisan/tables/hmtx_table.rb', line 66 def metric_for(glyph_id) ensure_context! return nil unless parsed parsed.metric_for(glyph_id) end |
#num_glyphs ⇒ Integer?
Get total number of glyphs
104 105 106 107 108 109 |
# File 'lib/fontisan/tables/hmtx_table.rb', line 104 def num_glyphs return @num_glyphs if @num_glyphs return nil unless parsed parsed.num_glyphs end |
#number_of_h_metrics ⇒ Integer?
Get number of horizontal metrics
94 95 96 97 98 99 |
# File 'lib/fontisan/tables/hmtx_table.rb', line 94 def number_of_h_metrics return @number_of_h_metrics if @number_of_h_metrics return nil unless parsed parsed.number_of_h_metrics end |
#parse_with_context(number_of_h_metrics, num_glyphs) ⇒ self
Parse the hmtx table with required context
The hmtx table cannot be used without context from hhea and maxp tables.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/fontisan/tables/hmtx_table.rb', line 36 def parse_with_context(number_of_h_metrics, num_glyphs) unless number_of_h_metrics && num_glyphs raise ArgumentError, "hmtx table requires number_of_h_metrics and num_glyphs" end @number_of_h_metrics = number_of_h_metrics @num_glyphs = num_glyphs # Ensure parsed data is loaded parse # Parse with context parsed.parse_with_context(number_of_h_metrics, num_glyphs) self end |
#statistics ⇒ Hash
Get metrics statistics
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/fontisan/tables/hmtx_table.rb', line 145 def statistics return {} unless has_context? widths = all_advance_widths lsbs = all_lsbs { num_glyphs: num_glyphs, number_of_h_metrics: number_of_h_metrics, min_advance_width: widths.min, max_advance_width: widths.max, avg_advance_width: widths.sum.fdiv(widths.size).round(2), min_lsb: lsbs.min, max_lsb: lsbs.max, } end |