Skip to content

Commit

Permalink
adding points (pt) to Length
Browse files Browse the repository at this point in the history
[major][add]
With dpi properly taken into account for text now, lines (and other things relying on length) now also required a way to properly define the size in points (aka: in relation to DPI). This is now concluded with Length having support for `pt` size (eg. 72pt ~ 1inch).
  • Loading branch information
X39 committed Nov 25, 2023
1 parent 2967102 commit ec2c8d4
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 35 deletions.
12 changes: 6 additions & 6 deletions source/X39.Solutions.PdfTemplate/Controls/Base/Control.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,8 @@ public virtual Size Measure(
in Size remainingSize,
CultureInfo cultureInfo)
{
var padding = Padding.ToRectangle(fullPageSize);
var margin = Margin.ToRectangle(fullPageSize);
var padding = Padding.ToRectangle(fullPageSize, dpi);
var margin = Margin.ToRectangle(fullPageSize, dpi);
var measureResult = DoMeasure(
dpi,
ToSize(fullPageSize, margin),
Expand Down Expand Up @@ -219,8 +219,8 @@ public virtual Size Arrange(
{
FramedSize = framedPageSize;
RemainingSize = framedPageSize;
var padding = Padding.ToRectangle(remainingSize);
var margin = Margin.ToRectangle(remainingSize);
var padding = Padding.ToRectangle(remainingSize, dpi);
var margin = Margin.ToRectangle(remainingSize, dpi);
var measureResult = DoArrange(
dpi,
ToSize(fullPageSize, margin),
Expand Down Expand Up @@ -260,8 +260,8 @@ public virtual void Render(
in Size parentSize,
CultureInfo cultureInfo)
{
var padding = Padding.ToRectangle(parentSize);
var margin = Margin.ToRectangle(parentSize);
var padding = Padding.ToRectangle(parentSize, dpi);
var margin = Margin.ToRectangle(parentSize, dpi);
canvas.PushState();
try
{
Expand Down
6 changes: 3 additions & 3 deletions source/X39.Solutions.PdfTemplate/Controls/BorderControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ protected override Size DoMeasure(
in Size remainingSize,
CultureInfo cultureInfo)
{
var thickness = Thickness.ToRectangle(fullPageSize);
var thickness = Thickness.ToRectangle(fullPageSize, dpi);
var size = Size.Zero;
foreach (var child in Children)
{
Expand All @@ -65,7 +65,7 @@ protected override Size DoArrange(
in Size remainingSize,
CultureInfo cultureInfo)
{
var thickness = Thickness.ToRectangle(fullPageSize);
var thickness = Thickness.ToRectangle(fullPageSize, dpi);
var size = Size.Zero;
foreach (var child in Children)
{
Expand All @@ -89,7 +89,7 @@ protected override void DoRender(ICanvas canvas, float dpi, in Size parentSize,
using var state = canvas.CreateState();
canvas.Translate(-ArrangementInner);
canvas.Translate(Arrangement);
var thickness = Thickness.ToRectangle(parentSize);
var thickness = Thickness.ToRectangle(parentSize, dpi);
if (Background != Colors.Transparent)
canvas.DrawRect(Arrangement with {Left = 0, Top = 0}, Background);
if (thickness.Left > 0)
Expand Down
24 changes: 13 additions & 11 deletions source/X39.Solutions.PdfTemplate/Controls/LineControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public sealed class LineControl : AlignableControl

/// <inheritdoc />
protected override Size DoMeasure(
float pageSize,
float dpi,
in Size fullPageSize,
in Size framedPageSize,
in Size remainingSize,
Expand All @@ -48,11 +48,11 @@ protected override Size DoMeasure(
return Orientation switch
{
EOrientation.Horizontal => new Size(
Length.ToPixels(remainingSize.Width),
Thickness.ToPixels(remainingSize.Height)),
Length.ToPixels(remainingSize.Width, dpi),
Thickness.ToPixels(remainingSize.Height, dpi)),
EOrientation.Vertical => new Size(
Thickness.ToPixels(remainingSize.Width),
Length.ToPixels(remainingSize.Height)),
Thickness.ToPixels(remainingSize.Width, dpi),
Length.ToPixels(remainingSize.Height, dpi)),
_ => throw new InvalidEnumArgumentException(nameof(Orientation), (int) Orientation, typeof(EOrientation)),
};
}
Expand All @@ -68,11 +68,11 @@ protected override Size DoArrange(
return Orientation switch
{
EOrientation.Horizontal => new Size(
Length.ToPixels(remainingSize.Width),
Thickness.ToPixels(remainingSize.Height)),
Length.ToPixels(remainingSize.Width, dpi),
Thickness.ToPixels(remainingSize.Height, dpi)),
EOrientation.Vertical => new Size(
Thickness.ToPixels(remainingSize.Width),
Length.ToPixels(remainingSize.Height)),
Thickness.ToPixels(remainingSize.Width, dpi),
Length.ToPixels(remainingSize.Height, dpi)),
_ => throw new InvalidEnumArgumentException(nameof(Orientation), (int) Orientation, typeof(EOrientation)),
};
}
Expand All @@ -88,11 +88,13 @@ protected override void DoRender(
var length = Length.ToPixels(
Orientation is EOrientation.Horizontal
? parentSize.Width
: parentSize.Height);
: parentSize.Height,
dpi);
var thickness = Thickness.ToPixels(
Orientation is EOrientation.Horizontal
? parentSize.Height
: parentSize.Width);
: parentSize.Width,
dpi);
switch (Orientation)
{
case EOrientation.Horizontal:
Expand Down
8 changes: 8 additions & 0 deletions source/X39.Solutions.PdfTemplate/Data/ELengthMode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,12 @@ public enum ELengthMode
/// The size is in percent of the available space.
/// </summary>
Percent,

/// <summary>
/// The size is in points.
/// </summary>
/// <remarks>
/// 1 point = 1/72.272 inch
/// </remarks>
Points,
}
7 changes: 6 additions & 1 deletion source/X39.Solutions.PdfTemplate/Data/Length.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,16 @@ public void Deconstruct(out float value, out ELengthMode lengthMode)
/// Translates the <see cref="Length"/> into a <see cref="float"/> based on the given bounds and <see cref="LengthMode"/>.
/// </summary>
/// <param name="bounds">The bounds to use for the calculation in case of <see cref="ELengthMode.Percent"/> in pixels</param>
/// <param name="dpi">The DPI to use for the calculation in case of <see cref="ELengthMode.Points"/> in pixels</param>
/// <returns>The pixel value of the <see cref="Length"/></returns>
/// <exception cref="ArgumentOutOfRangeException">Thrown when <see cref="LengthMode"/> is not a valid <see cref="ELengthMode"/></exception>
public float ToPixels(float bounds)
public float ToPixels(float bounds, float dpi)
{
return LengthMode switch
{
ELengthMode.Pixel => Value,
ELengthMode.Percent => Value * bounds,
ELengthMode.Points => Value * dpi / 72.272F,
_ => throw new InvalidEnumArgumentException(nameof(LengthMode), (int)LengthMode, typeof(ELengthMode)),
};
}
Expand Down Expand Up @@ -91,6 +93,7 @@ public static Length Parse(ReadOnlySpan<char> s, IFormatProvider? provider)
"" => ELengthMode.Pixel,
"px" => ELengthMode.Pixel,
"%" => ELengthMode.Percent,
"pt" => ELengthMode.Points,
_ => throw new NotSupportedException($"The unit '{unit}' is not supported.")
};
if (sizeMode is ELengthMode.Percent)
Expand All @@ -115,6 +118,7 @@ public static bool TryParse(ReadOnlySpan<char> s, IFormatProvider? provider, out
"" => ELengthMode.Pixel,
"px" => ELengthMode.Pixel,
"%" => ELengthMode.Percent,
"pt" => ELengthMode.Points,
_ => default(ELengthMode?),
};
if (sizeMode is null)
Expand All @@ -141,6 +145,7 @@ public string ToString(IFormatProvider? provider)
{
ELengthMode.Pixel => "px",
ELengthMode.Percent => "%",
ELengthMode.Points => "pt",
_ => throw new NotSupportedException($"The size mode '{sizeMode}' is not supported."),
};
return string.Concat(sizeValue.ToString(provider), sizeUnit);
Expand Down
22 changes: 12 additions & 10 deletions source/X39.Solutions.PdfTemplate/Data/Thickness.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,14 @@ public Thickness(Length horizontal, Length vertical) : this(horizontal, vertical
/// Translates the thickness to a pixels rectangle.
/// </summary>
/// <param name="bounds">The bounds of the rectangle</param>
/// <param name="dpi">The DPI to use for the calculation in case of <see cref="ELengthMode.Points"/> in pixels</param>
/// <returns>The translated rectangle</returns>
public Rectangle ToRectangle(Rectangle bounds)
public Rectangle ToRectangle(Rectangle bounds, float dpi)
{
var left = Left.ToPixels(bounds.Width);
var top = Top.ToPixels(bounds.Height);
var right = Right.ToPixels(bounds.Width);
var bottom = Bottom.ToPixels(bounds.Height);
var left = Left.ToPixels(bounds.Width, dpi);
var top = Top.ToPixels(bounds.Height, dpi);
var right = Right.ToPixels(bounds.Width, dpi);
var bottom = Bottom.ToPixels(bounds.Height, dpi);
return new Rectangle(
left,
top,
Expand All @@ -52,13 +53,14 @@ public Rectangle ToRectangle(Rectangle bounds)
/// Translates the thickness to a pixels rectangle.
/// </summary>
/// <param name="bounds">The bounds of the rectangle</param>
/// <param name="dpi">The DPI to use for the calculation in case of <see cref="ELengthMode.Points"/> in pixels</param>
/// <returns>The translated rectangle</returns>
public Rectangle ToRectangle(Size bounds)
public Rectangle ToRectangle(Size bounds, float dpi)
{
var left = Left.ToPixels(bounds.Width);
var top = Top.ToPixels(bounds.Height);
var width = Right.ToPixels(bounds.Width);
var height = Bottom.ToPixels(bounds.Height);
var left = Left.ToPixels(bounds.Width, dpi);
var top = Top.ToPixels(bounds.Height, dpi);
var width = Right.ToPixels(bounds.Width, dpi);
var height = Bottom.ToPixels(bounds.Height, dpi);
return new Rectangle(
left,
top,
Expand Down
12 changes: 8 additions & 4 deletions test/X39.Solutions.PdfTemplate.Test/Controls/LineControlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ public void DrawLineTests(
mock.AssertDrawLine(
Colors.Green,
lineControl.Thickness.ToPixels(
orientation == EOrientation.Horizontal ? pageBounds.Height : pageBounds.Width),
orientation == EOrientation.Horizontal ? pageBounds.Height : pageBounds.Width,
90),
expectedStartX,
expectedStartY,
expectedEndX,
Expand Down Expand Up @@ -111,7 +112,8 @@ public void PaddingIsApplied(EOrientation orientation)
lineControl.Thickness.ToPixels(
orientation == EOrientation.Horizontal
? pageBounds.Height
: pageBounds.Width),
: pageBounds.Width,
90),
orientation == EOrientation.Horizontal ? 10F : 10.5F,
orientation == EOrientation.Horizontal ? 10.5F : 10F,
orientation == EOrientation.Horizontal ? 990F : 10.5F,
Expand Down Expand Up @@ -152,7 +154,8 @@ public void MarginIsApplied(EOrientation orientation)
lineControl.Thickness.ToPixels(
orientation == EOrientation.Horizontal
? pageBounds.Height
: pageBounds.Width),
: pageBounds.Width,
90),
orientation == EOrientation.Horizontal ? 10F : 10.5F,
orientation == EOrientation.Horizontal ? 10.5F : 10F,
orientation == EOrientation.Horizontal ? 990F : 10.5F,
Expand Down Expand Up @@ -193,7 +196,8 @@ public void MarginAndPaddingBothAreApplied(EOrientation orientation)
lineControl.Thickness.ToPixels(
orientation == EOrientation.Horizontal
? pageBounds.Height
: pageBounds.Width),
: pageBounds.Width,
90),
orientation == EOrientation.Horizontal ? 20F : 20.5F,
orientation == EOrientation.Horizontal ? 20.5F : 20F,
orientation == EOrientation.Horizontal ? 980F : 20.5F,
Expand Down

0 comments on commit ec2c8d4

Please sign in to comment.