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