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

Commit 8ead1df

Browse files
authored
[web] Align offset for lines of rich text (#23043)
1 parent bbed7c6 commit 8ead1df

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

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

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,28 @@ class LineBuilder {
318318
bool get isEmpty => _segments.isEmpty;
319319
bool get isNotEmpty => _segments.isNotEmpty;
320320

321+
/// The horizontal offset necessary for the line to be correctly aligned.
322+
double get alignOffset {
323+
final double emptySpace = maxWidth - width;
324+
final ui.TextDirection textDirection =
325+
paragraph.paragraphStyle._textDirection ?? ui.TextDirection.ltr;
326+
final ui.TextAlign textAlign =
327+
paragraph.paragraphStyle._textAlign ?? ui.TextAlign.start;
328+
329+
switch (textAlign) {
330+
case ui.TextAlign.center:
331+
return emptySpace / 2.0;
332+
case ui.TextAlign.right:
333+
return emptySpace;
334+
case ui.TextAlign.start:
335+
return textDirection == ui.TextDirection.rtl ? emptySpace : 0.0;
336+
case ui.TextAlign.end:
337+
return textDirection == ui.TextDirection.rtl ? 0.0 : emptySpace;
338+
default:
339+
return 0.0;
340+
}
341+
}
342+
321343
/// Measures the width of text between the end of this line and [newEnd].
322344
double getAdditionalWidthTo(LineBreakResult newEnd) {
323345
// If the extension is all made of space characters, it shouldn't add
@@ -509,8 +531,7 @@ class LineBuilder {
509531
hardBreak: end.isHard,
510532
width: width + ellipsisWidth,
511533
widthWithTrailingSpaces: widthIncludingSpace + ellipsisWidth,
512-
// TODO(mdebbar): Calculate actual align offset.
513-
left: 0.0,
534+
left: alignOffset,
514535
lineNumber: lineNumber,
515536
);
516537
}

lib/web_ui/test/text/layout_service_plain_test.dart

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import 'package:ui/ui.dart' as ui;
1111

1212
import 'layout_service_helper.dart';
1313

14-
const bool skipTextAlign = true;
1514
const bool skipWordSpacing = true;
1615

1716
final EngineParagraphStyle ahemStyle = EngineParagraphStyle(
@@ -641,7 +640,7 @@ void testMain() async {
641640
l('defgh', 4, 9, hardBreak: false, width: 50.0, left: 0.0),
642641
l('i', 9, 10, hardBreak: true, width: 10.0, left: 40.0),
643642
]);
644-
}, skip: skipTextAlign);
643+
});
645644

646645
test('handles rtl with textAlign', () {
647646
CanvasParagraph paragraph;
@@ -694,5 +693,5 @@ void testMain() async {
694693
l('defgh', 4, 9, hardBreak: false, width: 50.0, left: 0.0),
695694
l('i', 9, 10, hardBreak: true, width: 10.0, left: 40.0),
696695
]);
697-
}, skip: skipTextAlign);
696+
});
698697
}

0 commit comments

Comments
 (0)