Code

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