text_painter_rtl_test.dart 29.9 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
Ian Hickson's avatar
Ian Hickson committed
2 3 4 5 6 7 8 9 10 11 12 13
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/foundation.dart';
import 'package:flutter/painting.dart';
import 'package:flutter_test/flutter_test.dart';

const bool skipTestsWithKnownBugs = true;
const bool skipExpectsWithKnownBugs = false;

void main() {
  test('TextPainter - basic words', () {
14
    final TextPainter painter = TextPainter()
Ian Hickson's avatar
Ian Hickson committed
15 16 17 18
      ..textDirection = TextDirection.ltr;

    painter.text = const TextSpan(
      text: 'ABC DEF\nGHI',
19
      style: TextStyle(fontFamily: 'Ahem', fontSize: 10.0),
Ian Hickson's avatar
Ian Hickson committed
20 21 22 23
    );
    painter.layout();

    expect(
24
      painter.getWordBoundary(const TextPosition(offset: 1)),
Ian Hickson's avatar
Ian Hickson committed
25 26 27
      const TextRange(start: 0, end: 3),
    );
    expect(
28
      painter.getWordBoundary(const TextPosition(offset: 5)),
Ian Hickson's avatar
Ian Hickson committed
29 30 31
      const TextRange(start: 4, end: 7),
    );
    expect(
32
      painter.getWordBoundary(const TextPosition(offset: 9)),
Ian Hickson's avatar
Ian Hickson committed
33 34 35 36 37
      const TextRange(start: 8, end: 11),
    );
  });

  test('TextPainter - bidi overrides in LTR', () {
38
    final TextPainter painter = TextPainter()
Ian Hickson's avatar
Ian Hickson committed
39 40 41 42 43
      ..textDirection = TextDirection.ltr;

    painter.text = const TextSpan(
      text: '${Unicode.RLO}HEBREW1 ${Unicode.LRO}english2${Unicode.PDF} HEBREW3${Unicode.PDF}',
           //      0       12345678      9      101234567       18     90123456       27
44
      style: TextStyle(fontFamily: 'Ahem', fontSize: 10.0),
Ian Hickson's avatar
Ian Hickson committed
45
    );
46
    TextSpan textSpan = painter.text! as TextSpan;
47
    expect(textSpan.text!.length, 28);
Ian Hickson's avatar
Ian Hickson committed
48 49
    painter.layout();

50 51 52
    // The skips here are because the old rendering code considers the bidi formatting characters
    // to be part of the word sometimes and not others, which is fine, but we'd mildly prefer if
    // we were consistently considering them part of words always.
53
    final TextRange hebrew1 = painter.getWordBoundary(const TextPosition(offset: 4));
54
    expect(hebrew1, const TextRange(start: 0, end: 8), skip: skipExpectsWithKnownBugs); // https://github.com/flutter/flutter/issues/87536
55
    final TextRange english2 = painter.getWordBoundary(const TextPosition(offset: 14));
56
    expect(english2, const TextRange(start: 9, end: 19), skip: skipExpectsWithKnownBugs); // https://github.com/flutter/flutter/issues/87536
57
    final TextRange hebrew3 = painter.getWordBoundary(const TextPosition(offset: 24));
Ian Hickson's avatar
Ian Hickson committed
58 59 60 61 62 63 64 65 66 67 68 69 70 71
    expect(hebrew3, const TextRange(start: 20, end: 28));

    //                              >>>>>>>>>>>>>>>                       embedding level 2
    //              <==============================================       embedding level 1
    //             ------------------------------------------------>      embedding level 0
    //            0 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 2
    //            0 6 5 4 3 2 1 0 9 0 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1 7  <- index of character in string
    // Paints as:   3 W E R B E H   e n g l i s h 2   1 W E R B E H
    //             0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2   <- pixel offset at boundary
    //             0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4
    //             0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

    expect(
      painter.getOffsetForCaret(const TextPosition(offset: 0, affinity: TextAffinity.upstream), Rect.zero),
72
      Offset.zero,
Ian Hickson's avatar
Ian Hickson committed
73 74
    );
    expect(
75
      painter.getOffsetForCaret(const TextPosition(offset: 0), Rect.zero),
76
      Offset.zero,
Ian Hickson's avatar
Ian Hickson committed
77 78 79 80 81 82
    );
    expect(
      painter.getOffsetForCaret(const TextPosition(offset: 1, affinity: TextAffinity.upstream), Rect.zero),
      const Offset(240.0, 0.0),
    );
    expect(
83
      painter.getOffsetForCaret(const TextPosition(offset: 1), Rect.zero),
Ian Hickson's avatar
Ian Hickson committed
84 85 86 87 88 89 90
      const Offset(240.0, 0.0),
    );
    expect(
      painter.getOffsetForCaret(const TextPosition(offset: 7, affinity: TextAffinity.upstream), Rect.zero),
      const Offset(180.0, 0.0),
    );
    expect(
91
      painter.getOffsetForCaret(const TextPosition(offset: 7), Rect.zero),
Ian Hickson's avatar
Ian Hickson committed
92 93 94 95 96 97 98
      const Offset(180.0, 0.0),
    );
    expect(
      painter.getOffsetForCaret(const TextPosition(offset: 8, affinity: TextAffinity.upstream), Rect.zero),
      const Offset(170.0, 0.0),
    );
    expect(
99
      painter.getOffsetForCaret(const TextPosition(offset: 8), Rect.zero),
Ian Hickson's avatar
Ian Hickson committed
100 101 102 103 104 105 106
      const Offset(170.0, 0.0),
    );
    expect(
      painter.getOffsetForCaret(const TextPosition(offset: 9, affinity: TextAffinity.upstream), Rect.zero),
      const Offset(160.0, 0.0),
    );
    expect(
107
      painter.getOffsetForCaret(const TextPosition(offset: 9), Rect.zero),
Ian Hickson's avatar
Ian Hickson committed
108 109 110 111 112 113 114
      const Offset(160.0, 0.0),
    );
    expect(
      painter.getOffsetForCaret(const TextPosition(offset: 10, affinity: TextAffinity.upstream), Rect.zero),
      const Offset(80.0, 0.0),
    );
    expect(
115
      painter.getOffsetForCaret(const TextPosition(offset: 10), Rect.zero),
Ian Hickson's avatar
Ian Hickson committed
116 117 118 119 120 121
      const Offset(80.0, 0.0),
    );

    expect(
      painter.getBoxesForSelection(const TextSelection(baseOffset: 0, extentOffset: 27)),
      const <TextBox>[
122 123 124
        TextBox.fromLTRBD(160.0, 0.0, 240.0, 10.0, TextDirection.rtl), // HEBREW1
        TextBox.fromLTRBD( 80.0, 0.0, 160.0, 10.0, TextDirection.ltr), // english2
        TextBox.fromLTRBD(  0.0, 0.0,  80.0, 10.0, TextDirection.rtl), // HEBREW3
Ian Hickson's avatar
Ian Hickson committed
125 126 127 128 129
      ],
      // Horizontal offsets are currently one pixel off in places; vertical offsets are good.
      // The list is currently in the wrong order (so selection boxes will paint in the wrong order).
    );

130
    textSpan = painter.text! as TextSpan;
131
    final List<List<TextBox>> list = <List<TextBox>>[
132
      for (int index = 0; index < textSpan.text!.length; index += 1)
133 134
        painter.getBoxesForSelection(TextSelection(baseOffset: index, extentOffset: index + 1)),
    ];
Ian Hickson's avatar
Ian Hickson committed
135
    expect(list, const <List<TextBox>>[
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
      <TextBox>[], // U+202E, non-printing Unicode bidi formatting character
      <TextBox>[TextBox.fromLTRBD(230.0, 0.0, 240.0, 10.0, TextDirection.rtl)],
      <TextBox>[TextBox.fromLTRBD(220.0, 0.0, 230.0, 10.0, TextDirection.rtl)],
      <TextBox>[TextBox.fromLTRBD(210.0, 0.0, 220.0, 10.0, TextDirection.rtl)],
      <TextBox>[TextBox.fromLTRBD(200.0, 0.0, 210.0, 10.0, TextDirection.rtl)],
      <TextBox>[TextBox.fromLTRBD(190.0, 0.0, 200.0, 10.0, TextDirection.rtl)],
      <TextBox>[TextBox.fromLTRBD(180.0, 0.0, 190.0, 10.0, TextDirection.rtl)],
      <TextBox>[TextBox.fromLTRBD(170.0, 0.0, 180.0, 10.0, TextDirection.rtl)],
      <TextBox>[TextBox.fromLTRBD(160.0, 0.0, 170.0, 10.0, TextDirection.rtl)],
      <TextBox>[], // U+202D, non-printing Unicode bidi formatting character
      <TextBox>[TextBox.fromLTRBD(80.0, 0.0, 90.0, 10.0, TextDirection.ltr)],
      <TextBox>[TextBox.fromLTRBD(90.0, 0.0, 100.0, 10.0, TextDirection.ltr)],
      <TextBox>[TextBox.fromLTRBD(100.0, 0.0, 110.0, 10.0, TextDirection.ltr)],
      <TextBox>[TextBox.fromLTRBD(110.0, 0.0, 120.0, 10.0, TextDirection.ltr)],
      <TextBox>[TextBox.fromLTRBD(120.0, 0.0, 130.0, 10.0, TextDirection.ltr)],
      <TextBox>[TextBox.fromLTRBD(130.0, 0.0, 140.0, 10.0, TextDirection.ltr)],
      <TextBox>[TextBox.fromLTRBD(140.0, 0.0, 150.0, 10.0, TextDirection.ltr)],
      <TextBox>[TextBox.fromLTRBD(150.0, 0.0, 160.0, 10.0, TextDirection.ltr)],
      <TextBox>[], // U+202C, non-printing Unicode bidi formatting character
      <TextBox>[TextBox.fromLTRBD(70.0, 0.0, 80.0, 10.0, TextDirection.rtl)],
      <TextBox>[TextBox.fromLTRBD(60.0, 0.0, 70.0, 10.0, TextDirection.rtl)],
      <TextBox>[TextBox.fromLTRBD(50.0, 0.0, 60.0, 10.0, TextDirection.rtl)],
      <TextBox>[TextBox.fromLTRBD(40.0, 0.0, 50.0, 10.0, TextDirection.rtl)],
      <TextBox>[TextBox.fromLTRBD(30.0, 0.0, 40.0, 10.0, TextDirection.rtl)],
      <TextBox>[TextBox.fromLTRBD(20.0, 0.0, 30.0, 10.0, TextDirection.rtl)],
      <TextBox>[TextBox.fromLTRBD(10.0, 0.0, 20.0, 10.0, TextDirection.rtl)],
      <TextBox>[TextBox.fromLTRBD(0.0, 0.0, 10.0, 10.0, TextDirection.rtl)],
      <TextBox>[], // U+202C, non-printing Unicode bidi formatting character
Ian Hickson's avatar
Ian Hickson committed
164 165
      // The list currently has one extra bogus entry (the last entry, for the
      // trailing U+202C PDF, should be empty but is one-pixel-wide instead).
166 167
    ], skip: skipExpectsWithKnownBugs); // https://github.com/flutter/flutter/issues/87536
  }, skip: skipTestsWithKnownBugs); // https://github.com/flutter/flutter/issues/87536
Ian Hickson's avatar
Ian Hickson committed
168 169

  test('TextPainter - bidi overrides in RTL', () {
170
    final TextPainter painter = TextPainter()
Ian Hickson's avatar
Ian Hickson committed
171 172 173 174 175
      ..textDirection = TextDirection.rtl;

    painter.text = const TextSpan(
      text: '${Unicode.RLO}HEBREW1 ${Unicode.LRO}english2${Unicode.PDF} HEBREW3${Unicode.PDF}',
           //      0       12345678      9      101234567       18     90123456       27
176
      style: TextStyle(fontFamily: 'Ahem', fontSize: 10.0),
Ian Hickson's avatar
Ian Hickson committed
177
    );
178
    final TextSpan textSpan = painter.text! as TextSpan;
179
    expect(textSpan.text!.length, 28);
Ian Hickson's avatar
Ian Hickson committed
180 181
    painter.layout();

182
    final TextRange hebrew1 = painter.getWordBoundary(const TextPosition(offset: 4));
183
    expect(hebrew1, const TextRange(start: 0, end: 8), skip: skipExpectsWithKnownBugs); // https://github.com/flutter/flutter/issues/87536
184
    final TextRange english2 = painter.getWordBoundary(const TextPosition(offset: 14));
185
    expect(english2, const TextRange(start: 9, end: 19), skip: skipExpectsWithKnownBugs); // https://github.com/flutter/flutter/issues/87536
186
    final TextRange hebrew3 = painter.getWordBoundary(const TextPosition(offset: 24));
Ian Hickson's avatar
Ian Hickson committed
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
    expect(hebrew3, const TextRange(start: 20, end: 28));

    //                              >>>>>>>>>>>>>>>                       embedding level 2
    //            <==================================================     embedding level 1
    //            2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0
    //            7 6 5 4 3 2 1 0 9 0 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1 0  <- index of character in string
    // Paints as:   3 W E R B E H   e n g l i s h 2   1 W E R B E H
    //             0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2   <- pixel offset at boundary
    //             0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4
    //             0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

    expect(
      painter.getOffsetForCaret(const TextPosition(offset: 0, affinity: TextAffinity.upstream), Rect.zero),
      const Offset(240.0, 0.0),
    );
    expect(
203
      painter.getOffsetForCaret(const TextPosition(offset: 0), Rect.zero),
Ian Hickson's avatar
Ian Hickson committed
204 205 206 207 208 209 210
      const Offset(240.0, 0.0),
    );
    expect(
      painter.getOffsetForCaret(const TextPosition(offset: 1, affinity: TextAffinity.upstream), Rect.zero),
      const Offset(240.0, 0.0),
    );
    expect(
211
      painter.getOffsetForCaret(const TextPosition(offset: 1), Rect.zero),
Ian Hickson's avatar
Ian Hickson committed
212 213 214 215 216 217 218
      const Offset(240.0, 0.0),
    );
    expect(
      painter.getOffsetForCaret(const TextPosition(offset: 7, affinity: TextAffinity.upstream), Rect.zero),
      const Offset(180.0, 0.0),
    );
    expect(
219
      painter.getOffsetForCaret(const TextPosition(offset: 7), Rect.zero),
Ian Hickson's avatar
Ian Hickson committed
220 221 222 223 224 225 226
      const Offset(180.0, 0.0),
    );
    expect(
      painter.getOffsetForCaret(const TextPosition(offset: 8, affinity: TextAffinity.upstream), Rect.zero),
      const Offset(170.0, 0.0),
    );
    expect(
227
      painter.getOffsetForCaret(const TextPosition(offset: 8), Rect.zero),
Ian Hickson's avatar
Ian Hickson committed
228 229 230 231 232 233 234
      const Offset(170.0, 0.0),
    );
    expect(
      painter.getOffsetForCaret(const TextPosition(offset: 9, affinity: TextAffinity.upstream), Rect.zero),
      const Offset(160.0, 0.0),
    );
    expect(
235
      painter.getOffsetForCaret(const TextPosition(offset: 9), Rect.zero),
Ian Hickson's avatar
Ian Hickson committed
236 237 238 239 240 241 242
      const Offset(160.0, 0.0),
    );
    expect(
      painter.getOffsetForCaret(const TextPosition(offset: 10, affinity: TextAffinity.upstream), Rect.zero),
      const Offset(80.0, 0.0),
    );
    expect(
243
      painter.getOffsetForCaret(const TextPosition(offset: 10), Rect.zero),
Ian Hickson's avatar
Ian Hickson committed
244 245 246 247 248 249
      const Offset(80.0, 0.0),
    );

    expect(
      painter.getBoxesForSelection(const TextSelection(baseOffset: 0, extentOffset: 27)),
      const <TextBox>[
250 251 252
        TextBox.fromLTRBD(160.0, 0.0, 240.0, 10.0, TextDirection.rtl), // HEBREW1
        TextBox.fromLTRBD( 80.0, 0.0, 160.0, 10.0, TextDirection.ltr), // english2
        TextBox.fromLTRBD(  0.0, 0.0,  80.0, 10.0, TextDirection.rtl), // HEBREW3
Ian Hickson's avatar
Ian Hickson committed
253 254 255
      ],
      // Horizontal offsets are currently one pixel off in places; vertical offsets are good.
      // The list is currently in the wrong order (so selection boxes will paint in the wrong order).
256
      skip: skipExpectsWithKnownBugs, // https://github.com/flutter/flutter/issues/87536
Ian Hickson's avatar
Ian Hickson committed
257
    );
258
  }, skip: skipTestsWithKnownBugs); // https://github.com/flutter/flutter/issues/87536
Ian Hickson's avatar
Ian Hickson committed
259 260

  test('TextPainter - forced line-wrapping with bidi', () {
261
    final TextPainter painter = TextPainter()
Ian Hickson's avatar
Ian Hickson committed
262 263 264 265
      ..textDirection = TextDirection.ltr;

    painter.text = const TextSpan(
      text: 'A\u05D0', // A, Alef
266
      style: TextStyle(fontFamily: 'Ahem', fontSize: 10.0),
Ian Hickson's avatar
Ian Hickson committed
267
    );
268
    final TextSpan textSpan = painter.text! as TextSpan;
269
    expect(textSpan.text!.length, 2);
Ian Hickson's avatar
Ian Hickson committed
270 271 272 273
    painter.layout(maxWidth: 10.0);

    for (int index = 0; index <= 2; index += 1) {
      expect(
274
        painter.getWordBoundary(const TextPosition(offset: 0)),
Ian Hickson's avatar
Ian Hickson committed
275 276 277 278 279 280
        const TextRange(start: 0, end: 2),
      );
    }

    expect( // before the A
      painter.getOffsetForCaret(const TextPosition(offset: 0, affinity: TextAffinity.upstream), Rect.zero),
281
      Offset.zero,
Ian Hickson's avatar
Ian Hickson committed
282 283
    );
    expect( // before the A
284
      painter.getOffsetForCaret(const TextPosition(offset: 0), Rect.zero),
285
      Offset.zero,
Ian Hickson's avatar
Ian Hickson committed
286 287 288 289 290 291 292
    );

    expect( // between A and Alef, after the A
      painter.getOffsetForCaret(const TextPosition(offset: 1, affinity: TextAffinity.upstream), Rect.zero),
      const Offset(10.0, 0.0),
    );
    expect( // between A and Alef, before the Alef
293
      painter.getOffsetForCaret(const TextPosition(offset: 1), Rect.zero),
Ian Hickson's avatar
Ian Hickson committed
294 295 296 297 298 299 300 301
      const Offset(10.0, 10.0),
    );

    expect( // after the Alef
      painter.getOffsetForCaret(const TextPosition(offset: 2, affinity: TextAffinity.upstream), Rect.zero),
      const Offset(0.0, 10.0),
    );
    expect( // after the Alef
302
      painter.getOffsetForCaret(const TextPosition(offset: 2), Rect.zero),
Ian Hickson's avatar
Ian Hickson committed
303 304 305 306 307 308
      const Offset(0.0, 10.0),
    );

    expect(
      painter.getBoxesForSelection(const TextSelection(baseOffset: 0, extentOffset: 2)),
      const <TextBox>[
309 310
        TextBox.fromLTRBD(0.0,  0.0, 10.0, 10.0, TextDirection.ltr), // A
        TextBox.fromLTRBD(0.0, 10.0, 10.0, 20.0, TextDirection.rtl), // Alef
Ian Hickson's avatar
Ian Hickson committed
311 312 313 314 315
      ],
    );
    expect(
      painter.getBoxesForSelection(const TextSelection(baseOffset: 0, extentOffset: 1)),
      const <TextBox>[
316
        TextBox.fromLTRBD(0.0,  0.0, 10.0, 10.0, TextDirection.ltr), // A
Ian Hickson's avatar
Ian Hickson committed
317 318 319 320 321
      ],
    );
    expect(
      painter.getBoxesForSelection(const TextSelection(baseOffset: 1, extentOffset: 2)),
      const <TextBox>[
322
        TextBox.fromLTRBD(0.0, 10.0, 10.0, 20.0, TextDirection.rtl), // Alef
Ian Hickson's avatar
Ian Hickson committed
323 324
      ],
    );
325
  }, skip: isBrowser); // https://github.com/flutter/flutter/issues/32238
Ian Hickson's avatar
Ian Hickson committed
326 327

  test('TextPainter - line wrap mid-word', () {
328
    final TextPainter painter = TextPainter()
Ian Hickson's avatar
Ian Hickson committed
329 330 331
      ..textDirection = TextDirection.ltr;

    painter.text = const TextSpan(
332 333 334
      style: TextStyle(fontFamily: 'Ahem', fontSize: 10.0),
      children: <TextSpan>[
        TextSpan(
Ian Hickson's avatar
Ian Hickson committed
335 336
          text: 'hello', // width 50
        ),
337
        TextSpan(
Ian Hickson's avatar
Ian Hickson committed
338
          text: 'lovely', // width 120
339
          style: TextStyle(fontFamily: 'Ahem', fontSize: 20.0),
Ian Hickson's avatar
Ian Hickson committed
340
        ),
341
        TextSpan(
Ian Hickson's avatar
Ian Hickson committed
342 343 344 345 346 347 348 349 350
          text: 'world', // width 50
        ),
      ],
    );
    painter.layout(maxWidth: 110.0); // half-way through "lovely"

    expect(
      painter.getBoxesForSelection(const TextSelection(baseOffset: 0, extentOffset: 16)),
      const <TextBox>[
351 352 353 354
        TextBox.fromLTRBD( 0.0,  8.0,  50.0, 18.0, TextDirection.ltr),
        TextBox.fromLTRBD(50.0,  0.0, 110.0, 20.0, TextDirection.ltr),
        TextBox.fromLTRBD( 0.0, 20.0,  60.0, 40.0, TextDirection.ltr),
        TextBox.fromLTRBD(60.0, 28.0, 110.0, 38.0, TextDirection.ltr),
Ian Hickson's avatar
Ian Hickson committed
355
      ],
356 357
      // horizontal offsets are one pixel off in places; vertical offsets are good
      skip: skipExpectsWithKnownBugs, // https://github.com/flutter/flutter/issues/87536
Ian Hickson's avatar
Ian Hickson committed
358
    );
359
  }, skip: skipTestsWithKnownBugs); // https://github.com/flutter/flutter/issues/87536
Ian Hickson's avatar
Ian Hickson committed
360 361

  test('TextPainter - line wrap mid-word, bidi - LTR base', () {
362
    final TextPainter painter = TextPainter()
Ian Hickson's avatar
Ian Hickson committed
363 364 365
      ..textDirection = TextDirection.ltr;

    painter.text = const TextSpan(
366 367 368
      style: TextStyle(fontFamily: 'Ahem', fontSize: 10.0),
      children: <TextSpan>[
        TextSpan(
Ian Hickson's avatar
Ian Hickson committed
369 370
          text: 'hello', // width 50
        ),
371
        TextSpan(
Ian Hickson's avatar
Ian Hickson committed
372
          text: '\u062C\u0645\u064A\u0644', // width 80
373
          style: TextStyle(fontFamily: 'Ahem', fontSize: 20.0),
Ian Hickson's avatar
Ian Hickson committed
374
        ),
375
        TextSpan(
Ian Hickson's avatar
Ian Hickson committed
376 377 378 379 380 381 382 383 384
          text: 'world', // width 50
        ),
      ],
    );
    painter.layout(maxWidth: 90.0); // half-way through the Arabic word

    expect(
      painter.getBoxesForSelection(const TextSelection(baseOffset: 0, extentOffset: 16)),
      const <TextBox>[
385 386 387 388
        TextBox.fromLTRBD( 0.0,  8.0, 50.0, 18.0, TextDirection.ltr),
        TextBox.fromLTRBD(50.0,  0.0, 90.0, 20.0, TextDirection.rtl),
        TextBox.fromLTRBD( 0.0, 20.0, 40.0, 40.0, TextDirection.rtl),
        TextBox.fromLTRBD(40.0, 28.0, 90.0, 38.0, TextDirection.ltr),
Ian Hickson's avatar
Ian Hickson committed
389
      ],
390 391
      // horizontal offsets are one pixel off in places; vertical offsets are good
      skip: skipExpectsWithKnownBugs, // https://github.com/flutter/flutter/issues/87536
Ian Hickson's avatar
Ian Hickson committed
392 393
    );

394 395 396 397 398
    final List<List<TextBox>> list = <List<TextBox>>[
      for (int index = 0; index < 5+4+5; index += 1)
        painter.getBoxesForSelection(TextSelection(baseOffset: index, extentOffset: index + 1)),
    ];

Ian Hickson's avatar
Ian Hickson committed
399
    expect(list, const <List<TextBox>>[
400 401 402 403 404 405 406 407 408 409 410 411 412
      <TextBox>[TextBox.fromLTRBD(0.0, 8.0, 10.0, 18.0, TextDirection.ltr)],
      <TextBox>[TextBox.fromLTRBD(10.0, 8.0, 20.0, 18.0, TextDirection.ltr)],
      <TextBox>[TextBox.fromLTRBD(20.0, 8.0, 30.0, 18.0, TextDirection.ltr)],
      <TextBox>[TextBox.fromLTRBD(30.0, 8.0, 40.0, 18.0, TextDirection.ltr)],
      <TextBox>[TextBox.fromLTRBD(40.0, 8.0, 50.0, 18.0, TextDirection.ltr)],
      <TextBox>[TextBox.fromLTRBD(70.0, 0.0, 90.0, 20.0, TextDirection.rtl)],
      <TextBox>[TextBox.fromLTRBD(50.0, 0.0, 70.0, 20.0, TextDirection.rtl)],
      <TextBox>[TextBox.fromLTRBD(20.0, 20.0, 40.0, 40.0, TextDirection.rtl)],
      <TextBox>[TextBox.fromLTRBD(0.0, 20.0, 20.0, 40.0, TextDirection.rtl)],
      <TextBox>[TextBox.fromLTRBD(40.0, 28.0, 50.0, 38.0, TextDirection.ltr)],
      <TextBox>[TextBox.fromLTRBD(50.0, 28.0, 60.0, 38.0, TextDirection.ltr)],
      <TextBox>[TextBox.fromLTRBD(60.0, 28.0, 70.0, 38.0, TextDirection.ltr)],
      <TextBox>[TextBox.fromLTRBD(70.0, 28.0, 80.0, 38.0, TextDirection.ltr)],
413
      <TextBox>[TextBox.fromLTRBD(80.0, 28.0, 90.0, 38.0, TextDirection.ltr)],
Ian Hickson's avatar
Ian Hickson committed
414
    ]);
415
  }, skip: skipTestsWithKnownBugs); // https://github.com/flutter/flutter/issues/87536
Ian Hickson's avatar
Ian Hickson committed
416 417

  test('TextPainter - line wrap mid-word, bidi - RTL base', () {
418
    final TextPainter painter = TextPainter()
Ian Hickson's avatar
Ian Hickson committed
419 420 421
      ..textDirection = TextDirection.rtl;

    painter.text = const TextSpan(
422 423 424
      style: TextStyle(fontFamily: 'Ahem', fontSize: 10.0),
      children: <TextSpan>[
        TextSpan(
Ian Hickson's avatar
Ian Hickson committed
425 426
          text: 'hello', // width 50
        ),
427
        TextSpan(
Ian Hickson's avatar
Ian Hickson committed
428
          text: '\u062C\u0645\u064A\u0644', // width 80
429
          style: TextStyle(fontFamily: 'Ahem', fontSize: 20.0),
Ian Hickson's avatar
Ian Hickson committed
430
        ),
431
        TextSpan(
Ian Hickson's avatar
Ian Hickson committed
432 433 434 435 436 437 438 439 440
          text: 'world', // width 50
        ),
      ],
    );
    painter.layout(maxWidth: 90.0); // half-way through the Arabic word

    expect(
      painter.getBoxesForSelection(const TextSelection(baseOffset: 0, extentOffset: 16)),
      const <TextBox>[
441 442 443 444
        TextBox.fromLTRBD(40.0,  8.0, 90.0, 18.0, TextDirection.ltr),
        TextBox.fromLTRBD( 0.0,  0.0, 40.0, 20.0, TextDirection.rtl),
        TextBox.fromLTRBD(50.0, 20.0, 90.0, 40.0, TextDirection.rtl),
        TextBox.fromLTRBD( 0.0, 28.0, 50.0, 38.0, TextDirection.ltr),
Ian Hickson's avatar
Ian Hickson committed
445 446 447
      ],
      // Horizontal offsets are currently one pixel off in places; vertical offsets are good.
      // The list is currently in the wrong order (so selection boxes will paint in the wrong order).
448
      skip: skipExpectsWithKnownBugs, // https://github.com/flutter/flutter/issues/87536
Ian Hickson's avatar
Ian Hickson committed
449
    );
450
  }, skip: skipTestsWithKnownBugs); // https://github.com/flutter/flutter/issues/87536
Ian Hickson's avatar
Ian Hickson committed
451 452

  test('TextPainter - multiple levels', () {
453
    final TextPainter painter = TextPainter()
Ian Hickson's avatar
Ian Hickson committed
454 455 456
      ..textDirection = TextDirection.rtl;

    final String pyramid = rlo(lro(rlo(lro(rlo('')))));
457
    painter.text = TextSpan(
Ian Hickson's avatar
Ian Hickson committed
458 459 460 461 462 463
      text: pyramid,
      style: const TextStyle(fontFamily: 'Ahem', fontSize: 10.0),
    );
    painter.layout();

    expect(
464
      painter.getBoxesForSelection(TextSelection(baseOffset: 0, extentOffset: pyramid.length)),
Ian Hickson's avatar
Ian Hickson committed
465
      const <TextBox>[
466 467 468 469 470 471 472 473 474
        TextBox.fromLTRBD(90.0, 0.0, 100.0, 10.0, TextDirection.rtl), // outer R, start (right)
        TextBox.fromLTRBD(10.0, 0.0,  20.0, 10.0, TextDirection.ltr), // level 1 L, start (left)
        TextBox.fromLTRBD(70.0, 0.0,  80.0, 10.0, TextDirection.rtl), // level 2 R, start (right)
        TextBox.fromLTRBD(30.0, 0.0,  40.0, 10.0, TextDirection.ltr), // level 3 L, start (left)
        TextBox.fromLTRBD(40.0, 0.0,  60.0, 10.0, TextDirection.rtl), // inner-most RR
        TextBox.fromLTRBD(60.0, 0.0,  70.0, 10.0, TextDirection.ltr), // lever 3 L, end (right)
        TextBox.fromLTRBD(20.0, 0.0,  30.0, 10.0, TextDirection.rtl), // level 2 R, end (left)
        TextBox.fromLTRBD(80.0, 0.0,  90.0, 10.0, TextDirection.ltr), // level 1 L, end (right)
        TextBox.fromLTRBD( 0.0, 0.0,  10.0, 10.0, TextDirection.rtl), // outer R, end (left)
Ian Hickson's avatar
Ian Hickson committed
475 476 477 478
      ],
      // Horizontal offsets are currently one pixel off in places; vertical offsets are good.
      // The list is currently in the wrong order (so selection boxes will paint in the wrong order).
      // Also currently there's an extraneous box at the start of the list.
479
      skip: skipExpectsWithKnownBugs, // https://github.com/flutter/flutter/issues/87536
Ian Hickson's avatar
Ian Hickson committed
480
    );
481
  }, skip: skipTestsWithKnownBugs); // https://github.com/flutter/flutter/issues/87536
Ian Hickson's avatar
Ian Hickson committed
482 483

  test('TextPainter - getPositionForOffset - RTL in LTR', () {
484
    final TextPainter painter = TextPainter()
Ian Hickson's avatar
Ian Hickson committed
485 486 487 488
      ..textDirection = TextDirection.ltr;

    painter.text = const TextSpan(
      text: 'ABC\u05D0\u05D1\u05D2DEF', // A B C Alef Bet Gimel D E F -- but the Hebrew letters are RTL
489
      style: TextStyle(fontFamily: 'Ahem', fontSize: 10.0),
Ian Hickson's avatar
Ian Hickson committed
490 491 492 493 494 495 496 497
    );
    painter.layout();

    // TODO(ianh): Remove the toString()s once https://github.com/flutter/engine/pull/4283 lands
    expect(
      //  Aaa  Bbb  Ccc  Gimel  Bet  Alef  Ddd  Eee  Fff
      // ^
      painter.getPositionForOffset(const Offset(0.0, 5.0)).toString(),
498
      const TextPosition(offset: 0).toString(),
Ian Hickson's avatar
Ian Hickson committed
499 500 501 502 503
    );
    expect(
      //                     Aaa  Bbb  Ccc  Gimel  Bet  Alef  Ddd  Eee  Fff
      // ^
      painter.getPositionForOffset(const Offset(-100.0, 5.0)).toString(),
504
      const TextPosition(offset: 0).toString(),
Ian Hickson's avatar
Ian Hickson committed
505 506 507 508 509
    );
    expect(
      //  Aaa  Bbb  Ccc  Gimel  Bet  Alef  Ddd  Eee  Fff
      //  ^
      painter.getPositionForOffset(const Offset(4.0, 5.0)).toString(),
510
      const TextPosition(offset: 0).toString(),
Ian Hickson's avatar
Ian Hickson committed
511 512 513 514 515 516 517 518 519 520 521
    );
    expect(
      //  Aaa  Bbb  Ccc  Gimel  Bet  Alef  Ddd  Eee  Fff
      //    ^
      painter.getPositionForOffset(const Offset(8.0, 5.0)).toString(),
      const TextPosition(offset: 1, affinity: TextAffinity.upstream).toString(),
    );
    expect(
      //  Aaa  Bbb  Ccc  Gimel  Bet  Alef  Ddd  Eee  Fff
      //       ^
      painter.getPositionForOffset(const Offset(12.0, 5.0)).toString(),
522
      const TextPosition(offset: 1).toString(),
523 524
      // currently we say upstream instead of downstream
      skip: skipExpectsWithKnownBugs, // https://github.com/flutter/flutter/issues/87536
Ian Hickson's avatar
Ian Hickson committed
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
    );
    expect(
      //  Aaa  Bbb  Ccc  Gimel  Bet  Alef  Ddd  Eee  Fff
      //              ^
      painter.getPositionForOffset(const Offset(28.0, 5.0)).toString(),
      const TextPosition(offset: 3, affinity: TextAffinity.upstream).toString(),
    );
    expect(
      //  Aaa  Bbb  Ccc  Gimel  Bet  Alef  Ddd  Eee  Fff
      //                 ^
      painter.getPositionForOffset(const Offset(32.0, 5.0)).toString(),
      const TextPosition(offset: 6, affinity: TextAffinity.upstream).toString(),
      skip: skipExpectsWithKnownBugs, // this is part of https://github.com/flutter/flutter/issues/11375
    );
    expect(
      //  Aaa  Bbb  Ccc  Gimel  Bet  Alef  Ddd  Eee  Fff
      //                                ^
      painter.getPositionForOffset(const Offset(58.0, 5.0)).toString(),
543
      const TextPosition(offset: 3).toString(),
Ian Hickson's avatar
Ian Hickson committed
544 545 546 547 548 549
      skip: skipExpectsWithKnownBugs, // this is part of https://github.com/flutter/flutter/issues/11375
    );
    expect(
      //  Aaa  Bbb  Ccc  Gimel  Bet  Alef  Ddd  Eee  Fff
      //                                   ^
      painter.getPositionForOffset(const Offset(62.0, 5.0)).toString(),
550
      const TextPosition(offset: 6).toString(),
Ian Hickson's avatar
Ian Hickson committed
551 552 553 554 555 556 557 558 559 560 561 562 563
    );
    expect(
      //  Aaa  Bbb  Ccc  Gimel  Bet  Alef  Ddd  Eee  Fff
      //                                               ^
      painter.getPositionForOffset(const Offset(88.0, 5.0)).toString(),
      const TextPosition(offset: 9, affinity: TextAffinity.upstream).toString(),
    );
    expect(
      //  Aaa  Bbb  Ccc  Gimel  Bet  Alef  Ddd  Eee  Fff
      //                                                     ^
      painter.getPositionForOffset(const Offset(100.0, 5.0)).toString(),
      const TextPosition(offset: 9, affinity: TextAffinity.upstream).toString(),
    );
564
  }, skip: skipTestsWithKnownBugs); // https://github.com/flutter/flutter/issues/87536
Ian Hickson's avatar
Ian Hickson committed
565 566

  test('TextPainter - getPositionForOffset - LTR in RTL', () {
567
    final TextPainter painter = TextPainter()
Ian Hickson's avatar
Ian Hickson committed
568 569 570 571
      ..textDirection = TextDirection.rtl;

    painter.text = const TextSpan(
      text: '\u05D0\u05D1\u05D2ABC\u05D3\u05D4\u05D5',
572
      style: TextStyle(fontFamily: 'Ahem', fontSize: 10.0),
Ian Hickson's avatar
Ian Hickson committed
573 574 575 576 577 578 579 580 581 582 583 584 585 586
    );
    painter.layout();

    // TODO(ianh): Remove the toString()s once https://github.com/flutter/engine/pull/4283 lands
    expect(
      //   Vav He Dalet Aaa Bbb Ccc Gimel Bet Alef
      // ^
      painter.getPositionForOffset(const Offset(-4.0, 5.0)).toString(),
      const TextPosition(offset: 9, affinity: TextAffinity.upstream).toString(),
    );
    expect(
      // Vav He Dalet Aaa Bbb Ccc Gimel Bet Alef
      //            ^
      painter.getPositionForOffset(const Offset(28.0, 5.0)).toString(),
587
      const TextPosition(offset: 6).toString(),
Ian Hickson's avatar
Ian Hickson committed
588 589 590 591 592
    );
    expect(
      // Vav He Dalet Aaa Bbb Ccc Gimel Bet Alef
      //              ^
      painter.getPositionForOffset(const Offset(32.0, 5.0)).toString(),
593
      const TextPosition(offset: 3).toString(),
Ian Hickson's avatar
Ian Hickson committed
594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
      skip: skipExpectsWithKnownBugs, // this is part of https://github.com/flutter/flutter/issues/11375
    );
    expect(
      // Vav He Dalet Aaa Bbb Ccc Gimel Bet Alef
      //                        ^
      painter.getPositionForOffset(const Offset(58.0, 5.0)).toString(),
      const TextPosition(offset: 6, affinity: TextAffinity.upstream).toString(),
      skip: skipExpectsWithKnownBugs, // this is part of https://github.com/flutter/flutter/issues/11375
    );
    expect(
      // Vav He Dalet Aaa Bbb Ccc Gimel Bet Alef
      //                          ^
      painter.getPositionForOffset(const Offset(62.0, 5.0)).toString(),
      const TextPosition(offset: 3, affinity: TextAffinity.upstream).toString(),
    );
609
  }, skip: skipTestsWithKnownBugs); // https://github.com/flutter/flutter/issues/87536
Ian Hickson's avatar
Ian Hickson committed
610 611

  test('TextPainter - Spaces', () {
612
    final TextPainter painter = TextPainter()
Ian Hickson's avatar
Ian Hickson committed
613 614 615 616
      ..textDirection = TextDirection.ltr;

    painter.text = const TextSpan(
      text: ' ',
617 618 619
      style: TextStyle(fontFamily: 'Ahem', fontSize: 100.0),
      children: <TextSpan>[
        TextSpan(
Ian Hickson's avatar
Ian Hickson committed
620
          text: ' ',
621
          style: TextStyle(fontSize: 10.0),
Ian Hickson's avatar
Ian Hickson committed
622
        ),
623
        TextSpan(
Ian Hickson's avatar
Ian Hickson committed
624
          text: ' ',
625
          style: TextStyle(fontSize: 200.0),
Ian Hickson's avatar
Ian Hickson committed
626
        ),
627 628
        // Add a non-whitespace character because the renderer's line breaker
        // may strip trailing whitespace on a line.
629
        TextSpan(text: 'A'),
Ian Hickson's avatar
Ian Hickson committed
630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654
      ],
    );
    painter.layout();

    // This renders as three (invisible) boxes:
    //
    //                 |<--------200------->|
    //                  ____________________
    //                 |        ^           |
    //                 |        :           |
    //                 |        :           |
    //                 |        :           |
    //                 |        :           |
    //   ___________   |        : 160       |
    //  |  ^        |  |        :           |
    //  |<-+-100--->|10|        :           |
    //  |  :        |__|        :           |
    //  |  : 80     |  |8       :           |
    // _|__v________|__|________v___________| BASELINE
    //  |     ^20   |__|2       ^           |
    //  |_____v_____|  |        |           |
    //                 |        | 40        |
    //                 |        |           |
    //                 |________v___________|

655
    expect(painter.width, 410.0);
Ian Hickson's avatar
Ian Hickson committed
656
    expect(painter.height, 200.0);
657
    expect(painter.computeDistanceToActualBaseline(TextBaseline.alphabetic), moreOrLessEquals(160.0, epsilon: 0.001));
Ian Hickson's avatar
Ian Hickson committed
658 659 660
    expect(painter.preferredLineHeight, 100.0);

    expect(
661
      painter.getBoxesForSelection(const TextSelection(baseOffset: 0, extentOffset: 3)),
Ian Hickson's avatar
Ian Hickson committed
662
      const <TextBox>[
663 664 665
        TextBox.fromLTRBD(  0.0,  80.0, 100.0, 180.0, TextDirection.ltr),
        TextBox.fromLTRBD(100.0, 152.0, 110.0, 162.0, TextDirection.ltr),
        TextBox.fromLTRBD(110.0,   0.0, 310.0, 200.0, TextDirection.ltr),
Ian Hickson's avatar
Ian Hickson committed
666 667
      ],
      // Horizontal offsets are currently one pixel off in places; vertical offsets are good.
668
      skip: skipExpectsWithKnownBugs, // https://github.com/flutter/flutter/issues/87536
Ian Hickson's avatar
Ian Hickson committed
669
    );
670
  }, skip: skipTestsWithKnownBugs); // https://github.com/flutter/flutter/issues/87536
671 672

  test('TextPainter - empty text baseline', () {
673
    final TextPainter painter = TextPainter()
674 675 676
      ..textDirection = TextDirection.ltr;
    painter.text = const TextSpan(
      text: '',
677
      style: TextStyle(fontFamily: 'Ahem', fontSize: 100.0, height: 1.0),
678 679
    );
    painter.layout();
680 681
    expect(painter.computeDistanceToActualBaseline(TextBaseline.alphabetic), moreOrLessEquals(80.0, epsilon: 0.001));
  });
Ian Hickson's avatar
Ian Hickson committed
682 683 684 685
}

String lro(String s) => '${Unicode.LRO}L${s}L${Unicode.PDF}';
String rlo(String s) => '${Unicode.RLO}R${s}R${Unicode.PDF}';