Milad akarie
Committed by David PHAM-VAN

Fix Arabic TextAlign.justify issues

Set default text align based on text direction
# Changelog
## 3.8.3
- Fix Arabic TextAlign.justify issues Set default text align based on text direction [Milad akarie]
## 3.8.2
- Fix Compressed Cross-Reference ID
... ...
... ... @@ -56,8 +56,11 @@ abstract class _Span {
var offset = PdfPoint.zero;
double get left;
double get top;
double get width;
double get height;
@override
... ... @@ -544,6 +547,7 @@ class _Line {
final int firstSpan;
final int countSpan;
int get lastSpan => firstSpan + countSpan;
TextAlign get textAlign => parent._textAlign;
... ... @@ -569,39 +573,46 @@ class _Line {
void realign(double totalWidth) {
final spans = parent._spans.sublist(firstSpan, lastSpan);
final isRTL = textDirection == TextDirection.rtl;
var delta = 0.0;
switch (textAlign) {
case TextAlign.left:
delta = isRTL ? totalWidth - wordsWidth : 0;
break;
case TextAlign.right:
delta = totalWidth - wordsWidth;
delta = isRTL ? 0 : totalWidth - wordsWidth;
break;
case TextAlign.center:
delta = (totalWidth - wordsWidth) / 2.0;
break;
case TextAlign.justify:
if (!justify) {
totalWidth = wordsWidth;
break;
}
delta = (totalWidth - wordsWidth) / (spans.length - 1);
var x = 0.0;
for (final span in spans) {
span.offset = span.offset.translate(x, -baseline);
if (isRTL) {
final xOffset = span.offset.x + span.width;
span.offset =
PdfPoint((totalWidth - xOffset) - x, span.offset.y - baseline);
} else {
span.offset = span.offset.translate(x, -baseline);
}
x += delta;
}
return;
}
if (textDirection == TextDirection.rtl) {
if (isRTL) {
for (final span in spans) {
span.offset = PdfPoint(
totalWidth - (span.offset.x + span.width) - delta,
span.offset.y - baseline,
);
}
return;
}
... ... @@ -688,7 +699,6 @@ class RichText extends Widget with SpanningWidget {
return;
}
}
_decorations.add(td);
}
... ... @@ -863,8 +873,13 @@ class RichText extends Widget with SpanningWidget {
final theme = Theme.of(context);
final _softWrap = softWrap ?? theme.softWrap;
final _maxLines = maxLines ?? theme.maxLines;
_textAlign = textAlign ?? theme.textAlign;
final _textDirection = textDirection ?? Directionality.of(context);
_textAlign = textAlign ??
theme.textAlign ??
(_textDirection == TextDirection.rtl
? TextAlign.right
: TextAlign.left);
final _overflow = this.overflow ?? theme.overflow;
final constraintWidth = constraints.hasBoundedWidth
... ...
... ... @@ -80,8 +80,8 @@ class ThemeData extends Inherited {
required this.tableCell,
required this.softWrap,
required this.overflow,
required this.textAlign,
required this.iconTheme,
this.textAlign,
this.maxLines,
}) : assert(defaultTextStyle.inherit == false),
assert(paragraphStyle.inherit == false),
... ... @@ -129,7 +129,6 @@ class ThemeData extends Inherited {
tableCell: defaultStyle.copyWith(fontSize: fontSize * 0.8),
softWrap: true,
overflow: TextOverflow.visible,
textAlign: TextAlign.left,
iconTheme: IconThemeData.fallback(icons),
);
}
... ... @@ -156,7 +155,7 @@ class ThemeData extends Inherited {
final TextStyle tableCell;
final TextAlign textAlign;
final TextAlign? textAlign;
final bool softWrap;
final int? maxLines;
final TextOverflow overflow;
... ...
... ... @@ -3,7 +3,7 @@ description: A pdf producer for Dart. It can create pdf files for both web or fl
homepage: https://github.com/DavBfr/dart_pdf/tree/master/pdf
repository: https://github.com/DavBfr/dart_pdf
issue_tracker: https://github.com/DavBfr/dart_pdf/issues
version: 3.8.2
version: 3.8.3
environment:
sdk: ">=2.12.0 <3.0.0"
... ...
... ... @@ -492,6 +492,31 @@ void main() {
));
});
test('Text Widgets Arabic with TextAlign.justify', () {
pdf.addPage(Page(
build: (Context context) => RichText(
textDirection: TextDirection.rtl,
textAlign: TextAlign.justify,
text: TextSpan(
text: 'قهوة\n',
style: TextStyle(
font: arabicFont,
fontSize: 30,
),
children: const <TextSpan>[
TextSpan(
text:
'القهوة مشروب يعد من بذور الب المحمصة، وينمو في أكثر من 70 لداً. خصوصاً في المناطق الاستوائية في أمريكا الشمالية والجنوبية وجنوب شرق آسيا وشبه القارة الهندية وأفريقيا. ويقال أن البن الأخضر هو ثاني أكثر السلع تداولاً في العالم بعد النفط الخام.',
style: TextStyle(
fontSize: 20,
),
),
],
),
),
));
});
test(
'Text Widgets, Mixed Arabic and Latin words should be rendered in order ',
() {
... ...
No preview for this file type