KiCad PCB EDA Suite
cairo_gal.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-2021 Kicad Developers, see AUTHORS.txt for contributors.
6 * Copyright (C) 2017-2018 CERN
7 *
8 * @author Maciej Suminski <[email protected]>
9 *
10 * CairoGal - Graphics Abstraction Layer for Cairo
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, you may find one here:
24 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
25 * or you may search the http://www.gnu.org website for the version 2 license,
26 * or you may write to the Free Software Foundation, Inc.,
27 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
28 */
29
30#include <wx/image.h>
31#include <wx/log.h>
32
33#include <gal/cairo/cairo_gal.h>
35#include <gal/definitions.h>
37#include <math/vector2wx.h>
38#include <math/util.h> // for KiROUND
39#include <trigo.h>
40#include <bitmap_base.h>
41
42#include <algorithm>
43#include <cmath>
44#include <limits>
45
46#include <pixman.h>
47
48using namespace KIGFX;
49
50
51CAIRO_GAL_BASE::CAIRO_GAL_BASE( GAL_DISPLAY_OPTIONS& aDisplayOptions ) : GAL( aDisplayOptions )
52{
53 // Initialise grouping
54 m_isGrouping = false;
55 m_isElementAdded = false;
57 m_currentGroup = nullptr;
58
59 m_lineWidth = 1.0;
61 m_lineWidthIsOdd = true;
62
63 // Initialise Cairo state
64 cairo_matrix_init_identity( &m_cairoWorldScreenMatrix );
65 m_currentContext = nullptr;
66 m_context = nullptr;
67 m_surface = nullptr;
68
69 // Grid color settings are different in Cairo and OpenGL
70 SetGridColor( COLOR4D( 0.1, 0.1, 0.1, 0.8 ) );
72
73 // Avoid uninitialized variables:
74 cairo_matrix_init_identity( &m_currentXform );
75 cairo_matrix_init_identity( &m_currentWorld2Screen );
76}
77
78
80{
81 ClearCache();
82
83 if( m_surface )
84 cairo_surface_destroy( m_surface );
85
86 if( m_context )
87 cairo_destroy( m_context );
88
89 for( _cairo_surface* imageSurface : m_imageSurfaces )
90 cairo_surface_destroy( imageSurface );
91}
92
93
95{
97}
98
99
101{
102 // Force remaining objects to be drawn
103 Flush();
104}
105
106
108{
110}
111
112
113const VECTOR2D CAIRO_GAL_BASE::xform( double x, double y )
114{
115 VECTOR2D rv;
116
119 return rv;
120}
121
122
124{
125 return xform( aP.x, aP.y );
126}
127
128
129const double CAIRO_GAL_BASE::angle_xform( const double aAngle )
130{
131 // calculate rotation angle due to the rotation transform
132 // and if flipped on X axis.
133 double world_rotation = -std::atan2( m_currentWorld2Screen.xy, m_currentWorld2Screen.xx );
134
135 // When flipped on X axis, the rotation angle is M_PI - initial angle:
136 if( IsFlippedX() )
137 world_rotation = M_PI - world_rotation;
138
139 return std::fmod( aAngle + world_rotation, 2.0 * M_PI );
140}
141
142
143void CAIRO_GAL_BASE::arc_angles_xform_and_normalize( double& aStartAngle, double& aEndAngle )
144{
145 // 360 deg arcs have a specific calculation.
146 bool is_360deg_arc = std::abs( aEndAngle - aStartAngle ) >= 2 * M_PI;
147 double startAngle = aStartAngle;
148 double endAngle = aEndAngle;
149
150 // When the view is flipped, the coordinates are flipped by the matrix transform
151 // However, arc angles need to be "flipped": the flipped angle is M_PI - initial angle.
152 if( IsFlippedX() )
153 {
154 startAngle = M_PI - startAngle;
155 endAngle = M_PI - endAngle;
156 }
157
158 // Normalize arc angles
159 SWAP( startAngle, >, endAngle );
160
161 // now rotate arc according to the rotation transform matrix
162 // Remark:
163 // We call angle_xform() to calculate angles according to the flip/rotation
164 // transform and normalize between -2M_PI and +2M_PI.
165 // Therefore, if aStartAngle = aEndAngle + 2*n*M_PI, the transform gives
166 // aEndAngle = aStartAngle
167 // So, if this is the case, force the aEndAngle value to draw a circle.
168 aStartAngle = angle_xform( startAngle );
169
170 if( is_360deg_arc ) // arc is a full circle
171 aEndAngle = aStartAngle + 2 * M_PI;
172 else
173 aEndAngle = angle_xform( endAngle );
174}
175
176
177const double CAIRO_GAL_BASE::xform( double x )
178{
179 double dx = m_currentWorld2Screen.xx * x;
180 double dy = m_currentWorld2Screen.yx * x;
181 return sqrt( dx * dx + dy * dy );
182}
183
184
185static double roundp( double x )
186{
187 return floor( x + 0.5 ) + 0.5;
188}
189
190
192{
194 return VECTOR2D( ::roundp( v.x ), ::roundp( v.y ) );
195 else
196 return VECTOR2D( floor( v.x + 0.5 ), floor( v.y + 0.5 ) );
197}
198
199
200void CAIRO_GAL_BASE::DrawLine( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint )
201{
203
204 VECTOR2D p0 = roundp( xform( aStartPoint ) );
205 VECTOR2D p1 = roundp( xform( aEndPoint ) );
206
207 cairo_move_to( m_currentContext, p0.x, p0.y );
208 cairo_line_to( m_currentContext, p1.x, p1.y );
209 flushPath();
210 m_isElementAdded = true;
211}
212
213
214void CAIRO_GAL_BASE::syncLineWidth( bool aForceWidth, double aWidth )
215{
216 double w = floor( xform( aForceWidth ? aWidth : m_lineWidth ) + 0.5 );
217
218 if( w <= 1.0 )
219 {
220 w = 1.0;
221 cairo_set_line_join( m_currentContext, CAIRO_LINE_JOIN_MITER );
222 cairo_set_line_cap( m_currentContext, CAIRO_LINE_CAP_BUTT );
223 cairo_set_line_width( m_currentContext, 1.0 );
224 m_lineWidthIsOdd = true;
225 }
226 else
227 {
228 cairo_set_line_join( m_currentContext, CAIRO_LINE_JOIN_ROUND );
229 cairo_set_line_cap( m_currentContext, CAIRO_LINE_CAP_ROUND );
230 cairo_set_line_width( m_currentContext, w );
231 m_lineWidthIsOdd = ( (int) w % 2 ) == 1;
232 }
233
235}
236
237
238void CAIRO_GAL_BASE::DrawSegmentChain( const std::vector<VECTOR2D>& aPointList, double aWidth )
239{
240 for( size_t i = 0; i + 1 < aPointList.size(); ++i )
241 DrawSegment( aPointList[i], aPointList[i + 1], aWidth );
242}
243
244
245void CAIRO_GAL_BASE::DrawSegmentChain( const SHAPE_LINE_CHAIN& aLineChain, double aWidth )
246{
247 int numPoints = aLineChain.PointCount();
248
249 if( aLineChain.IsClosed() )
250 numPoints += 1;
251
252 for( int i = 0; i + 1 < numPoints; ++i )
253 DrawSegment( aLineChain.CPoint( i ), aLineChain.CPoint( i + 1 ), aWidth );
254}
255
256
257void CAIRO_GAL_BASE::DrawSegment( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint,
258 double aWidth )
259{
260 if( m_isFillEnabled )
261 {
262 syncLineWidth( true, aWidth );
263
264 VECTOR2D p0 = roundp( xform( aStartPoint ) );
265 VECTOR2D p1 = roundp( xform( aEndPoint ) );
266
267 cairo_move_to( m_currentContext, p0.x, p0.y );
268 cairo_line_to( m_currentContext, p1.x, p1.y );
269 cairo_set_source_rgba( m_currentContext, m_fillColor.r, m_fillColor.g, m_fillColor.b,
270 m_fillColor.a );
271 cairo_stroke( m_currentContext );
272 }
273 else
274 {
275 aWidth /= 2.0;
276 SetLineWidth( 1.0 );
278
279 // Outline mode for tracks
280 VECTOR2D startEndVector = aEndPoint - aStartPoint;
281 double lineAngle = atan2( startEndVector.y, startEndVector.x );
282
283 double sa = sin( lineAngle + M_PI / 2.0 );
284 double ca = cos( lineAngle + M_PI / 2.0 );
285
286 VECTOR2D pa0 = xform( aStartPoint + VECTOR2D( aWidth * ca, aWidth * sa ) );
287 VECTOR2D pa1 = xform( aStartPoint - VECTOR2D( aWidth * ca, aWidth * sa ) );
288 VECTOR2D pb0 = xform( aEndPoint + VECTOR2D( aWidth * ca, aWidth * sa ) );
289 VECTOR2D pb1 = xform( aEndPoint - VECTOR2D( aWidth * ca, aWidth * sa ) );
290
293
294 cairo_move_to( m_currentContext, pa0.x, pa0.y );
295 cairo_line_to( m_currentContext, pb0.x, pb0.y );
296
297 cairo_move_to( m_currentContext, pa1.x, pa1.y );
298 cairo_line_to( m_currentContext, pb1.x, pb1.y );
299 flushPath();
300
301 // Calculate the segment angle and arc center in normal/mirrored transform for rounded ends.
302 VECTOR2D center_a = xform( aStartPoint );
303 VECTOR2D center_b = xform( aEndPoint );
304 startEndVector = center_b - center_a;
305 lineAngle = atan2( startEndVector.y, startEndVector.x );
306 double radius = ( pa0 - center_a ).EuclideanNorm();
307
308 // Draw the rounded end point of the segment
309 double arcStartAngle = lineAngle - M_PI / 2.0;
310 cairo_arc( m_currentContext, center_b.x, center_b.y, radius, arcStartAngle,
311 arcStartAngle + M_PI );
312
313 // Draw the rounded start point of the segment
314 arcStartAngle = lineAngle + M_PI / 2.0;
315 cairo_arc( m_currentContext, center_a.x, center_a.y, radius, arcStartAngle,
316 arcStartAngle + M_PI );
317
318 flushPath();
319 }
320
321 m_isElementAdded = true;
322}
323
324
325void CAIRO_GAL_BASE::DrawCircle( const VECTOR2D& aCenterPoint, double aRadius )
326{
328
329 VECTOR2D c = roundp( xform( aCenterPoint ) );
330 double r = ::roundp( xform( aRadius ) );
331
332 cairo_set_line_width( m_currentContext, std::min( 2.0 * r, m_lineWidthInPixels ) );
333 cairo_new_sub_path( m_currentContext );
334 cairo_arc( m_currentContext, c.x, c.y, r, 0.0, 2 * M_PI );
335 cairo_close_path( m_currentContext );
336 flushPath();
337 m_isElementAdded = true;
338}
339
340
341void CAIRO_GAL_BASE::DrawArc( const VECTOR2D& aCenterPoint, double aRadius,
342 const EDA_ANGLE& aStartAngle, const EDA_ANGLE& aEndAngle )
343{
345
346 double startAngle = aStartAngle.AsRadians();
347 double endAngle = aEndAngle.AsRadians();
348
349 // calculate start and end arc angles according to the rotation transform matrix
350 // and normalize:
351 arc_angles_xform_and_normalize( startAngle, endAngle );
352
353 double r = xform( aRadius );
354
355 // N.B. This is backwards. We set this because we want to adjust the center
356 // point that changes both endpoints. In the worst case, this is twice as far.
357 // We cannot adjust radius or center based on the other because this causes the
358 // whole arc to change position/size
359 m_lineWidthIsOdd = !( static_cast<int>( aRadius ) % 2 );
360
361 auto mid = roundp( xform( aCenterPoint ) );
362
363 cairo_set_line_width( m_currentContext, m_lineWidthInPixels );
364 cairo_new_sub_path( m_currentContext );
365
366 if( m_isFillEnabled )
367 cairo_move_to( m_currentContext, mid.x, mid.y );
368
369 cairo_arc( m_currentContext, mid.x, mid.y, r, startAngle, endAngle );
370
371 if( m_isFillEnabled )
372 cairo_close_path( m_currentContext );
373
374 flushPath();
375
376 m_isElementAdded = true;
377}
378
379
380void CAIRO_GAL_BASE::DrawArcSegment( const VECTOR2D& aCenterPoint, double aRadius,
381 const EDA_ANGLE& aStartAngle, const EDA_ANGLE& aEndAngle,
382 double aWidth, double aMaxError )
383{
384 // Note: aMaxError is not used because Cairo can draw true arcs
385 if( m_isFillEnabled )
386 {
387 m_lineWidth = aWidth;
388 m_isStrokeEnabled = true;
389 m_isFillEnabled = false;
390 DrawArc( aCenterPoint, aRadius, aStartAngle, aEndAngle );
391 m_isFillEnabled = true;
392 m_isStrokeEnabled = false;
393 return;
394 }
395
397
398 // calculate start and end arc angles according to the rotation transform matrix
399 // and normalize:
400 double startAngleS = aStartAngle.AsRadians();
401 double endAngleS = aEndAngle.AsRadians();
402 arc_angles_xform_and_normalize( startAngleS, endAngleS );
403
404 double r = xform( aRadius );
405
406 // N.B. This is backwards. We set this because we want to adjust the center
407 // point that changes both endpoints. In the worst case, this is twice as far.
408 // We cannot adjust radius or center based on the other because this causes the
409 // whole arc to change position/size
410 m_lineWidthIsOdd = !( static_cast<int>( aRadius ) % 2 );
411
412 VECTOR2D mid = roundp( xform( aCenterPoint ) );
413 double width = xform( aWidth / 2.0 );
414 VECTOR2D startPointS = VECTOR2D( r, 0.0 );
415 VECTOR2D endPointS = VECTOR2D( r, 0.0 );
416 RotatePoint( startPointS, -EDA_ANGLE( startAngleS, RADIANS_T ) );
417 RotatePoint( endPointS, -EDA_ANGLE( endAngleS, RADIANS_T ) );
418
419 cairo_save( m_currentContext );
420
423
424 cairo_translate( m_currentContext, mid.x, mid.y );
425
426 cairo_new_sub_path( m_currentContext );
427 cairo_arc( m_currentContext, 0, 0, r - width, startAngleS, endAngleS );
428
429 cairo_new_sub_path( m_currentContext );
430 cairo_arc( m_currentContext, 0, 0, r + width, startAngleS, endAngleS );
431
432 cairo_new_sub_path( m_currentContext );
433 cairo_arc_negative( m_currentContext, startPointS.x, startPointS.y, width, startAngleS,
434 startAngleS + M_PI );
435
436 cairo_new_sub_path( m_currentContext );
437 cairo_arc( m_currentContext, endPointS.x, endPointS.y, width, endAngleS, endAngleS + M_PI );
438
439 cairo_restore( m_currentContext );
440 flushPath();
441
442 m_isElementAdded = true;
443}
444
445
446void CAIRO_GAL_BASE::DrawRectangle( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint )
447{
448 // Calculate the diagonal points
450
451 const VECTOR2D p0 = roundp( xform( aStartPoint ) );
452 const VECTOR2D p1 = roundp( xform( VECTOR2D( aEndPoint.x, aStartPoint.y ) ) );
453 const VECTOR2D p2 = roundp( xform( aEndPoint ) );
454 const VECTOR2D p3 = roundp( xform( VECTOR2D( aStartPoint.x, aEndPoint.y ) ) );
455
456 // The path is composed from 4 segments
457 cairo_move_to( m_currentContext, p0.x, p0.y );
458 cairo_line_to( m_currentContext, p1.x, p1.y );
459 cairo_line_to( m_currentContext, p2.x, p2.y );
460 cairo_line_to( m_currentContext, p3.x, p3.y );
461 cairo_close_path( m_currentContext );
462 flushPath();
463
464 m_isElementAdded = true;
465}
466
467
468void CAIRO_GAL_BASE::DrawPolygon( const SHAPE_POLY_SET& aPolySet, bool aStrokeTriangulation )
469{
470 for( int i = 0; i < aPolySet.OutlineCount(); ++i )
471 drawPoly( aPolySet.COutline( i ) );
472}
473
474
476{
477 drawPoly( aPolygon );
478}
479
480
481void CAIRO_GAL_BASE::DrawCurve( const VECTOR2D& aStartPoint, const VECTOR2D& aControlPointA,
482 const VECTOR2D& aControlPointB, const VECTOR2D& aEndPoint,
483 double aFilterValue )
484{
485 // Note: aFilterValue is not used because the cubic Bezier curve is
486 // supported by Cairo.
488
489 const VECTOR2D sp = roundp( xform( aStartPoint ) );
490 const VECTOR2D cpa = roundp( xform( aControlPointA ) );
491 const VECTOR2D cpb = roundp( xform( aControlPointB ) );
492 const VECTOR2D ep = roundp( xform( aEndPoint ) );
493
494 cairo_move_to( m_currentContext, sp.x, sp.y );
495 cairo_curve_to( m_currentContext, cpa.x, cpa.y, cpb.x, cpb.y, ep.x, ep.y );
496 cairo_line_to( m_currentContext, ep.x, ep.y );
497
498 flushPath();
499 m_isElementAdded = true;
500}
501
502
503void CAIRO_GAL_BASE::DrawBitmap( const BITMAP_BASE& aBitmap, double alphaBlend )
504{
505 cairo_save( m_currentContext );
506
507 alphaBlend = std::clamp( alphaBlend, 0.0, 1.0 );
508
509 // We have to calculate the pixel size in users units to draw the image.
510 // m_worldUnitLength is a factor used for converting IU to inches
511 double scale = 1.0 / ( aBitmap.GetPPI() * m_worldUnitLength );
512
513 // The position of the bitmap is the bitmap center.
514 // move the draw origin to the top left bitmap corner:
515 int w = aBitmap.GetSizePixels().x;
516 int h = aBitmap.GetSizePixels().y;
517
518 cairo_set_matrix( m_currentContext, &m_currentWorld2Screen );
519 cairo_scale( m_currentContext, scale, scale );
520 cairo_translate( m_currentContext, -w / 2.0, -h / 2.0 );
521
522 cairo_new_path( m_currentContext );
523 cairo_surface_t* image = cairo_image_surface_create( CAIRO_FORMAT_ARGB32, w, h );
524 cairo_surface_flush( image );
525
526 unsigned char* pix_buffer = cairo_image_surface_get_data( image );
527
528 // The pixel buffer of the initial bitmap:
529 const wxImage& bm_pix_buffer = *aBitmap.GetImageData();
530
531 uint32_t mask_color = ( bm_pix_buffer.GetMaskRed() << 16 )
532 + ( bm_pix_buffer.GetMaskGreen() << 8 ) + ( bm_pix_buffer.GetMaskBlue() );
533
534 // Copy the source bitmap to the cairo bitmap buffer.
535 // In cairo bitmap buffer, a ARGB32 bitmap is an ARGB pixel packed into a uint_32
536 // 24 low bits only are used for color, top 8 are transparency.
537 for( int row = 0; row < h; row++ )
538 {
539 for( int col = 0; col < w; col++ )
540 {
541 // Build the RGB24 pixel:
542 uint32_t pixel = bm_pix_buffer.GetRed( col, row ) << 16;
543 pixel += bm_pix_buffer.GetGreen( col, row ) << 8;
544 pixel += bm_pix_buffer.GetBlue( col, row );
545
546 if( bm_pix_buffer.HasAlpha() )
547 pixel += bm_pix_buffer.GetAlpha( col, row ) << 24;
548 else if( bm_pix_buffer.HasMask() && pixel == mask_color )
549 pixel += ( wxALPHA_TRANSPARENT << 24 );
550 else
551 pixel += ( wxALPHA_OPAQUE << 24 );
552
553 // Write the pixel to the cairo image buffer:
554 uint32_t* pix_ptr = (uint32_t*) pix_buffer;
555 *pix_ptr = pixel;
556 pix_buffer += 4;
557 }
558 }
559
560 cairo_surface_mark_dirty( image );
561 cairo_set_source_surface( m_currentContext, image, 0, 0 );
562 cairo_paint_with_alpha( m_currentContext, alphaBlend );
563
564 // store the image handle so it can be destroyed later
565 m_imageSurfaces.push_back( image );
566
567 m_isElementAdded = true;
568
569 cairo_restore( m_currentContext );
570}
571
572
573void CAIRO_GAL_BASE::ResizeScreen( int aWidth, int aHeight )
574{
575 m_screenSize = VECTOR2I( aWidth, aHeight );
576}
577
578
580{
581 storePath();
582}
583
584
586{
587 cairo_set_source_rgb( m_currentContext, m_clearColor.r, m_clearColor.g, m_clearColor.b );
588 cairo_rectangle( m_currentContext, 0.0, 0.0, m_screenSize.x, m_screenSize.y );
589 cairo_fill( m_currentContext );
590}
591
592
593void CAIRO_GAL_BASE::SetIsFill( bool aIsFillEnabled )
594{
595 storePath();
596 m_isFillEnabled = aIsFillEnabled;
597
598 if( m_isGrouping )
599 {
600 GROUP_ELEMENT groupElement;
601 groupElement.m_Command = CMD_SET_FILL;
602 groupElement.m_Argument.BoolArg = aIsFillEnabled;
603 m_currentGroup->push_back( groupElement );
604 }
605}
606
607
608void CAIRO_GAL_BASE::SetIsStroke( bool aIsStrokeEnabled )
609{
610 storePath();
611 m_isStrokeEnabled = aIsStrokeEnabled;
612
613 if( m_isGrouping )
614 {
615 GROUP_ELEMENT groupElement;
616 groupElement.m_Command = CMD_SET_STROKE;
617 groupElement.m_Argument.BoolArg = aIsStrokeEnabled;
618 m_currentGroup->push_back( groupElement );
619 }
620}
621
622
624{
625 storePath();
626 m_strokeColor = aColor;
627
628 if( m_isGrouping )
629 {
630 GROUP_ELEMENT groupElement;
631 groupElement.m_Command = CMD_SET_STROKECOLOR;
632 groupElement.m_Argument.DblArg[0] = m_strokeColor.r;
633 groupElement.m_Argument.DblArg[1] = m_strokeColor.g;
634 groupElement.m_Argument.DblArg[2] = m_strokeColor.b;
635 groupElement.m_Argument.DblArg[3] = m_strokeColor.a;
636 m_currentGroup->push_back( groupElement );
637 }
638}
639
640
642{
643 storePath();
644 m_fillColor = aColor;
645
646 if( m_isGrouping )
647 {
648 GROUP_ELEMENT groupElement;
649 groupElement.m_Command = CMD_SET_FILLCOLOR;
650 groupElement.m_Argument.DblArg[0] = m_fillColor.r;
651 groupElement.m_Argument.DblArg[1] = m_fillColor.g;
652 groupElement.m_Argument.DblArg[2] = m_fillColor.b;
653 groupElement.m_Argument.DblArg[3] = m_fillColor.a;
654 m_currentGroup->push_back( groupElement );
655 }
656}
657
658
659void CAIRO_GAL_BASE::SetLineWidth( float aLineWidth )
660{
661 storePath();
662 GAL::SetLineWidth( aLineWidth );
663
664 if( m_isGrouping )
665 {
666 GROUP_ELEMENT groupElement;
667 groupElement.m_Command = CMD_SET_LINE_WIDTH;
668 groupElement.m_Argument.DblArg[0] = aLineWidth;
669 m_currentGroup->push_back( groupElement );
670 }
671 else
672 {
673 m_lineWidth = aLineWidth;
674 }
675}
676
677
678void CAIRO_GAL_BASE::SetLayerDepth( double aLayerDepth )
679{
680 super::SetLayerDepth( aLayerDepth );
681 storePath();
682}
683
684
685void CAIRO_GAL_BASE::Transform( const MATRIX3x3D& aTransformation )
686{
687 cairo_matrix_t cairoTransformation, newXform;
688
689 cairo_matrix_init( &cairoTransformation, aTransformation.m_data[0][0],
690 aTransformation.m_data[1][0], aTransformation.m_data[0][1],
691 aTransformation.m_data[1][1], aTransformation.m_data[0][2],
692 aTransformation.m_data[1][2] );
693
694 cairo_matrix_multiply( &newXform, &m_currentXform, &cairoTransformation );
695 m_currentXform = newXform;
697}
698
699
700void CAIRO_GAL_BASE::Rotate( double aAngle )
701{
702 storePath();
703
704 if( m_isGrouping )
705 {
706 GROUP_ELEMENT groupElement;
707 groupElement.m_Command = CMD_ROTATE;
708 groupElement.m_Argument.DblArg[0] = aAngle;
709 m_currentGroup->push_back( groupElement );
710 }
711 else
712 {
713 cairo_matrix_rotate( &m_currentXform, aAngle );
715 }
716}
717
718
719void CAIRO_GAL_BASE::Translate( const VECTOR2D& aTranslation )
720{
721 storePath();
722
723 if( m_isGrouping )
724 {
725 GROUP_ELEMENT groupElement;
726 groupElement.m_Command = CMD_TRANSLATE;
727 groupElement.m_Argument.DblArg[0] = aTranslation.x;
728 groupElement.m_Argument.DblArg[1] = aTranslation.y;
729 m_currentGroup->push_back( groupElement );
730 }
731 else
732 {
733 cairo_matrix_translate( &m_currentXform, aTranslation.x, aTranslation.y );
735 }
736}
737
738
739void CAIRO_GAL_BASE::Scale( const VECTOR2D& aScale )
740{
741 storePath();
742
743 if( m_isGrouping )
744 {
745 GROUP_ELEMENT groupElement;
746 groupElement.m_Command = CMD_SCALE;
747 groupElement.m_Argument.DblArg[0] = aScale.x;
748 groupElement.m_Argument.DblArg[1] = aScale.y;
749 m_currentGroup->push_back( groupElement );
750 }
751 else
752 {
753 cairo_matrix_scale( &m_currentXform, aScale.x, aScale.y );
755 }
756}
757
758
760{
761 storePath();
762
763 if( m_isGrouping )
764 {
765 GROUP_ELEMENT groupElement;
766 groupElement.m_Command = CMD_SAVE;
767 m_currentGroup->push_back( groupElement );
768 }
769 else
770 {
771 m_xformStack.push_back( m_currentXform );
773 }
774}
775
776
778{
779 storePath();
780
781 if( m_isGrouping )
782 {
783 GROUP_ELEMENT groupElement;
784 groupElement.m_Command = CMD_RESTORE;
785 m_currentGroup->push_back( groupElement );
786 }
787 else
788 {
789 if( !m_xformStack.empty() )
790 {
792 m_xformStack.pop_back();
794 }
795 }
796}
797
798
800{
801 // If the grouping is started: the actual path is stored in the group, when
802 // a attribute was changed or when grouping stops with the end group method.
803 storePath();
804
805 GROUP group;
806 int groupNumber = getNewGroupNumber();
807 m_groups.insert( std::make_pair( groupNumber, group ) );
808 m_currentGroup = &m_groups[groupNumber];
809 m_isGrouping = true;
810
811 return groupNumber;
812}
813
814
816{
817 storePath();
818 m_isGrouping = false;
819}
820
821
822void CAIRO_GAL_BASE::DrawGroup( int aGroupNumber )
823{
824 // This method implements a small Virtual Machine - all stored commands
825 // are executed; nested calling is also possible
826
827 storePath();
828
829 for( auto it = m_groups[aGroupNumber].begin(); it != m_groups[aGroupNumber].end(); ++it )
830 {
831 switch( it->m_Command )
832 {
833 case CMD_SET_FILL:
834 m_isFillEnabled = it->m_Argument.BoolArg;
835 break;
836
837 case CMD_SET_STROKE:
838 m_isStrokeEnabled = it->m_Argument.BoolArg;
839 break;
840
842 m_fillColor = COLOR4D( it->m_Argument.DblArg[0], it->m_Argument.DblArg[1],
843 it->m_Argument.DblArg[2], it->m_Argument.DblArg[3] );
844 break;
845
847 m_strokeColor = COLOR4D( it->m_Argument.DblArg[0], it->m_Argument.DblArg[1],
848 it->m_Argument.DblArg[2], it->m_Argument.DblArg[3] );
849 break;
850
852 {
853 // Make lines appear at least 1 pixel wide, no matter of zoom
854 double x = 1.0, y = 1.0;
855 cairo_device_to_user_distance( m_currentContext, &x, &y );
856 double minWidth = std::min( fabs( x ), fabs( y ) );
857 cairo_set_line_width( m_currentContext,
858 std::max( it->m_Argument.DblArg[0], minWidth ) );
859 break;
860 }
861
862
863 case CMD_STROKE_PATH:
864 cairo_set_source_rgba( m_currentContext, m_strokeColor.r, m_strokeColor.g,
866 cairo_append_path( m_currentContext, it->m_CairoPath );
867 cairo_stroke( m_currentContext );
868 break;
869
870 case CMD_FILL_PATH:
871 cairo_set_source_rgba( m_currentContext, m_fillColor.r, m_fillColor.g, m_fillColor.b,
873 cairo_append_path( m_currentContext, it->m_CairoPath );
874 cairo_fill( m_currentContext );
875 break;
876
877 /*
878 case CMD_TRANSFORM:
879 cairo_matrix_t matrix;
880 cairo_matrix_init( &matrix, it->argument.DblArg[0], it->argument.DblArg[1],
881 it->argument.DblArg[2], it->argument.DblArg[3],
882 it->argument.DblArg[4], it->argument.DblArg[5] );
883 cairo_transform( m_currentContext, &matrix );
884 break;
885 */
886
887 case CMD_ROTATE:
888 cairo_rotate( m_currentContext, it->m_Argument.DblArg[0] );
889 break;
890
891 case CMD_TRANSLATE:
892 cairo_translate( m_currentContext, it->m_Argument.DblArg[0], it->m_Argument.DblArg[1] );
893 break;
894
895 case CMD_SCALE:
896 cairo_scale( m_currentContext, it->m_Argument.DblArg[0], it->m_Argument.DblArg[1] );
897 break;
898
899 case CMD_SAVE:
900 cairo_save( m_currentContext );
901 break;
902
903 case CMD_RESTORE:
904 cairo_restore( m_currentContext );
905 break;
906
907 case CMD_CALL_GROUP:
908 DrawGroup( it->m_Argument.IntArg );
909 break;
910 }
911 }
912}
913
914
915void CAIRO_GAL_BASE::ChangeGroupColor( int aGroupNumber, const COLOR4D& aNewColor )
916{
917 storePath();
918
919 for( auto it = m_groups[aGroupNumber].begin(); it != m_groups[aGroupNumber].end(); ++it )
920 {
921 if( it->m_Command == CMD_SET_FILLCOLOR || it->m_Command == CMD_SET_STROKECOLOR )
922 {
923 it->m_Argument.DblArg[0] = aNewColor.r;
924 it->m_Argument.DblArg[1] = aNewColor.g;
925 it->m_Argument.DblArg[2] = aNewColor.b;
926 it->m_Argument.DblArg[3] = aNewColor.a;
927 }
928 }
929}
930
931
932void CAIRO_GAL_BASE::ChangeGroupDepth( int aGroupNumber, int aDepth )
933{
934 // Cairo does not have any possibilities to change the depth coordinate of stored items,
935 // it depends only on the order of drawing
936}
937
938
939void CAIRO_GAL_BASE::DeleteGroup( int aGroupNumber )
940{
941 storePath();
942
943 // Delete the Cairo paths
944 std::deque<GROUP_ELEMENT>::iterator it, end;
945
946 for( it = m_groups[aGroupNumber].begin(), end = m_groups[aGroupNumber].end(); it != end; ++it )
947 {
948 if( it->m_Command == CMD_FILL_PATH || it->m_Command == CMD_STROKE_PATH )
949 cairo_path_destroy( it->m_CairoPath );
950 }
951
952 // Delete the group
953 m_groups.erase( aGroupNumber );
954}
955
956
958{
959 for( auto it = m_groups.begin(); it != m_groups.end(); )
960 DeleteGroup( ( it++ )->first );
961}
962
963
965{
966 cairo_set_operator( m_currentContext, aSetting ? CAIRO_OPERATOR_CLEAR : CAIRO_OPERATOR_OVER );
967}
968
969
971{
974}
975
976
978{
979 m_compositor->DrawBuffer( m_tempBuffer, m_mainBuffer, CAIRO_OPERATOR_ADD );
980}
981
982
984{
987}
988
989
991{
992 m_compositor->DrawBuffer( m_tempBuffer, m_mainBuffer, CAIRO_OPERATOR_OVER );
993}
994
995
996void CAIRO_GAL_BASE::DrawCursor( const VECTOR2D& aCursorPosition )
997{
998 m_cursorPosition = aCursorPosition;
999}
1000
1001
1003{
1004}
1005
1006
1008{
1009 for( _cairo_surface* imageSurface : m_imageSurfaces )
1010 cairo_surface_destroy( imageSurface );
1011
1012 m_imageSurfaces.clear();
1013
1014 ClearScreen();
1015
1016 // Compute the world <-> screen transformations
1018
1019 cairo_matrix_init( &m_cairoWorldScreenMatrix, m_worldScreenMatrix.m_data[0][0],
1023
1024 // we work in screen-space coordinates and do the transforms outside.
1025 cairo_identity_matrix( m_context );
1026
1027 cairo_matrix_init_identity( &m_currentXform );
1028
1029 // Start drawing with a new path
1030 cairo_new_path( m_context );
1031 m_isElementAdded = true;
1032
1034
1035 m_lineWidth = 0;
1036}
1037
1038
1039void CAIRO_GAL_BASE::drawAxes( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint )
1040{
1041 syncLineWidth();
1042
1043 VECTOR2D p0 = roundp( xform( aStartPoint ) );
1044 VECTOR2D p1 = roundp( xform( aEndPoint ) );
1045 VECTOR2D org = roundp( xform( VECTOR2D( 0.0, 0.0 ) ) ); // Axis origin = 0,0 coord
1046
1047 cairo_set_source_rgba( m_currentContext, m_axesColor.r, m_axesColor.g, m_axesColor.b,
1048 m_axesColor.a );
1049 cairo_move_to( m_currentContext, p0.x, org.y );
1050 cairo_line_to( m_currentContext, p1.x, org.y );
1051 cairo_move_to( m_currentContext, org.x, p0.y );
1052 cairo_line_to( m_currentContext, org.x, p1.y );
1053 cairo_stroke( m_currentContext );
1054}
1055
1056
1057void CAIRO_GAL_BASE::drawGridLine( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint )
1058{
1059 syncLineWidth();
1060 VECTOR2D p0 = roundp( xform( aStartPoint ) );
1061 VECTOR2D p1 = roundp( xform( aEndPoint ) );
1062
1063 cairo_set_source_rgba( m_currentContext, m_gridColor.r, m_gridColor.g, m_gridColor.b,
1064 m_gridColor.a );
1065 cairo_move_to( m_currentContext, p0.x, p0.y );
1066 cairo_line_to( m_currentContext, p1.x, p1.y );
1067 cairo_stroke( m_currentContext );
1068}
1069
1070
1072{
1073 syncLineWidth();
1074 VECTOR2D offset( 0, 0 );
1075 double size = 2.0 * m_lineWidthInPixels + 0.5;
1076
1077 VECTOR2D p0 = roundp( xform( aPoint ) ) - VECTOR2D( size, 0 ) + offset;
1078 VECTOR2D p1 = roundp( xform( aPoint ) ) + VECTOR2D( size, 0 ) + offset;
1079 VECTOR2D p2 = roundp( xform( aPoint ) ) - VECTOR2D( 0, size ) + offset;
1080 VECTOR2D p3 = roundp( xform( aPoint ) ) + VECTOR2D( 0, size ) + offset;
1081
1082 cairo_set_source_rgba( m_currentContext, m_gridColor.r, m_gridColor.g, m_gridColor.b,
1083 m_gridColor.a );
1084 cairo_move_to( m_currentContext, p0.x, p0.y );
1085 cairo_line_to( m_currentContext, p1.x, p1.y );
1086 cairo_move_to( m_currentContext, p2.x, p2.y );
1087 cairo_line_to( m_currentContext, p3.x, p3.y );
1088 cairo_stroke( m_currentContext );
1089}
1090
1091
1092void CAIRO_GAL_BASE::drawGridPoint( const VECTOR2D& aPoint, double aWidth, double aHeight )
1093{
1094 VECTOR2D p = roundp( xform( aPoint ) );
1095
1096 double sw = std::max( 1.0, aWidth );
1097 double sh = std::max( 1.0, aHeight );
1098
1099 cairo_set_source_rgba( m_currentContext, m_gridColor.r, m_gridColor.g, m_gridColor.b,
1100 m_gridColor.a );
1101 cairo_rectangle( m_currentContext, p.x - std::floor( sw / 2 ) - 0.5,
1102 p.y - std::floor( sh / 2 ) - 0.5, sw, sh );
1103
1104 cairo_fill( m_currentContext );
1105}
1106
1107
1109{
1110 if( m_isFillEnabled )
1111 {
1112 cairo_set_source_rgba( m_currentContext, m_fillColor.r, m_fillColor.g, m_fillColor.b,
1113 m_fillColor.a );
1114
1115 if( m_isStrokeEnabled )
1116 {
1117 cairo_set_line_width( m_currentContext, m_lineWidthInPixels );
1118 cairo_fill_preserve( m_currentContext );
1119 }
1120 else
1121 {
1122 cairo_fill( m_currentContext );
1123 }
1124 }
1125
1126 if( m_isStrokeEnabled )
1127 {
1128 cairo_set_line_width( m_currentContext, m_lineWidthInPixels );
1129 cairo_set_source_rgba( m_currentContext, m_strokeColor.r, m_strokeColor.g, m_strokeColor.b,
1130 m_strokeColor.a );
1131 cairo_stroke( m_currentContext );
1132 }
1133}
1134
1135
1137{
1138 if( m_isElementAdded )
1139 {
1140 m_isElementAdded = false;
1141
1142 if( !m_isGrouping )
1143 {
1144 if( m_isFillEnabled )
1145 {
1146 cairo_set_source_rgba( m_currentContext, m_fillColor.r, m_fillColor.g,
1148 cairo_fill_preserve( m_currentContext );
1149 }
1150
1151 if( m_isStrokeEnabled )
1152 {
1153 cairo_set_source_rgba( m_currentContext, m_strokeColor.r, m_strokeColor.g,
1155 cairo_stroke_preserve( m_currentContext );
1156 }
1157 }
1158 else
1159 {
1160 // Copy the actual path, append it to the global path list
1161 // then check, if the path needs to be stroked/filled and
1162 // add this command to the group list;
1163 if( m_isStrokeEnabled )
1164 {
1165 GROUP_ELEMENT groupElement;
1166 groupElement.m_CairoPath = cairo_copy_path( m_currentContext );
1167 groupElement.m_Command = CMD_STROKE_PATH;
1168 m_currentGroup->push_back( groupElement );
1169 }
1170
1171 if( m_isFillEnabled )
1172 {
1173 GROUP_ELEMENT groupElement;
1174 groupElement.m_CairoPath = cairo_copy_path( m_currentContext );
1175 groupElement.m_Command = CMD_FILL_PATH;
1176 m_currentGroup->push_back( groupElement );
1177 }
1178 }
1179
1180 cairo_new_path( m_currentContext );
1181 }
1182}
1183
1184
1185void CAIRO_GAL_BASE::blitCursor( wxMemoryDC& clientDC )
1186{
1187 if( !IsCursorEnabled() )
1188 return;
1189
1191 const COLOR4D cColor = getCursorColor();
1192 const int cursorSize = m_fullscreenCursor ? 8000 : 80;
1193
1194 wxColour color( cColor.r * cColor.a * 255, cColor.g * cColor.a * 255, cColor.b * cColor.a * 255,
1195 255 );
1196 clientDC.SetPen( wxPen( color ) );
1197 clientDC.DrawLine( p.x - cursorSize / 2, p.y, p.x + cursorSize / 2, p.y );
1198 clientDC.DrawLine( p.x, p.y - cursorSize / 2, p.x, p.y + cursorSize / 2 );
1199}
1200
1201
1202void CAIRO_GAL_BASE::drawPoly( const std::deque<VECTOR2D>& aPointList )
1203{
1204 wxCHECK( aPointList.size() > 1, /* void */ );
1205
1206 // Iterate over the point list and draw the segments
1207 std::deque<VECTOR2D>::const_iterator it = aPointList.begin();
1208
1209 syncLineWidth();
1210
1211 const VECTOR2D p = roundp( xform( it->x, it->y ) );
1212
1213 cairo_move_to( m_currentContext, p.x, p.y );
1214
1215 for( ++it; it != aPointList.end(); ++it )
1216 {
1217 const VECTOR2D p2 = roundp( xform( it->x, it->y ) );
1218
1219 cairo_line_to( m_currentContext, p2.x, p2.y );
1220 }
1221
1222 flushPath();
1223 m_isElementAdded = true;
1224}
1225
1226
1227void CAIRO_GAL_BASE::drawPoly( const std::vector<VECTOR2D>& aPointList )
1228{
1229 wxCHECK( aPointList.size() > 1, /* void */ );
1230
1231 // Iterate over the point list and draw the segments
1232 std::vector<VECTOR2D>::const_iterator it = aPointList.begin();
1233
1234 syncLineWidth();
1235
1236 const VECTOR2D p = roundp( xform( it->x, it->y ) );
1237
1238 cairo_move_to( m_currentContext, p.x, p.y );
1239
1240 for( ++it; it != aPointList.end(); ++it )
1241 {
1242 const VECTOR2D p2 = roundp( xform( it->x, it->y ) );
1243
1244 cairo_line_to( m_currentContext, p2.x, p2.y );
1245 }
1246
1247 flushPath();
1248 m_isElementAdded = true;
1249}
1250
1251
1252void CAIRO_GAL_BASE::drawPoly( const VECTOR2D aPointList[], int aListSize )
1253{
1254 wxCHECK( aListSize > 1, /* void */ );
1255
1256 // Iterate over the point list and draw the segments
1257 const VECTOR2D* ptr = aPointList;
1258
1259 syncLineWidth();
1260
1261 const VECTOR2D p = roundp( xform( ptr->x, ptr->y ) );
1262 cairo_move_to( m_currentContext, p.x, p.y );
1263
1264 for( int i = 1; i < aListSize; ++i )
1265 {
1266 ++ptr;
1267 const VECTOR2D p2 = roundp( xform( ptr->x, ptr->y ) );
1268 cairo_line_to( m_currentContext, p2.x, p2.y );
1269 }
1270
1271 flushPath();
1272 m_isElementAdded = true;
1273}
1274
1275
1277{
1278 wxCHECK( aLineChain.PointCount() > 1, /* void */ );
1279
1280 syncLineWidth();
1281
1282 auto numPoints = aLineChain.PointCount();
1283
1284 if( aLineChain.IsClosed() )
1285 numPoints += 1;
1286
1287 const VECTOR2I start = aLineChain.CPoint( 0 );
1288 const VECTOR2D p = roundp( xform( start.x, start.y ) );
1289 cairo_move_to( m_currentContext, p.x, p.y );
1290
1291 for( int i = 1; i < numPoints; ++i )
1292 {
1293 const VECTOR2I& pw = aLineChain.CPoint( i );
1294 const VECTOR2D ps = roundp( xform( pw.x, pw.y ) );
1295 cairo_line_to( m_currentContext, ps.x, ps.y );
1296 }
1297
1298 flushPath();
1299 m_isElementAdded = true;
1300}
1301
1302
1304{
1305 wxASSERT_MSG( m_groups.size() < std::numeric_limits<unsigned int>::max(),
1306 wxT( "There are no free slots to store a group" ) );
1307
1308 while( m_groups.find( m_groupCounter ) != m_groups.end() )
1310
1311 return m_groupCounter++;
1312}
1313
1314
1315CAIRO_GAL::CAIRO_GAL( GAL_DISPLAY_OPTIONS& aDisplayOptions, wxWindow* aParent,
1316 wxEvtHandler* aMouseListener, wxEvtHandler* aPaintListener,
1317 const wxString& aName ) :
1318 CAIRO_GAL_BASE( aDisplayOptions ),
1319 wxWindow( aParent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxEXPAND, aName )
1320{
1321 // Initialise compositing state
1322 m_mainBuffer = 0;
1323 m_overlayBuffer = 0;
1324 m_tempBuffer = 0;
1325 m_savedBuffer = 0;
1326 m_validCompositor = false;
1329
1330 m_bitmapBuffer = nullptr;
1331 m_wxOutput = nullptr;
1332
1333 m_parentWindow = aParent;
1334 m_mouseListener = aMouseListener;
1335 m_paintListener = aPaintListener;
1336
1337 // Connect the native cursor handler
1338 Connect( wxEVT_SET_CURSOR, wxSetCursorEventHandler( CAIRO_GAL::onSetNativeCursor ), nullptr,
1339 this );
1340
1341 // Connecting the event handlers
1342 Connect( wxEVT_PAINT, wxPaintEventHandler( CAIRO_GAL::onPaint ) );
1343
1344 // Mouse events are skipped to the parent
1345 Connect( wxEVT_MOTION, wxMouseEventHandler( CAIRO_GAL::skipMouseEvent ) );
1346 Connect( wxEVT_LEFT_DOWN, wxMouseEventHandler( CAIRO_GAL::skipMouseEvent ) );
1347 Connect( wxEVT_LEFT_UP, wxMouseEventHandler( CAIRO_GAL::skipMouseEvent ) );
1348 Connect( wxEVT_LEFT_DCLICK, wxMouseEventHandler( CAIRO_GAL::skipMouseEvent ) );
1349 Connect( wxEVT_MIDDLE_DOWN, wxMouseEventHandler( CAIRO_GAL::skipMouseEvent ) );
1350 Connect( wxEVT_MIDDLE_UP, wxMouseEventHandler( CAIRO_GAL::skipMouseEvent ) );
1351 Connect( wxEVT_MIDDLE_DCLICK, wxMouseEventHandler( CAIRO_GAL::skipMouseEvent ) );
1352 Connect( wxEVT_RIGHT_DOWN, wxMouseEventHandler( CAIRO_GAL::skipMouseEvent ) );
1353 Connect( wxEVT_RIGHT_UP, wxMouseEventHandler( CAIRO_GAL::skipMouseEvent ) );
1354 Connect( wxEVT_RIGHT_DCLICK, wxMouseEventHandler( CAIRO_GAL::skipMouseEvent ) );
1355 Connect( wxEVT_AUX1_DOWN, wxMouseEventHandler( CAIRO_GAL::skipMouseEvent ) );
1356 Connect( wxEVT_AUX1_UP, wxMouseEventHandler( CAIRO_GAL::skipMouseEvent ) );
1357 Connect( wxEVT_AUX1_DCLICK, wxMouseEventHandler( CAIRO_GAL::skipMouseEvent ) );
1358 Connect( wxEVT_AUX2_DOWN, wxMouseEventHandler( CAIRO_GAL::skipMouseEvent ) );
1359 Connect( wxEVT_AUX2_UP, wxMouseEventHandler( CAIRO_GAL::skipMouseEvent ) );
1360 Connect( wxEVT_AUX2_DCLICK, wxMouseEventHandler( CAIRO_GAL::skipMouseEvent ) );
1361 Connect( wxEVT_MOUSEWHEEL, wxMouseEventHandler( CAIRO_GAL::skipMouseEvent ) );
1362#if defined _WIN32 || defined _WIN64
1363 Connect( wxEVT_ENTER_WINDOW, wxMouseEventHandler( CAIRO_GAL::skipMouseEvent ) );
1364#endif
1365
1366 SetSize( aParent->GetClientSize() );
1367 m_screenSize = ToVECTOR2I( aParent->GetClientSize() );
1368
1369 // Allocate memory for pixel storage
1371
1372 m_isInitialized = false;
1373}
1374
1375
1377{
1378 deleteBitmaps();
1379}
1380
1381
1383{
1384 initSurface();
1385
1387
1388 if( !m_validCompositor )
1389 setCompositor();
1390
1391 m_compositor->SetMainContext( m_context );
1392 m_compositor->SetBuffer( m_mainBuffer );
1393}
1394
1395
1397{
1399
1400 // Merge buffers on the screen
1401 m_compositor->DrawBuffer( m_mainBuffer );
1402 m_compositor->DrawBuffer( m_overlayBuffer );
1403
1404 // Now translate the raw context data from the format stored
1405 // by cairo into a format understood by wxImage.
1406 pixman_image_t* dstImg = pixman_image_create_bits(
1407 wxPlatformInfo::Get().GetEndianness() == wxENDIAN_LITTLE ? PIXMAN_b8g8r8
1408 : PIXMAN_r8g8b8,
1410 pixman_image_t* srcImg =
1411 pixman_image_create_bits( PIXMAN_a8r8g8b8, m_screenSize.x, m_screenSize.y,
1412 (uint32_t*) m_bitmapBuffer, m_wxBufferWidth * 4 );
1413
1414 pixman_image_composite( PIXMAN_OP_SRC, srcImg, nullptr, dstImg, 0, 0, 0, 0, 0, 0,
1416
1417 // Free allocated memory
1418 pixman_image_unref( srcImg );
1419 pixman_image_unref( dstImg );
1420
1421 wxImage img( m_wxBufferWidth, m_screenSize.y, m_wxOutput, true );
1422 wxBitmap bmp( img );
1423 wxMemoryDC mdc( bmp );
1424 wxClientDC clientDC( this );
1425
1426 // Now it is the time to blit the mouse cursor
1427 blitCursor( mdc );
1428 clientDC.Blit( 0, 0, m_screenSize.x, m_screenSize.y, &mdc, 0, 0, wxCOPY );
1429
1430 deinitSurface();
1431}
1432
1433
1434void CAIRO_GAL::PostPaint( wxPaintEvent& aEvent )
1435{
1436 // posts an event to m_paint_listener to ask for redraw the canvas.
1437 if( m_paintListener )
1438 wxPostEvent( m_paintListener, aEvent );
1439}
1440
1441
1442void CAIRO_GAL::ResizeScreen( int aWidth, int aHeight )
1443{
1444 CAIRO_GAL_BASE::ResizeScreen( aWidth, aHeight );
1445
1446 // Recreate the bitmaps
1447 deleteBitmaps();
1449
1450 if( m_validCompositor )
1451 m_compositor->Resize( aWidth, aHeight );
1452
1453 m_validCompositor = false;
1454
1455 SetSize( wxSize( aWidth, aHeight ) );
1456}
1457
1458
1459bool CAIRO_GAL::Show( bool aShow )
1460{
1461 bool s = wxWindow::Show( aShow );
1462
1463 if( aShow )
1464 wxWindow::Raise();
1465
1466 return s;
1467}
1468
1469
1471{
1472 initSurface();
1474}
1475
1476
1478{
1480 deinitSurface();
1481}
1482
1483
1485{
1486 // If the compositor is not set, that means that there is a recaching process going on
1487 // and we do not need the compositor now
1488 if( !m_validCompositor )
1489 return;
1490
1491 // Cairo grouping prevents display of overlapping items on the same layer in the lighter color
1492 if( m_isInitialized )
1493 storePath();
1494
1495 switch( aTarget )
1496 {
1497 default:
1498 case TARGET_CACHED:
1499 case TARGET_NONCACHED: m_compositor->SetBuffer( m_mainBuffer ); break;
1500 case TARGET_OVERLAY: m_compositor->SetBuffer( m_overlayBuffer ); break;
1501 case TARGET_TEMP: m_compositor->SetBuffer( m_tempBuffer ); break;
1502 }
1503
1504 m_currentTarget = aTarget;
1505}
1506
1507
1509{
1510 return m_currentTarget;
1511}
1512
1513
1515{
1516 // Save the current state
1517 unsigned int currentBuffer = m_compositor->GetBuffer();
1518
1519 switch( aTarget )
1520 {
1521 // Cached and noncached items are rendered to the same buffer
1522 default:
1523 case TARGET_CACHED:
1524 case TARGET_NONCACHED: m_compositor->SetBuffer( m_mainBuffer ); break;
1525 case TARGET_OVERLAY: m_compositor->SetBuffer( m_overlayBuffer ); break;
1526 case TARGET_TEMP: m_compositor->SetBuffer( m_tempBuffer ); break;
1527 }
1528
1529 m_compositor->ClearBuffer( COLOR4D::BLACK );
1530
1531 // Restore the previous state
1532 m_compositor->SetBuffer( currentBuffer );
1533}
1534
1535
1537{
1538 if( m_isInitialized )
1539 return;
1540
1541 m_surface = cairo_image_surface_create_for_data( m_bitmapBuffer, GAL_FORMAT, m_wxBufferWidth,
1543
1544 m_context = cairo_create( m_surface );
1545
1546#ifdef DEBUG
1547 cairo_status_t status = cairo_status( m_context );
1548 wxASSERT_MSG( status == CAIRO_STATUS_SUCCESS, wxT( "Cairo context creation error" ) );
1549#endif /* DEBUG */
1550
1552
1553 m_isInitialized = true;
1554}
1555
1556
1558{
1559 if( !m_isInitialized )
1560 return;
1561
1562 cairo_destroy( m_context );
1563 m_context = nullptr;
1564 cairo_surface_destroy( m_surface );
1565 m_surface = nullptr;
1566
1567 m_isInitialized = false;
1568}
1569
1570
1572{
1574
1575 while( ( ( m_wxBufferWidth * 3 ) % 4 ) != 0 )
1577
1578 // Create buffer, use the system independent Cairo context backend
1579 m_stride = cairo_format_stride_for_width( GAL_FORMAT, m_wxBufferWidth );
1581
1582 wxASSERT( m_bitmapBuffer == nullptr );
1583 m_bitmapBuffer = new unsigned char[m_bufferSize * 4];
1584
1585 wxASSERT( m_wxOutput == nullptr );
1586 m_wxOutput = new unsigned char[m_wxBufferWidth * 3 * m_screenSize.y];
1587}
1588
1589
1591{
1592 delete[] m_bitmapBuffer;
1593 m_bitmapBuffer = nullptr;
1594
1595 delete[] m_wxOutput;
1596 m_wxOutput = nullptr;
1597}
1598
1599
1601{
1602 // Recreate the compositor with the new Cairo context
1605 m_compositor->SetAntialiasingMode( m_options.cairo_antialiasing_mode );
1606
1607 // Prepare buffers
1608 m_mainBuffer = m_compositor->CreateBuffer();
1609 m_overlayBuffer = m_compositor->CreateBuffer();
1610 m_tempBuffer = m_compositor->CreateBuffer();
1611
1612 m_validCompositor = true;
1613}
1614
1615
1616void CAIRO_GAL::onPaint( wxPaintEvent& aEvent )
1617{
1618 PostPaint( aEvent );
1619}
1620
1621
1622void CAIRO_GAL::skipMouseEvent( wxMouseEvent& aEvent )
1623{
1624 // Post the mouse event to the event listener registered in constructor, if any
1625 if( m_mouseListener )
1626 wxPostEvent( m_mouseListener, aEvent );
1627}
1628
1629
1631{
1632 bool refresh = false;
1633
1634 if( m_validCompositor &&
1635 aOptions.cairo_antialiasing_mode != m_compositor->GetAntialiasingMode() )
1636 {
1637 m_compositor->SetAntialiasingMode( m_options.cairo_antialiasing_mode );
1638 m_validCompositor = false;
1639 deinitSurface();
1640
1641 refresh = true;
1642 }
1643
1644 if( super::updatedGalDisplayOptions( aOptions ) )
1645 {
1646 Refresh();
1647 refresh = true;
1648 }
1649
1650 return refresh;
1651}
1652
1653
1655{
1656 // Store the current cursor type and get the wxCursor for it
1657 if( !GAL::SetNativeCursorStyle( aCursor ) )
1658 return false;
1659
1661
1662 // Update the cursor in the wx control
1663 wxWindow::SetCursor( m_currentwxCursor );
1664
1665 return true;
1666}
1667
1668
1669void CAIRO_GAL::onSetNativeCursor( wxSetCursorEvent& aEvent )
1670{
1671 aEvent.SetCursor( m_currentwxCursor );
1672}
1673
1674
1676{
1678
1679 // Draw the grid
1680 // For the drawing the start points, end points and increments have
1681 // to be calculated in world coordinates
1682 VECTOR2D worldStartPoint = m_screenWorldMatrix * VECTOR2D( 0.0, 0.0 );
1684
1685 // Compute the line marker or point radius of the grid
1686 // Note: generic grids can't handle sub-pixel lines without
1687 // either losing fine/course distinction or having some dots
1688 // fail to render
1689 float marker = std::fmax( 1.0f, m_gridLineWidth ) / m_worldScale;
1690 float doubleMarker = 2.0f * marker;
1691
1692 // Draw axes if desired
1693 if( m_axesEnabled )
1694 {
1695 SetLineWidth( marker );
1696 drawAxes( worldStartPoint, worldEndPoint );
1697 }
1698
1699 if( !m_gridVisibility || m_gridSize.x == 0 || m_gridSize.y == 0 )
1700 return;
1701
1702 VECTOR2D gridScreenSize( m_gridSize );
1703
1704 double gridThreshold = KiROUND( computeMinGridSpacing() / m_worldScale );
1705
1707 gridThreshold *= 2.0;
1708
1709 // If we cannot display the grid density, scale down by a tick size and
1710 // try again. Eventually, we get some representation of the grid
1711 while( std::min( gridScreenSize.x, gridScreenSize.y ) <= gridThreshold )
1712 {
1713 gridScreenSize = gridScreenSize * static_cast<double>( m_gridTick );
1714 }
1715
1716 // Compute grid starting and ending indexes to draw grid points on the
1717 // visible screen area
1718 // Note: later any point coordinate will be offsetted by m_gridOrigin
1719 int gridStartX = KiROUND( ( worldStartPoint.x - m_gridOrigin.x ) / gridScreenSize.x );
1720 int gridEndX = KiROUND( ( worldEndPoint.x - m_gridOrigin.x ) / gridScreenSize.x );
1721 int gridStartY = KiROUND( ( worldStartPoint.y - m_gridOrigin.y ) / gridScreenSize.y );
1722 int gridEndY = KiROUND( ( worldEndPoint.y - m_gridOrigin.y ) / gridScreenSize.y );
1723
1724 // Ensure start coordinate > end coordinate
1725 SWAP( gridStartX, >, gridEndX );
1726 SWAP( gridStartY, >, gridEndY );
1727
1728 // Ensure the grid fills the screen
1729 --gridStartX;
1730 ++gridEndX;
1731 --gridStartY;
1732 ++gridEndY;
1733
1734 // Draw the grid behind all other layers
1735 SetLayerDepth( m_depthRange.y * 0.75 );
1736
1738 {
1739 // Now draw the grid, every coarse grid line gets the double width
1740
1741 // Vertical lines
1742 for( int j = gridStartY; j <= gridEndY; j++ )
1743 {
1744 const double y = j * gridScreenSize.y + m_gridOrigin.y;
1745
1746 if( m_axesEnabled && y == 0.0 )
1747 continue;
1748
1749 SetLineWidth( ( j % m_gridTick ) ? marker : doubleMarker );
1750 drawGridLine( VECTOR2D( gridStartX * gridScreenSize.x + m_gridOrigin.x, y ),
1751 VECTOR2D( gridEndX * gridScreenSize.x + m_gridOrigin.x, y ) );
1752 }
1753
1754 // Horizontal lines
1755 for( int i = gridStartX; i <= gridEndX; i++ )
1756 {
1757 const double x = i * gridScreenSize.x + m_gridOrigin.x;
1758
1759 if( m_axesEnabled && x == 0.0 )
1760 continue;
1761
1762 SetLineWidth( ( i % m_gridTick ) ? marker : doubleMarker );
1763 drawGridLine( VECTOR2D( x, gridStartY * gridScreenSize.y + m_gridOrigin.y ),
1764 VECTOR2D( x, gridEndY * gridScreenSize.y + m_gridOrigin.y ) );
1765 }
1766 }
1767 else // Dots or Crosses grid
1768 {
1769 m_lineWidthIsOdd = true;
1770 m_isStrokeEnabled = true;
1771
1772 for( int j = gridStartY; j <= gridEndY; j++ )
1773 {
1774 bool tickY = ( j % m_gridTick == 0 );
1775
1776 for( int i = gridStartX; i <= gridEndX; i++ )
1777 {
1778 bool tickX = ( i % m_gridTick == 0 );
1779 VECTOR2D pos{ i * gridScreenSize.x + m_gridOrigin.x,
1780 j * gridScreenSize.y + m_gridOrigin.y };
1781
1783 {
1784 SetLineWidth( ( tickX && tickY ) ? doubleMarker : marker );
1785 drawGridCross( pos );
1786 }
1787 else if( m_gridStyle == GRID_STYLE::DOTS )
1788 {
1789 double doubleGridLineWidth = m_gridLineWidth * 2.0f;
1790 drawGridPoint( pos, ( tickX ) ? doubleGridLineWidth : m_gridLineWidth,
1791 ( tickY ) ? doubleGridLineWidth : m_gridLineWidth );
1792 }
1793 }
1794 }
1795 }
1796}
1797
1798
1799void CAIRO_GAL_BASE::DrawGlyph( const KIFONT::GLYPH& aGlyph, int aNth, int aTotal )
1800{
1801 if( aGlyph.IsStroke() )
1802 {
1803 const KIFONT::STROKE_GLYPH& glyph = static_cast<const KIFONT::STROKE_GLYPH&>( aGlyph );
1804
1805 for( const std::vector<VECTOR2D>& pointList : glyph )
1806 drawPoly( pointList );
1807 }
1808 else if( aGlyph.IsOutline() )
1809 {
1810 const KIFONT::OUTLINE_GLYPH& glyph = static_cast<const KIFONT::OUTLINE_GLYPH&>( aGlyph );
1811
1812 if( aNth == 0 )
1813 {
1814 cairo_close_path( m_currentContext );
1815 flushPath();
1816
1817 cairo_new_path( m_currentContext );
1818 SetIsFill( true );
1819 SetIsStroke( false );
1820 }
1821
1822 // eventually glyphs should not be drawn as polygons at all,
1823 // but as bitmaps with antialiasing, this is just a stopgap measure
1824 // of getting some form of outline font display
1825
1826 glyph.Triangulate(
1827 [&]( const VECTOR2D& aVertex1, const VECTOR2D& aVertex2, const VECTOR2D& aVertex3 )
1828 {
1829 syncLineWidth();
1830
1831 const VECTOR2D p0 = roundp( xform( aVertex1 ) );
1832 const VECTOR2D p1 = roundp( xform( aVertex2 ) );
1833 const VECTOR2D p2 = roundp( xform( aVertex3 ) );
1834
1835 cairo_move_to( m_currentContext, p0.x, p0.y );
1836 cairo_line_to( m_currentContext, p1.x, p1.y );
1837 cairo_line_to( m_currentContext, p2.x, p2.y );
1838 cairo_close_path( m_currentContext );
1839 cairo_set_fill_rule( m_currentContext, CAIRO_FILL_RULE_EVEN_ODD );
1840 flushPath();
1841 cairo_fill( m_currentContext );
1842 } );
1843
1844 if( aNth == aTotal - 1 )
1845 {
1846 /*
1847 cairo_close_path( currentContext );
1848 setSourceRgba( currentContext, fillColor );
1849 cairo_set_fill_rule( currentContext, CAIRO_FILL_RULE_EVEN_ODD );
1850 cairo_fill_preserve( currentContext );
1851 setSourceRgba( currentContext, strokeColor );
1852 cairo_stroke( currentContext );
1853 */
1854 flushPath();
1855 m_isElementAdded = true;
1856 }
1857 }
1858}
int color
Definition: DXF_plotter.cpp:57
Class that handles multitarget rendering (ie.
static double roundp(double x)
Definition: cairo_gal.cpp:185
This class handle bitmap images in KiCad.
Definition: bitmap_base.h:52
VECTOR2I GetSizePixels() const
Definition: bitmap_base.h:112
int GetPPI() const
Definition: bitmap_base.h:123
wxImage * GetImageData()
Definition: bitmap_base.h:71
static const wxCursor GetCursor(KICURSOR aCursorType)
Definition: cursors.cpp:394
double AsRadians() const
Definition: eda_angle.h:153
virtual bool IsStroke() const
Definition: glyph.h:52
virtual bool IsOutline() const
Definition: glyph.h:51
void Triangulate(std::function< void(const VECTOR2I &aPt1, const VECTOR2I &aPt2, const VECTOR2I &aPt3)> aCallback) const
Definition: glyph.cpp:121
void DrawGlyph(const KIFONT::GLYPH &aPolySet, int aNth, int aTotal) override
Draw a polygon representing a font glyph.
Definition: cairo_gal.cpp:1799
void blitCursor(wxMemoryDC &clientDC)
Blit cursor into the current screen.
Definition: cairo_gal.cpp:1185
void drawGridLine(const VECTOR2D &aStartPoint, const VECTOR2D &aEndPoint)
Draw a grid line (usually a simplified line function).
Definition: cairo_gal.cpp:1057
cairo_surface_t * m_surface
Cairo surface.
Definition: cairo_gal.h:365
cairo_matrix_t m_cairoWorldScreenMatrix
Cairo world to screen transform matrix.
Definition: cairo_gal.h:360
void Flush() override
Force all remaining objects to be drawn.
Definition: cairo_gal.cpp:579
void BeginDrawing() override
Start/end drawing functions, draw calls can be only made in between the calls to BeginDrawing()/EndDr...
Definition: cairo_gal.cpp:94
void Restore() override
Restore the context.
Definition: cairo_gal.cpp:777
unsigned int m_groupCounter
Counter used for generating group keys.
Definition: cairo_gal.h:354
void Translate(const VECTOR2D &aTranslation) override
Translate the context.
Definition: cairo_gal.cpp:719
void Save() override
Save the context.
Definition: cairo_gal.cpp:759
void ClearCache() override
Delete all data created during caching of graphic items.
Definition: cairo_gal.cpp:957
bool m_isElementAdded
Was an graphic element added ?
Definition: cairo_gal.h:352
const double xform(double x)
Definition: cairo_gal.cpp:177
void DeleteGroup(int aGroupNumber) override
Delete the group from the memory.
Definition: cairo_gal.cpp:939
std::deque< GROUP_ELEMENT > GROUP
A graphic group type definition.
Definition: cairo_gal.h:348
void DrawLine(const VECTOR2D &aStartPoint, const VECTOR2D &aEndPoint) override
Draw a line.
Definition: cairo_gal.cpp:200
void ClearScreen() override
Clear the screen.
Definition: cairo_gal.cpp:585
void storePath()
Store the actual path.
Definition: cairo_gal.cpp:1136
void DrawGroup(int aGroupNumber) override
Draw the stored group.
Definition: cairo_gal.cpp:822
void drawAxes(const VECTOR2D &aStartPoint, const VECTOR2D &aEndPoint)
Definition: cairo_gal.cpp:1039
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:368
void DrawArc(const VECTOR2D &aCenterPoint, double aRadius, const EDA_ANGLE &aStartAngle, const EDA_ANGLE &aEndAngle) override
Draw an arc.
Definition: cairo_gal.cpp:341
void SetFillColor(const COLOR4D &aColor) override
Set the fill color.
Definition: cairo_gal.cpp:641
GROUP * m_currentGroup
Currently used group.
Definition: cairo_gal.h:355
void DrawCursor(const VECTOR2D &aCursorPosition) override
Draw the cursor.
Definition: cairo_gal.cpp:996
const VECTOR2D roundp(const VECTOR2D &v)
Definition: cairo_gal.cpp:191
bool m_isGrouping
Is grouping enabled ?
Definition: cairo_gal.h:351
static constexpr cairo_format_t GAL_FORMAT
Format used to store pixels.
Definition: cairo_gal.h:372
void SetNegativeDrawMode(bool aSetting) override
Set negative draw mode in the renderer.
Definition: cairo_gal.cpp:964
void ChangeGroupDepth(int aGroupNumber, int aDepth) override
Change the depth (Z-axis position) of the group.
Definition: cairo_gal.cpp:932
int BeginGroup() override
Begin a group.
Definition: cairo_gal.cpp:799
void SetLayerDepth(double aLayerDepth) override
Set the depth of the layer (position on the z-axis)
Definition: cairo_gal.cpp:678
unsigned int getNewGroupNumber()
Return a valid key that can be used as a new group number.
Definition: cairo_gal.cpp:1303
cairo_matrix_t m_currentXform
Definition: cairo_gal.h:361
void SetIsStroke(bool aIsStrokeEnabled) override
Enable/disable stroked outlines.
Definition: cairo_gal.cpp:608
void EndGroup() override
End the group.
Definition: cairo_gal.cpp:815
void drawPoly(const std::deque< VECTOR2D > &aPointList)
Drawing polygons & polylines is the same in Cairo, so here is the common code.
Definition: cairo_gal.cpp:1202
void Transform(const MATRIX3x3D &aTransformation) override
Transform the context.
Definition: cairo_gal.cpp:685
void DrawRectangle(const VECTOR2D &aStartPoint, const VECTOR2D &aEndPoint) override
Draw a rectangle.
Definition: cairo_gal.cpp:446
void ChangeGroupColor(int aGroupNumber, const COLOR4D &aNewColor) override
Change the color used to draw the group.
Definition: cairo_gal.cpp:915
std::map< int, GROUP > m_groups
List of graphic groups.
Definition: cairo_gal.h:353
void SetStrokeColor(const COLOR4D &aColor) override
Set the stroke color.
Definition: cairo_gal.cpp:623
cairo_t * m_context
Cairo image.
Definition: cairo_gal.h:364
void EndDrawing() override
End the drawing, needs to be called for every new frame.
Definition: cairo_gal.cpp:100
void updateWorldScreenMatrix()
Definition: cairo_gal.cpp:107
void EnableDepthTest(bool aEnabled=false) override
Definition: cairo_gal.cpp:1002
void DrawCurve(const VECTOR2D &startPoint, const VECTOR2D &controlPointA, const VECTOR2D &controlPointB, const VECTOR2D &endPoint, double aFilterValue=0.0) override
Draw a cubic bezier spline.
Definition: cairo_gal.cpp:481
void Scale(const VECTOR2D &aScale) override
Scale the context.
Definition: cairo_gal.cpp:739
std::vector< cairo_matrix_t > m_xformStack
Definition: cairo_gal.h:370
void DrawPolygon(const std::deque< VECTOR2D > &aPointList) override
Draw a polygon.
Definition: cairo_gal.h:114
void DrawArcSegment(const VECTOR2D &aCenterPoint, double aRadius, const EDA_ANGLE &aStartAngle, const EDA_ANGLE &aEndAngle, double aWidth, double aMaxError) override
Draw an arc segment.
Definition: cairo_gal.cpp:380
cairo_matrix_t m_currentWorld2Screen
Definition: cairo_gal.h:362
void drawGridCross(const VECTOR2D &aPoint)
Definition: cairo_gal.cpp:1071
void SetLineWidth(float aLineWidth) override
Set the line width.
Definition: cairo_gal.cpp:659
void arc_angles_xform_and_normalize(double &aStartAngle, double &aEndAngle)
Transform according to the rotation from m_currentWorld2Screen transform matrix for the start angle a...
Definition: cairo_gal.cpp:143
void syncLineWidth(bool aForceWidth=false, double aWidth=0.0)
Definition: cairo_gal.cpp:214
cairo_t * m_currentContext
Currently used Cairo context for drawing.
Definition: cairo_gal.h:363
void DrawCircle(const VECTOR2D &aCenterPoint, double aRadius) override
Draw a circle using world coordinates.
Definition: cairo_gal.cpp:325
const double angle_xform(const double aAngle)
Transform according to the rotation from m_currentWorld2Screen transform matrix.
Definition: cairo_gal.cpp:129
void DrawSegment(const VECTOR2D &aStartPoint, const VECTOR2D &aEndPoint, double aWidth) override
Draw a rounded segment.
Definition: cairo_gal.cpp:257
void drawGridPoint(const VECTOR2D &aPoint, double aWidth, double aHeight)
Definition: cairo_gal.cpp:1092
void SetIsFill(bool aIsFillEnabled) override
Enable/disable fill.
Definition: cairo_gal.cpp:593
void DrawBitmap(const BITMAP_BASE &aBitmap, double alphaBlend=1.0) override
Draw a bitmap image.
Definition: cairo_gal.cpp:503
@ CMD_SET_STROKE
Enable/disable stroking.
Definition: cairo_gal.h:321
@ CMD_SAVE
Save the transformation matrix.
Definition: cairo_gal.h:331
@ CMD_SCALE
Scale the context.
Definition: cairo_gal.h:330
@ CMD_SET_LINE_WIDTH
Set the line width.
Definition: cairo_gal.h:324
@ CMD_SET_FILL
Enable/disable filling.
Definition: cairo_gal.h:320
@ CMD_CALL_GROUP
Call a group.
Definition: cairo_gal.h:333
@ CMD_ROTATE
Rotate the context.
Definition: cairo_gal.h:328
@ CMD_STROKE_PATH
Set the stroke path.
Definition: cairo_gal.h:325
@ CMD_TRANSLATE
Translate the context.
Definition: cairo_gal.h:329
@ CMD_SET_FILLCOLOR
Set the fill color.
Definition: cairo_gal.h:322
@ CMD_FILL_PATH
Set the fill path.
Definition: cairo_gal.h:326
@ CMD_RESTORE
Restore the transformation matrix.
Definition: cairo_gal.h:332
@ CMD_SET_STROKECOLOR
Set the stroke color.
Definition: cairo_gal.h:323
void ResizeScreen(int aWidth, int aHeight) override
Resizes the canvas.
Definition: cairo_gal.cpp:573
void Rotate(double aAngle) override
Rotate the context.
Definition: cairo_gal.cpp:700
void DrawSegmentChain(const std::vector< VECTOR2D > &aPointList, double aWidth) override
Draw a chain of rounded segments.
Definition: cairo_gal.cpp:238
double m_lineWidthInPixels
Definition: cairo_gal.h:357
void DrawGrid() override
Definition: cairo_gal.cpp:1675
void EndGroup() override
End the group.
Definition: cairo_gal.cpp:1477
void deinitSurface()
Destroy Cairo surfaces when are not needed anymore.
Definition: cairo_gal.cpp:1557
void PostPaint(wxPaintEvent &aEvent)
Post an event to m_paint_listener.
Definition: cairo_gal.cpp:1434
bool Show(bool aShow) override
Show/hide the GAL canvas.
Definition: cairo_gal.cpp:1459
void SetTarget(RENDER_TARGET aTarget) override
Set the target for rendering.
Definition: cairo_gal.cpp:1484
wxCursor m_currentwxCursor
wxCursor showing the current native cursor
Definition: cairo_gal.h:518
void skipMouseEvent(wxMouseEvent &aEvent)
Mouse event handler, forwards the event to the child.
Definition: cairo_gal.cpp:1622
unsigned int m_overlayBuffer
Handle to the overlay buffer.
Definition: cairo_gal.h:499
unsigned int m_bufferSize
Size of buffers cairoOutput, bitmapBuffers.
Definition: cairo_gal.h:509
CAIRO_GAL(GAL_DISPLAY_OPTIONS &aDisplayOptions, wxWindow *aParent, wxEvtHandler *aMouseListener=nullptr, wxEvtHandler *aPaintListener=nullptr, const wxString &aName=wxT("CairoCanvas"))
Definition: cairo_gal.cpp:1315
void initSurface()
Prepare Cairo surfaces for drawing.
Definition: cairo_gal.cpp:1536
int BeginGroup() override
Begin a group.
Definition: cairo_gal.cpp:1470
bool SetNativeCursorStyle(KICURSOR aCursor) override
Set the cursor in the native panel.
Definition: cairo_gal.cpp:1654
unsigned char * m_wxOutput
wxImage compatible buffer
Definition: cairo_gal.h:510
bool m_isInitialized
Are Cairo image & surface ready to use.
Definition: cairo_gal.h:516
bool m_validCompositor
Compositor initialization flag.
Definition: cairo_gal.h:503
unsigned int m_mainBuffer
Handle to the main buffer.
Definition: cairo_gal.h:498
int m_stride
Stride value for Cairo.
Definition: cairo_gal.h:514
void onSetNativeCursor(wxSetCursorEvent &aEvent)
Give the correct cursor image when the native widget asks for it.
Definition: cairo_gal.cpp:1669
void EndDrawing() override
End the drawing, needs to be called for every new frame.
Definition: cairo_gal.cpp:1396
void allocateBitmaps()
Allocate the bitmaps for drawing.
Definition: cairo_gal.cpp:1571
void onPaint(wxPaintEvent &aEvent)
Paint event handler.
Definition: cairo_gal.cpp:1616
void StartNegativesLayer() override
Begins rendering in a new layer that will be copied to the main layer in EndNegativesLayer().
Definition: cairo_gal.cpp:983
void ResizeScreen(int aWidth, int aHeight) override
Resizes the canvas.
Definition: cairo_gal.cpp:1442
RENDER_TARGET GetTarget() const override
Get the currently used target for rendering.
Definition: cairo_gal.cpp:1508
void setCompositor()
Prepare the compositor.
Definition: cairo_gal.cpp:1600
unsigned char * m_bitmapBuffer
Storage of the Cairo image.
Definition: cairo_gal.h:513
wxEvtHandler * m_mouseListener
Mouse listener.
Definition: cairo_gal.h:507
void ClearTarget(RENDER_TARGET aTarget) override
Clear the target for rendering.
Definition: cairo_gal.cpp:1514
bool updatedGalDisplayOptions(const GAL_DISPLAY_OPTIONS &aOptions) override
Handle updating display options.
Definition: cairo_gal.cpp:1630
~CAIRO_GAL()
Return true if the GAL canvas is visible on the screen.
Definition: cairo_gal.cpp:1376
void BeginDrawing() override
Start/end drawing functions, draw calls can be only made in between the calls to BeginDrawing()/EndDr...
Definition: cairo_gal.cpp:1382
wxEvtHandler * m_paintListener
Paint listener.
Definition: cairo_gal.h:508
unsigned int m_tempBuffer
Handle to the temp buffer.
Definition: cairo_gal.h:500
RENDER_TARGET m_currentTarget
Current rendering target.
Definition: cairo_gal.h:502
wxWindow * m_parentWindow
Parent window.
Definition: cairo_gal.h:506
void EndDiffLayer() override
Ends rendering of a differential layer.
Definition: cairo_gal.cpp:977
std::shared_ptr< CAIRO_COMPOSITOR > m_compositor
Object for layers compositing.
Definition: cairo_gal.h:497
void deleteBitmaps()
Allocate the bitmaps for drawing.
Definition: cairo_gal.cpp:1590
void EndNegativesLayer() override
Ends rendering of a negatives layer and draws it to the main layer.
Definition: cairo_gal.cpp:990
void StartDiffLayer() override
Begins rendering of a differential layer.
Definition: cairo_gal.cpp:970
unsigned int m_savedBuffer
Handle to buffer to restore after rendering to temp buffer.
Definition: cairo_gal.h:501
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:102
double r
Red component.
Definition: color4d.h:372
double g
Green component.
Definition: color4d.h:373
double a
Alpha component.
Definition: color4d.h:375
static const COLOR4D BLACK
Definition: color4d.h:382
double b
Blue component.
Definition: color4d.h:374
CAIRO_ANTIALIASING_MODE cairo_antialiasing_mode
Abstract interface for drawing on a 2D-surface.
void SetGridColor(const COLOR4D &aGridColor)
Set the grid color.
virtual void SetLayerDepth(double aLayerDepth)
Set the depth of the layer (position on the z-axis)
bool IsCursorEnabled() const
Return information about cursor visibility.
MATRIX3x3D m_worldScreenMatrix
World transformation.
MATRIX3x3D m_screenWorldMatrix
Screen transformation.
bool m_axesEnabled
Should the axes be drawn.
float m_gridLineWidth
Line width of the grid.
VECTOR2I m_screenSize
Screen size in screen coordinates.
VECTOR2D m_depthRange
Range of the depth.
VECTOR2D ToScreen(const VECTOR2D &aPoint) const
Compute the point position in screen coordinates from given world coordinates.
GRID_STYLE m_gridStyle
Grid display style.
COLOR4D m_axesColor
Color of the axes.
float m_lineWidth
The line width.
virtual void SetLineWidth(float aLineWidth)
Set the line width.
VECTOR2D m_gridSize
The grid size.
COLOR4D getCursorColor() const
Get the actual cursor color to draw.
COLOR4D m_fillColor
The fill color.
double m_worldUnitLength
The unit length of the world coordinates [inch].
virtual bool updatedGalDisplayOptions(const GAL_DISPLAY_OPTIONS &aOptions)
Handle updating display options.
void SetAxesColor(const COLOR4D &aAxesColor)
Set the axes color.
VECTOR2D m_cursorPosition
Current cursor position (world coordinates)
int m_gridTick
Every tick line gets the double width.
double m_worldScale
The scale factor world->screen.
virtual bool SetNativeCursorStyle(KICURSOR aCursor)
Set the cursor in the native panel.
VECTOR2D m_gridOrigin
The grid origin.
KICURSOR m_currentNativeCursor
Current cursor.
bool m_fullscreenCursor
Shape of the cursor (fullscreen or small cross)
bool m_isFillEnabled
Is filling of graphic objects enabled ?
virtual void ComputeWorldScreenMatrix()
Compute the world <-> screen transformation matrix.
COLOR4D m_gridColor
Color of the grid.
COLOR4D m_strokeColor
The color of the outlines.
double computeMinGridSpacing() const
compute minimum grid spacing from the grid settings
bool m_isStrokeEnabled
Are the outlines stroked ?
GAL_DISPLAY_OPTIONS & m_options
bool m_gridVisibility
Should the grid be shown.
virtual void SetTarget(RENDER_TARGET aTarget)
Set the target for rendering.
T m_data[3][3]
Definition: matrix3x3.h:65
Represent a polyline containing arcs as well as line segments: A chain of connected line and/or arc s...
bool IsClosed() const override
int PointCount() const
Return the number of points (vertices) in this line chain.
const VECTOR2I & CPoint(int aIndex) const
Return a reference to a given point in the line chain.
Represent a set of closed polygons.
int OutlineCount() const
Return the number of vertices in a given outline/hole.
const SHAPE_LINE_CHAIN & COutline(int aIndex) const
@ BLUE
Definition: color4d.h:54
KICURSOR
Definition: cursors.h:34
#define SWAP(varA, condition, varB)
Swap the variables if a condition is met.
Definition: definitions.h:31
@ RADIANS_T
Definition: eda_angle.h:32
The Cairo implementation of the graphics abstraction layer.
Definition: color4d.cpp:246
@ SMALL_CROSS
Use small cross instead of dots for the grid.
@ DOTS
Use dots for the grid.
@ LINES
Use lines for the grid.
RENDER_TARGET
RENDER_TARGET: Possible rendering targets.
Definition: definitions.h:47
@ TARGET_NONCACHED
Auxiliary rendering target (noncached)
Definition: definitions.h:49
@ TARGET_TEMP
Temporary target for drawing in separate layer.
Definition: definitions.h:51
@ TARGET_CACHED
Main rendering target (cached)
Definition: definitions.h:48
@ TARGET_OVERLAY
Items that may change while the view stays the same (noncached)
Definition: definitions.h:50
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
Definition: eda_angle.h:418
void Refresh()
Update the board display after modifying it by a python script (note: it is automatically called by a...
const int scale
Type definition for an graphics group element.
Definition: cairo_gal.h:338
GRAPHICS_COMMAND m_Command
Command to execute.
Definition: cairo_gal.h:339
cairo_path_t * m_CairoPath
Pointer to a Cairo path.
Definition: cairo_gal.h:345
double DblArg[MAX_CAIRO_ARGUMENTS]
Arguments for Cairo commands.
Definition: cairo_gal.h:341
union KIGFX::CAIRO_GAL_BASE::GROUP_ELEMENT::@31 m_Argument
bool BoolArg
A bool argument.
Definition: cairo_gal.h:342
void RotatePoint(int *pX, int *pY, const EDA_ANGLE &aAngle)
Definition: trigo.cpp:183
double EuclideanNorm(const VECTOR2I &vector)
Definition: trigo.h:129
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:85
VECTOR2< double > VECTOR2D
Definition: vector2d.h:589
VECTOR2< int > VECTOR2I
Definition: vector2d.h:590
VECTOR2I ToVECTOR2I(const wxSize &aSize)
Definition: vector2wx.h:30