Fix #249: handle Unicode file names in PDF viewer

This commit is contained in:
Paddy Xu
2018-06-06 20:05:47 +03:00
parent 10b2c4b47c
commit 2e370cc972
11 changed files with 81 additions and 1607 deletions

View File

@@ -1,27 +0,0 @@
<?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>

View File

@@ -1,4 +1,4 @@
// Copyright © 2017 Paddy Xu // Copyright © 2018 Paddy Xu
// //
// This file is part of QuickLook program. // This file is part of QuickLook program.
// //
@@ -16,39 +16,43 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>. // along with this program. If not, see <http://www.gnu.org/licenses/>.
using System; using System;
using System.Windows.Media; using System.Drawing;
using System.Windows.Media.Imaging; using System.Windows.Media.Imaging;
using PDFiumSharp; using PdfiumViewer;
using QuickLook.Common.ExtensionMethods;
using QuickLook.Common.Helpers; using QuickLook.Common.Helpers;
namespace QuickLook.Plugin.PDFViewer namespace QuickLook.Plugin.PDFViewer
{ {
internal static class PdfPageExtension internal static class PdfPageExtension
{ {
public static BitmapSource RenderThumbnail(this PdfPage page) public static BitmapSource RenderThumbnail(this PdfDocument doc, int page)
{ {
var factorX = 130d / page.Width; var size = doc.PageSizes[page];
var factorY = 210d / page.Height; var factorX = 130d / size.Width;
var factorY = 210d / size.Height;
return page.Render(Math.Min(factorX, factorY), false); return doc.Render(page, Math.Min(factorX, factorY), false);
} }
public static BitmapSource Render(this PdfPage page, double factor, bool fixDpi = true) public static BitmapSource Render(this PdfDocument doc, int page, double factor, bool fixDpi = true)
{ {
var size = doc.PageSizes[page];
var scale = DpiHelper.GetCurrentScaleFactor(); var scale = DpiHelper.GetCurrentScaleFactor();
var dpiX = fixDpi ? scale.Horizontal * DpiHelper.DefaultDpi : 96; var dpiX = fixDpi ? scale.Horizontal * DpiHelper.DefaultDpi : 96;
var dpiY = fixDpi ? scale.Vertical * DpiHelper.DefaultDpi : 96; var dpiY = fixDpi ? scale.Vertical * DpiHelper.DefaultDpi : 96;
var realWidth = (int) Math.Round(page.Width * scale.Horizontal * factor); var realWidth = (int) Math.Round(size.Width * scale.Horizontal * factor);
var realHeight = (int) Math.Round(page.Height * scale.Vertical * factor); var realHeight = (int) Math.Round(size.Height * scale.Vertical * factor);
var bitmap = new WriteableBitmap(realWidth, realHeight, dpiX, dpiY, PixelFormats.Bgr24, null); var bitmap = doc.Render(page, realWidth, realHeight, dpiX, dpiY,
page.Render(bitmap, PdfRenderFlags.LimitImageCacheSize | PdfRenderFlags.LcdText | PdfRenderFlags.Annotations|PdfRenderFlags.ForPrinting) as Bitmap;
flags: RenderingFlags.LimitImageCache | RenderingFlags.Annotations | RenderingFlags.DontCatch |
RenderingFlags.LcdText);
bitmap.Freeze(); var bs = bitmap?.ToBitmapSource();
return bitmap; bitmap?.Dispose();
bs?.Freeze();
return bs;
} }
} }
} }

View File

