path.h
1 /*
2 ** UICore
3 ** Copyright (c) 1997-2015 The UICore Team
4 **
5 ** This software is provided 'as-is', without any express or implied
6 ** warranty. In no event will the authors be held liable for any damages
7 ** arising from the use of this software.
8 **
9 ** Permission is granted to anyone to use this software for any purpose,
10 ** including commercial applications, and to alter it and redistribute it
11 ** freely, subject to the following restrictions:
12 **
13 ** 1. The origin of this software must not be misrepresented; you must not
14 ** claim that you wrote the original software. If you use this software
15 ** in a product, an acknowledgment in the product documentation would be
16 ** appreciated but is not required.
17 ** 2. Altered source versions must be plainly marked as such, and must not be
18 ** misrepresented as being the original software.
19 ** 3. This notice may not be removed or altered from any source distribution.
20 **
21 ** Note: Some of the libraries UICore may link to may have additional
22 ** requirements or restrictions.
23 **
24 ** File Author(s):
25 **
26 ** Magnus Norddahl
27 ** Mark Page
28 */
29 
30 #pragma once
31 
32 #include <memory>
33 #include "../../Core/Math/rect.h"
34 #include "../../Core/Math/mat4.h"
35 #include "../../Core/Math/color.h"
36 
37 namespace uicore
38 {
39  class Font;
40  typedef std::shared_ptr<Font> FontPtr;
41  class PathImpl;
42  class GlyphMetrics;
43  class Canvas;
44  typedef std::shared_ptr<Canvas> CanvasPtr;
45  class Pen;
46  class Brush;
47 
48  enum class PathFillMode
49  {
50  alternate,
51  winding
52  };
53 
54  class Path
55  {
56  public:
57  static std::shared_ptr<Path> create();
58 
59  static std::shared_ptr<Path> line(const Pointf &start, const Pointf &end) { auto path = create(); path->add_line(start, end); return path; }
60  static std::shared_ptr<Path> line(float x1, float y1, float x2, float y2) { return Path::line(Pointf(x1, y1), Pointf(x2, y2)); }
61 
62  static std::shared_ptr<Path> rect(const Rectf &box) { auto path = create(); path->add_rect(box); return path; }
63  static std::shared_ptr<Path> rect(float x, float y, float width, float height) { return Path::rect(Rectf(x, y, Sizef(width, height))); }
64  static std::shared_ptr<Path> rect(const Rectf &box, const uicore::Sizef &corner) { auto path = create(); path->add_rect(box, corner); return path; }
65 
66  static std::shared_ptr<Path> circle(float center_x, float center_y, float radius) { return Path::ellipse(Pointf(center_x, center_y), Sizef(radius, radius)); }
67  static std::shared_ptr<Path> ellipse(float center_x, float center_y, float radius_x, float radius_y) { return Path::ellipse(Pointf(center_x, center_y), Sizef(radius_x, radius_y)); }
68  static std::shared_ptr<Path> circle(const Pointf &center, float radius) { return Path::ellipse(center, Sizef(radius, radius)); }
69  static std::shared_ptr<Path> ellipse(const Pointf &center, const Sizef &radius) { auto path = create(); path->add_ellipse(center, radius); return path; }
70 
71  // This function is to assist in debugging, it has not been decided if it will be removed. Don't use at the moment.
72  static std::shared_ptr<Path> glyph(const CanvasPtr &canvas, const FontPtr &font, unsigned int glyph, GlyphMetrics &out_metrics);
73 
74  virtual PathFillMode fill_mode() const = 0;
75  virtual void set_fill_mode(PathFillMode fill_mode) = 0;
76 
77  virtual void move_to(const Pointf &point) = 0;
78  void move_to(float x, float y) { move_to(Pointf(x, y)); }
79  virtual void line_to(const Pointf &point) = 0;
80  void line_to(float x, float y) { line_to(Pointf(x, y)); }
81  virtual void bezier_to(const Pointf &control, const Pointf &point) = 0;
82  virtual void bezier_to(const Pointf &control1, const Pointf &control2, const Pointf &point) = 0;
83  virtual void close() = 0;
84 
86  virtual void add(const std::shared_ptr<Path> &path) = 0;
87 
88  virtual void add_line(const Pointf &start, const Pointf &end) = 0;
89  void add_line(float x1, float y1, float x2, float y2) { return add_line(Pointf(x1, y1), Pointf(x2, y2)); }
90 
91  virtual void add_rect(const Rectf &box) = 0;
92  void add_rect(float x, float y, float width, float height) { add_rect(Rectf(x, y, Sizef(width, height))); }
93  virtual void add_rect(const Rectf &box, const uicore::Sizef &corner) = 0;
94 
95  void add_circle(float center_x, float center_y, float radius) { add_ellipse(Pointf(center_x, center_y), Sizef(radius, radius)); }
96  void add_ellipse(float center_x, float center_y, float radius_x, float radius_y) { add_ellipse(Pointf(center_x, center_y), Sizef(radius_x, radius_y)); }
97  void add_circle(const Pointf &center, float radius) { add_ellipse(center, Sizef(radius, radius)); }
98  virtual void add_ellipse(const Pointf &center, const Sizef &radius) = 0;
99 
101  virtual void apply_transform(const Mat3f &transform) = 0;
102 
104  virtual void stroke(const CanvasPtr &canvas, const Pen &pen) = 0;
105 
107  virtual void fill(const CanvasPtr &canvas, const Brush &brush) = 0;
108 
110  virtual void fill_and_stroke(const CanvasPtr &canvas, const Pen &pen, const Brush &brush) = 0;
111 
112  // \brief Make a copy of the path
113  virtual std::shared_ptr<Path> clone() const = 0;
114  };
115 
116  typedef std::shared_ptr<Path> PathPtr;
117 }
void move_to(float x, float y)
Definition: path.h:78
virtual void add_rect(const Rectf &box)=0
static std::shared_ptr< Path > rect(const Rectf &box)
Definition: path.h:62
virtual PathFillMode fill_mode() const =0
virtual void stroke(const CanvasPtr &canvas, const Pen &pen)=0
Strokes a path.
virtual void fill_and_stroke(const CanvasPtr &canvas, const Pen &pen, const Brush &brush)=0
First fills a path, then strokes on top.
static std::shared_ptr< Path > circle(float center_x, float center_y, float radius)
Definition: path.h:66
void add_ellipse(float center_x, float center_y, float radius_x, float radius_y)
Definition: path.h:96
2D (x,y) point structure - Float
Definition: point.h:68
virtual void bezier_to(const Pointf &control, const Pointf &point)=0
static std::shared_ptr< Path > rect(const Rectf &box, const uicore::Sizef &corner)
Definition: path.h:64
virtual void add_line(const Pointf &start, const Pointf &end)=0
static std::shared_ptr< Path > glyph(const CanvasPtr &canvas, const FontPtr &font, unsigned int glyph, GlyphMetrics &out_metrics)
void add_line(float x1, float y1, float x2, float y2)
Definition: path.h:89
Definition: pen.h:36
Font class.
Definition: font.h:56
static std::shared_ptr< Path > line(float x1, float y1, float x2, float y2)
Definition: path.h:60
virtual void line_to(const Pointf &point)=0
static std::shared_ptr< Path > rect(float x, float y, float width, float height)
Definition: path.h:63
void add_rect(float x, float y, float width, float height)
Definition: path.h:92
virtual void close()=0
Glyph metrics class.
Definition: glyph_metrics.h:37
static std::shared_ptr< Path > create()
virtual void fill(const CanvasPtr &canvas, const Brush &brush)=0
Fills a path.
virtual void set_fill_mode(PathFillMode fill_mode)=0
2D Graphics Canvas
Definition: canvas.h:52
virtual void add(const std::shared_ptr< Path > &path)=0
Add all subpaths in path to this path.
2D (width,height) size structure - Float
Definition: size.h:180
virtual std::shared_ptr< Path > clone() const =0
static std::shared_ptr< Path > circle(const Pointf &center, float radius)
Definition: path.h:68
static std::shared_ptr< Path > ellipse(float center_x, float center_y, float radius_x, float radius_y)
Definition: path.h:67
2D (left,top,right,bottom) rectangle structure - Float
Definition: rect.h:505
void line_to(float x, float y)
Definition: path.h:80
std::shared_ptr< Font > FontPtr
Definition: path.h:39
Definition: brush.h:70
void add_circle(float center_x, float center_y, float radius)
Definition: path.h:95
static std::shared_ptr< Path > ellipse(const Pointf &center, const Sizef &radius)
Definition: path.h:69
std::shared_ptr< Path > PathPtr
Definition: path.h:116
static std::shared_ptr< Path > line(const Pointf &start, const Pointf &end)
Definition: path.h:59
PathFillMode
Definition: path.h:48
virtual void apply_transform(const Mat3f &transform)=0
Apply a transformation matrix on all points in this path.
void add_circle(const Pointf &center, float radius)
Definition: path.h:97
std::shared_ptr< Canvas > CanvasPtr
Definition: canvas.h:126
Definition: path.h:54
virtual void move_to(const Pointf &point)=0
Definition: Application/application.h:35