1 /** \file
2 * SVG <feColorMatrix> implementation.
3 *
4 */
5 /*
6 * Authors:
7 * Felipe Sanches <juca@members.fsf.org>
8 * hugo Rodrigues <haa.rodrigues@gmail.com>
9 * Abhishek Sharma
10 *
11 * Copyright (C) 2007 Felipe C. da S. Sanches
12 * Copyright (C) 2006 Hugo Rodrigues
13 *
14 * Released under GNU GPL, read the file 'COPYING' for more information
15 */
17 #ifdef HAVE_CONFIG_H
18 # include "config.h"
19 #endif
21 #include <string.h>
23 #include "attributes.h"
24 #include "svg/svg.h"
25 #include "colormatrix.h"
26 #include "xml/repr.h"
27 #include "helper-fns.h"
29 #include "display/nr-filter-colormatrix.h"
31 /* FeColorMatrix base class */
33 static void sp_feColorMatrix_class_init(SPFeColorMatrixClass *klass);
34 static void sp_feColorMatrix_init(SPFeColorMatrix *feColorMatrix);
36 static void sp_feColorMatrix_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
37 static void sp_feColorMatrix_release(SPObject *object);
38 static void sp_feColorMatrix_set(SPObject *object, unsigned int key, gchar const *value);
39 static void sp_feColorMatrix_update(SPObject *object, SPCtx *ctx, guint flags);
40 static Inkscape::XML::Node *sp_feColorMatrix_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
41 static void sp_feColorMatrix_build_renderer(SPFilterPrimitive *primitive, Inkscape::Filters::Filter *filter);
43 static SPFilterPrimitiveClass *feColorMatrix_parent_class;
45 GType
46 sp_feColorMatrix_get_type()
47 {
48 static GType feColorMatrix_type = 0;
50 if (!feColorMatrix_type) {
51 GTypeInfo feColorMatrix_info = {
52 sizeof(SPFeColorMatrixClass),
53 NULL, NULL,
54 (GClassInitFunc) sp_feColorMatrix_class_init,
55 NULL, NULL,
56 sizeof(SPFeColorMatrix),
57 16,
58 (GInstanceInitFunc) sp_feColorMatrix_init,
59 NULL, /* value_table */
60 };
61 feColorMatrix_type = g_type_register_static(SP_TYPE_FILTER_PRIMITIVE, "SPFeColorMatrix", &feColorMatrix_info, (GTypeFlags)0);
62 }
63 return feColorMatrix_type;
64 }
66 static void
67 sp_feColorMatrix_class_init(SPFeColorMatrixClass *klass)
68 {
69 SPObjectClass *sp_object_class = (SPObjectClass *)klass;
70 SPFilterPrimitiveClass *sp_primitive_class = (SPFilterPrimitiveClass *)klass;
72 feColorMatrix_parent_class = (SPFilterPrimitiveClass*)g_type_class_peek_parent(klass);
74 sp_object_class->build = sp_feColorMatrix_build;
75 sp_object_class->release = sp_feColorMatrix_release;
76 sp_object_class->write = sp_feColorMatrix_write;
77 sp_object_class->set = sp_feColorMatrix_set;
78 sp_object_class->update = sp_feColorMatrix_update;
79 sp_primitive_class->build_renderer = sp_feColorMatrix_build_renderer;
80 }
82 static void
83 sp_feColorMatrix_init(SPFeColorMatrix */*feColorMatrix*/)
84 {
85 }
87 /**
88 * Reads the Inkscape::XML::Node, and initializes SPFeColorMatrix variables. For this to get called,
89 * our name must be associated with a repr via "sp_object_type_register". Best done through
90 * sp-object-repr.cpp's repr_name_entries array.
91 */
92 static void
93 sp_feColorMatrix_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
94 {
95 if (((SPObjectClass *) feColorMatrix_parent_class)->build) {
96 ((SPObjectClass *) feColorMatrix_parent_class)->build(object, document, repr);
97 }
99 /*LOAD ATTRIBUTES FROM REPR HERE*/
100 object->readAttr( "type" );
101 object->readAttr( "values" );
102 }
104 /**
105 * Drops any allocated memory.
106 */
107 static void
108 sp_feColorMatrix_release(SPObject *object)
109 {
110 if (((SPObjectClass *) feColorMatrix_parent_class)->release)
111 ((SPObjectClass *) feColorMatrix_parent_class)->release(object);
112 }
114 static Inkscape::Filters::FilterColorMatrixType sp_feColorMatrix_read_type(gchar const *value){
115 if (!value) return Inkscape::Filters::COLORMATRIX_MATRIX; //matrix is default
116 switch(value[0]){
117 case 'm':
118 if (strcmp(value, "matrix") == 0) return Inkscape::Filters::COLORMATRIX_MATRIX;
119 break;
120 case 's':
121 if (strcmp(value, "saturate") == 0) return Inkscape::Filters::COLORMATRIX_SATURATE;
122 break;
123 case 'h':
124 if (strcmp(value, "hueRotate") == 0) return Inkscape::Filters::COLORMATRIX_HUEROTATE;
125 break;
126 case 'l':
127 if (strcmp(value, "luminanceToAlpha") == 0) return Inkscape::Filters::COLORMATRIX_LUMINANCETOALPHA;
128 break;
129 }
130 return Inkscape::Filters::COLORMATRIX_MATRIX; //matrix is default
131 }
133 /**
134 * Sets a specific value in the SPFeColorMatrix.
135 */
136 static void
137 sp_feColorMatrix_set(SPObject *object, unsigned int key, gchar const *str)
138 {
139 SPFeColorMatrix *feColorMatrix = SP_FECOLORMATRIX(object);
140 (void)feColorMatrix;
142 Inkscape::Filters::FilterColorMatrixType read_type;
143 /*DEAL WITH SETTING ATTRIBUTES HERE*/
144 switch(key) {
145 case SP_ATTR_TYPE:
146 read_type = sp_feColorMatrix_read_type(str);
147 if (feColorMatrix->type != read_type){
148 feColorMatrix->type = read_type;
149 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
150 }
151 break;
152 case SP_ATTR_VALUES:
153 if (str){
154 feColorMatrix->values = helperfns_read_vector(str, 20);
155 feColorMatrix->value = helperfns_read_number(str, HELPERFNS_NO_WARNING);
156 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
157 }
158 break;
159 default:
160 if (((SPObjectClass *) feColorMatrix_parent_class)->set)
161 ((SPObjectClass *) feColorMatrix_parent_class)->set(object, key, str);
162 break;
163 }
164 }
166 /**
167 * Receives update notifications.
168 */
169 static void
170 sp_feColorMatrix_update(SPObject *object, SPCtx *ctx, guint flags)
171 {
172 if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG |
173 SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
175 /* do something to trigger redisplay, updates? */
177 }
179 if (((SPObjectClass *) feColorMatrix_parent_class)->update) {
180 ((SPObjectClass *) feColorMatrix_parent_class)->update(object, ctx, flags);
181 }
182 }
184 /**
185 * Writes its settings to an incoming repr object, if any.
186 */
187 static Inkscape::XML::Node *
188 sp_feColorMatrix_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags)
189 {
190 /* TODO: Don't just clone, but create a new repr node and write all
191 * relevant values into it */
192 if (!repr) {
193 repr = SP_OBJECT_REPR(object)->duplicate(doc);
194 }
196 if (((SPObjectClass *) feColorMatrix_parent_class)->write) {
197 ((SPObjectClass *) feColorMatrix_parent_class)->write(object, doc, repr, flags);
198 }
200 return repr;
201 }
203 static void sp_feColorMatrix_build_renderer(SPFilterPrimitive *primitive, Inkscape::Filters::Filter *filter) {
204 g_assert(primitive != NULL);
205 g_assert(filter != NULL);
207 SPFeColorMatrix *sp_colormatrix = SP_FECOLORMATRIX(primitive);
209 int primitive_n = filter->add_primitive(Inkscape::Filters::NR_FILTER_COLORMATRIX);
210 Inkscape::Filters::FilterPrimitive *nr_primitive = filter->get_primitive(primitive_n);
211 Inkscape::Filters::FilterColorMatrix *nr_colormatrix = dynamic_cast<Inkscape::Filters::FilterColorMatrix*>(nr_primitive);
212 g_assert(nr_colormatrix != NULL);
214 sp_filter_primitive_renderer_common(primitive, nr_primitive);
215 nr_colormatrix->set_type(sp_colormatrix->type);
216 nr_colormatrix->set_value(sp_colormatrix->value);
217 nr_colormatrix->set_values(sp_colormatrix->values);
218 }
220 /*
221 Local Variables:
222 mode:c++
223 c-file-style:"stroustrup"
224 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
225 indent-tabs-mode:nil
226 fill-column:99
227 End:
228 */
229 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :