Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ObjectElement and ICanvas support for it #80

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions QuestPDF/Drawing/FreeCanvas.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ public void Scale(float scaleX, float scaleY)

}

public void DrawObject(object Object, Position position, Size size)
{

}



#endregion
}
}
5 changes: 5 additions & 0 deletions QuestPDF/Drawing/SkiaCanvasBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,10 @@ public void Scale(float scaleX, float scaleY)
{
Canvas.Scale(scaleX, scaleY);
}

public void DrawObject(object Object, Position position, Size size)
{
throw new System.NotImplementedException($"{nameof(SkiaCanvasBase)} does not currently implement drawing for ObjectElement");
}
}
}
25 changes: 25 additions & 0 deletions QuestPDF/Elements/ObjectElement.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using QuestPDF.Drawing;
using QuestPDF.Infrastructure;
using SkiaSharp;

namespace QuestPDF.Elements
{
internal class ObjectElement : Element, ICacheable
{
public object? Object { get; set; }
public AspectRatioOption SpaceFitBehavior { get; set; }

internal override SpacePlan Measure(Size availableSpace)
{
return SpacePlan.FullRender(availableSpace);
}

internal override void Draw(Size availableSpace)
{
if (Object == null)
return;

Canvas.DrawObject(Object, Position.Zero, availableSpace);
}
}
}
25 changes: 25 additions & 0 deletions QuestPDF/Fluent/ObjectElementExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.IO;
using QuestPDF.Drawing.Exceptions;
using QuestPDF.Elements;
using QuestPDF.Infrastructure;
using SkiaSharp;

namespace QuestPDF.Fluent
{
public static class ObjectElementExtensions
{
/// <summary>
/// Add an inspecific object. To use this feature, your ICanvas must implement DrawObject().
/// </summary>
public static void Object(this IContainer parent, object Object, AspectRatioOption fitSpaceOption = AspectRatioOption.FitWidth)
{
parent.Element(
new ObjectElement()
{
Object = Object,
SpaceFitBehavior = fitSpaceOption
});
}
}
}
4 changes: 3 additions & 1 deletion QuestPDF/Infrastructure/ICanvas.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
using SkiaSharp;
using System;

namespace QuestPDF.Infrastructure
{
internal interface ICanvas
public interface ICanvas
{
void Translate(Position vector);

void DrawRectangle(Position vector, Size size, string color);
void DrawText(string text, Position position, TextStyle style);
void DrawImage(SKImage image, Position position, Size size);
void DrawObject(object Object, Position position, Size size);

void DrawExternalLink(string url, Size size);
void DrawLocationLink(string locationName, Size size);
Expand Down
2 changes: 1 addition & 1 deletion QuestPDF/Infrastructure/Position.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace QuestPDF.Infrastructure
{
internal readonly struct Position
public readonly struct Position
{
public readonly float X;
public readonly float Y;
Expand Down