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

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