2006-12-10 05:33:38 +08:00
|
|
|
/* GIMP - The GNU Image Manipulation Program
|
2005-05-19 06:50:54 +08:00
|
|
|
* Copyright (C) 1995-1999 Spencer Kimball and Peter Mattis
|
|
|
|
*
|
|
|
|
* gimpdashpattern.c
|
|
|
|
* Copyright (C) 2003 Simon Budig
|
|
|
|
* Copyright (C) 2005 Sven Neumann
|
|
|
|
*
|
2009-01-18 06:28:01 +08:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
2005-05-19 06:50:54 +08:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
2009-01-18 06:28:01 +08:00
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
2005-05-19 06:50:54 +08:00
|
|
|
* (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
|
2009-01-18 06:28:01 +08:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2005-05-19 06:50:54 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <glib-object.h>
|
|
|
|
|
|
|
|
#include "core-types.h"
|
|
|
|
|
|
|
|
#include "gimpdashpattern.h"
|
|
|
|
|
|
|
|
|
2007-01-13 04:27:40 +08:00
|
|
|
GType
|
|
|
|
gimp_dash_pattern_get_type (void)
|
|
|
|
{
|
|
|
|
static GType type = 0;
|
|
|
|
|
|
|
|
if (! type)
|
|
|
|
type = g_boxed_type_register_static ("GimpDashPattern",
|
|
|
|
(GBoxedCopyFunc) gimp_dash_pattern_copy,
|
|
|
|
(GBoxedFreeFunc) gimp_dash_pattern_free);
|
|
|
|
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
2005-05-19 06:50:54 +08:00
|
|
|
GArray *
|
2007-01-13 04:27:40 +08:00
|
|
|
gimp_dash_pattern_new_from_preset (GimpDashPreset preset)
|
2005-05-19 06:50:54 +08:00
|
|
|
{
|
|
|
|
GArray *pattern;
|
|
|
|
gdouble dash;
|
|
|
|
gint i;
|
|
|
|
|
|
|
|
pattern = g_array_new (FALSE, FALSE, sizeof (gdouble));
|
|
|
|
|
|
|
|
switch (preset)
|
|
|
|
{
|
2005-05-26 07:25:45 +08:00
|
|
|
case GIMP_DASH_CUSTOM:
|
|
|
|
g_warning ("GIMP_DASH_CUSTOM passed to gimp_dash_pattern_from_preset()");
|
|
|
|
break;
|
|
|
|
|
2005-05-19 06:50:54 +08:00
|
|
|
case GIMP_DASH_LINE:
|
|
|
|
break;
|
2005-05-26 07:25:45 +08:00
|
|
|
|
2005-05-19 06:50:54 +08:00
|
|
|
case GIMP_DASH_LONG_DASH:
|
|
|
|
dash = 9.0; g_array_append_val (pattern, dash);
|
|
|
|
dash = 3.0; g_array_append_val (pattern, dash);
|
|
|
|
break;
|
2005-05-26 07:25:45 +08:00
|
|
|
|
2005-05-19 06:50:54 +08:00
|
|
|
case GIMP_DASH_MEDIUM_DASH:
|
|
|
|
dash = 6.0; g_array_append_val (pattern, dash);
|
|
|
|
dash = 6.0; g_array_append_val (pattern, dash);
|
|
|
|
break;
|
2005-05-26 07:25:45 +08:00
|
|
|
|
2005-05-19 06:50:54 +08:00
|
|
|
case GIMP_DASH_SHORT_DASH:
|
|
|
|
dash = 3.0; g_array_append_val (pattern, dash);
|
|
|
|
dash = 9.0; g_array_append_val (pattern, dash);
|
|
|
|
break;
|
2005-05-26 07:25:45 +08:00
|
|
|
|
2005-05-19 06:50:54 +08:00
|
|
|
case GIMP_DASH_SPARSE_DOTS:
|
|
|
|
for (i = 0; i < 2; i++)
|
|
|
|
{
|
|
|
|
dash = 1.0; g_array_append_val (pattern, dash);
|
|
|
|
dash = 5.0; g_array_append_val (pattern, dash);
|
|
|
|
}
|
|
|
|
break;
|
2005-05-26 07:25:45 +08:00
|
|
|
|
2005-05-19 06:50:54 +08:00
|
|
|
case GIMP_DASH_NORMAL_DOTS:
|
|
|
|
for (i = 0; i < 3; i++)
|
|
|
|
{
|
|
|
|
dash = 1.0; g_array_append_val (pattern, dash);
|
|
|
|
dash = 3.0; g_array_append_val (pattern, dash);
|
|
|
|
}
|
|
|
|
break;
|
2005-05-26 07:25:45 +08:00
|
|
|
|
2005-05-19 06:50:54 +08:00
|
|
|
case GIMP_DASH_DENSE_DOTS:
|
|
|
|
for (i = 0; i < 12; i++)
|
|
|
|
{
|
|
|
|
dash = 1.0; g_array_append_val (pattern, dash);
|
|
|
|
}
|
|
|
|
break;
|
2005-05-26 07:25:45 +08:00
|
|
|
|
2005-05-19 06:50:54 +08:00
|
|
|
case GIMP_DASH_STIPPLES:
|
|
|
|
for (i = 0; i < 24; i++)
|
|
|
|
{
|
|
|
|
dash = 0.5; g_array_append_val (pattern, dash);
|
|
|
|
}
|
|
|
|
break;
|
2005-05-26 07:25:45 +08:00
|
|
|
|
2005-05-19 06:50:54 +08:00
|
|
|
case GIMP_DASH_DASH_DOT:
|
|
|
|
dash = 7.0; g_array_append_val (pattern, dash);
|
|
|
|
dash = 2.0; g_array_append_val (pattern, dash);
|
|
|
|
dash = 1.0; g_array_append_val (pattern, dash);
|
|
|
|
dash = 2.0; g_array_append_val (pattern, dash);
|
|
|
|
break;
|
2005-05-26 07:25:45 +08:00
|
|
|
|
2005-05-19 06:50:54 +08:00
|
|
|
case GIMP_DASH_DASH_DOT_DOT:
|
|
|
|
dash = 7.0; g_array_append_val (pattern, dash);
|
|
|
|
for (i=0; i < 5; i++)
|
|
|
|
{
|
|
|
|
dash = 1.0; g_array_append_val (pattern, dash);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pattern->len < 2)
|
|
|
|
{
|
2007-01-13 04:27:40 +08:00
|
|
|
gimp_dash_pattern_free (pattern);
|
2005-05-19 06:50:54 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return pattern;
|
|
|
|
}
|
2005-05-19 07:26:53 +08:00
|
|
|
|
2005-05-19 07:59:35 +08:00
|
|
|
GArray *
|
2007-01-13 04:27:40 +08:00
|
|
|
gimp_dash_pattern_new_from_segments (const gboolean *segments,
|
|
|
|
gint n_segments,
|
|
|
|
gdouble dash_length)
|
2005-05-19 07:59:35 +08:00
|
|
|
{
|
|
|
|
GArray *pattern;
|
|
|
|
gint i;
|
|
|
|
gint count;
|
|
|
|
gboolean state;
|
|
|
|
|
|
|
|
g_return_val_if_fail (segments != NULL || n_segments == 0, NULL);
|
|
|
|
|
|
|
|
pattern = g_array_new (FALSE, FALSE, sizeof (gdouble));
|
|
|
|
|
|
|
|
for (i = 0, count = 0, state = TRUE; i <= n_segments; i++)
|
|
|
|
{
|
|
|
|
if (i < n_segments && segments[i] == state)
|
|
|
|
{
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
gdouble l = (dash_length * count) / n_segments;
|
|
|
|
|
2005-05-22 20:20:25 +08:00
|
|
|
g_array_append_val (pattern, l);
|
2005-05-19 07:59:35 +08:00
|
|
|
|
|
|
|
count = 1;
|
|
|
|
state = ! state;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pattern->len < 2)
|
|
|
|
{
|
2007-01-13 04:27:40 +08:00
|
|
|
gimp_dash_pattern_free (pattern);
|
2005-05-19 07:59:35 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return pattern;
|
|
|
|
}
|
|
|
|
|
2005-05-19 08:40:48 +08:00
|
|
|
void
|
2007-01-13 04:27:40 +08:00
|
|
|
gimp_dash_pattern_fill_segments (GArray *pattern,
|
|
|
|
gboolean *segments,
|
|
|
|
gint n_segments)
|
2005-05-19 08:40:48 +08:00
|
|
|
{
|
|
|
|
gdouble factor;
|
|
|
|
gdouble sum;
|
|
|
|
gint i, j;
|
|
|
|
gboolean paint;
|
|
|
|
|
|
|
|
g_return_if_fail (segments != NULL || n_segments == 0);
|
|
|
|
|
|
|
|
if (pattern == NULL || pattern->len <= 1)
|
|
|
|
{
|
|
|
|
for (i = 0; i < n_segments; i++)
|
|
|
|
segments[i] = TRUE;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0, sum = 0; i < pattern->len ; i++)
|
|
|
|
{
|
|
|
|
sum += g_array_index (pattern, gdouble, i);
|
|
|
|
}
|
|
|
|
|
|
|
|
factor = ((gdouble) n_segments) / sum;
|
|
|
|
|
|
|
|
j = 0;
|
|
|
|
sum = g_array_index (pattern, gdouble, j) * factor;
|
|
|
|
paint = TRUE;
|
|
|
|
|
|
|
|
for (i = 0; i < n_segments ; i++)
|
|
|
|
{
|
|
|
|
while (j < pattern->len && sum <= i)
|
|
|
|
{
|
|
|
|
paint = ! paint;
|
|
|
|
j++;
|
|
|
|
sum += g_array_index (pattern, gdouble, j) * factor;
|
|
|
|
}
|
|
|
|
|
|
|
|
segments[i] = paint;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-05-19 07:26:53 +08:00
|
|
|
GArray *
|
2007-01-13 04:27:40 +08:00
|
|
|
gimp_dash_pattern_from_value_array (GValueArray *value_array)
|
2005-05-19 07:26:53 +08:00
|
|
|
{
|
2007-01-13 04:27:40 +08:00
|
|
|
if (value_array == NULL || value_array->n_values == 0)
|
2005-05-19 07:26:53 +08:00
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
GArray *pattern;
|
|
|
|
gint i;
|
|
|
|
|
|
|
|
pattern = g_array_sized_new (FALSE, FALSE,
|
2007-01-13 04:27:40 +08:00
|
|
|
sizeof (gdouble), value_array->n_values);
|
2005-05-19 07:26:53 +08:00
|
|
|
|
2007-01-13 04:27:40 +08:00
|
|
|
for (i = 0; i < value_array->n_values; i++)
|
2005-05-19 07:26:53 +08:00
|
|
|
{
|
2007-01-13 04:27:40 +08:00
|
|
|
GValue *item = g_value_array_get_nth (value_array, i);
|
2005-05-19 07:26:53 +08:00
|
|
|
gdouble val;
|
|
|
|
|
|
|
|
g_return_val_if_fail (G_VALUE_HOLDS_DOUBLE (item), NULL);
|
|
|
|
|
|
|
|
val = g_value_get_double (item);
|
|
|
|
|
2005-05-22 20:20:25 +08:00
|
|
|
g_array_append_val (pattern, val);
|
2005-05-19 07:26:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return pattern;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-13 04:27:40 +08:00
|
|
|
GValueArray *
|
|
|
|
gimp_dash_pattern_to_value_array (GArray *pattern)
|
2005-05-19 07:26:53 +08:00
|
|
|
{
|
|
|
|
if (pattern == NULL || pattern->len == 0)
|
|
|
|
{
|
2007-01-13 04:27:40 +08:00
|
|
|
return NULL;
|
2005-05-19 07:26:53 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-01-13 04:27:40 +08:00
|
|
|
GValueArray *value_array = g_value_array_new (pattern->len);
|
|
|
|
GValue item = { 0, };
|
2005-05-19 07:26:53 +08:00
|
|
|
gint i;
|
|
|
|
|
|
|
|
g_value_init (&item, G_TYPE_DOUBLE);
|
|
|
|
|
|
|
|
for (i = 0; i < pattern->len; i++)
|
|
|
|
{
|
|
|
|
g_value_set_double (&item, g_array_index (pattern, gdouble, i));
|
2007-01-13 04:27:40 +08:00
|
|
|
g_value_array_append (value_array, &item);
|
2005-05-19 07:26:53 +08:00
|
|
|
}
|
|
|
|
|
2007-01-13 04:27:40 +08:00
|
|
|
g_value_unset (&item);
|
|
|
|
|
|
|
|
return value_array;
|
2005-05-19 07:26:53 +08:00
|
|
|
}
|
|
|
|
}
|
2005-05-22 04:24:42 +08:00
|
|
|
|
|
|
|
GArray *
|
|
|
|
gimp_dash_pattern_copy (GArray *pattern)
|
|
|
|
{
|
|
|
|
if (pattern)
|
|
|
|
{
|
|
|
|
GArray *copy;
|
|
|
|
gint i;
|
|
|
|
|
|
|
|
copy = g_array_sized_new (FALSE, FALSE, sizeof (gdouble), pattern->len);
|
|
|
|
|
|
|
|
for (i = 0; i < pattern->len; i++)
|
2005-05-22 20:20:25 +08:00
|
|
|
g_array_append_val (copy, g_array_index (pattern, gdouble, i));
|
2005-05-22 04:24:42 +08:00
|
|
|
|
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gimp_dash_pattern_free (GArray *pattern)
|
|
|
|
{
|
|
|
|
if (pattern)
|
|
|
|
g_array_free (pattern, TRUE);
|
|
|
|
}
|