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 (C) 2012-2024 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{
56class CAIRO_COMPOSITOR;
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 DrawArc( const VECTOR2D& aCenterPoint, double aRadius, const EDA_ANGLE& aStartAngle,
83 const EDA_ANGLE& aAngle ) override;
84
87 void DrawArcSegment( const VECTOR2D& aCenterPoint, double aRadius, const EDA_ANGLE& aStartAngle,
88 const EDA_ANGLE& aAngle, double aWidth, double aMaxError ) override;
89
91 void DrawRectangle( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint ) override;
92
94 void DrawSegmentChain( const std::vector<VECTOR2D>& aPointList, double aWidth ) override;
95 void DrawSegmentChain( const SHAPE_LINE_CHAIN& aLineChain, double aWidth ) override;
96
98 void DrawPolyline( const std::deque<VECTOR2D>& aPointList ) override { drawPoly( aPointList ); }
99 void DrawPolyline( const VECTOR2D aPointList[], int aListSize ) override
100 {
101 drawPoly( aPointList, aListSize );
102 }
103
104 void DrawPolyline( const SHAPE_LINE_CHAIN& aLineChain ) override { drawPoly( aLineChain ); }
105
107 void DrawPolylines( const std::vector<std::vector<VECTOR2D>>& aPointLists ) override
108 {
109 for( const std::vector<VECTOR2D>& points : aPointLists )
110 drawPoly( points );
111 }
112
114 void DrawPolygon( const std::deque<VECTOR2D>& aPointList ) override { drawPoly( aPointList ); }
115 void DrawPolygon( const VECTOR2D aPointList[], int aListSize ) override
116 {
117 drawPoly( aPointList, aListSize );
118 }
119
120 void DrawPolygon( const SHAPE_POLY_SET& aPolySet, bool aStrokeTriangulation = false ) override;
121 void DrawPolygon( const SHAPE_LINE_CHAIN& aPolySet ) override;
122
124 void DrawGlyph( const KIFONT::GLYPH& aPolySet, int aNth, int aTotal ) override;
125
127 void DrawGlyphs( const std::vector<std::unique_ptr<KIFONT::GLYPH>>& aGlyphs ) override
128 {
129 for( size_t i = 0; i < aGlyphs.size(); i++ )
130 DrawGlyph( *aGlyphs[i], i, aGlyphs.size() );
131 }
132
134 void DrawCurve( const VECTOR2D& startPoint, const VECTOR2D& controlPointA,
135 const VECTOR2D& controlPointB, const VECTOR2D& endPoint,
136 double aFilterValue = 0.0 ) override;
137
139 void DrawBitmap( const BITMAP_BASE& aBitmap, double alphaBlend = 1.0 ) override;
140
141 // --------------
142 // Screen methods
143 // --------------
144
146 void ResizeScreen( int aWidth, int aHeight ) override;
147
149 void Flush() override;
150
152 void ClearScreen() override;
153
154 // -----------------
155 // Attribute setting
156 // -----------------
157
159 void SetIsFill( bool aIsFillEnabled ) override;
160
162 void SetIsStroke( bool aIsStrokeEnabled ) override;
163
165 void SetStrokeColor( const COLOR4D& aColor ) override;
166
168 void SetFillColor( const COLOR4D& aColor ) override;
169
171 void SetLineWidth( float aLineWidth ) override;
172
174 void SetLayerDepth( double aLayerDepth ) override;
175
176 // --------------
177 // Transformation
178 // --------------
179
181 void Transform( const MATRIX3x3D& aTransformation ) override;
182
184 void Rotate( double aAngle ) override;
185
187 void Translate( const VECTOR2D& aTranslation ) override;
188
190 void Scale( const VECTOR2D& aScale ) override;
191
193 void Save() override;
194
196 void Restore() override;
197
198 // --------------------------------------------
199 // Group methods
200 // ---------------------------------------------
201
203 int BeginGroup() override;
204
206 void EndGroup() override;
207
209 void DrawGroup( int aGroupNumber ) override;
210
212 void ChangeGroupColor( int aGroupNumber, const COLOR4D& aNewColor ) override;
213
215 void ChangeGroupDepth( int aGroupNumber, int aDepth ) override;
216
218 void DeleteGroup( int aGroupNumber ) override;
219
221 void ClearCache() override;
222
223 // --------------------------------------------------------
224 // Handling the world <-> screen transformation
225 // --------------------------------------------------------
226
228 void SetNegativeDrawMode( bool aSetting ) override;
229
230 // -------
231 // Cursor
232 // -------
233
235 void DrawCursor( const VECTOR2D& aCursorPosition ) override;
236
237 void EnableDepthTest( bool aEnabled = false ) override;
238
240 void DrawGrid() override;
241
243 void BeginDrawing() override;
244
246 void EndDrawing() override;
247
248
249protected:
250 // Geometric transforms according to the m_currentWorld2Screen transform matrix:
251 double xform( double x ); // scale
252 const VECTOR2D xform( double x, double y ); // rotation, scale and offset
253 const VECTOR2D xform( const VECTOR2D& aP ); // rotation, scale and offset
254
255 // Return the scaling factor for current window.
256 virtual double getScalingFactor() { return 1.0; }
257
264 double angle_xform( double aAngle );
265
273 void arc_angles_xform_and_normalize( double& aStartAngle, double& aEndAngle );
274
275 void resetContext();
276
283 void drawGridLine( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint );
284 void drawGridCross( const VECTOR2D& aPoint );
285 void drawGridPoint( const VECTOR2D& aPoint, double aWidth, double aHeight );
286 void drawAxes( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint );
287
288
289 void flushPath();
290 void storePath();
291
295 void blitCursor( wxMemoryDC& clientDC );
296
298 void drawPoly( const std::deque<VECTOR2D>& aPointList );
299 void drawPoly( const std::vector<VECTOR2D>& aPointList );
300 void drawPoly( const VECTOR2D aPointList[], int aListSize );
301 void drawPoly( const SHAPE_LINE_CHAIN& aLineChain );
302
308 unsigned int getNewGroupNumber();
309
310 void syncLineWidth( bool aForceWidth = false, double aWidth = 0.0 );
311 void updateWorldScreenMatrix();
312 const VECTOR2D roundp( const VECTOR2D& v );
313
315 typedef GAL super;
316
318 static const int MAX_CAIRO_ARGUMENTS = 4;
319
322 {
330 //CMD_TRANSFORM, ///< Transform the actual context
336 CMD_CALL_GROUP
337 };
338
341 {
343 union {
344 double DblArg[MAX_CAIRO_ARGUMENTS];
345 bool BoolArg;
346 int IntArg = 0;
347 } m_Argument;
348 cairo_path_t* m_CairoPath = nullptr;
349 };
350
351 typedef std::deque<GROUP_ELEMENT> GROUP;
352
353 // Variables for the grouping function
356 std::map<int, GROUP> m_groups;
357 unsigned int m_groupCounter;
359
362
364 cairo_matrix_t m_currentXform;
365 cairo_matrix_t m_currentWorld2Screen;
367 cairo_t* m_context;
368 cairo_surface_t* m_surface;
369
371 std::vector<cairo_surface_t*> m_imageSurfaces;
372
373 std::vector<cairo_matrix_t> m_xformStack;
375 static constexpr cairo_format_t GAL_FORMAT = CAIRO_FORMAT_ARGB32;
376};
377
378
379class GAL_API CAIRO_GAL : public CAIRO_GAL_BASE, public wxWindow
380{
381public:
393 CAIRO_GAL( GAL_DISPLAY_OPTIONS& aDisplayOptions, wxWindow* aParent,
394 wxEvtHandler* aMouseListener = nullptr, wxEvtHandler* aPaintListener = nullptr,
395 const wxString& aName = wxT( "CairoCanvas" ) );
396
397 ~CAIRO_GAL();
398
400 bool IsVisible() const override
401 {
402 return IsShownOnScreen() && !GetClientRect().IsEmpty();
403 }
404
405 void ResizeScreen( int aWidth, int aHeight ) override;
406
407 bool Show( bool aShow ) override;
408
409 int BeginGroup() override;
410
411 void EndGroup() override;
412
413 void SetTarget( RENDER_TARGET aTarget ) override;
414
415 RENDER_TARGET GetTarget() const override;
416
417 void ClearTarget( RENDER_TARGET aTarget ) override;
418
420 void StartDiffLayer() override;
421
423 void EndDiffLayer() override;
424
426 void StartNegativesLayer() override;
427
429 void EndNegativesLayer() override;
430
437 void PostPaint( wxPaintEvent& aEvent );
438
439 void SetMouseListener( wxEvtHandler* aMouseListener )
440 {
441 m_mouseListener = aMouseListener;
442 }
443
444 void SetPaintListener( wxEvtHandler* aPaintListener )
445 {
446 m_paintListener = aPaintListener;
447 }
448
450 bool SetNativeCursorStyle( KICURSOR aCursor, bool aHiDPI ) override;
451
453 void BeginDrawing() override;
454
456 void EndDrawing() override;
457
458protected:
459
461 void initSurface();
462
464 void deinitSurface();
465
467 void allocateBitmaps();
468
470 void deleteBitmaps();
471
473 void setCompositor();
474
475 // Event handlers
481 void onPaint( wxPaintEvent& aEvent );
482
488 void skipMouseEvent( wxMouseEvent& aEvent );
489
495 void skipGestureEvent( wxGestureEvent& aEvent );
496
502 void onSetNativeCursor( wxSetCursorEvent& aEvent );
503
505 bool updatedGalDisplayOptions( const GAL_DISPLAY_OPTIONS& aOptions ) override;
506
508 double getScalingFactor() override;
509
510protected:
511 // Compositor related variables
512 std::shared_ptr<CAIRO_COMPOSITOR> m_compositor;
513 unsigned int m_mainBuffer;
514 unsigned int m_overlayBuffer;
515 unsigned int m_tempBuffer;
516 unsigned int m_savedBuffer;
519
520 // Variables related to wxWidgets
521 wxWindow* m_parentWindow;
522 wxEvtHandler* m_mouseListener;
523 wxEvtHandler* m_paintListener;
524 unsigned int m_bufferSize;
525 wxBitmap* m_wxBitmap;
526
527 // Variables related to Cairo <-> wxWidgets
528 unsigned char* m_bitmapBuffer;
534};
535
536} // namespace KIGFX
537
538#endif // CAIROGAL_H_
static double roundp(double x)
Definition: cairo_gal.cpp:186
This class handle bitmap images in KiCad.
Definition: bitmap_base.h:48
cairo_surface_t * m_surface
Cairo surface.
Definition: cairo_gal.h:368
void DrawPolyline(const std::deque< VECTOR2D > &aPointList) override
Draw a polyline.
Definition: cairo_gal.h:98
GAL super
Super class definition.
Definition: cairo_gal.h:315
cairo_matrix_t m_cairoWorldScreenMatrix
Cairo world to screen transform matrix.
Definition: cairo_gal.h:363
unsigned int m_groupCounter
Counter used for generating group keys.
Definition: cairo_gal.h:357
virtual double getScalingFactor()
Definition: cairo_gal.h:256
bool m_isElementAdded
Was an graphic element added ?
Definition: cairo_gal.h:355
std::deque< GROUP_ELEMENT > GROUP
A graphic group type definition.
Definition: cairo_gal.h:351
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:127
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:371
GROUP * m_currentGroup
Currently used group.
Definition: cairo_gal.h:358
bool m_isGrouping
Is grouping enabled ?
Definition: cairo_gal.h:354
cairo_matrix_t m_currentXform
Definition: cairo_gal.h:364
void DrawPolygon(const VECTOR2D aPointList[], int aListSize) override
Definition: cairo_gal.h:115
std::map< int, GROUP > m_groups
List of graphic groups.
Definition: cairo_gal.h:356
cairo_t * m_context
Cairo image.
Definition: cairo_gal.h:367
void DrawPolylines(const std::vector< std::vector< VECTOR2D > > &aPointLists) override
Draw multiple polylines.
Definition: cairo_gal.h:107
void DrawPolyline(const SHAPE_LINE_CHAIN &aLineChain) override
Definition: cairo_gal.h:104
std::vector< cairo_matrix_t > m_xformStack
Definition: cairo_gal.h:373
void DrawPolyline(const VECTOR2D aPointList[], int aListSize) override
Definition: cairo_gal.h:99
void DrawPolygon(const std::deque< VECTOR2D > &aPointList) override
Draw a polygon.
Definition: cairo_gal.h:114
cairo_matrix_t m_currentWorld2Screen
Definition: cairo_gal.h:365
cairo_t * m_currentContext
Currently used Cairo context for drawing.
Definition: cairo_gal.h:366
GRAPHICS_COMMAND
Definitions for the command recorder.
Definition: cairo_gal.h:322
@ CMD_SET_STROKE
Enable/disable stroking.
Definition: cairo_gal.h:324
@ CMD_SAVE
Save the transformation matrix.
Definition: cairo_gal.h:334
@ CMD_SCALE
Scale the context.
Definition: cairo_gal.h:333
@ CMD_SET_LINE_WIDTH
Set the line width.
Definition: cairo_gal.h:327
@ CMD_SET_FILL
Enable/disable filling.
Definition: cairo_gal.h:323
@ CMD_ROTATE
Rotate the context.
Definition: cairo_gal.h:331
@ CMD_STROKE_PATH
Set the stroke path.
Definition: cairo_gal.h:328
@ CMD_TRANSLATE
Translate the context.
Definition: cairo_gal.h:332
@ CMD_SET_FILLCOLOR
Set the fill color.
Definition: cairo_gal.h:325
@ CMD_FILL_PATH
Set the fill path.
Definition: cairo_gal.h:329
@ CMD_RESTORE
Restore the transformation matrix.
Definition: cairo_gal.h:335
@ CMD_SET_STROKECOLOR
Set the stroke color.
Definition: cairo_gal.h:326
double m_lineWidthInPixels
Definition: cairo_gal.h:360
wxCursor m_currentwxCursor
wxCursor showing the current native cursor
Definition: cairo_gal.h:533
unsigned int m_overlayBuffer
Handle to the overlay buffer.
Definition: cairo_gal.h:514
unsigned int m_bufferSize
Size of buffers cairoOutput, bitmapBuffers.
Definition: cairo_gal.h:524
void SetPaintListener(wxEvtHandler *aPaintListener)
Definition: cairo_gal.h:444
void SetMouseListener(wxEvtHandler *aMouseListener)
Definition: cairo_gal.h:439
bool m_isInitialized
Are Cairo image & surface ready to use.
Definition: cairo_gal.h:531
bool m_validCompositor
Compositor initialization flag.
Definition: cairo_gal.h:518
unsigned int m_mainBuffer
Handle to the main buffer.
Definition: cairo_gal.h:513
int m_stride
Stride value for Cairo.
Definition: cairo_gal.h:529
COLOR4D m_backgroundColor
Background color.
Definition: cairo_gal.h:532
unsigned char * m_bitmapBuffer
Storage of the Cairo image.
Definition: cairo_gal.h:528
wxEvtHandler * m_mouseListener
Mouse listener.
Definition: cairo_gal.h:522
wxEvtHandler * m_paintListener
Paint listener.
Definition: cairo_gal.h:523
bool IsVisible() const override
Return true if the GAL canvas is visible on the screen.
Definition: cairo_gal.h:400
unsigned int m_tempBuffer
Handle to the temp buffer.
Definition: cairo_gal.h:515
RENDER_TARGET m_currentTarget
Current rendering target.
Definition: cairo_gal.h:517
wxWindow * m_parentWindow
Parent window.
Definition: cairo_gal.h:521
wxBitmap * m_wxBitmap
Output buffer bitmap.
Definition: cairo_gal.h:525
std::shared_ptr< CAIRO_COMPOSITOR > m_compositor
Object for layers compositing.
Definition: cairo_gal.h:512
unsigned int m_savedBuffer
Handle to buffer to restore after rendering to temp buffer.
Definition: cairo_gal.h:516
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:104
Abstract interface for drawing on a 2D-surface.
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:34
#define GAL_API
Definition: gal.h:28
The Cairo implementation of the graphics abstraction layer.
Definition: color4d.cpp:247
RENDER_TARGET
RENDER_TARGET: Possible rendering targets.
Definition: definitions.h:47
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:341
GRAPHICS_COMMAND m_Command
Command to execute.
Definition: cairo_gal.h:342
bool BoolArg
A bool argument.
Definition: cairo_gal.h:345