Milad
Committed by David PHAM-VAN

Fix bidirectional text incorrect render order

Properly handle line breakers (\\n) in bidirectional text
1 # Changelog 1 # Changelog
2 2
  3 +## 3.7.4
  4 +
  5 +- Fix Bidirectional text (Arabic + Latin words) order and line breakers issue #990 [Milad]
  6 +
3 ## 3.7.3 7 ## 3.7.3
4 8
5 - Fix missing endobj with compressed xref 9 - Fix missing endobj with compressed xref
@@ -386,14 +386,22 @@ Iterable<String> _parse(String text) sync* { @@ -386,14 +386,22 @@ Iterable<String> _parse(String text) sync* {
386 } 386 }
387 // if notArabicWords.length != 0, that means all sentence doesn't contain Arabic. 387 // if notArabicWords.length != 0, that means all sentence doesn't contain Arabic.
388 for (var i = 0; i < notArabicWords.length; i++) { 388 for (var i = 0; i < notArabicWords.length; i++) {
389 - yield String.fromCharCodes(notArabicWords[i]);  
390 - if (i != notArabicWords.length - 1) { 389 + if (!first) {
391 yield ' '; 390 yield ' ';
392 } 391 }
  392 + yield String.fromCharCodes(notArabicWords[i]);
393 } 393 }
394 } 394 }
395 395
396 /// Apply Arabic shape substitutions 396 /// Apply Arabic shape substitutions
397 String convert(String input) { 397 String convert(String input) {
398 - return List<String>.from(_parse(input)).join(''); 398 + final lines = input.split('\n');
  399 + final parsed = <String>[];
  400 + for (var i = 0; i < lines.length; i++) {
  401 + if (lines[i].isEmpty) {
  402 + continue;
  403 + }
  404 + parsed.addAll([..._parse(lines[i]), if (i != lines.length - 1) '\n']);
  405 + }
  406 + return parsed.join();
399 } 407 }
@@ -3,7 +3,7 @@ description: A pdf producer for Dart. It can create pdf files for both web or fl @@ -3,7 +3,7 @@ description: A pdf producer for Dart. It can create pdf files for both web or fl
3 homepage: https://github.com/DavBfr/dart_pdf/tree/master/pdf 3 homepage: https://github.com/DavBfr/dart_pdf/tree/master/pdf
4 repository: https://github.com/DavBfr/dart_pdf 4 repository: https://github.com/DavBfr/dart_pdf
5 issue_tracker: https://github.com/DavBfr/dart_pdf/issues 5 issue_tracker: https://github.com/DavBfr/dart_pdf/issues
6 -version: 3.7.3 6 +version: 3.7.4
7 7
8 environment: 8 environment:
9 sdk: ">=2.12.0 <3.0.0" 9 sdk: ">=2.12.0 <3.0.0"
@@ -439,7 +439,7 @@ void main() { @@ -439,7 +439,7 @@ void main() {
439 65176, 439 65176,
440 65252, 440 65252,
441 65247, 441 65247,
442 - 1575 442 + 1575,
443 ]), 443 ]),
444 ]; 444 ];
445 445
@@ -492,6 +492,38 @@ void main() { @@ -492,6 +492,38 @@ void main() {
492 )); 492 ));
493 }); 493 });
494 494
  495 + test(
  496 + 'Text Widgets, Mixed Arabic and Latin words should be rendered in order ',
  497 + () {
  498 + pdf.addPage(Page(
  499 + textDirection: TextDirection.rtl,
  500 + build: (Context context) => RichText(
  501 + text: TextSpan(
  502 + text: 'النصوص ثنائية الإتجاه Bidirectional Text\n',
  503 + style: TextStyle(
  504 + font: arabicFont,
  505 + fontSize: 30,
  506 + ),
  507 + children: const <TextSpan>[
  508 + TextSpan(
  509 + text: r'''
  510 +الكلمات اللاتينية المضافة إلى نص عربي يجب أن توضع في الترتيب الصحيح Right Order مهما كان موضعها في النص.
  511 +At the Beginning of the sentence في بداية الجملة
  512 +أو في منتصفها In the middle of the sentence حيث يكون بعدها كلام عربي
  513 +أو في نهاية النص At the end of the sentence
  514 +أيضا ترتيب الأرقام والرموز يجب 1 أن 2 يكون 3 صحيحاً$.
  515 +ولا ننسى أيضا فواصل السطور Line breakers حيث وجودها في موضعها الصحيح مهم جدا في النصوص ثنائية الاتجاه Bidirectional
  516 + ''',
  517 + style: TextStyle(
  518 + fontSize: 18,
  519 + ),
  520 + ),
  521 + ],
  522 + ),
  523 + ),
  524 + ));
  525 + });
  526 +
495 tearDownAll(() async { 527 tearDownAll(() async {
496 final file = File('arabic.pdf'); 528 final file = File('arabic.pdf');
497 await file.writeAsBytes(await pdf.save()); 529 await file.writeAsBytes(await pdf.save());
No preview for this file type