Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 7adb07a

Browse files
wip
1 parent e2cac30 commit 7adb07a

File tree

8 files changed

+72
-54
lines changed

8 files changed

+72
-54
lines changed

lib/ui/text.dart

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
// found in the LICENSE file.
44
part of dart.ui;
55

6+
/// A [TextStyle.height] value that indicates the text span should take
7+
/// the height defined by the font, which may not be exactly the height of
8+
/// [TextStyle.fontSize].
9+
const double kTextHeightNone = 0.0;
10+
611
/// Whether to use the italic type variation of glyphs in the font.
712
///
813
/// Some modern fonts allow this to be selected in a more fine-grained manner.
@@ -1687,10 +1692,10 @@ class TextStyle {
16871692
/// * `letterSpacing`: The amount of space (in logical pixels) to add between each letter.
16881693
/// * `wordSpacing`: The amount of space (in logical pixels) to add at each sequence of white-space (i.e. between each word).
16891694
/// * `textBaseline`: The common baseline that should be aligned between this text span and its parent text span, or, for the root text spans, with the line box.
1690-
/// * `height`: The height of this text span, as a multiplier of the font size. Omitting `height` will allow the line height
1695+
/// * `height`: The height of this text span, as a multiplier of the font size. Setting the `height` to `kTextHeightNone` will allow the line height
16911696
/// to take the height as defined by the font, which may not be exactly the height of the fontSize.
1692-
/// * `leadingDistribution`: When `height` is specified, how the extra vertical space should be distributed over and under the text. Defaults
1693-
/// to the paragraph's [TextHeightBehavior] if left unspecified.
1697+
/// * `leadingDistribution`: When `height` is set to a non-null that is not `kTextHeightNone`, how the extra vertical space should be distributed over and under the text.
1698+
/// Defaults to the paragraph's [TextHeightBehavior] if left unspecified.
16941699
/// * `locale`: The locale used to select region-specific glyphs.
16951700
/// * `background`: The paint drawn as a background for the text.
16961701
/// * `foreground`: The paint used to draw the text. If this is specified, `color` must be null.
@@ -1923,7 +1928,9 @@ Int32List _encodeParagraphStyle(
19231928
result[0] |= 1 << 8;
19241929
// Passed separately to native.
19251930
}
1926-
if (height != null) {
1931+
// Paragraph styles are unique in a paragraph, there is no inheriting so
1932+
// height == null and height == kTextHeightNone are semantically equivalent.
1933+
if (height != null && height != kTextHeightNone) {
19271934
result[0] |= 1 << 9;
19281935
// Passed separately to native.
19291936
}
@@ -1973,9 +1980,10 @@ class ParagraphStyle {
19731980
///
19741981
/// * `height`: The fallback height of the spans as a multiplier of the font
19751982
/// size. The fallback height is used when no height is provided through
1976-
/// [TextStyle.height]. Omitting `height` here and in [TextStyle] will allow
1977-
/// the line height to take the height as defined by the font, which may not
1978-
/// be exactly the height of the `fontSize`.
1983+
/// [TextStyle.height]. Omitting `height` here (or setting it to
1984+
/// [kTextHeightNone]) and in [TextStyle] will allow the line height to take
1985+
/// the height as defined by the font, which may not be exactly the height of
1986+
/// the `fontSize`.
19791987
///
19801988
/// * `textHeightBehavior`: Specifies how the `height` multiplier is
19811989
/// applied to ascent of the first line and the descent of the last line.
@@ -2110,9 +2118,12 @@ ByteData _encodeStrut(
21102118
FontWeight? fontWeight,
21112119
FontStyle? fontStyle,
21122120
bool? forceStrutHeight) {
2121+
// Strut styles are unique in a paragraph, there is no inheriting so
2122+
// height == null and height == kTextHeightNone are semantically equivalent.
2123+
final bool hasHeightOverride = height != null && height != kTextHeightNone;
21132124
if (fontFamily == null &&
21142125
fontSize == null &&
2115-
height == null &&
2126+
!hasHeightOverride &&
21162127
leadingDistribution == null &&
21172128
leading == null &&
21182129
fontWeight == null &&
@@ -2146,7 +2157,7 @@ ByteData _encodeStrut(
21462157
data.setFloat32(byteCount, fontSize, _kFakeHostEndian);
21472158
byteCount += 4;
21482159
}
2149-
if (height != null) {
2160+
if (hasHeightOverride) {
21502161
bitmask |= 1 << 5;
21512162
data.setFloat32(byteCount, height, _kFakeHostEndian);
21522163
byteCount += 4;
@@ -2186,12 +2197,13 @@ class StrutStyle {
21862197
/// * `height`: The minimum height of the line boxes, as a multiplier of the
21872198
/// font size. The lines of the paragraph will be at least
21882199
/// `(height + leading) * fontSize` tall when `fontSize` is not null. Omitting
2189-
/// `height` will allow the minimum line height to take the height as defined
2190-
/// by the font, which may not be exactly the height of the `fontSize`. When
2191-
/// `fontSize` is null, there is no minimum line height. Tall glyphs due to
2192-
/// baseline alignment or large [TextStyle.fontSize] may cause the actual line
2193-
/// height after layout to be taller than specified here. The `fontSize` must
2194-
/// be provided for this property to take effect.
2200+
/// `height` (or setting it to [kTextHeightNone]) will allow the minimum line
2201+
/// height to take the height as defined by the font, which may not be exactly
2202+
/// the height of the `fontSize`. When `fontSize` is null, there is no minimum
2203+
/// line height. Tall glyphs due to baseline alignment or large
2204+
/// [TextStyle.fontSize] may cause the actual line height after layout to be
2205+
/// taller than specified here. The `fontSize` must be provided for
2206+
/// this property to take effect.
21952207
///
21962208
/// * `leading`: The minimum amount of leading between lines as a multiple of
21972209
/// the font size. `fontSize` must be provided for this property to take

lib/ui/text/paragraph_builder.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ void ParagraphBuilder::pushStyle(const tonic::Int32List& encoded,
445445

446446
if (mask & kTSHeightMask) {
447447
style.height = height;
448-
style.has_height_override = true;
448+
style.has_height_override = style.height != 0;
449449
}
450450

451451
if (mask & kTSLocaleMask) {

lib/web_ui/lib/src/engine/canvaskit/text.dart

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class CkParagraphStyle implements ui.ParagraphStyle {
3939
maxLines,
4040
_computeEffectiveFontFamily(fontFamily),
4141
fontSize,
42-
height,
42+
height == ui.kTextHeightNone ? null : height,
4343
textHeightBehavior,
4444
fontWeight,
4545
fontStyle,
@@ -55,7 +55,7 @@ class CkParagraphStyle implements ui.ParagraphStyle {
5555
_originalFontFamily = fontFamily,
5656
_effectiveFontFamily = _computeEffectiveFontFamily(fontFamily),
5757
_fontSize = fontSize,
58-
_height = height,
58+
_height = height == ui.kTextHeightNone ? null : height,
5959
_textHeightBehavior = textHeightBehavior,
6060
_strutStyle = strutStyle,
6161
_ellipsis = ellipsis,
@@ -413,6 +413,9 @@ class CkTextStyle implements ui.TextStyle {
413413
/// The values in this text style are used unless [other] specifically
414414
/// overrides it.
415415
CkTextStyle mergeWith(CkTextStyle other) {
416+
final double? textHeight = other.height == ui.kTextHeightNone
417+
? null
418+
: (other.height ?? height);
416419
return CkTextStyle._(
417420
color: other.color ?? color,
418421
decoration: other.decoration ?? decoration,
@@ -429,7 +432,7 @@ class CkTextStyle implements ui.TextStyle {
429432
fontSize: other.fontSize ?? fontSize,
430433
letterSpacing: other.letterSpacing ?? letterSpacing,
431434
wordSpacing: other.wordSpacing ?? wordSpacing,
432-
height: other.height ?? height,
435+
height: textHeight,
433436
leadingDistribution: other.leadingDistribution ?? leadingDistribution,
434437
locale: other.locale ?? locale,
435438
background: other.background ?? background,
@@ -699,7 +702,7 @@ class CkStrutStyle implements ui.StrutStyle {
699702
}) : _fontFamily = _computeEffectiveFontFamily(fontFamily),
700703
_fontFamilyFallback = ui_web.debugEmulateFlutterTesterEnvironment ? null : fontFamilyFallback,
701704
_fontSize = fontSize,
702-
_height = height,
705+
_height = height == ui.kTextHeightNone ? null : height,
703706
_leadingDistribution = leadingDistribution,
704707
_leading = leading,
705708
_fontWeight = fontWeight,

lib/web_ui/lib/src/engine/skwasm/skwasm_impl/paragraph.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ final class SkwasmStrutStyle extends SkwasmObjectWrapper<RawStrutStyle> implemen
594594
if (fontSize != null) {
595595
strutStyleSetFontSize(handle, fontSize);
596596
}
597-
if (height != null) {
597+
if (height != null || height == kTextHeightNone) {
598598
strutStyleSetHeight(handle, height);
599599
}
600600
if (leadingDistribution != null) {
@@ -734,7 +734,7 @@ class SkwasmParagraphStyle extends SkwasmObjectWrapper<RawParagraphStyle> implem
734734
if (maxLines != null) {
735735
paragraphStyleSetMaxLines(handle, maxLines);
736736
}
737-
if (height != null) {
737+
if (height != null || height != kTextHeightNone) {
738738
paragraphStyleSetHeight(handle, height);
739739
}
740740
if (textHeightBehavior != null) {

lib/web_ui/lib/src/engine/text/canvas_paragraph.dart

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -401,33 +401,29 @@ abstract class StyleNode {
401401
/// The resolved text style is equivalent to the entire ascendent chain of
402402
/// parent style nodes.
403403
EngineTextStyle resolveStyle() {
404-
final EngineTextStyle? style = _cachedStyle;
405-
if (style == null) {
406-
return _cachedStyle ??= EngineTextStyle(
407-
color: _color,
408-
decoration: _decoration,
409-
decorationColor: _decorationColor,
410-
decorationStyle: _decorationStyle,
411-
decorationThickness: _decorationThickness,
412-
fontWeight: _fontWeight,
413-
fontStyle: _fontStyle,
414-
textBaseline: _textBaseline,
415-
fontFamily: _fontFamily,
416-
fontFamilyFallback: _fontFamilyFallback,
417-
fontFeatures: _fontFeatures,
418-
fontVariations: _fontVariations,
419-
fontSize: _fontSize,
420-
letterSpacing: _letterSpacing,
421-
wordSpacing: _wordSpacing,
422-
height: _height,
423-
leadingDistribution: _leadingDistribution,
424-
locale: _locale,
425-
background: _background,
426-
foreground: _foreground,
427-
shadows: _shadows,
428-
);
429-
}
430-
return style;
404+
return _cachedStyle ??= EngineTextStyle(
405+
color: _color,
406+
decoration: _decoration,
407+
decorationColor: _decorationColor,
408+
decorationStyle: _decorationStyle,
409+
decorationThickness: _decorationThickness,
410+
fontWeight: _fontWeight,
411+
fontStyle: _fontStyle,
412+
textBaseline: _textBaseline,
413+
fontFamily: _fontFamily,
414+
fontFamilyFallback: _fontFamilyFallback,
415+
fontFeatures: _fontFeatures,
416+
fontVariations: _fontVariations,
417+
fontSize: _fontSize,
418+
letterSpacing: _letterSpacing,
419+
wordSpacing: _wordSpacing,
420+
height: _height,
421+
leadingDistribution: _leadingDistribution,
422+
locale: _locale,
423+
background: _background,
424+
foreground: _foreground,
425+
shadows: _shadows,
426+
);
431427
}
432428

433429
ui.Color? get _color;
@@ -510,7 +506,9 @@ class ChildStyleNode extends StyleNode {
510506
double? get _wordSpacing => style.wordSpacing ?? parent._wordSpacing;
511507

512508
@override
513-
double? get _height => style.height ?? parent._height;
509+
double? get _height {
510+
return style.height == ui.kTextHeightNone ? null : (style.height ?? parent._height);
511+
}
514512

515513
@override
516514
ui.TextLeadingDistribution? get _leadingDistribution => style.leadingDistribution ?? parent._leadingDistribution;

lib/web_ui/lib/src/engine/text/paragraph.dart

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ class EngineParagraphStyle implements ui.ParagraphStyle {
435435
this.maxLines,
436436
this.fontFamily,
437437
this.fontSize,
438-
this.height,
438+
double? height,
439439
ui.TextHeightBehavior? textHeightBehavior,
440440
this.fontWeight,
441441
this.fontStyle,
@@ -444,7 +444,8 @@ class EngineParagraphStyle implements ui.ParagraphStyle {
444444
this.locale,
445445
}) : _textHeightBehavior = textHeightBehavior,
446446
// TODO(mdebbar): add support for strut style., b/128317744
447-
_strutStyle = strutStyle as EngineStrutStyle?;
447+
_strutStyle = strutStyle as EngineStrutStyle?,
448+
this.height = height == ui.kTextHeightNone ? null : height;
448449

449450
final ui.TextAlign? textAlign;
450451
final ui.TextDirection? textDirection;
@@ -808,7 +809,7 @@ class EngineStrutStyle implements ui.StrutStyle {
808809
}) : _fontFamily = fontFamily,
809810
_fontFamilyFallback = fontFamilyFallback,
810811
_fontSize = fontSize,
811-
_height = height,
812+
_height = height == ui.kTextHeightNone ? null : height,
812813
_leadingDistribution = leadingDistribution,
813814
_leading = leading,
814815
_fontWeight = fontWeight,

lib/web_ui/lib/text.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
part of ui;
66

7+
// This constant must be consistent with `kTextHeightNone` defined in
8+
// flutter/lib/ui/text.dart.
9+
const double kTextHeightNone = 0.0;
10+
711
enum FontStyle {
812
normal,
913
italic,

lib/web_ui/skwasm/text/text_style.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ SKWASM_EXPORT void textStyle_setWordSpacing(TextStyle* style,
9696

9797
SKWASM_EXPORT void textStyle_setHeight(TextStyle* style, SkScalar height) {
9898
style->setHeight(height);
99-
style->setHeightOverride(true);
99+
style->setHeightOverride(height != 0.0);
100100
}
101101

102102
SKWASM_EXPORT void textStyle_setHalfLeading(TextStyle* style,

0 commit comments

Comments
 (0)