未验证 提交 468bc3e9 编写于 作者: M Mouad Debbar 提交者: GitHub

[web] Reuse the existing font string builer in TextStyle (#22444)

上级 f62b754e
......@@ -1021,6 +1021,20 @@ class EngineTextStyle implements ui.TextStyle {
return _fontFamily;
}
String? _cssFontString;
/// Font string to be used in CSS.
///
/// See <https://developer.mozilla.org/en-US/docs/Web/CSS/font>.
String get cssFontString {
return _cssFontString ??= _buildCssFontString(
fontStyle: _fontStyle,
fontWeight: _fontWeight,
fontSize: _fontSize,
fontFamily: _effectiveFontFamily,
);
}
@override
bool operator ==(Object other) {
if (identical(this, other)) {
......
......@@ -5,6 +5,41 @@
// @dart = 2.10
part of engine;
String _buildCssFontString({
required ui.FontStyle? fontStyle,
required ui.FontWeight? fontWeight,
required double? fontSize,
required String? fontFamily,
}) {
final StringBuffer result = StringBuffer();
// Font style
if (fontStyle != null) {
result.write(fontStyle == ui.FontStyle.normal ? 'normal' : 'italic');
} else {
result.write(DomRenderer.defaultFontStyle);
}
result.write(' ');
// Font weight.
if (fontWeight != null) {
result.write(fontWeightToCss(fontWeight));
} else {
result.write(DomRenderer.defaultFontWeight);
}
result.write(' ');
if (fontSize != null) {
result.write(fontSize.floor());
} else {
result.write(DomRenderer.defaultFontSize);
}
result.write('px ');
result.write(canonicalizeFontFamily(fontFamily));
return result.toString();
}
/// Contains the subset of [ui.ParagraphStyle] properties that affect layout.
class ParagraphGeometricStyle {
ParagraphGeometricStyle({
......@@ -65,36 +100,13 @@ class ParagraphGeometricStyle {
/// Cached font string that can be used in CSS.
///
/// See <https://developer.mozilla.org/en-US/docs/Web/CSS/font>.
String get cssFontString => _cssFontString ??= _buildCssFontString();
String _buildCssFontString() {
final StringBuffer result = StringBuffer();
// Font style
if (fontStyle != null) {
result.write(fontStyle == ui.FontStyle.normal ? 'normal' : 'italic');
} else {
result.write(DomRenderer.defaultFontStyle);
}
result.write(' ');
// Font weight.
if (fontWeight != null) {
result.write(fontWeightToCss(fontWeight));
} else {
result.write(DomRenderer.defaultFontWeight);
}
result.write(' ');
if (fontSize != null) {
result.write(fontSize!.floor());
} else {
result.write(DomRenderer.defaultFontSize);
}
result.write('px ');
result.write(canonicalizeFontFamily(effectiveFontFamily));
return result.toString();
String get cssFontString {
return _cssFontString ??= _buildCssFontString(
fontStyle: fontStyle,
fontWeight: fontWeight,
fontSize: fontSize,
fontFamily: effectiveFontFamily,
);
}
@override
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册