Class: Idml::TextEngine::FontMetrics

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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

#ascentObject (readonly)

Returns the value of attribute ascent.



33
34
35
# File 'lib/idml/text_engine/font_metrics.rb', line 33

def ascent
  @ascent
end

#descentObject (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_gapObject (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

#pathObject (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_emObject (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_cacheObject



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_nameObject



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_nameObject



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_nameObject



62
63
64
# File 'lib/idml/text_engine/font_metrics.rb', line 62

def style_name
  @style_name ||= parse_name_entry(2) || "Regular"
end