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