1 /* === S Y N F I G ========================================================= */
2 /*!	\file savecanvas.cpp
3 **	\brief Writeme
4 **
5 **	$Id$
6 **
7 **	\legal
8 **	Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **	Copyright (c) 2007, 2008 Chris Moore
10 **  Copyright (c) 2011, 2012 Carlos López
11 **
12 **	This package is free software; you can redistribute it and/or
13 **	modify it under the terms of the GNU General Public License as
14 **	published by the Free Software Foundation; either version 2 of
15 **	the License, or (at your option) any later version.
16 **
17 **	This package is distributed in the hope that it will be useful,
18 **	but WITHOUT ANY WARRANTY; without even the implied warranty of
19 **	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 **	General Public License for more details.
21 **	\endlegal
22 */
23 /* ========================================================================= */
24 
25 /* === H E A D E R S ======================================================= */
26 
27 #ifdef USING_PCH
28 #	include "pch.h"
29 #else
30 #ifdef HAVE_CONFIG_H
31 #	include <config.h>
32 #endif
33 
34 #ifdef HAVE_SYS_ERRNO_H
35 #	include <sys/errno.h>
36 #endif
37 
38 #include "savecanvas.h"
39 #include "general.h"
40 #include <synfig/localization.h>
41 #include "valuenode.h"
42 #include "valuenode_registry.h"
43 #include "valuenodes/valuenode_animated.h"
44 #include "valuenodes/valuenode_const.h"
45 #include "valuenodes/valuenode_linear.h"
46 #include "valuenodes/valuenode_staticlist.h"
47 #include "valuenodes/valuenode_dynamiclist.h"
48 #include "valuenodes/valuenode_reference.h"
49 #include "valuenodes/valuenode_subtract.h"
50 #include "valuenodes/valuenode_bline.h"
51 #include "valuenodes/valuenode_bone.h"
52 #include "valuenodes/valuenode_wplist.h"
53 #include "valuenodes/valuenode_dilist.h"
54 #include "dashitem.h"
55 #include "time.h"
56 #include "keyframe.h"
57 #include "layer.h"
58 #include "string.h"
59 #include "paramdesc.h"
60 #include "weightedvalue.h"
61 #include "pair.h"
62 #include "segment.h"
63 #include "transformation.h"
64 
65 #include "zstreambuf.h"
66 #include "importer.h"
67 #include "cairoimporter.h"
68 
69 #include <libxml++/libxml++.h>
70 #include <ETL/stringf>
71 #include "gradient.h"
72 #include <errno.h>
73 
74 extern "C" {
75 #include <libxml/tree.h>
76 }
77 
78 #endif
79 
80 /* === U S I N G =========================================================== */
81 
82 using namespace std;
83 using namespace etl;
84 using namespace synfig;
85 
86 /* === M A C R O S ========================================================= */
87 
88 #define COLOR_VALUE_TYPE_FORMAT		"%f"
89 #define	VECTOR_VALUE_TYPE_FORMAT	"%0.10f"
90 #define	TIME_TYPE_FORMAT			"%0.3f"
91 #define	VIEW_BOX_FORMAT				"%f %f %f %f"
92 
93 /* === G L O B A L S ======================================================= */
94 
95 ReleaseVersion save_canvas_version = ReleaseVersion(RELEASE_VERSION_END-1);
96 int valuenode_too_new_count;
97 save_canvas_external_file_callback_t save_canvas_external_file_callback = NULL;
98 void *save_canvas_external_file_user_data = NULL;
99 
100 /* === P R O C E D U R E S ================================================= */
101 
102 xmlpp::Element* encode_canvas(xmlpp::Element* root,Canvas::ConstHandle canvas);
103 xmlpp::Element* encode_value_node(xmlpp::Element* root,ValueNode::ConstHandle value_node,Canvas::ConstHandle canvas);
104 xmlpp::Element* encode_value_node_bone(xmlpp::Element* root,ValueNode::ConstHandle value_node,Canvas::ConstHandle canvas);
105 xmlpp::Element* encode_value_node_bone_id(xmlpp::Element* root,ValueNode::ConstHandle value_node,Canvas::ConstHandle canvas);
106 
encode_keyframe(xmlpp::Element * root,const Keyframe & kf,float fps)107 xmlpp::Element* encode_keyframe(xmlpp::Element* root,const Keyframe &kf, float fps)
108 {
109 	root->set_name("keyframe");
110  	root->set_attribute("time",kf.get_time().get_string(fps));
111 	if(!kf.get_description().empty())
112 		root->set_child_text(kf.get_description());
113 	root->set_attribute("active", kf.active()?"true":"false");
114 	return root;
115 }
116 
encode_interpolation(xmlpp::Element * root,Interpolation value,String attribute)117 xmlpp::Element* encode_interpolation(xmlpp::Element* root,Interpolation value,String attribute)
118 {
119 	if (value!=INTERPOLATION_UNDEFINED)
120 	{
121 		switch(value)
122 		{
123 		case INTERPOLATION_HALT:
124 			root->set_attribute(attribute,"halt");
125 			break;
126 		case INTERPOLATION_LINEAR:
127 			root->set_attribute(attribute,"linear");
128 			break;
129 		case INTERPOLATION_MANUAL:
130 			root->set_attribute(attribute,"manual");
131 			break;
132 		case INTERPOLATION_CONSTANT:
133 			root->set_attribute(attribute,"constant");
134 			break;
135 		case INTERPOLATION_TCB:
136 			root->set_attribute(attribute,"auto");
137 			break;
138 		case INTERPOLATION_CLAMPED:
139 			root->set_attribute(attribute,"clamped");
140 			break;
141 		default:
142 			error("Unknown waypoint type for \""+attribute+"\" attribute");
143 		}
144 	}
145 	return root;
146 }
147 
encode_static(xmlpp::Element * root,bool s)148 xmlpp::Element* encode_static(xmlpp::Element* root,bool s)
149 {
150 	if(s)
151 		root->set_attribute("static", s?"true":"false");
152 	return root;
153 }
154 
155 
encode_real(xmlpp::Element * root,Real v)156 xmlpp::Element* encode_real(xmlpp::Element* root,Real v)
157 {
158 	root->set_name("real");
159 	root->set_attribute("value",strprintf(VECTOR_VALUE_TYPE_FORMAT,v));
160 	return root;
161 }
162 
encode_time(xmlpp::Element * root,Time t)163 xmlpp::Element* encode_time(xmlpp::Element* root,Time t)
164 {
165 	root->set_name("time");
166 	root->set_attribute("value",t.get_string());
167 	return root;
168 }
169 
encode_integer(xmlpp::Element * root,int i)170 xmlpp::Element* encode_integer(xmlpp::Element* root,int i)
171 {
172 	root->set_name("integer");
173 	root->set_attribute("value",strprintf("%i",i));
174 	return root;
175 }
176 
encode_bool(xmlpp::Element * root,bool b)177 xmlpp::Element* encode_bool(xmlpp::Element* root, bool b)
178 {
179 	root->set_name("bool");
180 	root->set_attribute("value",b?"true":"false");
181 	return root;
182 }
183 
encode_string(xmlpp::Element * root,const String & str)184 xmlpp::Element* encode_string(xmlpp::Element* root,const String &str)
185 {
186 	root->set_name("string");
187 	root->set_child_text(str);
188 	return root;
189 }
190 
encode_vector(xmlpp::Element * root,Vector vect)191 xmlpp::Element* encode_vector(xmlpp::Element* root,Vector vect)
192 {
193 	root->set_name("vector");
194 	root->add_child("x")->set_child_text(strprintf(VECTOR_VALUE_TYPE_FORMAT,(float)vect[0]));
195 	root->add_child("y")->set_child_text(strprintf(VECTOR_VALUE_TYPE_FORMAT,(float)vect[1]));
196 	return root;
197 }
198 
encode_color(xmlpp::Element * root,Color color)199 xmlpp::Element* encode_color(xmlpp::Element* root,Color color)
200 {
201 	root->set_name("color");
202 	root->add_child("r")->set_child_text(strprintf(COLOR_VALUE_TYPE_FORMAT,(float)color.get_r()));
203 	root->add_child("g")->set_child_text(strprintf(COLOR_VALUE_TYPE_FORMAT,(float)color.get_g()));
204 	root->add_child("b")->set_child_text(strprintf(COLOR_VALUE_TYPE_FORMAT,(float)color.get_b()));
205 	root->add_child("a")->set_child_text(strprintf(COLOR_VALUE_TYPE_FORMAT,(float)color.get_a()));
206 	return root;
207 }
208 
encode_angle(xmlpp::Element * root,Angle theta)209 xmlpp::Element* encode_angle(xmlpp::Element* root,Angle theta)
210 {
211 	root->set_name("angle");
212 	root->set_attribute("value",strprintf("%f",(float)Angle::deg(theta).get()));
213 	return root;
214 }
215 
encode_segment(xmlpp::Element * root,Segment seg)216 xmlpp::Element* encode_segment(xmlpp::Element* root,Segment seg)
217 {
218 	root->set_name("segment");
219 	encode_vector(root->add_child("p1")->add_child("vector"),seg.p1);
220 	encode_vector(root->add_child("t1")->add_child("vector"),seg.t1);
221 	encode_vector(root->add_child("p2")->add_child("vector"),seg.p2);
222 	encode_vector(root->add_child("t2")->add_child("vector"),seg.t2);
223 	return root;
224 }
225 
encode_bline_point(xmlpp::Element * root,BLinePoint bline_point)226 xmlpp::Element* encode_bline_point(xmlpp::Element* root,BLinePoint bline_point)
227 {
228 	root->set_name(type_bline_point.description.name);
229 
230 	encode_vector(root->add_child("vertex")->add_child("vector"),bline_point.get_vertex());
231 	encode_vector(root->add_child("t1")->add_child("vector"),bline_point.get_tangent1());
232 
233 	if(bline_point.get_split_tangent_both())
234 		encode_vector(root->add_child("t2")->add_child("vector"),bline_point.get_tangent2());
235 
236 	encode_real(root->add_child("width")->add_child("real"),bline_point.get_width());
237 	encode_real(root->add_child("origin")->add_child("real"),bline_point.get_origin());
238 	return root;
239 }
240 
encode_width_point(xmlpp::Element * root,WidthPoint width_point)241 xmlpp::Element* encode_width_point(xmlpp::Element* root,WidthPoint width_point)
242 {
243 	root->set_name(type_width_point.description.name);
244 	encode_real(root->add_child("position")->add_child("real"),width_point.get_position());
245 	encode_real(root->add_child("width")->add_child("real"),width_point.get_width());
246 	encode_integer(root->add_child("side_before")->add_child("integer"),width_point.get_side_type_before());
247 	encode_integer(root->add_child("side_after")->add_child("integer"),width_point.get_side_type_after());
248 	return root;
249 }
250 
encode_dash_item(xmlpp::Element * root,DashItem dash_item)251 xmlpp::Element* encode_dash_item(xmlpp::Element* root, DashItem dash_item)
252 {
253 	root->set_name(type_dash_item.description.name);
254 	encode_real(root->add_child("offset")->add_child("real"),dash_item.get_offset());
255 	encode_real(root->add_child("length")->add_child("real"),dash_item.get_length());
256 	encode_integer(root->add_child("side_before")->add_child("integer"),dash_item.get_side_type_before());
257 	encode_integer(root->add_child("side_after")->add_child("integer"),dash_item.get_side_type_after());
258 	return root;
259 }
260 
encode_gradient(xmlpp::Element * root,Gradient x)261 xmlpp::Element* encode_gradient(xmlpp::Element* root,Gradient x)
262 {
263 	root->set_name("gradient");
264 	Gradient::const_iterator iter;
265 	x.sort();
266 	for(iter=x.begin();iter!=x.end();iter++)
267 	{
268 		xmlpp::Element *cpoint(encode_color(root->add_child("color"),iter->color));
269 		cpoint->set_attribute("pos",strprintf("%f",iter->pos));
270 	}
271 	return root;
272 }
273 
274 
275 xmlpp::Element* encode_value(xmlpp::Element* root,const ValueBase &data,Canvas::ConstHandle canvas=0);
276 
encode_list(xmlpp::Element * root,std::vector<ValueBase> list,Canvas::ConstHandle canvas=0)277 xmlpp::Element* encode_list(xmlpp::Element* root,std::vector<ValueBase> list, Canvas::ConstHandle canvas=0)
278 {
279 	root->set_name("list");
280 
281 	while(!list.empty())
282 	{
283 		encode_value(root->add_child("value"),list.front(),canvas);
284 		list.erase(list.begin());
285 	}
286 
287 	return root;
288 }
289 
encode_transformation(xmlpp::Element * root,const Transformation & transformation)290 xmlpp::Element* encode_transformation(xmlpp::Element* root,const Transformation &transformation)
291 {
292 	root->set_name("transformation");
293 	encode_vector(root->add_child("offset")->add_child("vector"),transformation.offset);
294 	encode_angle(root->add_child("angle")->add_child("angle"),transformation.angle);
295 	encode_angle(root->add_child("skew_angle")->add_child("angle"),transformation.skew_angle);
296 	encode_vector(root->add_child("scale")->add_child("vector"),transformation.scale);
297 	return root;
298 }
299 
encode_weighted_value(xmlpp::Element * root,types_namespace::TypeWeightedValueBase & type,const ValueBase & data,Canvas::ConstHandle canvas)300 xmlpp::Element* encode_weighted_value(xmlpp::Element* root,types_namespace::TypeWeightedValueBase &type, const ValueBase &data,Canvas::ConstHandle canvas)
301 {
302 	root->set_name(type.description.name);
303 	encode_real(root->add_child("weight")->add_child("real"), type.extract_weight(data));
304 	encode_value(root->add_child("value")->add_child("value"), type.extract_value(data), canvas);
305 	return root;
306 }
307 
encode_pair(xmlpp::Element * root,types_namespace::TypePairBase & type,const ValueBase & data,Canvas::ConstHandle canvas)308 xmlpp::Element* encode_pair(xmlpp::Element* root,types_namespace::TypePairBase &type, const ValueBase &data,Canvas::ConstHandle canvas)
309 {
310 	root->set_name(type.description.name);
311 	encode_value(root->add_child("first")->add_child("value"), type.extract_first(data), canvas);
312 	encode_value(root->add_child("second")->add_child("value"), type.extract_second(data), canvas);
313 	return root;
314 }
315 
encode_value(xmlpp::Element * root,const ValueBase & data,Canvas::ConstHandle canvas)316 xmlpp::Element* encode_value(xmlpp::Element* root,const ValueBase &data,Canvas::ConstHandle canvas)
317 {
318 	if (getenv("SYNFIG_DEBUG_SAVE_CANVAS")) printf("%s:%d encode_value (type %s)\n", __FILE__, __LINE__, data.get_type().description.name.c_str());
319 	Type &type(data.get_type());
320 	if (type == type_real)
321 	{
322 		encode_real(root,data.get(Real()));
323 		encode_static(root, data.get_static());
324 		encode_interpolation(root, data.get_interpolation(), "interpolation");
325 		return root;
326 	}
327 	if (type == type_time)
328 	{
329 		encode_time(root,data.get(Time()));
330 		encode_static(root, data.get_static());
331 		encode_interpolation(root, data.get_interpolation(), "interpolation");
332 		return root;
333 	}
334 	if (type == type_integer)
335 	{
336 		encode_integer(root,data.get(int()));
337 		encode_static(root, data.get_static());
338 		encode_interpolation(root, data.get_interpolation(), "interpolation");
339 		return root;
340 	}
341 	if (type == type_color)
342 	{
343 		encode_color(root,data.get(Color()));
344 		encode_static(root, data.get_static());
345 		encode_interpolation(root, data.get_interpolation(), "interpolation");
346 		return root;
347 	}
348 	if (type == type_vector)
349 	{
350 		encode_vector(root,data.get(Vector()));
351 		encode_static(root, data.get_static());
352 		encode_interpolation(root, data.get_interpolation(), "interpolation");
353 		return root;
354 	}
355 	if (type == type_angle)
356 	{
357 		encode_angle(root,data.get(Angle()));
358 		encode_static(root, data.get_static());
359 		encode_interpolation(root, data.get_interpolation(), "interpolation");
360 		return root;
361 	}
362 	if (type == type_bool)
363 	{
364 		encode_bool(root,data.get(bool()));
365 		encode_static(root, data.get_static());
366 		encode_interpolation(root, data.get_interpolation(), "interpolation");
367 		return root;
368 	}
369 	if (type == type_string)
370 	{
371 		encode_string(root,data.get(String()));
372 		encode_static(root, data.get_static());
373 		encode_interpolation(root, data.get_interpolation(), "interpolation");
374 		return root;
375 	}
376 	if (type == type_segment)
377 	{
378 		encode_segment(root,data.get(Segment()));
379 		encode_static(root, data.get_static());
380 		encode_interpolation(root, data.get_interpolation(), "interpolation");
381 		return root;
382 	}
383 	if (type == type_bline_point)
384 		return encode_bline_point(root,data.get(BLinePoint()));
385 	if (type == type_width_point)
386 		return encode_width_point(root,data.get(WidthPoint()));
387 	if (type == type_dash_item)
388 		return encode_dash_item(root,data.get(DashItem()));
389 	if (type == type_gradient)
390 	{
391 		encode_gradient(root,data.get(Gradient()));
392 		encode_static(root, data.get_static());
393 		encode_interpolation(root, data.get_interpolation(), "interpolation");
394 		return root;
395 	}
396 	if (type == type_transformation)
397 	{
398 		encode_transformation(root,data.get(Transformation()));
399 		encode_static(root, data.get_static());
400 		encode_interpolation(root, data.get_interpolation(), "interpolation");
401 		return root;
402 	}
403 	if (type == type_list)
404 		return encode_list(root,data.get_list(),canvas);
405 	if (type == type_canvas)
406 	{
407 		return encode_canvas(root,data.get(Canvas::Handle()).get());
408 		//encode_static(root, data.get_static());
409 	}
410 	if (type == type_bone_valuenode)
411 	{
412 		if (!canvas)
413 		{
414 			printf("%s:%d ------------------------------------------------------------------------\n", __FILE__, __LINE__);
415 			printf("%s:%d zero canvas - please fix - report\n", __FILE__, __LINE__);
416 			printf("%s:%d ------------------------------------------------------------------------\n", __FILE__, __LINE__);
417 		}
418 		root = encode_value_node_bone_id(root,data.get(ValueNode_Bone::Handle()).get(),canvas);
419 		root->set_name("bone_valuenode");
420 		return root;
421 	}
422 	if (dynamic_cast<types_namespace::TypeWeightedValueBase*>(&type) != NULL)
423 	{
424 		encode_weighted_value(root, *dynamic_cast<types_namespace::TypeWeightedValueBase*>(&type), data, canvas);
425 		encode_static(root, data.get_static());
426 		encode_interpolation(root, data.get_interpolation(), "interpolation");
427 		return root;
428 	}
429 	if (dynamic_cast<types_namespace::TypePairBase*>(&type) != NULL)
430 	{
431 		encode_pair(root, *dynamic_cast<types_namespace::TypePairBase*>(&type), data, canvas);
432 		encode_static(root, data.get_static());
433 		encode_interpolation(root, data.get_interpolation(), "interpolation");
434 		return root;
435 	}
436 	if (type == type_nil)
437 	{
438 		synfig::error("Encountered NIL ValueBase");
439 		root->set_name("nil");
440 		return root;
441 	}
442 
443 	synfig::error(strprintf(_("Unknown value(%s), cannot create XML representation!"), data.get_type().description.local_name.c_str()));
444 	root->set_name("nil");
445 	return root;
446 }
447 
encode_animated(xmlpp::Element * root,ValueNode_Animated::ConstHandle value_node,Canvas::ConstHandle canvas=0)448 xmlpp::Element* encode_animated(xmlpp::Element* root,ValueNode_Animated::ConstHandle value_node,Canvas::ConstHandle canvas=0)
449 {
450 	assert(value_node);
451 	root->set_name("animated");
452 
453 	root->set_attribute("type",value_node->get_type().description.name);
454 
455 	const ValueNode_Animated::WaypointList &waypoint_list=value_node->waypoint_list();
456 	ValueNode_Animated::WaypointList::const_iterator iter;
457 
458 	encode_interpolation(root, value_node->get_interpolation(), "interpolation");
459 
460 	for(iter=waypoint_list.begin();iter!=waypoint_list.end();++iter)
461 	{
462 		xmlpp::Element *waypoint_node=root->add_child("waypoint");
463 		waypoint_node->set_attribute("time",iter->get_time().get_string());
464 
465 		if(iter->get_value_node()->is_exported())
466 			waypoint_node->set_attribute("use",iter->get_value_node()->get_relative_id(canvas));
467 		else {
468 			ValueNode::ConstHandle value_node = iter->get_value_node();
469 			if(ValueNode_Const::ConstHandle::cast_dynamic(value_node))
470 			{
471 				const ValueBase data = ValueNode_Const::ConstHandle::cast_dynamic(value_node)->get_value();
472 				if (data.get_type() == type_canvas)
473 					waypoint_node->set_attribute("use",data.get(Canvas::Handle()).get()->get_relative_id(canvas));
474 				else
475 					encode_value_node(waypoint_node->add_child("value_node"),iter->get_value_node(),canvas);
476 			}
477 			else
478 				encode_value_node(waypoint_node->add_child("value_node"),iter->get_value_node(),canvas);
479 		}
480 
481 		if (iter->get_before()!=INTERPOLATION_UNDEFINED)
482 			encode_interpolation(waypoint_node,iter->get_before(),"before");
483 		else
484 			error("Unknown waypoint type for \"before\" attribute");
485 
486 		if (iter->get_after()!=INTERPOLATION_UNDEFINED)
487 			encode_interpolation(waypoint_node,iter->get_after(),"after");
488 		else
489 			error("Unknown waypoint type for \"after\" attribute");
490 
491 		if(iter->get_tension()!=0.0)
492 			waypoint_node->set_attribute("tension",strprintf("%f",iter->get_tension()));
493 		if(iter->get_temporal_tension()!=0.0)
494 			waypoint_node->set_attribute("temporal-tension",strprintf("%f",iter->get_temporal_tension()));
495 		if(iter->get_continuity()!=0.0)
496 			waypoint_node->set_attribute("continuity",strprintf("%f",iter->get_continuity()));
497 		if(iter->get_bias()!=0.0)
498 			waypoint_node->set_attribute("bias",strprintf("%f",iter->get_bias()));
499 
500 	}
501 
502 	return root;
503 }
504 
505 
encode_subtract(xmlpp::Element * root,ValueNode_Subtract::ConstHandle value_node,Canvas::ConstHandle canvas=0)506 xmlpp::Element* encode_subtract(xmlpp::Element* root,ValueNode_Subtract::ConstHandle value_node,Canvas::ConstHandle canvas=0)
507 {
508 	assert(value_node);
509 	root->set_name("subtract");
510 
511 	ValueNode::ConstHandle lhs=value_node->get_lhs();
512 	ValueNode::ConstHandle rhs=value_node->get_rhs();
513 	ValueNode::ConstHandle scalar=value_node->get_scalar();
514 
515 	assert(lhs);
516 	assert(rhs);
517 
518 	root->set_attribute("type",value_node->get_type().description.name);
519 
520 	if(lhs==rhs)
521 		warning("LHS is equal to RHS, this <subtract> will always be zero!");
522 
523 	//if(value_node->get_scalar()!=1)
524 	//	root->set_attribute("scalar",strprintf(VECTOR_VALUE_TYPE_FORMAT,value_node->get_scalar()));
525 
526 	if(!scalar->get_id().empty())
527 		root->set_attribute("scalar",scalar->get_relative_id(canvas));
528 	else
529 		encode_value_node(root->add_child("scalar")->add_child("value_node"),scalar,canvas);
530 
531 	if(!lhs->get_id().empty())
532 		root->set_attribute("lhs",lhs->get_relative_id(canvas));
533 	else
534 		encode_value_node(root->add_child("lhs")->add_child("value_node"),lhs,canvas);
535 
536 	if(!rhs->get_id().empty())
537 		root->set_attribute("rhs",rhs->get_relative_id(canvas));
538 	else
539 		encode_value_node(root->add_child("rhs")->add_child("value_node"),rhs,canvas);
540 
541 	return root;
542 }
543 
encode_static_list(xmlpp::Element * root,ValueNode_StaticList::ConstHandle value_node,Canvas::ConstHandle canvas=0)544 xmlpp::Element* encode_static_list(xmlpp::Element* root,ValueNode_StaticList::ConstHandle value_node,Canvas::ConstHandle canvas=0)
545 {
546 	if (getenv("SYNFIG_DEBUG_SAVE_CANVAS")) printf("%s:%d encode_static_list %s\n", __FILE__, __LINE__, value_node->get_string().c_str());
547 	assert(value_node);
548 
549 	root->set_name(value_node->get_name());
550 
551 	root->set_attribute("type",value_node->get_contained_type().description.name);
552 
553 	vector<ValueNode::RHandle>::const_iterator iter;
554 
555 	for(iter=value_node->list.begin();iter!=value_node->list.end();++iter)
556 	{
557 		xmlpp::Element	*entry_node=root->add_child("entry");
558 		assert(*iter);
559 		if(!(*iter)->get_id().empty())
560 			entry_node->set_attribute("use",(*iter)->get_relative_id(canvas));
561 		else
562 		{
563 			if (getenv("SYNFIG_DEBUG_SAVE_CANVAS")) printf("%s:%d encode entry %s\n", __FILE__, __LINE__, (*iter)->get_string().c_str());
564 			encode_value_node(entry_node->add_child("value_node"),*iter,canvas);
565 		}
566 	}
567 
568 	if (getenv("SYNFIG_DEBUG_SAVE_CANVAS")) printf("%s:%d encode_static_list %s done\n", __FILE__, __LINE__, value_node->get_string().c_str());
569 	return root;
570 }
571 
encode_dynamic_list(xmlpp::Element * root,ValueNode_DynamicList::ConstHandle value_node,Canvas::ConstHandle canvas=0)572 xmlpp::Element* encode_dynamic_list(xmlpp::Element* root,ValueNode_DynamicList::ConstHandle value_node,Canvas::ConstHandle canvas=0)
573 {
574 	assert(value_node);
575 	const float fps(canvas?canvas->rend_desc().get_frame_rate():0);
576 
577 	root->set_name(value_node->get_name());
578 
579 	root->set_attribute("type",value_node->get_contained_type().description.name);
580 
581 	vector<ValueNode_DynamicList::ListEntry>::const_iterator iter;
582 
583 	ValueNode_BLine::ConstHandle bline_value_node(ValueNode_BLine::ConstHandle::cast_dynamic(value_node));
584 	ValueNode_WPList::ConstHandle wplist_value_node(ValueNode_WPList::ConstHandle::cast_dynamic(value_node));
585 	ValueNode_DIList::ConstHandle dilist_value_node(ValueNode_DIList::ConstHandle::cast_dynamic(value_node));
586 
587 	if(bline_value_node)
588 	{
589 		if(bline_value_node->get_loop())
590 			root->set_attribute("loop","true");
591 		else
592 			root->set_attribute("loop","false");
593 	}
594 	if(wplist_value_node)
595 	{
596 		if(wplist_value_node->get_loop())
597 			root->set_attribute("loop","true");
598 		else
599 			root->set_attribute("loop","false");
600 	}
601 	if(dilist_value_node)
602 	{
603 		if(dilist_value_node->get_loop())
604 			root->set_attribute("loop","true");
605 		else
606 			root->set_attribute("loop","false");
607 	}
608 
609 	for(iter=value_node->list.begin();iter!=value_node->list.end();++iter)
610 	{
611 		xmlpp::Element	*entry_node=root->add_child("entry");
612 		assert(iter->value_node);
613 		if(!iter->value_node->get_id().empty())
614 			entry_node->set_attribute("use",iter->value_node->get_relative_id(canvas));
615 		else
616 			encode_value_node(entry_node->add_child("value_node"),iter->value_node,canvas);
617 
618 		// process waypoints
619 		{
620 			typedef synfig::ValueNode_DynamicList::ListEntry::ActivepointList ActivepointList;
621 			String begin_sequence;
622 			String end_sequence;
623 
624 			const ActivepointList& timing_info(iter->timing_info);
625 			ActivepointList::const_iterator entry_iter;
626 
627 			for(entry_iter=timing_info.begin();entry_iter!=timing_info.end();++entry_iter)
628 				if(entry_iter->state==true)
629 				{
630 					if(entry_iter->priority)
631 						begin_sequence+=strprintf("p%d ",entry_iter->priority);
632 					begin_sequence+=entry_iter->time.get_string(fps)+", ";
633 				}
634 				else
635 				{
636 					if(entry_iter->priority)
637 						end_sequence+=strprintf("p%d ",entry_iter->priority);
638 					end_sequence+=entry_iter->time.get_string(fps)+", ";
639 				}
640 
641 			// If this is just a plane-jane vanilla entry,
642 			// then don't bother with begins and ends
643 			if(end_sequence.empty() && begin_sequence=="SOT, ")
644 				begin_sequence.clear();
645 
646 			if(!begin_sequence.empty())
647 			{
648 				// Remove the last ", " stuff
649 				begin_sequence=String(begin_sequence.begin(),begin_sequence.end()-2);
650 				// Add the attribute
651 				entry_node->set_attribute("on",begin_sequence);
652 			}
653 
654 			if(!end_sequence.empty())
655 			{
656 				// Remove the last ", " stuff
657 				end_sequence=String(end_sequence.begin(),end_sequence.end()-2);
658 				// Add the attribute
659 				entry_node->set_attribute("off",end_sequence);
660 			}
661 		}
662 	}
663 
664 	return root;
665 }
666 
667 // Generic linkable data node entry
encode_linkable_value_node(xmlpp::Element * root,LinkableValueNode::ConstHandle value_node,Canvas::ConstHandle canvas=0)668 xmlpp::Element* encode_linkable_value_node(xmlpp::Element* root,LinkableValueNode::ConstHandle value_node,Canvas::ConstHandle canvas=0)
669 {
670 	if (getenv("SYNFIG_DEBUG_SAVE_CANVAS")) printf("%s:%d encode_linkable_value_node %s\n", __FILE__, __LINE__, value_node->get_string().c_str());
671 	assert(value_node);
672 
673 	String name(value_node->get_name());
674 	ReleaseVersion saving_version(get_file_version());
675 	ReleaseVersion feature_version(ValueNodeRegistry::book()[name].release_version);
676 
677 	if (saving_version < feature_version)
678 	{
679 		valuenode_too_new_count++;
680 		warning("can't save <%s> valuenodes in this old file format version", name.c_str());
681 
682 		ValueBase value((*value_node)(0));
683 		encode_value(root,value,canvas);
684 
685 		return root;
686 	}
687 
688 	root->set_name(name);
689 
690 	root->set_attribute("type",value_node->get_type().description.name);
691 
692 	int i;
693 	synfig::ParamVocab child_vocab(value_node->get_children_vocab());
694 	synfig::ParamVocab::iterator iter(child_vocab.begin());
695 	for(i=0;i<value_node->link_count();i++, iter++)
696 	{
697 		// printf("saving link %d : %s\n", i, value_node->link_local_name(i).c_str());
698 		ValueNode::ConstHandle link=value_node->get_link(i).constant();
699 		if(!link)
700 			throw runtime_error("Bad link");
701 		if(link->is_exported())
702 			root->set_attribute(value_node->link_name(i),link->get_relative_id(canvas));
703 		else if(iter->get_critical())
704 		{
705 			if (name == "bone" && value_node->link_name(i) == "parent")
706 			{
707 				if (getenv("SYNFIG_DEBUG_SAVE_CANVAS")) printf("%s:%d saving bone's parent\n", __FILE__, __LINE__);
708 			}
709 			encode_value_node(root->add_child(value_node->link_name(i))->add_child("value_node"),link,canvas);
710 		}
711 	}
712 
713 	if (getenv("SYNFIG_DEBUG_SAVE_CANVAS")) printf("%s:%d encode_linkable_value_node %s done\n", __FILE__, __LINE__, value_node->get_string().c_str());
714 	return root;
715 }
716 
encode_value_node(xmlpp::Element * root,ValueNode::ConstHandle value_node,Canvas::ConstHandle canvas)717 xmlpp::Element* encode_value_node(xmlpp::Element* root,ValueNode::ConstHandle value_node,Canvas::ConstHandle canvas)
718 {
719 	assert(value_node);
720 	if (getenv("SYNFIG_DEBUG_SAVE_CANVAS")) printf("%s:%d encode_value_node %s %s\n", __FILE__, __LINE__, value_node->get_string().c_str(), value_node->get_guid().get_string().c_str());
721 
722 	if(value_node->rcount()>1)
723 		root->set_attribute("guid",(value_node->get_guid()^canvas->get_root()->get_guid()).get_string());
724 
725 	if(ValueNode_Bone::ConstHandle::cast_dynamic(value_node))
726 	{
727 		if (getenv("SYNFIG_DEBUG_SAVE_CANVAS")) printf("%s:%d shortcutting for valuenode_bone\n", __FILE__, __LINE__);
728 		encode_value_node_bone_id(root,ValueNode_Bone::ConstHandle::cast_dynamic(value_node),canvas);
729 	}
730 	else
731 	if(ValueNode_Animated::ConstHandle::cast_dynamic(value_node))
732 		encode_animated(root,ValueNode_Animated::ConstHandle::cast_dynamic(value_node),canvas);
733 	else
734 	if(ValueNode_Subtract::ConstHandle::cast_dynamic(value_node))
735 		encode_subtract(root,ValueNode_Subtract::ConstHandle::cast_dynamic(value_node),canvas);
736 	else
737 	if(ValueNode_StaticList::ConstHandle::cast_dynamic(value_node))
738 		encode_static_list(root,ValueNode_StaticList::ConstHandle::cast_dynamic(value_node),canvas);
739 	else
740 	if(ValueNode_DynamicList::ConstHandle::cast_dynamic(value_node))
741 	{
742 		encode_dynamic_list(root,ValueNode_DynamicList::ConstHandle::cast_dynamic(value_node),canvas);
743 	}
744 	// if it's a ValueNode_Const
745 	else if(ValueNode_Const::ConstHandle::cast_dynamic(value_node))
746 	{
747 		if (getenv("SYNFIG_DEBUG_SAVE_CANVAS")) printf("%s:%d got ValueNode_Const encoding value\n", __FILE__, __LINE__);
748 		// encode its get_value()
749 		encode_value(root,ValueNode_Const::ConstHandle::cast_dynamic(value_node)->get_value(),canvas);
750 	}
751 	else
752 	if(LinkableValueNode::ConstHandle::cast_dynamic(value_node))
753 		encode_linkable_value_node(root,LinkableValueNode::ConstHandle::cast_dynamic(value_node),canvas);
754 	else
755 	{
756 		error(_("Unknown ValueNode Type (%s), cannot create an XML representation"),value_node->get_local_name().c_str());
757 		root->set_name("nil");
758 	}
759 
760 	assert(root);
761 
762 	if(!value_node->get_id().empty())
763 		root->set_attribute("id",value_node->get_id());
764 
765 //	if(ValueNode_Bone::ConstHandle::cast_dynamic(value_node))
766 //		root->set_attribute("guid",value_node->get_guid().get_string());
767 
768 	if (getenv("SYNFIG_DEBUG_SAVE_CANVAS")) printf("%s:%d encode_value_node %s done\n", __FILE__, __LINE__, value_node->get_string().c_str());
769 	return root;
770 }
771 
encode_value_node_bone(xmlpp::Element * root,ValueNode::ConstHandle value_node,Canvas::ConstHandle canvas)772 xmlpp::Element* encode_value_node_bone(xmlpp::Element* root,ValueNode::ConstHandle value_node,Canvas::ConstHandle canvas)
773 {
774 	assert(value_node);
775 	if (getenv("SYNFIG_DEBUG_SAVE_CANVAS")) printf("%s:%d encode_value_node_bone %s %s\n", __FILE__, __LINE__, value_node->get_string().c_str(), value_node->get_guid().get_string().c_str());
776 
777 	if(ValueNode_Bone::ConstHandle::cast_dynamic(value_node))
778 		encode_linkable_value_node(root,LinkableValueNode::ConstHandle::cast_dynamic(value_node),canvas);
779 	else
780 	{
781 		error(_("Unknown ValueNode Type (%s), cannot create an XML representation"),value_node->get_local_name().c_str());
782 		assert(0);
783 		root->set_name("nil");
784 	}
785 
786 	assert(root);
787 
788 	if(!value_node->get_id().empty())
789 		root->set_attribute("id",value_node->get_id());
790 
791 	if(ValueNode_Bone::ConstHandle::cast_dynamic(value_node))
792 		root->set_attribute("guid",(value_node->get_guid()^canvas->get_root()->get_guid()).get_string());
793 
794 	if(value_node->rcount()>1)
795 	{
796 		// ~/notes/synfig/crash-when-saving.txt is an example of the execution reaching this line
797 		printf("%s:%d xxx value_node->rcount() = %d\n", __FILE__, __LINE__, value_node->rcount());
798 		root->set_attribute("guid",(value_node->get_guid()^canvas->get_root()->get_guid()).get_string());
799 	}
800 
801 	if (getenv("SYNFIG_DEBUG_SAVE_CANVAS")) printf("%s:%d encode_value_node %s done\n", __FILE__, __LINE__, value_node->get_string().c_str());
802 	return root;
803 }
804 
encode_value_node_bone_id(xmlpp::Element * root,ValueNode::ConstHandle value_node,Canvas::ConstHandle canvas)805 xmlpp::Element* encode_value_node_bone_id(xmlpp::Element* root,ValueNode::ConstHandle value_node,Canvas::ConstHandle canvas)
806 {
807 	root->set_name("bone");
808 	root->set_attribute("type",type_bone_object.description.name);
809 	if (getenv("SYNFIG_DEBUG_SAVE_CANVAS")) printf("%s:%d encode_value_node_bone_id %s %s\n", __FILE__, __LINE__, value_node->get_string().c_str(), value_node->get_guid().get_string().c_str());
810 	if(!value_node->get_id().empty())
811 		root->set_attribute("id",value_node->get_id());
812 
813 	if(ValueNode_Bone::ConstHandle::cast_dynamic(value_node))
814 	{
815 		if (getenv("SYNFIG_DEBUG_SAVE_CANVAS")) printf("%s:%d bone guid case 1 guid %s\n", __FILE__, __LINE__, value_node->get_guid().get_string().c_str());
816 		root->set_attribute("guid",(value_node->get_guid()^canvas->get_root()->get_guid()).get_string());
817 	}
818 
819 	if(value_node->rcount()>1)
820 	{
821 		printf("%s:%d this happens too\n", __FILE__, __LINE__);
822 		root->set_attribute("guid",(value_node->get_guid()^canvas->get_root()->get_guid()).get_string());
823 	}
824 
825 	return root;
826 }
827 
encode_layer(xmlpp::Element * root,Layer::ConstHandle layer)828 xmlpp::Element* encode_layer(xmlpp::Element* root,Layer::ConstHandle layer)
829 {
830 	root->set_name("layer");
831 
832 	root->set_attribute("type",layer->get_name());
833 	root->set_attribute("active",layer->active()?"true":"false");
834 	root->set_attribute("exclude_from_rendering",layer->get_exclude_from_rendering()?"true":"false");
835 
836 	if(!layer->get_version().empty())
837 		root->set_attribute("version",layer->get_version());
838 	if(!layer->get_description().empty())
839 		root->set_attribute("desc",layer->get_description());
840 	if(!layer->get_group().empty())
841 		root->set_attribute("group",layer->get_group());
842 
843 	Layer::Vocab vocab(layer->get_param_vocab());
844 	Layer::Vocab::const_iterator iter;
845 
846 	const Layer::DynamicParamList &dynamic_param_list=layer->dynamic_param_list();
847 
848 	for(iter=vocab.begin();iter!=vocab.end();++iter)
849 	{
850 		// Handle dynamic parameters
851 		if(dynamic_param_list.count(iter->get_name()))
852 		{
853 			xmlpp::Element *node=root->add_child("param");
854 			node->set_attribute("name",iter->get_name());
855 
856 			handle<const ValueNode> value_node=dynamic_param_list.find(iter->get_name())->second;
857 
858 			// If the valuenode has no ID, then it must be defined in-place
859 			if(value_node->get_id().empty())
860 			{
861 				encode_value_node(node->add_child("value_node"),value_node,layer->get_canvas().constant());
862 			}
863 			else
864 			{
865 				node->set_attribute("use",value_node->get_relative_id(layer->get_canvas()));
866 			}
867 		}
868 		else  // Handle normal parameters
869 		if(iter->get_critical())
870 		{
871 			ValueBase value=layer->get_param(iter->get_name());
872 			if(!value.is_valid())
873 			{
874 				error("Layer doesn't know its own vocabulary -- "+iter->get_name());
875 				continue;
876 			}
877 
878 			if(value.get_type()==type_canvas)
879 			{
880 				// the ->is_inline() below was crashing if the canvas
881 				// contained a PasteCanvas with the default <No Image
882 				// Selected> Canvas setting;  this avoids the crash
883 				if (!value.get(Canvas::LooseHandle()))
884 					continue;
885 
886 				if (!value.get(Canvas::LooseHandle())->is_inline())
887 				{
888 					Canvas::Handle child(value.get(Canvas::LooseHandle()));
889 
890 					if(!value.get(Canvas::Handle()))
891 						continue;
892 					xmlpp::Element *node=root->add_child("param");
893 					node->set_attribute("name",iter->get_name());
894 					node->set_attribute("use",child->get_relative_id(layer->get_canvas()));
895 					if(value.get_static())
896  						node->set_attribute("static", value.get_static()?"true":"false");
897 					continue;
898 				}
899 			}
900 			xmlpp::Element *node=root->add_child("param");
901 			node->set_attribute("name",iter->get_name());
902 
903 			// remember filename param if need
904 			if (save_canvas_external_file_callback != NULL
905 			 && iter->get_name() == "filename"
906 			 && value.get_type() == type_string)
907 			{
908 				std::string filename( value.get(String()) );
909 				std::string ext = filename_extension(filename);
910 				if (!ext.empty()) ext = ext.substr(1); // skip initial '.'
911 				bool registered_in_importer = Importer::book().count(ext) > 0;
912 				bool supports_by_importer = registered_in_importer
913 						                 && Importer::book()[ext].supports_file_system_wrapper;
914 				bool registered_in_cairoimporter = CairoImporter::book().count(ext) > 0;
915 				bool supports_by_cairoimporter = registered_in_cairoimporter
916 						                      && CairoImporter::book()[ext].supports_file_system_wrapper;
917 				bool supports = (supports_by_importer && supports_by_cairoimporter)
918 						     || (supports_by_importer && !registered_in_cairoimporter)
919 						     || (!registered_in_importer && supports_by_cairoimporter);
920 				if (supports)
921 					if (save_canvas_external_file_callback(save_canvas_external_file_user_data, layer, iter->get_name(), filename))
922 						value.set(filename);
923 			}
924 
925 			encode_value(node->add_child("value"),value,layer->get_canvas().constant());
926 		}
927 	}
928 
929 
930 	return root;
931 }
932 
encode_canvas(xmlpp::Element * root,Canvas::ConstHandle canvas)933 xmlpp::Element* encode_canvas(xmlpp::Element* root,Canvas::ConstHandle canvas)
934 {
935 	assert(canvas);
936 	const RendDesc &rend_desc=canvas->rend_desc();
937 	root->set_name("canvas");
938 
939 	if(canvas->is_root())
940 		root->set_attribute("version",canvas->get_version());
941 
942 	if(!canvas->get_id().empty() && !canvas->is_root() && !canvas->is_inline())
943 		root->set_attribute("id",canvas->get_id());
944 
945 	if(!canvas->parent() || canvas->parent()->rend_desc().get_w()!=canvas->rend_desc().get_w())
946 		root->set_attribute("width",strprintf("%d",rend_desc.get_w()));
947 
948 	if(!canvas->parent() || canvas->parent()->rend_desc().get_h()!=canvas->rend_desc().get_h())
949 		root->set_attribute("height",strprintf("%d",rend_desc.get_h()));
950 
951 	if(!canvas->parent() || canvas->parent()->rend_desc().get_x_res()!=canvas->rend_desc().get_x_res())
952 		root->set_attribute("xres",strprintf("%f",rend_desc.get_x_res()));
953 
954 	if(!canvas->parent() || canvas->parent()->rend_desc().get_y_res()!=canvas->rend_desc().get_y_res())
955 		root->set_attribute("yres",strprintf("%f",rend_desc.get_y_res()));
956 
957 
958 	if(!canvas->parent() ||
959 		canvas->parent()->rend_desc().get_tl()!=canvas->rend_desc().get_tl() ||
960 		canvas->parent()->rend_desc().get_br()!=canvas->rend_desc().get_br())
961 	root->set_attribute("view-box",strprintf(VIEW_BOX_FORMAT,
962 		rend_desc.get_tl()[0],
963 		rend_desc.get_tl()[1],
964 		rend_desc.get_br()[0],
965 		rend_desc.get_br()[1])
966 	);
967 
968 	if(!canvas->parent() || canvas->parent()->rend_desc().get_antialias()!=canvas->rend_desc().get_antialias())
969 		root->set_attribute("antialias",strprintf("%d",rend_desc.get_antialias()));
970 
971 	if(!canvas->parent())
972 		root->set_attribute("fps",strprintf(TIME_TYPE_FORMAT,rend_desc.get_frame_rate()));
973 
974 	if(!canvas->parent() || canvas->parent()->rend_desc().get_time_start()!=canvas->rend_desc().get_time_start())
975 		root->set_attribute("begin-time",rend_desc.get_time_start().get_string(rend_desc.get_frame_rate()));
976 
977 	if(!canvas->parent() || canvas->parent()->rend_desc().get_time_end()!=canvas->rend_desc().get_time_end())
978 		root->set_attribute("end-time",rend_desc.get_time_end().get_string(rend_desc.get_frame_rate()));
979 
980 	if(!canvas->is_inline())
981 	{
982 		root->set_attribute("bgcolor",strprintf(VIEW_BOX_FORMAT,
983 			rend_desc.get_bg_color().get_r(),
984 			rend_desc.get_bg_color().get_g(),
985 			rend_desc.get_bg_color().get_b(),
986 			rend_desc.get_bg_color().get_a())
987 		);
988 
989 		if(!canvas->get_name().empty())
990 			root->add_child("name")->set_child_text(canvas->get_name());
991 		if(!canvas->get_description().empty())
992 			root->add_child("desc")->set_child_text(canvas->get_description());
993 		if(!canvas->get_author().empty())
994 			root->add_child("author")->set_child_text(canvas->get_description());
995 
996 		std::list<String> meta_keys(canvas->get_meta_data_keys());
997 		while(!meta_keys.empty())
998 		{
999 			xmlpp::Element* meta_element(root->add_child("meta"));
1000 			meta_element->set_attribute("name",meta_keys.front());
1001 			meta_element->set_attribute("content",canvas->get_meta_data(meta_keys.front()));
1002 			meta_keys.pop_front();
1003 		}
1004 		for(KeyframeList::const_iterator iter=canvas->keyframe_list().begin();iter!=canvas->keyframe_list().end();++iter)
1005 			encode_keyframe(root->add_child("keyframe"),*iter,canvas->rend_desc().get_frame_rate());
1006 	}
1007 
1008 	// Output the <bones> section
1009 	if((!canvas->is_inline() && !ValueNode_Bone::get_bone_map(canvas).empty()))
1010 	{
1011 		xmlpp::Element *node=root->add_child("bones");
1012 
1013 		encode_value_node_bone(node->add_child("value_node"),ValueNode_Bone::get_root_bone(),canvas);
1014 
1015 		ValueNode_Bone::BoneList bone_list(ValueNode_Bone::get_ordered_bones(canvas));
1016 		for(ValueNode_Bone::BoneList::iterator iter=bone_list.begin();iter!=bone_list.end();++iter)
1017 		{
1018 			ValueNode_Bone::Handle bone(*iter);
1019 			encode_value_node_bone(node->add_child("value_node"),bone,canvas);
1020 		}
1021 	}
1022 
1023 	// Output the <defs> section
1024 
1025 	//! \todo check where the parentheses should really go - around the && or the ||?
1026 	// I guess it should be the other way - but then, why would an inline canvas have either exported valuenode or child canvases?  it shouldn't, right?
1027 	// (not an inline canvas)   (has some exported valuenodes)         (has some child canvases)
1028 
1029 	//! If children is not empty (there are exported canvases in the current canvas)
1030 	//! they must be listed in the defs section regardless the result of check the
1031 	//! Value Node list (exported value nodes in the canvas) and if the canvas is
1032 	//! in line or not. Inline canvases cannot have exported canvases inside.
1033 
1034 	if((!canvas->is_inline() && !canvas->value_node_list().empty()) || !canvas->children().empty())
1035 	{
1036 		xmlpp::Element *node=root->add_child("defs");
1037 		const ValueNodeList &value_node_list(canvas->value_node_list());
1038 
1039 		for(ValueNodeList::const_iterator iter=value_node_list.begin();iter!=value_node_list.end();++iter)
1040 		{
1041 			// If the value_node is a constant, then use the shorthand
1042 			if(handle<ValueNode_Const>::cast_dynamic(*iter))
1043 			{
1044 				ValueNode_Const::Handle value_node(ValueNode_Const::Handle::cast_dynamic(*iter));
1045 				reinterpret_cast<xmlpp::Element*>(encode_value(node->add_child("value"),value_node->get_value(),canvas))->set_attribute("id",value_node->get_id());
1046 				continue;
1047 			}
1048 			encode_value_node(node->add_child("value_node"),*iter,canvas);
1049 			// writeme
1050 		}
1051 
1052 		for(Canvas::Children::const_iterator iter=canvas->children().begin();iter!=canvas->children().end();++iter)
1053 		{
1054 			encode_canvas(node->add_child("canvas"),*iter);
1055 		}
1056 	}
1057 
1058 	Canvas::const_reverse_iterator iter;
1059 
1060 	for(iter=canvas->rbegin();iter!=canvas->rend();++iter)
1061 		encode_layer(root->add_child("layer"),*iter);
1062 
1063 	return root;
1064 }
1065 
encode_canvas_toplevel(xmlpp::Element * root,Canvas::ConstHandle canvas)1066 xmlpp::Element* encode_canvas_toplevel(xmlpp::Element* root,Canvas::ConstHandle canvas)
1067 {
1068 	valuenode_too_new_count = 0;
1069 
1070 	xmlpp::Element* ret = encode_canvas(root, canvas);
1071 
1072 	if (valuenode_too_new_count)
1073 		warning("saved %d valuenodes as constant values in old file format\n", valuenode_too_new_count);
1074 
1075 	return ret;
1076 }
1077 
1078 bool
save_canvas(const FileSystem::Identifier & identifier,Canvas::ConstHandle canvas,bool safe)1079 synfig::save_canvas(const FileSystem::Identifier &identifier, Canvas::ConstHandle canvas, bool safe)
1080 {
1081     ChangeLocale change_locale(LC_NUMERIC, "C");
1082 
1083     synfig::String tmp_filename(safe ? identifier.filename+".TMP" : identifier.filename);
1084 
1085 	try
1086 	{
1087 		assert(canvas);
1088 		xmlpp::Document document;
1089 
1090 		encode_canvas_toplevel(document.create_root_node("canvas"),canvas);
1091 
1092 		FileSystem::WriteStream::Handle stream = identifier.file_system->get_write_stream(tmp_filename);
1093 		if (!stream)
1094 		{
1095 			synfig::error("synfig::save_canvas(): Unable to open file for write");
1096 			return false;
1097 		}
1098 
1099 		if (filename_extension(identifier.filename) == ".sifz")
1100 			stream = FileSystem::WriteStream::Handle(new ZWriteStream(stream));
1101 
1102 		document.write_to_stream_formatted(*stream, "UTF-8");
1103 
1104 		// close stream
1105 		stream.reset();
1106 
1107 		if (safe)
1108 		{
1109 			if(!identifier.file_system->file_rename(tmp_filename, identifier.filename))
1110 			{
1111 				synfig::error("synfig::save_canvas(): Unable to rename file to correct filename");
1112 				return false;
1113 			}
1114 		}
1115 	}
1116 	catch(...) { synfig::error("synfig::save_canvas(): Caught unknown exception"); return false; }
1117 
1118 	return true;
1119 }
1120 
1121 String
canvas_to_string(Canvas::ConstHandle canvas)1122 synfig::canvas_to_string(Canvas::ConstHandle canvas)
1123 {
1124     ChangeLocale change_locale(LC_NUMERIC, "C");
1125 	assert(canvas);
1126 
1127 	xmlpp::Document document;
1128 
1129 	encode_canvas_toplevel(document.create_root_node("canvas"),canvas);
1130 
1131 	return document.write_to_string_formatted();
1132 }
1133 
1134 void
set_save_canvas_external_file_callback(save_canvas_external_file_callback_t callback,void * user_data)1135 synfig::set_save_canvas_external_file_callback(save_canvas_external_file_callback_t callback, void *user_data)
1136 {
1137 	save_canvas_external_file_callback = callback;
1138 	save_canvas_external_file_user_data = user_data;
1139 }
1140 
1141 void
set_file_version(ReleaseVersion version)1142 synfig::set_file_version(ReleaseVersion version)
1143 {
1144 	save_canvas_version = version;
1145 }
1146 
1147 ReleaseVersion
get_file_version()1148 synfig::get_file_version()
1149 {
1150 	return save_canvas_version;
1151 }
1152