Skip to content

Commit

Permalink
RTL Fixes and changelog
Browse files Browse the repository at this point in the history
  • Loading branch information
DavBfr committed Jul 24, 2023
1 parent 4f6ec25 commit 9d3ca6a
Show file tree
Hide file tree
Showing 18 changed files with 580 additions and 280 deletions.
1 change: 1 addition & 0 deletions pdf/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Improve TTF writer with multi-compound characters
- Partially revert underline on spans changes
- Add RTL support [Milad-Akarie]

## 3.10.4

Expand Down
6 changes: 4 additions & 2 deletions pdf/lib/src/pdf/rect.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ import 'point.dart';
class PdfRect {
const PdfRect(this.x, this.y, this.width, this.height);

factory PdfRect.fromLTRB(double left, double top, double right, double bottom) {
factory PdfRect.fromLTRB(
double left, double top, double right, double bottom) {
return PdfRect(left, top, right - left, bottom - top);
}

Expand Down Expand Up @@ -70,7 +71,8 @@ class PdfRect {

/// Returns a new rectangle with edges moved outwards by the given delta.
PdfRect inflate(double delta) {
return PdfRect.fromLTRB(left - delta, top - delta, right + delta, bottom + delta);
return PdfRect.fromLTRB(
left - delta, top - delta, right + delta, bottom + delta);
}

/// Returns a new rectangle with edges moved inwards by the given delta.
Expand Down
191 changes: 130 additions & 61 deletions pdf/lib/src/widgets/basic.dart

Large diffs are not rendered by default.

93 changes: 57 additions & 36 deletions pdf/lib/src/widgets/border_radius.dart
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ abstract class BorderRadiusGeometry {
@override
String toString() {
String? visual, logical;
if (_topLeft == _topRight && _topRight == _bottomLeft && _bottomLeft == _bottomRight) {
if (_topLeft == _topRight &&
_topRight == _bottomLeft &&
_bottomLeft == _bottomRight) {
if (_topLeft != Radius.zero) {
if (_topLeft.x == _topLeft.y) {
visual = 'BorderRadius.circular(${_topLeft.x.toStringAsFixed(1)})';
Expand Down Expand Up @@ -125,10 +127,13 @@ abstract class BorderRadiusGeometry {
result.write(')');
visual = result.toString();
}
if (_topStart == _topEnd && _topEnd == _bottomEnd && _bottomEnd == _bottomStart) {
if (_topStart == _topEnd &&
_topEnd == _bottomEnd &&
_bottomEnd == _bottomStart) {
if (_topStart != Radius.zero) {
if (_topStart.x == _topStart.y) {
logical = 'BorderRadiusDirectional.circular(${_topStart.x.toStringAsFixed(1)})';
logical =
'BorderRadiusDirectional.circular(${_topStart.x.toStringAsFixed(1)})';
} else {
logical = 'BorderRadiusDirectional.all($_topStart)';
}
Expand Down Expand Up @@ -228,8 +233,6 @@ class BorderRadius extends BorderRadiusGeometry {
this.bottomRight = Radius.zero,
});



/// A border radius with all zero radii.
static const BorderRadius zero = BorderRadius.all(Radius.zero);

Expand All @@ -245,14 +248,13 @@ class BorderRadius extends BorderRadiusGeometry {
/// The bottom-right [Radius].
final Radius bottomRight;


@override
bool get isUniform => topLeft == topRight && topLeft == bottomLeft && topLeft == bottomRight;
bool get isUniform =>
topLeft == topRight && topLeft == bottomLeft && topLeft == bottomRight;

@override
Radius get uniform => isUniform ? topLeft : Radius.zero;


void paint(Context context, PdfRect box) {
// Ellipse 4-spline magic number
const _m4 = 0.551784;
Expand All @@ -261,13 +263,23 @@ class BorderRadius extends BorderRadiusGeometry {
// Start
..moveTo(box.x, box.y + bottomLeft.y)
// bottomLeft
..curveTo(box.x, box.y - _m4 * bottomLeft.y + bottomLeft.y, box.x - _m4 * bottomLeft.x + bottomLeft.x, box.y,
box.x + bottomLeft.x, box.y)
..curveTo(
box.x,
box.y - _m4 * bottomLeft.y + bottomLeft.y,
box.x - _m4 * bottomLeft.x + bottomLeft.x,
box.y,
box.x + bottomLeft.x,
box.y)
// bottom
..lineTo(box.x + box.width - bottomRight.x, box.y)
// bottomRight
..curveTo(box.x + _m4 * bottomRight.x + box.width - bottomRight.x, box.y, box.x + box.width,
box.y - _m4 * bottomRight.y + bottomRight.y, box.x + box.width, box.y + bottomRight.y)
..curveTo(
box.x + _m4 * bottomRight.x + box.width - bottomRight.x,
box.y,
box.x + box.width,
box.y - _m4 * bottomRight.y + bottomRight.y,
box.x + box.width,
box.y + bottomRight.y)
// right
..lineTo(box.x + box.width, box.y + box.height - topRight.y)
// topRight
Expand All @@ -281,8 +293,13 @@ class BorderRadius extends BorderRadiusGeometry {
// top
..lineTo(box.x + topLeft.x, box.y + box.height)
// topLeft
..curveTo(box.x - _m4 * topLeft.x + topLeft.x, box.y + box.height, box.x,
box.y + _m4 * topLeft.y + box.height - topLeft.y, box.x, box.y + box.height - topLeft.y)
..curveTo(
box.x - _m4 * topLeft.x + topLeft.x,
box.y + box.height,
box.x,
box.y + _m4 * topLeft.y + box.height - topLeft.y,
box.x,
box.y + box.height - topLeft.y)
// left
..lineTo(box.x, box.y + bottomLeft.y);
}
Expand Down Expand Up @@ -329,41 +346,43 @@ class BorderRadius extends BorderRadiusGeometry {
/// `topRight` instead of `topStart` and `topEnd`).
class BorderRadiusDirectional extends BorderRadiusGeometry {
/// Creates a border radius where all radii are [radius].
const BorderRadiusDirectional.all(Radius radius) : this.only(
topStart: radius,
topEnd: radius,
bottomStart: radius,
bottomEnd: radius,
);
const BorderRadiusDirectional.all(Radius radius)
: this.only(
topStart: radius,
topEnd: radius,
bottomStart: radius,
bottomEnd: radius,
);

/// Creates a border radius where all radii are [Radius.circular(radius)].
BorderRadiusDirectional.circular(double radius) : this.all(
Radius.circular(radius),
);
BorderRadiusDirectional.circular(double radius)
: this.all(
Radius.circular(radius),
);

/// Creates a vertically symmetric border radius where the top and bottom
/// sides of the rectangle have the same radii.
const BorderRadiusDirectional.vertical({
Radius top = Radius.zero,
Radius bottom = Radius.zero,
}) : this.only(
topStart: top,
topEnd: top,
bottomStart: bottom,
bottomEnd: bottom,
);
topStart: top,
topEnd: top,
bottomStart: bottom,
bottomEnd: bottom,
);

/// Creates a horizontally symmetrical border radius where the start and end
/// sides of the rectangle have the same radii.
const BorderRadiusDirectional.horizontal({
Radius start = Radius.zero,
Radius end = Radius.zero,
}) : this.only(
topStart: start,
topEnd: end,
bottomStart: start,
bottomEnd: end,
);
topStart: start,
topEnd: end,
bottomStart: start,
bottomEnd: end,
);

/// Creates a border radius with only the given non-zero values. The other
/// corners will be right angles.
Expand All @@ -378,7 +397,8 @@ class BorderRadiusDirectional extends BorderRadiusGeometry {
///
/// Consider using [BorderRadius.zero] instead, since that object has the same
/// effect, but will be cheaper to [resolve].
static const BorderRadiusDirectional zero = BorderRadiusDirectional.all(Radius.zero);
static const BorderRadiusDirectional zero =
BorderRadiusDirectional.all(Radius.zero);

/// The top-start [Radius].
final Radius topStart;
Expand Down Expand Up @@ -417,7 +437,8 @@ class BorderRadiusDirectional extends BorderRadiusGeometry {
Radius get _bottomRight => Radius.zero;

@override
bool get isUniform => topStart == topEnd && topStart == bottomStart && topStart == bottomEnd;
bool get isUniform =>
topStart == topEnd && topStart == bottomStart && topStart == bottomEnd;

@override
Radius get uniform => isUniform ? topStart : Radius.zero;
Expand All @@ -442,4 +463,4 @@ class BorderRadiusDirectional extends BorderRadiusGeometry {
);
}
}
}
}
7 changes: 4 additions & 3 deletions pdf/lib/src/widgets/decoration.dart
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ class RadialGradient extends Gradient {
final _focal = focal ?? center;

final _radius = math.min(box.width, box.height);
final textDirection = Directionality.of(context);
final textDirection = Directionality.of(context);
context.canvas
..saveContext()
..clipPath()
Expand Down Expand Up @@ -277,7 +277,8 @@ class BoxDecoration {
PdfRect box, [
PaintPhase phase = PaintPhase.all,
]) {
final resolvedBorderRadius = borderRadius?.resolve(Directionality.of(context));
final resolvedBorderRadius =
borderRadius?.resolve(Directionality.of(context));
if (phase == PaintPhase.all || phase == PaintPhase.background) {
if (color != null) {
switch (shape) {
Expand Down Expand Up @@ -363,7 +364,7 @@ class BoxDecoration {

break;
case BoxShape.rectangle:
if (resolvedBorderRadius!= null) {
if (resolvedBorderRadius != null) {
resolvedBorderRadius.paint(context, box);
context.canvas.clipPath();
}
Expand Down
13 changes: 8 additions & 5 deletions pdf/lib/src/widgets/flex.dart
Original file line number Diff line number Diff line change
Expand Up @@ -394,8 +394,9 @@ class Flex extends MultiChildWidget with SpanningWidget {
double? leadingSpace;
late double betweenSpace;

final textDirection = Directionality.of(context) ;
final flipMainAxis = !(_startIsTopLeft(direction, textDirection, verticalDirection) ?? true);
final textDirection = Directionality.of(context);
final flipMainAxis =
!(_startIsTopLeft(direction, textDirection, verticalDirection) ?? true);

switch (mainAxisAlignment) {
case MainAxisAlignment.start:
Expand Down Expand Up @@ -436,8 +437,9 @@ class Flex extends MultiChildWidget with SpanningWidget {
switch (crossAxisAlignment) {
case CrossAxisAlignment.start:
case CrossAxisAlignment.end:
childCrossPosition = _startIsTopLeft(flipAxis(direction), textDirection, verticalDirection)
== (crossAxisAlignment == CrossAxisAlignment.start)
childCrossPosition = _startIsTopLeft(
flipAxis(direction), textDirection, verticalDirection) ==
(crossAxisAlignment == CrossAxisAlignment.start)
? 0.0
: crossSize - _getCrossSize(child);
break;
Expand Down Expand Up @@ -479,7 +481,8 @@ class Flex extends MultiChildWidget with SpanningWidget {
}
}

bool? _startIsTopLeft(Axis direction, TextDirection? textDirection, VerticalDirection? verticalDirection) {
bool? _startIsTopLeft(Axis direction, TextDirection? textDirection,
VerticalDirection? verticalDirection) {
// If the relevant value of textDirection or verticalDirection is null, this returns null too.
switch (direction) {
case Axis.horizontal:
Expand Down
Loading

0 comments on commit 9d3ca6a

Please sign in to comment.