Skip to content

Commit

Permalink
Add Actual-/PageSize to IDeferredCanvas and housekeeping
Browse files Browse the repository at this point in the history
This is actually parts of a different feature coming up.
Figured it might be useful for someone, so i left these parts in.
  • Loading branch information
X39 committed Oct 31, 2024
1 parent 86316b2 commit a283b7a
Show file tree
Hide file tree
Showing 19 changed files with 364 additions and 188 deletions.
47 changes: 38 additions & 9 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@

[*]

# ReSharper properties
resharper_align_multiline_binary_patterns = true
resharper_align_multiline_calls_chain = true
resharper_braces_for_fixed = required_for_multiline
resharper_braces_for_ifelse = not_required_for_both
resharper_braces_for_lock = required_for_multiline
resharper_braces_for_using = required_for_multiline
resharper_builtin_type_apply_to_native_integer = true
resharper_csharp_align_multiline_calls_chain = true
resharper_csharp_align_multiline_calls_chain = false
resharper_csharp_allow_alias = false
resharper_csharp_empty_block_style = together_same_line
resharper_csharp_max_line_length = 120
resharper_csharp_place_type_constraints_on_same_line = false
resharper_csharp_stick_comment = false
resharper_csharp_wrap_before_binary_opsign = true
resharper_csharp_wrap_before_declaration_rpar = true
resharper_csharp_wrap_before_first_type_parameter_constraint = true
resharper_csharp_wrap_before_invocation_rpar = true
resharper_csharp_wrap_extends_list_style = chop_if_long
resharper_csharp_wrap_ternary_expr_style = chop_if_long
resharper_indent_invocation_pars = inside
resharper_indent_preprocessor_if = usual_indent
resharper_indent_preprocessor_other = usual_indent
resharper_initializer_braces = next_line
resharper_int_align_assignments = true
resharper_int_align_switch_expressions = true
Expand All @@ -23,33 +33,48 @@ resharper_csharp_wrap_after_invocation_lpar = true
resharper_csharp_wrap_arguments_style = chop_if_long
resharper_csharp_wrap_parameters_style = chop_if_long
resharper_instance_members_qualify_declared_in = base_class
resharper_force_attribute_style = join
resharper_force_attribute_style = separate
resharper_keep_existing_arrangement = false
resharper_local_function_body = expression_body
resharper_max_attribute_length_for_same_line = 120
resharper_max_enum_members_on_line = 1
resharper_max_initializer_elements_on_line = 10000
resharper_nested_ternary_style = expanded
resharper_outdent_statement_labels = true
resharper_parentheses_redundancy_style = remove_if_not_clarifies_precedence
resharper_place_accessorholder_attribute_on_same_line = false
resharper_place_accessor_attribute_on_same_line = true
resharper_place_constructor_initializer_on_same_line = false
resharper_place_expr_accessor_on_single_line = true
resharper_place_expr_method_on_single_line = true
resharper_place_expr_property_on_single_line = true
resharper_place_expr_accessor_on_single_line = if_owner_is_single_line
resharper_place_expr_method_on_single_line = false
resharper_place_expr_property_on_single_line = if_owner_is_single_line
resharper_place_field_attribute_on_same_line = false
resharper_place_record_field_attribute_on_same_line = true
resharper_place_linq_into_on_new_line = false
resharper_place_record_field_attribute_on_same_line = if_owner_is_single_line
resharper_place_simple_embedded_block_on_same_line = false
resharper_place_simple_embedded_statement_on_same_line = false
resharper_place_simple_initializer_on_single_line = true
resharper_place_simple_switch_expression_on_single_line = true
resharper_place_type_attribute_on_same_line = false
resharper_space_between_attribute_sections = false
resharper_space_within_single_line_array_initializer_braces = true
resharper_trailing_comma_in_multiline_lists = true
resharper_trailing_comma_in_singleline_lists = false
resharper_wrap_after_property_in_chained_method_calls = true
resharper_wrap_before_arrow_with_expressions = true
resharper_wrap_before_extends_colon = true
resharper_wrap_before_first_method_call = false
resharper_wrap_before_linq_expression = true
resharper_wrap_before_primary_constructor_declaration_rpar = false
resharper_wrap_chained_method_calls = chop_if_long
resharper_wrap_before_primary_constructor_declaration_lpar = false
resharper_wrap_before_primary_constructor_declaration_rpar = true
resharper_wrap_chained_binary_expressions = chop_if_long
resharper_wrap_chained_binary_patterns = chop_if_long
resharper_wrap_chained_method_calls = chop_always
resharper_wrap_list_pattern = chop_if_long
resharper_wrap_property_pattern = wrap_if_long
resharper_wrap_ternary_expr_style = chop_if_long
resharper_xmldoc_linebreaks_inside_tags_for_elements_longer_than = 120
resharper_wrap_multiple_type_parameter_constraints_style = chop_always

