Add objects without quadtree
[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: // matches is not amused, but sulix is nice and moved it inside the #ifdef for him.
443                 m_count++;
444                 PropagateQuadChanges(qti);
445         }
446 #else // Although empty documents do always render correctly at any zoom,
447                 // I suspect the markers will not be amused
448         m_count++;
449 #endif
450         return m_count; // Why can't we just use the size of types or something?
451 }
452
453 unsigned Document::AddBezierData(const Bezier & bezier)
454 {
455         m_objects.beziers.push_back(bezier);
456         return m_objects.beziers.size()-1;
457 }
458
459 unsigned Document::AddPathData(const Path & path)
460 {
461         m_objects.paths.push_back(path);
462         return m_objects.paths.size()-1;
463 }
464
465 void Document::DebugDumpObjects()
466 {
467         Debug("Objects for Document %p are:", this);
468         for (unsigned id = 0; id < ObjectCount(); ++id)
469         {
470                 Debug("%u. \tType: %u\tBounds: %s", id, m_objects.types[id], m_objects.bounds[id].Str().c_str());
471         }
472 }
473
474 bool Document::operator==(const Document & equ) const
475 {
476         return (ObjectCount() == equ.ObjectCount() 
477                 && memcmp(m_objects.bounds.data(), equ.m_objects.bounds.data(), ObjectCount() * sizeof(Rect)) == 0
478                 && memcmp(m_objects.data_indices.data(), equ.m_objects.data_indices.data(), ObjectCount() * sizeof(unsigned)) == 0
479                 && memcmp(m_objects.beziers.data(), equ.m_objects.beziers.data(), m_objects.beziers.size() * sizeof(Bezier)) == 0);
480 }
481
482
483
484 // Behold my amazing tokenizing abilities
485 static string & GetToken(const string & d, string & token, unsigned & i, const string & delims = "()[],{}<>;:=")
486 {
487         token.clear();
488         while (i < d.size() && iswspace(d[i]))
489         {
490                 ++i;
491         }
492         
493         while (i < d.size())
494         {
495                 if (iswspace(d[i]) || strchr(delims.c_str(),d[i]) != NULL)
496                 {
497                         if (token.size() == 0 && !iswspace(d[i]))
498                         {
499                                 token += d[i++];
500                         }
501                         break;  
502                 }
503                 token += d[i++];
504         }
505         //Debug("Got token \"%s\"", token.c_str());
506         return token;
507 }
508
509 static void GetXYPair(const string & d, Real & x, Real & y, unsigned & i,const string & delims = "()[],{}<>;:=")
510 {
511         string token("");
512         while (GetToken(d, token, i, delims) == ",");
513         x = RealFromStr(token);
514         if (GetToken(d, token, i, delims) != ",")
515         {
516                 Fatal("Expected \",\" seperating x,y pair");
517         }
518         y = RealFromStr(GetToken(d,token,i,delims));
519 }
520
521 static bool GetKeyValuePair(const string & d, string & key, string & value, unsigned & i, const string & delims = "()[],{}<>;:=")
522 {
523         key = "";
524         string token;
525         while (GetToken(d, token, i, delims) == ":" || token == ";");
526         key = token;
527         if (GetToken(d, token, i, delims) != ":")
528         {
529                 Error("Expected \":\" seperating key:value pair");
530                 return false;
531         }
532         value = "";
533         GetToken(d, value, i, delims);
534         return true;
535 }
536
537 static void TransformXYPair(Real & x, Real & y, const SVGMatrix & transform)
538 {
539         Real x0(x);
540         x = transform.a * x + transform.c * y + transform.e;
541         y = transform.b * x0 + transform.d * y + transform.f;
542 }
543
544 void Document::ParseSVGTransform(const string & s, SVGMatrix & transform)
545 {
546         //Debug("Parsing transform %s", s.c_str());
547         string token;
548         string command;
549         unsigned i = 0;
550         
551         while (i < s.size())
552         {
553                 GetToken(s, command, i);
554                 if (command == "," || command == "" || command == ":")
555                 {
556                         if (i < s.size())
557                                 GetToken(s, command, i);
558                         else
559                                 return;
560                 }
561                 //Debug("Token is \"%s\"", command.c_str());
562         
563                 SVGMatrix delta = {1,0,0,0,1,0};
564         
565         
566                 assert(GetToken(s,token, i) == "(");
567                 if (command == "translate")
568                 {
569                         GetXYPair(s, delta.e, delta.f, i);
570                         assert(GetToken(s,token, i) == ")");    
571                 }
572                 else if (command == "matrix")
573                 {
574                         GetXYPair(s, delta.a, delta.b,i);
575                         GetXYPair(s, delta.c, delta.d,i);
576                         GetXYPair(s, delta.e, delta.f,i);
577                         assert(GetToken(s,token, i) == ")");    
578                 }
579                 else if (command == "scale")
580                 {
581                         delta.a = RealFromStr(GetToken(s,token,i));
582                         GetToken(s, token, i);
583                         if (token == ",")
584                         {
585                                 delta.d = RealFromStr(GetToken(s,token,i));
586                                 assert(GetToken(s, token, i) == ")");
587                         }
588                         else
589                         {
590                                 delta.d = delta.a;
591                                 assert(token == ")");
592                         }
593                         
594                 }
595                 else
596                 {
597                         Warn("Unrecognised transform \"%s\", using identity", command.c_str());
598                 }
599         
600                 //Debug("Old transform is {%f,%f,%f,%f,%f,%f}", transform.a, transform.b, transform.c, transform.d,transform.e,transform.f);
601                 //Debug("Delta transform is {%f,%f,%f,%f,%f,%f}", delta.a, delta.b, delta.c, delta.d,delta.e,delta.f);
602         
603                 SVGMatrix old(transform);
604                 transform.a = old.a * delta.a + old.c * delta.b;
605                 transform.c = old.a * delta.c + old.c * delta.d;
606                 transform.e = old.a * delta.e + old.c * delta.f + old.e;
607         
608                 transform.b = old.b * delta.a + old.d * delta.b;
609                 transform.d = old.b * delta.c + old.d * delta.d;
610                 transform.f = old.b * delta.e + old.d * delta.f + old.f;
611         
612                 //Debug("New transform is {%f,%f,%f,%f,%f,%f}", transform.a, transform.b, transform.c, transform.d,transform.e,transform.f);
613         }
614 }
615
616 inline Colour ParseColourString(const string & colour_str)
617 {
618         Colour c(0,0,0,0);
619         if (colour_str == "red")
620                 c = {255,0,0,255};
621         else if (colour_str == "blue")
622                 c = {0,0,255,255};
623         else if (colour_str == "green")
624                 c = {0,255,0,255};
625         else if (colour_str == "black")
626                 c = {0,0,0,255};
627         else if (colour_str == "white")
628                 c = {255,255,255,255};
629         else if (colour_str.size() == 7 && colour_str[0] == '#')
630         {
631                 //Debug("Parse colour string: \"%s\"", colour_str.c_str());
632                 char comp[3] = {colour_str[1], colour_str[2], '\0'};
633                 c.r = strtoul(comp, NULL, 16);
634                 comp[0] = colour_str[3]; comp[1] = colour_str[4];
635                 c.g = strtoul(comp, NULL, 16);
636                 comp[0] = colour_str[5]; comp[1] = colour_str[6];
637                 c.b = strtoul(comp, NULL, 16);
638                 c.a = 255;
639                 //Debug("Colour is: %u, %u, %u, %u", c.r, c.g, c.b, c.a);
640         }
641         return c;
642 }
643
644 void Document::ParseSVGNode(pugi::xml_node & root, SVGMatrix & parent_transform)
645 {
646         //Debug("Parse node <%s>", root.name());
647
648         
649         // Centre the SVGs
650         if (strcmp(root.name(),"svg") == 0)
651         {
652                 Real ww = RealFromStr(root.attribute("width").as_string());
653                 Real hh = RealFromStr(root.attribute("height").as_string());
654                 parent_transform.e -= parent_transform.a * ww/Real(2);
655                 parent_transform.f -= parent_transform.d * hh/Real(2);
656         }
657         
658         for (pugi::xml_node child = root.first_child(); child; child = child.next_sibling())
659         {
660                 SVGMatrix transform(parent_transform);  
661                 pugi::xml_attribute attrib_trans = child.attribute("transform");
662                 if (!attrib_trans.empty())
663                 {
664                         ParseSVGTransform(attrib_trans.as_string(), transform);
665                 }
666                 
667                 
668                 
669                 if (strcmp(child.name(), "svg") == 0 || strcmp(child.name(),"g") == 0
670                         || strcmp(child.name(), "group") == 0)
671                 {
672                         
673                         ParseSVGNode(child, transform);
674                         continue;
675                 }
676                 else if (strcmp(child.name(), "path") == 0)
677                 {
678                         string d = child.attribute("d").as_string();
679                         //Debug("Path data attribute is \"%s\"", d.c_str());
680                         bool closed = false;
681                         pair<unsigned, unsigned> range = ParseSVGPathData(d, transform, closed);
682                         if (true && range.first < m_count && range.second < m_count)//(closed)
683                         {
684                                 
685                                 string colour_str("");
686                                 map<string, string> style;
687                                 if (child.attribute("style"))
688                                 {
689                                         ParseSVGStyleData(child.attribute("style").as_string(), style);
690                                 }
691                                 
692                                 // Determine shading colour
693                                 if (child.attribute("fill"))
694                                 {
695                                         colour_str = child.attribute("fill").as_string();
696                                 }
697                                 else if (style.find("fill") != style.end())
698                                 {
699                                         colour_str = style["fill"];
700                                 }
701                                 Colour fill = ParseColourString(colour_str);
702                                 Colour stroke = fill;
703                         
704                                 if (child.attribute("stroke"))
705                                 {
706                                         colour_str = child.attribute("stroke").as_string();
707                                         stroke = ParseColourString(colour_str);
708                                 }
709                                 else if (style.find("stroke") != style.end())
710                                 {
711                                         colour_str = style["stroke"];
712                                         stroke = ParseColourString(colour_str);
713                                 }
714                                 
715                                 
716                                 // Determin shading alpha
717                                 if (child.attribute("fill-opacity"))
718                                 {
719                                         fill.a = 255*child.attribute("fill-opacity").as_float();
720                                 }
721                                 else if (style.find("fill-opacity") != style.end())
722                                 {
723                                         fill.a = 255*strtod(style["fill-opacity"].c_str(), NULL);
724                                 }
725                                 if (child.attribute("stroke-opacity"))
726                                 {
727                                         stroke.a = 255*child.attribute("stroke-opacity").as_float();
728                                 }
729                                 else if (style.find("stroke-opacity") != style.end())
730                                 {
731                                         stroke.a = 255*strtod(style["stroke-opacity"].c_str(), NULL);
732                                 }
733                                 AddPath(range.first, range.second, fill, stroke);
734                         }
735                         
736                 }
737                 else if (strcmp(child.name(), "line") == 0)
738                 {
739                         Real x0(child.attribute("x1").as_float());
740                         Real y0(child.attribute("y1").as_float());
741                         Real x1(child.attribute("x2").as_float());
742                         Real y1(child.attribute("y2").as_float());
743                         TransformXYPair(x0,y0,transform);
744                         TransformXYPair(x1,y1,transform);
745                         AddBezier(Bezier(x0,y0,x1,y1,x1,y1,x1,y1));
746                 }
747                 else if (strcmp(child.name(), "rect") == 0)
748                 {
749                         Real coords[4];
750                         const char * attrib_names[] = {"x", "y", "width", "height"};
751                         for (size_t i = 0; i < 4; ++i)
752                                 coords[i] = child.attribute(attrib_names[i]).as_float();
753                         
754                         Real x2(coords[0]+coords[2]);
755                         Real y2(coords[1]+coords[3]);
756                         TransformXYPair(coords[0],coords[1],transform); // x, y, transform
757                         TransformXYPair(x2,y2,transform);
758                         coords[2] = x2 - coords[0];
759                         coords[3] = y2 - coords[1];
760                         
761                         bool outline = !(child.attribute("fill") && strcmp(child.attribute("fill").as_string(),"none") != 0);
762                         Add(outline?RECT_OUTLINE:RECT_FILLED, Rect(coords[0], coords[1], coords[2], coords[3]),0);
763                 }
764                 else if (strcmp(child.name(), "circle") == 0)
765                 {
766                         Real cx = child.attribute("cx").as_float();
767                         Real cy = child.attribute("cy").as_float();
768                         Real r = child.attribute("r").as_float();
769                         
770                         Real x = (cx - r);
771                         Real y = (cy - r);
772                         TransformXYPair(x,y,transform);
773                         Real w = Real(2)*r*transform.a; // width scales
774                         Real h = Real(2)*r*transform.d; // height scales
775                         
776                         
777                         Rect rect(x,y,w,h);
778                         Add(CIRCLE_FILLED, rect,0);
779                         Debug("Added Circle %s", rect.Str().c_str());                   
780                 }
781                 else if (strcmp(child.name(), "text") == 0)
782                 {
783                         Real x = child.attribute("x").as_float();
784                         Real y = child.attribute("y").as_float();
785                         TransformXYPair(x,y,transform);
786                         Debug("Add text \"%s\"", child.child_value());
787                         AddText(child.child_value(), 0.05, x, y);
788                 }
789         }
790 }
791
792 void Document::ParseSVGStyleData(const string & style, map<string, string> & results)
793 {
794         unsigned i = 0;
795         string key;
796         string value;
797         while (i < style.size() && GetKeyValuePair(style, key, value, i))
798         {
799                 results[key] = value;
800         }
801 }
802
803 /**
804  * Parse an SVG string into a rectangle
805  */
806 void Document::ParseSVG(const string & input, const Rect & bounds)
807 {
808         using namespace pugi;
809         
810         xml_document doc_xml;
811         xml_parse_result result = doc_xml.load(input.c_str());
812         
813         if (!result)
814                 Error("Couldn't parse SVG input - %s", result.description());
815                 
816         Debug("Loaded XML - %s", result.description());
817         SVGMatrix transform = {bounds.w, 0,bounds.x, 0,bounds.h,bounds.y};
818         ParseSVGNode(doc_xml, transform);
819 }
820
821 /**
822  * Load an SVG into a rectangle
823  */
824 void Document::LoadSVG(const string & filename, const Rect & bounds)
825 {
826         using namespace pugi;
827         
828         xml_document doc_xml;
829         ifstream input(filename.c_str(), ios_base::in);
830         xml_parse_result result = doc_xml.load(input);
831         
832         if (!result)
833                 Error("Couldn't load \"%s\" - %s", filename.c_str(), result.description());
834                 
835         Debug("Loaded XML from \"%s\" - %s", filename.c_str(), result.description());
836         
837         input.close();
838                                                 // a c e, b d f
839         SVGMatrix transform = {bounds.w,0 ,bounds.x, 0,bounds.h,bounds.y};
840         ParseSVGNode(doc_xml, transform);
841 }
842
843
844
845 // Fear the wrath of the tokenizing svg data
846 // Seriously this isn't really very DOM-like at all is it?
847 pair<unsigned, unsigned> Document::ParseSVGPathData(const string & d, const SVGMatrix & transform, bool & closed)
848 {
849         closed = false;
850         Real x[4] = {0,0,0,0};
851         Real y[4] = {0,0,0,0};
852         
853         string token("");
854         string command("m");
855         
856         Real x0(0);
857         Real y0(0);
858         
859         unsigned i = 0;
860         unsigned prev_i = 0;
861         
862         bool start = false;
863         
864
865         static string delims("()[],{}<>;:=LlHhVvmMqQzZcC");
866
867         pair<unsigned, unsigned> range(m_count, m_count);
868         
869         while (i < d.size() && GetToken(d, token, i, delims).size() > 0)
870         {
871                 if (isalpha(token[0]))
872                         command = token;
873                 else
874                 {
875                         i = prev_i; // hax
876                         if(command == "")
877                                 command = "L";
878                 }
879                 
880                 bool relative = islower(command[0]);
881                         
882                 if (command == "m" || command == "M")
883                 {
884                         //Debug("Construct moveto command");
885                         Real dx = RealFromStr(GetToken(d,token,i,delims));
886                         assert(GetToken(d,token,i,delims) == ",");
887                         Real dy = RealFromStr(GetToken(d,token,i,delims));
888                         
889                         x[0] = (relative) ? x[0] + dx : dx;
890                         y[0] = (relative) ? y[0] + dy : dy;
891                         
892                         x0 = x[0];
893                         y0 = y[0];
894                         //Debug("mmoveto %f,%f", Float(x[0]),Float(y[0]));
895                         command = (command == "m") ? "l" : "L";
896                 }
897                 else if (command == "c" || command == "C" || command == "q" || command == "Q")
898                 {
899                         //Debug("Construct curveto command");
900                         Real dx = RealFromStr(GetToken(d,token,i,delims));
901                         assert(GetToken(d,token,i,delims) == ",");
902                         Real dy = RealFromStr(GetToken(d,token,i,delims));
903                         
904                         x[1] = (relative) ? x[0] + dx : dx;
905                         y[1] = (relative) ? y[0] + dy : dy;
906                         
907                         dx = RealFromStr(GetToken(d,token,i,delims));
908                         assert(GetToken(d,token,i,delims) == ",");
909                         dy = RealFromStr(GetToken(d,token,i,delims));
910                         
911                         x[2] = (relative) ? x[0] + dx : dx;
912                         y[2] = (relative) ? y[0] + dy : dy;
913                         
914                         if (command != "q" && command != "Q")
915                         {
916                                 dx = RealFromStr(GetToken(d,token,i,delims));
917                                 assert(GetToken(d,token,i,delims) == ",");
918                                 dy = RealFromStr(GetToken(d,token,i,delims));
919                                 x[3] = (relative) ? x[0] + dx : dx;
920                                 y[3] = (relative) ? y[0] + dy : dy;
921                         }
922                         else
923                         {
924                                 x[3] = x[2];
925                                 y[3] = y[2];
926                                 Real old_x1(x[1]), old_y1(y[1]);
927                                 x[1] = x[0] + Real(2) * (old_x1 - x[0])/ Real(3);
928                                 y[1] = y[0] + Real(2) * (old_y1 - y[0])/ Real(3);
929                                 x[2] = x[3] + Real(2) * (old_x1 - x[3])/ Real(3);
930                                 y[2] = y[3] + Real(2) * (old_y1 - y[3])/ Real(3);
931                         }
932                         
933                         Real x3(x[3]);
934                         Real y3(y[3]);
935                         for (int j = 0; j < 4; ++j)
936                                 TransformXYPair(x[j],y[j], transform);
937
938                         range.second = AddBezier(Bezier(x[0],y[0],x[1],y[1],x[2],y[2],x[3],y[3]));
939                         
940                         //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]));
941                         
942                         x[0] = x3;
943                         y[0] = y3;
944
945                         
946                 }
947                 else if (command == "l" || command == "L" || command == "h" || command == "H" || command == "v" || command == "V")
948                 {
949                         //Debug("Construct lineto command, relative %d", relative);
950                 
951                         Real dx = RealFromStr(GetToken(d,token,i,delims));
952                         Real dy = 0;
953                         if (command == "l" || command == "L")
954                         {
955                                 assert(GetToken(d,token,i,delims) == ",");
956                                 dy = RealFromStr(GetToken(d,token,i,delims));
957                         }
958                         else if (command == "v" || command == "V")
959                         {
960                                 swap(dx,dy);
961                         }
962                         
963                         x[1] = (relative) ? x[0] + dx : dx;
964                         y[1] = (relative) ? y[0] + dy : dy;
965                         if (command == "v" || command == "V")
966                         {
967                                 x[1] = x[0];
968                         }
969                         else if (command == "h" || command == "H")
970                         {
971                                 y[1] = y[0];
972                         }
973                         
974                         Real x1(x[1]);
975                         Real y1(y[1]);
976                         
977                         TransformXYPair(x[0],y[0],transform);
978                         TransformXYPair(x[1],y[1],transform);
979
980
981                         range.second = AddBezier(Bezier(x[0],y[0],x[1],y[1],x[1],y[1],x[1],y[1]));
982                         
983                         //Debug("[%u] lineto %f,%f %f,%f", index, Float(x[0]),Float(y[0]),Float(x[1]),Float(y[1]));
984                         
985                         x[0] = x1;
986                         y[0] = y1;
987
988                 }
989                 else if (command == "z" || command == "Z")
990                 {
991                         //Debug("Construct returnto command");
992                         x[1] = x0;
993                         y[1] = y0;
994                         x[2] = x0;
995                         y[2] = y0;
996                         x[3] = x0;
997                         y[3] = y0;
998                         
999                         Real x3(x[3]);
1000                         Real y3(y[3]);
1001                         for (int j = 0; j < 4; ++j)
1002                                 TransformXYPair(x[j],y[j], transform);
1003
1004                         range.second = AddBezier(Bezier(x[0],y[0],x[1],y[1],x[2],y[2],x[3],y[3]));
1005                         //Debug("[%u] returnto %f,%f %f,%f", index, Float(x[0]),Float(y[0]),Float(x[1]),Float(y[1]));
1006                         
1007                         x[0] = x3;
1008                         y[0] = y3;
1009                         command = "m";
1010                         closed = true;
1011                 }
1012                 else
1013                 {
1014                         Warn("Unrecognised command \"%s\", set to \"m\"", command.c_str());
1015                         command = "m";
1016                 }
1017                 
1018                 if (!start)
1019                 {
1020                         x0 = x[0];
1021                         y0 = y[0];
1022                         start = true;
1023                 }
1024                 prev_i = i;
1025         }
1026         return range;
1027 }
1028
1029 void Document::SetFont(const string & font_filename)
1030 {
1031         if (m_font_data != NULL)
1032         {
1033                 free(m_font_data);
1034         }
1035         
1036         FILE *font_file = fopen(font_filename.c_str(), "rb");
1037         fseek(font_file, 0, SEEK_END);
1038         size_t font_file_size = ftell(font_file);
1039         fseek(font_file, 0, SEEK_SET);
1040         m_font_data = (unsigned char*)malloc(font_file_size);
1041         size_t read = fread(m_font_data, 1, font_file_size, font_file);
1042         if (read != font_file_size)
1043         {
1044                 Fatal("Failed to read font data from \"%s\" - Read %u bytes expected %u - %s", font_filename.c_str(), read, font_file_size, strerror(errno));
1045         }
1046         fclose(font_file);
1047         stbtt_InitFont(&m_font, m_font_data, 0);
1048 }
1049
1050 void Document::AddText(const string & text, Real scale, Real x, Real y)
1051 {
1052         if (m_font_data == NULL)
1053         {
1054                 Warn("No font loaded");
1055                 return;
1056         }
1057                 
1058         Real x0(x);
1059         //Real y0(y);
1060         int ascent = 0, descent = 0, line_gap = 0;
1061         stbtt_GetFontVMetrics(&m_font, &ascent, &descent, &line_gap);
1062         Real font_scale = scale;
1063         font_scale /= Real(ascent - descent);
1064         Real y_advance = Real(font_scale) * Real(ascent - descent + line_gap);
1065         for (unsigned i = 0; i < text.size(); ++i)
1066         {
1067                 if (text[i] == '\n')
1068                 {
1069                         y += y_advance;
1070                         x = x0;
1071                 }
1072                 if (!isprint(text[i]))
1073                         continue;
1074                         
1075                 int advance_width = 0, left_side_bearing = 0, kerning = 0;
1076                 stbtt_GetCodepointHMetrics(&m_font, text[i], &advance_width, &left_side_bearing);
1077                 if (i >= 1)
1078                 {
1079                         kerning = stbtt_GetCodepointKernAdvance(&m_font, text[i-1], text[i]);
1080                 }
1081                 x += font_scale * Real(kerning);
1082                 AddFontGlyphAtPoint(&m_font, text[i], font_scale, x, y);
1083                 x += font_scale * Real(advance_width);
1084         }
1085 }
1086
1087 void Document::AddFontGlyphAtPoint(stbtt_fontinfo *font, int character, Real scale, Real x, Real y)
1088 {
1089         int glyph_index = stbtt_FindGlyphIndex(font, character);
1090
1091         // Check if there is actully a glyph to render.
1092         if (stbtt_IsGlyphEmpty(font, glyph_index))
1093         {
1094                 return;
1095         }
1096
1097         stbtt_vertex *instructions;
1098         int num_instructions = stbtt_GetGlyphShape(font, glyph_index, &instructions);
1099
1100         Real current_x(0), current_y(0);
1101         unsigned start_index = m_count;
1102         unsigned end_index = m_count;
1103         for (int i = 0; i < num_instructions; ++i)
1104         {
1105                 // TTF uses 16-bit signed ints for coordinates:
1106                 // with the y-axis inverted compared to us.
1107                 // Convert and scale any data.
1108                 Real inst_x = Real(instructions[i].x)*scale;
1109                 Real inst_y = Real(instructions[i].y)*-scale;
1110                 Real inst_cx = Real(instructions[i].cx)*scale;
1111                 Real inst_cy = Real(instructions[i].cy)*-scale;
1112                 Real old_x(current_x), old_y(current_y);
1113                 current_x = inst_x;
1114                 current_y = inst_y;
1115                 
1116                 switch(instructions[i].type)
1117                 {
1118                 // Move To
1119                 case STBTT_vmove:
1120                         break;
1121                 // Line To
1122                 case STBTT_vline:
1123                         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));
1124                         break;
1125                 // Quadratic Bezier To:
1126                 case STBTT_vcurve:
1127                         // Quadratic -> Cubic:
1128                         // - Endpoints are the same.
1129                         // - cubic1 = quad0+(2/3)*(quad1-quad0)
1130                         // - cubic2 = quad2+(2/3)*(quad1-quad2)
1131                         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,
1132                                                 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));
1133                         break;
1134                 }
1135         }
1136         
1137         if (start_index < m_count && end_index < m_count)
1138         {
1139                 AddPath(start_index, end_index);
1140         }
1141         //Debug("Added Glyph \"%c\" at %f %f, scale %f", (char)character, Float(x), Float(y), Float(scale));
1142
1143         stbtt_FreeShape(font, instructions);
1144 }
1145
1146 void Document::TransformObjectBounds(const SVGMatrix & transform)
1147 {
1148         for (unsigned i = 0; i < m_count; ++i)
1149         {
1150                 TransformXYPair(m_objects.bounds[i].x, m_objects.bounds[i].y, transform);
1151                 m_objects.bounds[i].w *= transform.a;
1152                 m_objects.bounds[i].h *= transform.d;
1153         }
1154 }
1155
1156 void Document::TranslateObjects(const Real & dx, const Real & dy, ObjectType type)
1157 {
1158         #ifdef TRANSFORM_BEZIERS_TO_PATH
1159                 for (unsigned i = 0; i < m_objects.paths.size(); ++i)
1160                 {
1161                         Path & p = m_objects.paths[i];
1162                         p.m_bounds.x += dx;
1163                         p.m_bounds.y += dy;
1164                 }
1165                 return;
1166         #endif  
1167         
1168         for (unsigned i = 0; i < m_count; ++i)
1169         {
1170                 if (type == NUMBER_OF_OBJECT_TYPES || m_objects.types[i] == type)
1171                 {
1172                         m_objects.bounds[i].x += dx;
1173                         m_objects.bounds[i].y += dy;
1174                 }
1175         }
1176 }
1177
1178 void Document::ScaleObjectsAboutPoint(const Real & x, const Real & y, const Real & scale_amount, ObjectType type)
1179 {
1180         #ifdef TRANSFORM_BEZIERS_TO_PATH
1181                 for (unsigned i = 0; i < m_objects.paths.size(); ++i)
1182                 {
1183                         Path & p = m_objects.paths[i];
1184                         p.m_bounds.w /= scale_amount;
1185                         p.m_bounds.h /= scale_amount;
1186                         p.m_bounds.x -= x;
1187                         p.m_bounds.x /= scale_amount;
1188                         p.m_bounds.x += x;
1189                         p.m_bounds.y -= y;
1190                         p.m_bounds.y /= scale_amount;
1191                         p.m_bounds.y += y;
1192                 }
1193                 return;
1194         #endif
1195         
1196         for (unsigned i = 0; i < m_count; ++i)
1197         {
1198                 if (type != NUMBER_OF_OBJECT_TYPES && m_objects.types[i] != type)
1199                         continue;
1200                 
1201                 m_objects.bounds[i].w /= scale_amount;
1202                 m_objects.bounds[i].h /= scale_amount;
1203                 //m_objects.bounds[i].x = x + (m_objects.bounds[i].x-x)/scale_amount;
1204                 //m_objects.bounds[i].y = y + (m_objects.bounds[i].y-x)/scale_amount;
1205                 m_objects.bounds[i].x -= x;
1206                 m_objects.bounds[i].x /= scale_amount;
1207                 m_objects.bounds[i].x += x;
1208                 
1209                 m_objects.bounds[i].y -= y;
1210                 m_objects.bounds[i].y /= scale_amount;
1211                 m_objects.bounds[i].y += y;
1212         }
1213
1214 }
1215
1216

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