Web HTML support for variable fonts (flutter/engine#32327)
This commit is contained in:
@@ -52,6 +52,7 @@ class CompileTestsStep implements PipelineStep {
|
||||
const Map<String, String> _kTestFonts = <String, String>{
|
||||
'Ahem': 'ahem.ttf',
|
||||
'Roboto': 'Roboto-Regular.ttf',
|
||||
'RobotoVariable': 'RobotoSlab-VariableFont_wght.ttf',
|
||||
'Noto Naskh Arabic UI': 'NotoNaskhArabic-Regular.ttf',
|
||||
'Noto Color Emoji': 'NotoColorEmoji.ttf',
|
||||
};
|
||||
|
||||
@@ -369,6 +369,7 @@ abstract class StyleNode {
|
||||
fontFamily: _fontFamily,
|
||||
fontFamilyFallback: _fontFamilyFallback,
|
||||
fontFeatures: _fontFeatures,
|
||||
fontVariations: _fontVariations,
|
||||
fontSize: _fontSize,
|
||||
letterSpacing: _letterSpacing,
|
||||
wordSpacing: _wordSpacing,
|
||||
@@ -393,6 +394,7 @@ abstract class StyleNode {
|
||||
String get _fontFamily;
|
||||
List<String>? get _fontFamilyFallback;
|
||||
List<ui.FontFeature>? get _fontFeatures;
|
||||
List<ui.FontVariation>? get _fontVariations;
|
||||
double get _fontSize;
|
||||
double? get _letterSpacing;
|
||||
double? get _wordSpacing;
|
||||
@@ -447,6 +449,9 @@ class ChildStyleNode extends StyleNode {
|
||||
@override
|
||||
List<ui.FontFeature>? get _fontFeatures => style.fontFeatures ?? parent._fontFeatures;
|
||||
|
||||
@override
|
||||
List<ui.FontVariation>? get _fontVariations => style.fontVariations ?? parent._fontVariations;
|
||||
|
||||
@override
|
||||
double get _fontSize => style.fontSize ?? parent._fontSize;
|
||||
|
||||
@@ -521,6 +526,9 @@ class RootStyleNode extends StyleNode {
|
||||
@override
|
||||
List<ui.FontFeature>? get _fontFeatures => null;
|
||||
|
||||
@override
|
||||
List<ui.FontVariation>? get _fontVariations => null;
|
||||
|
||||
@override
|
||||
double get _fontSize => paragraphStyle.fontSize ?? FlutterViewEmbedder.defaultFontSize;
|
||||
|
||||
@@ -638,6 +646,13 @@ class CanvasParagraphBuilder implements ui.ParagraphBuilder {
|
||||
}
|
||||
}
|
||||
|
||||
if (_drawOnCanvas) {
|
||||
final List<ui.FontVariation>? fontVariations = style.fontVariations;
|
||||
if (fontVariations != null && fontVariations.isNotEmpty) {
|
||||
_drawOnCanvas = false;
|
||||
}
|
||||
}
|
||||
|
||||
_spans.add(FlatTextSpan(style: style, start: start, end: end));
|
||||
}
|
||||
|
||||
|
||||
@@ -17,6 +17,8 @@ const String ahemFontFamily = 'Ahem';
|
||||
const String ahemFontUrl = '/assets/fonts/ahem.ttf';
|
||||
const String robotoFontFamily = 'Roboto';
|
||||
const String robotoTestFontUrl = '/assets/fonts/Roboto-Regular.ttf';
|
||||
const String robotoVariableFontFamily = 'RobotoVariable';
|
||||
const String robotoVariableTestFontUrl = '/assets/fonts/RobotoSlab-VariableFont_wght.ttf';
|
||||
|
||||
/// This class is responsible for registering and loading fonts.
|
||||
///
|
||||
@@ -87,6 +89,8 @@ class FontCollection {
|
||||
ahemFontFamily, 'url($ahemFontUrl)', const <String, String>{});
|
||||
_testFontManager!.registerAsset(robotoFontFamily,
|
||||
'url($robotoTestFontUrl)', const <String, String>{});
|
||||
_testFontManager!.registerAsset(robotoVariableFontFamily,
|
||||
'url($robotoVariableTestFontUrl)', const <String, String>{});
|
||||
}
|
||||
|
||||
/// Returns a [Future] that completes when the registered fonts are loaded
|
||||
|
||||
@@ -333,6 +333,7 @@ class EngineTextStyle implements ui.TextStyle {
|
||||
required ui.Paint? foreground,
|
||||
required List<ui.Shadow>? shadows,
|
||||
required List<ui.FontFeature>? fontFeatures,
|
||||
required List<ui.FontVariation>? fontVariations,
|
||||
}) = EngineTextStyle.only;
|
||||
|
||||
/// Constructs an [EngineTextStyle] with only the given properties.
|
||||
@@ -359,6 +360,7 @@ class EngineTextStyle implements ui.TextStyle {
|
||||
this.foreground,
|
||||
this.shadows,
|
||||
this.fontFeatures,
|
||||
this.fontVariations,
|
||||
}) : assert(
|
||||
color == null || foreground == null,
|
||||
'Cannot provide both a color and a foreground\n'
|
||||
@@ -393,6 +395,7 @@ class EngineTextStyle implements ui.TextStyle {
|
||||
final String fontFamily;
|
||||
final List<String>? fontFamilyFallback;
|
||||
final List<ui.FontFeature>? fontFeatures;
|
||||
final List<ui.FontVariation>? fontVariations;
|
||||
final double? fontSize;
|
||||
final double? letterSpacing;
|
||||
final double? wordSpacing;
|
||||
@@ -440,6 +443,7 @@ class EngineTextStyle implements ui.TextStyle {
|
||||
// TODO(mdebbar): Pass the actual value when font features become supported
|
||||
// https://github.com/flutter/flutter/issues/64595
|
||||
fontFeatures: null,
|
||||
fontVariations: null,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -518,7 +522,8 @@ class EngineTextStyle implements ui.TextStyle {
|
||||
'background: ${background ?? "unspecified"}, '
|
||||
'foreground: ${foreground ?? "unspecified"}, '
|
||||
'shadows: ${shadows ?? "unspecified"}, '
|
||||
'fontFeatures: ${fontFeatures ?? "unspecified"}'
|
||||
'fontFeatures: ${fontFeatures ?? "unspecified"}, '
|
||||
'fontVariations: ${fontVariations ?? "unspecified"}'
|
||||
')';
|
||||
} else {
|
||||
return super.toString();
|
||||
@@ -754,6 +759,11 @@ void applyTextStyleToElement({
|
||||
if (fontFeatures != null && fontFeatures.isNotEmpty) {
|
||||
cssStyle.fontFeatureSettings = _fontFeatureListToCss(fontFeatures);
|
||||
}
|
||||
|
||||
final List<ui.FontVariation>? fontVariations = style.fontVariations;
|
||||
if (fontVariations != null && fontVariations.isNotEmpty) {
|
||||
cssStyle.setProperty('font-variation-settings', _fontVariationListToCss(fontVariations));
|
||||
}
|
||||
}
|
||||
|
||||
String _shadowListToCss(List<ui.Shadow> shadows) {
|
||||
@@ -795,6 +805,21 @@ String _fontFeatureListToCss(List<ui.FontFeature> fontFeatures) {
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
String _fontVariationListToCss(List<ui.FontVariation> fontVariations) {
|
||||
assert(fontVariations.isNotEmpty);
|
||||
|
||||
final StringBuffer sb = StringBuffer();
|
||||
final int len = fontVariations.length;
|
||||
for (int i = 0; i < len; i++) {
|
||||
if (i != 0) {
|
||||
sb.write(',');
|
||||
}
|
||||
final ui.FontVariation fontVariation = fontVariations[i];
|
||||
sb.write('"${fontVariation.axis}" ${fontVariation.value}');
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/// Converts text decoration style to CSS text-decoration-style value.
|
||||
String? _textDecorationToCssString(
|
||||
ui.TextDecoration? decoration, ui.TextDecorationStyle? decorationStyle) {
|
||||
|
||||
@@ -56,12 +56,14 @@ class TextHeightStyle {
|
||||
required this.fontSize,
|
||||
required this.height,
|
||||
required this.fontFeatures,
|
||||
required this.fontVariations,
|
||||
});
|
||||
|
||||
final String fontFamily;
|
||||
final double fontSize;
|
||||
final double? height;
|
||||
final List<ui.FontFeature>? fontFeatures;
|
||||
final List<ui.FontVariation>? fontVariations;
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) {
|
||||
@@ -77,6 +79,7 @@ class TextHeightStyle {
|
||||
fontSize,
|
||||
height,
|
||||
ui.hashList(fontFeatures),
|
||||
ui.hashList(fontVariations),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -339,8 +339,6 @@ abstract class TextStyle {
|
||||
Paint? foreground,
|
||||
List<Shadow>? shadows,
|
||||
List<FontFeature>? fontFeatures,
|
||||
// TODO(jsimmons): implement fontVariations for web
|
||||
// ignore: avoid_unused_constructor_parameters
|
||||
List<FontVariation>? fontVariations,
|
||||
}) {
|
||||
if (engine.useCanvasKit) {
|
||||
@@ -387,6 +385,7 @@ abstract class TextStyle {
|
||||
foreground: foreground,
|
||||
shadows: shadows,
|
||||
fontFeatures: fontFeatures,
|
||||
fontVariations: fontVariations,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -467,6 +467,49 @@ Future<void> testMain() async {
|
||||
return takeScreenshot(canvas, bounds, 'canvas_paragraph_font_features_dom');
|
||||
});
|
||||
|
||||
void testFontVariations(EngineCanvas canvas) {
|
||||
const String text = 'ABCDE 12345\n';
|
||||
FontVariation weight(double w) => FontVariation('wght', w);
|
||||
|
||||
final CanvasParagraph paragraph = rich(
|
||||
EngineParagraphStyle(fontFamily: 'RobotoVariable'),
|
||||
(CanvasParagraphBuilder builder) {
|
||||
builder.pushStyle(EngineTextStyle.only(
|
||||
fontSize: 48.0,
|
||||
));
|
||||
builder.addText(text);
|
||||
builder.pushStyle(EngineTextStyle.only(
|
||||
fontSize: 48.0,
|
||||
fontVariations: <FontVariation>[weight(900)],
|
||||
));
|
||||
builder.addText(text);
|
||||
builder.pushStyle(EngineTextStyle.only(
|
||||
fontSize: 48.0,
|
||||
fontVariations: <FontVariation>[weight(200)],
|
||||
));
|
||||
builder.addText(text);
|
||||
builder.pop();
|
||||
builder.pop();
|
||||
builder.pop();
|
||||
},
|
||||
)..layout(constrain(double.infinity));
|
||||
canvas.drawParagraph(paragraph, Offset.zero);
|
||||
}
|
||||
|
||||
test('font variations', () {
|
||||
const Rect bounds = Rect.fromLTWH(0, 0, 600, 500);
|
||||
final BitmapCanvas canvas = BitmapCanvas(bounds, RenderStrategy());
|
||||
testFontVariations(canvas);
|
||||
return takeScreenshot(canvas, bounds, 'canvas_paragraph_font_variations');
|
||||
});
|
||||
|
||||
test('font variations (DOM)', () {
|
||||
const Rect bounds = Rect.fromLTWH(0, 0, 600, 500);
|
||||
final DomCanvas canvas = DomCanvas(html.document.createElement('flt-picture'));
|
||||
testFontVariations(canvas);
|
||||
return takeScreenshot(canvas, bounds, 'canvas_paragraph_font_variations_dom');
|
||||
});
|
||||
|
||||
void testBackgroundStyle(EngineCanvas canvas) {
|
||||
final CanvasParagraph paragraph = rich(
|
||||
EngineParagraphStyle(fontFamily: 'Roboto', fontSize: 40.0),
|
||||
|
||||
BIN
engine/src/flutter/third_party/txt/third_party/fonts/RobotoSlab-VariableFont_wght.ttf
vendored
Normal file
BIN
engine/src/flutter/third_party/txt/third_party/fonts/RobotoSlab-VariableFont_wght.ttf
vendored
Normal file
Binary file not shown.
Reference in New Issue
Block a user