# Microsoft .NET properties
csharp_indent_braces = false
Expand All @@ -59,4 +84,8 @@ dotnet_style_predefined_type_for_locals_parameters_members = true:suggestion
dotnet_style_qualification_for_event = false:suggestion
dotnet_style_qualification_for_field = false:suggestion
dotnet_style_qualification_for_method = false:suggestion
dotnet_style_qualification_for_property = false:suggestion
dotnet_style_qualification_for_property = false:suggestion

# ReSharper inspection severities
resharper_arrange_trailing_comma_in_multiline_lists_highlighting = suggestion
resharper_suggest_discard_declaration_var_style_highlighting = suggestion
2 changes: 1 addition & 1 deletion source/X39.Solutions.PdfTemplate/Abstraction/Canvas.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static class Canvas
/// When the balancing call is done by disposing the return value,
/// the previous matrix, clipping, and drawing filters are restored.
/// </summary>
public static IDisposable CreateState(this IDeferredCanvas canvas)
public static IDisposable CreateState(this IDrawableCanvas canvas)
{
canvas.PushState();
return new Disposable(canvas.PopState);
Expand Down
17 changes: 12 additions & 5 deletions source/X39.Solutions.PdfTemplate/Abstraction/CanvasExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,33 @@
namespace X39.Solutions.PdfTemplate.Abstraction;

/// <summary>
/// Contains extension methods for <see cref="IDeferredCanvas"/>.
/// Contains extension methods for <see cref="IDrawableCanvas"/>.
/// </summary>
public static class CanvasExtensions
{
/// <inheritdoc cref="IDrawableCanvas.Translate"/>
/// <param name="self">The <see cref="IDeferredCanvas"/> to operate on.</param>
/// <param name="self">The <see cref="IDrawableCanvas"/> to operate on.</param>
/// <param name="x">The distance to translate along the x-axis.</param>
/// <param name="y">The distance to translate along the y-axis.</param>
public static void Translate(this IDeferredCanvas self, float x, float y)
public static void Translate(this IDrawableCanvas self, float x, float y)
{
self.Translate(new Point(x, y));
}
/// <inheritdoc cref="IDrawableCanvas.Translate"/>
/// <param name="self">The <see cref="IDrawableCanvas"/> to operate on.</param>
/// <param name="size">The distance to translate.</param>
public static void Translate(this IDrawableCanvas self, Size size)
{
self.Translate(new Point(size.Width, size.Height));
}

/// <inheritdoc cref="IDrawableCanvas.Clip"/>
/// <param name="self">The <see cref="IDeferredCanvas"/> to operate on.</param>
/// <param name="self">The <see cref="IDrawableCanvas"/> to operate on.</param>
/// <param name="x">The x-coordinate of the rectangle.</param>
/// <param name="y">The y-coordinate of the rectangle.</param>
/// <param name="width">The width of the rectangle.</param>
/// <param name="height">The height of the rectangle.</param>
public static void Clip(this IDeferredCanvas self, float x, float y, float width, float height)
public static void Clip(this IDrawableCanvas self, float x, float y, float width, float height)
{
self.Clip(new Rectangle(x, y, width, height));
}
Expand Down
13 changes: 13 additions & 0 deletions source/X39.Solutions.PdfTemplate/Abstraction/IDeferredCanvas.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,19 @@ namespace X39.Solutions.PdfTemplate.Abstraction;
/// </remarks>
public interface IDeferredCanvas : IDrawableCanvas
{
/// <summary>
/// The actual size of a single page without any margins applied.
/// </summary>
Size ActualPageSize { get; }

/// <summary>
/// The size of the page, including all margins.
/// </summary>
/// <remarks>
/// For foreground and background layer, this is the same as <see cref="ActualPageSize"/>.
/// </remarks>
Size PageSize { get; }

/// <summary>
/// Represents the current translation of the canvas.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public interface IDrawableCanvas
void PushState();

/// <summary>
/// Modify the current clip with the specified <see cref="Rectangle"/>.
/// Add to the current clip with the specified <see cref="Rectangle"/>.
/// </summary>
/// <param name="rectangle">The <see cref="Rectangle"/> to combine with the current clip.</param>
void Clip(Rectangle rectangle);
Expand Down
10 changes: 10 additions & 0 deletions source/X39.Solutions.PdfTemplate/Canvas/DeferredCanvasImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ internal sealed class DeferredCanvasImpl : IDeferredCanvas
private const int DefaultStackCapacity = 20 + 1;
private readonly Stack<Point> _stateStack = new(DefaultStackCapacity);
public Point Translation => _stateStack.Count is not 0 ? _stateStack.Peek() : new Point();
public Size ActualPageSize { get; set; }
public Size PageSize { get; set; }

public void Defer(Action<IImmediateCanvas> action)
{
Expand Down Expand Up @@ -39,11 +41,15 @@ public void Clip(Rectangle rectangle)

public void DrawRect(Rectangle rectangle, Color color)
{
if (color.Alpha is 0)
return;
_drawActions.Add((canvas) => canvas.DrawRect(rectangle, color));
}

public void Translate(Point point)
{
if (point is { X: 0, Y: 0 })
return;
var translation = Translation + point;
_stateStack.Pop();
_stateStack.Push(translation);
Expand All @@ -58,11 +64,15 @@ public void PopState()

public void DrawLine(Color color, float thickness, float startX, float startY, float endX, float endY)
{
if (color.Alpha is 0)
return;
_drawActions.Add((canvas) =>canvas.DrawLine(color, thickness, startX, startY, endX, endY));
}

public void DrawText(TextStyle textStyle, float dpi, string text, float x, float y)
{
if (text.IsNullOrWhiteSpace())
return;
_drawActions.Add((canvas) => canvas.DrawText(textStyle, dpi, text, x, y));
}

Expand Down
24 changes: 17 additions & 7 deletions source/X39.Solutions.PdfTemplate/Canvas/ImmediateCanvasImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,29 @@ public ImmediateCanvasImpl(SKCanvas canvas, SkPaintCache paintCache)
{
_canvas = canvas;
_paintCache = paintCache;
_canvas.Clear();
}

public void PushState() => _canvas.Save();
public void Clip(Rectangle rectangle) => _canvas.ClipRect(rectangle);
public void PopState() => _canvas.Restore();
public void PushState()
=> _canvas.Save();

public void Clip(Rectangle rectangle)
=> _canvas.ClipRect(rectangle);

public void PopState()
=> _canvas.Restore();

public void DrawLine(Color color, float thickness, float startX, float startY, float endX, float endY)
=> _canvas.DrawLine(startX, startY, endX, endY, _paintCache.Get(color, thickness));

public void Translate(Point point) => _canvas.Translate(point.X, point.Y);
public void Translate(Point point)
=> _canvas.Translate(point.X, point.Y);

public void DrawText(TextStyle textStyle, float dpi, string text, float x, float y)
=> _canvas.DrawText(text, x, y, _paintCache.Get(textStyle, dpi));

public void DrawRect(Rectangle rectangle, Color color) => _canvas.DrawRect(rectangle, _paintCache.Get(color));
public void DrawRect(Rectangle rectangle, Color color)
=> _canvas.DrawRect(rectangle, _paintCache.Get(color));

public void DrawBitmap(byte[] bytes, Rectangle rectangle)
{
Expand All @@ -39,7 +47,9 @@ public void DrawBitmap(byte[] bytes, Rectangle rectangle)
_canvas.DrawBitmap(bitmap, rectangle);
}

public void DrawBitmap(SKBitmap bitmap, Rectangle arrangementInner) => _canvas.DrawBitmap(bitmap, arrangementInner);
public void DrawBitmap(SKBitmap bitmap, Rectangle arrangementInner)
=> _canvas.DrawBitmap(bitmap, arrangementInner);

public ushort PageNumber { get; internal set; }
public ushort TotalPages { get; internal set; }
}
}
2 changes: 1 addition & 1 deletion source/X39.Solutions.PdfTemplate/Controls/BorderControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class BorderControl : AlignableContentControl
[Parameter]
public Color Background { get; set; }

private List<Size> _arrangedSizes = new();
private readonly List<Size> _arrangedSizes = new();

/// <inheritdoc />
protected override Size DoMeasure(
Expand Down
44 changes: 26 additions & 18 deletions source/X39.Solutions.PdfTemplate/Data/Size.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,20 @@ namespace X39.Solutions.PdfTemplate.Data;
/// </summary>
/// <param name="Width">The width of the size</param>
/// <param name="Height">The height of the size</param>
public readonly record struct Size(float Width, float Height)
: IAdditionOperators<Size, Size, Size>,
IAdditiveIdentity<Size, Size>,
IDecrementOperators<Size>,
IDivisionOperators<Size, Size, Size>,
IEqualityOperators<Size, Size, bool>,
IIncrementOperators<Size>,
IMultiplicativeIdentity<Size, Size>,
IMultiplyOperators<Size, Size, Size>,
ISubtractionOperators<Size, Size, Size>,
IUnaryPlusOperators<Size, Size>,
IUnaryNegationOperators<Size, Size>,
IMinMaxValue<Size>
public readonly record struct Size(float Width, float Height) : IAdditionOperators<Size, Size, Size>,
IAdditiveIdentity<Size, Size>,
IDecrementOperators<Size>,
IDivisionOperators<Size, Size, Size>,
IDivisionOperators<Size, float, Size>,
IEqualityOperators<Size, Size, bool>,
IIncrementOperators<Size>,
IMultiplicativeIdentity<Size, Size>,
IMultiplyOperators<Size, Size, Size>,
IMultiplyOperators<Size, float, Size>,
ISubtractionOperators<Size, Size, Size>,
IUnaryPlusOperators<Size, Size>,
IUnaryNegationOperators<Size, Size>,
IMinMaxValue<Size>
{
/// <inheritdoc />
public static Size operator +(Size left, Size right)
Expand All @@ -38,34 +39,41 @@ public readonly record struct Size(float Width, float Height)

/// <inheritdoc />
public static Size operator ++(Size value)
=> new( value.Width + 1, value.Height + 1);
=> new(value.Width + 1, value.Height + 1);

/// <inheritdoc />
public static Size MultiplicativeIdentity => new(1, 1);

/// <inheritdoc />
public static Size operator *(Size left, Size right)
=> new( left.Width * right.Width, left.Height * right.Height);
=> new(left.Width * right.Width, left.Height * right.Height);

/// <inheritdoc />
public static Size operator -(Size left, Size right)
=> new( left.Width - right.Width, left.Height - right.Height);
=> new(left.Width - right.Width, left.Height - right.Height);

/// <inheritdoc />
public static Size operator +(Size value)
=> new( +value.Width, +value.Height);
=> new(+value.Width, +value.Height);

/// <inheritdoc />
public static Size operator -(Size value)
=> new( -value.Width, -value.Height);
=> new(-value.Width, -value.Height);

/// <inheritdoc />
public static Size MaxValue => new(float.MaxValue, float.MaxValue);

/// <inheritdoc />
public static Size MinValue => new(float.MinValue, float.MinValue);

/// <summary>
/// A size that has a width and height of 0.
/// </summary>
public static Size Zero => new(0, 0);

/// <inheritdoc />
public static Size operator /(Size left, float right) => new Size(left.Width / right, left.Height / right);

/// <inheritdoc />
public static Size operator *(Size left, float right) => new Size(left.Width * right, left.Height * right);
}
Loading

0 comments on commit a283b7a

Please sign in to comment.