Code

Snap to intersections of line segments
[inkscape.git] / src / sp-fecolormatrix.cpp
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 "attributes.h"
23 #include "svg/svg.h"
24 #include "sp-fecolormatrix.h"
25 #include "xml/repr.h"
26 #include "helper-fns.h"
28 #include "display/nr-filter-colormatrix.h"
30 /* FeColorMatrix base class */
32 static void sp_feColorMatrix_class_init(SPFeColorMatrixClass *klass);
33 static void sp_feColorMatrix_init(SPFeColorMatrix *feColorMatrix);
35 static void sp_feColorMatrix_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
36 static void sp_feColorMatrix_release(SPObject *object);
37 static void sp_feColorMatrix_set(SPObject *object, unsigned int key, gchar const *value);
38 static void sp_feColorMatrix_update(SPObject *object, SPCtx *ctx, guint flags);
39 static Inkscape::XML::Node *sp_feColorMatrix_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
40 static void sp_feColorMatrix_build_renderer(SPFilterPrimitive *primitive, NR::Filter *filter);
42 static SPFilterPrimitiveClass *feColorMatrix_parent_class;
44 GType
45 sp_feColorMatrix_get_type()
46 {
47     static GType feColorMatrix_type = 0;
49     if (!feColorMatrix_type) {
50         GTypeInfo feColorMatrix_info = {
51             sizeof(SPFeColorMatrixClass),
52             NULL, NULL,
53             (GClassInitFunc) sp_feColorMatrix_class_init,
54             NULL, NULL,
55             sizeof(SPFeColorMatrix),
56             16,
57             (GInstanceInitFunc) sp_feColorMatrix_init,
58             NULL,    /* value_table */
59         };
60         feColorMatrix_type = g_type_register_static(SP_TYPE_FILTER_PRIMITIVE, "SPFeColorMatrix", &feColorMatrix_info, (GTypeFlags)0);
61     }
62     return feColorMatrix_type;
63 }
65 static void
66 sp_feColorMatrix_class_init(SPFeColorMatrixClass *klass)
67 {
68     SPObjectClass *sp_object_class = (SPObjectClass *)klass;
69     SPFilterPrimitiveClass *sp_primitive_class = (SPFilterPrimitiveClass *)klass;
71     feColorMatrix_parent_class = (SPFilterPrimitiveClass*)g_type_class_peek_parent(klass);
73     sp_object_class->build = sp_feColorMatrix_build;
74     sp_object_class->release = sp_feColorMatrix_release;
75     sp_object_class->write = sp_feColorMatrix_write;
76     sp_object_class->set = sp_feColorMatrix_set;
77     sp_object_class->update = sp_feColorMatrix_update;
78     sp_primitive_class->build_renderer = sp_feColorMatrix_build_renderer;
79 }
81 static void
82 sp_feColorMatrix_init(SPFeColorMatrix */*feColorMatrix*/)
83 {
84 }
86 /**
87  * Reads the Inkscape::XML::Node, and initializes SPFeColorMatrix variables.  For this to get called,
88  * our name must be associated with a repr via "sp_object_type_register".  Best done through
89  * sp-object-repr.cpp's repr_name_entries array.
90  */
91 static void
92 sp_feColorMatrix_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
93 {
94     if (((SPObjectClass *) feColorMatrix_parent_class)->build) {
95         ((SPObjectClass *) feColorMatrix_parent_class)->build(object, document, repr);
96     }
98     /*LOAD ATTRIBUTES FROM REPR HERE*/
99     sp_object_read_attr(object, "type");
100     sp_object_read_attr(object, "values");
103 /**
104  * Drops any allocated memory.
105  */
106 static void
107 sp_feColorMatrix_release(SPObject *object)
109     if (((SPObjectClass *) feColorMatrix_parent_class)->release)
110         ((SPObjectClass *) feColorMatrix_parent_class)->release(object);
113 static NR::FilterColorMatrixType sp_feColorMatrix_read_type(gchar const *value){
114     if (!value) return NR::COLORMATRIX_MATRIX; //matrix is default
115     switch(value[0]){
116         case 'm':
117             if (strcmp(value, "matrix") == 0) return NR::COLORMATRIX_MATRIX;
118             break;
119         case 's':
120             if (strcmp(value, "saturate") == 0) return NR::COLORMATRIX_SATURATE;
121             break;
122         case 'h':
123             if (strcmp(value, "hueRotate") == 0) return NR::COLORMATRIX_HUEROTATE;
124             break;
125         case 'l':
126             if (strcmp(value, "luminanceToAlpha") == 0) return NR::COLORMATRIX_LUMINANCETOALPHA;
127             break;
128     }
129     return NR::COLORMATRIX_MATRIX; //matrix is default
132 /**
133  * Sets a specific value in the SPFeColorMatrix.
134  */
135 static void
136 sp_feColorMatrix_set(SPObject *object, unsigned int key, gchar const *str)
138     SPFeColorMatrix *feColorMatrix = SP_FECOLORMATRIX(object);
139     (void)feColorMatrix;
141     NR::FilterColorMatrixType read_type;
142         /*DEAL WITH SETTING ATTRIBUTES HERE*/
143     switch(key) {
144         case SP_ATTR_TYPE:
145             read_type = sp_feColorMatrix_read_type(str);
146             if (feColorMatrix->type != read_type){
147                 feColorMatrix->type = read_type;
148                 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
149             }
150             break;
151         case SP_ATTR_VALUES:
152             if (str){
153                 feColorMatrix->values = helperfns_read_vector(str, 20);
154                 feColorMatrix->value = helperfns_read_number(str);
155                 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
156             }
157             break;
158         default:
159             if (((SPObjectClass *) feColorMatrix_parent_class)->set)
160                 ((SPObjectClass *) feColorMatrix_parent_class)->set(object, key, str);
161             break;
162     }
165 /**
166  * Receives update notifications.
167  */
168 static void
169 sp_feColorMatrix_update(SPObject *object, SPCtx *ctx, guint flags)
171     if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG |
172                  SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
174         /* do something to trigger redisplay, updates? */
176     }
178     if (((SPObjectClass *) feColorMatrix_parent_class)->update) {
179         ((SPObjectClass *) feColorMatrix_parent_class)->update(object, ctx, flags);
180     }
183 /**
184  * Writes its settings to an incoming repr object, if any.
185  */
186 static Inkscape::XML::Node *
187 sp_feColorMatrix_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
189     // Inkscape-only object, not copied during an "plain SVG" dump:
190     if (flags & SP_OBJECT_WRITE_EXT) {
191         if (repr) {
192             // is this sane?
193             repr->mergeFrom(SP_OBJECT_REPR(object), "id");
194         } else {
195             repr = SP_OBJECT_REPR(object)->duplicate(NULL); // FIXME
196         }
197     }
199     if (((SPObjectClass *) feColorMatrix_parent_class)->write) {
200         ((SPObjectClass *) feColorMatrix_parent_class)->write(object, repr, flags);
201     }
203     return repr;
206 static void sp_feColorMatrix_build_renderer(SPFilterPrimitive *primitive, NR::Filter *filter) {
207     g_assert(primitive != NULL);
208     g_assert(filter != NULL);
210     SPFeColorMatrix *sp_colormatrix = SP_FECOLORMATRIX(primitive);
212     int primitive_n = filter->add_primitive(NR::NR_FILTER_COLORMATRIX);
213     NR::FilterPrimitive *nr_primitive = filter->get_primitive(primitive_n);
214     NR::FilterColorMatrix *nr_colormatrix = dynamic_cast<NR::FilterColorMatrix*>(nr_primitive);
215     g_assert(nr_colormatrix != NULL);
217     sp_filter_primitive_renderer_common(primitive, nr_primitive);
218     nr_colormatrix->set_type(sp_colormatrix->type);
219     nr_colormatrix->set_value(sp_colormatrix->value);
220     nr_colormatrix->set_values(sp_colormatrix->values);
223 /*
224   Local Variables:
225   mode:c++
226   c-file-style:"stroustrup"
227   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
228   indent-tabs-mode:nil
229   fill-column:99
230   End:
231 */
232 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :