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, see <https://www.gnu.org/licenses/>.
23 */
24
25#ifndef CAIROGAL_H_
26#define CAIROGAL_H_
27
28#include <map>
29#include <iterator>
30
31#include <cairo.h>
32
33#include <gal/gal.h>
35#include <wx/dcbuffer.h>
36
37#include <memory>
38
50namespace KIGFX
51{
53
55{
56public:
57 CAIRO_GAL_BASE( GAL_DISPLAY_OPTIONS& aDisplayOptions );
58
60
61 bool IsCairoEngine() override { return true; }
62
63 // ---------------
64 // Drawing methods
65 // ---------------
66
68 void DrawLine( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint ) override;
69
71 void DrawSegment( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint,
72 double aWidth ) override;
73
75 void DrawCircle( const VECTOR2D& aCenterPoint, double aRadius ) override;
76
78 void DrawHoleWall( const VECTOR2D& aCenterPoint, double aHoleRadius,
79 double aWallWidth ) 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 DrawEllipse( const VECTOR2D& aCenterPoint, double aMajorRadius, double aMinorRadius,
92 const EDA_ANGLE& aRotation ) override;
93
95 void DrawEllipseArc( const VECTOR2D& aCenterPoint, double aMajorRadius, double aMinorRadius,
96 const EDA_ANGLE& aRotation, const EDA_ANGLE& aStartAngle,
97 const EDA_ANGLE& aEndAngle ) override;
98
100 void DrawRectangle( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint ) override;
101
103 void DrawSegmentChain( const std::vector<VECTOR2D>& aPointList, double aWidth ) override;
104 void DrawSegmentChain( const SHAPE_LINE_CHAIN& aLineChain, double aWidth ) override;
105
107 void DrawPolyline( const std::deque<VECTOR2D>& aPointList ) override { drawPoly( aPointList ); }
108 void DrawPolyline( const VECTOR2D aPointList[], int aListSize ) override
109 {
110 drawPoly( aPointList, aListSize );
111 }
112
113 void DrawPolyline( const SHAPE_LINE_CHAIN& aLineChain ) override { drawPoly( aLineChain ); }
114
116 void DrawPolylines( const std::vector<std::vector<VECTOR2D>>& aPointLists ) override
117 {
118 for( const std::vector<VECTOR2D>& points : aPointLists )
119 drawPoly( points );
120 }
121
123 void DrawPolygon( const std::deque<VECTOR2D>& aPointList ) override { drawPoly( aPointList ); }
124 void DrawPolygon( const VECTOR2D aPointList[], int aListSize ) override
125 {
126 drawPoly( aPointList, aListSize );
127 }
128
129 void DrawPolygon( const SHAPE_POLY_SET& aPolySet, bool aStrokeTriangulation = false ) override;
130 void DrawPolygon( const SHAPE_LINE_CHAIN& aPolySet ) override;
131
133 void DrawGlyph( const KIFONT::GLYPH& aPolySet, int aNth, int aTotal ) override;
134
136 void DrawGlyphs( const std::vector<std::unique_ptr<KIFONT::GLYPH>>& aGlyphs ) override
137 {
138 for( size_t i = 0; i < aGlyphs.size(); i++ )
139 DrawGlyph( *aGlyphs[i], i, aGlyphs.size() );
140 }
141
143 void DrawCurve( const VECTOR2D& startPoint, const VECTOR2D& controlPointA,
144 const VECTOR2D& controlPointB, const VECTOR2D& endPoint,
145 double aFilterValue = 0.0 ) override;
146
148 void DrawBitmap( const BITMAP_BASE& aBitmap, double alphaBlend = 1.0 ) override;
149
150 // --------------
151 // Screen methods
152 // --------------
153
155 void ResizeScreen( int aWidth, int aHeight ) override;
156
158 void Flush() override;
159
161 void ClearScreen() override;
162
163 // -----------------
164 // Attribute setting
165 // -----------------
166
168 void SetIsFill( bool aIsFillEnabled ) override;
169
171 void SetIsStroke( bool aIsStrokeEnabled ) override;
172
174 void SetStrokeColor( const COLOR4D& aColor ) override;
175
177 void SetFillColor( const COLOR4D& aColor ) override;
178
180 void SetLineWidth( float aLineWidth ) override;
181
183 void SetLayerDepth( double aLayerDepth ) override;
184
185 // --------------
186 // Transformation
187 // --------------
188
190 void Transform( const MATRIX3x3D& aTransformation ) override;
191
193 void Rotate( double aAngle ) override;
194
196 void Translate( const VECTOR2D& aTranslation ) override;
197
199 void Scale( const VECTOR2D& aScale ) override;
200
202 void Save() override;
203
205 void Restore() override;
206
207 // --------------------------------------------
208 // Group methods
209 // ---------------------------------------------
210
212 int BeginGroup() override;
213
215 void EndGroup() override;
216
218 void DrawGroup( int aGroupNumber ) override;
219
221 void ChangeGroupColor( int aGroupNumber, const COLOR4D& aNewColor ) override;
222
224 void ChangeGroupDepth( int aGroupNumber, int aDepth ) override;
225
227 void DeleteGroup( int aGroupNumber ) override;
228
230 void ClearCache() override;
231
232 // --------------------------------------------------------
233 // Handling the world <-> screen transformation
234 // --------------------------------------------------------
235
237 void SetNegativeDrawMode( bool aSetting ) override;
238
239 // -------
240 // Cursor
241 // -------
242
244 void DrawCursor( const VECTOR2D& aCursorPosition ) override;
245
246 void EnableDepthTest( bool aEnabled = false ) override;
247
249 void DrawGrid() override;
250
252 void BeginDrawing() override;
253
255 void EndDrawing() override;
256
257
258protected:
259 // Geometric transforms according to the m_currentWorld2Screen transform matrix:
260 double xform( double x ); // scale
261 const VECTOR2D xform( double x, double y ); // rotation, scale and offset
262 const VECTOR2D xform( const VECTOR2D& aP ); // rotation, scale and offset
263
270 double angle_xform( double aAngle );
271
279 void arc_angles_xform_and_normalize( double& aStartAngle, double& aEndAngle );
280
281 void resetContext();
282
290 void drawGridLine( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint,
291 const COLOR4D& aColor );
292 void drawGridCross( const VECTOR2D& aPoint, const COLOR4D& aColor );
293 void drawGridPoint( const VECTOR2D& aPoint, double aWidth, double aHeight,
294 const COLOR4D& aColor );
295 void drawAxes( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint );
296
301 void drawGridSources();
302
307 void drawGridSource( const GRID_SOURCE& aSrc );
308
312 void drawGridCoverageShape( const GRID_SOURCE& aSrc );
313
314
315 void flushPath();
316 void storePath();
317
321 void blitCursor( wxMemoryDC& clientDC );
322
324 void drawPoly( const std::deque<VECTOR2D>& aPointList );
325 void drawPoly( const std::vector<VECTOR2D>& aPointList );
326 void drawPoly( const VECTOR2D aPointList[], int aListSize );
327 void drawPoly( const SHAPE_LINE_CHAIN& aLineChain );
328
334 unsigned int getNewGroupNumber();
335
336 void syncLineWidth( bool aForceWidth = false, double aWidth = 0.0 );
337 void updateWorldScreenMatrix();
338 const VECTOR2D roundp( const VECTOR2D& v );
339
341 typedef GAL super;
342
344 static const int MAX_CAIRO_ARGUMENTS = 4;
345
364
367 {
369 union {
371 bool BoolArg;
372 int IntArg = 0;
373 } m_Argument;
374 cairo_path_t* m_CairoPath = nullptr;
375 };
376
377 typedef std::deque<GROUP_ELEMENT> GROUP;
378
379 // Variables for the grouping function
382 std::map<int, GROUP> m_groups;
383 unsigned int m_groupCounter;
385
388
390 cairo_matrix_t m_currentXform;
391 cairo_matrix_t m_currentWorld2Screen;
393 cairo_t* m_context;
394 cairo_surface_t* m_surface;
395
397 std::vector<cairo_surface_t*> m_imageSurfaces;
398
399 std::vector<cairo_matrix_t> m_xformStack;
401 static constexpr cairo_format_t GAL_FORMAT = CAIRO_FORMAT_ARGB32;
402};
403
404
405class GAL_API CAIRO_GAL : public CAIRO_GAL_BASE, public wxWindow
406{
407public:
419 CAIRO_GAL( GAL_DISPLAY_OPTIONS& aDisplayOptions, wxWindow* aParent,
420 wxEvtHandler* aMouseListener = nullptr, wxEvtHandler* aPaintListener = nullptr,
421 const wxString& aName = wxT( "CairoCanvas" ) );
422
423 ~CAIRO_GAL();
424
426 bool IsVisible() const override
427 {
428 return IsShownOnScreen() && !GetClientRect().IsEmpty();
429 }
430
431 void ResizeScreen( int aWidth, int aHeight ) override;
432
441 void SetOverlayExclusions( const std::vector<wxRect>& aRects ) { m_overlayExclusions = aRects; }
442
443 bool Show( bool aShow ) override;
444
445 int BeginGroup() override;
446
447 void EndGroup() override;
448
449 void SetTarget( RENDER_TARGET aTarget ) override;
450
451 RENDER_TARGET GetTarget() const override;
452
453 void ClearTarget( RENDER_TARGET aTarget ) override;
454
456 void StartDiffLayer() override;
457
459 void EndDiffLayer() override;
460
462 void StartNegativesLayer() override;
463
465 void EndNegativesLayer() override;
466
473 void PostPaint( wxPaintEvent& aEvent );
474
475 void SetMouseListener( wxEvtHandler* aMouseListener )
476 {
477 m_mouseListener = aMouseListener;
478 }
479
480 void SetPaintListener( wxEvtHandler* aPaintListener )
481 {
482 m_paintListener = aPaintListener;
483 }
484
486 bool SetNativeCursorStyle( KICURSOR aCursor, bool aHiDPI ) override;
487
489 void BeginDrawing() override;
490
492 void EndDrawing() override;
493
495 void initSurface();
496
498 void deinitSurface();
499
501 void allocateBitmaps();
502
504 void deleteBitmaps();
505
507 void setCompositor();
508
509 // Event handlers
515 void onPaint( wxPaintEvent& aEvent );
516
522 void skipMouseEvent( wxMouseEvent& aEvent );
523
529 void skipGestureEvent( wxGestureEvent& aEvent );
530
536 void onSetNativeCursor( wxSetCursorEvent& aEvent );
537
539 bool updatedGalDisplayOptions( const GAL_DISPLAY_OPTIONS& aOptions ) override;
540
541protected:
542 // Compositor related variables
543 std::shared_ptr<CAIRO_COMPOSITOR> m_compositor;
544 unsigned int m_mainBuffer;
545 unsigned int m_overlayBuffer;
546 unsigned int m_tempBuffer;
547 unsigned int m_savedBuffer;
550
551 // Variables related to wxWidgets
552 wxWindow* m_parentWindow;
553 wxEvtHandler* m_mouseListener;
554 wxEvtHandler* m_paintListener;
555 unsigned int m_bufferSize;
556 unsigned char* m_wxOutput;
557
558 // Variables related to Cairo <-> wxWidgets
559 unsigned char* m_bitmapBuffer;
564
566
567 std::vector<wxRect> m_overlayExclusions;
568};
569
570} // namespace KIGFX
571
572#endif // CAIROGAL_H_
static double roundp(double x)
This class handle bitmap images in KiCad.
Definition bitmap_base.h:45
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:394
void DrawPolyline(const std::deque< VECTOR2D > &aPointList) override
Draw a polyline.
Definition cairo_gal.h:107
GAL super
Super class definition.
Definition cairo_gal.h:341
cairo_matrix_t m_cairoWorldScreenMatrix
Cairo world to screen transform matrix.
Definition cairo_gal.h:389
unsigned int m_groupCounter
Counter used for generating group keys.
Definition cairo_gal.h:383
bool m_isElementAdded
Was an graphic element added ?
Definition cairo_gal.h:381
std::deque< GROUP_ELEMENT > GROUP
A graphic group type definition.
Definition cairo_gal.h:377
bool IsCairoEngine() override
Return true if the GAL engine is a Cairo based type.
Definition cairo_gal.h:61
void DrawGlyphs(const std::vector< std::unique_ptr< KIFONT::GLYPH > > &aGlyphs) override
Draw polygons representing font glyphs.
Definition cairo_gal.h:136
CAIRO_GAL_BASE(GAL_DISPLAY_OPTIONS &aDisplayOptions)
Definition cairo_gal.cpp:49
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:397
GROUP * m_currentGroup
Currently used group.
Definition cairo_gal.h:384
bool m_isGrouping
Is grouping enabled ?
Definition cairo_gal.h:380
static constexpr cairo_format_t GAL_FORMAT
Format used to store pixels.
Definition cairo_gal.h:401
cairo_matrix_t m_currentXform
Definition cairo_gal.h:390
static const int MAX_CAIRO_ARGUMENTS
Maximum number of arguments for one command.
Definition cairo_gal.h:344
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:124
std::map< int, GROUP > m_groups
List of graphic groups.
Definition cairo_gal.h:382
cairo_t * m_context
Cairo image.
Definition cairo_gal.h:393
void DrawPolylines(const std::vector< std::vector< VECTOR2D > > &aPointLists) override
Draw multiple polylines.
Definition cairo_gal.h:116
void DrawPolyline(const SHAPE_LINE_CHAIN &aLineChain) override
Definition cairo_gal.h:113
std::vector< cairo_matrix_t > m_xformStack
Definition cairo_gal.h:399
void DrawPolyline(const VECTOR2D aPointList[], int aListSize) override
Definition cairo_gal.h:108
void DrawPolygon(const std::deque< VECTOR2D > &aPointList) override
Draw a polygon.
Definition cairo_gal.h:123
cairo_matrix_t m_currentWorld2Screen
Definition cairo_gal.h:391
cairo_t * m_currentContext
Currently used Cairo context for drawing.
Definition cairo_gal.h:392
GRAPHICS_COMMAND
Definitions for the command recorder.
Definition cairo_gal.h:348
@ CMD_SET_STROKE
Enable/disable stroking.
Definition cairo_gal.h:350
@ CMD_SAVE
Save the transformation matrix.
Definition cairo_gal.h:360
@ CMD_SCALE
Scale the context.
Definition cairo_gal.h:359
@ CMD_SET_LINE_WIDTH
Set the line width.
Definition cairo_gal.h:353
@ CMD_SET_FILL
Enable/disable filling.
Definition cairo_gal.h:349
@ CMD_CALL_GROUP
Call a group.
Definition cairo_gal.h:362
@ CMD_ROTATE
Rotate the context.
Definition cairo_gal.h:357
@ CMD_STROKE_PATH
Set the stroke path.
Definition cairo_gal.h:354
@ CMD_TRANSLATE
Translate the context.
Definition cairo_gal.h:358
@ CMD_SET_FILLCOLOR
Set the fill color.
Definition cairo_gal.h:351
@ CMD_FILL_PATH
Set the fill path.
Definition cairo_gal.h:355
@ CMD_RESTORE
Restore the transformation matrix.
Definition cairo_gal.h:361
@ CMD_SET_STROKECOLOR
Set the stroke color.
Definition cairo_gal.h:352
unsigned int m_overlayBuffer
Handle to the overlay buffer.
Definition cairo_gal.h:545
unsigned int m_bufferSize
Size of buffers cairoOutput, bitmapBuffers.
Definition cairo_gal.h:555
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:480
void SetMouseListener(wxEvtHandler *aMouseListener)
Definition cairo_gal.h:475
unsigned char * m_wxOutput
wxImage compatible buffer
Definition cairo_gal.h:556
bool m_isInitialized
Are Cairo image & surface ready to use.
Definition cairo_gal.h:562
bool m_validCompositor
Compositor initialization flag.
Definition cairo_gal.h:549
unsigned int m_mainBuffer
Handle to the main buffer.
Definition cairo_gal.h:544
int m_stride
Stride value for Cairo.
Definition cairo_gal.h:560
COLOR4D m_backgroundColor
Background color.
Definition cairo_gal.h:563
unsigned char * m_bitmapBuffer
Storage of the Cairo image.
Definition cairo_gal.h:559
wxEvtHandler * m_mouseListener
Mouse listener.
Definition cairo_gal.h:553
void SetOverlayExclusions(const std::vector< wxRect > &aRects)
Set areas, in canvas coordinates, that the frame blit must leave alone.
Definition cairo_gal.h:441
wxEvtHandler * m_paintListener
Paint listener.
Definition cairo_gal.h:554
bool IsVisible() const override
Return true if the GAL canvas is visible on the screen.
Definition cairo_gal.h:426
unsigned int m_tempBuffer
Handle to the temp buffer.
Definition cairo_gal.h:546
RENDER_TARGET m_currentTarget
Current rendering target.
Definition cairo_gal.h:548
wxWindow * m_parentWindow
Parent window.
Definition cairo_gal.h:552
std::shared_ptr< CAIRO_COMPOSITOR > m_compositor
Object for layers compositing.
Definition cairo_gal.h:543
WX_CURSOR_TYPE m_currentwxCursor
wx cursor showing the current native cursor
Definition cairo_gal.h:565
unsigned int m_savedBuffer
Handle to buffer to restore after rendering to temp buffer.
Definition cairo_gal.h:547
std::vector< wxRect > m_overlayExclusions
Areas the frame blit must leave alone.
Definition cairo_gal.h:567
A color representation with 4 components: red, green, blue, alpha.
Definition color4d.h:101
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:40
wxCursor WX_CURSOR_TYPE
Represents either a wxCursorBundle for wx 3.3+ or a wxCursor for older versions.
Definition cursors.h:35
#define GAL_API
Definition gal.h:27
MATRIX3x3< double > MATRIX3x3D
Definition matrix3x3.h:469
The Cairo implementation of the graphics abstraction layer.
Definition eda_group.h:30
RENDER_TARGET
RENDER_TARGET: Possible rendering targets.
Definition definitions.h:32
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:367
GRAPHICS_COMMAND m_Command
Command to execute.
Definition cairo_gal.h:368
cairo_path_t * m_CairoPath
Pointer to a Cairo path.
Definition cairo_gal.h:374
double DblArg[MAX_CAIRO_ARGUMENTS]
Arguments for Cairo commands.
Definition cairo_gal.h:370
Render-time projection of a grid: GRID_GEOMETRY (kind/origin/pitch/orientation/extent) plus rendering...
VECTOR2< double > VECTOR2D
Definition vector2d.h:682