tbx  0.7.6
drawpath.h
1 /*
2  * tbx RISC OS toolbox library
3  *
4  * Copyright (C) 2012 Alan Buckley All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 
25 #ifndef TBX_DRAWPATH_H_
26 #define TBX_DRAWPATH_H_
27 
28 #include "drawtransform.h"
29 
30 namespace tbx
31 {
32  class DrawPath;
33 
41  {
50  PLOT_ALL_AT_ONCE = 0x80000000
51  };
52 
58  {
59  public:
60  enum JoinStyle {MITRED_JOINS, ROUND_JOINS, BEVELLED_JOINS};
61  enum CapStyle {BUTT_CAPS, ROUND_CAPS, SQUARE_CAPS, TRIANGULAR_CAPS};
62 
63  private:
64  JoinStyle _join_style;
65  CapStyle _leading_cap_style;
66  CapStyle _trailing_cap_style;
67  int _reserved;
68  Fixed16 _mitre_limit;
69  int _leading_tri_cap;
70  int _trailing_tri_cap;
71 
72  friend class DrawPath;
73 
74  public:
79  {
80  _join_style = ROUND_JOINS;
81  _leading_cap_style = ROUND_CAPS;
82  _trailing_cap_style = ROUND_CAPS;
83  _reserved = 0;
84  _mitre_limit = 1;
85  _leading_tri_cap = 0x1010;
86  _trailing_tri_cap = 0x1010;
87  }
88 
96  void join(JoinStyle style) {_join_style = style;}
102  JoinStyle join() const {return _join_style;}
118  void mitre_limit(const Fixed16 &limit) {_mitre_limit = limit;}
124  int mitre_limit() const {return _mitre_limit;}
132  void leading_cap(CapStyle cap_style) { _leading_cap_style = cap_style;}
138  CapStyle leading_cap() const {return _leading_cap_style;}
146  void trailing_cap(CapStyle cap_style) { _leading_cap_style = cap_style;}
152  CapStyle trailing_cap() const {return _trailing_cap_style;}
153 
161  void leading_cap_width(short width) {_leading_tri_cap = (_leading_tri_cap & 0xFFFF0000) | width;};
167  short leading_cap_width() const {return (short)(_leading_tri_cap & 0xFFFF);}
175  void leading_cap_length(short length) {_leading_tri_cap = (_leading_tri_cap & 0xFFFF) | (((int)length) << 16);}
181  short leading_cap_length() const {return (short)(_leading_tri_cap >> 16);}
189  void trailing_cap_width(short width) {_trailing_tri_cap = (_trailing_tri_cap & 0xFFFF0000) | width;};
195  short trailing_cap_width() const {return (short)(_trailing_tri_cap & 0xFFFF);}
203  void trailing_cap_length(short length) {_trailing_tri_cap = (_trailing_tri_cap & 0xFFFF) | (((int)length) << 16);}
209  short trailing_cap_length() const {return (short)(_trailing_tri_cap >> 16);}
210  };
211 
226  {
227  int *_data;
228  friend class DrawPath;
229 
230  public:
238  DrawDashPattern(int start, int *dashes, int count)
239  {
240  _data = new int[count+2];
241  _data[0] = start;
242  _data[1] = count;
243  for (int j = 0; j < count; j++) _data[j+2] = dashes[j];
244  }
245 
253  DrawDashPattern(int start, int dash = 1, int count = 2)
254  {
255  _data = new int[count+2];
256  _data[0] = start;
257  _data[1] = count;
258  for (int j = 0; j < count; j++) _data[j+2] = dash;
259  }
260 
266  void start(int value) {_data[0] = value;}
272  int start() const {return _data[0];}
276  int count() const {return _data[1];}
283  int &operator[](int index) {return _data[index+2];}
290  int operator[](int index) const {return _data[index+2];}
291  };
292 
297  {
298  public:
302  enum ElementType { END, CONTINUATION, MOVE, MOVE_INTERNAL, CLOSE_GAP, CLOSE_LINE, BEZIER, GAP, LINE };
303  private:
304  ElementType _type;
305  friend class DrawPath;
306  protected:
307  DrawElement(ElementType type) : _type(type) {};
308  public:
309  ElementType type() const {return _type;}
310 
311  static int size_in_words(ElementType type);
312  };
313 
318  {
319  int _left;
320  friend class DrawPath;
321 
322  public:
323  DrawElementEnd() : DrawElement(DrawElement::END), _left(0) {}
324  };
325 
330  {
331  public:
336  DrawElementContinuation(DrawElement *iptr) : DrawElement(DrawElement::CONTINUATION), ptr(iptr) {};
337 
338  DrawElement *ptr;
339  };
340 
346  {
347  public:
354  DrawElementMove(int ix, int iy) : DrawElement(DrawElement::MOVE), x(ix), y(iy) {};
355 
356  int x;
357  int y;
358  };
359 
365  {
366  public:
373  DrawElementMoveInternal(int ix, int iy) : DrawElement(DrawElement::MOVE_INTERNAL), x(ix), y(iy) {};
374 
375  int x;
376  int y;
377  };
378 
383  {
384  public:
385  DrawElementCloseGap() : DrawElement(DrawElement::CLOSE_GAP) {};
386  };
387 
393  {
394  public:
395  DrawElementCloseLine() : DrawElement(DrawElement::CLOSE_LINE) {};
396  };
397 
402  {
403  public:
404  DrawElementBezier(int icx1, int icy1, int icx2, int icy2, int ix, int iy) : DrawElement(DrawElement::BEZIER), cx1(icx1), cy1(icy1), cx2(icx2), cy2(icy2), x(ix), y(iy) {}
405  int cx1;
406  int cy1;
407  int cx2;
408  int cy2;
409  int x;
410  int y;
411  };
412 
419  {
420  public:
421  /*
422  * Construct a new gap element
423  *
424  * @param ix x coordinate in user coordinates
425  * @param iy y coordinate in user coordinates
426  */
427  DrawElementGap(int ix, int iy) : DrawElement(DrawElement::GAP), x(ix), y(iy) {};
428 
429  int x;
430  int y;
431  };
432 
437  {
438  public:
439  /*
440  * Construct a new line element
441  *
442  * @param ix x coordinate in user coordinates
443  * @param iy y coordinate in user coordinates
444  */
445  DrawElementLine(int ix, int iy) : DrawElement(DrawElement::LINE), x(ix), y(iy) {};
446 
447  int x;
448  int y;
449  };
450 
464  class DrawPath
465  {
466  private:
467  int *_data;
468  int _size;
469  int _capacity;
470 
474  void ensure_space(int needed) { if (_size + needed > _capacity) capacity(_size + needed + 8);}
475 
476  public:
477  DrawPath(int capacity = 64);
478  ~DrawPath();
479 
480  void add(const DrawElement &element);
481 
482  void capacity(int capacity);
486  int capacity() const {return _capacity;}
487 
488  void end_path();
489  void move(int x, int y);
490  void close_gap();
491  void close_line();
492  void bezier(int cx1, int cy1, int cx2, int cy2, int x, int y);
493  void gap(int x, int y);
494  void line(int x, int y);
495 
496  void circle(int x, int y, int radius);
497 
498  void fill(DrawFillStyle fill_style = WINDING_NON_ZERO, DrawTransform *transform = 0, int flatness = 0) const;
499  void stroke(DrawFillStyle fill_style = WINDING_NON_ZERO, DrawTransform *transform = 0, int flatness = 0,
500  int thickness = 0, DrawCapAndJoin *cap_and_join = 0, DrawDashPattern *dashes = 0) const;
501 
502  //TODO: Output to path functions
503 
504  };
505 
506 };
507 
508 #endif
tbx::DrawElementBezier
Bezier curve to element.
Definition: drawpath.h:402
tbx::DrawDashPattern
Class to represent a dash pattern for lines.
Definition: drawpath.h:226
tbx::DrawElementBezier::cx1
int cx1
First control x coordinate.
Definition: drawpath.h:405
tbx::DrawCapAndJoin
Class to set the cap and joins style for lines that are greater than a single pixel wide.
Definition: drawpath.h:58
tbx::DrawElementMoveInternal
Move to (x, y) starting new subpath.
Definition: drawpath.h:365
tbx
A library for creating RISC OS toolbox applications.
Definition: abouttobeshownlistener.cc:35
tbx::DrawElementBezier::cy1
int cy1
First control y coordinate.
Definition: drawpath.h:406
tbx::PLOT_NON_BOUNDARY_EXTERIOR
@ PLOT_NON_BOUNDARY_EXTERIOR
plot non-boundary exterior pixels.
Definition: drawpath.h:46
tbx::DrawPath
Class to represent, display and manipulate a graphical path used by the Draw RISC OS module.
Definition: drawpath.h:465
tbx::PLOT_BOUNDARY_INTERIOR
@ PLOT_BOUNDARY_INTERIOR
plot boundary interior pixels.
Definition: drawpath.h:48
tbx::DrawPath::close_line
void close_line()
Close path with a line.
Definition: drawpath.cc:120
tbx::DrawPath::close_gap
void close_gap()
Close path with a gap.
Definition: drawpath.cc:111
tbx::DrawDashPattern::DrawDashPattern
DrawDashPattern(int start, int *dashes, int count)
Construct a dash pattern from an array of dashes.
Definition: drawpath.h:238
tbx::DrawCapAndJoin::leading_cap_width
short leading_cap_width() const
Get the leading cap width.
Definition: drawpath.h:167
tbx::DrawElementGap
Gap to (x, y) Element.
Definition: drawpath.h:419
tbx::DrawElementEnd
End of path.
Definition: drawpath.h:318
tbx::DrawDashPattern::count
int count() const
Get the number of elements in the dash pattern.
Definition: drawpath.h:276
tbx::DrawCapAndJoin::leading_cap
CapStyle leading_cap() const
Get the leading cap style.
Definition: drawpath.h:138
tbx::DrawPath::add
void add(const DrawElement &element)
Add an element to the path.
Definition: drawpath.cc:72
tbx::DrawElementBezier::cy2
int cy2
Second control y coordinate.
Definition: drawpath.h:408
tbx::DrawElementMove::DrawElementMove
DrawElementMove(int ix, int iy)
Construct a new move element.
Definition: drawpath.h:354
tbx::DrawElement::size_in_words
static int size_in_words(ElementType type)
Return the size of each Draw element type.
Definition: drawpath.cc:38
tbx::DrawElementContinuation::DrawElementContinuation
DrawElementContinuation(DrawElement *iptr)
Construct Continuation path element.
Definition: drawpath.h:336
tbx::WINDING_POSITIVE
@ WINDING_POSITIVE
positive winding number rule.
Definition: drawpath.h:45
tbx::DrawCapAndJoin::leading_cap_width
void leading_cap_width(short width)
Leading triangular cap width on each side.
Definition: drawpath.h:161
tbx::DrawElementBezier::cx2
int cx2
Second control x coordinate.
Definition: drawpath.h:407
tbx::DrawCapAndJoin::trailing_cap_length
void trailing_cap_length(short length)
Trailing triangular cap length away from the line.
Definition: drawpath.h:203
tbx::DrawCapAndJoin::leading_cap_length
void leading_cap_length(short length)
Leading triangular cap length away from the line.
Definition: drawpath.h:175
tbx::DrawElementContinuation
Pointer to continuation of path.
Definition: drawpath.h:330
tbx::DrawCapAndJoin::join
void join(JoinStyle style)
Set the join style.
Definition: drawpath.h:96
tbx::WINDING_NON_ZERO
@ WINDING_NON_ZERO
non-zero winding number rule.
Definition: drawpath.h:42
tbx::DrawPath::end_path
void end_path()
End the path.
Definition: drawpath.cc:87
tbx::DrawElementLine
Line to (x, y) Element.
Definition: drawpath.h:437
tbx::DrawElementBezier::x
int x
End point x coordinate.
Definition: drawpath.h:409
tbx::DrawDashPattern::start
int start() const
Get the distance into the dash pattern of start.
Definition: drawpath.h:272
tbx::DrawDashPattern::operator[]
int & operator[](int index)
Get reference to a dash element.
Definition: drawpath.h:283
tbx::DrawCapAndJoin::trailing_cap
CapStyle trailing_cap() const
Get the trailing cap style.
Definition: drawpath.h:152
tbx::PLOT_BOUNDARY_EXTERIOR
@ PLOT_BOUNDARY_EXTERIOR
plot boundary exterior pixels.
Definition: drawpath.h:47
tbx::DrawCapAndJoin::join
JoinStyle join() const
Get the join style.
Definition: drawpath.h:102
tbx::DrawElementMoveInternal::DrawElementMoveInternal
DrawElementMoveInternal(int ix, int iy)
Construct a new move element.
Definition: drawpath.h:373
tbx::DrawTransform
Class to represent Drawing transforms.
Definition: drawtransform.h:60
tbx::WINDING_EVEN_ODD
@ WINDING_EVEN_ODD
even-odd winding number rule.
Definition: drawpath.h:44
tbx::Fixed16
Class to represent a fixed point number with 16bits before and after the point.
Definition: fixed16.h:40
tbx::DrawElement
Base class for elements added to a draw path.
Definition: drawpath.h:297
tbx::DrawCapAndJoin::trailing_cap_width
void trailing_cap_width(short width)
Trailing triangular cap width on each side.
Definition: drawpath.h:189
tbx::DrawPath::move
void move(int x, int y)
Move to (x, y) starting new subpath.
Definition: drawpath.cc:100
tbx::DrawElement::ElementType
ElementType
Draw element types.
Definition: drawpath.h:302
tbx::WINDING_NEGATIVE
@ WINDING_NEGATIVE
negative winding number rule.
Definition: drawpath.h:43
tbx::DrawCapAndJoin::mitre_limit
void mitre_limit(const Fixed16 &limit)
Sets the mitre limit.
Definition: drawpath.h:118
tbx::DrawPath::DrawPath
DrawPath(int capacity=64)
Create a new draw path object.
Definition: drawpath.cc:54
tbx::DrawFillStyle
DrawFillStyle
Enumeration to set the fill style when filling shapes.
Definition: drawpath.h:41
tbx::DrawDashPattern::operator[]
int operator[](int index) const
Get value of a dash element.
Definition: drawpath.h:290
tbx::DrawCapAndJoin::trailing_cap_width
short trailing_cap_width() const
Get the trailing cap width.
Definition: drawpath.h:195
tbx::DrawCapAndJoin::leading_cap_length
short leading_cap_length() const
Get the leading cap length.
Definition: drawpath.h:181
tbx::DrawDashPattern::DrawDashPattern
DrawDashPattern(int start, int dash=1, int count=2)
Construct a dash pattern initialised to equal sized dashes.
Definition: drawpath.h:253
tbx::PLOT_ALL_AT_ONCE
@ PLOT_ALL_AT_ONCE
Used with DrawPath::stroke, will only plot each pixel once at the cost of more temporary workspace.
Definition: drawpath.h:50
tbx::DrawPath::line
void line(int x, int y)
Add a line to a path.
Definition: drawpath.cc:155
tbx::DrawElementBezier::y
int y
End point y coordinate.
Definition: drawpath.h:410
tbx::DrawCapAndJoin::trailing_cap
void trailing_cap(CapStyle cap_style)
Set the trailing cap style.
Definition: drawpath.h:146
tbx::PLOT_NON_BOUNDARY_INTERIOR
@ PLOT_NON_BOUNDARY_INTERIOR
plot non-boundary interior pixels.
Definition: drawpath.h:49
tbx::DrawCapAndJoin::mitre_limit
int mitre_limit() const
Get the mitre limit.
Definition: drawpath.h:124
tbx::DrawPath::circle
void circle(int x, int y, int radius)
Adds a circle subpath to the drawing.
Definition: drawpath.cc:176
tbx::DrawPath::stroke
void stroke(DrawFillStyle fill_style=WINDING_NON_ZERO, DrawTransform *transform=0, int flatness=0, int thickness=0, DrawCapAndJoin *cap_and_join=0, DrawDashPattern *dashes=0) const
Sends a path to the VDU.
Definition: drawpath.cc:287
tbx::DrawPath::fill
void fill(DrawFillStyle fill_style=WINDING_NON_ZERO, DrawTransform *transform=0, int flatness=0) const
Fill the interior of a path.
Definition: drawpath.cc:258
tbx::DrawElementCloseGap
Close path with a gap element.
Definition: drawpath.h:383
tbx::DrawPath::capacity
int capacity() const
Get capacity in words (1 word = 4 bytes)
Definition: drawpath.h:486
tbx::DrawDashPattern::start
void start(int value)
Set the distance into the dash pattern of start.
Definition: drawpath.h:266
tbx::DrawPath::bezier
void bezier(int cx1, int cy1, int cx2, int cy2, int x, int y)
Bezier curve to.
Definition: drawpath.cc:129
tbx::DrawCapAndJoin::trailing_cap_length
short trailing_cap_length() const
Get the trailing cap length.
Definition: drawpath.h:209
tbx::DrawElementMove
Move to (x, y) starting new subpath.
Definition: drawpath.h:346
tbx::DrawCapAndJoin::leading_cap
void leading_cap(CapStyle cap_style)
Set the leading cap style.
Definition: drawpath.h:132
tbx::DrawCapAndJoin::DrawCapAndJoin
DrawCapAndJoin()
Construct with ROUND_JOINS and ROUND_CAPS at both ends.
Definition: drawpath.h:78
tbx::DrawPath::gap
void gap(int x, int y)
Add a gap to a path.
Definition: drawpath.cc:144
tbx::DrawElementCloseLine
Close path with a line element.
Definition: drawpath.h:393