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  SetFontUnderlined( false );
154  SetTextMirrored( aText->IsMirrored() );
155 }
156 
157 
159 {
160  // Tiny but non-zero - this will always need setting
161  // there is no built-in default
162  SetGlyphSize( { 1.0, 1.0 } );
163 
166 
167  SetFontBold( false );
168  SetFontItalic( false );
169  SetFontUnderlined( false );
170  SetTextMirrored( false );
171 }
172 
173 
174 VECTOR2D GAL::GetTextLineSize( const UTF8& aText ) const
175 {
176  // Compute the X and Y size of a given text.
177  // Because computeTextLineSize expects a one line text,
178  // aText is expected to be only one line text.
179  return strokeFont.computeTextLineSize( aText );
180 }
181 
182 
184 {
186 
187  MATRIX3x3D translation;
188  translation.SetIdentity();
189  translation.SetTranslation( 0.5 * VECTOR2D( screenSize ) );
190 
191  MATRIX3x3D rotate;
192  rotate.SetIdentity();
193  rotate.SetRotation( rotation );
194 
196  scale.SetIdentity();
197  scale.SetScale( VECTOR2D( worldScale, worldScale ) );
198 
199  MATRIX3x3D flip;
200  flip.SetIdentity();
201  flip.SetScale( VECTOR2D( globalFlipX ? -1.0 : 1.0, globalFlipY ? -1.0 : 1.0 ) );
202 
203  MATRIX3x3D lookat;
204  lookat.SetIdentity();
205  lookat.SetTranslation( -lookAtPoint );
206 
207  worldScreenMatrix = translation * rotate * flip * scale * lookat;
209 }
210 
211 
213 {
214  // just return the current value. This could be cleverer and take
215  // into account other settings in future
216  return gridMinSpacing;
217 }
218 
219 
220 VECTOR2D GAL::GetGridPoint( const VECTOR2D& aPoint ) const
221 {
222 #if 0
223  // This old code expects a non zero grid size, which can be wrong here.
224  return VECTOR2D( KiROUND( ( aPoint.x - gridOffset.x ) / gridSize.x ) * gridSize.x + gridOffset.x,
225  KiROUND( ( aPoint.y - gridOffset.y ) / gridSize.y ) * gridSize.y + gridOffset.y );
226 #else
227  // if grid size == 0.0 there is no grid, so use aPoint as grid reference position
228  double cx = gridSize.x > 0.0 ? KiROUND( ( aPoint.x - gridOffset.x ) / gridSize.x ) * gridSize.x + gridOffset.x
229  : aPoint.x;
230  double cy = gridSize.y > 0.0 ? KiROUND( ( aPoint.y - gridOffset.y ) / gridSize.y ) * gridSize.y + gridOffset.y
231  : aPoint.y;
232 
233  return VECTOR2D( cx, cy );
234 #endif
235 }
236 
237 const int GAL::MIN_DEPTH = -1024;
238 const int GAL::MAX_DEPTH = 1023;
239 const int GAL::GRID_DEPTH = MAX_DEPTH - 1;
240 
241 
243 {
244  auto color = cursorColor;
245 
246  // dim the cursor if it's only on because it was forced
247  // (this helps to provide a hint for active tools)
248  if( !isCursorEnabled )
249  {
250  color.a = color.a * 0.5;
251  }
252 
253  return color;
254 }
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:190
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:206
bool IsMirrored() const
Definition: eda_text.h:196
VECTOR2D gridOffset
The grid offset to compensate cursor position.
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
Snapping options for the grid.
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)
Add a subscription returning an RAII link.
Definition: observable.h:157
GAL_DISPLAY_OPTIONS & options
double rotation
Rotation transformation (radians)
int color
Definition: DXF_plotter.cpp:60
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:56
MATRIX3x3D screenWorldMatrix
Screen transformation.
bool m_forceDisplayCursor
The pixel scale factor (>1 for hi-DPI scaled displays)
bool globalFlipX
Flag for X axis flipping.
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: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
Fullscreen crosshair or small cross.
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:187
A mix-in class (via multiple inheritance) that handles texts such as labels, parts,...
Definition: eda_text.h:119
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:205
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:622
virtual void SetIsFill(bool aIsFillEnabled)
Enable/disable fill.
const wxSize & GetTextSize() const
Definition: eda_text.h:245
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 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.
virtual bool updatedGalDisplayOptions(const GAL_DISPLAY_OPTIONS &aOptions)
Handle updating display options.
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.
void SetGlyphSize(const VECTOR2D aSize)
Set the font glyph size.
const int scale
void SetFontUnderlined(bool aUnderlined)
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
Minimum pixel distance between displayed grid lines.
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)
Set the visibility setting of the grid.
void SetTranslation(VECTOR2< T > aTranslation)
Set the translation components of the matrix.
Definition: matrix3x3.h:230
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
Force cursor display.
bool forceDisplayCursor
Always show cursor.
static const int MAX_DEPTH
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:98