Code

Adding support for <color-profile> element
[inkscape.git] / src / color-profile.cpp
3 #include "xml/repr.h"
4 #include "color-profile.h"
5 #include "color-profile-fns.h"
6 #include "attributes.h"
8 using Inkscape::ColorProfile;
9 using Inkscape::ColorProfileClass;
11 static void colorprofile_class_init( ColorProfileClass *klass );
12 static void colorprofile_init( ColorProfile *cprof );
14 static void colorprofile_release( SPObject *object );
15 static void colorprofile_build( SPObject *object, SPDocument *document, Inkscape::XML::Node *repr );
16 static void colorprofile_set( SPObject *object, unsigned key, gchar const *value );
17 static Inkscape::XML::Node *colorprofile_write( SPObject *object, Inkscape::XML::Node *repr, guint flags );
19 static SPObject *cprof_parent_class;
21 /**
22  * Register ColorProfile class and return its type.
23  */
24 GType Inkscape::colorprofile_get_type()
25 {
26     static GType type = 0;
27     if (!type) {
28         GTypeInfo info = {
29             sizeof(ColorProfileClass),
30             NULL, NULL,
31             (GClassInitFunc) colorprofile_class_init,
32             NULL, NULL,
33             sizeof(ColorProfile),
34             16,
35             (GInstanceInitFunc) colorprofile_init,
36             NULL,   /* value_table */
37         };
38         type = g_type_register_static( SP_TYPE_OBJECT, "ColorProfile", &info, static_cast<GTypeFlags>(0) );
39     }
40     return type;
41 }
43 /**
44  * ColorProfile vtable initialization.
45  */
46 static void colorprofile_class_init( ColorProfileClass *klass )
47 {
48     SPObjectClass *sp_object_class = reinterpret_cast<SPObjectClass *>(klass);
50     cprof_parent_class = static_cast<SPObject*>(g_type_class_ref(SP_TYPE_OBJECT));
52     sp_object_class->release = colorprofile_release;
53     sp_object_class->build = colorprofile_build;
54     sp_object_class->set = colorprofile_set;
55     sp_object_class->write = colorprofile_write;
56 }
58 /**
59  * Callback for ColorProfile object initialization.
60  */
61 static void colorprofile_init( ColorProfile *cprof )
62 {
63     cprof->href = 0;
64     cprof->local = 0;
65     cprof->name = 0;
66     cprof->rendering_intent = 0;
67 #if ENABLE_LCMS
68     cprof->profHandle = 0;
69 #endif // ENABLE_LCMS
70 }
72 /**
73  * Callback: free object
74  */
75 static void colorprofile_release( SPObject *object )
76 {
77     ColorProfile *cprof = COLORPROFILE(object);
78     if ( cprof->href ) {
79         g_free( cprof->href );
80         cprof->href = 0;
81     }
83     if ( cprof->local ) {
84         g_free( cprof->local );
85         cprof->local = 0;
86     }
88     if ( cprof->name ) {
89         g_free( cprof->name );
90         cprof->name = 0;
91     }
93 #if ENABLE_LCMS
94     if ( cprof->profHandle ) {
95         cmsCloseProfile( cprof->profHandle );
96         cprof->profHandle = 0;
97     }
98 #endif // ENABLE_LCMS
99 }
101 /**
102  * Callback: set attributes from associated repr.
103  */
104 static void colorprofile_build( SPObject *object, SPDocument *document, Inkscape::XML::Node *repr )
106     ColorProfile *cprof = COLORPROFILE(object);
107     g_assert(cprof->href == 0);
108     g_assert(cprof->local == 0);
109     g_assert(cprof->name == 0);
111     if (((SPObjectClass *) cprof_parent_class)->build) {
112         (* ((SPObjectClass *) cprof_parent_class)->build)(object, document, repr);
113     }
114     sp_object_read_attr( object, "xlink:href" );
115     sp_object_read_attr( object, "local" );
116     sp_object_read_attr( object, "name" );
117     sp_object_read_attr( object, "rendering-intent" );
120 /**
121  * Callback: set attribute.
122  */
123 static void colorprofile_set( SPObject *object, unsigned key, gchar const *value )
125     ColorProfile *cprof = COLORPROFILE(object);
127     switch (key) {
128         case SP_ATTR_XLINK_HREF:
129             if ( value ) {
130                 cprof->href = g_strdup( value );
131                 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
132                 if ( *cprof->href ) {
133 #if ENABLE_LCMS
134                     cmsErrorAction( LCMS_ERROR_SHOW );
136                     // TODO open filename and URIs properly
137                     //FILE* fp = fopen_utf8name( filename, "r" );
138                     //LCMSAPI cmsHPROFILE   LCMSEXPORT cmsOpenProfileFromMem(LPVOID MemPtr, DWORD dwSize);
139                     cprof->profHandle = cmsOpenProfileFromFile( value, "r" );
140 #endif // ENABLE_LCMS
141                 }
142             }
143             break;
145         case SP_ATTR_LOCAL:
146             if ( value ) {
147                 cprof->local = g_strdup( value );
148                 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
149             }
150             break;
152         case SP_ATTR_NAME:
153             if ( value ) {
154                 cprof->name = g_strdup( value );
155                 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
156             }
157             break;
159         case SP_ATTR_RENDERING_INTENT:
160             if ( value ) {
161 // auto | perceptual | relative-colorimetric | saturation | absolute-colorimetric
162                 //cprof->name = g_strdup( value );
163                 //object->requestModified(SP_OBJECT_MODIFIED_FLAG);
164             }
165             break;
167         default:
168             if (((SPObjectClass *) cprof_parent_class)->set) {
169                 (* ((SPObjectClass *) cprof_parent_class)->set)(object, key, value);
170             }
171             break;
172     }
175 /**
176  * Callback: write attributes to associated repr.
177  */
178 static Inkscape::XML::Node* colorprofile_write( SPObject *object, Inkscape::XML::Node *repr, guint flags )
180     ColorProfile *cprof = COLORPROFILE(object);
182     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
183         repr = sp_repr_new("svg:color-profile");
184     }
186     if ( (flags & SP_OBJECT_WRITE_ALL) || cprof->href ) {
187         repr->setAttribute( "xlink:href", cprof->name );
188     }
190     if ( (flags & SP_OBJECT_WRITE_ALL) || cprof->href ) {
191         repr->setAttribute( "local", cprof->name );
192     }
194     if ( (flags & SP_OBJECT_WRITE_ALL) || cprof->href ) {
195         repr->setAttribute( "name", cprof->name );
196     }
198     if ( (flags & SP_OBJECT_WRITE_ALL) || cprof->href ) {
199 //        repr->setAttribute( "rendering-intent", cprof->name );
200     }
202     if (((SPObjectClass *) cprof_parent_class)->write) {
203         (* ((SPObjectClass *) cprof_parent_class)->write)(object, repr, flags);
204     }
206     return repr;
210 /*
211   Local Variables:
212   mode:c++
213   c-file-style:"stroustrup"
214   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
215   indent-tabs-mode:nil
216   fill-column:99
217   End:
218 */
219 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :