Added ability to render via viewport.

This commit is contained in:
DJGosnell 2020-10-02 18:20:48 -04:00
Родитель 0e91f64053
Коммит 7d899b101d
4 изменённых файлов: 57 добавлений и 12 удалений

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

@ -10,7 +10,7 @@ namespace DtronixPdf.Actions
{
public readonly FpdfPageT _pageInstance;
private readonly float _scale;
private readonly Rectangle _viewport;
private readonly RectangleF _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,
Rectangle viewport,
RectangleF viewport,
RenderFlags flags,
Color? backgroundColor,
bool includeAlpha)
@ -36,15 +36,17 @@ namespace DtronixPdf.Actions
protected override PdfBitmap OnExecute()
{
var bitmap = fpdfview.FPDFBitmapCreateEx(
_viewport.Width,
_viewport.Height,
(int)_viewport.Width,
(int)_viewport.Height,
(int) (_includeAlpha ? FPDFBitmapFormat.BGRA : FPDFBitmapFormat.BGR) ,
IntPtr.Zero,
0);
if(_backgroundColor.HasValue)
fpdfview.FPDFBitmapFillRect(bitmap, 0, 0, _viewport.Width, _viewport.Height, (uint) _backgroundColor.Value.ToArgb());
fpdfview.FPDFBitmapFillRect(
bitmap, 0, 0, (int)_viewport.Width, (int)_viewport.Height, (uint) _backgroundColor.Value.ToArgb());
if (bitmap == null)
throw new Exception("failed to create a bitmap object");
@ -73,8 +75,8 @@ namespace DtronixPdf.Actions
return new PdfBitmap(
bitmap,
_viewport.Width,
_viewport.Height,
(int)_viewport.Width,
(int)_viewport.Height,
_dispatcher,
_includeAlpha ? PixelFormat.Format32bppArgb : PixelFormat.Format24bppRgb,
_scale,

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

@ -27,7 +27,7 @@ namespace DtronixPdf
public float Scale { get; }
public Rectangle Viewport { get; }
public RectangleF Viewport { get; }
public bool IsDisposed { get; private set; }
@ -38,7 +38,7 @@ namespace DtronixPdf
ThreadDispatcher dispatcher,
PixelFormat format,
float scale,
Rectangle viewport)
RectangleF viewport)
{
_pdfBitmap = pdfBitmap;
_dispatcher = dispatcher;

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

@ -59,19 +59,36 @@ namespace DtronixPdf
public Task<PdfBitmap> Render(RenderFlags flags, float scale)
{
return Render(flags, scale, new Rectangle(0,0, (int) (Size.Width * scale),(int) (Size.Height * scale)));
return Render(flags, scale, new Rectangle(0, 0, (int) (Size.Width * scale), (int) (Size.Height * scale)));
}
public Task<PdfBitmap> Render(RenderFlags flags, float scale, Rectangle viewport)
public Task<PdfBitmap> Render(RenderFlags flags, float scale, RectangleF viewport)
{
return Render(flags, scale, viewport, false, Color.White);
}
public Task<PdfBitmap> Render(
RenderFlags flags,
float scale,
Viewport viewport,
bool alpha,
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)),
viewport.Size.Width,
viewport.Size.Height);
return Render(flags, scale, translatedRectangle, alpha, backgroundColor);
}
public async Task<PdfBitmap> Render(
RenderFlags flags,
float scale,
Rectangle viewport,
RectangleF viewport,
bool alpha,
Color? backgroundColor)
{
@ -79,6 +96,7 @@ namespace DtronixPdf
new RenderPageAction(_dispatcher, _pageInstance, scale, viewport, flags, backgroundColor, alpha));
}
public async ValueTask DisposeAsync()
{
await _dispatcher.QueueForCompletion(() =>

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

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