Fix #154: Switch to Pdfium; better PDF async loading

This commit is contained in:
Paddy Xu
2017-12-22 01:28:30 +02:00
parent 1bcfd8db08
commit fe39854b57
17 changed files with 1675 additions and 545 deletions

View File

@@ -1,309 +0,0 @@
// Copyright © 2017 Paddy Xu
//
// This file is part of QuickLook program.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using QuickLook.Helpers;
namespace QuickLook.Plugin.PDFViewer
{
internal class LibMuPdf
{
public static Bitmap RenderPage(IntPtr context, IntPtr document, IntPtr page, double zoomFactor)
{
var pageBound = new Rectangle();
var ctm = new Matrix();
var pix = IntPtr.Zero;
var dev = IntPtr.Zero;
if (App.Is64Bit)
NativeMethods.BoundPage_64(document, page, ref pageBound);
else
NativeMethods.BoundPage_32(document, page, ref pageBound);
var scale = DpiHelper.GetCurrentScaleFactor();
var zoomX = zoomFactor * scale.Horizontal;
var zoomY = zoomFactor * scale.Vertical;
// gets the size of the page and multiplies it with zoom factors
var width = (int) (pageBound.Width * zoomX);
var height = (int) (pageBound.Height * zoomY);
// sets the matrix as a scaling matrix (zoomX,0,0,zoomY,0,0)
ctm.A = (float) zoomX;
ctm.D = (float) zoomY;
// creates a pixmap the same size as the width and height of the page
if (App.Is64Bit)
pix = NativeMethods.NewPixmap_64(context,
NativeMethods.LookupDeviceColorSpace_64(context, "DeviceRGB"), width, height);
else
pix = NativeMethods.NewPixmap_32(context,
NativeMethods.LookupDeviceColorSpace_32(context, "DeviceRGB"), width, height);
// sets white color as the background color of the pixmap
if (App.Is64Bit)
NativeMethods.ClearPixmap_64(context, pix, 0xFF);
else
NativeMethods.ClearPixmap_32(context, pix, 0xFF);
// creates a drawing device
dev = App.Is64Bit
? NativeMethods.NewDrawDevice_64(context, pix)
: NativeMethods.NewDrawDevice_32(context, pix);
// draws the page on the device created from the pixmap
if (App.Is64Bit)
NativeMethods.RunPage_64(document, page, dev, ref ctm, IntPtr.Zero);
else
NativeMethods.RunPage_32(document, page, dev, ref ctm, IntPtr.Zero);
if (App.Is64Bit)
NativeMethods.FreeDevice_64(dev); // frees the resources consumed by the device
else
NativeMethods.FreeDevice_32(dev); // frees the resources consumed by the device
dev = IntPtr.Zero;
// creates a colorful bitmap of the same size of the pixmap
var bmp = new Bitmap(width, height, PixelFormat.Format24bppRgb);
var imageData = bmp.LockBits(new System.Drawing.Rectangle(0, 0, width, height), ImageLockMode.ReadWrite,
bmp.PixelFormat);
unsafe
{
// converts the pixmap data to Bitmap data
byte* ptrSrcOrigin;
if (App.Is64Bit)
ptrSrcOrigin =
(byte*) NativeMethods.GetSamples_64(context, pix); // gets the rendered data from the pixmap
else
ptrSrcOrigin = (byte*) NativeMethods
.GetSamples_32(context, pix); // gets the rendered data from the pixmap
var ptrDestOrigin = (byte*) imageData.Scan0;
Parallel.For(0, height, y =>
{
var ptrDest = ptrDestOrigin + imageData.Stride * y;
var ptrSrc = ptrSrcOrigin + width * 4 * y;
Parallel.For(0, width, x =>
{
var pl = ptrDest + 3 * x;
var sl = ptrSrc + 4 * x;
//Swap these here instead of in MuPDF because most pdf images will be rgb or cmyk.
//Since we are going through the pixels one by one anyway swap here to save a conversion from rgb to bgr.
pl[2] = sl[0]; //b-r
pl[1] = sl[1]; //g-g
pl[0] = sl[2]; //r-b
//sl[3] is the alpha channel, we will skip it here
});
});
/*for (var y = 0; y < height; y++)
{
var pl = ptrDestOrigin;
var sl = ptrSrcOrigin;
for (var x = 0; x < width; x++)
{
//S these here instead of in MuPDF because most pdf images will be rgb or cmyk.
//Since we are going through the pixels one by one anyway swap here to save a conversion from rgb to bgr.
pl[2] = sl[0]; //b-r
pl[1] = sl[1]; //g-g
pl[0] = sl[2]; //r-b
//sl[3] is the alpha channel, we will skip it here
pl += 3;
sl += 4;
}
ptrDestOrigin += imageData.Stride;
ptrSrcOrigin += width * 4;
}*/
}
bmp.UnlockBits(imageData);
if (App.Is64Bit)
NativeMethods.DropPixmap_64(context, pix);
else
NativeMethods.DropPixmap_32(context, pix);
bmp.SetResolution(scale.Horizontal * DpiHelper.DefaultDpi, scale.Vertical * DpiHelper.DefaultDpi);
return bmp;
}
public struct Rectangle
{
public float Left, Top, Right, Bottom;
public float Width => Right - Left;
public float Height => Bottom - Top;
}
public struct Matrix
{
public float A, B, C, D, E, F;
public Matrix(float a, float b, float c, float d, float e, float f)
{
A = a;
B = b;
C = c;
D = d;
E = e;
F = f;
}
}
internal class NativeMethods
{
private const uint FzStoreDefault = 256 << 20;
private const string MuPdfVersion = "1.6";
public static IntPtr NewContext()
{
return App.Is64Bit
? NewContext_64(IntPtr.Zero, IntPtr.Zero, FzStoreDefault, MuPdfVersion)
: NewContext_32(IntPtr.Zero, IntPtr.Zero, FzStoreDefault, MuPdfVersion);
}
[DllImport("LibMuPdf32.dll", EntryPoint = "fz_new_context_imp",
CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr NewContext_32(IntPtr alloc, IntPtr locks, uint maxStore, string version);
[DllImport("LibMuPdf32.dll", EntryPoint = "fz_free_context", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr FreeContext_32(IntPtr ctx);
[DllImport("LibMuPdf32.dll", EntryPoint = "fz_open_file_w", CharSet = CharSet.Unicode,
CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr OpenFile_32(IntPtr ctx, string fileName);
[DllImport("LibMuPdf32.dll", EntryPoint = "pdf_open_document_with_stream",
CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr OpenDocumentStream_32(IntPtr ctx, IntPtr stm);
[DllImport("LibMuPdf32.dll", EntryPoint = "fz_close", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr CloseStream_32(IntPtr stm);
[DllImport("LibMuPdf32.dll", EntryPoint = "pdf_close_document",
CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr CloseDocument_32(IntPtr doc);
[DllImport("LibMuPdf32.dll", EntryPoint = "pdf_count_pages", CallingConvention = CallingConvention.Cdecl)]
public static extern int CountPages_32(IntPtr doc);
[DllImport("LibMuPdf32.dll", EntryPoint = "pdf_bound_page", CallingConvention = CallingConvention.Cdecl)]
public static extern void BoundPage_32(IntPtr doc, IntPtr page, ref Rectangle bound);
[DllImport("LibMuPdf32.dll", EntryPoint = "fz_clear_pixmap_with_value",
CallingConvention = CallingConvention.Cdecl)]
public static extern void ClearPixmap_32(IntPtr ctx, IntPtr pix, int byteValue);
[DllImport("LibMuPdf32.dll", EntryPoint = "fz_lookup_device_colorspace",
CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr LookupDeviceColorSpace_32(IntPtr ctx, string colorspace);
[DllImport("LibMuPdf32.dll", EntryPoint = "fz_free_device", CallingConvention = CallingConvention.Cdecl)]
public static extern void FreeDevice_32(IntPtr dev);
[DllImport("LibMuPdf32.dll", EntryPoint = "pdf_free_page", CallingConvention = CallingConvention.Cdecl)]
public static extern void FreePage_32(IntPtr doc, IntPtr page);
[DllImport("LibMuPdf32.dll", EntryPoint = "pdf_load_page", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr LoadPage_32(IntPtr doc, int pageNumber);
[DllImport("LibMuPdf32.dll", EntryPoint = "fz_new_draw_device",
CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr NewDrawDevice_32(IntPtr ctx, IntPtr pix);
[DllImport("LibMuPdf32.dll", EntryPoint = "fz_new_pixmap", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr NewPixmap_32(IntPtr ctx, IntPtr colorspace, int width, int height);
[DllImport("LibMuPdf32.dll", EntryPoint = "pdf_run_page", CallingConvention = CallingConvention.Cdecl)]
public static extern void RunPage_32(IntPtr doc, IntPtr page, IntPtr dev, ref Matrix transform,
IntPtr cookie);
[DllImport("LibMuPdf32.dll", EntryPoint = "fz_drop_pixmap", CallingConvention = CallingConvention.Cdecl)]
public static extern void DropPixmap_32(IntPtr ctx, IntPtr pix);
[DllImport("LibMuPdf32.dll", EntryPoint = "fz_pixmap_samples", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr GetSamples_32(IntPtr ctx, IntPtr pix);
[DllImport("LibMuPdf64.dll", EntryPoint = "fz_new_context_imp",
CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr NewContext_64(IntPtr alloc, IntPtr locks, uint maxStore, string version);
[DllImport("LibMuPdf64.dll", EntryPoint = "fz_free_context", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr FreeContext_64(IntPtr ctx);
[DllImport("LibMuPdf64.dll", EntryPoint = "fz_open_file_w", CharSet = CharSet.Unicode,
CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr OpenFile_64(IntPtr ctx, string fileName);
[DllImport("LibMuPdf64.dll", EntryPoint = "pdf_open_document_with_stream",
CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr OpenDocumentStream_64(IntPtr ctx, IntPtr stm);
[DllImport("LibMuPdf64.dll", EntryPoint = "fz_close", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr CloseStream_64(IntPtr stm);
[DllImport("LibMuPdf64.dll", EntryPoint = "pdf_close_document",
CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr CloseDocument_64(IntPtr doc);
[DllImport("LibMuPdf64.dll", EntryPoint = "pdf_count_pages", CallingConvention = CallingConvention.Cdecl)]
public static extern int CountPages_64(IntPtr doc);
[DllImport("LibMuPdf64.dll", EntryPoint = "pdf_bound_page", CallingConvention = CallingConvention.Cdecl)]
public static extern void BoundPage_64(IntPtr doc, IntPtr page, ref Rectangle bound);
[DllImport("LibMuPdf64.dll", EntryPoint = "fz_clear_pixmap_with_value",
CallingConvention = CallingConvention.Cdecl)]
public static extern void ClearPixmap_64(IntPtr ctx, IntPtr pix, int byteValue);
[DllImport("LibMuPdf64.dll", EntryPoint = "fz_lookup_device_colorspace",
CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr LookupDeviceColorSpace_64(IntPtr ctx, string colorspace);
[DllImport("LibMuPdf64.dll", EntryPoint = "fz_free_device", CallingConvention = CallingConvention.Cdecl)]
public static extern void FreeDevice_64(IntPtr dev);
[DllImport("LibMuPdf64.dll", EntryPoint = "pdf_free_page", CallingConvention = CallingConvention.Cdecl)]
public static extern void FreePage_64(IntPtr doc, IntPtr page);
[DllImport("LibMuPdf64.dll", EntryPoint = "pdf_load_page", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr LoadPage_64(IntPtr doc, int pageNumber);
[DllImport("LibMuPdf64.dll", EntryPoint = "fz_new_draw_device",
CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr NewDrawDevice_64(IntPtr ctx, IntPtr pix);
[DllImport("LibMuPdf64.dll", EntryPoint = "fz_new_pixmap", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr NewPixmap_64(IntPtr ctx, IntPtr colorspace, int width, int height);
[DllImport("LibMuPdf64.dll", EntryPoint = "pdf_run_page", CallingConvention = CallingConvention.Cdecl)]
public static extern void RunPage_64(IntPtr doc, IntPtr page, IntPtr dev, ref Matrix transform,
IntPtr cookie);
[DllImport("LibMuPdf64.dll", EntryPoint = "fz_drop_pixmap", CallingConvention = CallingConvention.Cdecl)]
public static extern void DropPixmap_64(IntPtr ctx, IntPtr pix);
[DllImport("LibMuPdf64.dll", EntryPoint = "fz_pixmap_samples",
CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr GetSamples_64(IntPtr ctx, IntPtr pix);
}
}
}

View File

@@ -0,0 +1,27 @@
<?xml version="1.0"?>
<doc>
<assembly>
<name>PDFiumSharp.Wpf</name>
</assembly>
<members>
<member name="M:PDFiumSharp.RenderingExtensionsWpf.Render(PDFiumSharp.PdfPage,System.Windows.Media.Imaging.WriteableBitmap,System.ValueTuple{System.Int32,System.Int32,System.Int32,System.Int32},PDFiumSharp.PageOrientations,PDFiumSharp.RenderingFlags)">
<summary>
Renders the page to a <see cref="T:System.Windows.Media.Imaging.WriteableBitmap"/>
</summary>
<param name="page">The page which is to be rendered.</param>
<param name="renderTarget">The bitmap to which the page is to be rendered.</param>
<param name="rectDest">The destination rectangle in <paramref name="renderTarget"/>.</param>
<param name="orientation">The orientation at which the page is to be rendered.</param>
<param name="flags">The flags specifying how the page is to be rendered.</param>
</member>
<member name="M:PDFiumSharp.RenderingExtensionsWpf.Render(PDFiumSharp.PdfPage,System.Windows.Media.Imaging.WriteableBitmap,PDFiumSharp.PageOrientations,PDFiumSharp.RenderingFlags)">
<summary>
Renders the page to a <see cref="T:System.Windows.Media.Imaging.WriteableBitmap"/>
</summary>
<param name="page">The page which is to be rendered.</param>
<param name="renderTarget">The bitmap to which the page is to be rendered.</param>
<param name="orientation">The orientation at which the page is to be rendered.</param>
<param name="flags">The flags specifying how the page is to be rendered.</param>
</member>
</members>
</doc>

File diff suppressed because it is too large Load Diff

View File

@@ -1,56 +0,0 @@
// Copyright © 2017 Paddy Xu
//
// This file is part of QuickLook program.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
using System;
using System.Globalization;
using System.Windows.Data;
using QuickLook.ExtensionMethods;
namespace QuickLook.Plugin.PDFViewer
{
public sealed class PageIdToImageConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values.Length < 2)
throw new Exception("PageIdToImageConverter");
var zoom = 0.3f;
if (parameter != null)
float.TryParse((string) parameter, out zoom);
var handle = values[0] as PdfFile;
if (handle == null) return null;
var pageId = (int) values[1];
if (pageId < 0) return null;
var bitmap = handle.GetPage(pageId, zoom);
var bs = bitmap.ToBitmapSource();
bitmap.Dispose();
GC.Collect(GC.MaxGeneration, GCCollectionMode.Optimized);
return bs;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}

View File

@@ -1,128 +0,0 @@
// Copyright © 2017 Paddy Xu
//
// This file is part of QuickLook program.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
using System;
using System.Drawing;
using Size = System.Windows.Size;
namespace QuickLook.Plugin.PDFViewer
{
public class PdfFile : IDisposable
{
private readonly IntPtr _ctx;
private readonly IntPtr _doc;
private readonly IntPtr _stm;
public PdfFile(string path)
{
if (App.Is64Bit)
{
_ctx = LibMuPdf.NativeMethods.NewContext();
_stm = LibMuPdf.NativeMethods.OpenFile_64(_ctx, path);
_doc = LibMuPdf.NativeMethods.OpenDocumentStream_64(_ctx, _stm);
TotalPages = LibMuPdf.NativeMethods.CountPages_64(_doc);
}
else
{
_ctx = LibMuPdf.NativeMethods.NewContext();
_stm = LibMuPdf.NativeMethods.OpenFile_32(_ctx, path);
_doc = LibMuPdf.NativeMethods.OpenDocumentStream_32(_ctx, _stm);
TotalPages = LibMuPdf.NativeMethods.CountPages_32(_doc);
}
}
public int TotalPages { get; }
public void Dispose()
{
GC.SuppressFinalize(this);
if (App.Is64Bit)
{
LibMuPdf.NativeMethods.CloseDocument_64(_doc);
LibMuPdf.NativeMethods.CloseStream_64(_stm);
LibMuPdf.NativeMethods.FreeContext_64(_ctx);
}
else
{
LibMuPdf.NativeMethods.CloseDocument_32(_doc);
LibMuPdf.NativeMethods.CloseStream_32(_stm);
LibMuPdf.NativeMethods.FreeContext_32(_ctx);
}
}
~PdfFile()
{
Dispose();
}
public bool IsLastPage(int pageId)
{
return pageId >= TotalPages;
}
public Size GetPageSize(int pageId, double zoomFactor)
{
if (pageId < 0 || pageId >= TotalPages)
throw new OverflowException(
$"Page id {pageId} should greater or equal than 0 and less than total page count {TotalPages}.");
var p = App.Is64Bit
? LibMuPdf.NativeMethods.LoadPage_64(_doc, pageId)
: LibMuPdf.NativeMethods.LoadPage_32(_doc, pageId);
var realSize = new LibMuPdf.Rectangle();
if (App.Is64Bit)
LibMuPdf.NativeMethods.BoundPage_64(_doc, p, ref realSize);
else
LibMuPdf.NativeMethods.BoundPage_32(_doc, p, ref realSize);
var size = new Size
{
Width = realSize.Right * zoomFactor,
Height = realSize.Bottom * zoomFactor
};
if (App.Is64Bit)
LibMuPdf.NativeMethods.FreePage_64(_doc, p);
else
LibMuPdf.NativeMethods.FreePage_32(_doc, p);
return size;
}
public Bitmap GetPage(int pageId, double zoomFactor)
{
if (pageId < 0 || pageId >= TotalPages)
throw new OverflowException(
$"Page id {pageId} should greater or equal than 0 and less than total page count {TotalPages}.");
var p = App.Is64Bit
? LibMuPdf.NativeMethods.LoadPage_64(_doc, pageId)
: LibMuPdf.NativeMethods.LoadPage_32(_doc, pageId);
var bmp = LibMuPdf.RenderPage(_ctx, _doc, p, zoomFactor);
if (App.Is64Bit)
LibMuPdf.NativeMethods.FreePage_64(_doc, p);
else
LibMuPdf.NativeMethods.FreePage_32(_doc, p);
return bmp;
}
}
}

View File

@@ -0,0 +1,54 @@
// Copyright © 2017 Paddy Xu
//
// This file is part of QuickLook program.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
using System;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using PDFiumSharp;
using QuickLook.Helpers;
namespace QuickLook.Plugin.PDFViewer
{
internal static class PdfPageExtension
{
public static BitmapSource RenderThumbnail(this PdfPage page)
{
var factorX = 130d / page.Width;
var factorY = 210d / page.Height;
return page.Render(Math.Min(factorX, factorY), false);
}
public static BitmapSource Render(this PdfPage page, double factor, bool fixDpi = true)
{
var scale = DpiHelper.GetCurrentScaleFactor();
var dpiX = fixDpi ? scale.Horizontal * DpiHelper.DefaultDpi : 96;
var dpiY = fixDpi ? scale.Vertical * DpiHelper.DefaultDpi : 96;
var realWidth = (int) Math.Ceiling(page.Width * (dpiX / 72) * factor);
var realHeight = (int) Math.Ceiling(page.Height * (dpiY / 72) * factor);
var bitmap = new WriteableBitmap(realWidth, realHeight, dpiX, dpiY, PixelFormats.Bgr24, null);
page.Render(bitmap,
flags: RenderingFlags.LimitImageCache | RenderingFlags.Annotations | RenderingFlags.DontCatch |
RenderingFlags.LcdText);
bitmap.Freeze();
return bitmap;
}
}
}

View File

@@ -12,7 +12,6 @@
d:DesignWidth="720.29">
<UserControl.Resources>
<ResourceDictionary>
<local:PageIdToImageConverter x:Key="PageIdToImageConverter" />
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ListBoxItemStyleNoFocusedBorder.xaml" />
</ResourceDictionary.MergedDictionaries>
@@ -23,12 +22,12 @@
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<ListBox x:Name="listThumbnails" Grid.Column="0" VirtualizingPanel.ScrollUnit="Item"
<ListBox x:Name="listThumbnails" Grid.Column="0" VirtualizingPanel.ScrollUnit="Pixel"
VirtualizingPanel.IsVirtualizing="True" Width="150"
SelectedIndex="0"
Focusable="False"
Background="#00FFFFFF"
ItemsSource="{Binding PageIds, ElementName=thisPdfViewer}"
ItemsSource="{Binding PageThumbnails, ElementName=thisPdfViewer}"
ScrollViewer.HorizontalScrollBarVisibility="Disabled" BorderThickness="0,0,1,0"
ItemContainerStyle="{Binding Mode=OneWay, Source={StaticResource ListBoxItemStyleNoFocusedBorder}}">
<ListBox.ItemTemplate>
@@ -46,14 +45,7 @@
<RowDefinition Height="10" />
</Grid.RowDefinitions>
<Border x:Name="bbd" Grid.Row="1" Grid.Column="1" BorderThickness="1" BorderBrush="#FFE1E1E1">
<Image>
<Image.Source>
<MultiBinding Converter="{StaticResource PageIdToImageConverter}">
<Binding Path="PdfHandleForThumbnails" ElementName="thisPdfViewer" />
<Binding />
</MultiBinding>
</Image.Source>
</Image>
<Image Source="{Binding}" />
</Border>
<!--
<Label Grid.Row="1" Grid.Column="1" Content="{Binding Mode=OneWay, Converter={StaticResource MathConverter}, ConverterParameter=@VALUE+1}" FontSize="14" />
@@ -70,7 +62,8 @@
</ListBox.ItemTemplate>
</ListBox>
<Grid Grid.Column="1" Background="#00EFEFEF">
<imageViewer:ImagePanel x:Name="pagePanel" RenderMode="NearestNeighbor" ShowZoomLevelInfo="False" BackgroundVisibility="Collapsed" />
<imageViewer:ImagePanel x:Name="pagePanel" RenderMode="NearestNeighbor" ShowZoomLevelInfo="False"
BackgroundVisibility="Collapsed" />
</Grid>
</Grid>
</UserControl>

View File

@@ -21,8 +21,11 @@ using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using PDFiumSharp;
using QuickLook.ExtensionMethods;
namespace QuickLook.Plugin.PDFViewer
@@ -35,9 +38,10 @@ namespace QuickLook.Plugin.PDFViewer
private const double MinZoomFactor = 0.1d;
private const double MaxZoomFactor = 5d;
private int _changePageDeltaSum;
private bool _initPage = true;
private bool _pdfLoaded;
private double _viewRenderFactor = 1d;
private double _viewRenderFactor = 0.5d;
public PdfViewerControl()
{
@@ -49,13 +53,14 @@ namespace QuickLook.Plugin.PDFViewer
pagePanel.ImageScrolled += NavigatePage;
}
public ObservableCollection<int> PageIds { get; set; } = new ObservableCollection<int>();
public ObservableCollection<BitmapSource> PageThumbnails { get; set; } =
new ObservableCollection<BitmapSource>();
public PdfFile PdfHandleForThumbnails { get; private set; }
public PdfDocument PdfHandleForThumbnails { get; private set; }
public PdfFile PdfHandle { get; private set; }
public PdfDocument PdfHandle { get; private set; }
public int TotalPages => PdfHandle.TotalPages;
public int TotalPages => PdfHandle.Pages.Count;
public int CurrentPage
{
@@ -81,9 +86,9 @@ namespace QuickLook.Plugin.PDFViewer
}
_pdfLoaded = false;
PdfHandleForThumbnails?.Dispose();
PdfHandleForThumbnails?.Close();
PdfHandleForThumbnails = null;
PdfHandle?.Dispose();
PdfHandle?.Close();
PdfHandle = null;
}
@@ -132,7 +137,7 @@ namespace QuickLook.Plugin.PDFViewer
private void NextPage()
{
if (CurrentPage < PdfHandle.TotalPages - 1)
if (CurrentPage < TotalPages - 1)
{
CurrentPage++;
pagePanel.ScrollToTop();
@@ -163,9 +168,7 @@ namespace QuickLook.Plugin.PDFViewer
pagePanel.MinZoomFactor = MinZoomFactor / factor;
pagePanel.MaxZoomFactor = MaxZoomFactor / factor;
var bitmap = PdfHandle.GetPage(CurrentPage, factor);
var image = bitmap.ToBitmapSource();
bitmap.Dispose();
var image = PdfHandle.Pages[CurrentPage].Render(factor);
pagePanel.Source = image;
pagePanel.ResetZoom();
@@ -188,38 +191,59 @@ namespace QuickLook.Plugin.PDFViewer
CurrentPageChanged?.Invoke(this, new EventArgs());
ReRenderCurrentPage();
if (_initPage)
{
_initPage = false;
pagePanel.DoZoomToFit();
}
}
public static Size GetDesiredControlSizeByFirstPage(string path)
{
var tempHandle = new PdfFile(path);
Size size;
using (var tempHandle = new PdfDocument(path))
{
size = new Size(0, 0);
tempHandle.Pages.Take(5).ForEach(p =>
{
size.Width = Math.Max(size.Width, p.Width);
size.Height = Math.Max(size.Height, p.Height);
});
var size = tempHandle.GetPageSize(0, 1d);
tempHandle.Dispose();
if (tempHandle.TotalPages > 1)
size.Width += /*listThumbnails.ActualWidth*/ 150;
if (tempHandle.Pages.Count > 1)
size.Width += /*listThumbnails.ActualWidth*/ 150;
tempHandle.Close();
}
return size;
}
public void LoadPdf(string path)
{
PageIds.Clear();
PdfHandleForThumbnails = new PdfFile(path);
PdfHandle = new PdfFile(path);
PdfHandle = new PdfDocument(path);
_pdfLoaded = true;
// fill thumbnails list
Enumerable.Range(0, PdfHandle.TotalPages).ForEach(PageIds.Add);
OnPropertyChanged(nameof(PageIds));
BeginLoadThumbnails(path);
if (PdfHandle.TotalPages < 2)
if (PdfHandle.Pages.Count < 2)
listThumbnails.Visibility = Visibility.Collapsed;
}
CurrentPage = 0;
pagePanel.DoZoomToFit();
private void BeginLoadThumbnails(string path, string password = null)
{
new Task(() =>
{
using (var handle = new PdfDocument(path, password))
{
handle.Pages.ForEach(p =>
{
var bs = p.RenderThumbnail();
Dispatcher.BeginInvoke(new Action(() => PageThumbnails.Add(bs)));
});
}
}).Start();
}
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)

View File

@@ -19,7 +19,6 @@ using System;
using System.IO;
using System.Runtime.ExceptionServices;
using System.Text;
using System.Windows;
using System.Windows.Threading;
namespace QuickLook.Plugin.PDFViewer

View File

@@ -60,11 +60,18 @@
<AssemblyOriginatorKeyFile>key.snk</AssemblyOriginatorKeyFile>
</PropertyGroup>
<ItemGroup>
<Reference Include="PDFiumSharp, Version=0.1.2.0, Culture=neutral, PublicKeyToken=8828c73f39c04650, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>PDFiumSharp\PDFiumSharp.dll</HintPath>
</Reference>
<Reference Include="PDFiumSharp.Wpf, Version=0.1.0.0, Culture=neutral, PublicKeyToken=e03c80e9a3af1656, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>PDFiumSharp\PDFiumSharp.Wpf.dll</HintPath>
</Reference>
<Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" />
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Drawing" />
<Reference Include="System.Xaml" />
<Reference Include="WindowsBase" />
</ItemGroup>
@@ -76,9 +83,7 @@
<DependentUpon>PdfViewerControl.xaml</DependentUpon>
</Compile>
<Compile Include="Plugin.cs" />
<Compile Include="LibMuPdf.cs" />
<Compile Include="PageIdToImageConverter.cs" />
<Compile Include="PdfFile.cs" />
<Compile Include="PdfPageExtension.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
@@ -93,14 +98,6 @@
<Private>False</Private>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Content Include="LibMuPdf32.dll">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="LibMuPdf64.dll">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<Page Include="ListBoxItemStyleNoFocusedBorder.xaml">
<SubType>Designer</SubType>
@@ -114,5 +111,13 @@
<ItemGroup>
<None Include="key.snk" />
</ItemGroup>
<ItemGroup>
<Content Include="pdfium_x64.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="pdfium_x86.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>