Go nuts with Qt
[ipdf/code.git] / src / document.h
1 #ifndef _DOCUMENT_H
2 #define _DOCUMENT_H
3
4 #include "ipdf.h"
5 #include "quadtree.h"
6
7 #include "../contrib/pugixml-1.4/src/pugixml.hpp"
8 #include "stb_truetype.h"
9
10 typedef struct stbtt_fontinfo stbtt_fontinfo;
11
12 namespace IPDF
13 {
14         struct SVGMatrix
15         {
16                 Real a; // width
17                 Real c; // skew x by y
18                 Real e; // translate x
19                 
20                 Real b; // skew y by x
21                 Real d; // height
22                 Real f; // translate y
23         };
24         // SVG matrix transforms (x,y) <- (a x' + c y' + e, b x' + d y' + f)
25         // Equivelant to OpenGL 3d matrix transform ((a, c, e) (b, d, f) (0,0,1))
26         
27         class Document
28         {
29                 public:
30                         Document(const std::string & filename = "", const std::string & font_filename = "fonts/DejaVuSansMono.ttf") : m_objects(), m_count(0), m_font_data(NULL), m_font()
31                         {
32                                 Load(filename);
33                                 if (font_filename != "")
34                                         SetFont(font_filename);
35                         }
36                         virtual ~Document() 
37                         {
38                                 free(m_font_data);
39                         }
40                         
41                         
42
43                         void Load(const std::string & filename = "");
44                         void Save(const std::string & filename);
45                         void DebugDumpObjects();
46
47                         unsigned ObjectCount() const {return m_count;}
48                         inline const Objects & GetObjects() const {return m_objects;}
49
50                         bool operator==(const Document & equ) const;
51                         bool operator!=(const Document & equ) const {return !(this->operator==(equ));}
52
53                         unsigned AddGroup(unsigned start_index, unsigned end_index);
54                         unsigned AddBezier(const Bezier & bezier);
55                         unsigned Add(ObjectType type, const Rect & bounds, unsigned data_index = 0);
56                         unsigned AddBezierData(const Bezier & bezier);
57                         
58
59
60                         
61
62                         
63                         
64                         /** SVG Related functions **/
65                         
66                         /** Load an SVG text file and add to the document **/
67                         void LoadSVG(const std::string & filename, const Rect & bounds = Rect(0,0,1,1));
68                         void ParseSVG(const std::string & svg, const Rect & bounds = Rect(0,0,1,1));
69                         
70                         /** Parse an SVG node or SVG-group node, adding children to the document **/
71                         void ParseSVGNode(pugi::xml_node & root, SVGMatrix & transform);
72                         /** Parse an SVG path with string **/
73                         std::pair<unsigned, unsigned> ParseSVGPathData(const std::string & d, const SVGMatrix & transform);
74                         
75                         /** Modify an SVG transformation matrix **/
76                         static void ParseSVGTransform(const std::string & s, SVGMatrix & transform);
77
78                         /** Font related functions **/
79                         void SetFont(const std::string & font_filename);
80                         void AddText(const std::string & text, Real scale, Real x, Real y);
81                         
82                         void AddFontGlyphAtPoint(stbtt_fontinfo *font, int character, Real scale, Real x, Real y);
83
84 #ifndef QUADTREE_DISABLED
85                         inline const QuadTree& GetQuadTree() { if (m_quadtree.root_id == QUADTREE_EMPTY) { GenBaseQuadtree(); } return m_quadtree; }
86                         QuadTreeIndex GenQuadChild(QuadTreeIndex parent, QuadTreeNodeChildren type);
87                         QuadTreeIndex GenQuadParent(QuadTreeIndex child, QuadTreeNodeChildren mytype);
88                         // Returns the number of objects the current object formed when clipped, the objects in question are added to the end of the document.
89                         int ClipObjectToQuadChild(int object_id, QuadTreeNodeChildren type);
90 #endif
91
92                 private:
93                         friend class View;
94                         Objects m_objects;
95 #ifndef QUADTREE_DISABLED
96                         QuadTree m_quadtree;
97                         void GenBaseQuadtree();
98 #endif
99                         unsigned m_count;
100                         unsigned char * m_font_data;
101                         stbtt_fontinfo m_font;
102                 
103                         
104
105         };
106 }
107
108 #endif //_DOCUMENT_H

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