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

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