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

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