This commit is contained in:
Paddy Xu
2017-08-06 15:06:55 +03:00
parent 768425ad0f
commit 3a20245304
41 changed files with 427 additions and 607 deletions

View File

@@ -1,63 +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.Runtime.InteropServices;
namespace QuickLook.Plugin.PDFViewer
{
internal static class DpiHelper
{
public enum DeviceCap
{
/// <summary>
/// Logical pixels inch in X
/// </summary>
LOGPIXELSX = 88,
/// <summary>
/// Logical pixels inch in Y
/// </summary>
LOGPIXELSY = 90
}
public const float DEFAULT_DPI = 96;
public static Dpi GetCurrentDpi()
{
var g = Graphics.FromHwnd(IntPtr.Zero);
var desktop = g.GetHdc();
var dpi = new Dpi
{
HorizontalDpi = GetDeviceCaps(desktop, (int) DeviceCap.LOGPIXELSX),
VerticalDpi = GetDeviceCaps(desktop, (int) DeviceCap.LOGPIXELSY)
};
return dpi;
}
[DllImport("gdi32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
public static extern int GetDeviceCaps(IntPtr hDC, int nIndex);
}
internal class Dpi
{
public float HorizontalDpi { get; set; }
public float VerticalDpi { get; set; }
}
}

View File

@@ -1,80 +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.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace QuickLook.Plugin.PDFViewer
{
public static class Extensions
{
public static void ForEach<T>(this IEnumerable<T> enumeration, Action<T> action)
{
foreach (var item in enumeration)
action(item);
}
public static T GetDescendantByType<T>(this Visual element) where T : class
{
if (element == null)
return default(T);
if (element.GetType() == typeof(T))
return element as T;
T foundElement = null;
(element as FrameworkElement)?.ApplyTemplate();
for (var i = 0; i < VisualTreeHelper.GetChildrenCount(element); i++)
{
var visual = VisualTreeHelper.GetChild(element, i) as Visual;
foundElement = visual.GetDescendantByType<T>();
if (foundElement != null)
break;
}
return foundElement;
}
public static BitmapSource ToBitmapSource(this Bitmap source)
{
BitmapSource bs = null;
try
{
var data = source.LockBits(new Rectangle(0, 0, source.Width, source.Height),
ImageLockMode.ReadOnly, source.PixelFormat);
bs = BitmapSource.Create(source.Width, source.Height, source.HorizontalResolution,
source.VerticalResolution, PixelFormats.Bgr24, null,
data.Scan0, data.Stride * source.Height, data.Stride);
source.UnlockBits(data);
bs.Freeze();
}
catch
{
// ignored
}
return bs;
}
}
}

View File

@@ -19,6 +19,7 @@ using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using QuickLook.Helpers;
namespace QuickLook.Plugin.PDFViewer
{
@@ -37,9 +38,9 @@ namespace QuickLook.Plugin.PDFViewer
else
NativeMethods.BoundPage_32(document, page, ref pageBound);
var currentDpi = DpiHelper.GetCurrentDpi();
var zoomX = zoomFactor * (currentDpi.HorizontalDpi / DpiHelper.DEFAULT_DPI);
var zoomY = zoomFactor * (currentDpi.VerticalDpi / DpiHelper.DEFAULT_DPI);
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);
@@ -119,7 +120,7 @@ namespace QuickLook.Plugin.PDFViewer
else
NativeMethods.DropPixmap_32(context, pix);
bmp.SetResolution(currentDpi.HorizontalDpi, currentDpi.VerticalDpi);
bmp.SetResolution(scale.Horizontal * DpiHelper.DefaultDpi, scale.Vertical * DpiHelper.DefaultDpi);
return bmp;
}

View File

@@ -18,6 +18,7 @@
using System;
using System.Globalization;
using System.Windows.Data;
using QuickLook.ExtensionMethods;
namespace QuickLook.Plugin.PDFViewer
{

View File

@@ -212,7 +212,7 @@ namespace QuickLook.Plugin.PDFViewer
// fill thumbnails list
Enumerable.Range(0, PdfHandle.TotalPages).ForEach(PageIds.Add);
OnPropertyChanged("PageIds");
OnPropertyChanged(nameof(PageIds));
CurrentPage = 0;
pagePanel.DoZoomToFit();

View File

@@ -24,6 +24,8 @@ namespace QuickLook.Plugin.PDFViewer
{
public class Plugin : IViewer
{
private ContextObject _context;
private string _path;
private PdfViewerControl _pdfControl;
public int Priority => int.MaxValue;
@@ -50,6 +52,9 @@ namespace QuickLook.Plugin.PDFViewer
public void Prepare(string path, ContextObject context)
{
_context = context;
_path = path;
var desiredSize = PdfViewerControl.GetDesiredControlSizeByFirstPage(path);
context.SetPreferredSizeFit(desiredSize, 0.8);
@@ -69,8 +74,8 @@ namespace QuickLook.Plugin.PDFViewer
_pdfControl.LoadPdf(path);
context.Title = $"{Path.GetFileName(path)} (1 / {_pdfControl.TotalPages})";
_pdfControl.CurrentPageChanged += (sender2, e2) => context.Title =
$"{Path.GetFileName(path)} ({_pdfControl.CurrentPage + 1} / {_pdfControl.TotalPages})";
_pdfControl.CurrentPageChanged += UpdateVindowCaption;
context.IsBusy = false;
}
catch (Exception e)
@@ -87,8 +92,16 @@ namespace QuickLook.Plugin.PDFViewer
{
GC.SuppressFinalize(this);
_pdfControl.CurrentPageChanged -= UpdateVindowCaption;
_pdfControl?.Dispose();
_pdfControl = null;
_context = null;
}
private void UpdateVindowCaption(object sender, EventArgs e2)
{
_context.Title = $"{Path.GetFileName(_path)} ({_pdfControl.CurrentPage + 1} / {_pdfControl.TotalPages})";
}
~Plugin()

View File

@@ -76,8 +76,6 @@
<DependentUpon>PdfViewerControl.xaml</DependentUpon>
</Compile>
<Compile Include="Plugin.cs" />
<Compile Include="DpiHelpers.cs" />
<Compile Include="Extensions.cs" />
<Compile Include="LibMuPdf.cs" />
<Compile Include="PageIdToImageConverter.cs" />
<Compile Include="PdfFile.cs" />