PIKApp/app/display/pikacanvaspath.c

360 lines
11 KiB
C

/* PIKA - Photo and Image Kooker Application
* a rebranding of The GNU Image Manipulation Program (created with heckimp)
* A derived work which may be trivial. However, any changes may be (C)2023 by Aldercone Studio
*
* Original copyright, applying to most contents (license remains unchanged):
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
*
* pikacanvaspath.c
* Copyright (C) 2010 Michael Natterer <mitch@gimp.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <gegl.h>
#include <gtk/gtk.h>
#include "libpikabase/pikabase.h"
#include "libpikamath/pikamath.h"
#include "display-types.h"
#include "core/pikabezierdesc.h"
#include "core/pikaparamspecs.h"
#include "pikacanvas-style.h"
#include "pikacanvaspath.h"
#include "pikadisplayshell.h"
enum
{
PROP_0,
PROP_PATH,
PROP_X,
PROP_Y,
PROP_FILLED,
PROP_PATH_STYLE
};
typedef struct _PikaCanvasPathPrivate PikaCanvasPathPrivate;
struct _PikaCanvasPathPrivate
{
cairo_path_t *path;
gdouble x;
gdouble y;
gboolean filled;
PikaPathStyle path_style;
};
#define GET_PRIVATE(path) \
((PikaCanvasPathPrivate *) pika_canvas_path_get_instance_private ((PikaCanvasPath *) (path)))
/* local function prototypes */
static void pika_canvas_path_finalize (GObject *object);
static void pika_canvas_path_set_property (GObject *object,
guint property_id,
const GValue *value,
GParamSpec *pspec);
static void pika_canvas_path_get_property (GObject *object,
guint property_id,
GValue *value,
GParamSpec *pspec);
static void pika_canvas_path_draw (PikaCanvasItem *item,
cairo_t *cr);
static cairo_region_t * pika_canvas_path_get_extents (PikaCanvasItem *item);
static void pika_canvas_path_stroke (PikaCanvasItem *item,
cairo_t *cr);
G_DEFINE_TYPE_WITH_PRIVATE (PikaCanvasPath, pika_canvas_path,
PIKA_TYPE_CANVAS_ITEM)
#define parent_class pika_canvas_path_parent_class
static void
pika_canvas_path_class_init (PikaCanvasPathClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
PikaCanvasItemClass *item_class = PIKA_CANVAS_ITEM_CLASS (klass);
object_class->finalize = pika_canvas_path_finalize;
object_class->set_property = pika_canvas_path_set_property;
object_class->get_property = pika_canvas_path_get_property;
item_class->draw = pika_canvas_path_draw;
item_class->get_extents = pika_canvas_path_get_extents;
item_class->stroke = pika_canvas_path_stroke;
g_object_class_install_property (object_class, PROP_PATH,
g_param_spec_boxed ("path", NULL, NULL,
PIKA_TYPE_BEZIER_DESC,
PIKA_PARAM_READWRITE));
g_object_class_install_property (object_class, PROP_X,
g_param_spec_double ("x", NULL, NULL,
-PIKA_MAX_IMAGE_SIZE,
PIKA_MAX_IMAGE_SIZE, 0,
PIKA_PARAM_READWRITE));
g_object_class_install_property (object_class, PROP_Y,
g_param_spec_double ("y", NULL, NULL,
-PIKA_MAX_IMAGE_SIZE,
PIKA_MAX_IMAGE_SIZE, 0,
PIKA_PARAM_READWRITE));
g_object_class_install_property (object_class, PROP_FILLED,
g_param_spec_boolean ("filled", NULL, NULL,
FALSE,
PIKA_PARAM_READWRITE));
g_object_class_install_property (object_class, PROP_PATH_STYLE,
g_param_spec_enum ("path-style", NULL, NULL,
PIKA_TYPE_PATH_STYLE,
PIKA_PATH_STYLE_DEFAULT,
PIKA_PARAM_READWRITE));
}
static void
pika_canvas_path_init (PikaCanvasPath *path)
{
}
static void
pika_canvas_path_finalize (GObject *object)
{
PikaCanvasPathPrivate *private = GET_PRIVATE (object);
if (private->path)
{
pika_bezier_desc_free (private->path);
private->path = NULL;
}
G_OBJECT_CLASS (parent_class)->finalize (object);
}
static void
pika_canvas_path_set_property (GObject *object,
guint property_id,
const GValue *value,
GParamSpec *pspec)
{
PikaCanvasPathPrivate *private = GET_PRIVATE (object);
switch (property_id)
{
case PROP_PATH:
if (private->path)
pika_bezier_desc_free (private->path);
private->path = g_value_dup_boxed (value);
break;
case PROP_X:
private->x = g_value_get_double (value);
break;
case PROP_Y:
private->y = g_value_get_double (value);
break;
case PROP_FILLED:
private->filled = g_value_get_boolean (value);
break;
case PROP_PATH_STYLE:
private->path_style = g_value_get_enum (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}
static void
pika_canvas_path_get_property (GObject *object,
guint property_id,
GValue *value,
GParamSpec *pspec)
{
PikaCanvasPathPrivate *private = GET_PRIVATE (object);
switch (property_id)
{
case PROP_PATH:
g_value_set_boxed (value, private->path);
break;
case PROP_X:
g_value_set_double (value, private->x);
break;
case PROP_Y:
g_value_set_double (value, private->y);
break;
case PROP_FILLED:
g_value_set_boolean (value, private->filled);
break;
case PROP_PATH_STYLE:
g_value_set_enum (value, private->path_style);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}
static void
pika_canvas_path_draw (PikaCanvasItem *item,
cairo_t *cr)
{
PikaCanvasPathPrivate *private = GET_PRIVATE (item);
if (private->path)
{
cairo_save (cr);
pika_canvas_item_transform (item, cr);
cairo_translate (cr, private->x, private->y);
cairo_append_path (cr, private->path);
cairo_restore (cr);
if (private->filled)
_pika_canvas_item_fill (item, cr);
else
_pika_canvas_item_stroke (item, cr);
}
}
static cairo_region_t *
pika_canvas_path_get_extents (PikaCanvasItem *item)
{
PikaCanvasPathPrivate *private = GET_PRIVATE (item);
GtkWidget *canvas = pika_canvas_item_get_canvas (item);
if (private->path && gtk_widget_get_realized (canvas))
{
cairo_surface_t *surface;
cairo_t *cr;
cairo_rectangle_int_t rectangle;
gdouble x1, y1, x2, y2;
surface = cairo_recording_surface_create (CAIRO_CONTENT_COLOR, NULL);
cr = cairo_create (surface);
cairo_surface_destroy (surface);
cairo_save (cr);
pika_canvas_item_transform (item, cr);
cairo_translate (cr, private->x, private->y);
cairo_append_path (cr, private->path);
cairo_restore (cr);
cairo_path_extents (cr, &x1, &y1, &x2, &y2);
cairo_destroy (cr);
if (private->filled)
{
rectangle.x = floor (x1 - 1.0);
rectangle.y = floor (y1 - 1.0);
rectangle.width = ceil (x2 + 1.0) - rectangle.x;
rectangle.height = ceil (y2 + 1.0) - rectangle.y;
}
else
{
rectangle.x = floor (x1 - 1.5);
rectangle.y = floor (y1 - 1.5);
rectangle.width = ceil (x2 + 1.5) - rectangle.x;
rectangle.height = ceil (y2 + 1.5) - rectangle.y;
}
return cairo_region_create_rectangle (&rectangle);
}
return NULL;
}
static void
pika_canvas_path_stroke (PikaCanvasItem *item,
cairo_t *cr)
{
PikaCanvasPathPrivate *private = GET_PRIVATE (item);
GtkWidget *canvas = pika_canvas_item_get_canvas (item);
gboolean active;
switch (private->path_style)
{
case PIKA_PATH_STYLE_VECTORS:
active = pika_canvas_item_get_highlight (item);
pika_canvas_set_vectors_bg_style (canvas, cr, active);
cairo_stroke_preserve (cr);
pika_canvas_set_vectors_fg_style (canvas, cr, active);
cairo_stroke (cr);
break;
case PIKA_PATH_STYLE_OUTLINE:
pika_canvas_set_outline_bg_style (canvas, cr);
cairo_stroke_preserve (cr);
pika_canvas_set_outline_fg_style (canvas, cr);
cairo_stroke (cr);
break;
case PIKA_PATH_STYLE_DEFAULT:
PIKA_CANVAS_ITEM_CLASS (parent_class)->stroke (item, cr);
break;
}
}
PikaCanvasItem *
pika_canvas_path_new (PikaDisplayShell *shell,
const PikaBezierDesc *bezier,
gdouble x,
gdouble y,
gboolean filled,
PikaPathStyle style)
{
g_return_val_if_fail (PIKA_IS_DISPLAY_SHELL (shell), NULL);
return g_object_new (PIKA_TYPE_CANVAS_PATH,
"shell", shell,
"path", bezier,
"x", x,
"y", y,
"filled", filled,
"path-style", style,
NULL);
}
void
pika_canvas_path_set (PikaCanvasItem *path,
const PikaBezierDesc *bezier)
{
g_return_if_fail (PIKA_IS_CANVAS_PATH (path));
pika_canvas_item_begin_change (path);
g_object_set (path,
"path", bezier,
NULL);
pika_canvas_item_end_change (path);
}