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
29 30 31 |
# File 'lib/fontisan/tables/hmtx_table.rb', line 29 def hhea_table @hhea_table end |
#maxp_table ⇒ Object (readonly)
Cache for context tables
29 30 31 |
# File 'lib/fontisan/tables/hmtx_table.rb', line 29 def maxp_table @maxp_table end |
Instance Method Details
#advance_width_for(glyph_id) ⇒ Integer?
Get advance width for a glyph
80 81 82 83 |
# File 'lib/fontisan/tables/hmtx_table.rb', line 80 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
130 131 132 133 134 |
# File 'lib/fontisan/tables/hmtx_table.rb', line 130 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
139 140 141 142 143 |
# File 'lib/fontisan/tables/hmtx_table.rb', line 139 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
60 61 62 |
# File 'lib/fontisan/tables/hmtx_table.rb', line 60 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
120 121 122 123 124 125 |
# File 'lib/fontisan/tables/hmtx_table.rb', line 120 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
89 90 91 92 |
# File 'lib/fontisan/tables/hmtx_table.rb', line 89 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
69 70 71 72 73 74 |
# File 'lib/fontisan/tables/hmtx_table.rb', line 69 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
107 108 109 110 111 112 |
# File 'lib/fontisan/tables/hmtx_table.rb', line 107 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
97 98 99 100 101 102 |
# File 'lib/fontisan/tables/hmtx_table.rb', line 97 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.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/fontisan/tables/hmtx_table.rb', line 39 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
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/fontisan/tables/hmtx_table.rb', line 148 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 |