1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2016 Jean-Pierre Charras, jp.charras at wanadoo.fr
5  * Copyright (C) 1992-2020 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
23  */
24 
25 #ifndef BASIC_GAL_H
26 #define BASIC_GAL_H
27 
28 #include <eda_rect.h>
29 
30 #include <gal/stroke_font.h>
31 #include <gal/graphics_abstraction_layer.h>
32 #include <newstroke_font.h>
33 
34 class PLOTTER;
35 
36 
37 /*
38  * A minimal GAL implementation to draw, plot and convert stroke texts to a set of segments
39  * for DRC tests, and to calculate text sizes.
40  *
41  * Currently it allows one to use GAL and STROKE_FONT methods in legacy draw mode
42  * (using wxDC functions) in plot functions only for texts.
43  * It is used also to calculate the text bounding boxes
44  *
45  * The main purpose is to avoid duplicate code to do the same thing in GAL canvas,
46  * print & plotter canvasses and DRC.
47  *
48  * It will be certainly removed when a full GAL canvas using wxDC is implemented
49  * (or at least restricted to plotter and DRC "canvas")
50  */
51 
52 struct TRANSFORM_PRM    // A helper class to transform coordinates in BASIC_GAL canvas
53 {
54     VECTOR2D m_rotCenter;
55     VECTOR2D m_moveOffset;
56     double   m_rotAngle;
57 };
58 
59 
60 class BASIC_GAL: public KIGFX::GAL
61 {
62 public:
BASIC_GAL(KIGFX::GAL_DISPLAY_OPTIONS & aDisplayOptions)63     BASIC_GAL( KIGFX::GAL_DISPLAY_OPTIONS& aDisplayOptions ) :
64         GAL( aDisplayOptions ),
65         m_DC( nullptr ),
66         m_Color( RED ),
67         m_transform(),
68         m_clipBox(),
69         m_isClipped( false ),
70         m_callback( nullptr ),
71         m_callbackData( nullptr ),
72         m_plotter( nullptr )
73     {
74     }
75 
SetPlotter(PLOTTER * aPlotter)76     void SetPlotter( PLOTTER* aPlotter )
77     {
78         m_plotter = aPlotter;
79     }
80 
SetCallback(void (* aCallback)(int x0,int y0,int xf,int yf,void * aData),void * aData)81     void SetCallback( void (* aCallback)( int x0, int y0, int xf, int yf, void* aData ),
82                       void* aData  )
83     {
84         m_callback = aCallback;
85         m_callbackData = aData;
86     }
87 
88     /// Set a clip box for drawings
89     /// If NULL, no clip will be made
SetClipBox(EDA_RECT * aClipBox)90     void SetClipBox( EDA_RECT* aClipBox )
91     {
92         m_isClipped = aClipBox != nullptr;
93 
94         if( aClipBox )
95             m_clipBox = *aClipBox;
96     }
97 
98     /// Save the context.
Save()99     virtual void Save() override
100     {
101         m_transformHistory.push( m_transform );
102     }
103 
Restore()104     virtual void Restore() override
105     {
106         m_transform = m_transformHistory.top();
107         m_transformHistory.pop();
108     }
109 
110     /**
111      * Draw a polyline
112      *
113      * @param aPointList is a list of 2D-Vectors containing the polyline points.
114      */
115     virtual void DrawPolyline( const std::deque<VECTOR2D>& aPointList ) override;
116 
117     virtual void DrawPolyline( const VECTOR2D aPointList[], int aListSize ) override;
118 
119     /**
120      * Start and end points are defined as 2D-Vectors.
121      *
122      * @param aStartPoint   is the start point of the line.
123      * @param aEndPoint     is the end point of the line.
124      */
125     virtual void DrawLine( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint ) override;
126 
127     /**
128      * Translate the context.
129      *
130      * @param aTranslation is the translation vector.
131      */
Translate(const VECTOR2D & aTranslation)132     virtual void Translate( const VECTOR2D& aTranslation ) override
133     {
134         m_transform.m_moveOffset += aTranslation;
135     }
136 
137     /**
138      * Rotate the context.
139      *
140      * @param aAngle is the rotation angle in radians.
141      */
Rotate(double aAngle)142     virtual void Rotate( double aAngle ) override
143     {
144         m_transform.m_rotAngle = aAngle;
145         m_transform.m_rotCenter = m_transform.m_moveOffset;
146     }
147 
148 private:
149     void doDrawPolyline( const std::vector<wxPoint>& aLocalPointList );
150 
151     // Apply the rotation/translation transform to aPoint
152     const VECTOR2D transform( const VECTOR2D& aPoint ) const;
153 
154 public:
155     wxDC* m_DC;
156     COLOR4D m_Color;
157 
158 private:
159     TRANSFORM_PRM m_transform;
160     std::stack <TRANSFORM_PRM>  m_transformHistory;
161 
162     // A clip box, to clip drawings in a wxDC (mandatory to avoid draw issues)
163     EDA_RECT  m_clipBox;        // The clip box
164     bool      m_isClipped;      // Allows/disallows clipping
165 
166     // When calling the draw functions outside a wxDC, to get the basic drawings
167     // lines / polylines ..., a callback function (used in DRC) to store
168     // coordinates of each segment:
169     void (* m_callback)( int x0, int y0, int xf, int yf, void* aData );
170     void* m_callbackData;       // a optional parameter for m_callback
171 
172     // When calling the draw functions for plot, the plotter acts as a wxDC to plot basic items.
173     PLOTTER* m_plotter;
174 };
175 
176 
177 extern BASIC_GAL basic_gal;
178 
179 #endif      // define BASIC_GAL_H
180