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



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

#cap_heightObject

Cap height in font units; nil when the font tables don't report one (some pdfrb-loaded fonts).



42
43
44
# File 'lib/idml/text_engine/pdfrb_font_metrics.rb', line 42

def cap_height
  metrics_data[:cap_height]
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



68
69
70
# File 'lib/idml/text_engine/pdfrb_font_metrics.rb', line 68

def family_name
  postscript_name
end

#glyph_width(codepoint) ⇒ Object

Returns raw advance width in font units (not scaled by size).



47
48
49
50
# File 'lib/idml/text_engine/pdfrb_font_metrics.rb', line 47

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



60
61
62
# File 'lib/idml/text_engine/pdfrb_font_metrics.rb', line 60

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



52
53
54
55
56
57
58
# File 'lib/idml/text_engine/pdfrb_font_metrics.rb', line 52

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



76
77
78
# File 'lib/idml/text_engine/pdfrb_font_metrics.rb', line 76

def path
  nil
end

#postscript_nameObject



64
65
66
# File 'lib/idml/text_engine/pdfrb_font_metrics.rb', line 64

def postscript_name
  @resource.to_s
end

#style_nameObject



72
73
74
# File 'lib/idml/text_engine/pdfrb_font_metrics.rb', line 72

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