KiCad PCB EDA Suite
Loading...
Searching...
No Matches
cairo_gal.h
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 The KiCad Developers, see AUTHORS.txt for contributors.
6 * Copyright (C) 2017-2018 CERN
7 * @author Maciej Suminski <[email protected]>
8 *
9 * CairoGal - Graphics Abstraction Layer for Cairo
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, you may find one here:
23 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
24 * or you may search the http://www.gnu.org website for the version 2 license,
25 * or you may write to the Free Software Foundation, Inc.,
26 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
27 */
28
29#ifndef CAIROGAL_H_
30#define CAIROGAL_H_
31
32#include <map>
33#include <iterator>
34
35#include <cairo.h>
36
37#include <gal/gal.h>
39#include <wx/dcbuffer.h>
40
41#include <memory>
42
54namespace KIGFX
55{
57
59{
60public:
61 CAIRO_GAL_BASE( GAL_DISPLAY_OPTIONS& aDisplayOptions );
62
64
65 bool IsCairoEngine() override { return true; }
66
67 // ---------------
68 // Drawing methods
69 // ---------------
70
72 void DrawLine( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint ) override;
73
75 void DrawSegment( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint,
76 double aWidth ) override;
77
79 void DrawCircle( const VECTOR2D& aCenterPoint, double aRadius ) override;
80
82 void DrawHoleWall( const VECTOR2D& aCenterPoint, double aHoleRadius,
83 double aWallWidth ) override;
84
86 void DrawArc( const VECTOR2D& aCenterPoint, double aRadius, const EDA_ANGLE& aStartAngle,
87 const EDA_ANGLE& aAngle ) override;
88
91 void DrawArcSegment( const VECTOR2D& aCenterPoint, double aRadius, const EDA_ANGLE& aStartAngle,
92 const EDA_ANGLE& aAngle, double aWidth, double aMaxError ) override;
93
95 void DrawEllipse( const VECTOR2D& aCenterPoint, double aMajorRadius, double aMinorRadius,
96 const EDA_ANGLE& aRotation ) override;
97
99 void DrawEllipseArc( const VECTOR2D& aCenterPoint, double aMajorRadius, double aMinorRadius,
100 const EDA_ANGLE& aRotation, const EDA_ANGLE& aStartAngle,
101 const EDA_ANGLE& aEndAngle ) override;
102
104 void DrawRectangle( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint ) override;
105
107 void DrawSegmentChain( const std::vector<VECTOR2D>& aPointList, double aWidth ) override;
108 void DrawSegmentChain( const SHAPE_LINE_CHAIN& aLineChain, double aWidth ) override;
109
111 void DrawPolyline( const std::deque<VECTOR2D>& aPointList ) override { drawPoly( aPointList ); }
112 void DrawPolyline( const VECTOR2D aPointList[], int aListSize ) override
113 {
114 drawPoly( aPointList, aListSize );
115 }
116
117 void DrawPolyline( const SHAPE_LINE_CHAIN& aLineChain ) override { drawPoly( aLineChain ); }
118
120 void DrawPolylines( const std::vector<std::vector<VECTOR2D>>& aPointLists ) override
121 {
122 for( const std::vector<VECTOR2D>& points : aPointLists )
123 drawPoly( points );
124 }
125
127 void DrawPolygon( const std::deque<VECTOR2D>& aPointList ) override { drawPoly( aPointList ); }
128 void DrawPolygon( const VECTOR2D aPointList[], int aListSize ) override
129 {
130 drawPoly( aPointList, aListSize );
131 }
132
133 void DrawPolygon( const SHAPE_POLY_SET& aPolySet, bool aStrokeTriangulation = false ) override;
134 void DrawPolygon( const SHAPE_LINE_CHAIN& aPolySet ) override;
135
137 void DrawGlyph( const KIFONT::GLYPH& aPolySet, int aNth, int aTotal ) override;
138
140 void DrawGlyphs( const std::vector<std::unique_ptr<KIFONT::GLYPH>>& aGlyphs ) override
141 {
142 for( size_t i = 0; i < aGlyphs.size(); i++ )
143 DrawGlyph( *aGlyphs[i], i, aGlyphs.size() );
144 }
145
147 void DrawCurve( const VECTOR2D& startPoint, const VECTOR2D& controlPointA,
148 const VECTOR2D& controlPointB, const VECTOR2D& endPoint,
149 double aFilterValue = 0.0 ) override;
150
152 void DrawBitmap( const BITMAP_BASE& aBitmap, double alphaBlend = 1.0 ) override;
153
154 // --------------
155 // Screen methods
156 // --------------
157
159 void ResizeScreen( int aWidth, int aHeight ) override;
160
162 void Flush() override;
163
165 void ClearScreen() override;
166
167 // -----------------
168 // Attribute setting
169 // -----------------
170
172 void SetIsFill( bool aIsFillEnabled ) override;
173
175 void SetIsStroke( bool aIsStrokeEnabled ) override;
176
178 void SetStrokeColor( const COLOR4D& aColor ) override;
179
181 void SetFillColor( const COLOR4D& aColor ) override;
182
184 void SetLineWidth( float aLineWidth ) override;
185
187 void SetLayerDepth( double aLayerDepth ) override;
188
189 // --------------
190 // Transformation
191 // --------------
192
194 void Transform( const MATRIX3x3D& aTransformation ) override;
195
197 void Rotate( double aAngle ) override;
198
200 void Translate( const VECTOR2D& aTranslation ) override;
201
203 void Scale( const VECTOR2D& aScale ) override;
204
206 void Save() override;
207
209 void Restore() override;
210
211 // --------------------------------------------
212 // Group methods
213 // ---------------------------------------------
214
216 int BeginGroup() override;
217
219 void EndGroup() override;
220
222 void DrawGroup( int aGroupNumber ) override;
223
225 void ChangeGroupColor( int aGroupNumber, const COLOR4D& aNewColor ) override;
226
228 void ChangeGroupDepth( int aGroupNumber, int aDepth ) override;
229
231 void DeleteGroup( int aGroupNumber ) override;
232
234 void ClearCache() override;
235
236 // --------------------------------------------------------
237 // Handling the world <-> screen transformation
238 // --------------------------------------------------------
239
241 void SetNegativeDrawMode( bool aSetting ) override;
242
243 // -------
244 // Cursor
245 // -------
246
248 void DrawCursor( const VECTOR2D& aCursorPosition ) override;
249
250 void EnableDepthTest( bool aEnabled = false ) override;
251
253 void DrawGrid() override;
254
256 void BeginDrawing() override;
257
259 void EndDrawing() override;
260
261
262protected:
263 // Geometric transforms according to the m_currentWorld2Screen transform matrix:
264 double xform( double x ); // scale
265 const VECTOR2D xform( double x, double y ); // rotation, scale and offset
266 const VECTOR2D xform( const VECTOR2D& aP ); // rotation, scale and offset
267
274 double angle_xform( double aAngle );
275
283 void arc_angles_xform_and_normalize( double& aStartAngle, double& aEndAngle );
284
285 void resetContext();
286
293 void drawGridLine( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint );
294 void drawGridCross( const VECTOR2D& aPoint );
295 void drawGridPoint( const VECTOR2D& aPoint, double aWidth, double aHeight );
296 void drawAxes( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint );
297
298
299 void flushPath();
300 void storePath();
301
305 void blitCursor( wxMemoryDC& clientDC );
306
308 void drawPoly( const std::deque<VECTOR2D>& aPointList );
309 void drawPoly( const std::vector<VECTOR2D>& aPointList );
310 void drawPoly( const VECTOR2D aPointList[], int aListSize );
311 void drawPoly( const SHAPE_LINE_CHAIN& aLineChain );
312
318 unsigned int getNewGroupNumber();
319
320 void syncLineWidth( bool aForceWidth = false, double aWidth = 0.0 );
321 void updateWorldScreenMatrix();
322 const VECTOR2D roundp( const VECTOR2D& v );
323
325 typedef GAL super;
326
328 static const int MAX_CAIRO_ARGUMENTS = 4;
329
348
351 {
353 union {
355 bool BoolArg;
356 int IntArg = 0;
357 } m_Argument;
358 cairo_path_t* m_CairoPath = nullptr;
359 };
360
361 typedef std::deque<GROUP_ELEMENT> GROUP;
362
363 // Variables for the grouping function
366 std::map<int, GROUP> m_groups;
367 unsigned int m_groupCounter;
369
372
374 cairo_matrix_t m_currentXform;
375 cairo_matrix_t m_currentWorld2Screen;
377 cairo_t* m_context;
378 cairo_surface_t* m_surface;
379
381 std::vector<cairo_surface_t*> m_imageSurfaces;
382
383 std::vector<cairo_matrix_t> m_xformStack;
385 static constexpr cairo_format_t GAL_FORMAT = CAIRO_FORMAT_ARGB32;
386};
387
388
389class GAL_API CAIRO_GAL : public CAIRO_GAL_BASE, public wxWindow
390{
391public:
403 CAIRO_GAL( GAL_DISPLAY_OPTIONS& aDisplayOptions, wxWindow* aParent,
404 wxEvtHandler* aMouseListener = nullptr, wxEvtHandler* aPaintListener = nullptr,
405 const wxString& aName = wxT( "CairoCanvas" ) );
406
407 ~CAIRO_GAL();
408
410 bool IsVisible() const override
411 {
412 return IsShownOnScreen() && !GetClientRect().IsEmpty();
413 }
414
415 void ResizeScreen( int aWidth, int aHeight ) override;
416
417 bool Show( bool aShow ) override;
418
419 int BeginGroup() override;
420
421 void EndGroup() override;
422
423 void SetTarget( RENDER_TARGET aTarget ) override;
424
425 RENDER_TARGET GetTarget() const override;
426
427 void ClearTarget( RENDER_TARGET aTarget ) override;
428
430 void StartDiffLayer() override;
431
433 void EndDiffLayer() override;
434
436 void StartNegativesLayer() override;
437
439 void EndNegativesLayer() override;
440
447 void PostPaint( wxPaintEvent& aEvent );
448
449 void SetMouseListener( wxEvtHandler* aMouseListener )
450 {
451 m_mouseListener = aMouseListener;
452 }
453
454 void SetPaintListener( wxEvtHandler* aPaintListener )
455 {
456 m_paintListener = aPaintListener;
457 }
458
460 bool SetNativeCursorStyle( KICURSOR aCursor, bool aHiDPI ) override;
461
463 void BeginDrawing() override;
464
466 void EndDrawing() override;
467
469 void initSurface();
470
472 void deinitSurface();
473
475 void allocateBitmaps();
476
478 void deleteBitmaps();
479
481 void setCompositor();
482
483 // Event handlers
489 void onPaint( wxPaintEvent& aEvent );
490
496 void skipMouseEvent( wxMouseEvent& aEvent );
497
503 void skipGestureEvent( wxGestureEvent& aEvent );
504
510 void onSetNativeCursor( wxSetCursorEvent& aEvent );
511
513 bool updatedGalDisplayOptions( const GAL_DISPLAY_OPTIONS& aOptions ) override;
514
515protected:
516 // Compositor related variables
517 std::shared_ptr<CAIRO_COMPOSITOR> m_compositor;
518 unsigned int m_mainBuffer;
519 unsigned int m_overlayBuffer;
520 unsigned int m_tempBuffer;
521 unsigned int m_savedBuffer;
524
525 // Variables related to wxWidgets
526 wxWindow* m_parentWindow;
527 wxEvtHandler* m_mouseListener;
528 wxEvtHandler* m_paintListener;
529 unsigned int m_bufferSize;
530 unsigned char* m_wxOutput;
531
532 // Variables related to Cairo <-> wxWidgets
533 unsigned char* m_bitmapBuffer;
538
540};
541
542} // namespace KIGFX
543
544#endif // CAIROGAL_H_
static double roundp(double x)
This class handle bitmap images in KiCad.
Definition bitmap_base.h:49
void DrawGlyph(const KIFONT::GLYPH &aPolySet, int aNth, int aTotal) override
Draw a polygon representing a font glyph.
cairo_surface_t * m_surface
Cairo surface.
Definition cairo_gal.h:378
void DrawPolyline(const std::deque< VECTOR2D > &aPointList) override
Draw a polyline.
Definition cairo_gal.h:111
GAL super
Super class definition.
Definition cairo_gal.h:325
cairo_matrix_t m_cairoWorldScreenMatrix
Cairo world to screen transform matrix.
Definition cairo_gal.h:373
unsigned int m_groupCounter
Counter used for generating group keys.
Definition cairo_gal.h:367
bool m_isElementAdded
Was an graphic element added ?
Definition cairo_gal.h:365
std::deque< GROUP_ELEMENT > GROUP
A graphic group type definition.
Definition cairo_gal.h:361
bool IsCairoEngine() override
Return true if the GAL engine is a Cairo based type.
Definition cairo_gal.h:65
void DrawGlyphs(const std::vector< std::unique_ptr< KIFONT::GLYPH > > &aGlyphs) override
Draw polygons representing font glyphs.
Definition cairo_gal.h:140
CAIRO_GAL_BASE(GAL_DISPLAY_OPTIONS &aDisplayOptions)
Definition cairo_gal.cpp:52
std::vector< cairo_surface_t * > m_imageSurfaces
List of surfaces that were created by painting images, to be cleaned up later.
Definition cairo_gal.h:381
GROUP * m_currentGroup
Currently used group.
Definition cairo_gal.h:368
bool m_isGrouping
Is grouping enabled ?
Definition cairo_gal.h:364
static constexpr cairo_format_t GAL_FORMAT
Format used to store pixels.
Definition cairo_gal.h:385
cairo_matrix_t m_currentXform
Definition cairo_gal.h:374
static const int MAX_CAIRO_ARGUMENTS
Maximum number of arguments for one command.
Definition cairo_gal.h:328
void drawPoly(const std::deque< VECTOR2D > &aPointList)
Drawing polygons & polylines is the same in Cairo, so here is the common code.
void DrawPolygon(const VECTOR2D aPointList[], int aListSize) override
Definition cairo_gal.h:128
std::map< int, GROUP > m_groups
List of graphic groups.
Definition cairo_gal.h:366
cairo_t * m_context
Cairo image.
Definition cairo_gal.h:377
void DrawPolylines(const std::vector< std::vector< VECTOR2D > > &aPointLists) override
Draw multiple polylines.
Definition cairo_gal.h:120
void DrawPolyline(const SHAPE_LINE_CHAIN &aLineChain) override
Definition cairo_gal.h:117
std::vector< cairo_matrix_t > m_xformStack
Definition cairo_gal.h:383
void DrawPolyline(const VECTOR2D aPointList[], int aListSize) override
Definition cairo_gal.h:112
void DrawPolygon(const std::deque< VECTOR2D > &aPointList) override
Draw a polygon.
Definition cairo_gal.h:127
cairo_matrix_t m_currentWorld2Screen
Definition cairo_gal.h:375
cairo_t * m_currentContext
Currently used Cairo context for drawing.
Definition cairo_gal.h:376
GRAPHICS_COMMAND
Definitions for the command recorder.
Definition cairo_gal.h:332
@ CMD_SET_STROKE
Enable/disable stroking.
Definition cairo_gal.h:334
@ CMD_SAVE
Save the transformation matrix.
Definition cairo_gal.h:344
@ CMD_SCALE
Scale the context.
Definition cairo_gal.h:343
@ CMD_SET_LINE_WIDTH
Set the line width.
Definition cairo_gal.h:337
@ CMD_SET_FILL
Enable/disable filling.
Definition cairo_gal.h:333
@ CMD_CALL_GROUP
Call a group.
Definition cairo_gal.h:346
@ CMD_ROTATE
Rotate the context.
Definition cairo_gal.h:341
@ CMD_STROKE_PATH
Set the stroke path.
Definition cairo_gal.h:338
@ CMD_TRANSLATE
Translate the context.
Definition cairo_gal.h:342
@ CMD_SET_FILLCOLOR
Set the fill color.
Definition cairo_gal.h:335
@ CMD_FILL_PATH
Set the fill path.
Definition cairo_gal.h:339
@ CMD_RESTORE
Restore the transformation matrix.
Definition cairo_gal.h:345
@ CMD_SET_STROKECOLOR
Set the stroke color.
Definition cairo_gal.h:336
unsigned int m_overlayBuffer
Handle to the overlay buffer.
Definition cairo_gal.h:519
unsigned int m_bufferSize
Size of buffers cairoOutput, bitmapBuffers.
Definition cairo_gal.h:529
CAIRO_GAL(GAL_DISPLAY_OPTIONS &aDisplayOptions, wxWindow *aParent, wxEvtHandler *aMouseListener=nullptr, wxEvtHandler *aPaintListener=nullptr, const wxString &aName=wxT("CairoCanvas"))
void SetPaintListener(wxEvtHandler *aPaintListener)
Definition cairo_gal.h:454
void SetMouseListener(wxEvtHandler *aMouseListener)
Definition cairo_gal.h:449
unsigned char * m_wxOutput
wxImage compatible buffer
Definition cairo_gal.h:530
bool m_isInitialized
Are Cairo image & surface ready to use.
Definition cairo_gal.h:536
bool m_validCompositor
Compositor initialization flag.
Definition cairo_gal.h:523
unsigned int m_mainBuffer
Handle to the main buffer.
Definition cairo_gal.h:518
int m_stride
Stride value for Cairo.
Definition cairo_gal.h:534
COLOR4D m_backgroundColor
Background color.
Definition cairo_gal.h:537
unsigned char * m_bitmapBuffer
Storage of the Cairo image.
Definition cairo_gal.h:533
wxEvtHandler * m_mouseListener
Mouse listener.
Definition cairo_gal.h:527
wxEvtHandler * m_paintListener
Paint listener.
Definition cairo_gal.h:528
bool IsVisible() const override
Return true if the GAL canvas is visible on the screen.
Definition cairo_gal.h:410
unsigned int m_tempBuffer
Handle to the temp buffer.
Definition cairo_gal.h:520
RENDER_TARGET m_currentTarget
Current rendering target.
Definition cairo_gal.h:522
wxWindow * m_parentWindow
Parent window.
Definition cairo_gal.h:526
std::shared_ptr< CAIRO_COMPOSITOR > m_compositor
Object for layers compositing.
Definition cairo_gal.h:517
WX_CURSOR_TYPE m_currentwxCursor
wx cursor showing the current native cursor
Definition cairo_gal.h:539
unsigned int m_savedBuffer
Handle to buffer to restore after rendering to temp buffer.
Definition cairo_gal.h:521
A color representation with 4 components: red, green, blue, alpha.
Definition color4d.h:105
GAL(GAL_DISPLAY_OPTIONS &aOptions)
Represent a polyline containing arcs as well as line segments: A chain of connected line and/or arc s...
Represent a set of closed polygons.
KICURSOR
Definition cursors.h:44
wxCursor WX_CURSOR_TYPE
Represents either a wxCursorBundle for wx 3.3+ or a wxCursor for older versions.
Definition cursors.h:39
#define GAL_API
Definition gal.h:27
MATRIX3x3< double > MATRIX3x3D
Definition matrix3x3.h:473
The Cairo implementation of the graphics abstraction layer.
Definition eda_group.h:33
RENDER_TARGET
RENDER_TARGET: Possible rendering targets.
Definition definitions.h:36
void DrawSegment(const ROUND_SEGMENT_2D &aSegment, unsigned int aNrSidesPerCircle)
Draw a thick line segment with rounded ends.
Type definition for an graphics group element.
Definition cairo_gal.h:351
GRAPHICS_COMMAND m_Command
Command to execute.
Definition cairo_gal.h:352
cairo_path_t * m_CairoPath
Pointer to a Cairo path.
Definition cairo_gal.h:358
double DblArg[MAX_CAIRO_ARGUMENTS]
Arguments for Cairo commands.
Definition cairo_gal.h:354
VECTOR2< double > VECTOR2D
Definition vector2d.h:686