Some code cleanup, quadtree fixes, non-quadtree build fixes.
[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 <map>
8
9 #include "../contrib/pugixml-1.4/src/pugixml.hpp"
10 #include "stb_truetype.h"
11
12 typedef struct stbtt_fontinfo stbtt_fontinfo;
13
14 namespace IPDF
15 {
16         struct SVGMatrix
17         {
18                 Real a; // width
19                 Real c; // skew x by y
20                 Real e; // translate x
21                 
22                 Real b; // skew y by x
23                 Real d; // height
24                 Real f; // translate y
25         };
26         // SVG matrix transforms (x,y) <- (a x' + c y' + e, b x' + d y' + f)
27         // Equivelant to OpenGL 3d matrix transform ((a, c, e) (b, d, f) (0,0,1))
28         
29         class Document
30         {
31                 public:
32                         Document(const std::string & filename = "", const std::string & font_filename = "fonts/DejaVuSansMono.ttf") : m_objects(), m_count(0), m_font_data(NULL), m_font()
33                         {
34                                 Load(filename);
35                                 if (font_filename != "")
36                                         SetFont(font_filename);
37 #ifndef QUADTREE_DISABLED
38                                 m_current_insert_node = -1;
39 #endif
40                         }
41                         virtual ~Document() 
42                         {
43                                 free(m_font_data);
44                         }
45                         
46                         
47
48                         void Load(const std::string & filename = "");
49                         void Save(const std::string & filename);
50                         void DebugDumpObjects();
51
52                         unsigned ObjectCount() const {return m_count;}
53                         inline const Objects & GetObjects() const {return m_objects;}
54
55                         bool operator==(const Document & equ) const;
56                         bool operator!=(const Document & equ) const {return !(this->operator==(equ));}
57
58                         unsigned AddPath(unsigned start_index, unsigned end_index, const Colour & shading=Colour(0.6,0.6,0.6,1), const Colour & stroke=Colour(0,0,0,0));
59                         unsigned AddBezier(const Bezier & bezier);
60                         int AddClip(ObjectType type, const Rect & bounds, unsigned data_index, const Rect & clip_rect);
61                         unsigned Add(ObjectType type, const Rect & bounds, unsigned data_index = 0, QuadTreeIndex qtnode = -1);
62                         unsigned AddBezierData(const Bezier & bezier);
63                         unsigned AddPathData(const Path & path);
64
65
66                         /** SVG Related functions **/
67                         
68                         /** Load an SVG text file and add to the document **/
69                         void LoadSVG(const std::string & filename, const Rect & bounds = Rect(0,0,1,1));
70                         void ParseSVG(const std::string & svg, const Rect & bounds = Rect(0,0,1,1));
71                         
72                         /** Parse an SVG node or SVG-group node, adding children to the document **/
73                         void ParseSVGNode(pugi::xml_node & root, SVGMatrix & transform);
74                         /** Parse an SVG path with string **/
75                         std::pair<unsigned, unsigned> ParseSVGPathData(const std::string & d, const SVGMatrix & transform, bool & closed);
76                         
77                         /** Modify an SVG transformation matrix **/
78                         static void ParseSVGTransform(const std::string & s, SVGMatrix & transform);
79                         
80                         /** Extract CSS values (shudder) from style **/
81                         static void ParseSVGStyleData(const std::string & style, std::map<std::string, std::string> & results);
82
83                         /** Font related functions **/
84                         void SetFont(const std::string & font_filename);
85                         void AddText(const std::string & text, Real scale, Real x, Real y);
86                         
87                         void AddFontGlyphAtPoint(stbtt_fontinfo *font, int character, Real scale, Real x, Real y);
88                         
89                         void TransformObjectBounds(const SVGMatrix & transform, ObjectType type = NUMBER_OF_OBJECT_TYPES);
90                         void TranslateObjects(const Real & x, const Real & y, ObjectType type = NUMBER_OF_OBJECT_TYPES);
91                         void ScaleObjectsAboutPoint(const Real & x, const Real & y, const Real & scale_amount, ObjectType type = NUMBER_OF_OBJECT_TYPES);
92                         
93 #ifndef QUADTREE_DISABLED
94                         inline const QuadTree& GetQuadTree() { if (m_quadtree.root_id == QUADTREE_EMPTY) { GenBaseQuadtree(); } return m_quadtree; }
95                         QuadTreeIndex GenQuadChild(QuadTreeIndex parent, QuadTreeNodeChildren type);
96                         QuadTreeIndex GenQuadParent(QuadTreeIndex child, QuadTreeNodeChildren mytype);
97                         void OverlayQuadChildren(QuadTreeIndex orig_parent, QuadTreeIndex parent, QuadTreeNodeChildren type);
98                         void OverlayQuadParent(QuadTreeIndex orig_child, QuadTreeIndex child, QuadTreeNodeChildren type);
99                         void PropagateQuadChanges(QuadTreeIndex node);
100                         // Returns the number of objects the current object formed when clipped, the objects in question are added to the end of the document.
101                         int ClipObjectToQuadChild(int object_id, QuadTreeNodeChildren type);
102
103                         void SetQuadtreeInsertNode(QuadTreeIndex node) { m_current_insert_node = node; }
104 #endif
105
106                         void ClearObjects()
107                         {
108                                 m_count = 0;
109                                 m_objects.Clear();
110                         }
111
112
113                 private:
114                         friend class View;
115                         Objects m_objects;
116 #ifndef QUADTREE_DISABLED
117                         QuadTree m_quadtree;
118                         void GenBaseQuadtree();
119
120                         QuadTreeIndex m_current_insert_node;
121 #endif
122                         bool m_document_dirty;
123                         unsigned m_count;
124                         unsigned char * m_font_data;
125                         stbtt_fontinfo m_font;
126                 
127                         
128
129         };
130 }
131
132 #endif //_DOCUMENT_H

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