@@ -1,4 +1,4 @@
// Copyright © 2017 Paddy Xu // Copyright © 2018 Paddy Xu
// //
// This file is part of QuickLook program. // This file is part of QuickLook program.
// //
@@ -19,13 +19,14 @@ using System;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.ComponentModel; using System.ComponentModel;
using System.Diagnostics; using System.Diagnostics;
using System.IO;
using System.Linq; using System.Linq;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows; using System.Windows;
using System.Windows.Controls; using System.Windows.Controls;
using System.Windows.Media.Imaging; using System.Windows.Media.Imaging;
using PDFiumSharp; using PdfiumViewer;
using QuickLook.Common.ExtensionMethods; using QuickLook.Common.ExtensionMethods;
namespace QuickLook.Plugin.PDFViewer namespace QuickLook.Plugin.PDFViewer
@@ -39,7 +40,10 @@ namespace QuickLook.Plugin.PDFViewer
private bool _initPage = true; private bool _initPage = true;
private double _maxZoomFactor = double.NaN; private double _maxZoomFactor = double.NaN;
private double _minZoomFactor = double.NaN; private double _minZoomFactor = double.NaN;
private PdfDocument _pdfHandle;
private bool _pdfLoaded; private bool _pdfLoaded;
private Stream _pdfStream;
private double _viewRenderFactor = double.NaN; private double _viewRenderFactor = double.NaN;
public PdfViewerControl() public PdfViewerControl()
@@ -58,9 +62,7 @@ namespace QuickLook.Plugin.PDFViewer
public ObservableCollection<BitmapSource> PageThumbnails { get; set; } = public ObservableCollection<BitmapSource> PageThumbnails { get; set; } =
new ObservableCollection<BitmapSource>(); new ObservableCollection<BitmapSource>();
public PdfDocument PdfHandle { get; private set; } public int TotalPages => _pdfHandle.PageCount;
public int TotalPages => PdfHandle.Pages.Count;
public int CurrentPage public int CurrentPage
{ {
@@ -86,8 +88,9 @@ namespace QuickLook.Plugin.PDFViewer
} }
_pdfLoaded = false; _pdfLoaded = false;
PdfHandle?.Close(); _pdfHandle?.Dispose();
PdfHandle = null; _pdfHandle = null;
_pdfStream.Close();
} }
public event PropertyChangedEventHandler PropertyChanged; public event PropertyChangedEventHandler PropertyChanged;
@@ -160,16 +163,16 @@ namespace QuickLook.Plugin.PDFViewer
// First time showing. Set thresholds here. // First time showing. Set thresholds here.
if (double.IsNaN(_minZoomFactor) || double.IsNaN(_maxZoomFactor)) if (double.IsNaN(_minZoomFactor) || double.IsNaN(_maxZoomFactor))
{ {
factor = Math.Min(pagePanel.ActualHeight / PdfHandle.Pages[CurrentPage].Height, factor = Math.Min(pagePanel.ActualHeight / _pdfHandle.PageSizes[CurrentPage].Height,
pagePanel.ActualWidth / PdfHandle.Pages[CurrentPage].Width); pagePanel.ActualWidth / _pdfHandle.PageSizes[CurrentPage].Width);
_viewRenderFactor = factor; _viewRenderFactor = factor;
_minZoomFactor = 0.1 * factor; _minZoomFactor = 0.1 * factor;
_maxZoomFactor = 5 * factor; _maxZoomFactor = 5 * factor;
} }
else if (pagePanel.ZoomToFit) else if (pagePanel.ZoomToFit)
{ {
factor = Math.Min(pagePanel.ActualHeight / PdfHandle.Pages[CurrentPage].Height, factor = Math.Min(pagePanel.ActualHeight / _pdfHandle.PageSizes[CurrentPage].Height,
pagePanel.ActualWidth / PdfHandle.Pages[CurrentPage].Width); pagePanel.ActualWidth / _pdfHandle.PageSizes[CurrentPage].Width);
} }
else else
{ {
@@ -180,7 +183,7 @@ namespace QuickLook.Plugin.PDFViewer
pagePanel.MaxZoomFactor = _maxZoomFactor / factor; pagePanel.MaxZoomFactor = _maxZoomFactor / factor;
} }
var image = PdfHandle.Pages[CurrentPage].Render(factor); var image = _pdfHandle.Render(CurrentPage, factor);
pagePanel.Source = image; pagePanel.Source = image;
pagePanel.ResetZoom(); pagePanel.ResetZoom();
@@ -214,19 +217,21 @@ namespace QuickLook.Plugin.PDFViewer
public static Size GetDesiredControlSizeByFirstPage(string path) public static Size GetDesiredControlSizeByFirstPage(string path)
{ {
Size size; Size size;
using (var tempHandle = new PdfDocument(path))
using (var s = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{ {
size = new Size(0, 0); using (var tempHandle = PdfDocument.Load(s))
tempHandle.Pages.Take(5).ForEach(p =>
{ {
size.Width = Math.Max(size.Width, p.Width); size = new Size(0, 0);
size.Height = Math.Max(size.Height, p.Height); tempHandle.PageSizes.Take(5).ForEach(p =>
}); {
size.Width = Math.Max(size.Width, p.Width);
size.Height = Math.Max(size.Height, p.Height);
});
if (tempHandle.Pages.Count > 1) if (tempHandle.PageCount > 1)
size.Width += /*listThumbnails.ActualWidth*/ 150; size.Width += /*listThumbnails.ActualWidth*/ 150;
}
tempHandle.Close();
} }
return new Size(size.Width * 3, size.Height * 3); return new Size(size.Width * 3, size.Height * 3);
@@ -234,12 +239,13 @@ namespace QuickLook.Plugin.PDFViewer
public void LoadPdf(string path) public void LoadPdf(string path)
{ {
PdfHandle = new PdfDocument(path); _pdfStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
_pdfHandle = PdfDocument.Load(_pdfStream);
_pdfLoaded = true; _pdfLoaded = true;
BeginLoadThumbnails(path); BeginLoadThumbnails(path);
if (PdfHandle.Pages.Count < 2) if (_pdfHandle.PageCount < 2)
listThumbnails.Visibility = Visibility.Collapsed; listThumbnails.Visibility = Visibility.Collapsed;
} }
@@ -247,16 +253,19 @@ namespace QuickLook.Plugin.PDFViewer
{ {
new Task(() => new Task(() =>
{ {
using (var handle = new PdfDocument(path, password)) using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{ {
handle.Pages.ForEach(p => using (var handle = PdfDocument.Load(stream, password))
{ {
var bs = p.RenderThumbnail(); for (var p = 0; p < handle.PageCount; p++)
{
var bs = handle.RenderThumbnail(p);
Dispatcher.BeginInvoke(new Action(() => PageThumbnails.Add(bs))); Dispatcher.BeginInvoke(new Action(() => PageThumbnails.Add(bs)));
}); }
handle.Close(); handle.Dispose();
}
} }
}).Start(); }).Start();
} }

View File

@@ -1,5 +1,7 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\..\packages\PdfiumViewer.Native.x86.v8-xfa.2018.4.8.256\build\PdfiumViewer.Native.x86.v8-xfa.props" Condition="Exists('..\..\packages\PdfiumViewer.Native.x86.v8-xfa.2018.4.8.256\build\PdfiumViewer.Native.x86.v8-xfa.props')" />
<Import Project="..\..\packages\PdfiumViewer.Native.x86_64.v8-xfa.2018.4.8.256\build\PdfiumViewer.Native.x86_64.v8-xfa.props" Condition="Exists('..\..\packages\PdfiumViewer.Native.x86_64.v8-xfa.2018.4.8.256\build\PdfiumViewer.Native.x86_64.v8-xfa.props')" />
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /> <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup> <PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
@@ -12,6 +14,8 @@
<TargetFrameworkVersion>v4.6.2</TargetFrameworkVersion> <TargetFrameworkVersion>v4.6.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment> <FileAlignment>512</FileAlignment>
<TargetFrameworkProfile /> <TargetFrameworkProfile />
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
<DebugSymbols>true</DebugSymbols> <DebugSymbols>true</DebugSymbols>
@@ -61,18 +65,15 @@
</AssemblyOriginatorKeyFile> </AssemblyOriginatorKeyFile>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="PDFiumSharp, Version=0.1.2.0, Culture=neutral, PublicKeyToken=8828c73f39c04650, processorArchitecture=MSIL"> <Reference Include="PdfiumViewer, Version=2.13.0.0, Culture=neutral, PublicKeyToken=91e4789cfb0609e0, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion> <HintPath>..\..\packages\PdfiumViewer.2.13.0.0\lib\net20\PdfiumViewer.dll</HintPath>
<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>
<Reference Include="PresentationCore" /> <Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" /> <Reference Include="PresentationFramework" />
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Core" /> <Reference Include="System.Core" />
<Reference Include="System.Drawing" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xaml" /> <Reference Include="System.Xaml" />
<Reference Include="WindowsBase" /> <Reference Include="WindowsBase" />
</ItemGroup> </ItemGroup>
@@ -110,12 +111,14 @@
</Page> </Page>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Content Include="pdfium_x64.dll"> <None Include="packages.config" />
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="pdfium_x86.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup> </ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\..\packages\PdfiumViewer.Native.x86_64.v8-xfa.2018.4.8.256\build\PdfiumViewer.Native.x86_64.v8-xfa.props')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\PdfiumViewer.Native.x86_64.v8-xfa.2018.4.8.256\build\PdfiumViewer.Native.x86_64.v8-xfa.props'))" />
<Error Condition="!Exists('..\..\packages\PdfiumViewer.Native.x86.v8-xfa.2018.4.8.256\build\PdfiumViewer.Native.x86.v8-xfa.props')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\PdfiumViewer.Native.x86.v8-xfa.2018.4.8.256\build\PdfiumViewer.Native.x86.v8-xfa.props'))" />
</Target>
</Project> </Project>

View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="PdfiumViewer" version="2.13.0.0" targetFramework="net462" />
<package id="PdfiumViewer.Native.x86.v8-xfa" version="2018.4.8.256" targetFramework="net462" />
<package id="PdfiumViewer.Native.x86_64.v8-xfa" version="2018.4.8.256" targetFramework="net462" />
</packages>