Code

start switching sp_repr_new* over to XML::Document::create*, and rename create method...
[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"
7 #include "inkscape.h"
8 #include "document.h"
10 #include "dom/uri.h"
12 //#define DEBUG_LCMS
14 #ifdef DEBUG_LCMS
15 #include "prefs-utils.h"
16 #include <gtk/gtkmessagedialog.h>
17 #endif // DEBUG_LCMS
19 using Inkscape::ColorProfile;
20 using Inkscape::ColorProfileClass;
22 namespace Inkscape
23 {
24 static void colorprofile_class_init( ColorProfileClass *klass );
25 static void colorprofile_init( ColorProfile *cprof );
27 static void colorprofile_release( SPObject *object );
28 static void colorprofile_build( SPObject *object, SPDocument *document, Inkscape::XML::Node *repr );
29 static void colorprofile_set( SPObject *object, unsigned key, gchar const *value );
30 static Inkscape::XML::Node *colorprofile_write( SPObject *object, Inkscape::XML::Node *repr, guint flags );
31 }
33 #ifdef DEBUG_LCMS
34 extern guint update_in_progress;
35 #define DEBUG_MESSAGE(key, ...) \
36 {\
37     gint dump = prefs_get_int_attribute_limited("options.scislac", #key, 0, 0, 1);\
38     gint dumpD = prefs_get_int_attribute_limited("options.scislac", #key"D", 0, 0, 1);\
39     gint dumpD2 = prefs_get_int_attribute_limited("options.scislac", #key"D2", 0, 0, 1);\
40     dumpD &= ( (update_in_progress == 0) || dumpD2 );\
41     if ( dump )\
42     {\
43         g_message( __VA_ARGS__ );\
44 \
45     }\
46     if ( dumpD )\
47     {\
48         GtkWidget *dialog = gtk_message_dialog_new(NULL,\
49                                                    GTK_DIALOG_DESTROY_WITH_PARENT, \
50                                                    GTK_MESSAGE_INFO,    \
51                                                    GTK_BUTTONS_OK,      \
52                                                    __VA_ARGS__          \
53                                                    );\
54         g_signal_connect_swapped(dialog, "response",\
55                                  G_CALLBACK(gtk_widget_destroy),        \
56                                  dialog);                               \
57         gtk_widget_show_all( dialog );\
58     }\
59 }
60 #endif // DEBUG_LCMS
62 static SPObject *cprof_parent_class;
64 /**
65  * Register ColorProfile class and return its type.
66  */
67 GType Inkscape::colorprofile_get_type()
68 {
69     static GType type = 0;
70     if (!type) {
71         GTypeInfo info = {
72             sizeof(ColorProfileClass),
73             NULL, NULL,
74             (GClassInitFunc) colorprofile_class_init,
75             NULL, NULL,
76             sizeof(ColorProfile),
77             16,
78             (GInstanceInitFunc) colorprofile_init,
79             NULL,   /* value_table */
80         };
81         type = g_type_register_static( SP_TYPE_OBJECT, "ColorProfile", &info, static_cast<GTypeFlags>(0) );
82     }
83     return type;
84 }
86 /**
87  * ColorProfile vtable initialization.
88  */
89 static void Inkscape::colorprofile_class_init( ColorProfileClass *klass )
90 {
91     SPObjectClass *sp_object_class = reinterpret_cast<SPObjectClass *>(klass);
93     cprof_parent_class = static_cast<SPObject*>(g_type_class_ref(SP_TYPE_OBJECT));
95     sp_object_class->release = colorprofile_release;
96     sp_object_class->build = colorprofile_build;
97     sp_object_class->set = colorprofile_set;
98     sp_object_class->write = colorprofile_write;
99 }
101 /**
102  * Callback for ColorProfile object initialization.
103  */
104 static void Inkscape::colorprofile_init( ColorProfile *cprof )
106     cprof->href = 0;
107     cprof->local = 0;
108     cprof->name = 0;
109     cprof->intentStr = 0;
110     cprof->rendering_intent = Inkscape::RENDERING_INTENT_UNKNOWN;
111 #if ENABLE_LCMS
112     cprof->profHandle = 0;
113 #endif // ENABLE_LCMS
116 /**
117  * Callback: free object
118  */
119 static void Inkscape::colorprofile_release( SPObject *object )
121     ColorProfile *cprof = COLORPROFILE(object);
122     if ( cprof->href ) {
123         g_free( cprof->href );
124         cprof->href = 0;
125     }
127     if ( cprof->local ) {
128         g_free( cprof->local );
129         cprof->local = 0;
130     }
132     if ( cprof->name ) {
133         g_free( cprof->name );
134         cprof->name = 0;
135     }
137     if ( cprof->intentStr ) {
138         g_free( cprof->intentStr );
139         cprof->intentStr = 0;
140     }
142 #if ENABLE_LCMS
143     if ( cprof->profHandle ) {
144         cmsCloseProfile( cprof->profHandle );
145         cprof->profHandle = 0;
146     }
147 #endif // ENABLE_LCMS
150 /**
151  * Callback: set attributes from associated repr.
152  */
153 static void Inkscape::colorprofile_build( SPObject *object, SPDocument *document, Inkscape::XML::Node *repr )
155     ColorProfile *cprof = COLORPROFILE(object);
156     g_assert(cprof->href == 0);
157     g_assert(cprof->local == 0);
158     g_assert(cprof->name == 0);
159     g_assert(cprof->intentStr == 0);
161     if (((SPObjectClass *) cprof_parent_class)->build) {
162         (* ((SPObjectClass *) cprof_parent_class)->build)(object, document, repr);
163     }
164     sp_object_read_attr( object, "xlink:href" );
165     sp_object_read_attr( object, "local" );
166     sp_object_read_attr( object, "name" );
167     sp_object_read_attr( object, "rendering-intent" );
170 /**
171  * Callback: set attribute.
172  */
173 static void Inkscape::colorprofile_set( SPObject *object, unsigned key, gchar const *value )
175     ColorProfile *cprof = COLORPROFILE(object);
177     switch (key) {
178         case SP_ATTR_XLINK_HREF:
179             if ( cprof->href ) {
180                 g_free( cprof->href );
181                 cprof->href = 0;
182             }
183             if ( value ) {
184                 cprof->href = g_strdup( value );
185                 if ( *cprof->href ) {
186 #if ENABLE_LCMS
187                     cmsErrorAction( LCMS_ERROR_SHOW );
189                     // TODO open filename and URIs properly
190                     //FILE* fp = fopen_utf8name( filename, "r" );
191                     //LCMSAPI cmsHPROFILE   LCMSEXPORT cmsOpenProfileFromMem(LPVOID MemPtr, DWORD dwSize);
193                     // Try to open relative
194                     SPDocument *doc = SP_OBJECT_DOCUMENT(object);
195                     if (!doc) {
196                         doc = SP_ACTIVE_DOCUMENT;
197                         g_warning("object has no document.  using active");
198                     }
199                     //# 1.  Get complete URI of document
200                     gchar* docbase = SP_DOCUMENT_URI( doc );
201                     if (!docbase)
202                         {
203                         g_warning("null docbase");
204                         docbase = "";
205                         }
206                     //g_message("docbase:%s\n", docbase);
207                     org::w3c::dom::URI docUri(docbase);
208                     //# 2. Get href of icc file.  we don't care if it's rel or abs
209                     org::w3c::dom::URI hrefUri(cprof->href);
210                     //# 3.  Resolve the href according the docBase.  This follows
211                     //      the w3c specs.  All absolute and relative issues are considered
212                     org::w3c::dom::URI cprofUri = docUri.resolve(hrefUri);
213                     gchar* fullname = (gchar *)cprofUri.getNativePath().c_str();
214                     cprof->profHandle = cmsOpenProfileFromFile( fullname, "r" );
215 #ifdef DEBUG_LCMS
216                     DEBUG_MESSAGE( lcmsOne, "cmsOpenProfileFromFile( '%s'...) = %p", fullname, (void*)cprof->profHandle );
217 #endif // DEBUG_LCMS
219 #endif // ENABLE_LCMS
220                 }
221             }
222             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
223             break;
225         case SP_ATTR_LOCAL:
226             if ( cprof->local ) {
227                 g_free( cprof->local );
228                 cprof->local = 0;
229             }
230             cprof->local = g_strdup( value );
231             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
232             break;
234         case SP_ATTR_NAME:
235             if ( cprof->name ) {
236                 g_free( cprof->name );
237                 cprof->name = 0;
238             }
239             cprof->name = g_strdup( value );
240 #ifdef DEBUG_LCMS
241             DEBUG_MESSAGE( lcmsTwo, "<color-profile> name set to '%s'", cprof->name );
242 #endif // DEBUG_LCMS
243             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
244             break;
246         case SP_ATTR_RENDERING_INTENT:
247             if ( cprof->intentStr ) {
248                 g_free( cprof->intentStr );
249                 cprof->intentStr = 0;
250             }
251             cprof->intentStr = g_strdup( value );
253             if ( value ) {
254                 if ( strcmp( value, "auto" ) == 0 ) {
255                     cprof->rendering_intent = RENDERING_INTENT_AUTO;
256                 } else if ( strcmp( value, "perceptual" ) == 0 ) {
257                     cprof->rendering_intent = RENDERING_INTENT_PERCEPTUAL;
258                 } else if ( strcmp( value, "relative-colorimetric" ) == 0 ) {
259                     cprof->rendering_intent = RENDERING_INTENT_RELATIVE_COLORIMETRIC;
260                 } else if ( strcmp( value, "saturation" ) == 0 ) {
261                     cprof->rendering_intent = RENDERING_INTENT_SATURATION;
262                 } else if ( strcmp( value, "absolute-colorimetric" ) == 0 ) {
263                     cprof->rendering_intent = RENDERING_INTENT_ABSOLUTE_COLORIMETRIC;
264                 } else {
265                     cprof->rendering_intent = RENDERING_INTENT_UNKNOWN;
266                 }
267             } else {
268                 cprof->rendering_intent = RENDERING_INTENT_UNKNOWN;
269             }
271             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
272             break;
274         default:
275             if (((SPObjectClass *) cprof_parent_class)->set) {
276                 (* ((SPObjectClass *) cprof_parent_class)->set)(object, key, value);
277             }
278             break;
279     }
283 /**
284  * Callback: write attributes to associated repr.
285  */
286 static Inkscape::XML::Node* Inkscape::colorprofile_write( SPObject *object, Inkscape::XML::Node *repr, guint flags )
288     ColorProfile *cprof = COLORPROFILE(object);
290     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
291         Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
292         repr = xml_doc->createElement("svg:color-profile");
293     }
295     if ( (flags & SP_OBJECT_WRITE_ALL) || cprof->href ) {
296         repr->setAttribute( "xlink:href", cprof->href );
297     }
299     if ( (flags & SP_OBJECT_WRITE_ALL) || cprof->local ) {
300         repr->setAttribute( "local", cprof->local );
301     }
303     if ( (flags & SP_OBJECT_WRITE_ALL) || cprof->name ) {
304         repr->setAttribute( "name", cprof->name );
305     }
307     if ( (flags & SP_OBJECT_WRITE_ALL) || cprof->intentStr ) {
308         repr->setAttribute( "rendering-intent", cprof->intentStr );
309     }
311     if (((SPObjectClass *) cprof_parent_class)->write) {
312         (* ((SPObjectClass *) cprof_parent_class)->write)(object, repr, flags);
313     }
315     return repr;
319 #if ENABLE_LCMS
322 static SPObject* bruteFind( SPObject* curr, gchar* const name )
324     SPObject* result = 0;
326     if ( curr ) {
327         if ( IS_COLORPROFILE(curr) ) {
328             ColorProfile* prof = COLORPROFILE(curr);
329             if ( prof ) {
330                 if ( prof->name && (strcmp(prof->name, name) == 0) ) {
331                     result = curr;
332                 }
333             }
334         } else {
335             if ( curr->hasChildren() ) {
336                 SPObject* child = curr->firstChild();
337                 while ( child && !result ) {
338                     result = bruteFind( child, name );
339                     if ( !result ) {
340                         child = child->next;
341                     }
342                 };
343             }
344         }
345     }
347     return result;
350 cmsHPROFILE Inkscape::colorprofile_get_handle( SPDocument* document, guint* intent, gchar* const name )
352     cmsHPROFILE prof = 0;
354     SPObject* root = SP_DOCUMENT_ROOT(document);
355     SPObject* thing = bruteFind( root, name );
356     if ( thing ) {
357         prof = COLORPROFILE(thing)->profHandle;
358     }
360     if ( intent ) {
361         *intent = thing ? COLORPROFILE(thing)->rendering_intent : (guint)RENDERING_INTENT_UNKNOWN;
362     }
364 #ifdef DEBUG_LCMS
365     DEBUG_MESSAGE( lcmsThree, "<color-profile> queried for profile of '%s'. Returning %p with intent of %d", name, prof, (intent? *intent:0) );
366 #endif // DEBUG_LCMS
368     return prof;
370 #endif // ENABLE_LCMS
372 /*
373   Local Variables:
374   mode:c++
375   c-file-style:"stroustrup"
376   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
377   indent-tabs-mode:nil
378   fill-column:99
379   End:
380 */
381 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :