David PHAM-VAN

Update Image dependency

... ... @@ -17,7 +17,7 @@ dependencies:
url_launcher: ^6.0.6
dev_dependencies:
flutter_launcher_icons: ^0.10.0
# flutter_launcher_icons: ^0.10.0
flutter_lints: ^1.0.4
flutter_test:
sdk: flutter
... ...
# Changelog
## 3.8.5
## 3.9.0
- Improve TTF Writer compatibility
- Apply THE BIDIRECTIONAL ALGORITHM using dart_bidi [Milad akarie]
- Add Inseparable Widget
- Fix unit tests
- Update Image dependency
## 3.8.4
... ...
... ... @@ -41,4 +41,5 @@ export 'src/pdf/obj/smask.dart';
export 'src/pdf/obj/ttffont.dart';
export 'src/pdf/page_format.dart';
export 'src/pdf/point.dart';
export 'src/pdf/raster.dart';
export 'src/pdf/rect.dart';
... ...
... ... @@ -21,6 +21,7 @@ import 'package:image/image.dart' as im;
import '../data_types.dart';
import '../document.dart';
import '../exif.dart';
import '../raster.dart';
import 'xobject.dart';
/// Represents the position of the first pixel in the data stream
... ... @@ -152,12 +153,13 @@ class PdfImage extends PdfXObject {
required im.Image image,
PdfImageOrientation orientation = PdfImageOrientation.topLeft,
}) {
final raster = PdfRasterBase.fromImage(image);
return PdfImage(
pdfDocument,
image: image.getBytes(format: im.Format.rgba),
width: image.width,
height: image.height,
alpha: image.channels == im.Channels.rgba,
image: raster.pixels,
width: raster.width,
height: raster.height,
alpha: raster.alpha,
orientation: orientation,
);
}
... ...
/*
* Copyright (C) 2017, David PHAM-VAN <dev.nfet.net@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import 'dart:async';
import 'dart:typed_data';
import 'package:image/image.dart' as im;
import 'color.dart';
/// Represents a bitmap image
class PdfRasterBase {
/// Create a bitmap image
const PdfRasterBase(
this.width,
this.height,
this.alpha,
this.pixels,
);
factory PdfRasterBase.fromImage(im.Image image) {
final data = image
.convert(format: im.Format.uint8, numChannels: 4, noAnimation: true)
.toUint8List();
return PdfRasterBase(image.width, image.height, true, data);
}
factory PdfRasterBase.fromPng(Uint8List png) {
final img = im.PngDecoder().decode(png)!;
return PdfRasterBase.fromImage(img);
}
static im.Image shadowRect(
double width,
double height,
double spreadRadius,
double blurRadius,
PdfColor color,
) {
final shadow = im.Image(
width: (width + spreadRadius * 2).round(),
height: (height + spreadRadius * 2).round(),
format: im.Format.uint8,
numChannels: 4,
);
im.fillRect(
shadow,
x1: spreadRadius.round(),
y1: spreadRadius.round(),
x2: (spreadRadius + width).round(),
y2: (spreadRadius + height).round(),
color: im.ColorUint8(4)
..r = color.red * 255
..g = color.green * 255
..b = color.blue * 255
..a = color.alpha * 255,
);
return im.gaussianBlur(shadow, radius: blurRadius.round());
}
static im.Image shadowEllipse(
double width,
double height,
double spreadRadius,
double blurRadius,
PdfColor color,
) {
final shadow = im.Image(
width: (width + spreadRadius * 2).round(),
height: (height + spreadRadius * 2).round(),
format: im.Format.uint8,
numChannels: 4,
);
im.fillCircle(
shadow,
x: (spreadRadius + width / 2).round(),
y: (spreadRadius + height / 2).round(),
radius: (width / 2).round(),
color: im.ColorUint8(4)
..r = color.red * 255
..g = color.green * 255
..b = color.blue * 255
..a = color.alpha * 255,
);
return im.gaussianBlur(shadow, radius: blurRadius.round());
}
/// The width of the image
final int width;
/// The height of the image
final int height;
/// The alpha channel is used
final bool alpha;
/// The raw RGBA pixels of the image
final Uint8List pixels;
@override
String toString() => 'Image ${width}x$height ${width * height * 4} bytes';
/// Convert to a PNG image
Future<Uint8List> toPng() async {
final img = asImage();
return im.PngEncoder().encode(img);
}
/// Returns the image as an [Image] object from the pub:image library
im.Image asImage() {
return im.Image.fromBytes(
width: width,
height: height,
bytes: pixels.buffer,
format: im.Format.uint8,
numChannels: 4,
);
}
}
... ...
... ... @@ -15,6 +15,7 @@
*/
import 'dart:convert';
import 'dart:typed_data';
import 'package:image/image.dart' as im;
import 'package:vector_math/vector_math_64.dart';
... ... @@ -74,7 +75,7 @@ class SvgImg extends SvgOperation {
final img = im.decodeImage(bytes)!;
image = PdfImage(
painter.document,
image: img.data.buffer.asUint8List(),
image: img.data?.buffer.asUint8List() ?? Uint8List(0),
width: img.width,
height: img.height,
);
... ...
... ... @@ -16,7 +16,6 @@
import 'dart:math' as math;
import 'package:image/image.dart' as im;
import 'package:meta/meta.dart';
import 'package:vector_math/vector_math_64.dart';
... ... @@ -251,45 +250,6 @@ class BoxShadow {
final PdfPoint offset;
final double blurRadius;
final double spreadRadius;
im.Image _rect(double width, double height) {
final shadow = im.Image(
(width + spreadRadius * 2).round(),
(height + spreadRadius * 2).round(),
);
im.fillRect(
shadow,
spreadRadius.round(),
spreadRadius.round(),
(spreadRadius + width).round(),
(spreadRadius + height).round(),
color.toInt(),
);
im.gaussianBlur(shadow, blurRadius.round());
return shadow;
}
im.Image _ellipse(double width, double height) {
final shadow = im.Image(
(width + spreadRadius * 2).round(),
(height + spreadRadius * 2).round(),
);
im.fillCircle(
shadow,
(spreadRadius + width / 2).round(),
(spreadRadius + height / 2).round(),
(width / 2).round(),
color.toInt(),
);
im.gaussianBlur(shadow, blurRadius.round());
return shadow;
}
}
enum BoxShape { circle, rectangle }
... ... @@ -329,7 +289,8 @@ class BoxDecoration {
if (borderRadius == null) {
if (boxShadow != null) {
for (final s in boxShadow!) {
final i = s._rect(box.width, box.height);
final i = PdfRasterBase.shadowRect(box.width, box.height,
s.spreadRadius, s.blurRadius, s.color);
final m = PdfImage.fromImage(context.document, image: i);
context.canvas.drawImage(
m,
... ... @@ -342,7 +303,8 @@ class BoxDecoration {
} else {
if (boxShadow != null) {
for (final s in boxShadow!) {
final i = s._rect(box.width, box.height);
final i = PdfRasterBase.shadowRect(box.width, box.height,
s.spreadRadius, s.blurRadius, s.color);
final m = PdfImage.fromImage(context.document, image: i);
context.canvas.drawImage(
m,
... ... @@ -357,7 +319,8 @@ class BoxDecoration {
case BoxShape.circle:
if (boxShadow != null && box.width == box.height) {
for (final s in boxShadow!) {
final i = s._ellipse(box.width, box.height);
final i = PdfRasterBase.shadowEllipse(box.width, box.height,
s.spreadRadius, s.blurRadius, s.color);
final m = PdfImage.fromImage(context.document, image: i);
context.canvas.drawImage(
m,
... ...
... ... @@ -187,6 +187,6 @@ class RawImage extends ImageImage {
required int height,
PdfImageOrientation? orientation,
double? dpi,
}) : super(im.Image.fromBytes(width, height, bytes),
}) : super(PdfRasterBase(width, height, true, bytes).asImage(),
orientation: orientation, dpi: dpi);
}
... ...
... ... @@ -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.5
version: 3.9.0
environment:
sdk: ">=2.12.0 <3.0.0"
... ... @@ -11,9 +11,9 @@ environment:
dependencies:
archive: ^3.1.0
barcode: ">=2.2.3 <3.0.0"
bidi: ^2.0.2
bidi: ^2.0.2
crypto: ^3.0.0
image: ">=3.0.1 <4.0.0"
image: ^4.0.0
meta: ">=1.3.0 <2.0.0"
path_parsing: ">=0.2.0 <2.0.0"
vector_math: ^2.1.0
... ...
... ... @@ -179,7 +179,7 @@ void main() {
));
});
test('Container Widgets BoxShadow', () {
test('Container Widgets BoxShadow Rectangle', () {
pdf.addPage(Page(
build: (Context context) => Container(
margin: const EdgeInsets.all(30),
... ... @@ -200,6 +200,28 @@ void main() {
));
});
test('Container Widgets BoxShadow Ellipse', () {
pdf.addPage(Page(
build: (Context context) => Container(
margin: const EdgeInsets.all(30),
padding: const EdgeInsets.all(20),
decoration: const BoxDecoration(
shape: BoxShape.circle,
boxShadow: <BoxShadow>[
BoxShadow(
blurRadius: 4,
spreadRadius: 10,
offset: PdfPoint(2, 2),
),
],
color: PdfColors.blue,
),
width: 200,
height: 200,
),
));
});
tearDownAll(() async {
final file = File('widgets-container.pdf');
await file.writeAsBytes(await pdf.save());
... ...
... ... @@ -4,6 +4,7 @@
- Remove deprecated Android embedding
- Add custom pages builder to PdfPreview widget [Milad akarie]
- Update Image dependency
## 5.9.3
... ...
... ... @@ -24,7 +24,6 @@ import 'dart:ui';
import 'package:flutter/foundation.dart';
import 'package:flutter_web_plugins/flutter_web_plugins.dart';
import 'package:image/image.dart' as im;
import 'package:pdf/pdf.dart';
import 'src/callback.dart';
... ... @@ -358,11 +357,7 @@ class _WebPdfRaster extends PdfRaster {
@override
Uint8List get pixels {
if (_pixels == null) {
final img = im.PngDecoder().decodeImage(png)!;
_pixels = img.data.buffer.asUint8List();
}
_pixels ??= PdfRasterBase.fromPng(png).pixels;
return _pixels!;
}
... ...
... ... @@ -19,28 +19,16 @@ import 'dart:typed_data';
import 'dart:ui' as ui;
import 'package:flutter/painting.dart';
import 'package:image/image.dart' as im;
import 'package:pdf/pdf.dart';
/// Represents a bitmap image
class PdfRaster {
class PdfRaster extends PdfRasterBase {
/// Create a bitmap image
const PdfRaster(
this.width,
this.height,
this.pixels,
);
/// The width of the image
final int width;
/// The height of the image
final int height;
/// The raw RGBA pixels of the image
final Uint8List pixels;
@override
String toString() => 'Image ${width}x$height ${width * height * 4} bytes';
PdfRaster(
int width,
int height,
Uint8List pixels,
) : super(width, height, true, pixels);
/// Decode RGBA raw image to dart:ui Image
Future<ui.Image> toImage() {
... ... @@ -56,16 +44,12 @@ class PdfRaster {
}
/// Convert to a PNG image
@override
Future<Uint8List> toPng() async {
final image = await toImage();
final data = await image.toByteData(format: ui.ImageByteFormat.png);
return data!.buffer.asUint8List();
}
/// Returns the image as an [Image] object from the pub:image library
im.Image asImage() {
return im.Image.fromBytes(width, height, pixels);
}
}
/// Image provider for a [PdfRaster]
... ...
... ... @@ -19,10 +19,10 @@ dependencies:
flutter_web_plugins:
sdk: flutter
http: ^0.13.0
image: ">=3.0.1 <4.0.0"
image: ^4.0.0
js: ^0.6.3
meta: ">=1.3.0 <2.0.0"
pdf: ^3.7.2
pdf: ^3.9.0
plugin_platform_interface: ^2.0.0
dev_dependencies:
... ...
No preview for this file type