Module: FontMetrics

Defined in:
lib/rsyntaxtree/utils.rb

Defined Under Namespace

Classes: Metrics

Constant Summary collapse

LINE_HEIGHT_FACTOR =

Vertical rhythm is defined deterministically from the font size rather than taken from Pango's font-dependent logical extents. The 1.4 factor reproduces the line height the previous RMagick-based measurement reported for the Latin fonts (45px at a 32px font size), preserving the vertical look of existing trees; Pango's own logical height (~1.03x) would compact every tree by roughly 25%. It also makes the rhythm identical across scripts (the old engine gave JP 1.5x and WenQuanYi 1.25x, so mixed-language documents had inconsistent spacing).

1.4

Class Method Summary collapse

Class Method Details

.get_metrics(text, font, fontsize, font_style, font_weight) ⇒ Object

Measures text with Pango, the same engine (and the same font fallback resolution) that librsvg uses to render the SVG output, so widths stay consistent with the drawn output for any script. font is a comma-separated family list (e.g. FontFamily.for_pango(:sans)).



91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/rsyntaxtree/utils.rb', line 91

def get_metrics(text, font, fontsize, font_style, font_weight)
  layout = pango_layout
  desc = Pango::FontDescription.new
  desc.family = font
  # absolute_size is in device pixels, matching the unitless font-size
  # (px) of the SVG text that librsvg renders.
  desc.absolute_size = fontsize * Pango::SCALE
  desc.style = font_style == :italic ? :italic : :normal
  desc.weight = font_weight == :bold ? :bold : :normal
  layout.font_description = desc
  layout.text = text
  width, = layout.pixel_size
  Metrics.new(width, fontsize * LINE_HEIGHT_FACTOR)
end

.pango_layoutObject



106
107
108
# File 'lib/rsyntaxtree/utils.rb', line 106

def pango_layout
  @pango_layout ||= Pango::Layout.new(Pango::CairoFontMap.default.create_context)
end