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  options( aDisplayOptions ),
41  strokeFont( this )
42 {
43  // Set the default values for the internal variables
44  SetIsFill( false );
45  SetIsStroke( true );
46  SetFillColor( COLOR4D( 0.0, 0.0, 0.0, 0.0 ) );
47  SetStrokeColor( COLOR4D( 1.0, 1.0, 1.0, 1.0 ) );
48  SetLookAtPoint( VECTOR2D( 0, 0 ) );
49  SetZoomFactor( 1.0 );
50  SetRotation( 0.0 );
51  // this value for SetWorldUnitLength is only suitable for Pcbnew.
52  // Other editors/viewer must call SetWorldUnitLength with their internal units
53  SetWorldUnitLength( 1e-9 /* 1 nm */ / 0.0254 /* 1 inch in meters */ );
54  // wxDC::GetPPI() reports 96 DPI, but somehow this value
55  // is the closest match to the legacy renderer
56  SetScreenDPI( 91 );
58  SetLayerDepth( 0.0 );
59  SetFlip( false, false );
60  SetLineWidth( 1.0f );
62  SetAxesEnabled( false );
63 
64  // Set grid defaults
65  SetGridVisibility( true );
66  SetCoarseGrid( 10 );
67  gridLineWidth = 0.5f;
69  gridMinSpacing = 10;
70 
71  // Initialize the cursor shape
72  SetCursorColor( COLOR4D( 1.0, 1.0, 1.0, 1.0 ) );
73  fullscreenCursor = false;
74  forceDisplayCursor = false;
75  SetCursorEnabled( false );
76 
77  // Initialize text properties
79 
81 
82  // subscribe for settings updates
83  observerLink = options.Subscribe( this );
84 }
85 
86 
88 {
89 }
90 
91 
93 {
94  // defer to the child class first
95  updatedGalDisplayOptions( aOptions );
96 
97  // there is no refresh to do at this level
98 }
99 
100 
102 {
103  bool refresh = false;
104 
105  if( options.m_gridStyle != gridStyle )
106  {
108  refresh = true;
109  }
110 
112  {
113  gridLineWidth = std::floor( options.m_gridLineWidth + 0.5 );
114  refresh = true;
115  }
116 
118  {
120  refresh = true;
121  }
122 
124  {
126  refresh = true;
127  }
128 
130  {
132  refresh = true;
133  }
134 
136  {
138  refresh = true;
139  }
140 
141  // tell the derived class if the base class needs an update or not
142  return refresh;
143 }
144 
145 
146 void GAL::SetTextAttributes( const EDA_TEXT* aText )
147 {
148  SetGlyphSize( VECTOR2D( aText->GetTextSize() ) );
151  SetFontBold( aText->IsBold() );
152  SetFontItalic( aText->IsItalic() );
153  SetTextMirrored( aText->IsMirrored() );
154 }
155 
156 
158 {
159  // Tiny but non-zero - this will always need setting
160  // there is no built-in default
161  SetGlyphSize( { 1.0, 1.0 } );
162 
165 
166  SetFontBold( false );
167  SetFontItalic( false );
168  SetTextMirrored( false );
169 }
170 
171 
172 VECTOR2D GAL::GetTextLineSize( const UTF8& aText ) const
173 {
174  // Compute the X and Y size of a given text.
175  // Because computeTextLineSize expects a one line text,
176  // aText is expected to be only one line text.
177  return strokeFont.computeTextLineSize( aText );
178 }
179 
180 
182 {
184 
185  MATRIX3x3D translation;
186  translation.SetIdentity();
187  translation.SetTranslation( 0.5 * VECTOR2D( screenSize ) );
188 
189  MATRIX3x3D rotate;
190  rotate.SetIdentity();
191  rotate.SetRotation( rotation );
192 
194  scale.SetIdentity();
195  scale.SetScale( VECTOR2D( worldScale, worldScale ) );
196 
197  MATRIX3x3D flip;
198  flip.SetIdentity();
199  flip.SetScale( VECTOR2D( globalFlipX ? -1.0 : 1.0, globalFlipY ? -1.0 : 1.0 ) );
200 
201  MATRIX3x3D lookat;
202  lookat.SetIdentity();
203  lookat.SetTranslation( -lookAtPoint );
204 
205  worldScreenMatrix = translation * rotate * flip * scale * lookat;
207 }
208 
209 
211 {
212  // just return the current value. This could be cleverer and take
213  // into account other settings in future
214  return gridMinSpacing;
215 }
216 
217 
218 VECTOR2D GAL::GetGridPoint( const VECTOR2D& aPoint ) const
219 {
220 #if 0
221  // This old code expects a non zero grid size, which can be wrong here.
222  return VECTOR2D( KiROUND( ( aPoint.x - gridOffset.x ) / gridSize.x ) * gridSize.x + gridOffset.x,
223  KiROUND( ( aPoint.y - gridOffset.y ) / gridSize.y ) * gridSize.y + gridOffset.y );
224 #else
225  // if grid size == 0.0 there is no grid, so use aPoint as grid reference position
226  double cx = gridSize.x > 0.0 ? KiROUND( ( aPoint.x - gridOffset.x ) / gridSize.x ) * gridSize.x + gridOffset.x
227  : aPoint.x;
228  double cy = gridSize.y > 0.0 ? KiROUND( ( aPoint.y - gridOffset.y ) / gridSize.y ) * gridSize.y + gridOffset.y
229  : aPoint.y;
230 
231  return VECTOR2D( cx, cy );
232 #endif
233 }
234 
235 const int GAL::MIN_DEPTH = -1024;
236 const int GAL::MAX_DEPTH = 1023;
237 const int GAL::GRID_DEPTH = MAX_DEPTH - 1;
238 
239 
241 {
242  auto color = cursorColor;
243 
244  // dim the cursor if it's only on because it was forced
245  // (this helps to provide a hint for active tools)
246  if( !isCursorEnabled )
247  {
248  color.a = color.a * 0.5;
249  }
250 
251  return color;
252 }
static const int GRID_DEPTH
Depth level on which the grid is drawn.
virtual void SetFillColor(const COLOR4D &aColor)
Set the fill color.
UTF8 is an 8 bit string that is assuredly encoded in UTF8, and supplies special conversion support to...
Definition: utf8.h:73
void SetFontItalic(const bool aItalic)
Set italic property of current font.
bool IsBold() const
Definition: eda_text.h:182
STROKE_FONT strokeFont
Instance of object that stores information about how to draw texts.
Use lines for the grid.
bool axesEnabled
Should the axes be drawn.
EDA_TEXT_VJUSTIFY_T GetVertJustify() const
Definition: eda_text.h:199
bool IsMirrored() const
Definition: eda_text.h:188
VECTOR2D gridOffset
The grid offset to compensate cursor position.
Class CAIRO_GAL is the cairo implementation of the graphics abstraction layer.
Definition: color4d.cpp:175
void SetRotation(T aAngle)
Set the rotation components of the matrix.
Definition: matrix3x3.h:249
double computeMinGridSpacing() const
compute minimum grid spacing from the grid settings
KIGFX::GRID_STYLE m_gridStyle
The grid style to draw the grid in
bool isCursorEnabled
Is the cursor enabled?
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).
int gridMinSpacing
Minimum screen size of the grid (pixels) below which the grid is not drawn.
MATRIX3x3 Inverse() const
Determine the inverse of the matrix.
Definition: matrix3x3.h:343
GRID_STYLE gridStyle
Grid display style.
LINK Subscribe(ObserverInterface *aObserver)
Function Subscribe adds a subscription returning an RAII link.
Definition: observable.h:161
GAL_DISPLAY_OPTIONS & options
double rotation
Rotation transformation (radians)
int color
Definition: DXF_plotter.cpp:61
void SetFlip(bool xAxis, bool yAxis)
Sets flipping of the screen.
bool LoadNewStrokeFont(const char *const aNewStrokeFont[], int aNewStrokeFontSize)
Load the new stroke font.
Definition: stroke_font.cpp:55
MATRIX3x3D screenWorldMatrix
Screen transformation.
bool m_forceDisplayCursor
Force cursor display
bool globalFlipX
Flag for X axis flipping.
double m_gridMinSpacing
Minimum pixel distance between displayed grid lines
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:214
virtual void SetLayerDepth(double aLayerDepth)
Set the depth of the layer (position on the z-axis)
VECTOR2D lookAtPoint
Point to be looked at in world space.
virtual void SetLineWidth(float aLineWidth)
Set the line width.
bool m_axesEnabled
Whether or not to draw the coordinate system axes
VECTOR2D GetGridPoint(const VECTOR2D &aPoint) const
Function GetGridPoint() For a given point it returns the nearest point belonging to the grid in world...
bool IsItalic() const
Definition: eda_text.h:179
A mix-in class (via multiple inheritance) that handles texts such as labels, parts,...
Definition: eda_text.h:112
static const int MIN_DEPTH
Possible depth range.
GAL(GAL_DISPLAY_OPTIONS &aOptions)
void SetFontBold(const bool aBold)
Set bold property of current font.
COLOR4D cursorColor
Cursor color.
EDA_TEXT_HJUSTIFY_T GetHorizJustify() const
Definition: eda_text.h:198
const char *const newstroke_font[]
Array containing strokes for unicode glyphs.
bool fullscreenCursor
Shape of the cursor (fullscreen or small cross)
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:593
virtual void SetIsFill(bool aIsFillEnabled)
Enable/disable fill.
const wxSize & GetTextSize() const
Definition: eda_text.h:238
VECTOR2D computeTextLineSize(const UTF8 &aText) const
Compute the X and Y size of a given text.
float gridLineWidth
Line width of the grid.
void SetTextMirrored(const bool aMirrored)
Set a mirrored property of 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.
MATRIX3x3D worldScreenMatrix
World transformation.
void SetAxesEnabled(bool aAxesEnabled)
Enables drawing the axes.
void SetWorldUnitLength(double aWorldUnitLength)
Set the unit length.
COLOR4D getCursorColor() const
Gets the actual cursor color to draw.
virtual bool updatedGalDisplayOptions(const GAL_DISPLAY_OPTIONS &aOptions)
Function updatedGalDisplayOptions.
const int newstroke_font_bufsize
void computeWorldScale()
Compute the scaling factor for the world->screen matrix.
virtual void SetStrokeColor(const COLOR4D &aColor)
Set the stroke color.
void SetCoarseGrid(int aInterval)
Draw every tick line wider.
const int scale
void SetScale(VECTOR2< T > aScale)
Set the scale components of the matrix.
Definition: matrix3x3.h:261
void SetRotation(double aRotation)
Set the rotation angle.
bool globalFlipY
Flag for Y axis flipping.
double worldScale
The scale factor world->screen.
VECTOR2D gridSize
The grid size.
double m_gridLineWidth
Thickness to render grid lines/dots
VECTOR2I screenSize
Screen size in screen coordinates.
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:68
void SetGridVisibility(bool aVisibility)
Sets the visibility setting of the grid.
void SetTranslation(VECTOR2< T > aTranslation)
Set the translation components of the matrix.
Definition: matrix3x3.h:230
void SetGlyphSize(const VECTOR2D aGlyphSize)
Set the font glyph size.
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.
bool m_fullscreenCursor
Fullscreen crosshair or small cross
bool forceDisplayCursor
Always show cursor.
static const int MAX_DEPTH
COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:99