David PHAM-VAN

Add dpi attribute to PdfPreview

@@ -7,6 +7,7 @@ @@ -7,6 +7,7 @@
7 - Fix raster quality on Android 7 - Fix raster quality on Android
8 - Use a CDN for emoji and cupertino fonts 8 - Use a CDN for emoji and cupertino fonts
9 - Improved Android rendering 9 - Improved Android rendering
  10 +- Add dpi attribute to PdfPreview
10 11
11 ## 5.7.1 12 ## 5.7.1
12 13
@@ -40,6 +40,7 @@ class PdfPreviewCustom extends StatefulWidget { @@ -40,6 +40,7 @@ class PdfPreviewCustom extends StatefulWidget {
40 this.padding, 40 this.padding,
41 this.shouldRepaint = false, 41 this.shouldRepaint = false,
42 this.loadingWidget, 42 this.loadingWidget,
  43 + this.dpi,
43 }) : super(key: key); 44 }) : super(key: key);
44 45
45 /// Pdf paper page format 46 /// Pdf paper page format
@@ -78,6 +79,10 @@ class PdfPreviewCustom extends StatefulWidget { @@ -78,6 +79,10 @@ class PdfPreviewCustom extends StatefulWidget {
78 /// If null, a [CircularProgressIndicator] is used instead. 79 /// If null, a [CircularProgressIndicator] is used instead.
79 final Widget? loadingWidget; 80 final Widget? loadingWidget;
80 81
  82 + /// The rendering dots per inch resolution
  83 + /// If not provided, this value is calculated.
  84 + final double? dpi;
  85 +
81 @override 86 @override
82 PdfPreviewCustomState createState() => PdfPreviewCustomState(); 87 PdfPreviewCustomState createState() => PdfPreviewCustomState();
83 } 88 }
@@ -103,6 +108,9 @@ class PdfPreviewCustomState extends State<PdfPreviewCustom> @@ -103,6 +108,9 @@ class PdfPreviewCustomState extends State<PdfPreviewCustom>
103 static const _errorMessage = 'Unable to display the document'; 108 static const _errorMessage = 'Unable to display the document';
104 109
105 @override 110 @override
  111 + double? get forcedDpi => widget.dpi;
  112 +
  113 + @override
106 void dispose() { 114 void dispose() {
107 previewUpdate?.cancel(); 115 previewUpdate?.cancel();
108 super.dispose(); 116 super.dispose();
@@ -60,6 +60,7 @@ class PdfPreview extends StatefulWidget { @@ -60,6 +60,7 @@ class PdfPreview extends StatefulWidget {
60 this.shouldRepaint = false, 60 this.shouldRepaint = false,
61 this.loadingWidget, 61 this.loadingWidget,
62 this.onPageFormatChanged, 62 this.onPageFormatChanged,
  63 + this.dpi,
63 }) : super(key: key); 64 }) : super(key: key);
64 65
65 static const _defaultPageFormats = <String, PdfPageFormat>{ 66 static const _defaultPageFormats = <String, PdfPageFormat>{
@@ -158,6 +159,10 @@ class PdfPreview extends StatefulWidget { @@ -158,6 +159,10 @@ class PdfPreview extends StatefulWidget {
158 /// The page format has changed 159 /// The page format has changed
159 final ValueChanged<PdfPageFormat>? onPageFormatChanged; 160 final ValueChanged<PdfPageFormat>? onPageFormatChanged;
160 161
  162 + /// The rendering dots per inch resolution
  163 + /// If not provided, this value is calculated.
  164 + final double? dpi;
  165 +
161 @override 166 @override
162 _PdfPreviewState createState() => _PdfPreviewState(); 167 _PdfPreviewState createState() => _PdfPreviewState();
163 } 168 }
@@ -323,6 +328,7 @@ class _PdfPreviewState extends State<PdfPreview> { @@ -323,6 +328,7 @@ class _PdfPreviewState extends State<PdfPreview> {
323 previewPageMargin: widget.previewPageMargin, 328 previewPageMargin: widget.previewPageMargin,
324 scrollViewDecoration: widget.scrollViewDecoration, 329 scrollViewDecoration: widget.scrollViewDecoration,
325 shouldRepaint: widget.shouldRepaint, 330 shouldRepaint: widget.shouldRepaint,
  331 + dpi: widget.dpi,
326 ), 332 ),
327 ), 333 ),
328 if (actions.isNotEmpty && widget.useActions) 334 if (actions.isNotEmpty && widget.useActions)
@@ -48,6 +48,8 @@ mixin PdfPreviewRaster on State<PdfPreviewCustom> { @@ -48,6 +48,8 @@ mixin PdfPreviewRaster on State<PdfPreviewCustom> {
48 /// Dots per inch 48 /// Dots per inch
49 double dpi = PdfPageFormat.inch; 49 double dpi = PdfPageFormat.inch;
50 50
  51 + double? get forcedDpi;
  52 +
51 var _rastering = false; 53 var _rastering = false;
52 54
53 Timer? _previewUpdate; 55 Timer? _previewUpdate;
@@ -62,21 +64,26 @@ mixin PdfPreviewRaster on State<PdfPreviewCustom> { @@ -62,21 +64,26 @@ mixin PdfPreviewRaster on State<PdfPreviewCustom> {
62 void raster() { 64 void raster() {
63 _previewUpdate?.cancel(); 65 _previewUpdate?.cancel();
64 _previewUpdate = Timer(_updateTime, () { 66 _previewUpdate = Timer(_updateTime, () {
65 - final mq = MediaQuery.of(context);  
66 - final double dpr;  
67 - if (!kIsWeb && Platform.isAndroid) {  
68 - if (mq.size.shortestSide * mq.devicePixelRatio < 800) {  
69 - dpr = 2 * mq.devicePixelRatio; 67 + if (forcedDpi != null) {
  68 + dpi = forcedDpi!;
  69 + } else {
  70 + final mq = MediaQuery.of(context);
  71 + final double dpr;
  72 + if (!kIsWeb && Platform.isAndroid) {
  73 + if (mq.size.shortestSide * mq.devicePixelRatio < 800) {
  74 + dpr = 2 * mq.devicePixelRatio;
  75 + } else {
  76 + dpr = mq.devicePixelRatio;
  77 + }
70 } else { 78 } else {
71 dpr = mq.devicePixelRatio; 79 dpr = mq.devicePixelRatio;
72 } 80 }
73 - } else {  
74 - dpr = mq.devicePixelRatio; 81 + dpi =
  82 + (min(mq.size.width - 16, widget.maxPageWidth ?? double.infinity)) *
  83 + dpr /
  84 + pageFormat.width *
  85 + PdfPageFormat.inch;
75 } 86 }
76 - dpi = (min(mq.size.width - 16, widget.maxPageWidth ?? double.infinity)) *  
77 - dpr /  
78 - pageFormat.width *  
79 - PdfPageFormat.inch;  
80 87
81 _raster(); 88 _raster();
82 }); 89 });