Deal with groups in SVG parsing
[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
8 #include "stb_truetype.h"
9
10 using namespace IPDF;
11 using namespace std;
12
13 //TODO: Make this work for variable sized Reals
14
15 // Loads an std::vector<T> of size num_elements from a file.
16 template<typename T>
17 static void LoadStructVector(FILE *src_file, size_t num_elems, std::vector<T>& dest)
18 {
19         size_t structsread = 0;
20         dest.resize(num_elems);
21         structsread = fread(dest.data(), sizeof(T), num_elems, src_file);
22         if (structsread != num_elems)
23                 Fatal("Only read %u structs (expected %u)!", structsread, num_elems);
24 }
25
26 // Saves an std::vector<T> to a file. Size must be saves separately.
27 template<typename T>
28 static void SaveStructVector(FILE *dst_file, std::vector<T>& src)
29 {
30         size_t written = 0;
31         written = fwrite(src.data(), sizeof(T), src.size(), dst_file);
32         if (written != src.size())
33                 Fatal("Only wrote %u structs (expected %u)!", written, src.size());
34 }
35
36 static void WriteChunkHeader(FILE *dst_file, DocChunkTypes type, uint32_t size)
37 {
38         size_t written = 0;
39         written = fwrite(&type, sizeof(type), 1, dst_file);
40         if (written != 1)
41                 Fatal("Could not write Chunk header! (ID)");
42         written = fwrite(&size, sizeof(size), 1, dst_file);
43         if (written != 1)
44                 Fatal("Could not write Chunk header (size)!");
45 }
46
47 static bool ReadChunkHeader(FILE *src_file, DocChunkTypes& type, uint32_t& size)
48 {
49         if (fread(&type, sizeof(DocChunkTypes), 1, src_file) != 1)
50                 return false;
51         if (fread(&size, sizeof(uint32_t), 1, src_file) != 1)
52                 return false;
53         return true;
54 }
55
56 void Document::Save(const string & filename)
57 {
58         Debug("Saving document to file \"%s\"...", filename.c_str());
59         FILE * file = fopen(filename.c_str(), "w");
60         if (file == NULL)
61                 Fatal("Couldn't open file \"%s\" - %s", filename.c_str(), strerror(errno));
62
63         size_t written;
64         Debug("Number of objects (%u)...", ObjectCount());
65         WriteChunkHeader(file, CT_NUMOBJS, sizeof(m_count));
66         written = fwrite(&m_count, sizeof(m_count), 1, file);
67         if (written != 1)
68                 Fatal("Failed to write number of objects!");
69
70         Debug("Object types...");
71         WriteChunkHeader(file, CT_OBJTYPES, m_objects.types.size() * sizeof(ObjectType));
72         SaveStructVector<ObjectType>(file, m_objects.types);
73
74         Debug("Object bounds...");
75         WriteChunkHeader(file, CT_OBJBOUNDS, m_objects.bounds.size() * sizeof(Rect));
76         SaveStructVector<Rect>(file, m_objects.bounds);
77
78         Debug("Object data indices...");
79         WriteChunkHeader(file, CT_OBJINDICES, m_objects.data_indices.size() * sizeof(unsigned));
80         SaveStructVector<unsigned>(file, m_objects.data_indices);
81         
82         Debug("Bezier data...");
83         WriteChunkHeader(file, CT_OBJBEZIERS, m_objects.beziers.size() * sizeof(uint8_t));
84         SaveStructVector<Bezier>(file, m_objects.beziers);
85
86         int err = fclose(file);
87         if (err != 0)
88                 Fatal("Failed to close file \"%s\" - %s", filename.c_str(), strerror(err));
89
90         Debug("Successfully saved %u objects to \"%s\"", ObjectCount(), filename.c_str());
91 }
92
93 #ifndef QUADTREE_DISABLED
94
95 void Document::GenBaseQuadtree()
96 {
97         m_quadtree.nodes.push_back(QuadTreeNode{QUADTREE_EMPTY, QUADTREE_EMPTY, QUADTREE_EMPTY, QUADTREE_EMPTY, QUADTREE_EMPTY, QTC_UNKNOWN, 0, ObjectCount()});
98         m_quadtree.root_id = 0;
99         GenQuadChild(0, QTC_TOP_LEFT);
100         GenQuadParent(0, QTC_BOTTOM_RIGHT);
101 }
102
103 QuadTreeIndex Document::GenQuadChild(QuadTreeIndex parent, QuadTreeNodeChildren type)
104 {
105         QuadTreeIndex new_index = m_quadtree.nodes.size();
106         m_quadtree.nodes.push_back(QuadTreeNode{QUADTREE_EMPTY, QUADTREE_EMPTY, QUADTREE_EMPTY, QUADTREE_EMPTY, parent, type, 0, 0});
107
108         m_quadtree.nodes[new_index].object_begin = m_objects.bounds.size();
109         for (unsigned i = m_quadtree.nodes[parent].object_begin; i < m_quadtree.nodes[parent].object_end; ++i)
110         {
111                 if (ContainedInQuadChild(m_objects.bounds[i], type))
112                 {
113                         m_objects.bounds.push_back(TransformToQuadChild(m_objects.bounds[i], type));
114                         m_objects.types.push_back(m_objects.types[i]);
115                         m_objects.data_indices.push_back(m_objects.data_indices[i]);
116                         m_count++;
117                 }
118         }
119         m_quadtree.nodes[new_index].object_end = m_objects.bounds.size();
120         switch (type)
121         {
122                 case QTC_TOP_LEFT:
123                         m_quadtree.nodes[parent].top_left = new_index;
124                         break;
125                 case QTC_TOP_RIGHT:
126                         m_quadtree.nodes[parent].top_right = new_index;
127                         break;
128                 case QTC_BOTTOM_LEFT:
129                         m_quadtree.nodes[parent].bottom_left = new_index;
130                         break;
131                 case QTC_BOTTOM_RIGHT:
132                         m_quadtree.nodes[parent].bottom_right = new_index;
133                         break;
134                 default:
135                         Fatal("Tried to add a QuadTree child of invalid type!");
136         }
137         return new_index;
138 }
139
140 // Reparent a quadtree node, making it the "type" child of a new node.
141 QuadTreeIndex Document::GenQuadParent(QuadTreeIndex child, QuadTreeNodeChildren type)
142 {
143         QuadTreeIndex new_index = m_quadtree.nodes.size();
144         m_quadtree.nodes.push_back(QuadTreeNode{QUADTREE_EMPTY, QUADTREE_EMPTY, QUADTREE_EMPTY, QUADTREE_EMPTY, -1, QTC_UNKNOWN, 0, 0});
145
146         m_quadtree.nodes[new_index].object_begin = m_objects.bounds.size();
147         for (unsigned i = m_quadtree.nodes[child].object_begin; i < m_quadtree.nodes[child].object_end; ++i)
148         {
149                 m_objects.bounds.push_back(TransformFromQuadChild(m_objects.bounds[i], type));
150                 m_objects.types.push_back(m_objects.types[i]);
151                 m_objects.data_indices.push_back(m_objects.data_indices[i]);
152                 m_count++;
153         }
154         m_quadtree.nodes[new_index].object_end = m_objects.bounds.size();
155         switch (type)
156         {
157                 case QTC_TOP_LEFT:
158                         m_quadtree.nodes[new_index].top_left = child;
159                         break;
160                 case QTC_TOP_RIGHT:
161                         m_quadtree.nodes[new_index].top_right = child;
162                         break;
163                 case QTC_BOTTOM_LEFT:
164                         m_quadtree.nodes[new_index].bottom_left = child;
165                         break;
166                 case QTC_BOTTOM_RIGHT:
167                         m_quadtree.nodes[new_index].bottom_right = child;
168                         break;
169                 default:
170                         Fatal("Tried to add a QuadTree child of invalid type!");
171         }
172         return new_index;
173 }
174
175 #endif
176
177 void Document::Load(const string & filename)
178 {
179         m_objects.bounds.clear();
180         m_count = 0;
181         if (filename == "")
182         {
183                 Debug("Loaded empty document.");
184                 return;
185         }
186         Debug("Loading document from file \"%s\"", filename.c_str());
187         FILE * file = fopen(filename.c_str(), "r");
188         if (file == NULL)
189                 Fatal("Couldn't open file \"%s\"", filename.c_str(), strerror(errno));
190
191         size_t read;
192
193         DocChunkTypes chunk_type;
194         uint32_t chunk_size;
195         while (ReadChunkHeader(file, chunk_type, chunk_size))
196         {
197                 switch(chunk_type)
198                 {
199                 case CT_NUMOBJS:
200                         read = fread(&m_count, sizeof(m_count), 1, file);
201                         if (read != 1)
202                                 Fatal("Failed to read number of objects!");
203                         Debug("Number of objects: %u", ObjectCount());
204                         break;
205                 case CT_OBJTYPES:
206                         Debug("Object types...");
207                         LoadStructVector<ObjectType>(file, chunk_size/sizeof(ObjectType), m_objects.types);
208                         break;
209                 case CT_OBJBOUNDS:
210                         Debug("Object bounds...");
211                         LoadStructVector<Rect>(file, chunk_size/sizeof(Rect), m_objects.bounds);
212                         break;
213                 case CT_OBJINDICES:
214                         Debug("Object data indices...");
215                         LoadStructVector<unsigned>(file, chunk_size/sizeof(unsigned), m_objects.data_indices);
216                         break;
217                 case CT_OBJBEZIERS:
218                         Debug("Bezier data...");
219                         LoadStructVector<Bezier>(file, chunk_size/sizeof(Bezier), m_objects.beziers);
220                         break;
221                 }
222         }
223         Debug("Successfully loaded %u objects from \"%s\"", ObjectCount(), filename.c_str());
224 #ifndef QUADTREE_DISABLED
225         if (m_quadtree.root_id == QUADTREE_EMPTY)
226         {
227                 GenBaseQuadtree();
228         }
229 #endif
230 }
231
232 void Document::Add(ObjectType type, const Rect & bounds, unsigned data_index)
233 {
234         m_objects.types.push_back(type);
235         m_objects.bounds.push_back(bounds);
236         m_objects.data_indices.push_back(data_index);
237         ++m_count; // Why can't we just use the size of types or something?
238 }
239
240 unsigned Document::AddBezierData(const Bezier & bezier)
241 {
242         m_objects.beziers.push_back(bezier);
243         return m_objects.beziers.size()-1;
244 }
245
246
247 void Document::DebugDumpObjects()
248 {
249         Debug("Objects for Document %p are:", this);
250         for (unsigned id = 0; id < ObjectCount(); ++id)
251         {
252                 Debug("%u. \tType: %u\tBounds: %s", id, m_objects.types[id], m_objects.bounds[id].Str().c_str());
253         }
254 }
255
256 bool Document::operator==(const Document & equ) const
257 {
258         return (ObjectCount() == equ.ObjectCount() 
259                 && memcmp(m_objects.bounds.data(), equ.m_objects.bounds.data(), ObjectCount() * sizeof(Rect)) == 0
260                 && memcmp(m_objects.data_indices.data(), equ.m_objects.data_indices.data(), ObjectCount() * sizeof(unsigned)) == 0
261                 && memcmp(m_objects.beziers.data(), equ.m_objects.beziers.data(), m_objects.beziers.size() * sizeof(Bezier)) == 0);
262 }
263
264
265
266
267 void Document::ParseSVGNode(pugi::xml_node & root, const Rect & bounds, Real & width, Real & height)
268 {
269         Debug("Parse node <%s>", root.name());
270         pugi::xml_attribute attrib_w = root.attribute("width");
271         pugi::xml_attribute attrib_h = root.attribute("height");
272         if (!attrib_w.empty())
273                 width = attrib_w.as_float() * bounds.w;
274         if (!attrib_h.empty())
275                 height = attrib_h.as_float() * bounds.h;
276                         
277         for (pugi::xml_node child = root.first_child(); child; child = child.next_sibling())
278         {
279
280                 
281                 if (strcmp(child.name(), "svg") == 0 || strcmp(child.name(),"g") == 0
282                         || strcmp(child.name(), "group") == 0)
283                 {
284                         //TODO: Handle translates etc here
285                         ParseSVGNode(child, bounds, width, height);
286                         continue;
287                 }
288                 else if (strcmp(child.name(), "path") == 0)
289                 {
290                         string d = child.attribute("d").as_string();
291                         Debug("Path data attribute is \"%s\"", d.c_str());
292                         ParseSVGPathData(d, Rect(bounds.x,bounds.y,width,height));
293                 }
294                 else if (strcmp(child.name(), "rect") == 0)
295                 {
296                         Real coords[4];
297                         const char * attrib_names[] = {"x", "y", "width", "height"};
298                         for (size_t i = 0; i < 4; ++i)
299                                 coords[i] = child.attribute(attrib_names[i]).as_float();
300                         
301                         bool outline = !(child.attribute("fill"));
302                         Add(outline?RECT_OUTLINE:RECT_FILLED, Rect(coords[0]/width + bounds.x, coords[1]/height + bounds.y, coords[2]/width, coords[3]/height),0);
303                 }
304                 else if (strcmp(child.name(), "circle") == 0)
305                 {
306                         Real cx = child.attribute("cx").as_float();
307                         Real cy = child.attribute("cy").as_float();
308                         Real r = child.attribute("r").as_float();
309                         
310                         Real x = (cx - r)/width + bounds.x; 
311                         Real y = (cy - r)/height + bounds.y; 
312                         Real w = Real(2)*r/width; 
313                         Real h = Real(2)*r/height;
314                         
315                         Rect rect(x,y,w,h);
316                         Add(CIRCLE_FILLED, rect,0);
317                         Debug("Added Circle %s", rect.Str().c_str());                   
318                 }
319         }
320 }
321
322 /**
323  * Load an SVG into a rectangle
324  */
325 void Document::LoadSVG(const string & filename, const Rect & bounds)
326 {
327         using namespace pugi;
328         
329         xml_document doc_xml;
330         ifstream input(filename.c_str(), ios_base::in);
331         xml_parse_result result = doc_xml.load(input);
332         
333         if (!result)
334                 Fatal("Couldn't load \"%s\" - %s", filename.c_str(), result.description());
335                 
336         Debug("Loaded XML - %s", result.description());
337         
338         input.close();
339         Real width(1);
340         Real height(1);
341         ParseSVGNode(doc_xml, bounds,width,height);
342 }
343
344 // Behold my amazing tokenizing abilities
345 static string & GetToken(const string & d, string & token, unsigned & i)
346 {
347         token.clear();
348         while (i < d.size() && iswspace(d[i]))
349         {
350                 ++i;
351         }
352         
353         while (i < d.size())
354         {
355                 if (d[i] == ',' || (isalpha(d[i]) && d[i] != 'e') || iswspace(d[i]))
356                 {
357                         if (token.size() == 0 && !iswspace(d[i]))
358                         {
359                                 token += d[i++];
360                         }
361                         break;  
362                 }
363                 token += d[i++];
364         }
365         //Debug("Got token \"%s\"", token.c_str());
366         return token;
367 }
368
369
370 // Fear the wrath of the tokenizing svg data
371 // Seriously this isn't really very DOM-like at all is it?
372 void Document::ParseSVGPathData(const string & d, const Rect & bounds)
373 {
374         Real x[4] = {0,0,0,0};
375         Real y[4] = {0,0,0,0};
376         
377         string token("");
378         string command("m");
379         
380         Real x0(0);
381         Real y0(0);
382         
383         unsigned i = 0;
384         unsigned prev_i = 0;
385         
386         bool start = false;
387         
388         while (i < d.size() && GetToken(d, token, i).size() > 0)
389         {
390                 if (isalpha(token[0]))
391                         command = token;
392                 else
393                 {
394                         i = prev_i; // hax
395                         if(command == "")
396                                 command = "L";
397                 }
398                 
399                 bool relative = islower(command[0]);
400                         
401                 if (command == "m" || command == "M")
402                 {
403                         Debug("Construct moveto command");
404                         Real dx = Real(strtod(GetToken(d,token,i).c_str(),NULL)) / bounds.w;
405                         assert(GetToken(d,token,i) == ",");
406                         Real dy = Real(strtod(GetToken(d,token,i).c_str(),NULL)) / bounds.h;
407                         
408                         x[0] = (relative) ? x[0] + dx : dx;
409                         y[0] = (relative) ? y[0] + dy : dy;
410                         
411
412                         
413                         Debug("mmoveto %f,%f", Float(x[0]),Float(y[0]));
414                         command = (command == "m") ? "l" : "L";
415                 }
416                 else if (command == "c" || command == "C" || command == "q" || command == "Q")
417                 {
418                         Debug("Construct curveto command");
419                         Real dx = Real(strtod(GetToken(d,token,i).c_str(),NULL))/bounds.w;
420                         assert(GetToken(d,token,i) == ",");
421                         Real dy = Real(strtod(GetToken(d,token,i).c_str(),NULL))/bounds.h;
422                         
423                         x[1] = (relative) ? x[0] + dx : dx;
424                         y[1] = (relative) ? y[0] + dy : dy;
425                         
426                         dx = Real(strtod(GetToken(d,token,i).c_str(),NULL)) / bounds.w;
427                         assert(GetToken(d,token,i) == ",");
428                         dy = Real(strtod(GetToken(d,token,i).c_str(),NULL)) / bounds.h;
429                         
430                         x[2] = (relative) ? x[0] + dx : dx;
431                         y[2] = (relative) ? y[0] + dy : dy;
432                         
433                         if (command != "q" && command != "Q")
434                         {
435                                 dx = Real(strtod(GetToken(d,token,i).c_str(),NULL)) / bounds.w;
436                                 assert(GetToken(d,token,i) == ",");
437                                 dy = Real(strtod(GetToken(d,token,i).c_str(),NULL)) / bounds.h;
438                                 x[3] = (relative) ? x[0] + dx : dx;
439                                 y[3] = (relative) ? y[0] + dy : dy;
440                         }
441                         else
442                         {
443                                 x[3] = x[2];
444                                 y[3] = y[2];
445                                 Real old_x1(x[1]), old_y1(y[1]);
446                                 x[1] = x[0] + Real(2) * (old_x1 - x[0])/ Real(3);
447                                 y[1] = y[0] + Real(2) * (old_y1 - y[0])/ Real(3);
448                                 x[2] = x[3] + Real(2) * (old_x1 - x[3])/ Real(3);
449                                 y[2] = y[3] + Real(2) * (old_y1 - y[3])/ Real(3);
450                         }
451                         
452                         unsigned index = AddBezierData(Bezier(x[0],y[0],x[1],y[1],x[2],y[2],x[3],y[3]));
453                         Add(BEZIER,Rect(0,0,1,1),index);
454                         
455                         
456                         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]));
457                         
458                         x[0] = x[3];
459                         y[0] = y[3];
460
461                         
462                 }
463                 else if (command == "l" || command == "L")
464                 {
465                         Debug("Construct lineto command");
466                 
467                         Real dx = Real(strtod(GetToken(d,token,i).c_str(),NULL)) / bounds.w;
468                         assert(GetToken(d,token,i) == ",");
469                         Real dy = Real(strtod(GetToken(d,token,i).c_str(),NULL)) / bounds.h;
470                         
471                         x[1] = (relative) ? x[0] + dx : dx;
472                         y[1] = (relative) ? y[0] + dy : dy;
473                         
474                         x[2] = x[1];
475                         y[2] = y[1];
476                         
477                         x[3] = x[1];
478                         y[3] = y[1];
479
480                         unsigned index = AddBezierData(Bezier(x[0],y[0],x[1],y[1],x[2],y[2],x[3],y[3]));
481                         Add(BEZIER,Rect(0,0,1,1),index);
482                         
483                         Debug("[%u] lineto %f,%f %f,%f", index, Float(x[0]),Float(y[0]),Float(x[1]),Float(y[1]));
484                         
485                         x[0] = x[3];
486                         y[0] = y[3];
487
488                 }
489                 else if (command == "z" || command == "Z")
490                 {
491                         Debug("Construct returnto command");
492                         x[1] = x0;
493                         y[1] = y0;
494                         x[2] = x0;
495                         y[2] = y0;
496                         x[3] = x0;
497                         y[3] = y0;
498                         
499                         unsigned index = AddBezierData(Bezier(x[0],y[0],x[1],y[1],x[2],y[2],x[3],y[3]));
500                         Add(BEZIER,Rect(0,0,1,1),index);
501                         
502                         Debug("[%u] returnto %f,%f %f,%f", index, Float(x[0]),Float(y[0]),Float(x[1]),Float(y[1]));
503                         
504                         x[0] = x[3];
505                         y[0] = y[3];
506                         command = "m";
507                 }
508                 else
509                 {
510                         Warn("Unrecognised command \"%s\", set to \"m\"", command.c_str());
511                         command = "m";
512                 }
513                 
514                 if (!start)
515                 {
516                         x0 = x[0];
517                         y0 = y[0];
518                         start = true;
519                 }
520                 prev_i = i;
521         }
522 }
523
524 void Document::AddFontGlyphAtPoint(stbtt_fontinfo *font, int character, Real scale, Real x, Real y)
525 {
526         int glyph_index = stbtt_FindGlyphIndex(font, character);
527
528         // Check if there is actully a glyph to render.
529         if (stbtt_IsGlyphEmpty(font, glyph_index))
530         {
531                 return;
532         }
533
534         stbtt_vertex *instructions;
535         int num_instructions = stbtt_GetGlyphShape(font, glyph_index, &instructions);
536
537         Real current_x(0), current_y(0);
538
539         for (int i = 0; i < num_instructions; ++i)
540         {
541                 // TTF uses 16-bit signed ints for coordinates:
542                 // with the y-axis inverted compared to us.
543                 // Convert and scale any data.
544                 Real inst_x = Real(instructions[i].x)*scale;
545                 Real inst_y = Real(instructions[i].y)*-scale;
546                 Real inst_cx = Real(instructions[i].cx)*scale;
547                 Real inst_cy = Real(instructions[i].cy)*-scale;
548                 Real old_x(current_x), old_y(current_y);
549                 current_x = inst_x;
550                 current_y = inst_y;
551                 unsigned bezier_index;
552                 switch(instructions[i].type)
553                 {
554                 // Move To
555                 case STBTT_vmove:
556                         break;
557                 // Line To
558                 case STBTT_vline:
559                         bezier_index = AddBezierData(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));
560                         Add(BEZIER,Rect(0,0,1,1),bezier_index);
561                         break;
562                 // Quadratic Bezier To:
563                 case STBTT_vcurve:
564                         // Quadratic -> Cubic:
565                         // - Endpoints are the same.
566                         // - cubic1 = quad0+(2/3)*(quad1-quad0)
567                         // - cubic2 = quad2+(2/3)*(quad1-quad2)
568                         bezier_index = AddBezierData(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,
569                                                 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));
570                         Add(BEZIER,Rect(0,0,1,1),bezier_index);
571                         break;
572                 }
573         }
574
575         stbtt_FreeShape(font, instructions);
576 }

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