Not working Quadtree object adding.
[ipdf/code.git] / src / document.cpp
1 #include "document.h"
2 #include "bezier.h"
3 #include "profiler.h"
4 #include <cstdio>
5 #include <fstream>
6
7 #include "../contrib/pugixml-1.4/src/pugixml.cpp"
8 #include "transformationtype.h"
9
10 #include "stb_truetype.h"
11
12 using namespace IPDF;
13 using namespace std;
14
15 //TODO: Make this work for variable sized Reals
16
17 // Loads an std::vector<T> of size num_elements from a file.
18 template<typename T>
19 static void LoadStructVector(FILE *src_file, size_t num_elems, std::vector<T>& dest)
20 {
21         size_t structsread = 0;
22         dest.resize(num_elems);
23         structsread = fread(dest.data(), sizeof(T), num_elems, src_file);
24         if (structsread != num_elems)
25                 Fatal("Only read %u structs (expected %u)!", structsread, num_elems);
26 }
27
28 // Saves an std::vector<T> to a file. Size must be saves separately.
29 template<typename T>
30 static void SaveStructVector(FILE *dst_file, std::vector<T>& src)
31 {
32         size_t written = 0;
33         written = fwrite(src.data(), sizeof(T), src.size(), dst_file);
34         if (written != src.size())
35                 Fatal("Only wrote %u structs (expected %u)!", written, src.size());
36 }
37
38 static void WriteChunkHeader(FILE *dst_file, DocChunkTypes type, uint32_t size)
39 {
40         size_t written = 0;
41         written = fwrite(&type, sizeof(type), 1, dst_file);
42         if (written != 1)
43                 Fatal("Could not write Chunk header! (ID)");
44         written = fwrite(&size, sizeof(size), 1, dst_file);
45         if (written != 1)
46                 Fatal("Could not write Chunk header (size)!");
47 }
48
49 static bool ReadChunkHeader(FILE *src_file, DocChunkTypes& type, uint32_t& size)
50 {
51         if (fread(&type, sizeof(DocChunkTypes), 1, src_file) != 1)
52                 return false;
53         if (fread(&size, sizeof(uint32_t), 1, src_file) != 1)
54                 return false;
55         return true;
56 }
57
58 void Document::Save(const string & filename)
59 {
60         Debug("Saving document to file \"%s\"...", filename.c_str());
61         FILE * file = fopen(filename.c_str(), "w");
62         if (file == NULL)
63                 Fatal("Couldn't open file \"%s\" - %s", filename.c_str(), strerror(errno));
64
65         size_t written;
66         Debug("Number of objects (%u)...", ObjectCount());
67         WriteChunkHeader(file, CT_NUMOBJS, sizeof(m_count));
68         written = fwrite(&m_count, sizeof(m_count), 1, file);
69         if (written != 1)
70                 Fatal("Failed to write number of objects!");
71
72         Debug("Object types...");
73         WriteChunkHeader(file, CT_OBJTYPES, m_objects.types.size() * sizeof(ObjectType));
74         SaveStructVector<ObjectType>(file, m_objects.types);
75
76         Debug("Object bounds...");
77         WriteChunkHeader(file, CT_OBJBOUNDS, m_objects.bounds.size() * sizeof(Rect));
78         SaveStructVector<Rect>(file, m_objects.bounds);
79
80         Debug("Object data indices...");
81         WriteChunkHeader(file, CT_OBJINDICES, m_objects.data_indices.size() * sizeof(unsigned));
82         SaveStructVector<unsigned>(file, m_objects.data_indices);
83         
84         Debug("Bezier data...");
85         WriteChunkHeader(file, CT_OBJBEZIERS, m_objects.beziers.size() * sizeof(uint8_t));
86         SaveStructVector<Bezier>(file, m_objects.beziers);
87
88         int err = fclose(file);
89         if (err != 0)
90                 Fatal("Failed to close file \"%s\" - %s", filename.c_str(), strerror(err));
91
92         Debug("Successfully saved %u objects to \"%s\"", ObjectCount(), filename.c_str());
93 }
94
95 #ifndef QUADTREE_DISABLED
96
97 void Document::GenBaseQuadtree()
98 {
99         m_quadtree.nodes.push_back(QuadTreeNode{QUADTREE_EMPTY, QUADTREE_EMPTY, QUADTREE_EMPTY, QUADTREE_EMPTY, QUADTREE_EMPTY, QTC_UNKNOWN, 0, ObjectCount(), -1});
100         m_quadtree.root_id = 0;
101 }
102
103 int Document::ClipObjectToQuadChild(int object_id, QuadTreeNodeChildren type)
104 {
105         PROFILE_SCOPE("Document::ClipObjectToQuadChild");
106         switch (m_objects.types[object_id])
107         {
108         case RECT_FILLED:
109         case RECT_OUTLINE:
110         case PATH:
111                 {
112                 Rect obj_bounds = TransformToQuadChild(m_objects.bounds[object_id], type);
113                 if (obj_bounds.x < 0)
114                 {
115                         obj_bounds.w += obj_bounds.x;
116                         obj_bounds.x = 0;
117                 }
118                 if (obj_bounds.y < 0)
119                 {
120                         obj_bounds.h += obj_bounds.y;
121                         obj_bounds.y = 0;
122                 }
123                 if (obj_bounds.x + obj_bounds.w > 1)
124                 {
125                         obj_bounds.w += (1 - (obj_bounds.x + obj_bounds.w));
126                 }
127                 if (obj_bounds.y + obj_bounds.h > 1)
128                 {
129                         obj_bounds.h += (1 - (obj_bounds.y + obj_bounds.h));
130                 }
131                 m_objects.bounds.push_back(obj_bounds);
132                 m_objects.types.push_back(m_objects.types[object_id]);
133                 m_objects.data_indices.push_back(m_objects.data_indices[object_id]);
134                 return 1;
135                 }
136         case BEZIER:
137                 {
138                 // If we're entirely within the quadtree node, no clipping need occur.
139                 if (ContainedInQuadChild(m_objects.bounds[object_id], type))
140                 {
141                         m_objects.bounds.push_back(TransformToQuadChild(m_objects.bounds[object_id], type));
142                         m_objects.types.push_back(m_objects.types[object_id]);
143                         m_objects.data_indices.push_back(m_objects.data_indices[object_id]);
144                         return 1;
145                 }
146                 Rect clip_bezier_bounds = TransformRectCoordinates(m_objects.bounds[object_id], TransformFromQuadChild(Rect{0,0,1,1}, type)); 
147                 std::vector<Bezier> new_curves = m_objects.beziers[m_objects.data_indices[object_id]].ClipToRectangle(clip_bezier_bounds);
148                 for (size_t i = 0; i < new_curves.size(); ++i)
149                 {
150                         Rect new_bounds = TransformToQuadChild(m_objects.bounds[object_id], type);
151                         Bezier new_curve_data = new_curves[i].ToAbsolute(TransformToQuadChild(m_objects.bounds[object_id],type));
152                         new_bounds = new_curve_data.SolveBounds();
153                         Debug("New bounds: %s", new_bounds.Str().c_str());
154                         new_curve_data = new_curve_data.ToRelative(new_bounds);
155                         unsigned index = AddBezierData(new_curve_data);
156                         m_objects.bounds.push_back(new_bounds);
157                         m_objects.types.push_back(BEZIER);
158                         m_objects.data_indices.push_back(index);
159                 }
160                 return new_curves.size();
161                 }
162         default:
163                 Debug("Adding %s -> %s", m_objects.bounds[object_id].Str().c_str(), TransformToQuadChild(m_objects.bounds[object_id], type).Str().c_str());
164                 m_objects.bounds.push_back(TransformToQuadChild(m_objects.bounds[object_id], type));
165                 m_objects.types.push_back(m_objects.types[object_id]);
166                 m_objects.data_indices.push_back(m_objects.data_indices[object_id]);
167                 return 1;
168         }
169         return 0;
170 }
171 QuadTreeIndex Document::GenQuadChild(QuadTreeIndex parent, QuadTreeNodeChildren type)
172 {
173         PROFILE_SCOPE("Document::GenQuadChild()");
174         QuadTreeIndex new_index = m_quadtree.nodes.size();
175         Debug("-------------- Generating Quadtree Node %d (parent %d, type %d) ----------------------", new_index, parent, type);
176         m_quadtree.nodes.push_back(QuadTreeNode{QUADTREE_EMPTY, QUADTREE_EMPTY, QUADTREE_EMPTY, QUADTREE_EMPTY, parent, type, 0, 0, -1});
177
178         m_quadtree.nodes[new_index].object_begin = m_objects.bounds.size();
179         for (QuadTreeIndex overlay = parent; overlay != -1; overlay = m_quadtree.nodes[overlay].next_overlay)
180         {
181                 for (unsigned i = m_quadtree.nodes[overlay].object_begin; i < m_quadtree.nodes[overlay].object_end; ++i)
182                 {
183                         if (IntersectsQuadChild(m_objects.bounds[i], type))
184                         {
185                                 m_count += ClipObjectToQuadChild(i, type);
186                         }
187                 }
188         }
189         m_quadtree.nodes[new_index].object_end = m_objects.bounds.size();
190         // No objects are dirty.
191         m_quadtree.nodes[new_index].object_dirty = m_objects.bounds.size();
192         switch (type)
193         {
194                 case QTC_TOP_LEFT:
195                         m_quadtree.nodes[parent].top_left = new_index;
196                         break;
197                 case QTC_TOP_RIGHT:
198                         m_quadtree.nodes[parent].top_right = new_index;
199                         break;
200                 case QTC_BOTTOM_LEFT:
201                         m_quadtree.nodes[parent].bottom_left = new_index;
202                         break;
203                 case QTC_BOTTOM_RIGHT:
204                         m_quadtree.nodes[parent].bottom_right = new_index;
205                         break;
206                 default:
207                         Fatal("Tried to add a QuadTree child of invalid type!");
208         }
209         return new_index;
210 }
211
212 void Document::OverlayQuadChildren(QuadTreeIndex orig_parent, QuadTreeIndex parent, QuadTreeNodeChildren type)
213 {
214         PROFILE_SCOPE("Document::OverlayQuadChildren()");
215         QuadTreeIndex new_index = m_quadtree.nodes.size();
216         Debug("-------------- Generating Quadtree Node %d (orig %d parent %d, type %d) ----------------------", new_index, orig_parent, parent, type);
217         m_quadtree.nodes.push_back(QuadTreeNode{QUADTREE_EMPTY, QUADTREE_EMPTY, QUADTREE_EMPTY, QUADTREE_EMPTY, orig_parent, type, 0, 0, -1});
218
219         m_quadtree.nodes[new_index].object_begin = m_objects.bounds.size();
220         for (unsigned i = m_quadtree.nodes[parent].object_dirty; i < m_quadtree.nodes[parent].object_end; ++i)
221         {
222                 if (IntersectsQuadChild(m_objects.bounds[i], type))
223                 {
224                         m_count += ClipObjectToQuadChild(i, type);
225                 }
226         }
227         m_quadtree.nodes[new_index].object_end = m_objects.bounds.size();
228         QuadTreeIndex orig_node = -1;
229         switch (type)
230         {
231                 case QTC_TOP_LEFT:
232                         orig_node = m_quadtree.nodes[orig_parent].top_left;
233                         break;
234                 case QTC_TOP_RIGHT:
235                         orig_node = m_quadtree.nodes[orig_parent].top_right;
236                         break;
237                 case QTC_BOTTOM_LEFT:
238                         orig_node = m_quadtree.nodes[orig_parent].bottom_left;
239                         break;
240                 case QTC_BOTTOM_RIGHT:
241                         orig_node = m_quadtree.nodes[orig_parent].bottom_right;
242                         break;
243                 default:
244                         Fatal("Tried to overlay a QuadTree child of invalid type!");
245         }
246         if (orig_node == -1)
247                 Fatal("Tried to overlay a QuadTree child that didn't exist!");
248
249         // Add us to the node's overlay linked list.
250         QuadTreeIndex prev_overlay = orig_node;
251         while (m_quadtree.nodes[prev_overlay].next_overlay != -1) prev_overlay = m_quadtree.nodes[prev_overlay].next_overlay;
252         Debug("- Original node %d, Previous overlay %d, new overlay %d", orig_node, prev_overlay, new_index);
253         m_quadtree.nodes[prev_overlay].next_overlay = new_index;
254
255         // Recurse into any extant children.
256         if (m_quadtree.nodes[orig_node].top_left != -1)
257                 OverlayQuadChildren(orig_node, new_index, QTC_TOP_LEFT);
258         if (m_quadtree.nodes[orig_node].top_right != -1)
259                 OverlayQuadChildren(orig_node, new_index, QTC_TOP_RIGHT);
260         if (m_quadtree.nodes[orig_node].bottom_left != -1)
261                 OverlayQuadChildren(orig_node, new_index, QTC_BOTTOM_LEFT);
262         if (m_quadtree.nodes[orig_node].bottom_right != -1)
263                 OverlayQuadChildren(orig_node, new_index, QTC_BOTTOM_RIGHT);
264
265         m_quadtree.nodes[new_index].object_dirty = m_quadtree.nodes[new_index].object_end;
266         m_quadtree.nodes[new_index].next_overlay = -1;
267 }
268
269 void Document::PropagateQuadChanges(QuadTreeIndex node)
270 {
271         for(QuadTreeIndex overlay = node; overlay != -1; overlay = m_quadtree.nodes[overlay].next_overlay)
272         {
273                 // We don't care about clean overlays.
274                 if (m_quadtree.nodes[overlay].object_dirty == m_quadtree.nodes[overlay].object_end) continue;
275                 // Recurse into any extant children.
276                 if (m_quadtree.nodes[node].top_left != -1)
277                         OverlayQuadChildren(node, overlay, QTC_TOP_LEFT);
278                 if (m_quadtree.nodes[node].top_right != -1)
279                         OverlayQuadChildren(node, overlay, QTC_TOP_RIGHT);
280                 if (m_quadtree.nodes[node].bottom_left != -1)
281                         OverlayQuadChildren(node, overlay, QTC_BOTTOM_LEFT);
282                 if (m_quadtree.nodes[node].bottom_right != -1)
283                         OverlayQuadChildren(node, overlay, QTC_BOTTOM_RIGHT);
284
285                 m_quadtree.nodes[overlay].object_dirty = m_quadtree.nodes[overlay].object_end;
286         }
287 }
288
289 // Reparent a quadtree node, making it the "type" child of a new node.
290 QuadTreeIndex Document::GenQuadParent(QuadTreeIndex child, QuadTreeNodeChildren type)
291 {
292         QuadTreeIndex new_index = m_quadtree.nodes.size();
293         m_quadtree.nodes.push_back(QuadTreeNode{QUADTREE_EMPTY, QUADTREE_EMPTY, QUADTREE_EMPTY, QUADTREE_EMPTY, -1, QTC_UNKNOWN, 0, 0, -1});
294
295         m_quadtree.nodes[new_index].object_begin = m_objects.bounds.size();
296         for (QuadTreeIndex overlay = child; overlay != -1; overlay = m_quadtree.nodes[overlay].next_overlay)
297         {
298                 for (unsigned i = m_quadtree.nodes[overlay].object_begin; i < m_quadtree.nodes[overlay].object_end; ++i)
299                 {
300                         m_objects.bounds.push_back(TransformFromQuadChild(m_objects.bounds[i], type));
301                         m_objects.types.push_back(m_objects.types[i]);
302                         m_objects.data_indices.push_back(m_objects.data_indices[i]);
303                         m_count++;
304                 }
305         }
306         m_quadtree.nodes[new_index].object_end = m_objects.bounds.size();
307         m_quadtree.nodes[new_index].object_dirty = m_objects.bounds.size();
308         switch (type)
309         {
310                 case QTC_TOP_LEFT:
311                         m_quadtree.nodes[new_index].top_left = child;
312                         break;
313                 case QTC_TOP_RIGHT:
314                         m_quadtree.nodes[new_index].top_right = child;
315                         break;
316                 case QTC_BOTTOM_LEFT:
317                         m_quadtree.nodes[new_index].bottom_left = child;
318                         break;
319                 case QTC_BOTTOM_RIGHT:
320                         m_quadtree.nodes[new_index].bottom_right = child;
321                         break;
322                 default:
323                         Fatal("Tried to add a QuadTree child of invalid type!");
324         }
325         return new_index;
326 }
327
328 #endif
329
330 void Document::Load(const string & filename)
331 {
332         m_objects.bounds.clear();
333         m_count = 0;
334         if (filename == "")
335         {
336                 Debug("Loaded empty document.");
337                 return;
338         }
339         Debug("Loading document from file \"%s\"", filename.c_str());
340         FILE * file = fopen(filename.c_str(), "r");
341         if (file == NULL)
342                 Fatal("Couldn't open file \"%s\"", filename.c_str(), strerror(errno));
343
344         size_t read;
345
346         DocChunkTypes chunk_type;
347         uint32_t chunk_size;
348         while (ReadChunkHeader(file, chunk_type, chunk_size))
349         {
350                 switch(chunk_type)
351                 {
352                 case CT_NUMOBJS:
353                         read = fread(&m_count, sizeof(m_count), 1, file);
354                         if (read != 1)
355                                 Fatal("Failed to read number of objects!");
356                         Debug("Number of objects: %u", ObjectCount());
357                         break;
358                 case CT_OBJTYPES:
359                         Debug("Object types...");
360                         LoadStructVector<ObjectType>(file, chunk_size/sizeof(ObjectType), m_objects.types);
361                         break;
362                 case CT_OBJBOUNDS:
363                         Debug("Object bounds...");
364                         LoadStructVector<Rect>(file, chunk_size/sizeof(Rect), m_objects.bounds);
365                         break;
366                 case CT_OBJINDICES:
367                         Debug("Object data indices...");
368                         LoadStructVector<unsigned>(file, chunk_size/sizeof(unsigned), m_objects.data_indices);
369                         break;
370                 case CT_OBJBEZIERS:
371                         Debug("Bezier data...");
372                         LoadStructVector<Bezier>(file, chunk_size/sizeof(Bezier), m_objects.beziers);
373                         break;
374                         
375                 case CT_OBJPATHS:
376                         Debug("Path data...");
377                         Warn("Not handled because lazy");
378                         break;
379                 }
380         }
381         Debug("Successfully loaded %u objects from \"%s\"", ObjectCount(), filename.c_str());
382 #ifndef QUADTREE_DISABLED
383         if (m_quadtree.root_id == QUADTREE_EMPTY)
384         {
385                 GenBaseQuadtree();
386         }
387 #endif
388 }
389
390 unsigned Document::AddPath(unsigned start_index, unsigned end_index, const Colour & fill, const Colour & stroke)
391 {
392         Path path(m_objects, start_index, end_index, fill, stroke);
393         unsigned data_index = AddPathData(path);
394         Rect bounds = path.SolveBounds(m_objects);
395         unsigned result = Add(PATH, bounds,data_index);
396         m_objects.paths[data_index].m_index = result;
397         //Debug("Added path %u -> %u (%u objects) colour {%u,%u,%u,%u}, stroke {%u,%u,%u,%u}", start_index, end_index, (end_index - start_index), fill.r, fill.g, fill.b, fill.a, stroke.r, stroke.g, stroke.b, stroke.a);
398         return result;
399 }
400
401 /**
402  * Add a Bezier using Absolute coords
403  */
404 unsigned Document::AddBezier(const Bezier & bezier)
405 {
406         Rect bounds = bezier.SolveBounds();
407         Bezier data = bezier.ToRelative(bounds); // Relative
408         if (data.ToAbsolute(bounds) != bezier)
409         {
410                 Warn("%s != %s", data.ToAbsolute(bounds).Str().c_str(),
411                         bezier.Str().c_str());
412                 Warn("ToAbsolute on ToRelative does not give original Bezier");
413         }
414         unsigned index = AddBezierData(data);
415         return Add(BEZIER, bounds, index);
416 }
417
418 unsigned Document::Add(ObjectType type, const Rect & bounds, unsigned data_index, QuadTreeIndex qti)
419 {
420         PROFILE_SCOPE("Document::Add");
421         m_objects.types.push_back(type);
422         m_objects.bounds.push_back(bounds);
423         m_objects.data_indices.push_back(data_index);
424 #ifndef QUADTREE_DISABLED
425         if (qti == -1) qti = m_current_insert_node;
426         if (qti != -1)
427         {
428                 QuadTreeIndex new_qti = qti;
429                 while (m_quadtree.nodes[new_qti].next_overlay != -1)
430                 {
431                         if (m_count == m_quadtree.nodes[new_qti].object_end+1)
432                         {
433                                 m_quadtree.nodes[new_qti].object_end++;
434                                 goto done;
435                         }
436                         new_qti = m_quadtree.nodes[new_qti].next_overlay;
437                 }
438                 {
439                         QuadTreeIndex overlay = m_quadtree.nodes.size();
440                         Debug("Adding new overlay, nqti = %d, overlay = %d", new_qti, overlay);
441                         m_quadtree.nodes.push_back(m_quadtree.nodes[qti]);
442                         m_quadtree.nodes[overlay].object_begin = m_count;
443                         // All objects are dirty.
444                         m_quadtree.nodes[overlay].object_dirty = m_count;
445                         m_quadtree.nodes[overlay].object_end = m_count+1;
446                         m_quadtree.nodes[overlay].next_overlay = -1;
447                         m_quadtree.nodes[new_qti].next_overlay = overlay;
448                         new_qti = overlay;
449                 }
450 done: // matches is not amused, but sulix is nice and moved it inside the #ifdef for him.
451                 m_count++;
452         }
453         return m_count-1;
454 #else // words fail me (still not amused)
455         return (m_count++);
456 #endif
457         
458 }
459
460 unsigned Document::AddBezierData(const Bezier & bezier)
461 {
462         m_objects.beziers.push_back(bezier);
463         return m_objects.beziers.size()-1;
464 }
465
466 unsigned Document::AddPathData(const Path & path)
467 {
468         m_objects.paths.push_back(path);
469         return m_objects.paths.size()-1;
470 }
471
472 void Document::DebugDumpObjects()
473 {
474         Debug("Objects for Document %p are:", this);
475         for (unsigned id = 0; id < ObjectCount(); ++id)
476         {
477                 Debug("%u. \tType: %u\tBounds: %s", id, m_objects.types[id], m_objects.bounds[id].Str().c_str());
478         }
479 }
480
481 bool Document::operator==(const Document & equ) const
482 {
483         return (ObjectCount() == equ.ObjectCount() 
484                 && memcmp(m_objects.bounds.data(), equ.m_objects.bounds.data(), ObjectCount() * sizeof(Rect)) == 0
485                 && memcmp(m_objects.data_indices.data(), equ.m_objects.data_indices.data(), ObjectCount() * sizeof(unsigned)) == 0
486                 && memcmp(m_objects.beziers.data(), equ.m_objects.beziers.data(), m_objects.beziers.size() * sizeof(Bezier)) == 0);
487 }
488
489
490
491 // Behold my amazing tokenizing abilities
492 static string & GetToken(const string & d, string & token, unsigned & i, const string & delims = "()[],{}<>;:=")
493 {
494         token.clear();
495         while (i < d.size() && iswspace(d[i]))
496         {
497                 ++i;
498         }
499         
500         while (i < d.size())
501         {
502                 if (iswspace(d[i]) || strchr(delims.c_str(),d[i]) != NULL)
503                 {
504                         if (token.size() == 0 && !iswspace(d[i]))
505                         {
506                                 token += d[i++];
507                         }
508                         break;  
509                 }
510                 token += d[i++];
511         }
512         //Debug("Got token \"%s\"", token.c_str());
513         return token;
514 }
515
516 static void GetXYPair(const string & d, Real & x, Real & y, unsigned & i,const string & delims = "()[],{}<>;:=")
517 {
518         string token("");
519         while (GetToken(d, token, i, delims) == ",");
520         x = RealFromStr(token);
521         if (GetToken(d, token, i, delims) != ",")
522         {
523                 Fatal("Expected \",\" seperating x,y pair");
524         }
525         y = RealFromStr(GetToken(d,token,i,delims));
526 }
527
528 static bool GetKeyValuePair(const string & d, string & key, string & value, unsigned & i, const string & delims = "()[],{}<>;:=")
529 {
530         key = "";
531         string token;
532         while (GetToken(d, token, i, delims) == ":" || token == ";");
533         key = token;
534         if (GetToken(d, token, i, delims) != ":")
535         {
536                 Error("Expected \":\" seperating key:value pair");
537                 return false;
538         }
539         value = "";
540         GetToken(d, value, i, delims);
541         return true;
542 }
543
544 static void TransformXYPair(Real & x, Real & y, const SVGMatrix & transform)
545 {
546         Real x0(x);
547         x = transform.a * x + transform.c * y + transform.e;
548         y = transform.b * x0 + transform.d * y + transform.f;
549 }
550
551 void Document::ParseSVGTransform(const string & s, SVGMatrix & transform)
552 {
553         //Debug("Parsing transform %s", s.c_str());
554         string token;
555         string command;
556         unsigned i = 0;
557         
558         while (i < s.size())
559         {
560                 GetToken(s, command, i);
561                 if (command == "," || command == "" || command == ":")
562                 {
563                         if (i < s.size())
564                                 GetToken(s, command, i);
565                         else
566                                 return;
567                 }
568                 //Debug("Token is \"%s\"", command.c_str());
569         
570                 SVGMatrix delta = {1,0,0,0,1,0};
571         
572         
573                 assert(GetToken(s,token, i) == "(");
574                 if (command == "translate")
575                 {
576                         GetXYPair(s, delta.e, delta.f, i);
577                         assert(GetToken(s,token, i) == ")");    
578                 }
579                 else if (command == "matrix")
580                 {
581                         GetXYPair(s, delta.a, delta.b,i);
582                         GetXYPair(s, delta.c, delta.d,i);
583                         GetXYPair(s, delta.e, delta.f,i);
584                         assert(GetToken(s,token, i) == ")");    
585                 }
586                 else if (command == "scale")
587                 {
588                         delta.a = RealFromStr(GetToken(s,token,i));
589                         GetToken(s, token, i);
590                         if (token == ",")
591                         {
592                                 delta.d = RealFromStr(GetToken(s,token,i));
593                                 assert(GetToken(s, token, i) == ")");
594                         }
595                         else
596                         {
597                                 delta.d = delta.a;
598                                 assert(token == ")");
599                         }
600                         
601                 }
602                 else
603                 {
604                         Warn("Unrecognised transform \"%s\", using identity", command.c_str());
605                 }
606         
607                 //Debug("Old transform is {%f,%f,%f,%f,%f,%f}", transform.a, transform.b, transform.c, transform.d,transform.e,transform.f);
608                 //Debug("Delta transform is {%f,%f,%f,%f,%f,%f}", delta.a, delta.b, delta.c, delta.d,delta.e,delta.f);
609         
610                 SVGMatrix old(transform);
611                 transform.a = old.a * delta.a + old.c * delta.b;
612                 transform.c = old.a * delta.c + old.c * delta.d;
613                 transform.e = old.a * delta.e + old.c * delta.f + old.e;
614         
615                 transform.b = old.b * delta.a + old.d * delta.b;
616                 transform.d = old.b * delta.c + old.d * delta.d;
617                 transform.f = old.b * delta.e + old.d * delta.f + old.f;
618         
619                 //Debug("New transform is {%f,%f,%f,%f,%f,%f}", transform.a, transform.b, transform.c, transform.d,transform.e,transform.f);
620         }
621 }
622
623 inline Colour ParseColourString(const string & colour_str)
624 {
625         Colour c(0,0,0,0);
626         if (colour_str == "red")
627                 c = {255,0,0,255};
628         else if (colour_str == "blue")
629                 c = {0,0,255,255};
630         else if (colour_str == "green")
631                 c = {0,255,0,255};
632         else if (colour_str == "black")
633                 c = {0,0,0,255};
634         else if (colour_str == "white")
635                 c = {255,255,255,255};
636         else if (colour_str.size() == 7 && colour_str[0] == '#')
637         {
638                 //Debug("Parse colour string: \"%s\"", colour_str.c_str());
639                 char comp[3] = {colour_str[1], colour_str[2], '\0'};
640                 c.r = strtoul(comp, NULL, 16);
641                 comp[0] = colour_str[3]; comp[1] = colour_str[4];
642                 c.g = strtoul(comp, NULL, 16);
643                 comp[0] = colour_str[5]; comp[1] = colour_str[6];
644                 c.b = strtoul(comp, NULL, 16);
645                 c.a = 255;
646                 //Debug("Colour is: %u, %u, %u, %u", c.r, c.g, c.b, c.a);
647         }
648         return c;
649 }
650
651 void Document::ParseSVGNode(pugi::xml_node & root, SVGMatrix & parent_transform)
652 {
653         //Debug("Parse node <%s>", root.name());
654
655         
656         // Centre the SVGs
657         if (strcmp(root.name(),"svg") == 0)
658         {
659                 Real ww = RealFromStr(root.attribute("width").as_string());
660                 Real hh = RealFromStr(root.attribute("height").as_string());
661                 parent_transform.e -= parent_transform.a * ww/Real(2);
662                 parent_transform.f -= parent_transform.d * hh/Real(2);
663         }
664         
665         for (pugi::xml_node child = root.first_child(); child; child = child.next_sibling())
666         {
667                 SVGMatrix transform(parent_transform);  
668                 pugi::xml_attribute attrib_trans = child.attribute("transform");
669                 if (!attrib_trans.empty())
670                 {
671                         ParseSVGTransform(attrib_trans.as_string(), transform);
672                 }
673                 
674                 
675                 
676                 if (strcmp(child.name(), "svg") == 0 || strcmp(child.name(),"g") == 0
677                         || strcmp(child.name(), "group") == 0)
678                 {
679                         
680                         ParseSVGNode(child, transform);
681                         continue;
682                 }
683                 else if (strcmp(child.name(), "path") == 0)
684                 {
685                         string d = child.attribute("d").as_string();
686                         //Debug("Path data attribute is \"%s\"", d.c_str());
687                         bool closed = false;
688                         pair<unsigned, unsigned> range = ParseSVGPathData(d, transform, closed);
689                         if (true && range.first < m_count && range.second < m_count)//(closed)
690                         {
691                                 
692                                 string colour_str("");
693                                 map<string, string> style;
694                                 if (child.attribute("style"))
695                                 {
696                                         ParseSVGStyleData(child.attribute("style").as_string(), style);
697                                 }
698                                 
699                                 // Determine shading colour
700                                 if (child.attribute("fill"))
701                                 {
702                                         colour_str = child.attribute("fill").as_string();
703                                 }
704                                 else if (style.find("fill") != style.end())
705                                 {
706                                         colour_str = style["fill"];
707                                 }
708                                 Colour fill = ParseColourString(colour_str);
709                                 Colour stroke = fill;
710                         
711                                 if (child.attribute("stroke"))
712                                 {
713                                         colour_str = child.attribute("stroke").as_string();
714                                         stroke = ParseColourString(colour_str);
715                                 }
716                                 else if (style.find("stroke") != style.end())
717                                 {
718                                         colour_str = style["stroke"];
719                                         stroke = ParseColourString(colour_str);
720                                 }
721                                 
722                                 
723                                 // Determin shading alpha
724                                 if (child.attribute("fill-opacity"))
725                                 {
726                                         fill.a = 255*child.attribute("fill-opacity").as_float();
727                                 }
728                                 else if (style.find("fill-opacity") != style.end())
729                                 {
730                                         fill.a = 255*strtod(style["fill-opacity"].c_str(), NULL);
731                                 }
732                                 if (child.attribute("stroke-opacity"))
733                                 {
734                                         stroke.a = 255*child.attribute("stroke-opacity").as_float();
735                                 }
736                                 else if (style.find("stroke-opacity") != style.end())
737                                 {
738                                         stroke.a = 255*strtod(style["stroke-opacity"].c_str(), NULL);
739                                 }
740                                 AddPath(range.first, range.second, fill, stroke);
741                         }
742                         
743                 }
744                 else if (strcmp(child.name(), "line") == 0)
745                 {
746                         Real x0(child.attribute("x1").as_float());
747                         Real y0(child.attribute("y1").as_float());
748                         Real x1(child.attribute("x2").as_float());
749                         Real y1(child.attribute("y2").as_float());
750                         TransformXYPair(x0,y0,transform);
751                         TransformXYPair(x1,y1,transform);
752                         AddBezier(Bezier(x0,y0,x1,y1,x1,y1,x1,y1));
753                 }
754                 else if (strcmp(child.name(), "rect") == 0)
755                 {
756                         Real coords[4];
757                         const char * attrib_names[] = {"x", "y", "width", "height"};
758                         for (size_t i = 0; i < 4; ++i)
759                                 coords[i] = child.attribute(attrib_names[i]).as_float();
760                         
761                         Real x2(coords[0]+coords[2]);
762                         Real y2(coords[1]+coords[3]);
763                         TransformXYPair(coords[0],coords[1],transform); // x, y, transform
764                         TransformXYPair(x2,y2,transform);
765                         coords[2] = x2 - coords[0];
766                         coords[3] = y2 - coords[1];
767                         
768                         bool outline = !(child.attribute("fill") && strcmp(child.attribute("fill").as_string(),"none") != 0);
769                         Add(outline?RECT_OUTLINE:RECT_FILLED, Rect(coords[0], coords[1], coords[2], coords[3]),0);
770                 }
771                 else if (strcmp(child.name(), "circle") == 0)
772                 {
773                         Real cx = child.attribute("cx").as_float();
774                         Real cy = child.attribute("cy").as_float();
775                         Real r = child.attribute("r").as_float();
776                         
777                         Real x = (cx - r);
778                         Real y = (cy - r);
779                         TransformXYPair(x,y,transform);
780                         Real w = Real(2)*r*transform.a; // width scales
781                         Real h = Real(2)*r*transform.d; // height scales
782                         
783                         
784                         Rect rect(x,y,w,h);
785                         Add(CIRCLE_FILLED, rect,0);
786                         Debug("Added Circle %s", rect.Str().c_str());                   
787                 }
788                 else if (strcmp(child.name(), "text") == 0)
789                 {
790                         Real x = child.attribute("x").as_float();
791                         Real y = child.attribute("y").as_float();
792                         TransformXYPair(x,y,transform);
793                         Debug("Add text \"%s\"", child.child_value());
794                         AddText(child.child_value(), 0.05, x, y);
795                 }
796         }
797 }
798
799 void Document::ParseSVGStyleData(const string & style, map<string, string> & results)
800 {
801         unsigned i = 0;
802         string key;
803         string value;
804         while (i < style.size() && GetKeyValuePair(style, key, value, i))
805         {
806                 results[key] = value;
807         }
808 }
809
810 /**
811  * Parse an SVG string into a rectangle
812  */
813 void Document::ParseSVG(const string & input, const Rect & bounds)
814 {
815         using namespace pugi;
816         
817         xml_document doc_xml;
818         xml_parse_result result = doc_xml.load(input.c_str());
819         
820         if (!result)
821                 Error("Couldn't parse SVG input - %s", result.description());
822                 
823         Debug("Loaded XML - %s", result.description());
824         SVGMatrix transform = {bounds.w, 0,bounds.x, 0,bounds.h,bounds.y};
825         ParseSVGNode(doc_xml, transform);
826 }
827
828 /**
829  * Load an SVG into a rectangle
830  */
831 void Document::LoadSVG(const string & filename, const Rect & bounds)
832 {
833         using namespace pugi;
834         
835         xml_document doc_xml;
836         ifstream input(filename.c_str(), ios_base::in);
837         xml_parse_result result = doc_xml.load(input);
838         
839         if (!result)
840                 Error("Couldn't load \"%s\" - %s", filename.c_str(), result.description());
841                 
842         Debug("Loaded XML from \"%s\" - %s", filename.c_str(), result.description());
843         
844         input.close();
845                                                 // a c e, b d f
846         SVGMatrix transform = {bounds.w,0 ,bounds.x, 0,bounds.h,bounds.y};
847         ParseSVGNode(doc_xml, transform);
848 }
849
850
851
852 // Fear the wrath of the tokenizing svg data
853 // Seriously this isn't really very DOM-like at all is it?
854 pair<unsigned, unsigned> Document::ParseSVGPathData(const string & d, const SVGMatrix & transform, bool & closed)
855 {
856         closed = false;
857         Real x[4] = {0,0,0,0};
858         Real y[4] = {0,0,0,0};
859         
860         string token("");
861         string command("m");
862         
863         Real x0(0);
864         Real y0(0);
865         
866         unsigned i = 0;
867         unsigned prev_i = 0;
868         
869         bool start = false;
870         
871
872         static string delims("()[],{}<>;:=LlHhVvmMqQzZcC");
873
874         pair<unsigned, unsigned> range(m_count, m_count);
875         
876         while (i < d.size() && GetToken(d, token, i, delims).size() > 0)
877         {
878                 if (isalpha(token[0]))
879                         command = token;
880                 else
881                 {
882                         i = prev_i; // hax
883                         if(command == "")
884                                 command = "L";
885                 }
886                 
887                 bool relative = islower(command[0]);
888                         
889                 if (command == "m" || command == "M")
890                 {
891                         //Debug("Construct moveto command");
892                         Real dx = RealFromStr(GetToken(d,token,i,delims));
893                         assert(GetToken(d,token,i,delims) == ",");
894                         Real dy = RealFromStr(GetToken(d,token,i,delims));
895                         
896                         x[0] = (relative) ? x[0] + dx : dx;
897                         y[0] = (relative) ? y[0] + dy : dy;
898                         
899                         x0 = x[0];
900                         y0 = y[0];
901                         //Debug("mmoveto %f,%f", Float(x[0]),Float(y[0]));
902                         command = (command == "m") ? "l" : "L";
903                 }
904                 else if (command == "c" || command == "C" || command == "q" || command == "Q")
905                 {
906                         //Debug("Construct curveto command");
907                         Real dx = RealFromStr(GetToken(d,token,i,delims));
908                         assert(GetToken(d,token,i,delims) == ",");
909                         Real dy = RealFromStr(GetToken(d,token,i,delims));
910                         
911                         x[1] = (relative) ? x[0] + dx : dx;
912                         y[1] = (relative) ? y[0] + dy : dy;
913                         
914                         dx = RealFromStr(GetToken(d,token,i,delims));
915                         assert(GetToken(d,token,i,delims) == ",");
916                         dy = RealFromStr(GetToken(d,token,i,delims));
917                         
918                         x[2] = (relative) ? x[0] + dx : dx;
919                         y[2] = (relative) ? y[0] + dy : dy;
920                         
921                         if (command != "q" && command != "Q")
922                         {
923                                 dx = RealFromStr(GetToken(d,token,i,delims));
924                                 assert(GetToken(d,token,i,delims) == ",");
925                                 dy = RealFromStr(GetToken(d,token,i,delims));
926                                 x[3] = (relative) ? x[0] + dx : dx;
927                                 y[3] = (relative) ? y[0] + dy : dy;
928                         }
929                         else
930                         {
931                                 x[3] = x[2];
932                                 y[3] = y[2];
933                                 Real old_x1(x[1]), old_y1(y[1]);
934                                 x[1] = x[0] + Real(2) * (old_x1 - x[0])/ Real(3);
935                                 y[1] = y[0] + Real(2) * (old_y1 - y[0])/ Real(3);
936                                 x[2] = x[3] + Real(2) * (old_x1 - x[3])/ Real(3);
937                                 y[2] = y[3] + Real(2) * (old_y1 - y[3])/ Real(3);
938                         }
939                         
940                         Real x3(x[3]);
941                         Real y3(y[3]);
942                         for (int j = 0; j < 4; ++j)
943                                 TransformXYPair(x[j],y[j], transform);
944
945                         range.second = AddBezier(Bezier(x[0],y[0],x[1],y[1],x[2],y[2],x[3],y[3]));
946                         
947                         //Debug("[%u] curveto %f,%f %f,%f %f,%f", index, Float(x[1]),Float(y[1]),Float(x[2]),Float(y[2]),Float(x[3]),Float(y[3]));
948                         
949                         x[0] = x3;
950                         y[0] = y3;
951
952                         
953                 }
954                 else if (command == "l" || command == "L" || command == "h" || command == "H" || command == "v" || command == "V")
955                 {
956                         //Debug("Construct lineto command, relative %d", relative);
957                 
958                         Real dx = RealFromStr(GetToken(d,token,i,delims));
959                         Real dy = 0;
960                         if (command == "l" || command == "L")
961                         {
962                                 assert(GetToken(d,token,i,delims) == ",");
963                                 dy = RealFromStr(GetToken(d,token,i,delims));
964                         }
965                         else if (command == "v" || command == "V")
966                         {
967                                 swap(dx,dy);
968                         }
969                         
970                         x[1] = (relative) ? x[0] + dx : dx;
971                         y[1] = (relative) ? y[0] + dy : dy;
972                         if (command == "v" || command == "V")
973                         {
974                                 x[1] = x[0];
975                         }
976                         else if (command == "h" || command == "H")
977                         {
978                                 y[1] = y[0];
979                         }
980                         
981                         Real x1(x[1]);
982                         Real y1(y[1]);
983                         
984                         TransformXYPair(x[0],y[0],transform);
985                         TransformXYPair(x[1],y[1],transform);
986
987
988                         range.second = AddBezier(Bezier(x[0],y[0],x[1],y[1],x[1],y[1],x[1],y[1]));
989                         
990                         //Debug("[%u] lineto %f,%f %f,%f", index, Float(x[0]),Float(y[0]),Float(x[1]),Float(y[1]));
991                         
992                         x[0] = x1;
993                         y[0] = y1;
994
995                 }
996                 else if (command == "z" || command == "Z")
997                 {
998                         //Debug("Construct returnto command");
999                         x[1] = x0;
1000                         y[1] = y0;
1001                         x[2] = x0;
1002                         y[2] = y0;
1003                         x[3] = x0;
1004                         y[3] = y0;
1005                         
1006                         Real x3(x[3]);
1007                         Real y3(y[3]);
1008                         for (int j = 0; j < 4; ++j)
1009                                 TransformXYPair(x[j],y[j], transform);
1010
1011                         range.second = AddBezier(Bezier(x[0],y[0],x[1],y[1],x[2],y[2],x[3],y[3]));
1012                         //Debug("[%u] returnto %f,%f %f,%f", index, Float(x[0]),Float(y[0]),Float(x[1]),Float(y[1]));
1013                         
1014                         x[0] = x3;
1015                         y[0] = y3;
1016                         command = "m";
1017                         closed = true;
1018                 }
1019                 else
1020                 {
1021                         Warn("Unrecognised command \"%s\", set to \"m\"", command.c_str());
1022                         command = "m";
1023                 }
1024                 
1025                 if (!start)
1026                 {
1027                         x0 = x[0];
1028                         y0 = y[0];
1029                         start = true;
1030                 }
1031                 prev_i = i;
1032         }
1033         return range;
1034 }
1035
1036 void Document::SetFont(const string & font_filename)
1037 {
1038         if (m_font_data != NULL)
1039         {
1040                 free(m_font_data);
1041         }
1042         
1043         FILE *font_file = fopen(font_filename.c_str(), "rb");
1044         fseek(font_file, 0, SEEK_END);
1045         size_t font_file_size = ftell(font_file);
1046         fseek(font_file, 0, SEEK_SET);
1047         m_font_data = (unsigned char*)malloc(font_file_size);
1048         size_t read = fread(m_font_data, 1, font_file_size, font_file);
1049         if (read != font_file_size)
1050         {
1051                 Fatal("Failed to read font data from \"%s\" - Read %u bytes expected %u - %s", font_filename.c_str(), read, font_file_size, strerror(errno));
1052         }
1053         fclose(font_file);
1054         stbtt_InitFont(&m_font, m_font_data, 0);
1055 }
1056
1057 void Document::AddText(const string & text, Real scale, Real x, Real y)
1058 {
1059         if (m_font_data == NULL)
1060         {
1061                 Warn("No font loaded");
1062                 return;
1063         }
1064                 
1065         Real x0(x);
1066         //Real y0(y);
1067         int ascent = 0, descent = 0, line_gap = 0;
1068         stbtt_GetFontVMetrics(&m_font, &ascent, &descent, &line_gap);
1069         Real font_scale = scale;
1070         font_scale /= Real(ascent - descent);
1071         Real y_advance = Real(font_scale) * Real(ascent - descent + line_gap);
1072         for (unsigned i = 0; i < text.size(); ++i)
1073         {
1074                 if (text[i] == '\n')
1075                 {
1076                         y += y_advance;
1077                         x = x0;
1078                 }
1079                 if (!isprint(text[i]))
1080                         continue;
1081                         
1082                 int advance_width = 0, left_side_bearing = 0, kerning = 0;
1083                 stbtt_GetCodepointHMetrics(&m_font, text[i], &advance_width, &left_side_bearing);
1084                 if (i >= 1)
1085                 {
1086                         kerning = stbtt_GetCodepointKernAdvance(&m_font, text[i-1], text[i]);
1087                 }
1088                 x += font_scale * Real(kerning);
1089                 AddFontGlyphAtPoint(&m_font, text[i], font_scale, x, y);
1090                 x += font_scale * Real(advance_width);
1091         }
1092 }
1093
1094 void Document::AddFontGlyphAtPoint(stbtt_fontinfo *font, int character, Real scale, Real x, Real y)
1095 {
1096         int glyph_index = stbtt_FindGlyphIndex(font, character);
1097
1098         // Check if there is actully a glyph to render.
1099         if (stbtt_IsGlyphEmpty(font, glyph_index))
1100         {
1101                 return;
1102         }
1103
1104         stbtt_vertex *instructions;
1105         int num_instructions = stbtt_GetGlyphShape(font, glyph_index, &instructions);
1106
1107         Real current_x(0), current_y(0);
1108         unsigned start_index = m_count;
1109         unsigned end_index = m_count;
1110         for (int i = 0; i < num_instructions; ++i)
1111         {
1112                 // TTF uses 16-bit signed ints for coordinates:
1113                 // with the y-axis inverted compared to us.
1114                 // Convert and scale any data.
1115                 Real inst_x = Real(instructions[i].x)*scale;
1116                 Real inst_y = Real(instructions[i].y)*-scale;
1117                 Real inst_cx = Real(instructions[i].cx)*scale;
1118                 Real inst_cy = Real(instructions[i].cy)*-scale;
1119                 Real old_x(current_x), old_y(current_y);
1120                 current_x = inst_x;
1121                 current_y = inst_y;
1122                 
1123                 switch(instructions[i].type)
1124                 {
1125                 // Move To
1126                 case STBTT_vmove:
1127                         break;
1128                 // Line To
1129                 case STBTT_vline:
1130                         end_index = AddBezier(Bezier(old_x + x, old_y + y, old_x + x, old_y + y, current_x + x, current_y + y, current_x + x, current_y + y));
1131                         break;
1132                 // Quadratic Bezier To:
1133                 case STBTT_vcurve:
1134                         // Quadratic -> Cubic:
1135                         // - Endpoints are the same.
1136                         // - cubic1 = quad0+(2/3)*(quad1-quad0)
1137                         // - cubic2 = quad2+(2/3)*(quad1-quad2)
1138                         end_index = AddBezier(Bezier(old_x + x, old_y + y, old_x + Real(2)*(inst_cx-old_x)/Real(3) + x, old_y + Real(2)*(inst_cy-old_y)/Real(3) + y,
1139                                                 current_x + Real(2)*(inst_cx-current_x)/Real(3) + x, current_y + Real(2)*(inst_cy-current_y)/Real(3) + y, current_x + x, current_y + y));
1140                         break;
1141                 }
1142         }
1143         
1144         if (start_index < m_count && end_index < m_count)
1145         {
1146                 AddPath(start_index, end_index);
1147         }
1148         //Debug("Added Glyph \"%c\" at %f %f, scale %f", (char)character, Float(x), Float(y), Float(scale));
1149
1150         stbtt_FreeShape(font, instructions);
1151 }
1152
1153 void Document::TransformObjectBounds(const SVGMatrix & transform, ObjectType type)
1154 {
1155         #ifdef TRANSFORM_BEZIERS_TO_PATH
1156                 for (unsigned i = 0; i < m_objects.paths.size(); ++i)
1157                 {
1158                         Path & p = m_objects.paths[i];
1159                         p.m_bounds.x = transform.a * p.m_bounds.x + transform.e;
1160                         p.m_bounds.y = transform.d * p.m_bounds.y + transform.f;
1161                         p.m_bounds.w *= transform.a;
1162                         p.m_bounds.h *= transform.d;
1163                 }
1164                 return;
1165         #endif          
1166         
1167         for (unsigned i = 0; i < m_count; ++i)
1168         {
1169                 if (type == NUMBER_OF_OBJECT_TYPES || m_objects.types[i] == type)
1170                 {
1171                         TransformXYPair(m_objects.bounds[i].x, m_objects.bounds[i].y, transform);
1172                         m_objects.bounds[i].w *= transform.a;
1173                         m_objects.bounds[i].h *= transform.d;
1174                 }
1175         }
1176 }
1177
1178 void Document::TranslateObjects(const Real & dx, const Real & dy, ObjectType type)
1179 {
1180         #ifdef TRANSFORM_BEZIERS_TO_PATH
1181         for (unsigned i = 0; i < m_objects.paths.size(); ++i)
1182         {
1183                 Path & p = m_objects.paths[i];
1184                 p.m_bounds.x += dx;
1185                 p.m_bounds.y += dy;
1186         }
1187         return;
1188         #endif
1189
1190         for (unsigned i = 0; i < m_count; ++i)
1191         {
1192                 if (type == NUMBER_OF_OBJECT_TYPES || m_objects.types[i] == type)
1193                 {
1194                         m_objects.bounds[i].x += dx;
1195                         m_objects.bounds[i].y += dy;
1196                 }
1197         }
1198 }
1199
1200 void Document::ScaleObjectsAboutPoint(const Real & x, const Real & y, const Real & scale_amount, ObjectType type)
1201 {
1202         #ifdef TRANSFORM_BEZIERS_TO_PATH
1203                 for (unsigned i = 0; i < m_objects.paths.size(); ++i)
1204                 {
1205                         Path & p = m_objects.paths[i];
1206                         p.m_bounds.w /= scale_amount;
1207                         p.m_bounds.h /= scale_amount;
1208                         p.m_bounds.x -= x;
1209                         p.m_bounds.x /= scale_amount;
1210                         p.m_bounds.x += x;
1211                         p.m_bounds.y -= y;
1212                         p.m_bounds.y /= scale_amount;
1213                         p.m_bounds.y += y;
1214                 }
1215                 return;
1216         #endif
1217         
1218         for (unsigned i = 0; i < m_count; ++i)
1219         {
1220                 if (type != NUMBER_OF_OBJECT_TYPES && m_objects.types[i] != type)
1221                         continue;
1222                 
1223                 m_objects.bounds[i].w /= scale_amount;
1224                 m_objects.bounds[i].h /= scale_amount;
1225                 //m_objects.bounds[i].x = x + (m_objects.bounds[i].x-x)/scale_amount;
1226                 //m_objects.bounds[i].y = y + (m_objects.bounds[i].y-x)/scale_amount;
1227                 m_objects.bounds[i].x -= x;
1228                 m_objects.bounds[i].x /= scale_amount;
1229                 m_objects.bounds[i].x += x;
1230                 
1231                 m_objects.bounds[i].y -= y;
1232                 m_objects.bounds[i].y /= scale_amount;
1233                 m_objects.bounds[i].y += y;
1234         }
1235
1236 }
1237
1238

UCC git Repository :: git.ucc.asn.au