Commit a6f4f626 authored by Sven Neumann's avatar Sven Neumann Committed by Sven Neumann
Browse files

libgimpcolor/gimprgb-parse.c use a signed integer to pass the string

2004-07-22  Sven Neumann  <sven@gimp.org>

	* libgimpcolor/gimprgb-parse.c
	* libgimpcolor/gimprgb.h: use a signed integer to pass the string
	length to the new parser functions. The API explicitely asks for
	-1 to be passed...

	* app/core/gimp.c
	* app/core/gimpgradient-load.[ch]
	* app/core/gimpgradient.h: added preliminary support for loading
	simple SVG gradients (see bug #148127).  Be careful with this new
	feature; editing the loaded gradient will cause the SVG file to be
	overwritten! Work in progress...
parent a14bddae
2004-07-22 Sven Neumann <sven@gimp.org>
* libgimpcolor/gimprgb-parse.c
* libgimpcolor/gimprgb.h: use a signed integer to pass the string
length to the new parser functions. The API explicitely asks for
-1 to be passed...
* app/core/gimp.c
* app/core/gimpgradient-load.[ch]
* app/core/gimpgradient.h: added preliminary support for loading
simple SVG gradients (see bug #148127). Be careful with this new
feature; editing the loaded gradient will cause the SVG file to be
overwritten! Work in progress...
2004-07-22 Sven Neumann <sven@gimp.org>
* app/core/Makefile.am
......
......@@ -544,6 +544,7 @@ gimp_real_initialize (Gimp *gimp,
static const GimpDataFactoryLoaderEntry gradient_loader_entries[] =
{
{ gimp_gradient_load, GIMP_GRADIENT_FILE_EXTENSION },
{ gimp_gradient_load_svg, GIMP_GRADIENT_SVG_FILE_EXTENSION },
{ gimp_gradient_load, NULL /* legacy loader */ }
};
......
......@@ -26,9 +26,12 @@
#include <glib-object.h>
#include "libgimpbase/gimpbase.h"
#include "libgimpcolor/gimpcolor.h"
#include "core-types.h"
#include "config/gimpxmlparser.h"
#include "gimpgradient.h"
#include "gimpgradient-load.h"
......@@ -177,3 +180,304 @@ gimp_gradient_load (const gchar *filename,
return GIMP_DATA (gradient);
}
/* SVG gradient parser */
typedef enum
{
SVG_START,
SVG_IN_SVG,
SVG_IN_GRADIENT,
SVG_IN_GRADIENT_STOP,
SVG_IN_UNKNOWN
} SvgParserState;
typedef struct
{
SvgParserState state;
SvgParserState last_known_state;
gint unknown_depth;
GimpGradient *gradient;
GList *stops;
} SvgParser;
typedef struct
{
gdouble offset;
GimpRGB color;
} SvgStop;
static void svg_parser_start_element (GMarkupParseContext *context,
const gchar *element_name,
const gchar **attribute_names,
const gchar **attribute_values,
gpointer user_data,
GError **error);
static void svg_parser_end_element (GMarkupParseContext *context,
const gchar *element_name,
gpointer user_data,
GError **error);
static void svg_parser_start_unknown (SvgParser *parser);
static void svg_parser_end_unknown (SvgParser *parser);
static SvgStop * svg_parse_gradient_stop (const gchar **names,
const gchar **values);
static const GMarkupParser markup_parser =
{
svg_parser_start_element,
svg_parser_end_element,
NULL, /* characters */
NULL, /* passthrough */
NULL /* error */
};
GimpData *
gimp_gradient_load_svg (const gchar *filename,
gboolean stingy_memory_use,
GError **error)
{
GimpData *data = NULL;
GimpXmlParser *xml_parser;
SvgParser parser;
gboolean success;
g_return_val_if_fail (filename != NULL, NULL);
g_return_val_if_fail (g_path_is_absolute (filename), NULL);
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
parser.state = SVG_START;
parser.unknown_depth = 0;
parser.gradient = NULL;
parser.stops = NULL;
xml_parser = gimp_xml_parser_new (&markup_parser, &parser);
success = gimp_xml_parser_parse_file (xml_parser, filename, error);
gimp_xml_parser_free (xml_parser);
if (success && parser.gradient && parser.stops)
{
GimpGradientSegment *seg = gimp_gradient_segment_new ();
GimpGradientSegment *next = NULL;
SvgStop *stop = parser.stops->data;
GList *list;
seg->left_color = stop->color;
seg->right_color = stop->color;
/* the list of offsets is sorted from largest to smallest */
for (list = g_list_next (parser.stops); list; list = g_list_next (list))
{
seg->left = stop->offset;
seg->middle = (seg->left + seg->right) / 2.0;
next = seg;
seg = gimp_gradient_segment_new ();
seg->next = next;
next->prev = seg;
seg->right = stop->offset;
seg->right_color = stop->color;
stop = list->data;
seg->left_color = stop->color;
}
seg->middle = (seg->left + seg->right) / 2.0;
parser.gradient->segments = seg;
data = g_object_ref (parser.gradient);
/* FIXME: this will be overwritten by GimpDataFactory */
data->writable = FALSE;
}
/* FIXME: error handling */
if (parser.gradient)
g_object_unref (parser.gradient);
g_list_foreach (parser.stops, (GFunc) g_free, NULL);
g_list_free (parser.stops);
return data;
}
static void
svg_parser_start_element (GMarkupParseContext *context,
const gchar *element_name,
const gchar **attribute_names,
const gchar **attribute_values,
gpointer user_data,
GError **error)
{
SvgParser *parser = user_data;
switch (parser->state)
{
case SVG_START:
if (strcmp (element_name, "svg") == 0)
parser->state = SVG_IN_SVG;
else
svg_parser_start_unknown (parser);
break;
case SVG_IN_SVG:
if (parser->gradient == NULL &&
strcmp (element_name, "linearGradient") == 0)
{
const gchar *name = NULL;
while (*attribute_names && *attribute_values)
{
if (strcmp (*attribute_names, "id") == 0 && *attribute_values)
name = *attribute_values;
attribute_names++;
attribute_values++;
}
parser->gradient = g_object_new (GIMP_TYPE_GRADIENT,
"name", name,
NULL);
parser->state = SVG_IN_GRADIENT;
}
else
svg_parser_start_unknown (parser);
break;
case SVG_IN_GRADIENT:
if (strcmp (element_name, "stop") == 0)
{
SvgStop *stop = svg_parse_gradient_stop (attribute_names,
attribute_values);
/* The spec clearly states that each gradient stop's offset
* value is required to be equal to or greater than the
* previous gradient stop's offset value.
*/
if (parser->stops)
stop->offset = MAX (stop->offset,
((SvgStop *) parser->stops->data)->offset);
parser->stops = g_list_prepend (parser->stops, stop);
parser->state = SVG_IN_GRADIENT_STOP;
}
else
svg_parser_start_unknown (parser);
break;
case SVG_IN_GRADIENT_STOP:
svg_parser_start_unknown (parser);
break;
case SVG_IN_UNKNOWN:
svg_parser_start_unknown (parser);
break;
}
}
static void
svg_parser_end_element (GMarkupParseContext *context,
const gchar *element_name,
gpointer user_data,
GError **error)
{
SvgParser *parser = user_data;
switch (parser->state)
{
case SVG_START:
g_return_if_reached ();
break;
case SVG_IN_SVG:
parser->state = SVG_START;
break;
case SVG_IN_GRADIENT:
parser->state = SVG_IN_SVG;
break;
case SVG_IN_GRADIENT_STOP:
parser->state = SVG_IN_GRADIENT;
break;
case SVG_IN_UNKNOWN:
svg_parser_end_unknown (parser);
break;
}
}
static void
svg_parser_start_unknown (SvgParser *parser)
{
if (parser->unknown_depth == 0)
parser->last_known_state = parser->state;
parser->state = SVG_IN_UNKNOWN;
parser->unknown_depth++;
}
static void
svg_parser_end_unknown (SvgParser *parser)
{
g_return_if_fail (parser->unknown_depth > 0 &&
parser->state == SVG_IN_UNKNOWN);
parser->unknown_depth--;
if (parser->unknown_depth == 0)
parser->state = parser->last_known_state;
}
static SvgStop *
svg_parse_gradient_stop (const gchar **names,
const gchar **values)
{
SvgStop stop;
stop.offset = 0.0;
gimp_rgba_set (&stop.color, 0.0, 0.0, 0.0, 1.0);
while (*names && *values)
{
if (strcmp (*names, "offset") == 0)
{
gchar *end;
stop.offset = g_ascii_strtod (*values, &end);
if (end && *end == '%')
stop.offset /= 100.0;
stop.offset = CLAMP (stop.offset, 0.0, 1.0);
}
else if (strcmp (*names, "stop-color") == 0)
{
gimp_rgb_parse_css (&stop.color, *values, -1);
}
else if (strcmp (*names, "stop-opacity") == 0)
{
gdouble opacity = g_ascii_strtod (*values, NULL);
if (errno != ERANGE)
gimp_rgb_set_alpha (&stop.color, CLAMP (opacity, 0.0, 1.0));
}
names++;
values++;
}
return g_memdup (&stop, sizeof (SvgStop));
}
......@@ -20,9 +20,12 @@
#define __GIMP_GRADIENT_LOAD_H__
GimpData * gimp_gradient_load (const gchar *filename,
gboolean stingy_memory_use,
GError **error);
GimpData * gimp_gradient_load (const gchar *filename,
gboolean stingy_memory_use,
GError **error);
GimpData * gimp_gradient_load_svg (const gchar *filename,
gboolean stingy_memory_use,
GError **error);
#endif /* __GIMP_GRADIENT_LOAD_H__ */
......@@ -24,6 +24,7 @@
#define GIMP_GRADIENT_FILE_EXTENSION ".ggr"
#define GIMP_GRADIENT_SVG_FILE_EXTENSION ".svg"
#define GIMP_GRADIENT_DEFAULT_SAMPLE_SIZE 40
......
......@@ -66,7 +66,7 @@ static gboolean gimp_rgb_parse_css_internal (GimpRGB *rgb,
gboolean
gimp_rgb_parse_name (GimpRGB *rgb,
const gchar *name,
gsize len)
gint len)
{
gchar *tmp;
gboolean result;
......@@ -105,7 +105,7 @@ gimp_rgb_parse_name (GimpRGB *rgb,
gboolean
gimp_rgb_parse_hex (GimpRGB *rgb,
const gchar *hex,
gsize len)
gint len)
{
gchar *tmp;
gboolean result;
......@@ -131,17 +131,12 @@ gimp_rgb_parse_hex (GimpRGB *rgb,
* @css: a string describing a color in CSS notation
* @len: the length of @hex, in bytes. or -1 if @hex is nul-terminated
*
* Attempts to parse a string describing a color in RGB value in
* CSS notation. This can be either a numerical representation:
* <informalexample><programlisting>
* rgb (255, 0, 0)
* rgb (100%, 0%, 0%)
* </programlisting></informalexample>
* or a hexadecimal notation as parsed by gimp_rgb_parse_hex():
* <informalexample><programlisting>
* #ff0000
* </programlisting></informalexample>
* or a color name as parsed by gimp_rgb_parse_name().
* Attempts to parse a string describing a color in RGB value in CSS
* notation. This can be either a numerical representation
* (<code>rgb (255, 0, 0)</code> or <code>rgb (100%, 0%, 0%)</code>) or
* a hexadecimal notation as parsed by gimp_rgb_parse_hex()
* (<code>##ff0000</code>) or a color name as parsed by
* gimp_rgb_parse_name() (<code>red</code>).
*
* This funcion does not touch the alpha component of @rgb.
*
......@@ -153,7 +148,7 @@ gimp_rgb_parse_hex (GimpRGB *rgb,
gboolean
gimp_rgb_parse_css (GimpRGB *rgb,
const gchar *css,
gsize len)
gint len)
{
gchar *tmp;
gboolean result;
......@@ -453,6 +448,7 @@ gimp_rgb_parse_css_internal (GimpRGB *rgb,
if (*css == ')')
{
gimp_rgb_set (rgb, values[0], values[1], values[2]);
gimp_rgb_clamp (rgb);
return TRUE;
}
......
......@@ -53,13 +53,13 @@ void gimp_rgb_get_uchar (const GimpRGB *rgb,
gboolean gimp_rgb_parse_name (GimpRGB *rgb,
const gchar *name,
gsize len);
gint len);
gboolean gimp_rgb_parse_hex (GimpRGB *rgb,
const gchar *hex,
gsize len);
gint len);
gboolean gimp_rgb_parse_css (GimpRGB *rgb,
const gchar *css,
gsize len);
gint len);
void gimp_rgb_add (GimpRGB *rgb1,
const GimpRGB *rgb2);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment