Removed Rectangles and replaced with viewports.

Viewport now can specify an origin point.
This commit is contained in:
DJGosnell 2020-10-09 11:58:51 -04:00
Родитель 7d899b101d
Коммит 2bda42610a
5 изменённых файлов: 71 добавлений и 31 удалений

Просмотреть файл

@ -10,7 +10,7 @@ namespace DtronixPdf.Actions
{
public readonly FpdfPageT _pageInstance;
private readonly float _scale;
private readonly RectangleF _viewport;
private readonly Viewport _viewport;
private readonly RenderFlags _flags;
private readonly Color? _backgroundColor;
private readonly bool _includeAlpha;
@ -20,7 +20,7 @@ namespace DtronixPdf.Actions
ThreadDispatcher dispatcher,
FpdfPageT pageInstance,
float scale,
RectangleF viewport,
Viewport viewport,
RenderFlags flags,
Color? backgroundColor,
bool includeAlpha)
@ -38,15 +38,15 @@ namespace DtronixPdf.Actions
{
var bitmap = fpdfview.FPDFBitmapCreateEx(
(int)_viewport.Width,
(int)_viewport.Height,
(int)_viewport.Size.Width,
(int)_viewport.Size.Height,
(int) (_includeAlpha ? FPDFBitmapFormat.BGRA : FPDFBitmapFormat.BGR) ,
IntPtr.Zero,
0);
if(_backgroundColor.HasValue)
fpdfview.FPDFBitmapFillRect(
bitmap, 0, 0, (int)_viewport.Width, (int)_viewport.Height, (uint) _backgroundColor.Value.ToArgb());
bitmap, 0, 0, (int)_viewport.Size.Width, (int)_viewport.Size.Height, (uint) _backgroundColor.Value.ToArgb());
if (bitmap == null)
throw new Exception("failed to create a bitmap object");
@ -63,20 +63,38 @@ namespace DtronixPdf.Actions
matrix.B = 0;
matrix.C = 0;
matrix.D = _scale;
matrix.E = -_viewport.Left;
matrix.F = -_viewport.Top;
switch (_viewport.OriginLocation)
{
case ViewportOrigin.Unset:
break;
case ViewportOrigin.TopLeft:
matrix.E = -_viewport.Origin.X;
matrix.F = -_viewport.Origin.Y;
break;
case ViewportOrigin.TopRight:
break;
case ViewportOrigin.BottomRight:
break;
case ViewportOrigin.BottomLeft:
break;
case ViewportOrigin.Center:
break;
default:
throw new ArgumentOutOfRangeException();
}
clipping.Left = 0;
clipping.Right = _viewport.Width;
clipping.Right = _viewport.Size.Width;
clipping.Bottom = 0;
clipping.Top = _viewport.Height;
clipping.Top = _viewport.Size.Height;
fpdfview.FPDF_RenderPageBitmapWithMatrix(bitmap, _pageInstance, matrix, clipping, (int) _flags);
return new PdfBitmap(
bitmap,
(int)_viewport.Width,
(int)_viewport.Height,
(int)_viewport.Size.Width,
(int)_viewport.Size.Height,
_dispatcher,
_includeAlpha ? PixelFormat.Format32bppArgb : PixelFormat.Format24bppRgb,
_scale,

Просмотреть файл

@ -27,10 +27,20 @@ namespace DtronixPdf
public float Scale { get; }
public RectangleF Viewport { get; }
public Viewport Viewport { get; }
public bool IsDisposed { get; private set; }
/// <summary>
/// Only call within the dispatcher since dll calls are made.
/// </summary>
/// <param name="pdfBitmap"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="dispatcher"></param>
/// <param name="format"></param>
/// <param name="scale"></param>
/// <param name="viewport"></param>
internal PdfBitmap(
FpdfBitmapT pdfBitmap,
int width,
@ -38,7 +48,7 @@ namespace DtronixPdf
ThreadDispatcher dispatcher,
PixelFormat format,
float scale,
RectangleF viewport)
Viewport viewport)
{
_pdfBitmap = pdfBitmap;
_dispatcher = dispatcher;

Просмотреть файл

@ -51,7 +51,7 @@ namespace DtronixPdf
return page;
}
/*
public Task<PdfBitmap> Render(RenderFlags flags)
{
return Render(flags, 1);
@ -67,7 +67,7 @@ namespace DtronixPdf
{
return Render(flags, scale, viewport, false, Color.White);
}
public Task<PdfBitmap> Render(
RenderFlags flags,
float scale,
@ -76,19 +76,19 @@ namespace DtronixPdf
Color? backgroundColor)
{
var translatedRectangle = new RectangleF(
(int) ((Size.Width / 2 - viewport.Size.Width / 2 + viewport.Center.X) * scale + viewport.Size.Width / 2 * (scale - 1)),
(int) ((Size.Height / 2 - viewport.Size.Height / 2 - viewport.Center.Y) * scale + viewport.Size.Height / 2 * (scale - 1)),
(int) ((Size.Width / 2 - viewport.Size.Width / 2 + viewport.Origin.X) * scale + viewport.Size.Width / 2 * (scale - 1)),
(int) ((Size.Height / 2 - viewport.Size.Height / 2 - viewport.Origin.Y) * scale + viewport.Size.Height / 2 * (scale - 1)),
viewport.Size.Width,
viewport.Size.Height);
return Render(flags, scale, translatedRectangle, alpha, backgroundColor);
}
*/
public async Task<PdfBitmap> Render(
RenderFlags flags,
float scale,
RectangleF viewport,
Viewport viewport,
bool alpha,
Color? backgroundColor)
{

Просмотреть файл

@ -1,25 +1,25 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Drawing;
namespace DtronixPdf
{
public struct Viewport
public readonly struct Viewport
{
public readonly PointF Center;
public readonly SizeF Size;
public ViewportOrigin OriginLocation { get; }
public PointF Origin { get; }
public SizeF Size { get; }
public Viewport(PointF center, SizeF size)
public Viewport(PointF origin, SizeF size, ViewportOrigin originLocation)
{
Center = center;
OriginLocation = originLocation;
Origin = origin;
Size = size;
}
public Viewport(int x, int y, int width, int height)
public Viewport(int x, int y, int width, int height, ViewportOrigin originLocation)
{
Center = new Point(x, y);
OriginLocation = originLocation;
Origin = new Point(x, y);
Size = new Size(width, height);
}
}
}
}

Просмотреть файл

@ -0,0 +1,12 @@
namespace DtronixPdf
{
public enum ViewportOrigin
{
Unset,
TopLeft,
TopRight,
BottomRight,
BottomLeft,
Center
}
}