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
-
.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.
- .pango_layout ⇒ Object
-
.visual_centre(font, fontsize) ⇒ Object
How far below the baseline the visual centre of a line of text sits.
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)).
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/rsyntaxtree/utils.rb', line 154 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 ink, = layout.pixel_extents baseline = layout.baseline / Pango::SCALE.to_f Metrics.new(width, fontsize * LINE_HEIGHT_FACTOR, ink.height, baseline - ink.y) end |
.pango_layout ⇒ Object
171 172 173 |
# File 'lib/rsyntaxtree/utils.rb', line 171 def pango_layout @pango_layout ||= Pango::Layout.new(Pango::CairoFontMap.default.create_context) end |
.visual_centre(font, fontsize) ⇒ Object
How far below the baseline the visual centre of a line of text sits.
Text is placed by its baseline, so anything meant to sit level with it — a
box drawn around it, a rule beside it — has to know where the middle of the
marks is. The answer is half the height of a capital, not half of whatever
is written: centring on the marks themselves puts the middle of an s
lower than the middle of a G, and centring on the whole cap-to-descender
band sits low around digits and capitals, which reach nothing below the
baseline and are most of what a label holds.
146 147 148 |
# File 'lib/rsyntaxtree/utils.rb', line 146 def visual_centre(font, fontsize) get_metrics("X", font, fontsize, :normal, :normal).ink_above / 2.0 end |