David PHAM-VAN

Add dpi attribute to PdfPreview

... ... @@ -7,6 +7,7 @@
- Fix raster quality on Android
- Use a CDN for emoji and cupertino fonts
- Improved Android rendering
- Add dpi attribute to PdfPreview
## 5.7.1
... ...
... ... @@ -40,6 +40,7 @@ class PdfPreviewCustom extends StatefulWidget {
this.padding,
this.shouldRepaint = false,
this.loadingWidget,
this.dpi,
}) : super(key: key);
/// Pdf paper page format
... ... @@ -78,6 +79,10 @@ class PdfPreviewCustom extends StatefulWidget {
/// If null, a [CircularProgressIndicator] is used instead.
final Widget? loadingWidget;
/// The rendering dots per inch resolution
/// If not provided, this value is calculated.
final double? dpi;
@override
PdfPreviewCustomState createState() => PdfPreviewCustomState();
}
... ... @@ -103,6 +108,9 @@ class PdfPreviewCustomState extends State<PdfPreviewCustom>
static const _errorMessage = 'Unable to display the document';
@override
double? get forcedDpi => widget.dpi;
@override
void dispose() {
previewUpdate?.cancel();
super.dispose();
... ...
... ... @@ -60,6 +60,7 @@ class PdfPreview extends StatefulWidget {
this.shouldRepaint = false,
this.loadingWidget,
this.onPageFormatChanged,
this.dpi,
}) : super(key: key);
static const _defaultPageFormats = <String, PdfPageFormat>{
... ... @@ -158,6 +159,10 @@ class PdfPreview extends StatefulWidget {
/// The page format has changed
final ValueChanged<PdfPageFormat>? onPageFormatChanged;
/// The rendering dots per inch resolution
/// If not provided, this value is calculated.
final double? dpi;
@override
_PdfPreviewState createState() => _PdfPreviewState();
}
... ... @@ -323,6 +328,7 @@ class _PdfPreviewState extends State<PdfPreview> {
previewPageMargin: widget.previewPageMargin,
scrollViewDecoration: widget.scrollViewDecoration,
shouldRepaint: widget.shouldRepaint,
dpi: widget.dpi,
),
),
if (actions.isNotEmpty && widget.useActions)
... ...
... ... @@ -48,6 +48,8 @@ mixin PdfPreviewRaster on State<PdfPreviewCustom> {
/// Dots per inch
double dpi = PdfPageFormat.inch;
double? get forcedDpi;
var _rastering = false;
Timer? _previewUpdate;
... ... @@ -62,21 +64,26 @@ mixin PdfPreviewRaster on State<PdfPreviewCustom> {
void raster() {
_previewUpdate?.cancel();
_previewUpdate = Timer(_updateTime, () {
final mq = MediaQuery.of(context);
final double dpr;
if (!kIsWeb && Platform.isAndroid) {
if (mq.size.shortestSide * mq.devicePixelRatio < 800) {
dpr = 2 * mq.devicePixelRatio;
if (forcedDpi != null) {
dpi = forcedDpi!;
} else {
final mq = MediaQuery.of(context);
final double dpr;
if (!kIsWeb && Platform.isAndroid) {
if (mq.size.shortestSide * mq.devicePixelRatio < 800) {
dpr = 2 * mq.devicePixelRatio;
} else {
dpr = mq.devicePixelRatio;
}
} else {
dpr = mq.devicePixelRatio;
}
} else {
dpr = mq.devicePixelRatio;
dpi =
(min(mq.size.width - 16, widget.maxPageWidth ?? double.infinity)) *
dpr /
pageFormat.width *
PdfPageFormat.inch;
}
dpi = (min(mq.size.width - 16, widget.maxPageWidth ?? double.infinity)) *
dpr /
pageFormat.width *
PdfPageFormat.inch;
_raster();
});
... ...