Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions Pinta.Core/Classes/Document.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
//
//
// Document.cs
//
//
// Author:
// Jonathan Pobst <[email protected]>
//
//
// Copyright (c) 2010 Jonathan Pobst
//
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Expand Down Expand Up @@ -76,8 +76,8 @@ public Document (
{
// --- Mandatory fields

PreviousSelection = new (this);
Selection = new DocumentSelection (this);
PreviousSelection = new ();
Selection = new DocumentSelection ();

Layers = new DocumentLayers (tools, this);
Workspace = new DocumentWorkspace (actions, tools, this);
Expand All @@ -89,7 +89,7 @@ public Document (
this.tools = tools;
this.workspace = workspace;

// --- Post-initialization
// --- Post-initialization

if (file is not null) {

Expand Down Expand Up @@ -285,7 +285,7 @@ public ColorBgra GetComputedPixel (PointI position)
/// <param name="canvasOnly">false for the whole selection, true for the part only on our canvas</param>
public RectangleI GetSelectedBounds (bool canvasOnly)
{
RectangleI bounds = Selection.SelectionPath.GetBounds ();
RectangleI bounds = Selection.GetBounds ().ToInt ();

if (canvasOnly)
bounds = ClampToImageSize (bounds);
Expand Down
19 changes: 8 additions & 11 deletions Pinta.Core/Classes/DocumentSelection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,15 @@

using System;
using System.Collections.Generic;
using System.Linq;
using Cairo;
using ClipperLib;

namespace Pinta.Core;

public sealed class DocumentSelection
{
private readonly Document owning_document;
internal DocumentSelection (Document owningDocument)
internal DocumentSelection ()
{
this.owning_document = owningDocument;
}

private Path? selection_path;
Expand All @@ -62,7 +59,7 @@ public bool Visible {
public Path SelectionPath {
get {
if (selection_path == null) {
using Context g = new (owning_document.Layers.CurrentUserLayer.Surface);
using Context g = CairoExtensions.CreatePathContext ();
selection_path = g.CreatePolygonPath (ConvertToPolygonSet (SelectionPolygons));
}

Expand Down Expand Up @@ -95,7 +92,7 @@ public void Clip (Context g)
/// </summary>
public DocumentSelection Clone ()
{
return new (owning_document) {
return new () {
SelectionPolygons = [.. SelectionPolygons],
Origin = new PointD (Origin.X, Origin.Y),
End = new PointD (End.X, End.Y),
Expand Down Expand Up @@ -126,7 +123,7 @@ public static List<List<IntPoint>> ConvertToPolygons (IReadOnlyList<IReadOnlyLis
/// </summary>
/// <param name="clipperPolygons">A Clipper Polygon collection.</param>
/// <returns>A Pinta Polygon set.</returns>
public static IReadOnlyList<IReadOnlyList<PointI>> ConvertToPolygonSet (IReadOnlyList<IReadOnlyList<IntPoint>> clipperPolygons)
private static IReadOnlyList<IReadOnlyList<PointI>> ConvertToPolygonSet (IReadOnlyList<IReadOnlyList<IntPoint>> clipperPolygons)
{
var resultingPolygonSet = new PointI[clipperPolygons.Count][];

Expand Down Expand Up @@ -174,7 +171,7 @@ public DocumentSelection Transform (Matrix transform)
transform.TransformPoint (ref origin);
transform.TransformPoint (ref end);

return new (owning_document) {
return new () {
SelectionPolygons = newPolygons,
Origin = origin,
End = end,
Expand Down Expand Up @@ -330,7 +327,7 @@ public void CreateRectangleSelection (RectangleD r)
/// <param name='imageSize'>
/// The size of the document.
/// </param>
public void Invert (Core.Size imageSize)
public void Invert (Size imageSize)
{
List<List<IntPoint>> resultingPolygons = [];

Expand Down Expand Up @@ -419,8 +416,8 @@ public RectangleD GetBounds ()
double maxY = double.MinValue;

// Calculate the minimum rectangular bounds that surround the current selection.
foreach (var li in SelectionPolygons) {
foreach (var ip in li) {
foreach (List<IntPoint> li in SelectionPolygons) {
foreach (IntPoint ip in li) {
minX = Math.Min (minX, ip.X);
minY = Math.Min (minY, ip.Y);
maxX = Math.Max (maxX, ip.X);
Expand Down
17 changes: 12 additions & 5 deletions Pinta.Core/Extensions/Cairo/CairoExtensions.Core.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,19 @@ public static ImageSurface Clone (this ImageSurface surf)
return newsurf;
}

public static Path Clone (this Path path)
/// <summary>
/// Placeholder surface used for <see cref="CreatePathContext"/>
/// Another option would be to use a different surface type, like
/// cairo_recording_surface_create(), but an empty image works okay.
/// </summary>
private static readonly Lazy<ImageSurface> empty_surface = new (() => CreateImageSurface (Format.Argb32, 0, 0));

/// <summary>
/// Creates a Cairo context without a backing image surface, which can only be used for building a Cairo.Path.
/// </summary>
public static Context CreatePathContext ()
{
Document doc = PintaCore.Workspace.ActiveDocument;
using Context g = new (doc.Layers.CurrentUserLayer.Surface);
g.AppendPath (path);
return g.CopyPath ();
return new Context (empty_surface.Value);
}

/// <summary>
Expand Down
8 changes: 0 additions & 8 deletions Pinta.Core/Extensions/Cairo/CairoExtensions.Geometry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,6 @@ public static RectangleD PathExtents (this Context context)
y2 - y1);
}

public static RectangleI GetBounds (this Path path)
{
Document doc = PintaCore.Workspace.ActiveDocument;
using Context g = new (doc.Layers.CurrentUserLayer.Surface);
g.AppendPath (path);
return g.PathExtents ().ToInt ();
}

public static RectangleI GetBounds (this ImageSurface surf)
=> new (0, 0, surf.Width, surf.Height);

Expand Down
3 changes: 1 addition & 2 deletions Pinta.Core/Managers/LivePreviewManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,7 @@ public async void Start (BaseEffect effect)
workspace.ImageSize.Width,
workspace.ImageSize.Height);

Cairo.Path? selectionPath = selection.Visible ? selection.SelectionPath : null;
RenderBounds = (selectionPath != null) ? selectionPath.GetBounds () : LivePreviewSurface.GetBounds ();
RenderBounds = selection.Visible ? selection.GetBounds ().ToInt () : LivePreviewSurface.GetBounds ();
RenderBounds = workspace.ClampToImageSize (RenderBounds);

const uint UPDATE_MILLISECONDS = 100;
Expand Down
2 changes: 1 addition & 1 deletion Pinta.Effects/Dialogs/Effects.LevelsDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ private void UpdateInputHistogram ()
Document doc = workspace.ActiveDocument;

ImageSurface surface = doc.Layers.CurrentUserLayer.Surface;
RectangleI rect = doc.Selection.SelectionPath.GetBounds ();
RectangleI rect = doc.Selection.GetBounds ().ToInt ();
histogram_input.Histogram.UpdateHistogram (surface, rect);
UpdateOutputHistogram ();
}
Expand Down
14 changes: 7 additions & 7 deletions Pinta.Tools/Tools/MoveSelectedTool.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
//
//
// MoveSelectedTool.cs
//
//
// Author:
// Jonathan Pobst <[email protected]>
//
//
// Copyright (c) 2010 Jonathan Pobst
//
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Expand Down Expand Up @@ -58,7 +58,7 @@ public MoveSelectedTool (IServiceProvider services) : base (services)
public override int Priority => 5;

protected override RectangleD GetSourceRectangle (Document document)
=> document.Selection.SelectionPath.GetBounds ().ToDouble ();
=> document.Selection.GetBounds ();

protected override void OnStartTransform (Document document)
{
Expand Down
2 changes: 1 addition & 1 deletion Pinta.Tools/Tools/MoveSelectionTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public MoveSelectionTool (IServiceProvider services) : base (services)
public override bool IsSelectionTool => true;

protected override RectangleD GetSourceRectangle (Document document)
=> document.Selection.SelectionPath.GetBounds ().ToDouble ();
=> document.Selection.GetBounds ();

protected override void OnStartTransform (Document document)
{
Expand Down
2 changes: 1 addition & 1 deletion Pinta/MainWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ private bool HandleDrop (Gtk.DropTarget sender, Gtk.DropTarget.DropSignalArgs ar

private void ZoomToSelection_Activated (object sender, EventArgs e)
{
PintaCore.Workspace.ActiveWorkspace.ZoomToCanvasRectangle (PintaCore.Workspace.ActiveDocument.Selection.SelectionPath.GetBounds ().ToDouble ());
PintaCore.Workspace.ActiveWorkspace.ZoomToCanvasRectangle (PintaCore.Workspace.ActiveDocument.Selection.GetBounds ());
}

private void ZoomToWindow_Activated (object sender, EventArgs e)
Expand Down
Loading