KiCad PCB EDA Suite
graphics_abstraction_layer.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KICAD, a free EDA CAD application.
3  *
4  * Copyright (C) 2012 Torsten Hueter, torstenhtr <at> gmx.de
5  * Copyright (C) 2012-2017 Kicad Developers, see change_log.txt for contributors.
6  *
7  * Graphics Abstraction Layer (GAL) - base class
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, you may find one here:
21  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
22  * or you may search the http://www.gnu.org website for the version 2 license,
23  * or you may write to the Free Software Foundation, Inc.,
24  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25  */
26 
27 #include <wx/log.h>
28 
30 #include <gal/definitions.h>
31 
32 #include <math/util.h> // for KiROUND
33 
34 #include <cmath>
35 
36 using namespace KIGFX;
37 
38 
39 GAL::GAL( GAL_DISPLAY_OPTIONS& aDisplayOptions ) :
40  m_options( aDisplayOptions ),
41  m_strokeFont( this ),
42  // m_currentNativeCursor is initialized with KICURSOR::DEFAULT value to avoid
43  // if comparison with uninitialized value on SetNativeCursorStyle method.
44  // Some classes inheriting from GAL has different SetNativeCursorStyle method
45  // implementation and therefore it's called also on constructor
46  // to change the value from DEFAULT to KICURSOR::ARROW
47  m_currentNativeCursor( KICURSOR::DEFAULT )
48 {
49  // Set the default values for the internal variables
50  SetIsFill( false );
51  SetIsStroke( true );
52  SetFillColor( COLOR4D( 0.0, 0.0, 0.0, 0.0 ) );
53  SetStrokeColor( COLOR4D( 1.0, 1.0, 1.0, 1.0 ) );
54  SetLookAtPoint( VECTOR2D( 0, 0 ) );
55  SetZoomFactor( 1.0 );
56  SetRotation( 0.0 );
57  // this value for SetWorldUnitLength is only suitable for Pcbnew.
58  // Other editors/viewer must call SetWorldUnitLength with their internal units
59  SetWorldUnitLength( 1e-9 /* 1 nm */ / 0.0254 /* 1 inch in meters */ );
60  // wxDC::GetPPI() reports 96 DPI, but somehow this value
61  // is the closest match to the legacy renderer
62  SetScreenDPI( 91 );
64  SetLayerDepth( 0.0 );
65  SetFlip( false, false );
66  SetLineWidth( 1.0f );
68  SetAxesEnabled( false );
69 
70  // Set grid defaults
71  SetGridVisibility( true );
72  SetCoarseGrid( 10 );
73  m_gridLineWidth = 0.5f;
75  m_gridMinSpacing = 10;
76 
77  // Initialize the cursor shape
78  SetCursorColor( COLOR4D( 1.0, 1.0, 1.0, 1.0 ) );
79  m_fullscreenCursor = false;
80  m_forceDisplayCursor = false;
81  SetCursorEnabled( false );
82 
83  // Initialize the native widget to an arrow cursor
85 
86  // Initialize text properties
88 
90 
91  // subscribe for settings updates
93 }
94 
95 
97 {
98 }
99 
100 
102 {
103  // defer to the child class first
104  updatedGalDisplayOptions( aOptions );
105 
106  // there is no refresh to do at this level
107 }
108 
109 
111 {
112  bool refresh = false;
113 
115  {
117  refresh = true;
118  }
119 
121  {
122  m_gridLineWidth = std::floor( m_options.m_gridLineWidth + 0.5 );
123  refresh = true;
124  }
125 
127  {
129  refresh = true;
130  }
131 
133  {
135  refresh = true;
136  }
137 
139  {
141  refresh = true;
142  }
143 
145  {
147  refresh = true;
148  }
149 
150  // tell the derived class if the base class needs an update or not
151  return refresh;
152 }
153 
154 
155 void GAL::SetTextAttributes( const EDA_TEXT* aText )
156 {
157  SetGlyphSize( VECTOR2D( aText->GetTextSize() ) );
160  SetFontBold( aText->IsBold() );
161  SetFontItalic( aText->IsItalic() );
162  SetFontUnderlined( false );
163  SetTextMirrored( aText->IsMirrored() );
164 }
165 
166 
168 {
169  // Tiny but non-zero - this will always need setting
170  // there is no built-in default
171  SetGlyphSize( { 1.0, 1.0 } );
172 
175 
176  SetFontBold( false );
177  SetFontItalic( false );
178  SetFontUnderlined( false );
179  SetTextMirrored( false );
180 }
181 
182 
183 VECTOR2D GAL::GetTextLineSize( const UTF8& aText ) const
184 {
185  // Compute the X and Y size of a given text.
186  // Because computeTextLineSize expects a one line text,
187  // aText is expected to be only one line text.
188  return m_strokeFont.computeTextLineSize( aText );
189 }
190 
191 
193 {
195 
196  MATRIX3x3D translation;
197  translation.SetIdentity();
198  // We're deliberately dividing integers to avoid fractional pixel offsets.
199  translation.SetTranslation( VECTOR2D( m_screenSize.x/2, m_screenSize.y/2 ) );
200 
201  MATRIX3x3D rotate;
202  rotate.SetIdentity();
203  rotate.SetRotation( m_rotation );
204 
206  scale.SetIdentity();
207  scale.SetScale( VECTOR2D( m_worldScale, m_worldScale ) );
208 
209  MATRIX3x3D flip;
210  flip.SetIdentity();
211  flip.SetScale( VECTOR2D( m_globalFlipX ? -1.0 : 1.0, m_globalFlipY ? -1.0 : 1.0 ) );
212 
213  MATRIX3x3D lookat;
214  lookat.SetIdentity();
215  lookat.SetTranslation( -m_lookAtPoint );
216 
217  m_worldScreenMatrix = translation * rotate * flip * scale * lookat;
219 }
220 
221 
223 {
224  const MATRIX3x3D& matrix = GetScreenWorldMatrix();
225 
226  VECTOR2D halfSize = VECTOR2D( matrix.GetScale().x * m_screenSize.x * 0.5,
227  matrix.GetScale().y * m_screenSize.y * 0.5 );
228 
229  BOX2D extents;
230  extents.SetOrigin( GetLookAtPoint() - halfSize );
231  extents.SetSize( halfSize * 2 );
232 
233  return extents;
234 }
235 
236 
238 {
239  // just return the current value. This could be cleverer and take
240  // into account other settings in future
241  return m_gridMinSpacing;
242 }
243 
244 
245 VECTOR2D GAL::GetGridPoint( const VECTOR2D& aPoint ) const
246 {
247 #if 0
248  // This old code expects a non zero grid size, which can be wrong here.
249  return VECTOR2D( KiROUND( ( aPoint.x - m_gridOffset.x ) / m_gridSize.x ) * m_gridSize.x + m_gridOffset.x,
250  KiROUND( ( aPoint.y - m_gridOffset.y ) / m_gridSize.y ) * m_gridSize.y + m_gridOffset.y );
251 #else
252  // if grid size == 0.0 there is no grid, so use aPoint as grid reference position
253  double cx = m_gridSize.x > 0.0 ? KiROUND( ( aPoint.x - m_gridOffset.x ) / m_gridSize.x ) * m_gridSize.x + m_gridOffset.x
254  : aPoint.x;
255  double cy = m_gridSize.y > 0.0 ? KiROUND( ( aPoint.y - m_gridOffset.y ) / m_gridSize.y ) * m_gridSize.y + m_gridOffset.y
256  : aPoint.y;
257 
258  return VECTOR2D( cx, cy );
259 #endif
260 }
261 
262 const int GAL::MIN_DEPTH = -1024;
263 const int GAL::MAX_DEPTH = 1023;
264 const int GAL::GRID_DEPTH = MAX_DEPTH - 1;
265 
266 
268 {
270 
271  // dim the cursor if it's only on because it was forced
272  // (this helps to provide a hint for active tools)
273  if( !m_isCursorEnabled )
274  color.a = color.a * 0.5;
275 
276  return color;
277 }
278 
279 
281 {
282  if( m_currentNativeCursor == aCursor )
283  return false;
284 
285  m_currentNativeCursor = aCursor;
286 
287  return true;
288 }
static const int GRID_DEPTH
Depth level on which the grid is drawn.
virtual void SetFillColor(const COLOR4D &aColor)
Set the fill color.
An 8 bit string that is assuredly encoded in UTF8, and supplies special conversion support to and fro...
Definition: utf8.h:70
bool IsBold() const
Definition: eda_text.h:204
Use lines for the grid.
EDA_TEXT_VJUSTIFY_T GetVertJustify() const
Definition: eda_text.h:220
bool IsMirrored() const
Definition: eda_text.h:210
The Cairo implementation of the graphics abstraction layer.
Definition: color4d.cpp:236
bool m_axesEnabled
Should the axes be drawn.
const MATRIX3x3D & GetScreenWorldMatrix() const
Get the screen <-> world transformation matrix.
void SetRotation(T aAngle)
Set the rotation components of the matrix.
Definition: matrix3x3.h:245
double computeMinGridSpacing() const
compute minimum grid spacing from the grid settings
STROKE_FONT m_strokeFont
Instance of object that stores information about how to draw texts.
KIGFX::GRID_STYLE m_gridStyle
Snapping options for the grid.
MATRIX3x3D m_screenWorldMatrix
Screen transformation.
void SetCursorEnabled(bool aCursorEnabled)
Enable/disable cursor.
virtual void SetTextAttributes(const EDA_TEXT *aText)
Loads attributes of the given text (bold/italic/underline/mirrored and so on).
MATRIX3x3 Inverse() const
Determine the inverse of the matrix.
Definition: matrix3x3.h:339
LINK Subscribe(ObserverInterface *aObserver)
Add a subscription returning an RAII link.
Definition: observable.h:157
int color
Definition: DXF_plotter.cpp:57
VECTOR2D m_gridSize
The grid size.
void SetFontBold(bool aBold)
Set bold property of current font.
void SetFlip(bool xAxis, bool yAxis)
Sets flipping of the screen.
VECTOR2D m_lookAtPoint
Point to be looked at in world space.
bool LoadNewStrokeFont(const char *const aNewStrokeFont[], int aNewStrokeFontSize)
Load the new stroke font.
Definition: stroke_font.cpp:58
KICURSOR
Definition: cursors.h:33
bool m_forceDisplayCursor
The pixel scale factor (>1 for hi-DPI scaled displays)
double m_gridMinSpacing
Whether or not to draw the coordinate system axes.
VECTOR2D GetTextLineSize(const UTF8 &aText) const
Compute the X and Y size of a given text.
virtual void ComputeWorldScreenMatrix()
Compute the world <-> screen transformation matrix.
void SetScreenDPI(double aScreenDPI)
Set the dots per inch of the screen.
void SetIdentity()
Set the matrix to the identity matrix.
Definition: matrix3x3.h:210
virtual void SetLayerDepth(double aLayerDepth)
Set the depth of the layer (position on the z-axis)
VECTOR2< T > GetScale() const
Get the scale components of the matrix.
Definition: matrix3x3.h:265
float m_gridLineWidth
Line width of the grid.
MATRIX3x3D m_worldScreenMatrix
World transformation.
virtual void SetLineWidth(float aLineWidth)
Set the line width.
bool m_axesEnabled
Fullscreen crosshair or small cross.
double m_worldScale
The scale factor world->screen.
VECTOR2D GetGridPoint(const VECTOR2D &aPoint) const
For a given point it returns the nearest point belonging to the grid in world coordinates.
bool IsItalic() const
Definition: eda_text.h:201
A mix-in class (via multiple inheritance) that handles texts such as labels, parts,...
Definition: eda_text.h:140
static const int MIN_DEPTH
Possible depth range.
GAL_DISPLAY_OPTIONS & m_options
GAL(GAL_DISPLAY_OPTIONS &aOptions)
bool m_globalFlipX
Flag for X axis flipping.
EDA_TEXT_HJUSTIFY_T GetHorizJustify() const
Definition: eda_text.h:219
const char *const newstroke_font[]
Array containing strokes for unicode glyphs.
void OnGalDisplayOptionsChanged(const GAL_DISPLAY_OPTIONS &aOptions) override
Handler for observer settings changes.
void ResetTextAttributes()
Reset text attributes to default styling.
VECTOR2< double > VECTOR2D
Definition: vector2d.h:622
virtual void SetIsFill(bool aIsFillEnabled)
Enable/disable fill.
int m_gridMinSpacing
Minimum screen size of the grid (pixels) below which the grid is not drawn.
bool m_isCursorEnabled
Is the cursor enabled?
const wxSize & GetTextSize() const
Definition: eda_text.h:259
VECTOR2D computeTextLineSize(const UTF8 &aText) const
Compute the X and Y size of a given text.
void SetCursorColor(const COLOR4D &aCursorColor)
Set the cursor color.
void SetVerticalJustify(const EDA_TEXT_VJUSTIFY_T aVerticalJustify)
Set the vertical justify for text drawing.
void SetFontItalic(bool aItalic)
Set italic property of current font.
void SetAxesEnabled(bool aAxesEnabled)
Enable drawing the axes.
void SetWorldUnitLength(double aWorldUnitLength)
Set the unit length.
COLOR4D getCursorColor() const
Get the actual cursor color to draw.
COLOR4D m_cursorColor
Cursor color.
void SetGlyphSize(const VECTOR2D &aSize)
Set the font glyph size.
virtual bool updatedGalDisplayOptions(const GAL_DISPLAY_OPTIONS &aOptions)
Handle updating display options.
virtual bool SetNativeCursorStyle(KICURSOR aCursor)
Set the cursor in the native panel.
const int newstroke_font_bufsize
void computeWorldScale()
Compute the scaling factor for the world->screen matrix.
GRID_STYLE m_gridStyle
Grid display style.
void SetTextMirrored(bool aMirrored)
Set a mirrored property of text.
const VECTOR2D & GetLookAtPoint() const
Get the look at point.
virtual void SetStrokeColor(const COLOR4D &aColor)
Set the stroke color.
void SetCoarseGrid(int aInterval)
Draw every tick line wider.
double m_rotation
Rotation transformation (radians)
const int scale
bool m_forceDisplayCursor
Always show cursor.
void SetFontUnderlined(bool aUnderlined)
void SetScale(VECTOR2< T > aScale)
Set the scale components of the matrix.
Definition: matrix3x3.h:257
void SetRotation(double aRotation)
Set the rotation angle.
bool m_globalFlipY
Flag for Y axis flipping.
VECTOR2D m_gridOffset
The grid offset to compensate cursor position.
void SetOrigin(const Vec &pos)
Definition: box2.h:193
double m_gridLineWidth
Minimum pixel distance between displayed grid lines.
void SetZoomFactor(double aZoomFactor)
Set the zoom factor of the scene.
constexpr ret_type KiROUND(fp_type v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: util.h:73
BOX2D GetVisibleWorldExtents() const
void SetGridVisibility(bool aVisibility)
Set the visibility setting of the grid.
bool m_fullscreenCursor
Shape of the cursor (fullscreen or small cross)
void SetTranslation(VECTOR2< T > aTranslation)
Set the translation components of the matrix.
Definition: matrix3x3.h:226
void SetLookAtPoint(const VECTOR2D &aPoint)
Set the Point in world space to look at.
void SetHorizontalJustify(const EDA_TEXT_HJUSTIFY_T aHorizontalJustify)
Set the horizontal justify for text drawing.
virtual void SetIsStroke(bool aIsStrokeEnabled)
Enable/disable stroked outlines.
void SetDepthRange(const VECTOR2D &aDepthRange)
Set the range of the layer depth.
VECTOR2I m_screenSize
Screen size in screen coordinates.
bool m_fullscreenCursor
Force cursor display.
KICURSOR m_currentNativeCursor
Current cursor.
static const int MAX_DEPTH
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:103