From 468bc3e9acdbbd2d21640c4ac213bf177b22509b Mon Sep 17 00:00:00 2001 From: Mouad Debbar Date: Wed, 11 Nov 2020 15:49:46 -0800 Subject: [PATCH] [web] Reuse the existing font string builer in TextStyle (#22444) --- lib/web_ui/lib/src/engine/text/paragraph.dart | 14 ++++ lib/web_ui/lib/src/engine/text/ruler.dart | 72 +++++++++++-------- 2 files changed, 56 insertions(+), 30 deletions(-) diff --git a/lib/web_ui/lib/src/engine/text/paragraph.dart b/lib/web_ui/lib/src/engine/text/paragraph.dart index d0d16528867..d956e520e62 100644 --- a/lib/web_ui/lib/src/engine/text/paragraph.dart +++ b/lib/web_ui/lib/src/engine/text/paragraph.dart @@ -1021,6 +1021,20 @@ class EngineTextStyle implements ui.TextStyle { return _fontFamily; } + String? _cssFontString; + + /// Font string to be used in CSS. + /// + /// See . + String get cssFontString { + return _cssFontString ??= _buildCssFontString( + fontStyle: _fontStyle, + fontWeight: _fontWeight, + fontSize: _fontSize, + fontFamily: _effectiveFontFamily, + ); + } + @override bool operator ==(Object other) { if (identical(this, other)) { diff --git a/lib/web_ui/lib/src/engine/text/ruler.dart b/lib/web_ui/lib/src/engine/text/ruler.dart index cfd6d2c6ae9..2883d83d56e 100644 --- a/lib/web_ui/lib/src/engine/text/ruler.dart +++ b/lib/web_ui/lib/src/engine/text/ruler.dart @@ -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 . - 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