fluent2WebBaselineDrop function
How far below a browser's baseline Flutter sets words in style in
their line box, in logical pixels (negative: above it). Both lead a line
as CSS does, the room the face leaves in the line height split about its
ascent and descent; Blink (the Chromium browsers, where Fluent's own
fields render) first rounds the face's ascent and its descent each to a whole
pixel and then floors the leaded ascent (SimpleFontData::PlatformInit,
FontHeight::AddLeading), where Flutter keeps the fractions. Read off
the face itself: Selawik's ascent and descent are 2027 and 431 of 2048,
so caption1 (12 on 16) stands 12.68 down its line box in Flutter and 12
in Blink, body1 (14 on 20) 15.46 and 15, body2 (16 on 22) 17.23 and 17,
subtitle1 (20 on 28) 21.79 and 22. Zero for a style with no line height
of its own (its words fill the line box in both).
Implementation
double fluent2WebBaselineDrop(TextStyle style) {
if (style.height == null || style.fontSize == null) return 0;
if (_dropsHeard == null) {
// A face that loads later measures anew.
_dropsHeard = _drops.clear;
PaintingBinding.instance.systemFonts.addListener(_dropsHeard!);
}
return _drops[style] ??= () {
final face = TextStyle(
fontFamily: style.fontFamily,
fontFamilyFallback: style.fontFamilyFallback,
fontSize: style.fontSize,
fontWeight: style.fontWeight,
fontStyle: style.fontStyle,
fontVariations: style.fontVariations,
fontFeatures: style.fontFeatures,
);
final bare = TextPainter(
text: TextSpan(text: 'x', style: face),
textDirection: TextDirection.ltr,
)..layout();
final f = bare.computeLineMetrics().first;
bare.dispose();
final led = TextPainter(
text: TextSpan(text: 'x', style: face.merge(_leadOf(style))),
textDirection: TextDirection.ltr,
)..layout();
final line = led.computeLineMetrics().first;
led.dispose();
// Blink keeps a line height to 1/64 of a pixel.
final height = (line.height * 64).roundToDouble() / 64;
final ascent = f.ascent.roundToDouble();
final descent = f.descent.roundToDouble();
final blink = (ascent + (height - ascent - descent) / 2).floorToDouble();
return line.baseline - blink;
}();
}