Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
bezzad committed May 18, 2021
2 parents 23b7fb1 + c1267b9 commit 4978493
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 39 deletions.
33 changes: 19 additions & 14 deletions src/PdfiumViewer/Core/PdfDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,18 +114,12 @@ public static PdfDocument Load(Stream stream, string password)
/// <summary>
/// Number of pages in the PDF document.
/// </summary>
public int PageCount
{
get { return PageSizes.Count; }
}
public int PageCount => _file?.GetPageCount() ?? 0;

/// <summary>
/// Bookmarks stored in this PdfFile
/// </summary>
public PdfBookmarkCollection Bookmarks
{
get { return _file.Bookmarks; }
}
public PdfBookmarkCollection Bookmarks => _file.Bookmarks;

/// <summary>
/// Size of each page in the PDF document.
Expand All @@ -135,11 +129,9 @@ public PdfBookmarkCollection Bookmarks
private PdfDocument(Stream stream, string password)
{
_file = new PdfFile(stream, password);

_pageSizes = _file.GetPDFDocInfo();
if (_pageSizes == null)
throw new Win32Exception();

_pageSizes = new List<SizeF>(PageCount);
for (var i = 0; i < PageCount; i++)
_pageSizes.Add(new SizeF());
PageSizes = new ReadOnlyCollection<SizeF>(_pageSizes);
}

