PIKApp/app/core/pikastrokeoptions.c

642 lines
20 KiB
C
Raw Normal View History

2023-09-26 00:35:21 +02:00
/* 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-1999 Spencer Kimball and Peter Mattis
*
* pikastrokeoptions.c
* Copyright (C) 2003 Simon Budig
* Copyright (C) 2004 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 <gdk-pixbuf/gdk-pixbuf.h>
#include <gegl.h>
#include "libpikabase/pikabase.h"
#include "libpikaconfig/pikaconfig.h"
#include "core-types.h"
#include "config/pikacoreconfig.h"
#include "pika.h"
#include "pikacontext.h"
#include "pikadashpattern.h"
#include "pikapaintinfo.h"
#include "pikaparamspecs.h"
#include "pikastrokeoptions.h"
#include "paint/pikapaintoptions.h"
#include "pika-intl.h"
enum
{
PROP_0,
PROP_METHOD,
PROP_STYLE,
PROP_WIDTH,
PROP_UNIT,
PROP_CAP_STYLE,
PROP_JOIN_STYLE,
PROP_MITER_LIMIT,
PROP_ANTIALIAS,
PROP_DASH_UNIT,
PROP_DASH_OFFSET,
PROP_DASH_INFO,
PROP_PAINT_OPTIONS,
PROP_EMULATE_DYNAMICS
};
enum
{
DASH_INFO_CHANGED,
LAST_SIGNAL
};
typedef struct _PikaStrokeOptionsPrivate PikaStrokeOptionsPrivate;
struct _PikaStrokeOptionsPrivate
{
PikaStrokeMethod method;
/* options for method == LIBART */
gdouble width;
PikaUnit unit;
PikaCapStyle cap_style;
PikaJoinStyle join_style;
gdouble miter_limit;
gdouble dash_offset;
GArray *dash_info;
/* options for method == PAINT_TOOL */
PikaPaintOptions *paint_options;
gboolean emulate_dynamics;
};
#define GET_PRIVATE(options) \
((PikaStrokeOptionsPrivate *) pika_stroke_options_get_instance_private ((PikaStrokeOptions *) (options)))
static void pika_stroke_options_config_iface_init (gpointer iface,
gpointer iface_data);
static void pika_stroke_options_finalize (GObject *object);
static void pika_stroke_options_set_property (GObject *object,
guint property_id,
const GValue *value,
GParamSpec *pspec);
static void pika_stroke_options_get_property (GObject *object,
guint property_id,
GValue *value,
GParamSpec *pspec);
static PikaConfig * pika_stroke_options_duplicate (PikaConfig *config);
G_DEFINE_TYPE_WITH_CODE (PikaStrokeOptions, pika_stroke_options,
PIKA_TYPE_FILL_OPTIONS,
G_ADD_PRIVATE (PikaStrokeOptions)
G_IMPLEMENT_INTERFACE (PIKA_TYPE_CONFIG,
pika_stroke_options_config_iface_init))
#define parent_class pika_stroke_options_parent_class
static PikaConfigInterface *parent_config_iface = NULL;
static guint stroke_options_signals[LAST_SIGNAL] = { 0 };
static void
pika_stroke_options_class_init (PikaStrokeOptionsClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GParamSpec *array_spec;
object_class->finalize = pika_stroke_options_finalize;
object_class->set_property = pika_stroke_options_set_property;
object_class->get_property = pika_stroke_options_get_property;
klass->dash_info_changed = NULL;
stroke_options_signals[DASH_INFO_CHANGED] =
g_signal_new ("dash-info-changed",
G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_FIRST,
G_STRUCT_OFFSET (PikaStrokeOptionsClass, dash_info_changed),
NULL, NULL, NULL,
G_TYPE_NONE, 1,
PIKA_TYPE_DASH_PRESET);
PIKA_CONFIG_PROP_ENUM (object_class, PROP_METHOD,
"method",
_("Method"),
NULL,
PIKA_TYPE_STROKE_METHOD,
PIKA_STROKE_LINE,
PIKA_PARAM_STATIC_STRINGS);
PIKA_CONFIG_PROP_DOUBLE (object_class, PROP_WIDTH,
"width",
_("Line width"),
NULL,
0.0, 2000.0, 6.0,
PIKA_PARAM_STATIC_STRINGS);
PIKA_CONFIG_PROP_UNIT (object_class, PROP_UNIT,
"unit",
_("Unit"),
NULL,
TRUE, FALSE, PIKA_UNIT_PIXEL,
PIKA_PARAM_STATIC_STRINGS);
PIKA_CONFIG_PROP_ENUM (object_class, PROP_CAP_STYLE,
"cap-style",
_("Cap style"),
NULL,
PIKA_TYPE_CAP_STYLE, PIKA_CAP_BUTT,
PIKA_PARAM_STATIC_STRINGS);
PIKA_CONFIG_PROP_ENUM (object_class, PROP_JOIN_STYLE,
"join-style",
_("Join style"),
NULL,
PIKA_TYPE_JOIN_STYLE, PIKA_JOIN_MITER,
PIKA_PARAM_STATIC_STRINGS);
PIKA_CONFIG_PROP_DOUBLE (object_class, PROP_MITER_LIMIT,
"miter-limit",
_("Miter limit"),
_("Convert a mitered join to a bevelled "
"join if the miter would extend to a "
"distance of more than miter-limit * "
"line-width from the actual join point."),
0.0, 100.0, 10.0,
PIKA_PARAM_STATIC_STRINGS);
PIKA_CONFIG_PROP_DOUBLE (object_class, PROP_DASH_OFFSET,
"dash-offset",
_("Dash offset"),
NULL,
0.0, 2000.0, 0.0,
PIKA_PARAM_STATIC_STRINGS);
array_spec = g_param_spec_double ("dash-length", NULL, NULL,
0.0, 2000.0, 1.0, PIKA_PARAM_READWRITE);
g_object_class_install_property (object_class, PROP_DASH_INFO,
pika_param_spec_value_array ("dash-info",
NULL, NULL,
array_spec,
PIKA_PARAM_STATIC_STRINGS |
PIKA_CONFIG_PARAM_FLAGS));
PIKA_CONFIG_PROP_OBJECT (object_class, PROP_PAINT_OPTIONS,
"paint-options",
NULL, NULL,
PIKA_TYPE_PAINT_OPTIONS,
PIKA_PARAM_STATIC_STRINGS);
PIKA_CONFIG_PROP_BOOLEAN (object_class, PROP_EMULATE_DYNAMICS,
"emulate-brush-dynamics",
_("Emulate brush dynamics"),
NULL,
FALSE,
PIKA_PARAM_STATIC_STRINGS);
}
static void
pika_stroke_options_config_iface_init (gpointer iface,
gpointer iface_data)
{
PikaConfigInterface *config_iface = (PikaConfigInterface *) iface;
parent_config_iface = g_type_interface_peek_parent (config_iface);
if (! parent_config_iface)
parent_config_iface = g_type_default_interface_peek (PIKA_TYPE_CONFIG);
config_iface->duplicate = pika_stroke_options_duplicate;
}
static void
pika_stroke_options_init (PikaStrokeOptions *options)
{
}
static void
pika_stroke_options_finalize (GObject *object)
{
PikaStrokeOptionsPrivate *private = GET_PRIVATE (object);
if (private->dash_info)
{
pika_dash_pattern_free (private->dash_info);
private->dash_info = NULL;
}
g_clear_object (&private->paint_options);
G_OBJECT_CLASS (parent_class)->finalize (object);
}
static void
pika_stroke_options_set_property (GObject *object,
guint property_id,
const GValue *value,
GParamSpec *pspec)
{
PikaStrokeOptions *options = PIKA_STROKE_OPTIONS (object);
PikaStrokeOptionsPrivate *private = GET_PRIVATE (object);
switch (property_id)
{
case PROP_METHOD:
private->method = g_value_get_enum (value);
break;
case PROP_WIDTH:
private->width = g_value_get_double (value);
break;
case PROP_UNIT:
private->unit = g_value_get_int (value);
break;
case PROP_CAP_STYLE:
private->cap_style = g_value_get_enum (value);
break;
case PROP_JOIN_STYLE:
private->join_style = g_value_get_enum (value);
break;
case PROP_MITER_LIMIT:
private->miter_limit = g_value_get_double (value);
break;
case PROP_DASH_OFFSET:
private->dash_offset = g_value_get_double (value);
break;
case PROP_DASH_INFO:
{
PikaValueArray *value_array = g_value_get_boxed (value);
GArray *pattern;
pattern = pika_dash_pattern_from_value_array (value_array);
pika_stroke_options_take_dash_pattern (options, PIKA_DASH_CUSTOM,
pattern);
}
break;
case PROP_PAINT_OPTIONS:
if (private->paint_options)
g_object_unref (private->paint_options);
private->paint_options = g_value_dup_object (value);
break;
case PROP_EMULATE_DYNAMICS:
private->emulate_dynamics = g_value_get_boolean (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}
static void
pika_stroke_options_get_property (GObject *object,
guint property_id,
GValue *value,
GParamSpec *pspec)
{
PikaStrokeOptionsPrivate *private = GET_PRIVATE (object);
switch (property_id)
{
case PROP_METHOD:
g_value_set_enum (value, private->method);
break;
case PROP_WIDTH:
g_value_set_double (value, private->width);
break;
case PROP_UNIT:
g_value_set_int (value, private->unit);
break;
case PROP_CAP_STYLE:
g_value_set_enum (value, private->cap_style);
break;
case PROP_JOIN_STYLE:
g_value_set_enum (value, private->join_style);
break;
case PROP_MITER_LIMIT:
g_value_set_double (value, private->miter_limit);
break;
case PROP_DASH_OFFSET:
g_value_set_double (value, private->dash_offset);
break;
case PROP_DASH_INFO:
{
PikaValueArray *value_array;
value_array = pika_dash_pattern_to_value_array (private->dash_info);
g_value_take_boxed (value, value_array);
}
break;
case PROP_PAINT_OPTIONS:
g_value_set_object (value, private->paint_options);
break;
case PROP_EMULATE_DYNAMICS:
g_value_set_boolean (value, private->emulate_dynamics);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}
static PikaConfig *
pika_stroke_options_duplicate (PikaConfig *config)
{
PikaStrokeOptions *options = PIKA_STROKE_OPTIONS (config);
PikaStrokeOptionsPrivate *private = GET_PRIVATE (options);
PikaStrokeOptions *new_options;
new_options = PIKA_STROKE_OPTIONS (parent_config_iface->duplicate (config));
if (private->paint_options)
{
GObject *paint_options;
paint_options = pika_config_duplicate (PIKA_CONFIG (private->paint_options));
g_object_set (new_options, "paint-options", paint_options, NULL);
g_object_unref (paint_options);
}
return PIKA_CONFIG (new_options);
}
/* public functions */
PikaStrokeOptions *
pika_stroke_options_new (Pika *pika,
PikaContext *context,
gboolean use_context_color)
{
PikaPaintInfo *paint_info = NULL;
PikaStrokeOptions *options;
g_return_val_if_fail (PIKA_IS_PIKA (pika), NULL);
g_return_val_if_fail (context == NULL || PIKA_IS_CONTEXT (context), NULL);
g_return_val_if_fail (use_context_color == FALSE || context != NULL, NULL);
if (context)
paint_info = pika_context_get_paint_info (context);
if (! paint_info)
paint_info = pika_paint_info_get_standard (pika);
options = g_object_new (PIKA_TYPE_STROKE_OPTIONS,
"pika", pika,
"paint-info", paint_info,
NULL);
if (use_context_color)
{
pika_context_define_properties (PIKA_CONTEXT (options),
PIKA_CONTEXT_PROP_MASK_FOREGROUND |
PIKA_CONTEXT_PROP_MASK_BACKGROUND |
PIKA_CONTEXT_PROP_MASK_PATTERN,
FALSE);
pika_context_set_parent (PIKA_CONTEXT (options), context);
}
return options;
}
PikaStrokeMethod
pika_stroke_options_get_method (PikaStrokeOptions *options)
{
g_return_val_if_fail (PIKA_IS_STROKE_OPTIONS (options),
PIKA_STROKE_LINE);
return GET_PRIVATE (options)->method;
}
gdouble
pika_stroke_options_get_width (PikaStrokeOptions *options)
{
g_return_val_if_fail (PIKA_IS_STROKE_OPTIONS (options), 1.0);
return GET_PRIVATE (options)->width;
}
PikaUnit
pika_stroke_options_get_unit (PikaStrokeOptions *options)
{
g_return_val_if_fail (PIKA_IS_STROKE_OPTIONS (options), PIKA_UNIT_PIXEL);
return GET_PRIVATE (options)->unit;
}
PikaCapStyle
pika_stroke_options_get_cap_style (PikaStrokeOptions *options)
{
g_return_val_if_fail (PIKA_IS_STROKE_OPTIONS (options), PIKA_CAP_BUTT);
return GET_PRIVATE (options)->cap_style;
}
PikaJoinStyle
pika_stroke_options_get_join_style (PikaStrokeOptions *options)
{
g_return_val_if_fail (PIKA_IS_STROKE_OPTIONS (options), PIKA_JOIN_MITER);
return GET_PRIVATE (options)->join_style;
}
gdouble
pika_stroke_options_get_miter_limit (PikaStrokeOptions *options)
{
g_return_val_if_fail (PIKA_IS_STROKE_OPTIONS (options), 1.0);
return GET_PRIVATE (options)->miter_limit;
}
gdouble
pika_stroke_options_get_dash_offset (PikaStrokeOptions *options)
{
g_return_val_if_fail (PIKA_IS_STROKE_OPTIONS (options), 0.0);
return GET_PRIVATE (options)->dash_offset;
}
GArray *
pika_stroke_options_get_dash_info (PikaStrokeOptions *options)
{
g_return_val_if_fail (PIKA_IS_STROKE_OPTIONS (options), NULL);
return GET_PRIVATE (options)->dash_info;
}
PikaPaintOptions *
pika_stroke_options_get_paint_options (PikaStrokeOptions *options)
{
g_return_val_if_fail (PIKA_IS_STROKE_OPTIONS (options), NULL);
return GET_PRIVATE (options)->paint_options;
}
gboolean
pika_stroke_options_get_emulate_dynamics (PikaStrokeOptions *options)
{
g_return_val_if_fail (PIKA_IS_STROKE_OPTIONS (options), FALSE);
return GET_PRIVATE (options)->emulate_dynamics;
}
/**
* pika_stroke_options_take_dash_pattern:
* @options: a #PikaStrokeOptions object
* @preset: a value out of the #PikaDashPreset enum
* @pattern: a #GArray or %NULL if @preset is not %PIKA_DASH_CUSTOM
*
* Sets the dash pattern. Either a @preset is passed and @pattern is
* %NULL or @preset is %PIKA_DASH_CUSTOM and @pattern is the #GArray
* to use as the dash pattern. Note that this function takes ownership
* of the passed pattern.
*/
void
pika_stroke_options_take_dash_pattern (PikaStrokeOptions *options,
PikaDashPreset preset,
GArray *pattern)
{
PikaStrokeOptionsPrivate *private;
g_return_if_fail (PIKA_IS_STROKE_OPTIONS (options));
g_return_if_fail (preset == PIKA_DASH_CUSTOM || pattern == NULL);
private = GET_PRIVATE (options);
if (preset != PIKA_DASH_CUSTOM)
pattern = pika_dash_pattern_new_from_preset (preset);
if (private->dash_info)
pika_dash_pattern_free (private->dash_info);
private->dash_info = pattern;
g_object_notify (G_OBJECT (options), "dash-info");
g_signal_emit (options, stroke_options_signals [DASH_INFO_CHANGED], 0,
preset);
}
void
pika_stroke_options_prepare (PikaStrokeOptions *options,
PikaContext *context,
PikaPaintOptions *paint_options)
{
PikaStrokeOptionsPrivate *private;
g_return_if_fail (PIKA_IS_STROKE_OPTIONS (options));
g_return_if_fail (PIKA_IS_CONTEXT (context));
g_return_if_fail (paint_options == NULL ||
PIKA_IS_PAINT_OPTIONS (paint_options));
private = GET_PRIVATE (options);
switch (private->method)
{
case PIKA_STROKE_LINE:
break;
case PIKA_STROKE_PAINT_METHOD:
{
PikaPaintInfo *paint_info = PIKA_CONTEXT (options)->paint_info;
if (paint_options)
{
g_return_if_fail (paint_info == paint_options->paint_info);
/* undefine the paint-relevant context properties and get them
* from the passed context
*/
pika_context_define_properties (PIKA_CONTEXT (paint_options),
PIKA_CONTEXT_PROP_MASK_PAINT,
FALSE);
pika_context_set_parent (PIKA_CONTEXT (paint_options), context);
g_object_ref (paint_options);
}
else
{
PikaCoreConfig *config = context->pika->config;
PikaContextPropMask global_props = 0;
paint_options =
pika_config_duplicate (PIKA_CONFIG (paint_info->paint_options));
/* FG and BG are always shared between all tools */
global_props |= PIKA_CONTEXT_PROP_MASK_FOREGROUND;
global_props |= PIKA_CONTEXT_PROP_MASK_BACKGROUND;
if (config->global_brush)
global_props |= PIKA_CONTEXT_PROP_MASK_BRUSH;
if (config->global_dynamics)
global_props |= PIKA_CONTEXT_PROP_MASK_DYNAMICS;
if (config->global_pattern)
global_props |= PIKA_CONTEXT_PROP_MASK_PATTERN;
if (config->global_palette)
global_props |= PIKA_CONTEXT_PROP_MASK_PALETTE;
if (config->global_gradient)
global_props |= PIKA_CONTEXT_PROP_MASK_GRADIENT;
if (config->global_font)
global_props |= PIKA_CONTEXT_PROP_MASK_FONT;
pika_context_copy_properties (context,
PIKA_CONTEXT (paint_options),
global_props);
}
g_object_set (options, "paint-options", paint_options, NULL);
g_object_unref (paint_options);
}
break;
default:
g_return_if_reached ();
}
}
void
pika_stroke_options_finish (PikaStrokeOptions *options)
{
g_return_if_fail (PIKA_IS_STROKE_OPTIONS (options));
g_object_set (options, "paint-options", NULL, NULL);
}