KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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-2023 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
129double CAIRO_GAL_BASE::angle_xform( 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
177double 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& aAngle )
343{
345
346 double startAngle = aStartAngle.AsRadians();
347 double endAngle = startAngle + aAngle.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& aAngle,
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, aAngle );
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 = startAngleS + aAngle.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 unsigned char r = bm_pix_buffer.GetRed( col, row );
542 unsigned char g = bm_pix_buffer.GetGreen( col, row );
543 unsigned char b = bm_pix_buffer.GetBlue( col, row );
544 unsigned char a = wxALPHA_OPAQUE;
545
546 if( bm_pix_buffer.HasAlpha() )
547 {
548 a = bm_pix_buffer.GetAlpha( col, row );
549
550 // ARGB32 format needs pre-multiplied alpha
551 r = uint32_t( r ) * a / 0xFF;
552 g = uint32_t( g ) * a / 0xFF;
553 b = uint32_t( b ) * a / 0xFF;
554 }
555 else if( bm_pix_buffer.HasMask() && (uint32_t)( r << 16 | g << 8 | b ) == mask_color )
556 {
557 a = wxALPHA_TRANSPARENT;
558 }
559
560 // Build the ARGB24 pixel:
561 uint32_t pixel = a << 24 | r << 16 | g << 8 | b;
562
563 // Write the pixel to the cairo image buffer:
564 uint32_t* pix_ptr = (uint32_t*) pix_buffer;
565 *pix_ptr = pixel;
566 pix_buffer += 4;
567 }
568 }
569
570 cairo_surface_mark_dirty( image );
571 cairo_set_source_surface( m_currentContext, image, 0, 0 );
572 cairo_paint_with_alpha( m_currentContext, alphaBlend );
573
574 // store the image handle so it can be destroyed later
575 m_imageSurfaces.push_back( image );
576
577 m_isElementAdded = true;
578
579 cairo_restore( m_currentContext );
580}
581
582
583void CAIRO_GAL_BASE::ResizeScreen( int aWidth, int aHeight )
584{
585 m_screenSize = VECTOR2I( aWidth, aHeight );
586}
587
588
590{
591 storePath();
592}
593
594
596{
597 cairo_set_source_rgb( m_currentContext, m_clearColor.r, m_clearColor.g, m_clearColor.b );
598 cairo_rectangle( m_currentContext, 0.0, 0.0, m_screenSize.x, m_screenSize.y );
599 cairo_fill( m_currentContext );
600}
601
602
603void CAIRO_GAL_BASE::SetIsFill( bool aIsFillEnabled )
604{
605 storePath();
606 m_isFillEnabled = aIsFillEnabled;
607
608 if( m_isGrouping )
609 {
610 GROUP_ELEMENT groupElement;
611 groupElement.m_Command = CMD_SET_FILL;
612 groupElement.m_Argument.BoolArg = aIsFillEnabled;
613 m_currentGroup->push_back( groupElement );
614 }
615}
616
617
618void CAIRO_GAL_BASE::SetIsStroke( bool aIsStrokeEnabled )
619{
620 storePath();
621 m_isStrokeEnabled = aIsStrokeEnabled;
622
623 if( m_isGrouping )
624 {
625 GROUP_ELEMENT groupElement;
626 groupElement.m_Command = CMD_SET_STROKE;
627 groupElement.m_Argument.BoolArg = aIsStrokeEnabled;
628 m_currentGroup->push_back( groupElement );
629 }
630}
631
632
634{
635 storePath();
636 m_strokeColor = aColor;
637
638 if( m_isGrouping )
639 {
640 GROUP_ELEMENT groupElement;
641 groupElement.m_Command = CMD_SET_STROKECOLOR;
642 groupElement.m_Argument.DblArg[0] = m_strokeColor.r;
643 groupElement.m_Argument.DblArg[1] = m_strokeColor.g;
644 groupElement.m_Argument.DblArg[2] = m_strokeColor.b;
645 groupElement.m_Argument.DblArg[3] = m_strokeColor.a;
646 m_currentGroup->push_back( groupElement );
647 }
648}
649
650
652{
653 storePath();
654 m_fillColor = aColor;
655
656 if( m_isGrouping )
657 {
658 GROUP_ELEMENT groupElement;
659 groupElement.m_Command = CMD_SET_FILLCOLOR;
660 groupElement.m_Argument.DblArg[0] = m_fillColor.r;
661 groupElement.m_Argument.DblArg[1] = m_fillColor.g;
662 groupElement.m_Argument.DblArg[2] = m_fillColor.b;
663 groupElement.m_Argument.DblArg[3] = m_fillColor.a;
664 m_currentGroup->push_back( groupElement );
665 }
666}
667
668
669void CAIRO_GAL_BASE::SetLineWidth( float aLineWidth )
670{
671 storePath();
672 GAL::SetLineWidth( aLineWidth );
673
674 if( m_isGrouping )
675 {
676 GROUP_ELEMENT groupElement;
677 groupElement.m_Command = CMD_SET_LINE_WIDTH;
678 groupElement.m_Argument.DblArg[0] = aLineWidth;
679 m_currentGroup->push_back( groupElement );
680 }
681 else
682 {
683 m_lineWidth = aLineWidth;
684 }
685}
686
687
688void CAIRO_GAL_BASE::SetLayerDepth( double aLayerDepth )
689{
690 super::SetLayerDepth( aLayerDepth );
691 storePath();
692}
693
694
695void CAIRO_GAL_BASE::Transform( const MATRIX3x3D& aTransformation )
696{
697 cairo_matrix_t cairoTransformation, newXform;
698
699 cairo_matrix_init( &cairoTransformation, aTransformation.m_data[0][0],
700 aTransformation.m_data[1][0], aTransformation.m_data[0][1],
701 aTransformation.m_data[1][1], aTransformation.m_data[0][2],
702 aTransformation.m_data[1][2] );
703
704 cairo_matrix_multiply( &newXform, &m_currentXform, &cairoTransformation );
705 m_currentXform = newXform;
707}
708
709
710void CAIRO_GAL_BASE::Rotate( double aAngle )
711{
712 storePath();
713
714 if( m_isGrouping )
715 {
716 GROUP_ELEMENT groupElement;
717 groupElement.m_Command = CMD_ROTATE;
718 groupElement.m_Argument.DblArg[0] = aAngle;
719 m_currentGroup->push_back( groupElement );
720 }
721 else
722 {
723 cairo_matrix_rotate( &m_currentXform, aAngle );
725 }
726}
727
728
729void CAIRO_GAL_BASE::Translate( const VECTOR2D& aTranslation )
730{
731 storePath();
732
733 if( m_isGrouping )
734 {
735 GROUP_ELEMENT groupElement;
736 groupElement.m_Command = CMD_TRANSLATE;
737 groupElement.m_Argument.DblArg[0] = aTranslation.x;
738 groupElement.m_Argument.DblArg[1] = aTranslation.y;
739 m_currentGroup->push_back( groupElement );
740 }
741 else
742 {
743 cairo_matrix_translate( &m_currentXform, aTranslation.x, aTranslation.y );
745 }
746}
747
748
749void CAIRO_GAL_BASE::Scale( const VECTOR2D& aScale )
750{
751 storePath();
752
753 if( m_isGrouping )
754 {
755 GROUP_ELEMENT groupElement;
756 groupElement.m_Command = CMD_SCALE;
757 groupElement.m_Argument.DblArg[0] = aScale.x;
758 groupElement.m_Argument.DblArg[1] = aScale.y;
759 m_currentGroup->push_back( groupElement );
760 }
761 else
762 {
763 cairo_matrix_scale( &m_currentXform, aScale.x, aScale.y );
765 }
766}
767
768
770{
771 storePath();
772
773 if( m_isGrouping )
774 {
775 GROUP_ELEMENT groupElement;
776 groupElement.m_Command = CMD_SAVE;
777 m_currentGroup->push_back( groupElement );
778 }
779 else
780 {
781 m_xformStack.push_back( m_currentXform );
783 }
784}
785
786
788{
789 storePath();
790
791 if( m_isGrouping )
792 {
793 GROUP_ELEMENT groupElement;
794 groupElement.m_Command = CMD_RESTORE;
795 m_currentGroup->push_back( groupElement );
796 }
797 else
798 {
799 if( !m_xformStack.empty() )
800 {
802 m_xformStack.pop_back();
804 }
805 }
806}
807
808
810{
811 // If the grouping is started: the actual path is stored in the group, when
812 // a attribute was changed or when grouping stops with the end group method.
813 storePath();
814
815 GROUP group;
816 int groupNumber = getNewGroupNumber();
817 m_groups.insert( std::make_pair( groupNumber, group ) );
818 m_currentGroup = &m_groups[groupNumber];
819 m_isGrouping = true;
820
821 return groupNumber;
822}
823
824
826{
827 storePath();
828 m_isGrouping = false;
829}
830
831
832void CAIRO_GAL_BASE::DrawGroup( int aGroupNumber )
833{
834 // This method implements a small Virtual Machine - all stored commands
835 // are executed; nested calling is also possible
836
837 storePath();
838
839 for( auto it = m_groups[aGroupNumber].begin(); it != m_groups[aGroupNumber].end(); ++it )
840 {
841 switch( it->m_Command )
842 {
843 case CMD_SET_FILL:
844 m_isFillEnabled = it->m_Argument.BoolArg;
845 break;
846
847 case CMD_SET_STROKE:
848 m_isStrokeEnabled = it->m_Argument.BoolArg;
849 break;
850
852 m_fillColor = COLOR4D( it->m_Argument.DblArg[0], it->m_Argument.DblArg[1],
853 it->m_Argument.DblArg[2], it->m_Argument.DblArg[3] );
854 break;
855
857 m_strokeColor = COLOR4D( it->m_Argument.DblArg[0], it->m_Argument.DblArg[1],
858 it->m_Argument.DblArg[2], it->m_Argument.DblArg[3] );
859 break;
860
862 {
863 // Make lines appear at least 1 pixel wide, no matter of zoom
864 double x = 1.0, y = 1.0;
865 cairo_device_to_user_distance( m_currentContext, &x, &y );
866 double minWidth = std::min( fabs( x ), fabs( y ) );
867 cairo_set_line_width( m_currentContext,
868 std::max( it->m_Argument.DblArg[0], minWidth ) );
869 break;
870 }
871
872
873 case CMD_STROKE_PATH:
874 cairo_set_source_rgba( m_currentContext, m_strokeColor.r, m_strokeColor.g,
876 cairo_append_path( m_currentContext, it->m_CairoPath );
877 cairo_stroke( m_currentContext );
878 break;
879
880 case CMD_FILL_PATH:
881 cairo_set_source_rgba( m_currentContext, m_fillColor.r, m_fillColor.g, m_fillColor.b,
883 cairo_append_path( m_currentContext, it->m_CairoPath );
884 cairo_fill( m_currentContext );
885 break;
886
887 /*
888 case CMD_TRANSFORM:
889 cairo_matrix_t matrix;
890 cairo_matrix_init( &matrix, it->argument.DblArg[0], it->argument.DblArg[1],
891 it->argument.DblArg[2], it->argument.DblArg[3],
892 it->argument.DblArg[4], it->argument.DblArg[5] );
893 cairo_transform( m_currentContext, &matrix );
894 break;
895 */
896
897 case CMD_ROTATE:
898 cairo_rotate( m_currentContext, it->m_Argument.DblArg[0] );
899 break;
900
901 case CMD_TRANSLATE:
902 cairo_translate( m_currentContext, it->m_Argument.DblArg[0], it->m_Argument.DblArg[1] );
903 break;
904
905 case CMD_SCALE:
906 cairo_scale( m_currentContext, it->m_Argument.DblArg[0], it->m_Argument.DblArg[1] );
907 break;
908
909 case CMD_SAVE:
910 cairo_save( m_currentContext );
911 break;
912
913 case CMD_RESTORE:
914 cairo_restore( m_currentContext );
915 break;
916
917 case CMD_CALL_GROUP:
918 DrawGroup( it->m_Argument.IntArg );
919 break;
920 }
921 }
922}
923
924
925void CAIRO_GAL_BASE::ChangeGroupColor( int aGroupNumber, const COLOR4D& aNewColor )
926{
927 storePath();
928
929 for( auto it = m_groups[aGroupNumber].begin(); it != m_groups[aGroupNumber].end(); ++it )
930 {
931 if( it->m_Command == CMD_SET_FILLCOLOR || it->m_Command == CMD_SET_STROKECOLOR )
932 {
933 it->m_Argument.DblArg[0] = aNewColor.r;
934 it->m_Argument.DblArg[1] = aNewColor.g;
935 it->m_Argument.DblArg[2] = aNewColor.b;
936 it->m_Argument.DblArg[3] = aNewColor.a;
937 }
938 }
939}
940
941
942void CAIRO_GAL_BASE::ChangeGroupDepth( int aGroupNumber, int aDepth )
943{
944 // Cairo does not have any possibilities to change the depth coordinate of stored items,
945 // it depends only on the order of drawing
946}
947
948
949void CAIRO_GAL_BASE::DeleteGroup( int aGroupNumber )
950{
951 storePath();
952
953 // Delete the Cairo paths
954 std::deque<GROUP_ELEMENT>::iterator it, end;
955
956 for( it = m_groups[aGroupNumber].begin(), end = m_groups[aGroupNumber].end(); it != end; ++it )
957 {
958 if( it->m_Command == CMD_FILL_PATH || it->m_Command == CMD_STROKE_PATH )
959 cairo_path_destroy( it->m_CairoPath );
960 }
961
962 // Delete the group
963 m_groups.erase( aGroupNumber );
964}
965
966
968{
969 for( auto it = m_groups.begin(); it != m_groups.end(); )
970 DeleteGroup( ( it++ )->first );
971}
972
973
975{
976 cairo_set_operator( m_currentContext, aSetting ? CAIRO_OPERATOR_CLEAR : CAIRO_OPERATOR_OVER );
977}
978
979
981{
984}
985
986
988{
989 m_compositor->DrawBuffer( m_tempBuffer, m_mainBuffer, CAIRO_OPERATOR_ADD );
990}
991
992
994{
997}
998
999
1001{
1002 m_compositor->DrawBuffer( m_tempBuffer, m_mainBuffer, CAIRO_OPERATOR_OVER );
1003}
1004
1005
1006void CAIRO_GAL_BASE::DrawCursor( const VECTOR2D& aCursorPosition )
1007{
1008 m_cursorPosition = aCursorPosition;
1009}
1010
1011
1013{
1014}
1015
1016
1018{
1019 for( _cairo_surface* imageSurface : m_imageSurfaces )
1020 cairo_surface_destroy( imageSurface );
1021
1022 m_imageSurfaces.clear();
1023
1024 ClearScreen();
1025
1026 // Compute the world <-> screen transformations
1028
1029 cairo_matrix_init( &m_cairoWorldScreenMatrix, m_worldScreenMatrix.m_data[0][0],
1033
1034 // we work in screen-space coordinates and do the transforms outside.
1035 cairo_identity_matrix( m_context );
1036
1037 cairo_matrix_init_identity( &m_currentXform );
1038
1039 // Start drawing with a new path
1040 cairo_new_path( m_context );
1041 m_isElementAdded = true;
1042
1044
1045 m_lineWidth = 0;
1046}
1047
1048
1049void CAIRO_GAL_BASE::drawAxes( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint )
1050{
1051 syncLineWidth();
1052
1053 VECTOR2D p0 = roundp( xform( aStartPoint ) );
1054 VECTOR2D p1 = roundp( xform( aEndPoint ) );
1055 VECTOR2D org = roundp( xform( VECTOR2D( 0.0, 0.0 ) ) ); // Axis origin = 0,0 coord
1056
1057 cairo_set_source_rgba( m_currentContext, m_axesColor.r, m_axesColor.g, m_axesColor.b,
1058 m_axesColor.a );
1059 cairo_move_to( m_currentContext, p0.x, org.y );
1060 cairo_line_to( m_currentContext, p1.x, org.y );
1061 cairo_move_to( m_currentContext, org.x, p0.y );
1062 cairo_line_to( m_currentContext, org.x, p1.y );
1063 cairo_stroke( m_currentContext );
1064}
1065
1066
1067void CAIRO_GAL_BASE::drawGridLine( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint )
1068{
1069 syncLineWidth();
1070 VECTOR2D p0 = roundp( xform( aStartPoint ) );
1071 VECTOR2D p1 = roundp( xform( aEndPoint ) );
1072
1073 cairo_set_source_rgba( m_currentContext, m_gridColor.r, m_gridColor.g, m_gridColor.b,
1074 m_gridColor.a );
1075 cairo_move_to( m_currentContext, p0.x, p0.y );
1076 cairo_line_to( m_currentContext, p1.x, p1.y );
1077 cairo_stroke( m_currentContext );
1078}
1079
1080
1082{
1083 syncLineWidth();
1084 VECTOR2D offset( 0, 0 );
1085 double size = 2.0 * m_lineWidthInPixels + 0.5;
1086
1087 VECTOR2D p0 = roundp( xform( aPoint ) ) - VECTOR2D( size, 0 ) + offset;
1088 VECTOR2D p1 = roundp( xform( aPoint ) ) + VECTOR2D( size, 0 ) + offset;
1089 VECTOR2D p2 = roundp( xform( aPoint ) ) - VECTOR2D( 0, size ) + offset;
1090 VECTOR2D p3 = roundp( xform( aPoint ) ) + VECTOR2D( 0, size ) + offset;
1091
1092 cairo_set_source_rgba( m_currentContext, m_gridColor.r, m_gridColor.g, m_gridColor.b,
1093 m_gridColor.a );
1094 cairo_move_to( m_currentContext, p0.x, p0.y );
1095 cairo_line_to( m_currentContext, p1.x, p1.y );
1096 cairo_move_to( m_currentContext, p2.x, p2.y );
1097 cairo_line_to( m_currentContext, p3.x, p3.y );
1098 cairo_stroke( m_currentContext );
1099}
1100
1101
1102void CAIRO_GAL_BASE::drawGridPoint( const VECTOR2D& aPoint, double aWidth, double aHeight )
1103{
1104 VECTOR2D p = roundp( xform( aPoint ) );
1105
1106 double sw = std::max( 1.0, aWidth );
1107 double sh = std::max( 1.0, aHeight );
1108
1109 cairo_set_source_rgba( m_currentContext, m_gridColor.r, m_gridColor.g, m_gridColor.b,
1110 m_gridColor.a );
1111 cairo_rectangle( m_currentContext, p.x - std::floor( sw / 2 ) - 0.5,
1112 p.y - std::floor( sh / 2 ) - 0.5, sw, sh );
1113
1114 cairo_fill( m_currentContext );
1115}
1116
1117
1119{
1120 if( m_isFillEnabled )
1121 {
1122 cairo_set_source_rgba( m_currentContext, m_fillColor.r, m_fillColor.g, m_fillColor.b,
1123 m_fillColor.a );
1124
1125 if( m_isStrokeEnabled )
1126 {
1127 cairo_set_line_width( m_currentContext, m_lineWidthInPixels );
1128 cairo_fill_preserve( m_currentContext );
1129 }
1130 else
1131 {
1132 cairo_fill( m_currentContext );
1133 }
1134 }
1135
1136 if( m_isStrokeEnabled )
1137 {
1138 cairo_set_line_width( m_currentContext, m_lineWidthInPixels );
1139 cairo_set_source_rgba( m_currentContext, m_strokeColor.r, m_strokeColor.g, m_strokeColor.b,
1140 m_strokeColor.a );
1141 cairo_stroke( m_currentContext );
1142 }
1143}
1144
1145
1147{
1148 if( m_isElementAdded )
1149 {
1150 m_isElementAdded = false;
1151
1152 if( !m_isGrouping )
1153 {
1154 if( m_isFillEnabled )
1155 {
1156 cairo_set_source_rgba( m_currentContext, m_fillColor.r, m_fillColor.g,
1158 cairo_fill_preserve( m_currentContext );
1159 }
1160
1161 if( m_isStrokeEnabled )
1162 {
1163 cairo_set_source_rgba( m_currentContext, m_strokeColor.r, m_strokeColor.g,
1165 cairo_stroke_preserve( m_currentContext );
1166 }
1167 }
1168 else
1169 {
1170 // Copy the actual path, append it to the global path list
1171 // then check, if the path needs to be stroked/filled and
1172 // add this command to the group list;
1173 if( m_isStrokeEnabled )
1174 {
1175 GROUP_ELEMENT groupElement;
1176 groupElement.m_CairoPath = cairo_copy_path( m_currentContext );
1177 groupElement.m_Command = CMD_STROKE_PATH;
1178 m_currentGroup->push_back( groupElement );
1179 }
1180
1181 if( m_isFillEnabled )
1182 {
1183 GROUP_ELEMENT groupElement;
1184 groupElement.m_CairoPath = cairo_copy_path( m_currentContext );
1185 groupElement.m_Command = CMD_FILL_PATH;
1186 m_currentGroup->push_back( groupElement );
1187 }
1188 }
1189
1190 cairo_new_path( m_currentContext );
1191 }
1192}
1193
1194
1195void CAIRO_GAL_BASE::blitCursor( wxMemoryDC& clientDC )
1196{
1197 if( !IsCursorEnabled() )
1198 return;
1199
1201 const COLOR4D cColor = getCursorColor();
1202 const int cursorSize = m_fullscreenCursor ? 8000 : 80;
1203
1204 wxColour color( cColor.r * cColor.a * 255, cColor.g * cColor.a * 255, cColor.b * cColor.a * 255,
1205 255 );
1206 clientDC.SetPen( wxPen( color ) );
1207 clientDC.DrawLine( p.x - cursorSize / 2, p.y, p.x + cursorSize / 2, p.y );
1208 clientDC.DrawLine( p.x, p.y - cursorSize / 2, p.x, p.y + cursorSize / 2 );
1209}
1210
1211
1212void CAIRO_GAL_BASE::drawPoly( const std::deque<VECTOR2D>& aPointList )
1213{
1214 wxCHECK( aPointList.size() > 1, /* void */ );
1215
1216 // Iterate over the point list and draw the segments
1217 std::deque<VECTOR2D>::const_iterator it = aPointList.begin();
1218
1219 syncLineWidth();
1220
1221 const VECTOR2D p = roundp( xform( it->x, it->y ) );
1222
1223 cairo_move_to( m_currentContext, p.x, p.y );
1224
1225 for( ++it; it != aPointList.end(); ++it )
1226 {
1227 const VECTOR2D p2 = roundp( xform( it->x, it->y ) );
1228
1229 cairo_line_to( m_currentContext, p2.x, p2.y );
1230 }
1231
1232 flushPath();
1233 m_isElementAdded = true;
1234}
1235
1236
1237void CAIRO_GAL_BASE::drawPoly( const std::vector<VECTOR2D>& aPointList )
1238{
1239 wxCHECK( aPointList.size() > 1, /* void */ );
1240
1241 // Iterate over the point list and draw the segments
1242 std::vector<VECTOR2D>::const_iterator it = aPointList.begin();
1243
1244 syncLineWidth();
1245
1246 const VECTOR2D p = roundp( xform( it->x, it->y ) );
1247
1248 cairo_move_to( m_currentContext, p.x, p.y );
1249
1250 for( ++it; it != aPointList.end(); ++it )
1251 {
1252 const VECTOR2D p2 = roundp( xform( it->x, it->y ) );
1253
1254 cairo_line_to( m_currentContext, p2.x, p2.y );
1255 }
1256
1257 flushPath();
1258 m_isElementAdded = true;
1259}
1260
1261
1262void CAIRO_GAL_BASE::drawPoly( const VECTOR2D aPointList[], int aListSize )
1263{
1264 wxCHECK( aListSize > 1, /* void */ );
1265
1266 // Iterate over the point list and draw the segments
1267 const VECTOR2D* ptr = aPointList;
1268
1269 syncLineWidth();
1270
1271 const VECTOR2D p = roundp( xform( ptr->x, ptr->y ) );
1272 cairo_move_to( m_currentContext, p.x, p.y );
1273
1274 for( int i = 1; i < aListSize; ++i )
1275 {
1276 ++ptr;
1277 const VECTOR2D p2 = roundp( xform( ptr->x, ptr->y ) );
1278 cairo_line_to( m_currentContext, p2.x, p2.y );
1279 }
1280
1281 flushPath();
1282 m_isElementAdded = true;
1283}
1284
1285
1287{
1288 wxCHECK( aLineChain.PointCount() > 1, /* void */ );
1289
1290 syncLineWidth();
1291
1292 auto numPoints = aLineChain.PointCount();
1293
1294 if( aLineChain.IsClosed() )
1295 numPoints += 1;
1296
1297 const VECTOR2I start = aLineChain.CPoint( 0 );
1298 const VECTOR2D p = roundp( xform( start.x, start.y ) );
1299 cairo_move_to( m_currentContext, p.x, p.y );
1300
1301 for( int i = 1; i < numPoints; ++i )
1302 {
1303 const VECTOR2I& pw = aLineChain.CPoint( i );
1304 const VECTOR2D ps = roundp( xform( pw.x, pw.y ) );
1305 cairo_line_to( m_currentContext, ps.x, ps.y );
1306 }
1307
1308 flushPath();
1309 m_isElementAdded = true;
1310}
1311
1312
1314{
1315 wxASSERT_MSG( m_groups.size() < std::numeric_limits<unsigned int>::max(),
1316 wxT( "There are no free slots to store a group" ) );
1317
1318 while( m_groups.find( m_groupCounter ) != m_groups.end() )
1320
1321 return m_groupCounter++;
1322}
1323
1324
1325CAIRO_GAL::CAIRO_GAL( GAL_DISPLAY_OPTIONS& aDisplayOptions, wxWindow* aParent,
1326 wxEvtHandler* aMouseListener, wxEvtHandler* aPaintListener,
1327 const wxString& aName ) :
1328 CAIRO_GAL_BASE( aDisplayOptions ),
1329 wxWindow( aParent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxEXPAND, aName )
1330{
1331 // Initialise compositing state
1332 m_mainBuffer = 0;
1333 m_overlayBuffer = 0;
1334 m_tempBuffer = 0;
1335 m_savedBuffer = 0;
1336 m_validCompositor = false;
1339
1340 m_bitmapBuffer = nullptr;
1341 m_wxOutput = nullptr;
1342
1343 m_parentWindow = aParent;
1344 m_mouseListener = aMouseListener;
1345 m_paintListener = aPaintListener;
1346
1347 // Connect the native cursor handler
1348 Connect( wxEVT_SET_CURSOR, wxSetCursorEventHandler( CAIRO_GAL::onSetNativeCursor ), nullptr,
1349 this );
1350
1351 // Connecting the event handlers
1352 Connect( wxEVT_PAINT, wxPaintEventHandler( CAIRO_GAL::onPaint ) );
1353
1354 // Mouse events are skipped to the parent
1355 Connect( wxEVT_MOTION, wxMouseEventHandler( CAIRO_GAL::skipMouseEvent ) );
1356 Connect( wxEVT_LEFT_DOWN, wxMouseEventHandler( CAIRO_GAL::skipMouseEvent ) );
1357 Connect( wxEVT_LEFT_UP, wxMouseEventHandler( CAIRO_GAL::skipMouseEvent ) );
1358 Connect( wxEVT_LEFT_DCLICK, wxMouseEventHandler( CAIRO_GAL::skipMouseEvent ) );
1359 Connect( wxEVT_MIDDLE_DOWN, wxMouseEventHandler( CAIRO_GAL::skipMouseEvent ) );
1360 Connect( wxEVT_MIDDLE_UP, wxMouseEventHandler( CAIRO_GAL::skipMouseEvent ) );
1361 Connect( wxEVT_MIDDLE_DCLICK, wxMouseEventHandler( CAIRO_GAL::skipMouseEvent ) );
1362 Connect( wxEVT_RIGHT_DOWN, wxMouseEventHandler( CAIRO_GAL::skipMouseEvent ) );
1363 Connect( wxEVT_RIGHT_UP, wxMouseEventHandler( CAIRO_GAL::skipMouseEvent ) );
1364 Connect( wxEVT_RIGHT_DCLICK, wxMouseEventHandler( CAIRO_GAL::skipMouseEvent ) );
1365 Connect( wxEVT_AUX1_DOWN, wxMouseEventHandler( CAIRO_GAL::skipMouseEvent ) );
1366 Connect( wxEVT_AUX1_UP, wxMouseEventHandler( CAIRO_GAL::skipMouseEvent ) );
1367 Connect( wxEVT_AUX1_DCLICK, wxMouseEventHandler( CAIRO_GAL::skipMouseEvent ) );
1368 Connect( wxEVT_AUX2_DOWN, wxMouseEventHandler( CAIRO_GAL::skipMouseEvent ) );
1369 Connect( wxEVT_AUX2_UP, wxMouseEventHandler( CAIRO_GAL::skipMouseEvent ) );
1370 Connect( wxEVT_AUX2_DCLICK, wxMouseEventHandler( CAIRO_GAL::skipMouseEvent ) );
1371 Connect( wxEVT_MOUSEWHEEL, wxMouseEventHandler( CAIRO_GAL::skipMouseEvent ) );
1372#if defined _WIN32 || defined _WIN64
1373 Connect( wxEVT_ENTER_WINDOW, wxMouseEventHandler( CAIRO_GAL::skipMouseEvent ) );
1374#endif
1375
1376 SetSize( aParent->GetClientSize() );
1377 m_screenSize = ToVECTOR2I( aParent->GetClientSize() );
1378
1379 // Allocate memory for pixel storage
1381
1382 m_isInitialized = false;
1383}
1384
1385
1387{
1388 deleteBitmaps();
1389}
1390
1391
1393{
1394 initSurface();
1395
1397
1398 if( !m_validCompositor )
1399 setCompositor();
1400
1401 m_compositor->SetMainContext( m_context );
1402 m_compositor->SetBuffer( m_mainBuffer );
1403}
1404
1405
1407{
1409
1410 // Merge buffers on the screen
1411 m_compositor->DrawBuffer( m_mainBuffer );
1412 m_compositor->DrawBuffer( m_overlayBuffer );
1413
1414 // Now translate the raw context data from the format stored
1415 // by cairo into a format understood by wxImage.
1416 int height = m_screenSize.y;
1417 int stride = m_stride;
1418
1419 unsigned char* srcRow = m_bitmapBuffer;
1420 unsigned char* dst = m_wxOutput;
1421
1422 for( int y = 0; y < height; y++ )
1423 {
1424 for( int x = 0; x < stride; x += 4 )
1425 {
1426 const unsigned char* src = srcRow + x;
1427
1428#if defined( __BYTE_ORDER__ ) && ( __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ )
1429 // XRGB
1430 dst[0] = src[1];
1431 dst[1] = src[2];
1432 dst[2] = src[3];
1433#else
1434 // BGRX
1435 dst[0] = src[2];
1436 dst[1] = src[1];
1437 dst[2] = src[0];
1438#endif
1439 dst += 3;
1440 }
1441
1442 srcRow += stride;
1443 }
1444
1445 wxImage img( m_wxBufferWidth, m_screenSize.y, m_wxOutput, true );
1446 wxBitmap bmp( img );
1447 wxMemoryDC mdc( bmp );
1448 wxClientDC clientDC( this );
1449
1450 // Now it is the time to blit the mouse cursor
1451 blitCursor( mdc );
1452 clientDC.Blit( 0, 0, m_screenSize.x, m_screenSize.y, &mdc, 0, 0, wxCOPY );
1453
1454 deinitSurface();
1455}
1456
1457
1458void CAIRO_GAL::PostPaint( wxPaintEvent& aEvent )
1459{
1460 // posts an event to m_paint_listener to ask for redraw the canvas.
1461 if( m_paintListener )
1462 wxPostEvent( m_paintListener, aEvent );
1463}
1464
1465
1466void CAIRO_GAL::ResizeScreen( int aWidth, int aHeight )
1467{
1468 CAIRO_GAL_BASE::ResizeScreen( aWidth, aHeight );
1469
1470 // Recreate the bitmaps
1471 deleteBitmaps();
1473
1474 if( m_validCompositor )
1475 m_compositor->Resize( aWidth, aHeight );
1476
1477 m_validCompositor = false;
1478
1479 SetSize( wxSize( aWidth, aHeight ) );
1480}
1481
1482
1483bool CAIRO_GAL::Show( bool aShow )
1484{
1485 bool s = wxWindow::Show( aShow );
1486
1487 if( aShow )
1488 wxWindow::Raise();
1489
1490 return s;
1491}
1492
1493
1495{
1496 initSurface();
1498}
1499
1500
1502{
1504 deinitSurface();
1505}
1506
1507
1509{
1510 // If the compositor is not set, that means that there is a recaching process going on
1511 // and we do not need the compositor now
1512 if( !m_validCompositor )
1513 return;
1514
1515 // Cairo grouping prevents display of overlapping items on the same layer in the lighter color
1516 if( m_isInitialized )
1517 storePath();
1518
1519 switch( aTarget )
1520 {
1521 default:
1522 case TARGET_CACHED:
1523 case TARGET_NONCACHED: m_compositor->SetBuffer( m_mainBuffer ); break;
1524 case TARGET_OVERLAY: m_compositor->SetBuffer( m_overlayBuffer ); break;
1525 case TARGET_TEMP: m_compositor->SetBuffer( m_tempBuffer ); break;
1526 }
1527
1528 m_currentTarget = aTarget;
1529}
1530
1531
1533{
1534 return m_currentTarget;
1535}
1536
1537
1539{
1540 // Save the current state
1541 unsigned int currentBuffer = m_compositor->GetBuffer();
1542
1543 switch( aTarget )
1544 {
1545 // Cached and noncached items are rendered to the same buffer
1546 default:
1547 case TARGET_CACHED:
1548 case TARGET_NONCACHED: m_compositor->SetBuffer( m_mainBuffer ); break;
1549 case TARGET_OVERLAY: m_compositor->SetBuffer( m_overlayBuffer ); break;
1550 case TARGET_TEMP: m_compositor->SetBuffer( m_tempBuffer ); break;
1551 }
1552
1553 m_compositor->ClearBuffer( COLOR4D::BLACK );
1554
1555 // Restore the previous state
1556 m_compositor->SetBuffer( currentBuffer );
1557}
1558
1559
1561{
1562 if( m_isInitialized )
1563 return;
1564
1565 m_surface = cairo_image_surface_create_for_data( m_bitmapBuffer, GAL_FORMAT, m_wxBufferWidth,
1567
1568 m_context = cairo_create( m_surface );
1569
1570#ifdef DEBUG
1571 cairo_status_t status = cairo_status( m_context );
1572 wxASSERT_MSG( status == CAIRO_STATUS_SUCCESS, wxT( "Cairo context creation error" ) );
1573#endif /* DEBUG */
1574
1576
1577 m_isInitialized = true;
1578}
1579
1580
1582{
1583 if( !m_isInitialized )
1584 return;
1585
1586 cairo_destroy( m_context );
1587 m_context = nullptr;
1588 cairo_surface_destroy( m_surface );
1589 m_surface = nullptr;
1590
1591 m_isInitialized = false;
1592}
1593
1594
1596{
1598
1599 // Create buffer, use the system independent Cairo context backend
1600 m_stride = cairo_format_stride_for_width( GAL_FORMAT, m_wxBufferWidth );
1602
1603 wxASSERT( m_bitmapBuffer == nullptr );
1604 m_bitmapBuffer = new unsigned char[m_bufferSize];
1605
1606 wxASSERT( m_wxOutput == nullptr );
1607 m_wxOutput = new unsigned char[m_wxBufferWidth * 3 * m_screenSize.y];
1608}
1609
1610
1612{
1613 delete[] m_bitmapBuffer;
1614 m_bitmapBuffer = nullptr;
1615
1616 delete[] m_wxOutput;
1617 m_wxOutput = nullptr;
1618}
1619
1620
1622{
1623 // Recreate the compositor with the new Cairo context
1626 m_compositor->SetAntialiasingMode( m_options.cairo_antialiasing_mode );
1627
1628 // Prepare buffers
1629 m_mainBuffer = m_compositor->CreateBuffer();
1630 m_overlayBuffer = m_compositor->CreateBuffer();
1631 m_tempBuffer = m_compositor->CreateBuffer();
1632
1633 m_validCompositor = true;
1634}
1635
1636
1637void CAIRO_GAL::onPaint( wxPaintEvent& aEvent )
1638{
1639 PostPaint( aEvent );
1640}
1641
1642
1643void CAIRO_GAL::skipMouseEvent( wxMouseEvent& aEvent )
1644{
1645 // Post the mouse event to the event listener registered in constructor, if any
1646 if( m_mouseListener )
1647 wxPostEvent( m_mouseListener, aEvent );
1648}
1649
1650
1652{
1653 bool refresh = false;
1654
1655 if( m_validCompositor &&
1656 aOptions.cairo_antialiasing_mode != m_compositor->GetAntialiasingMode() )
1657 {
1658 m_compositor->SetAntialiasingMode( m_options.cairo_antialiasing_mode );
1659 m_validCompositor = false;
1660 deinitSurface();
1661
1662 refresh = true;
1663 }
1664
1665 if( super::updatedGalDisplayOptions( aOptions ) )
1666 {
1667 Refresh();
1668 refresh = true;
1669 }
1670
1671 return refresh;
1672}
1673
1674
1676{
1677 // Store the current cursor type and get the wxCursor for it
1678 if( !GAL::SetNativeCursorStyle( aCursor ) )
1679 return false;
1680
1682
1683 // Update the cursor in the wx control
1684 wxWindow::SetCursor( m_currentwxCursor );
1685
1686 return true;
1687}
1688
1689
1690void CAIRO_GAL::onSetNativeCursor( wxSetCursorEvent& aEvent )
1691{
1692 aEvent.SetCursor( m_currentwxCursor );
1693}
1694
1695
1697{
1699
1700 // Draw the grid
1701 // For the drawing the start points, end points and increments have
1702 // to be calculated in world coordinates
1703 VECTOR2D worldStartPoint = m_screenWorldMatrix * VECTOR2D( 0.0, 0.0 );
1705
1706 // Compute the line marker or point radius of the grid
1707 // Note: generic grids can't handle sub-pixel lines without
1708 // either losing fine/course distinction or having some dots
1709 // fail to render
1710 float marker = std::fmax( 1.0f, m_gridLineWidth ) / m_worldScale;
1711 float doubleMarker = 2.0f * marker;
1712
1713 // Draw axes if desired
1714 if( m_axesEnabled )
1715 {
1716 SetLineWidth( marker );
1717 drawAxes( worldStartPoint, worldEndPoint );
1718 }
1719
1720 if( !m_gridVisibility || m_gridSize.x == 0 || m_gridSize.y == 0 )
1721 return;
1722
1723 VECTOR2D gridScreenSize( m_gridSize );
1724
1725 double gridThreshold = KiROUND( computeMinGridSpacing() / m_worldScale );
1726
1728 gridThreshold *= 2.0;
1729
1730 // If we cannot display the grid density, scale down by a tick size and
1731 // try again. Eventually, we get some representation of the grid
1732 while( std::min( gridScreenSize.x, gridScreenSize.y ) <= gridThreshold )
1733 {
1734 gridScreenSize = gridScreenSize * static_cast<double>( m_gridTick );
1735 }
1736
1737 // Compute grid starting and ending indexes to draw grid points on the
1738 // visible screen area
1739 // Note: later any point coordinate will be offsetted by m_gridOrigin
1740 int gridStartX = KiROUND( ( worldStartPoint.x - m_gridOrigin.x ) / gridScreenSize.x );
1741 int gridEndX = KiROUND( ( worldEndPoint.x - m_gridOrigin.x ) / gridScreenSize.x );
1742 int gridStartY = KiROUND( ( worldStartPoint.y - m_gridOrigin.y ) / gridScreenSize.y );
1743 int gridEndY = KiROUND( ( worldEndPoint.y - m_gridOrigin.y ) / gridScreenSize.y );
1744
1745 // Ensure start coordinate > end coordinate
1746 SWAP( gridStartX, >, gridEndX );
1747 SWAP( gridStartY, >, gridEndY );
1748
1749 // Ensure the grid fills the screen
1750 --gridStartX;
1751 ++gridEndX;
1752 --gridStartY;
1753 ++gridEndY;
1754
1755 // Draw the grid behind all other layers
1756 SetLayerDepth( m_depthRange.y * 0.75 );
1757
1759 {
1760 // Now draw the grid, every coarse grid line gets the double width
1761
1762 // Vertical lines
1763 for( int j = gridStartY; j <= gridEndY; j++ )
1764 {
1765 const double y = j * gridScreenSize.y + m_gridOrigin.y;
1766
1767 if( m_axesEnabled && y == 0.0 )
1768 continue;
1769
1770 SetLineWidth( ( j % m_gridTick ) ? marker : doubleMarker );
1771 drawGridLine( VECTOR2D( gridStartX * gridScreenSize.x + m_gridOrigin.x, y ),
1772 VECTOR2D( gridEndX * gridScreenSize.x + m_gridOrigin.x, y ) );
1773 }
1774
1775 // Horizontal lines
1776 for( int i = gridStartX; i <= gridEndX; i++ )
1777 {
1778 const double x = i * gridScreenSize.x + m_gridOrigin.x;
1779
1780 if( m_axesEnabled && x == 0.0 )
1781 continue;
1782
1783 SetLineWidth( ( i % m_gridTick ) ? marker : doubleMarker );
1784 drawGridLine( VECTOR2D( x, gridStartY * gridScreenSize.y + m_gridOrigin.y ),
1785 VECTOR2D( x, gridEndY * gridScreenSize.y + m_gridOrigin.y ) );
1786 }
1787 }
1788 else // Dots or Crosses grid
1789 {
1790 m_lineWidthIsOdd = true;
1791 m_isStrokeEnabled = true;
1792
1793 for( int j = gridStartY; j <= gridEndY; j++ )
1794 {
1795 bool tickY = ( j % m_gridTick == 0 );
1796
1797 for( int i = gridStartX; i <= gridEndX; i++ )
1798 {
1799 bool tickX = ( i % m_gridTick == 0 );
1800 VECTOR2D pos{ i * gridScreenSize.x + m_gridOrigin.x,
1801 j * gridScreenSize.y + m_gridOrigin.y };
1802
1804 {
1805 SetLineWidth( ( tickX && tickY ) ? doubleMarker : marker );
1806 drawGridCross( pos );
1807 }
1808 else if( m_gridStyle == GRID_STYLE::DOTS )
1809 {
1810 double doubleGridLineWidth = m_gridLineWidth * 2.0f;
1811 drawGridPoint( pos, ( tickX ) ? doubleGridLineWidth : m_gridLineWidth,
1812 ( tickY ) ? doubleGridLineWidth : m_gridLineWidth );
1813 }
1814 }
1815 }
1816 }
1817}
1818
1819
1820void CAIRO_GAL_BASE::DrawGlyph( const KIFONT::GLYPH& aGlyph, int aNth, int aTotal )
1821{
1822 if( aGlyph.IsStroke() )
1823 {
1824 const KIFONT::STROKE_GLYPH& glyph = static_cast<const KIFONT::STROKE_GLYPH&>( aGlyph );
1825
1826 for( const std::vector<VECTOR2D>& pointList : glyph )
1827 drawPoly( pointList );
1828 }
1829 else if( aGlyph.IsOutline() )
1830 {
1831 const KIFONT::OUTLINE_GLYPH& glyph = static_cast<const KIFONT::OUTLINE_GLYPH&>( aGlyph );
1832
1833 if( aNth == 0 )
1834 {
1835 cairo_close_path( m_currentContext );
1836 flushPath();
1837
1838 cairo_new_path( m_currentContext );
1839 SetIsFill( true );
1840 SetIsStroke( false );
1841 }
1842
1843 // eventually glyphs should not be drawn as polygons at all,
1844 // but as bitmaps with antialiasing, this is just a stopgap measure
1845 // of getting some form of outline font display
1846
1847 glyph.Triangulate(
1848 [&]( const VECTOR2D& aVertex1, const VECTOR2D& aVertex2, const VECTOR2D& aVertex3 )
1849 {
1850 syncLineWidth();
1851
1852 const VECTOR2D p0 = roundp( xform( aVertex1 ) );
1853 const VECTOR2D p1 = roundp( xform( aVertex2 ) );
1854 const VECTOR2D p2 = roundp( xform( aVertex3 ) );
1855
1856 cairo_move_to( m_currentContext, p0.x, p0.y );
1857 cairo_line_to( m_currentContext, p1.x, p1.y );
1858 cairo_line_to( m_currentContext, p2.x, p2.y );
1859 cairo_close_path( m_currentContext );
1860 cairo_set_fill_rule( m_currentContext, CAIRO_FILL_RULE_EVEN_ODD );
1861 flushPath();
1862 cairo_fill( m_currentContext );
1863 } );
1864
1865 if( aNth == aTotal - 1 )
1866 {
1867 /*
1868 cairo_close_path( currentContext );
1869 setSourceRgba( currentContext, fillColor );
1870 cairo_set_fill_rule( currentContext, CAIRO_FILL_RULE_EVEN_ODD );
1871 cairo_fill_preserve( currentContext );
1872 setSourceRgba( currentContext, strokeColor );
1873 cairo_stroke( currentContext );
1874 */
1875 flushPath();
1876 m_isElementAdded = true;
1877 }
1878 }
1879}
int color
Definition: DXF_plotter.cpp:58
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:48
VECTOR2I GetSizePixels() const
Definition: bitmap_base.h:106
int GetPPI() const
Definition: bitmap_base.h:117
wxImage * GetImageData()
Definition: bitmap_base.h:67
static const wxCursor GetCursor(KICURSOR aCursorType)
Definition: cursors.cpp:394
double AsRadians() const
Definition: eda_angle.h:159
virtual bool IsStroke() const
Definition: glyph.h:57
virtual bool IsOutline() const
Definition: glyph.h:56
void Triangulate(std::function< void(const VECTOR2I &aPt1, const VECTOR2I &aPt2, const VECTOR2I &aPt3)> aCallback) const
Definition: glyph.cpp:133
void DrawGlyph(const KIFONT::GLYPH &aPolySet, int aNth, int aTotal) override
Draw a polygon representing a font glyph.
Definition: cairo_gal.cpp:1820
void blitCursor(wxMemoryDC &clientDC)
Blit cursor into the current screen.
Definition: cairo_gal.cpp:1195
void drawGridLine(const VECTOR2D &aStartPoint, const VECTOR2D &aEndPoint)
Draw a grid line (usually a simplified line function).
Definition: cairo_gal.cpp:1067
cairo_surface_t * m_surface
Cairo surface.
Definition: cairo_gal.h:365
void DrawArcSegment(const VECTOR2D &aCenterPoint, double aRadius, const EDA_ANGLE &aStartAngle, const EDA_ANGLE &aAngle, double aWidth, double aMaxError) override
Draw an arc segment.
Definition: cairo_gal.cpp:380
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:589
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
double xform(double x)
Definition: cairo_gal.cpp:177
void Restore() override
Restore the context.
Definition: cairo_gal.cpp:787
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:729
void Save() override
Save the context.
Definition: cairo_gal.cpp:769
void ClearCache() override
Delete all data created during caching of graphic items.
Definition: cairo_gal.cpp:967
bool m_isElementAdded
Was an graphic element added ?
Definition: cairo_gal.h:352
void DeleteGroup(int aGroupNumber) override
Delete the group from the memory.
Definition: cairo_gal.cpp:949
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:595
void storePath()
Store the actual path.
Definition: cairo_gal.cpp:1146
void DrawGroup(int aGroupNumber) override
Draw the stored group.
Definition: cairo_gal.cpp:832
void drawAxes(const VECTOR2D &aStartPoint, const VECTOR2D &aEndPoint)
Definition: cairo_gal.cpp:1049
CAIRO_GAL_BASE(GAL_DISPLAY_OPTIONS &aDisplayOptions)
Definition: cairo_gal.cpp:51
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 SetFillColor(const COLOR4D &aColor) override
Set the fill color.
Definition: cairo_gal.cpp:651
GROUP * m_currentGroup
Currently used group.
Definition: cairo_gal.h:355
void DrawCursor(const VECTOR2D &aCursorPosition) override
Draw the cursor.
Definition: cairo_gal.cpp:1006
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:974
void ChangeGroupDepth(int aGroupNumber, int aDepth) override
Change the depth (Z-axis position) of the group.
Definition: cairo_gal.cpp:942
int BeginGroup() override
Begin a group.
Definition: cairo_gal.cpp:809
void SetLayerDepth(double aLayerDepth) override
Set the depth of the layer (position on the z-axis)
Definition: cairo_gal.cpp:688
unsigned int getNewGroupNumber()
Return a valid key that can be used as a new group number.
Definition: cairo_gal.cpp:1313
cairo_matrix_t m_currentXform
Definition: cairo_gal.h:361
void SetIsStroke(bool aIsStrokeEnabled) override
Enable/disable stroked outlines.
Definition: cairo_gal.cpp:618
void EndGroup() override
End the group.
Definition: cairo_gal.cpp:825
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:1212
void Transform(const MATRIX3x3D &aTransformation) override
Transform the context.
Definition: cairo_gal.cpp:695
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:925
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:633
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:1012
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:749
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
cairo_matrix_t m_currentWorld2Screen
Definition: cairo_gal.h:362
void drawGridCross(const VECTOR2D &aPoint)
Definition: cairo_gal.cpp:1081
void SetLineWidth(float aLineWidth) override
Set the line width.
Definition: cairo_gal.cpp:669
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
double angle_xform(double aAngle)
Transform according to the rotation from m_currentWorld2Screen transform matrix.
Definition: cairo_gal.cpp:129
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
void DrawSegment(const VECTOR2D &aStartPoint, const VECTOR2D &aEndPoint, double aWidth) override
Draw a rounded segment.
Definition: cairo_gal.cpp:257
void DrawArc(const VECTOR2D &aCenterPoint, double aRadius, const EDA_ANGLE &aStartAngle, const EDA_ANGLE &aAngle) override
Draw an arc.
Definition: cairo_gal.cpp:341
void drawGridPoint(const VECTOR2D &aPoint, double aWidth, double aHeight)
Definition: cairo_gal.cpp:1102
void SetIsFill(bool aIsFillEnabled) override
Enable/disable fill.
Definition: cairo_gal.cpp:603
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:583
void Rotate(double aAngle) override
Rotate the context.
Definition: cairo_gal.cpp:710
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:1696
void EndGroup() override
End the group.
Definition: cairo_gal.cpp:1501
void deinitSurface()
Destroy Cairo surfaces when are not needed anymore.
Definition: cairo_gal.cpp:1581
void PostPaint(wxPaintEvent &aEvent)
Post an event to m_paint_listener.
Definition: cairo_gal.cpp:1458
bool Show(bool aShow) override
Show/hide the GAL canvas.
Definition: cairo_gal.cpp:1483
void SetTarget(RENDER_TARGET aTarget) override
Set the target for rendering.
Definition: cairo_gal.cpp:1508
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:1643
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:1325
void initSurface()
Prepare Cairo surfaces for drawing.
Definition: cairo_gal.cpp:1560
int BeginGroup() override
Begin a group.
Definition: cairo_gal.cpp:1494
bool SetNativeCursorStyle(KICURSOR aCursor) override
Set the cursor in the native panel.
Definition: cairo_gal.cpp:1675
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:1690
void EndDrawing() override
End the drawing, needs to be called for every new frame.
Definition: cairo_gal.cpp:1406
void allocateBitmaps()
Allocate the bitmaps for drawing.
Definition: cairo_gal.cpp:1595
void onPaint(wxPaintEvent &aEvent)
Paint event handler.
Definition: cairo_gal.cpp:1637
void StartNegativesLayer() override
Begins rendering in a new layer that will be copied to the main layer in EndNegativesLayer().
Definition: cairo_gal.cpp:993
void ResizeScreen(int aWidth, int aHeight) override
Resizes the canvas.
Definition: cairo_gal.cpp:1466
RENDER_TARGET GetTarget() const override
Get the currently used target for rendering.
Definition: cairo_gal.cpp:1532
void setCompositor()
Prepare the compositor.
Definition: cairo_gal.cpp:1621
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:1538
bool updatedGalDisplayOptions(const GAL_DISPLAY_OPTIONS &aOptions) override
Handle updating display options.
Definition: cairo_gal.cpp:1651
~CAIRO_GAL()
Return true if the GAL canvas is visible on the screen.
Definition: cairo_gal.cpp:1386
void BeginDrawing() override
Start/end drawing functions, draw calls can be only made in between the calls to BeginDrawing()/EndDr...
Definition: cairo_gal.cpp:1392
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:987
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:1611
void EndNegativesLayer() override
Ends rendering of a negatives layer and draws it to the main layer.
Definition: cairo_gal.cpp:1000
void StartDiffLayer() override
Begins rendering of a differential layer.
Definition: cairo_gal.cpp:980
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:104
double r
Red component.
Definition: color4d.h:392
double g
Green component.
Definition: color4d.h:393
double a
Alpha component.
Definition: color4d.h:395
static const COLOR4D BLACK
Definition: color4d.h:402
double b
Blue component.
Definition: color4d.h:394
CAIRO_ANTIALIASING_MODE cairo_antialiasing_mode
The grid style to draw the grid in.
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 outlines in the set.
const SHAPE_LINE_CHAIN & COutline(int aIndex) const
@ BLUE
Definition: color4d.h:56
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:247
@ 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:424
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::@29 m_Argument
bool BoolArg
A bool argument.
Definition: cairo_gal.h:342
void RotatePoint(int *pX, int *pY, const EDA_ANGLE &aAngle)
Calculate the new point of coord coord pX, pY, for a rotation center 0, 0.
Definition: trigo.cpp:228
double EuclideanNorm(const VECTOR2I &vector)
Definition: trigo.h:128
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:587
VECTOR2< int > VECTOR2I
Definition: vector2d.h:588
VECTOR2I ToVECTOR2I(const wxSize &aSize)
Definition: vector2wx.h:30