Class: Idml::TextEngine::PdfrbFontMetrics

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

Instance Method Summary collapse

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

#fontsObject (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

#resourceObject (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

#ascentObject



28
29
30
# File 'lib/idml/text_engine/pdfrb_font_metrics.rb', line 28

def ascent
  metrics_data[:ascent] || 800
end

#descentObject



32
33
34
# File 'lib/idml/text_engine/pdfrb_font_metrics.rb', line 32

def descent
  metrics_data[:descent] || -200
end

#family_nameObject



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_gapObject



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

#pathObject



70
71
72
# File 'lib/idml/text_engine/pdfrb_font_metrics.rb', line 70

def path
  nil
end

#postscript_nameObject



58
59
60
# File 'lib/idml/text_engine/pdfrb_font_metrics.rb', line 58

def postscript_name
  @resource.to_s
end

#style_nameObject



66
67
68
# File 'lib/idml/text_engine/pdfrb_font_metrics.rb', line 66

def style_name
  "Regular"
end

#units_per_emObject



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