Pen.h
1 /* Copyright (C) 2006-2015 Laurentiu-Gheorghe Crisan
2  Copyright (C) 2006-2015 Marc Boris Duerner
3  Copyright (C) 2017-2017 Aloysius Indrayanto
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  As a special exception, you may use this file as part of a free
11  software library without restriction. Specifically, if other files
12  instantiate templates or use macros or inline functions from this
13  file, or you compile this file and link it with other files to
14  produce an executable, this file does not by itself cause the
15  resulting executable to be covered by the GNU General Public
16  License. This exception does not however invalidate any other
17  reasons why the executable file might be covered by the GNU Library
18  General Public License.
19 
20  This library is distributed in the hope that it will be useful,
21  but WITHOUT ANY WARRANTY; without even the implied warranty of
22  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23  Lesser General Public License for more details.
24 
25  You should have received a copy of the GNU Lesser General Public
26  License along with this library; if not, write to the Free Software
27  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
28  MA 02110-1301 USA
29 */
30 
31 #ifndef PT_GFX_PEN_H
32 #define PT_GFX_PEN_H
33 
34 #include <Pt/Gfx/Api.h>
35 #include <Pt/Gfx/Color.h>
36 #include <Pt/SmartPtr.h>
37 #include <cstddef>
38 
39 namespace Pt {
40 
41 namespace Gfx {
42 
43 class PenData;
44 
53 class PT_GFX_API Pen
54 {
55  public:
58  enum Style
59  {
60  Solid = 0,
61  Dot = 1,
62  Dash = 2,
63  DashPattern = 3
64  };
65 
68  enum CapStyle
69  {
70  FlatCap = 0,
71  SquareCap = 1,
72  RoundCap = 2
73  };
74 
77  enum JoinStyle
78  {
79  NoJoin = 0,
80  BevelJoin = 1,
81  MiterJoin = 2,
82  RoundJoin = 3
83  };
84 
89  Pen();
90 
96  Pen(const Color& color);
97 
100  Pen(const Color& color, std::size_t width, Style style = Solid,
101  CapStyle cap = FlatCap, JoinStyle join = BevelJoin);
102 
105  Pen(const Color& color, std::size_t width,
106  const std::vector<Pt::uint8_t>& dashPattern,
107  CapStyle cap = FlatCap, JoinStyle join = BevelJoin);
108 
111  bool isNull() const;
112 
115  void setSize(std::size_t size);
116 
119  std::size_t size() const;
120 
123  void setColor(const Color& color);
124 
127  const Color& color() const;
128 
131  void setStyle(Style style = Solid);
132 
135  Style style() const;
136 
139  bool isSolid() const
140  { return style() == Solid; }
141 
144  void setDashPattern(const std::vector<Pt::uint8_t>& dashPattern);
145 
148  const std::vector<Pt::uint8_t>& dashPattern() const;
149 
152  void setCapStyle(CapStyle cap = FlatCap);
153 
157 
160  void setJoinStyle(JoinStyle join = BevelJoin);
161 
165 
168  const void* instance() const
169  {
170  return _penData.get();
171  }
172 
173  private:
174  SmartPtr<PenData> _penData;
175 };
176 
177 
178 class PT_GFX_API PenData
179 {
180  public:
181  PenData(const Color& color, std::size_t size,
182  Pen::Style style, Pen::CapStyle cap, Pen::JoinStyle join)
183  : _color(color)
184  , _size(size)
185  , _style(style)
186  , _capStyle(cap)
187  , _joinStyle(join)
188  {}
189 
190  PenData(const Color& color, std::size_t size,
191  Pen::Style style, const std::vector<Pt::uint8_t>& dashPattern, Pen::CapStyle cap, Pen::JoinStyle join)
192  : _color(color)
193  , _size(size)
194  , _style(style)
195  , _dashPattern(dashPattern)
196  , _capStyle(cap)
197  , _joinStyle(join)
198  {}
199 
200  PenData(const Color& color, std::size_t size,
201  Pen::Style style, const Pt::uint8_t* dashPatternBeg, const Pt::uint8_t* dashPatternEnd, Pen::CapStyle cap, Pen::JoinStyle join)
202  : _color(color)
203  , _size(size)
204  , _style(style)
205  , _dashPattern(dashPatternBeg, dashPatternEnd)
206  , _capStyle(cap)
207  , _joinStyle(join)
208  {}
209 
210  void setColor(const Color& color)
211  { _color = color; }
212 
213  const Color& color() const
214  { return _color; }
215 
216  void setSize(std::size_t size)
217  { _size = size; }
218 
219  std::size_t size() const
220  { return _size; }
221 
222  void setStyle(Pen::Style style)
223  {
224  _style = style;
225  _dashPattern.clear();
226  }
227 
228  Pen::Style style() const
229  { return _style; }
230 
231  void setDashPattern(const std::vector<Pt::uint8_t>& dashPattern)
232  {
233  _style = Pen::DashPattern;
234  _dashPattern = dashPattern;
235  }
236 
237  const std::vector<Pt::uint8_t>& dashPattern() const
238  { return _dashPattern; }
239 
240  void setCapStyle(Pen::CapStyle cap)
241  { _capStyle = cap;}
242 
243  Pen::CapStyle capStyle() const
244  { return _capStyle;}
245 
246  void setJoinStyle(Pen::JoinStyle join)
247  { _joinStyle = join; }
248 
249  Pen::JoinStyle joinStyle() const
250  { return _joinStyle; }
251 
252  private:
253  Color _color;
254  std::size_t _size;
255  Pen::Style _style;
256  std::vector<Pt::uint8_t> _dashPattern;
257  Pen::CapStyle _capStyle;
258  Pen::JoinStyle _joinStyle;
259 };
260 
261 } // namespace
262 
263 } // namespace
264 
265 #endif
Core module.
Definition: pt-gfx-images.dox:14
Pen(const Color &color, std::size_t width, Style style=Solid, CapStyle cap=FlatCap, JoinStyle join=BevelJoin)
Constructs a pen with the specified size, color and styles.
void setSize(std::size_t size)
Sets the size of the pen.
Attributes for the drawing of outlines.
Definition: Pen.h:54
Pen()
Constructs a null pen.
void setJoinStyle(JoinStyle join=BevelJoin)
Sets the join style.
void setCapStyle(CapStyle cap=FlatCap)
Sets the cap style.
Style style() const
Returns the pen style.
const Color & color() const
Returns the color of the pen.
Style
Identifies how outlines are stroked.
Definition: Pen.h:59
JoinStyle joinStyle() const
Returns the join style.
void setColor(const Color &color)
Sets the color of the pen.
const std::vector< Pt::uint8_t > & dashPattern() const
Returns the pen user dash pattern.
std::size_t size() const
Returns the size of the pen.
Pen(const Color &color, std::size_t width, const std::vector< Pt::uint8_t > &dashPattern, CapStyle cap=FlatCap, JoinStyle join=BevelJoin)
Constructs a pen with a custom dash pattern.
uint_type uint8_t
Unsigned 8-bit integer type.
Definition: Types.h:18
JoinStyle
Identifies how connected line segments are joined.
Definition: Pen.h:78
Standard color type.
Definition: Color.h:47
void setStyle(Style style=Solid)
Sets the pen style.
Pen(const Color &color)
Constructs a pen with the specified color.
CapStyle capStyle() const
Returns the cap style.
bool isSolid() const
Returns true if the pen is solid.
Definition: Pen.h:139
const void * instance() const
Returns an implementation-specific pen instance.
Definition: Pen.h:168
void setDashPattern(const std::vector< Pt::uint8_t > &dashPattern)
Sets the pen user dash pattern.
CapStyle
Identifies how open line ends are rendered.
Definition: Pen.h:69
bool isNull() const
Returns true if the pen is null.