3 #include "xml/repr.h"
4 #include "color-profile.h"
5 #include "color-profile-fns.h"
6 #include "attributes.h"
7 #include "document.h"
9 using Inkscape::ColorProfile;
10 using Inkscape::ColorProfileClass;
12 static void colorprofile_class_init( ColorProfileClass *klass );
13 static void colorprofile_init( ColorProfile *cprof );
15 static void colorprofile_release( SPObject *object );
16 static void colorprofile_build( SPObject *object, SPDocument *document, Inkscape::XML::Node *repr );
17 static void colorprofile_set( SPObject *object, unsigned key, gchar const *value );
18 static Inkscape::XML::Node *colorprofile_write( SPObject *object, Inkscape::XML::Node *repr, guint flags );
20 static SPObject *cprof_parent_class;
22 /**
23 * Register ColorProfile class and return its type.
24 */
25 GType Inkscape::colorprofile_get_type()
26 {
27 static GType type = 0;
28 if (!type) {
29 GTypeInfo info = {
30 sizeof(ColorProfileClass),
31 NULL, NULL,
32 (GClassInitFunc) colorprofile_class_init,
33 NULL, NULL,
34 sizeof(ColorProfile),
35 16,
36 (GInstanceInitFunc) colorprofile_init,
37 NULL, /* value_table */
38 };
39 type = g_type_register_static( SP_TYPE_OBJECT, "ColorProfile", &info, static_cast<GTypeFlags>(0) );
40 }
41 return type;
42 }
44 /**
45 * ColorProfile vtable initialization.
46 */
47 static void colorprofile_class_init( ColorProfileClass *klass )
48 {
49 SPObjectClass *sp_object_class = reinterpret_cast<SPObjectClass *>(klass);
51 cprof_parent_class = static_cast<SPObject*>(g_type_class_ref(SP_TYPE_OBJECT));
53 sp_object_class->release = colorprofile_release;
54 sp_object_class->build = colorprofile_build;
55 sp_object_class->set = colorprofile_set;
56 sp_object_class->write = colorprofile_write;
57 }
59 /**
60 * Callback for ColorProfile object initialization.
61 */
62 static void colorprofile_init( ColorProfile *cprof )
63 {
64 cprof->href = 0;
65 cprof->local = 0;
66 cprof->name = 0;
67 cprof->rendering_intent = 0;
68 #if ENABLE_LCMS
69 cprof->profHandle = 0;
70 #endif // ENABLE_LCMS
71 }
73 /**
74 * Callback: free object
75 */
76 static void colorprofile_release( SPObject *object )
77 {
78 ColorProfile *cprof = COLORPROFILE(object);
79 if ( cprof->href ) {
80 g_free( cprof->href );
81 cprof->href = 0;
82 }
84 if ( cprof->local ) {
85 g_free( cprof->local );
86 cprof->local = 0;
87 }
89 if ( cprof->name ) {
90 g_free( cprof->name );
91 cprof->name = 0;
92 }
94 #if ENABLE_LCMS
95 if ( cprof->profHandle ) {
96 cmsCloseProfile( cprof->profHandle );
97 cprof->profHandle = 0;
98 }
99 #endif // ENABLE_LCMS
100 }
102 /**
103 * Callback: set attributes from associated repr.
104 */
105 static void colorprofile_build( SPObject *object, SPDocument *document, Inkscape::XML::Node *repr )
106 {
107 ColorProfile *cprof = COLORPROFILE(object);
108 g_assert(cprof->href == 0);
109 g_assert(cprof->local == 0);
110 g_assert(cprof->name == 0);
112 if (((SPObjectClass *) cprof_parent_class)->build) {
113 (* ((SPObjectClass *) cprof_parent_class)->build)(object, document, repr);
114 }
115 sp_object_read_attr( object, "xlink:href" );
116 sp_object_read_attr( object, "local" );
117 sp_object_read_attr( object, "name" );
118 sp_object_read_attr( object, "rendering-intent" );
119 }
121 /**
122 * Callback: set attribute.
123 */
124 static void colorprofile_set( SPObject *object, unsigned key, gchar const *value )
125 {
126 ColorProfile *cprof = COLORPROFILE(object);
128 switch (key) {
129 case SP_ATTR_XLINK_HREF:
130 if ( value ) {
131 cprof->href = g_strdup( value );
132 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
133 if ( *cprof->href ) {
134 #if ENABLE_LCMS
135 cmsErrorAction( LCMS_ERROR_SHOW );
137 // TODO open filename and URIs properly
138 //FILE* fp = fopen_utf8name( filename, "r" );
139 //LCMSAPI cmsHPROFILE LCMSEXPORT cmsOpenProfileFromMem(LPVOID MemPtr, DWORD dwSize);
141 if ( !g_path_is_absolute(cprof->href) ) {
142 // Try to open relative
143 gchar* docbase = SP_DOCUMENT_BASE( SP_OBJECT_DOCUMENT(object) );
144 gchar* fullname = g_build_filename( docbase ? docbase : ".", cprof->href, NULL );
146 cprof->profHandle = cmsOpenProfileFromFile( fullname, "r" );
148 g_free (fullname);
149 } else {
150 cprof->profHandle = cmsOpenProfileFromFile( cprof->href, "r" );
151 }
153 #endif // ENABLE_LCMS
154 }
155 }
156 break;
158 case SP_ATTR_LOCAL:
159 if ( value ) {
160 cprof->local = g_strdup( value );
161 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
162 }
163 break;
165 case SP_ATTR_NAME:
166 if ( value ) {
167 cprof->name = g_strdup( value );
168 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
169 }
170 break;
172 case SP_ATTR_RENDERING_INTENT:
173 if ( value ) {
174 // auto | perceptual | relative-colorimetric | saturation | absolute-colorimetric
175 //cprof->name = g_strdup( value );
176 //object->requestModified(SP_OBJECT_MODIFIED_FLAG);
177 }
178 break;
180 default:
181 if (((SPObjectClass *) cprof_parent_class)->set) {
182 (* ((SPObjectClass *) cprof_parent_class)->set)(object, key, value);
183 }
184 break;
185 }
186 }
188 /**
189 * Callback: write attributes to associated repr.
190 */
191 static Inkscape::XML::Node* colorprofile_write( SPObject *object, Inkscape::XML::Node *repr, guint flags )
192 {
193 ColorProfile *cprof = COLORPROFILE(object);
195 if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
196 repr = sp_repr_new("svg:color-profile");
197 }
199 if ( (flags & SP_OBJECT_WRITE_ALL) || cprof->href ) {
200 repr->setAttribute( "xlink:href", cprof->name );
201 }
203 if ( (flags & SP_OBJECT_WRITE_ALL) || cprof->href ) {
204 repr->setAttribute( "local", cprof->name );
205 }
207 if ( (flags & SP_OBJECT_WRITE_ALL) || cprof->href ) {
208 repr->setAttribute( "name", cprof->name );
209 }
211 if ( (flags & SP_OBJECT_WRITE_ALL) || cprof->href ) {
212 // repr->setAttribute( "rendering-intent", cprof->name );
213 }
215 if (((SPObjectClass *) cprof_parent_class)->write) {
216 (* ((SPObjectClass *) cprof_parent_class)->write)(object, repr, flags);
217 }
219 return repr;
220 }
223 #if ENABLE_LCMS
226 static SPObject* bruteFind( SPObject* curr, gchar* const name )
227 {
228 SPObject* result = 0;
230 if ( curr ) {
231 if ( IS_COLORPROFILE(curr) ) {
232 ColorProfile* prof = COLORPROFILE(curr);
233 if ( prof ) {
234 if ( prof->name && (strcmp(prof->name, name) == 0) ) {
235 result = curr;
236 }
237 }
238 } else {
239 if ( curr->hasChildren() ) {
240 SPObject* child = curr->firstChild();
241 while ( child && !result ) {
242 result = bruteFind( child, name );
243 if ( !result ) {
244 child = child->next;
245 }
246 };
247 }
248 }
249 }
251 return result;
252 }
254 cmsHPROFILE Inkscape::colorprofile_get_handle( SPDocument* document, gchar* const name )
255 {
256 cmsHPROFILE prof = 0;
258 SPObject* root = SP_DOCUMENT_ROOT(document);
259 SPObject* thing = bruteFind( root, name );
260 if ( thing ) {
261 prof = COLORPROFILE(thing)->profHandle;
262 }
264 return prof;
265 }
266 #endif // ENABLE_LCMS
268 /*
269 Local Variables:
270 mode:c++
271 c-file-style:"stroustrup"
272 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
273 indent-tabs-mode:nil
274 fill-column:99
275 End:
276 */
277 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :