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

Commit 616ecd8

Browse files
authored
[Web][HTML] Add mirrored characters support for RTL languages (#39162)
* [Web][HTML] Add mirrored characters support for RTL languages * Fix fontSize in test
1 parent b1ccae7 commit 616ecd8

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,9 @@ class CanvasParagraph implements ui.Paragraph {
174174
}
175175

176176
final DomElement spanElement = domDocument.createElement('flt-span');
177+
if (fragment.textDirection == ui.TextDirection.rtl) {
178+
spanElement.setAttribute('dir', 'rtl');
179+
}
177180
applyTextStyleToElement(element: spanElement, style: fragment.style);
178181
_positionSpanElement(spanElement, line, fragment);
179182

lib/web_ui/test/text/canvas_paragraph_builder_test.dart

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,40 @@ Future<void> testMain() async {
455455
);
456456
debugEmulateFlutterTesterEnvironment = true;
457457
});
458+
459+
// Regression test for https://github.com/flutter/flutter/issues/108431.
460+
// Set dir attribute for RTL fragments in order to let the browser
461+
// handle mirrored characters.
462+
test('Sets "dir" attribute for RTL fragment', () {
463+
final EngineParagraphStyle style = EngineParagraphStyle(
464+
fontSize: 20.0,
465+
textDirection: TextDirection.rtl,
466+
);
467+
final CanvasParagraphBuilder builder = CanvasParagraphBuilder(style);
468+
469+
builder.addText('(1)');
470+
471+
final CanvasParagraph paragraph = builder.build();
472+
expect(paragraph.paragraphStyle, style);
473+
expect(paragraph.plainText, '(1)');
474+
475+
paragraph.layout(const ParagraphConstraints(width: double.infinity));
476+
expectOuterHtml(
477+
paragraph,
478+
'<flt-paragraph style="${paragraphStyle()}">'
479+
'<flt-span dir="rtl" style="${spanStyle(top: null, left: null, width: null, fontSize: 20)}">'
480+
'('
481+
'</flt-span>'
482+
'<flt-span style="${spanStyle(top: null, left: null, width: null, fontSize: 20)}">'
483+
'1'
484+
'</flt-span>'
485+
'<flt-span dir="rtl" style="${spanStyle(top: null, left: null, width: null, fontSize: 20)}">'
486+
')'
487+
'</flt-span>'
488+
'</flt-paragraph>',
489+
ignorePositions: true,
490+
);
491+
});
458492
}
459493

460494
const String defaultFontFamily = 'Ahem';

0 commit comments

Comments
 (0)