Skip to content

Commit

Permalink
[canvaskit] Correctly get the directionality of the text boxes (flutt…
Browse files Browse the repository at this point in the history
  • Loading branch information
Harry Terkelsen authored Jun 24, 2022
1 parent 544bf7b commit 9bc723b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
7 changes: 4 additions & 3 deletions lib/web_ui/lib/src/engine/canvaskit/text.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'dart:typed_data';
import 'package:meta/meta.dart';
import 'package:ui/ui.dart' as ui;

import '../safe_browser_api.dart';
import '../util.dart';
import 'canvaskit_api.dart';
import 'font_fallbacks.dart';
Expand Down Expand Up @@ -44,7 +45,6 @@ class CkParagraphStyle implements ui.ParagraphStyle {
ellipsis,
locale,
),
_textDirection = textDirection ?? ui.TextDirection.ltr,
_fontFamily = ui.debugEmulateFlutterTesterEnvironment ? 'Ahem' : fontFamily,
_fontSize = fontSize,
_height = height,
Expand All @@ -53,7 +53,6 @@ class CkParagraphStyle implements ui.ParagraphStyle {
_fontStyle = fontStyle;

final SkParagraphStyle skParagraphStyle;
final ui.TextDirection? _textDirection;
final String? _fontFamily;
final double? _fontSize;
final double? _height;
Expand Down Expand Up @@ -729,12 +728,14 @@ class CkParagraph extends SkiaObject<SkParagraph> implements ui.Paragraph {

for (int i = 0; i < skRects.length; i++) {
final Float32List rect = skRects[i];
final int skTextDirection =
getJsProperty(getJsProperty(rect, 'direction'), 'value');
result.add(ui.TextBox.fromLTRBD(
rect[0],
rect[1],
rect[2],
rect[3],
_paragraphStyle._textDirection!,
ui.TextDirection.values[skTextDirection],
));
}

Expand Down
16 changes: 16 additions & 0 deletions lib/web_ui/test/canvaskit/text_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,22 @@ void testMain() {
expect(paragraph, isNotNull);
}
});

// Regression test for https://github.com/flutter/flutter/issues/78550
test('getBoxesForRange works for LTR text in an RTL paragraph', () {
// Create builder for an RTL paragraph.
final ui.ParagraphBuilder builder = ui.ParagraphBuilder(
ui.ParagraphStyle(fontSize: 16, textDirection: ui.TextDirection.rtl));
builder.addText('hello');
final ui.Paragraph paragraph = builder.build();
paragraph.layout(const ui.ParagraphConstraints(width: 100));
expect(paragraph, isNotNull);
final List<ui.TextBox> boxes = paragraph.getBoxesForRange(0, 1);
expect(boxes, hasLength(1));
// The direction for this span is LTR even though the paragraph is RTL
// because the directionality of the 'h' is LTR.
expect(boxes.single.direction, equals(ui.TextDirection.ltr));
});
// TODO(hterkelsen): https://github.com/flutter/flutter/issues/60040
}, skip: isIosSafari);
}

0 comments on commit 9bc723b

Please sign in to comment.