Toggle navigation
Toggle navigation
This project
Loading...
Sign in
flutter_package
/
dart_pdf
Go to a project
Toggle navigation
Projects
Groups
Snippets
Help
Toggle navigation pinning
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Authored by
David PHAM-VAN
2020-01-11 14:36:27 -0500
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
6ef2e375219f16a94bd70dd58f4d546dc3861111
6ef2e375
1 parent
78232de1
Fix Image fit
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
108 additions
and
11 deletions
pdf/CHANGELOG.md
pdf/lib/widgets/image.dart
pdf/test/jpeg_test.dart
test/golden/isolate.pdf
test/golden/jpeg.pdf
pdf/CHANGELOG.md
View file @
6ef2e37
...
...
@@ -4,6 +4,7 @@
-
Update dependency to barcode ^1.5.0
-
Update type1 font warning url
-
Fix Image fit
## 1.4.0
...
...
pdf/lib/widgets/image.dart
View file @
6ef2e37
...
...
@@ -18,9 +18,73 @@
part of
widget
;
void
_paintImage
(
{
@required
PdfGraphics
canvas
,
@required
PdfRect
rect
,
@required
PdfImage
image
,
double
scale
=
1.0
,
BoxFit
fit
,
Alignment
alignment
=
Alignment
.
center
,
})
{
assert
(
canvas
!=
null
);
assert
(
image
!=
null
);
assert
(
alignment
!=
null
);
final
PdfPoint
outputSize
=
rect
.
size
;
final
PdfPoint
inputSize
=
PdfPoint
(
image
.
width
.
toDouble
(),
image
.
height
.
toDouble
());
fit
??=
BoxFit
.
scaleDown
;
final
FittedSizes
fittedSizes
=
applyBoxFit
(
fit
,
PdfPoint
(
inputSize
.
x
/
scale
,
inputSize
.
y
/
scale
),
outputSize
);
final
PdfPoint
sourceSize
=
PdfPoint
(
fittedSizes
.
source
.
x
*
scale
,
fittedSizes
.
source
.
y
*
scale
);
final
PdfPoint
destinationSize
=
fittedSizes
.
destination
;
final
double
halfWidthDelta
=
(
outputSize
.
x
-
destinationSize
.
x
)
/
2.0
;
final
double
halfHeightDelta
=
(
outputSize
.
y
-
destinationSize
.
y
)
/
2.0
;
final
double
dx
=
halfWidthDelta
+
alignment
.
x
*
halfWidthDelta
;
final
double
dy
=
halfHeightDelta
+
alignment
.
y
*
halfHeightDelta
;
final
PdfPoint
destinationPosition
=
rect
.
topLeft
.
translate
(
dx
,
dy
);
final
PdfRect
destinationRect
=
PdfRect
.
fromPoints
(
destinationPosition
,
destinationSize
);
final
PdfRect
sourceRect
=
alignment
.
inscribe
(
sourceSize
,
PdfRect
.
fromPoints
(
PdfPoint
.
zero
,
inputSize
),
);
_drawImageRect
(
canvas
,
image
,
sourceRect
,
destinationRect
);
}
void
_drawImageRect
(
PdfGraphics
canvas
,
PdfImage
image
,
PdfRect
sourceRect
,
PdfRect
destinationRect
)
{
final
double
fw
=
destinationRect
.
width
/
sourceRect
.
width
;
final
double
fh
=
destinationRect
.
height
/
sourceRect
.
height
;
canvas
.
saveContext
();
canvas
..
drawRect
(
destinationRect
.
x
,
destinationRect
.
y
,
destinationRect
.
width
,
destinationRect
.
height
,
)
..
clipPath
()
..
drawImage
(
image
,
destinationRect
.
x
-
sourceRect
.
x
*
fw
,
destinationRect
.
y
-
sourceRect
.
y
*
fh
,
image
.
width
.
toDouble
()
*
fw
,
image
.
height
.
toDouble
()
*
fh
,
)
..
restoreContext
();
}
class
Image
extends
Widget
{
Image
(
this
.
image
,
{
this
.
fit
=
BoxFit
.
contain
})
:
assert
(
image
!=
null
),
Image
(
this
.
image
,
{
this
.
fit
=
BoxFit
.
contain
,
this
.
alignment
=
Alignment
.
center
,
this
.
width
,
this
.
height
,
})
:
assert
(
image
!=
null
),
aspectRatio
=
image
.
height
.
toDouble
()
/
image
.
width
.
toDouble
();
final
PdfImage
image
;
...
...
@@ -29,6 +93,12 @@ class Image extends Widget {
final
BoxFit
fit
;
final
Alignment
alignment
;
final
double
width
;
final
double
height
;
@override
void
layout
(
Context
context
,
BoxConstraints
constraints
,
{
bool
parentUsesSize
=
false
})
{
...
...
@@ -50,7 +120,13 @@ class Image extends Widget {
void
paint
(
Context
context
)
{
super
.
paint
(
context
);
context
.
canvas
.
drawImage
(
image
,
box
.
x
,
box
.
y
,
box
.
width
,
box
.
height
);
_paintImage
(
canvas:
context
.
canvas
,
image:
image
,
rect:
box
,
alignment:
alignment
,
fit:
fit
,
);
}
}
...
...
pdf/test/jpeg_test.dart
View file @
6ef2e37
...
...
@@ -26,19 +26,20 @@ import 'package:pdf/widgets.dart';
import
'package:test/test.dart'
;
Document
pdf
;
PdfImage
image
;
void
main
(
)
{
setUpAll
(()
{
setUpAll
(()
async
{
Document
.
debug
=
true
;
pdf
=
Document
();
});
test
(
'Pdf Jpeg Download'
,
()
async
{
final
PdfImage
image
=
PdfImage
.
jpeg
(
image
=
PdfImage
.
jpeg
(
pdf
.
document
,
image:
await
download
(
'https://www.nfet.net/nfet.jpg'
),
);
});
test
(
'Pdf Jpeg Download'
,
()
async
{
pdf
.
addPage
(
Page
(
build:
(
Context
context
)
=>
Center
(
child:
Image
(
image
)),
));
...
...
@@ -64,6 +65,25 @@ void main() {
);
});
test
(
'Pdf Image fit'
,
()
async
{
pdf
.
addPage
(
MultiPage
(
build:
(
Context
context
)
=>
List
<
Widget
>.
generate
(
BoxFit
.
values
.
length
,
(
int
index
)
{
final
BoxFit
fit
=
BoxFit
.
values
[
index
];
return
SizedBox
(
width:
200
,
height:
100
,
child:
Image
(
image
,
fit:
fit
,
),
);
}),
),
);
});
tearDownAll
(()
{
final
File
file
=
File
(
'jpeg.pdf'
);
file
.
writeAsBytesSync
(
pdf
.
save
());
...
...
test/golden/isolate.pdf
View file @
6ef2e37
No preview for this file type
test/golden/jpeg.pdf
View file @
6ef2e37
No preview for this file type
Please
register
or
login
to post a comment