52e9295ed55fe47cc3c3903d3f53c4aeb811a283
[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 b; // skew y by x
18                 Real c; // skew x by y
19                 Real d; // height
20                 Real e; // translate x
21                 Real f; // translate y
22         };
23         // SVG matrix transforms (x,y) <- (a x' + c y' + e, b x' + d y' + f)
24         // Equivelant to OpenGL 3d matrix transform ((a, c, e) (b, d, f) (0,0,1))
25         
26         class Document
27         {
28                 public:
29                         Document(const std::string & filename = "", const std::string & font_filename = "DejaVuSansMono.ttf") : m_objects(), m_count(0), m_font_data(NULL), m_font()
30                         {
31                                 Load(filename);
32                                 if (font_filename != "")
33                                         SetFont(font_filename);
34                         }
35                         virtual ~Document() 
36                         {
37                                 free(m_font_data);
38                         }
39                         
40                         
41
42                         void Load(const std::string & filename = "");
43                         void Save(const std::string & filename);
44                         void DebugDumpObjects();
45
46                         unsigned ObjectCount() const {return m_count;}
47                         inline const Objects & GetObjects() const {return m_objects;}
48
49                         bool operator==(const Document & equ) const;
50                         bool operator!=(const Document & equ) const {return !(this->operator==(equ));}
51
52                         void Add(ObjectType type, const Rect & bounds, unsigned data_index = 0);
53                         unsigned AddBezierData(const Bezier & bezier);
54                         
55
56
57                         
58
59                         
60                         
61                         /** SVG Related functions **/
62                         
63                         /** Load an SVG text file and add to the document **/
64                         void LoadSVG(const std::string & filename, const Rect & bounds = Rect(0,0,1,1));
65                         
66                         /** Parse an SVG node or SVG-group node, adding children to the document **/
67                         void ParseSVGNode(pugi::xml_node & root, SVGMatrix & transform);
68                         /** Parse an SVG path with string **/
69                         void ParseSVGPathData(const std::string & d, const SVGMatrix & transform);
70                         
71                         /** Modify an SVG transformation matrix **/
72                         static void ParseSVGTransform(const std::string & s, SVGMatrix & transform);
73
74                         /** Font related functions **/
75                         void SetFont(const std::string & font_filename);
76                         void AddText(const std::string & text, Real scale, Real x, Real y);
77                         
78                         void AddFontGlyphAtPoint(stbtt_fontinfo *font, int character, Real scale, Real x, Real y);
79
80 #ifndef QUADTREE_DISABLED
81                         inline const QuadTree& GetQuadTree() { if (m_quadtree.root_id == QUADTREE_EMPTY) { GenBaseQuadtree(); } return m_quadtree; }
82                         QuadTreeIndex GenQuadChild(QuadTreeIndex parent, QuadTreeNodeChildren type);
83                         QuadTreeIndex GenQuadParent(QuadTreeIndex child, QuadTreeNodeChildren mytype);
84 #endif
85
86                 private:
87                         friend class View;
88                         Objects m_objects;
89 #ifndef QUADTREE_DISABLED
90                         QuadTree m_quadtree;
91                         void GenBaseQuadtree();
92 #endif
93                         unsigned m_count;
94                         unsigned char * m_font_data;
95                         stbtt_fontinfo m_font;
96                 
97                         
98
99         };
100 }
101
102 #endif //_DOCUMENT_H

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