Expand Down Expand Up @@ -169,7 +161,7 @@ public void Render(int page, Graphics graphics, float dpiX, float dpiY, Rectangl
public void Render(int page, Graphics graphics, float dpiX, float dpiY, Rectangle bounds, PdfRenderFlags flags)
{
if (graphics == null)
throw new ArgumentNullException("graphics");
throw new ArgumentNullException(nameof(graphics));
if (_disposed)
throw new ObjectDisposedException(GetType().Name);

Expand Down Expand Up @@ -557,6 +549,19 @@ public PdfInformation GetInformation()
return _file.GetInformation();
}

public SizeF GetPageSize(int pageNo)
{
if (_pageSizes.Count > pageNo && pageNo >= 0)
{
if (_pageSizes[pageNo].IsEmpty)
_pageSizes[pageNo] = _file.GetPDFDocInfo(pageNo);

return _pageSizes[pageNo];
}

return _pageSizes[0];
}

/// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary>
/// <filterpriority>2</filterpriority>
public void Dispose()
Expand Down
4 changes: 4 additions & 0 deletions src/PdfiumViewer/Core/PdfFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ public bool RenderPDFPageToBitmap(int pageNumber, IntPtr bitmapHandle, int dpiX,
return true;
}

public int GetPageCount()
{
return NativeMethods.FPDF_GetPageCount(_document);
}
public PdfPageLinks GetPageLinks(int pageNumber, Size pageSize)
{
if (_disposed)
Expand Down
46 changes: 24 additions & 22 deletions src/PdfiumViewer/ScrollPanel.Kinetic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@ namespace PdfiumViewer
{
public partial class ScrollPanel
{
private const double DefaultFriction = 0.95;
private Point ScrollStartPoint { get; set; }
private Point ScrollStartOffset { get; set; }
private Point PreviousPoint { get; set; }
private Vector Velocity { get; set; }
// Note: don't change the below fields to Property.
// Note: Because INotifyPropertyChanged event change the page number!
private const double DefaultFriction = 0.90;
private const int InertiaHandlerInterval = 20; // milliseconds
private const int InertiaMaxAnimationTime = 3000; // milliseconds
private bool _isMouseDown;
private Vector _velocity;
private Point _scrollTarget;
private int InertiaHandlerInterval { get; set; } = 20; // milliseconds
private int InertiaMaxAnimationTime { get; set; } = 3000; // milliseconds
protected bool IsMouseDown { get; set; }
private Point _scrollStartPoint;
private Point _scrollStartOffset;
private Point _previousPoint;

#region Friction

Expand Down Expand Up @@ -64,18 +66,18 @@ protected override void OnPreviewMouseDown(MouseButtonEventArgs e)
Cursor = Cursors.ScrollAll;
// Save starting point, used later when
// determining how much to scroll.
Velocity = new Vector();
ScrollStartPoint = e.GetPosition(this);
ScrollStartOffset = new Point(HorizontalOffset, VerticalOffset);
IsMouseDown = true;
_velocity = new Vector();
_scrollStartPoint = e.GetPosition(this);
_scrollStartOffset = new Point(HorizontalOffset, VerticalOffset);
_isMouseDown = true;
}
}

protected override void OnPreviewMouseMove(MouseEventArgs e)
{
base.OnPreviewMouseMove(e);

if (EnableKinetic && IsMouseDown)
if (EnableKinetic && _isMouseDown)
{
var currentPoint = e.GetPosition(this);
// Determine the new amount to scroll.
Expand All @@ -94,15 +96,15 @@ protected override void OnPreviewMouseUp(MouseButtonEventArgs e)
if (EnableKinetic)
{
Cursor = Cursors.Hand;
IsMouseDown = false;
_isMouseDown = false;
InertiaHandleMouseUp();
}
}

private Point GetScrollTarget(Point currentPoint)
{
var delta = new Point(ScrollStartPoint.X - currentPoint.X, ScrollStartPoint.Y - currentPoint.Y);
var scrollTarget = new Point(ScrollStartOffset.X + delta.X, ScrollStartOffset.Y + delta.Y);
var delta = new Point(_scrollStartPoint.X - currentPoint.X, _scrollStartPoint.Y - currentPoint.Y);
var scrollTarget = new Point(_scrollStartOffset.X + delta.X, _scrollStartOffset.Y + delta.Y);

if (scrollTarget.Y < 0)
{
Expand All @@ -120,22 +122,22 @@ private Point GetScrollTarget(Point currentPoint)
private void InertiaHandleMouseMove()
{
var currentPoint = Mouse.GetPosition(this);
Velocity = PreviousPoint - currentPoint;
PreviousPoint = currentPoint;
_velocity = _previousPoint - currentPoint;
_previousPoint = currentPoint;
}

private async void InertiaHandleMouseUp()
{
for (var i = 0; i < InertiaMaxAnimationTime / InertiaHandlerInterval; i++)
{
if (IsMouseDown || Velocity.Length <= 1 || Environment.TickCount64 - MouseWheelUpdateTime < InertiaHandlerInterval * 2)
if (_isMouseDown || _velocity.Length <= 1 || Environment.TickCount64 - MouseWheelUpdateTime < InertiaHandlerInterval * 2)
break;

ScrollToHorizontalOffset(_scrollTarget.X);
ScrollToVerticalOffset(_scrollTarget.Y);
_scrollTarget.X += Velocity.X;
_scrollTarget.Y += Velocity.Y;
Velocity *= Friction;
_scrollTarget.X += _velocity.X;
_scrollTarget.Y += _velocity.Y;
_velocity *= Friction;
await Task.Delay(InertiaHandlerInterval);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/PdfiumViewer/ScrollPanel.PdfDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ public void GotoPage(int page)
{
if (IsDocumentLoaded)
{
PageNo = page;
CurrentPageSize = CalculatePageSize(page);

RenderPage(Frame1, page, CurrentPageSize.Width, CurrentPageSize.Height);
Expand All @@ -157,7 +158,6 @@ public void GotoPage(int page)
RenderPage(Frame2, page + 1, CurrentPageSize.Width, CurrentPageSize.Height);
}

PageNo = page;
ScrollToPage(PageNo);
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/PdfiumViewer/ScrollPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public ScrollPanel()
public const double DefaultZoomMin = 0.1;
public const double DefaultZoomMax = 5;
public const double DefaultZoomFactor = 1.2;
protected bool IsDisposed = false;
protected bool IsDisposed;
protected const int SmallScrollChange = 1;
protected const int LargeScrollChange = 10;
protected Process CurrentProcess { get; } = Process.GetCurrentProcess();
Expand Down Expand Up @@ -186,6 +186,7 @@ protected BitmapImage RenderPage(Image frame, int page, int width, int height)
bitmapImage.StreamSource = memory;
bitmapImage.CacheOption = BitmapCacheOption.OnLoad; // not a mistake - see below
bitmapImage.EndInit();
image.Dispose();
}
// Why BitmapCacheOption.OnLoad?
// It seems counter intuitive, but this flag has two effects:
Expand All @@ -199,6 +200,7 @@ protected BitmapImage RenderPage(Image frame, int page, int width, int height)
frame.Height = height;
frame.Source = bitmapImage;
});
GC.Collect();
return bitmapImage;
}
protected Size CalculatePageSize(int? page = null)
Expand All @@ -210,7 +212,7 @@ protected Size CalculatePageSize(int? page = null)

if (IsDocumentLoaded && containerWidth > 0 && containerHeight > 0)
{
var currentPageSize = Document.PageSizes[page.Value];
var currentPageSize = Document.GetPageSize(page.Value);
if (isReverse)
currentPageSize = new SizeF(currentPageSize.Height, currentPageSize.Width);

Expand Down

0 comments on commit 4978493

Please sign in to comment.