Path.h
1 /* Copyright (C) 2017 Marc Boris Duerner
2  Copyright (C) 2017 Aloysius Indrayanto
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Lesser General Public
6  License as published by the Free Software Foundation; either
7  version 2.1 of the License, or (at your option) any later version.
8 
9  As a special exception, you may use this file as part of a free
10  software library without restriction. Specifically, if other files
11  instantiate templates or use macros or inline functions from this
12  file, or you compile this file and link it with other files to
13  produce an executable, this file does not by itself cause the
14  resulting executable to be covered by the GNU General Public
15  License. This exception does not however invalidate any other
16  reasons why the executable file might be covered by the GNU Library
17  General Public License.
18 
19  This library is distributed in the hope that it will be useful,
20  but WITHOUT ANY WARRANTY; without even the implied warranty of
21  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  Lesser General Public License for more details.
23 
24  You should have received a copy of the GNU Lesser General Public
25  License along with this library; if not, write to the Free Software
26  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
27  MA 02110-1301 USA
28 */
29 
30 #ifndef PT_GFX_PATH_H
31 #define PT_GFX_PATH_H
32 
33 #include <Pt/Gfx/Api.h>
34 #include <Pt/Gfx/Point.h>
35 #include <Pt/Gfx/Size.h>
36 #include <Pt/Gfx/Rect.h>
37 #include <Pt/Gfx/Transform.h>
38 #include <vector>
39 
40 namespace Pt {
41 
42 namespace Gfx {
43 
44 class Polygon
45 {
46  public:
47  Polygon(const PointF* ps, std::size_t n)
48  : _points(ps, ps+n)
49  {
50  }
51 
52  Polygon()
53  {
54  }
55 
56  void assign(const PointF* ps, std::size_t n)
57  {
58  _points.assign(ps, ps+n);
59  }
60 
61  const PointF& at(std::size_t n) const
62  {
63  return _points[n];
64  }
65 
66  PointF& at(std::size_t n)
67  {
68  return _points[n];
69  }
70 
71  void clear()
72  {
73  _points.clear();
74  }
75 
76  bool empty() const
77  {
78  return _points.empty();
79  }
80 
81  std::size_t size() const
82  {
83  return _points.size();
84  }
85 
86  void push_back(const PointF& p)
87  {
88  _points.push_back(p);
89  }
90 
91  std::vector<PointF>& points()
92  {
93  return _points;
94  }
95 
96  const std::vector<PointF>& points() const
97  {
98  return _points;
99  }
100 
101  private:
102  std::vector<PointF> _points;
103 };
104 
105 struct Element
106 {
107  enum ElementType
108  {
109  IT_Close,
110  IT_MoveTo,
111  IT_LineTo,
112  IT_QuadBezierTo,
113  IT_CubicBezierTo,
114  IT_GenNBezierTo
115  };
116 
117  Element(ElementType type_)
118  : type(type_)
119  {}
120 
121  Element(ElementType type_, double x0, double y0)
122  : type(type_), pxy(2)
123  { pxy[0] = x0; pxy[1] = y0; }
124 
125  Element(ElementType type_, double x0, double y0, double x1, double y1)
126  : type(type_), pxy(4)
127  { pxy[0] = x0; pxy[1] = y0; pxy[2] = x1; pxy[3] = y1; }
128 
129  Element(ElementType type_, double x0, double y0, double x1, double y1, double x2, double y2)
130  : type(type_), pxy(6)
131  { pxy[0] = x0; pxy[1] = y0; pxy[2] = x1; pxy[3] = y1; pxy[4] = x2; pxy[5] = y2; }
132 
133  Element(ElementType type_, const std::vector<double>& pxy_)
134  : type(type_), pxy(pxy_)
135  {}
136 
137  ElementType type;
138  std::vector<double> pxy;
139 };
140 
141 
142 class PT_GFX_API Path
143 {
144  public:
145  Path();
146 
147  ~Path();
148 
149  std::size_t size() const;
150 
151  bool isEmpty() const;
152 
153  const Element& at(std::size_t n) const;
154 
155  void clear();
156 
157  RectF boundingRect() const;
158 
159  const PointF& currentPosition() const;
160 
161  void moveTo(const PointF& p);
162 
163  void lineTo(const PointF& p);
164 
165  void arcTo(const PointF& p, double r);
166 
167  void quadraticBezierTo(const PointF &c, const PointF& to);
168 
169  void cubicBezierTo(const PointF &c1, const PointF &c2, const PointF& to);
170 
171  void bezierTo(const PointF* controlPoints, size_t n, const PointF& to);
172 
175  void close();
176 
179  void addPath(const Path& p);
180 
183  void insertPath(const Path& p);
184 
185  void addRect(const SizeF& size);
186 
187  void addRoundedRect(const SizeF& size, float radius);
188 
189  void addEllipse(const SizeF& size);
190 
191  void addPie(const SizeF& size, float degBegin, float degEnd);
192 
193  void addChord(const SizeF& size, float degBegin, float degEnd);
194 
195  void transform(const Transform& transform);
196 
197  void toPolygons(std::vector<Polygon>& polygons, float smoothness = 1) const;
198 
199  private:
200  typedef std::vector<Element> ElementVector;
201 
202  private:
203  ElementVector _elements;
204  PointF _position;
205 };
206 
207 } // namespace
208 
209 } // namespace
210 
211 #endif