David PHAM-VAN

Fix SVG fit alignment

# Changelog
## 2.1.0
- Fix SVG fit alignment
## 2.0.0
- A borderRadius can only be given for a uniform Border
... ...
... ... @@ -27,43 +27,52 @@ class SvgImage extends Widget {
factory SvgImage({
@required String svg,
BoxFit fit = BoxFit.contain,
Alignment alignment = Alignment.center,
bool clip = true,
double width,
double height,
}) {
assert(clip != null);
assert(alignment != null);
final xml = XmlDocument.parse(svg);
final parser = SvgParser(xml: xml);
return SvgImage._fromPainter(
return SvgImage._fromParser(
parser,
fit,
alignment,
clip,
width,
height,
);
}
SvgImage._fromPainter(
SvgImage._fromParser(
this._svgParser,
this.fit,
this.alignment,
this.clip,
this.width,
this.height,
) : assert(_svgParser != null),
assert(fit != null);
assert(fit != null),
assert(alignment != null);
final SvgParser _svgParser;
final BoxFit fit;
final Alignment alignment;
final bool clip;
final double width;
final double height;
FittedSizes sizes;
@override
void layout(Context context, BoxConstraints constraints,
{bool parentUsesSize = false}) {
... ... @@ -78,10 +87,7 @@ class SvgImage extends Widget {
? constraints.maxHeight
: constraints.constrainHeight(_svgParser.viewBox.height);
final sizes = applyBoxFit(
fit,
PdfPoint(_svgParser.viewBox.width, _svgParser.viewBox.height),
PdfPoint(w, h));
sizes = applyBoxFit(fit, _svgParser.viewBox.size, PdfPoint(w, h));
box = PdfRect.fromPoints(PdfPoint.zero, sizes.destination);
}
... ... @@ -89,19 +95,20 @@ class SvgImage extends Widget {
void paint(Context context) {
super.paint(context);
final mat = Matrix4.identity();
mat.translate(
box.x,
box.y + box.height,
);
mat.scale(
box.width / _svgParser.viewBox.width,
-box.height / _svgParser.viewBox.height,
);
mat.translate(
-_svgParser.viewBox.x,
-_svgParser.viewBox.y,
);
final _alignment = Alignment(alignment.x, -alignment.y);
final sourceRect = _alignment.inscribe(sizes.source, _svgParser.viewBox);
final sx = sizes.destination.x / sizes.source.x;
final sy = sizes.destination.y / sizes.source.y;
final dx = sourceRect.x * sx;
final dy = sourceRect.y * sy;
final mat = Matrix4.identity()
..translate(
box.x - dx,
box.y + dy + box.height,
)
..scale(sx, -sy);
context.canvas.saveContext();
if (clip) {
context.canvas
... ...
... ... @@ -4,7 +4,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: 2.0.0
version: 2.1.0
environment:
sdk: ">=2.3.0 <3.0.0"
... ...
... ... @@ -110,6 +110,72 @@ void main() {
);
});
test('SVG Widgets BoxFit.cover and alignment', () {
const svg =
'<?xml version="1.0" encoding="utf-8"?><svg version="1.1" viewBox="10 20 200 200" xmlns="http://www.w3.org/2000/svg"><circle style="fill-opacity: 0.19; fill: rgb(0, 94, 255);" cx="110" cy="120" r="90"/><rect x="10" y="20" width="200" height="200" stroke="blue" fill="none"/><line style="stroke: black;" x1="110" y1="110" x2="110" y2="130"/><line style="stroke: black" x1="100" y1="120" x2="120" y2="120"/></svg>';
pdf.addPage(
Page(
build: (context) => Column(
children: [
GridView(
crossAxisCount: 3,
mainAxisSpacing: 10,
crossAxisSpacing: 10,
childAspectRatio: 0.3,
children: [
for (final align in <Alignment>[
Alignment.topLeft,
Alignment.topCenter,
Alignment.topRight,
Alignment.centerLeft,
Alignment.center,
Alignment.centerRight,
Alignment.bottomLeft,
Alignment.bottomCenter,
Alignment.bottomRight,
])
SvgImage(
svg: svg,
fit: BoxFit.cover,
alignment: align,
),
],
),
SizedBox(height: 10),
SizedBox(
width: 180,
child: GridView(
crossAxisCount: 3,
mainAxisSpacing: 10,
crossAxisSpacing: 10,
childAspectRatio: 3.3,
children: [
for (final align in <Alignment>[
Alignment.topLeft,
Alignment.topCenter,
Alignment.topRight,
Alignment.centerLeft,
Alignment.center,
Alignment.centerRight,
Alignment.bottomLeft,
Alignment.bottomCenter,
Alignment.bottomRight,
])
SvgImage(
svg: svg,
fit: BoxFit.cover,
alignment: align,
),
],
),
),
],
),
),
);
});
tearDownAll(() async {
final file = File('widgets-svg.pdf');
await file.writeAsBytes(await pdf.save());
... ...
... ... @@ -16,9 +16,11 @@
import 'dart:typed_data';
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/mockito.dart';
import 'package:pdf/pdf.dart';
import 'package:plugin_platform_interface/plugin_platform_interface.dart';
import 'package:printing/printing.dart';
import 'package:printing/src/interface.dart';
... ... @@ -112,6 +114,19 @@ void main() {
null,
);
});
test('test image', () async {
final bytes = Uint8List.fromList([
137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82, 0, 0, 1, //
0, 0, 0, 1, 0, 1, 3, 0, 0, 0, 102, 188, 58, 37, 0, 0, 0, 3, 80, 76, 84,
69, 181, 208, 208, 99, 4, 22, 234, 0, 0, 0, 31, 73, 68, 65, 84, 104,
129, 237, 193, 1, 13, 0, 0, 0, 194, 160, 247, 79, 109, 14, 55, 160, 0, 0,
0, 0, 0, 0, 0, 0, 190, 13, 33, 0, 0, 1, 154, 96, 225, 213, 0, 0, 0, 0, 73,
69, 78, 68, 174, 66, 96, 130
]);
final imageProvider = Image.memory(bytes).image;
expect(await flutterImageProvider(imageProvider), isNotNull);
});
}
class MockPrinting extends Mock
... ...