]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/generic/graphicc.cpp | |
3 | // Purpose: cairo device context class | |
4 | // Author: Stefan Csomor | |
5 | // Modified by: | |
6 | // Created: 2006-10-03 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 2006 Stefan Csomor | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #include "wx/wxprec.h" | |
13 | ||
14 | #ifdef __BORLANDC__ | |
15 | #pragma hdrstop | |
16 | #endif | |
17 | ||
18 | #if wxUSE_GRAPHICS_CONTEXT | |
19 | ||
20 | #include "wx/graphics.h" | |
21 | ||
22 | #if wxUSE_CAIRO | |
23 | ||
24 | // keep cairo.h from defining dllimport as we're defining the symbols inside | |
25 | // the wx dll in order to load them dynamically. | |
26 | #define cairo_public | |
27 | ||
28 | #include "wx/cairo.h" | |
29 | ||
30 | #ifndef WX_PRECOMP | |
31 | #include "wx/bitmap.h" | |
32 | #include "wx/icon.h" | |
33 | #include "wx/dcclient.h" | |
34 | #include "wx/dcmemory.h" | |
35 | #include "wx/dcprint.h" | |
36 | #include "wx/window.h" | |
37 | #endif | |
38 | ||
39 | #include "wx/private/graphics.h" | |
40 | #include "wx/rawbmp.h" | |
41 | #include "wx/vector.h" | |
42 | ||
43 | using namespace std; | |
44 | ||
45 | //----------------------------------------------------------------------------- | |
46 | // device context implementation | |
47 | // | |
48 | // more and more of the dc functionality should be implemented by calling | |
49 | // the appropricate wxCairoContext, but we will have to do that step by step | |
50 | // also coordinate conversions should be moved to native matrix ops | |
51 | //----------------------------------------------------------------------------- | |
52 | ||
53 | // we always stock two context states, one at entry, to be able to preserve the | |
54 | // state we were called with, the other one after changing to HI Graphics orientation | |
55 | // (this one is used for getting back clippings etc) | |
56 | ||
57 | //----------------------------------------------------------------------------- | |
58 | // wxGraphicsPath implementation | |
59 | //----------------------------------------------------------------------------- | |
60 | ||
61 | // TODO remove this dependency (gdiplus needs the macros) | |
62 | ||
63 | #ifndef max | |
64 | #define max(a,b) (((a) > (b)) ? (a) : (b)) | |
65 | #endif | |
66 | ||
67 | #ifndef min | |
68 | #define min(a,b) (((a) < (b)) ? (a) : (b)) | |
69 | #endif | |
70 | ||
71 | #include <cairo.h> | |
72 | #ifdef __WXMSW__ | |
73 | #include <cairo-win32.h> | |
74 | #endif | |
75 | ||
76 | #ifdef __WXGTK__ | |
77 | #include <gtk/gtk.h> | |
78 | #include "wx/fontutil.h" | |
79 | #include "wx/gtk/dc.h" | |
80 | #endif | |
81 | ||
82 | #ifdef __WXMSW__ | |
83 | #include <cairo-win32.h> | |
84 | // We must do this as cairo-win32.h includes windows.h which pollutes the | |
85 | // global name space with macros. | |
86 | #include "wx/msw/winundef.h" | |
87 | #endif | |
88 | ||
89 | #ifdef __WXMAC__ | |
90 | #include "wx/osx/private.h" | |
91 | #include <cairo-quartz.h> | |
92 | #include <cairo-atsui.h> | |
93 | #endif | |
94 | ||
95 | class WXDLLIMPEXP_CORE wxCairoPathData : public wxGraphicsPathData | |
96 | { | |
97 | public : | |
98 | wxCairoPathData(wxGraphicsRenderer* renderer, cairo_t* path = NULL); | |
99 | ~wxCairoPathData(); | |
100 | ||
101 | virtual wxGraphicsObjectRefData *Clone() const; | |
102 | ||
103 | // | |
104 | // These are the path primitives from which everything else can be constructed | |
105 | // | |
106 | ||
107 | // begins a new subpath at (x,y) | |
108 | virtual void MoveToPoint( wxDouble x, wxDouble y ); | |
109 | ||
110 | // adds a straight line from the current point to (x,y) | |
111 | virtual void AddLineToPoint( wxDouble x, wxDouble y ); | |
112 | ||
113 | // adds a cubic Bezier curve from the current point, using two control points and an end point | |
114 | virtual void AddCurveToPoint( wxDouble cx1, wxDouble cy1, wxDouble cx2, wxDouble cy2, wxDouble x, wxDouble y ); | |
115 | ||
116 | ||
117 | // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle | |
118 | virtual void AddArc( wxDouble x, wxDouble y, wxDouble r, wxDouble startAngle, wxDouble endAngle, bool clockwise ) ; | |
119 | ||
120 | // gets the last point of the current path, (0,0) if not yet set | |
121 | virtual void GetCurrentPoint( wxDouble* x, wxDouble* y) const; | |
122 | ||
123 | // adds another path | |
124 | virtual void AddPath( const wxGraphicsPathData* path ); | |
125 | ||
126 | // closes the current sub-path | |
127 | virtual void CloseSubpath(); | |
128 | ||
129 | // | |
130 | // These are convenience functions which - if not available natively will be assembled | |
131 | // using the primitives from above | |
132 | // | |
133 | ||
134 | /* | |
135 | ||
136 | // appends a rectangle as a new closed subpath | |
137 | virtual void AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h ) ; | |
138 | // appends an ellipsis as a new closed subpath fitting the passed rectangle | |
139 | virtual void AddEllipsis( wxDouble x, wxDouble y, wxDouble w , wxDouble h ) ; | |
140 | ||
141 | // draws a an arc to two tangents connecting (current) to (x1,y1) and (x1,y1) to (x2,y2), also a straight line from (current) to (x1,y1) | |
142 | virtual void AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r ) ; | |
143 | */ | |
144 | ||
145 | // returns the native path | |
146 | virtual void * GetNativePath() const ; | |
147 | ||
148 | // give the native path returned by GetNativePath() back (there might be some deallocations necessary) | |
149 | virtual void UnGetNativePath(void *p) const; | |
150 | ||
151 | // transforms each point of this path by the matrix | |
152 | virtual void Transform( const wxGraphicsMatrixData* matrix ) ; | |
153 | ||
154 | // gets the bounding box enclosing all points (possibly including control points) | |
155 | virtual void GetBox(wxDouble *x, wxDouble *y, wxDouble *w, wxDouble *h) const; | |
156 | ||
157 | virtual bool Contains( wxDouble x, wxDouble y, wxPolygonFillMode fillStyle = wxWINDING_RULE) const; | |
158 | ||
159 | private : | |
160 | cairo_t* m_pathContext; | |
161 | }; | |
162 | ||
163 | class WXDLLIMPEXP_CORE wxCairoMatrixData : public wxGraphicsMatrixData | |
164 | { | |
165 | public : | |
166 | wxCairoMatrixData(wxGraphicsRenderer* renderer, const cairo_matrix_t* matrix = NULL ) ; | |
167 | virtual ~wxCairoMatrixData() ; | |
168 | ||
169 | virtual wxGraphicsObjectRefData *Clone() const ; | |
170 | ||
171 | // concatenates the matrix | |
172 | virtual void Concat( const wxGraphicsMatrixData *t ); | |
173 | ||
174 | // sets the matrix to the respective values | |
175 | virtual void Set(wxDouble a=1.0, wxDouble b=0.0, wxDouble c=0.0, wxDouble d=1.0, | |
176 | wxDouble tx=0.0, wxDouble ty=0.0); | |
177 | ||
178 | // gets the component valuess of the matrix | |
179 | virtual void Get(wxDouble* a=NULL, wxDouble* b=NULL, wxDouble* c=NULL, | |
180 | wxDouble* d=NULL, wxDouble* tx=NULL, wxDouble* ty=NULL) const; | |
181 | ||
182 | // makes this the inverse matrix | |
183 | virtual void Invert(); | |
184 | ||
185 | // returns true if the elements of the transformation matrix are equal ? | |
186 | virtual bool IsEqual( const wxGraphicsMatrixData* t) const ; | |
187 | ||
188 | // return true if this is the identity matrix | |
189 | virtual bool IsIdentity() const; | |
190 | ||
191 | // | |
192 | // transformation | |
193 | // | |
194 | ||
195 | // add the translation to this matrix | |
196 | virtual void Translate( wxDouble dx , wxDouble dy ); | |
197 | ||
198 | // add the scale to this matrix | |
199 | virtual void Scale( wxDouble xScale , wxDouble yScale ); | |
200 | ||
201 | // add the rotation to this matrix (radians) | |
202 | virtual void Rotate( wxDouble angle ); | |
203 | ||
204 | // | |
205 | // apply the transforms | |
206 | // | |
207 | ||
208 | // applies that matrix to the point | |
209 | virtual void TransformPoint( wxDouble *x, wxDouble *y ) const; | |
210 | ||
211 | // applies the matrix except for translations | |
212 | virtual void TransformDistance( wxDouble *dx, wxDouble *dy ) const; | |
213 | ||
214 | // returns the native representation | |
215 | virtual void * GetNativeMatrix() const; | |
216 | private: | |
217 | cairo_matrix_t m_matrix ; | |
218 | } ; | |
219 | ||
220 | class WXDLLIMPEXP_CORE wxCairoPenData : public wxGraphicsObjectRefData | |
221 | { | |
222 | public: | |
223 | wxCairoPenData( wxGraphicsRenderer* renderer, const wxPen &pen ); | |
224 | ~wxCairoPenData(); | |
225 | ||
226 | void Init(); | |
227 | ||
228 | virtual void Apply( wxGraphicsContext* context ); | |
229 | virtual wxDouble GetWidth() { return m_width; } | |
230 | ||
231 | private : | |
232 | double m_width; | |
233 | ||
234 | double m_red; | |
235 | double m_green; | |
236 | double m_blue; | |
237 | double m_alpha; | |
238 | ||
239 | cairo_line_cap_t m_cap; | |
240 | cairo_line_join_t m_join; | |
241 | ||
242 | int m_count; | |
243 | const double *m_lengths; | |
244 | double *m_userLengths; | |
245 | ||
246 | wxPen m_pen; | |
247 | ||
248 | wxDECLARE_NO_COPY_CLASS(wxCairoPenData); | |
249 | }; | |
250 | ||
251 | class WXDLLIMPEXP_CORE wxCairoBrushData : public wxGraphicsObjectRefData | |
252 | { | |
253 | public: | |
254 | wxCairoBrushData( wxGraphicsRenderer* renderer ); | |
255 | wxCairoBrushData( wxGraphicsRenderer* renderer, const wxBrush &brush ); | |
256 | ~wxCairoBrushData (); | |
257 | ||
258 | virtual void Apply( wxGraphicsContext* context ); | |
259 | ||
260 | void CreateLinearGradientBrush(wxDouble x1, wxDouble y1, | |
261 | wxDouble x2, wxDouble y2, | |
262 | const wxGraphicsGradientStops& stops); | |
263 | void CreateRadialGradientBrush(wxDouble xo, wxDouble yo, | |
264 | wxDouble xc, wxDouble yc, wxDouble radius, | |
265 | const wxGraphicsGradientStops& stops); | |
266 | ||
267 | protected: | |
268 | virtual void Init(); | |
269 | ||
270 | // common part of Create{Linear,Radial}GradientBrush() | |
271 | void AddGradientStops(const wxGraphicsGradientStops& stops); | |
272 | ||
273 | private : | |
274 | double m_red; | |
275 | double m_green; | |
276 | double m_blue; | |
277 | double m_alpha; | |
278 | ||
279 | cairo_pattern_t* m_brushPattern; | |
280 | }; | |
281 | ||
282 | class wxCairoFontData : public wxGraphicsObjectRefData | |
283 | { | |
284 | public: | |
285 | wxCairoFontData( wxGraphicsRenderer* renderer, const wxFont &font, const wxColour& col ); | |
286 | wxCairoFontData(wxGraphicsRenderer* renderer, | |
287 | double sizeInPixels, | |
288 | const wxString& facename, | |
289 | int flags, | |
290 | const wxColour& col); | |
291 | ~wxCairoFontData(); | |
292 | ||
293 | virtual bool Apply( wxGraphicsContext* context ); | |
294 | #ifdef __WXGTK__ | |
295 | const wxFont& GetFont() const { return m_wxfont; } | |
296 | #endif | |
297 | private : | |
298 | void InitColour(const wxColour& col); | |
299 | void InitFontComponents(const wxString& facename, | |
300 | cairo_font_slant_t slant, | |
301 | cairo_font_weight_t weight); | |
302 | ||
303 | double m_size; | |
304 | double m_red; | |
305 | double m_green; | |
306 | double m_blue; | |
307 | double m_alpha; | |
308 | #ifdef __WXMAC__ | |
309 | cairo_font_face_t *m_font; | |
310 | #elif defined(__WXGTK__) | |
311 | wxFont m_wxfont; | |
312 | #endif | |
313 | ||
314 | // These members are used when the font is created from its face name and | |
315 | // flags (and not from wxFont) and also even when creating it from wxFont | |
316 | // on the platforms not covered above. | |
317 | // | |
318 | // Notice that we can't use cairo_font_face_t instead of storing those, | |
319 | // even though it would be simpler and need less #ifdefs, because | |
320 | // cairo_toy_font_face_create() that we'd need to create it is only | |
321 | // available in Cairo 1.8 and we require just 1.2 currently. If we do drop | |
322 | // support for < 1.8 versions in the future it would be definitely better | |
323 | // to use cairo_toy_font_face_create() instead. | |
324 | wxCharBuffer m_fontName; | |
325 | cairo_font_slant_t m_slant; | |
326 | cairo_font_weight_t m_weight; | |
327 | }; | |
328 | ||
329 | class wxCairoBitmapData : public wxGraphicsObjectRefData | |
330 | { | |
331 | public: | |
332 | wxCairoBitmapData( wxGraphicsRenderer* renderer, const wxBitmap& bmp ); | |
333 | #if wxUSE_IMAGE | |
334 | wxCairoBitmapData(wxGraphicsRenderer* renderer, const wxImage& image); | |
335 | #endif // wxUSE_IMAGE | |
336 | wxCairoBitmapData( wxGraphicsRenderer* renderer, cairo_surface_t* bitmap ); | |
337 | ~wxCairoBitmapData(); | |
338 | ||
339 | virtual cairo_surface_t* GetCairoSurface() { return m_surface; } | |
340 | virtual cairo_pattern_t* GetCairoPattern() { return m_pattern; } | |
341 | virtual wxSize GetSize() { return wxSize(m_width, m_height); } | |
342 | ||
343 | #if wxUSE_IMAGE | |
344 | wxImage ConvertToImage() const; | |
345 | #endif // wxUSE_IMAGE | |
346 | ||
347 | private : | |
348 | // Allocate m_buffer for the bitmap of the given size in the given format. | |
349 | // | |
350 | // Returns the stride used for the buffer. | |
351 | int InitBuffer(int width, int height, cairo_format_t format); | |
352 | ||
353 | // Really create the surface using the buffer (which was supposed to be | |
354 | // filled since InitBuffer() call). | |
355 | void InitSurface(cairo_format_t format, int stride); | |
356 | ||
357 | ||
358 | cairo_surface_t* m_surface; | |
359 | cairo_pattern_t* m_pattern; | |
360 | int m_width; | |
361 | int m_height; | |
362 | unsigned char* m_buffer; | |
363 | }; | |
364 | ||
365 | class WXDLLIMPEXP_CORE wxCairoContext : public wxGraphicsContext | |
366 | { | |
367 | public: | |
368 | wxCairoContext( wxGraphicsRenderer* renderer, const wxWindowDC& dc ); | |
369 | wxCairoContext( wxGraphicsRenderer* renderer, const wxMemoryDC& dc ); | |
370 | wxCairoContext( wxGraphicsRenderer* renderer, const wxPrinterDC& dc ); | |
371 | #ifdef __WXGTK__ | |
372 | wxCairoContext( wxGraphicsRenderer* renderer, GdkDrawable *drawable ); | |
373 | #endif | |
374 | #ifdef __WXMSW__ | |
375 | wxCairoContext( wxGraphicsRenderer* renderer, HDC context ); | |
376 | #endif | |
377 | wxCairoContext( wxGraphicsRenderer* renderer, cairo_t *context ); | |
378 | wxCairoContext( wxGraphicsRenderer* renderer, wxWindow *window); | |
379 | ||
380 | // If this ctor is used, Init() must be called by the derived class later. | |
381 | wxCairoContext( wxGraphicsRenderer* renderer ); | |
382 | ||
383 | virtual ~wxCairoContext(); | |
384 | ||
385 | virtual bool ShouldOffset() const | |
386 | { | |
387 | if ( !m_enableOffset ) | |
388 | return false; | |
389 | ||
390 | int penwidth = 0 ; | |
391 | if ( !m_pen.IsNull() ) | |
392 | { | |
393 | penwidth = (int)((wxCairoPenData*)m_pen.GetRefData())->GetWidth(); | |
394 | if ( penwidth == 0 ) | |
395 | penwidth = 1; | |
396 | } | |
397 | return ( penwidth % 2 ) == 1; | |
398 | } | |
399 | ||
400 | virtual void Clip( const wxRegion ®ion ); | |
401 | #ifdef __WXMSW__ | |
402 | cairo_surface_t* m_mswSurface; | |
403 | #endif | |
404 | ||
405 | // clips drawings to the rect | |
406 | virtual void Clip( wxDouble x, wxDouble y, wxDouble w, wxDouble h ); | |
407 | ||
408 | // resets the clipping to original extent | |
409 | virtual void ResetClip(); | |
410 | ||
411 | virtual void * GetNativeContext(); | |
412 | ||
413 | virtual bool SetAntialiasMode(wxAntialiasMode antialias); | |
414 | ||
415 | virtual bool SetInterpolationQuality(wxInterpolationQuality interpolation); | |
416 | ||
417 | virtual bool SetCompositionMode(wxCompositionMode op); | |
418 | ||
419 | virtual void BeginLayer(wxDouble opacity); | |
420 | ||
421 | virtual void EndLayer(); | |
422 | ||
423 | virtual void StrokePath( const wxGraphicsPath& p ); | |
424 | virtual void FillPath( const wxGraphicsPath& p , wxPolygonFillMode fillStyle = wxWINDING_RULE ); | |
425 | ||
426 | virtual void Translate( wxDouble dx , wxDouble dy ); | |
427 | virtual void Scale( wxDouble xScale , wxDouble yScale ); | |
428 | virtual void Rotate( wxDouble angle ); | |
429 | ||
430 | // concatenates this transform with the current transform of this context | |
431 | virtual void ConcatTransform( const wxGraphicsMatrix& matrix ); | |
432 | ||
433 | // sets the transform of this context | |
434 | virtual void SetTransform( const wxGraphicsMatrix& matrix ); | |
435 | ||
436 | // gets the matrix of this context | |
437 | virtual wxGraphicsMatrix GetTransform() const; | |
438 | ||
439 | virtual void DrawBitmap( const wxGraphicsBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h ); | |
440 | virtual void DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h ); | |
441 | virtual void DrawIcon( const wxIcon &icon, wxDouble x, wxDouble y, wxDouble w, wxDouble h ); | |
442 | virtual void PushState(); | |
443 | virtual void PopState(); | |
444 | ||
445 | virtual void GetTextExtent( const wxString &str, wxDouble *width, wxDouble *height, | |
446 | wxDouble *descent, wxDouble *externalLeading ) const; | |
447 | virtual void GetPartialTextExtents(const wxString& text, wxArrayDouble& widths) const; | |
448 | ||
449 | protected: | |
450 | virtual void DoDrawText( const wxString &str, wxDouble x, wxDouble y ); | |
451 | ||
452 | void Init(cairo_t *context); | |
453 | ||
454 | private: | |
455 | cairo_t* m_context; | |
456 | ||
457 | wxVector<float> m_layerOpacities; | |
458 | ||
459 | wxDECLARE_NO_COPY_CLASS(wxCairoContext); | |
460 | }; | |
461 | ||
462 | #if wxUSE_IMAGE | |
463 | // ---------------------------------------------------------------------------- | |
464 | // wxCairoImageContext: context associated with a wxImage. | |
465 | // ---------------------------------------------------------------------------- | |
466 | ||
467 | class wxCairoImageContext : public wxCairoContext | |
468 | { | |
469 | public: | |
470 | wxCairoImageContext(wxGraphicsRenderer* renderer, wxImage& image) : | |
471 | wxCairoContext(renderer), | |
472 | m_image(image), | |
473 | m_data(renderer, image) | |
474 | { | |
475 | Init(cairo_create(m_data.GetCairoSurface())); | |
476 | } | |
477 | ||
478 | virtual ~wxCairoImageContext() | |
479 | { | |
480 | m_image = m_data.ConvertToImage(); | |
481 | } | |
482 | ||
483 | private: | |
484 | wxImage& m_image; | |
485 | wxCairoBitmapData m_data; | |
486 | ||
487 | wxDECLARE_NO_COPY_CLASS(wxCairoImageContext); | |
488 | }; | |
489 | #endif // wxUSE_IMAGE | |
490 | ||
491 | //----------------------------------------------------------------------------- | |
492 | // wxCairoPenData implementation | |
493 | //----------------------------------------------------------------------------- | |
494 | ||
495 | wxCairoPenData::~wxCairoPenData() | |
496 | { | |
497 | delete[] m_userLengths; | |
498 | } | |
499 | ||
500 | void wxCairoPenData::Init() | |
501 | { | |
502 | m_lengths = NULL; | |
503 | m_userLengths = NULL; | |
504 | m_width = 0; | |
505 | m_count = 0; | |
506 | } | |
507 | ||
508 | wxCairoPenData::wxCairoPenData( wxGraphicsRenderer* renderer, const wxPen &pen ) | |
509 | : wxGraphicsObjectRefData(renderer) | |
510 | { | |
511 | Init(); | |
512 | m_pen = pen; | |
513 | m_width = m_pen.GetWidth(); | |
514 | if (m_width <= 0.0) | |
515 | m_width = 0.1; | |
516 | ||
517 | m_red = m_pen.GetColour().Red()/255.0; | |
518 | m_green = m_pen.GetColour().Green()/255.0; | |
519 | m_blue = m_pen.GetColour().Blue()/255.0; | |
520 | m_alpha = m_pen.GetColour().Alpha()/255.0; | |
521 | ||
522 | switch ( m_pen.GetCap() ) | |
523 | { | |
524 | case wxCAP_ROUND : | |
525 | m_cap = CAIRO_LINE_CAP_ROUND; | |
526 | break; | |
527 | ||
528 | case wxCAP_PROJECTING : | |
529 | m_cap = CAIRO_LINE_CAP_SQUARE; | |
530 | break; | |
531 | ||
532 | case wxCAP_BUTT : | |
533 | m_cap = CAIRO_LINE_CAP_BUTT; | |
534 | break; | |
535 | ||
536 | default : | |
537 | m_cap = CAIRO_LINE_CAP_BUTT; | |
538 | break; | |
539 | } | |
540 | ||
541 | switch ( m_pen.GetJoin() ) | |
542 | { | |
543 | case wxJOIN_BEVEL : | |
544 | m_join = CAIRO_LINE_JOIN_BEVEL; | |
545 | break; | |
546 | ||
547 | case wxJOIN_MITER : | |
548 | m_join = CAIRO_LINE_JOIN_MITER; | |
549 | break; | |
550 | ||
551 | case wxJOIN_ROUND : | |
552 | m_join = CAIRO_LINE_JOIN_ROUND; | |
553 | break; | |
554 | ||
555 | default : | |
556 | m_join = CAIRO_LINE_JOIN_MITER; | |
557 | break; | |
558 | } | |
559 | ||
560 | const double dashUnit = m_width < 1.0 ? 1.0 : m_width; | |
561 | const double dotted[] = | |
562 | { | |
563 | dashUnit , dashUnit + 2.0 | |
564 | }; | |
565 | static const double short_dashed[] = | |
566 | { | |
567 | 9.0 , 6.0 | |
568 | }; | |
569 | static const double dashed[] = | |
570 | { | |
571 | 19.0 , 9.0 | |
572 | }; | |
573 | static const double dotted_dashed[] = | |
574 | { | |
575 | 9.0 , 6.0 , 3.0 , 3.0 | |
576 | }; | |
577 | ||
578 | switch ( m_pen.GetStyle() ) | |
579 | { | |
580 | case wxPENSTYLE_SOLID : | |
581 | break; | |
582 | ||
583 | case wxPENSTYLE_DOT : | |
584 | m_count = WXSIZEOF(dotted); | |
585 | m_userLengths = new double[ m_count ] ; | |
586 | memcpy( m_userLengths, dotted, sizeof(dotted) ); | |
587 | m_lengths = m_userLengths; | |
588 | break; | |
589 | ||
590 | case wxPENSTYLE_LONG_DASH : | |
591 | m_lengths = dashed ; | |
592 | m_count = WXSIZEOF(dashed); | |
593 | break; | |
594 | ||
595 | case wxPENSTYLE_SHORT_DASH : | |
596 | m_lengths = short_dashed ; | |
597 | m_count = WXSIZEOF(short_dashed); | |
598 | break; | |
599 | ||
600 | case wxPENSTYLE_DOT_DASH : | |
601 | m_lengths = dotted_dashed ; | |
602 | m_count = WXSIZEOF(dotted_dashed); | |
603 | break; | |
604 | ||
605 | case wxPENSTYLE_USER_DASH : | |
606 | { | |
607 | wxDash *wxdashes ; | |
608 | m_count = m_pen.GetDashes( &wxdashes ) ; | |
609 | if ((wxdashes != NULL) && (m_count > 0)) | |
610 | { | |
611 | m_userLengths = new double[m_count] ; | |
612 | for ( int i = 0 ; i < m_count ; ++i ) | |
613 | { | |
614 | m_userLengths[i] = wxdashes[i] * dashUnit ; | |
615 | ||
616 | if ( i % 2 == 1 && m_userLengths[i] < dashUnit + 2.0 ) | |
617 | m_userLengths[i] = dashUnit + 2.0 ; | |
618 | else if ( i % 2 == 0 && m_userLengths[i] < dashUnit ) | |
619 | m_userLengths[i] = dashUnit ; | |
620 | } | |
621 | } | |
622 | m_lengths = m_userLengths ; | |
623 | } | |
624 | break; | |
625 | case wxPENSTYLE_STIPPLE : | |
626 | { | |
627 | /* | |
628 | wxBitmap* bmp = pen.GetStipple(); | |
629 | if ( bmp && bmp->IsOk() ) | |
630 | { | |
631 | wxDELETE( m_penImage ); | |
632 | wxDELETE( m_penBrush ); | |
633 | m_penImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE()); | |
634 | m_penBrush = new TextureBrush(m_penImage); | |
635 | m_pen->SetBrush( m_penBrush ); | |
636 | } | |
637 | */ | |
638 | } | |
639 | break; | |
640 | default : | |
641 | if ( m_pen.GetStyle() >= wxPENSTYLE_FIRST_HATCH | |
642 | && m_pen.GetStyle() <= wxPENSTYLE_LAST_HATCH ) | |
643 | { | |
644 | /* | |
645 | wxDELETE( m_penBrush ); | |
646 | HatchStyle style = HatchStyleHorizontal; | |
647 | switch( pen.GetStyle() ) | |
648 | { | |
649 | case wxPENSTYLE_BDIAGONAL_HATCH : | |
650 | style = HatchStyleBackwardDiagonal; | |
651 | break ; | |
652 | case wxPENSTYLE_CROSSDIAG_HATCH : | |
653 | style = HatchStyleDiagonalCross; | |
654 | break ; | |
655 | case wxPENSTYLE_FDIAGONAL_HATCH : | |
656 | style = HatchStyleForwardDiagonal; | |
657 | break ; | |
658 | case wxPENSTYLE_CROSS_HATCH : | |
659 | style = HatchStyleCross; | |
660 | break ; | |
661 | case wxPENSTYLE_HORIZONTAL_HATCH : | |
662 | style = HatchStyleHorizontal; | |
663 | break ; | |
664 | case wxPENSTYLE_VERTICAL_HATCH : | |
665 | style = HatchStyleVertical; | |
666 | break ; | |
667 | ||
668 | } | |
669 | m_penBrush = new HatchBrush(style,Color( pen.GetColour().Alpha() , pen.GetColour().Red() , | |
670 | pen.GetColour().Green() , pen.GetColour().Blue() ), Color.Transparent ); | |
671 | m_pen->SetBrush( m_penBrush ) | |
672 | */ | |
673 | } | |
674 | break; | |
675 | } | |
676 | } | |
677 | ||
678 | void wxCairoPenData::Apply( wxGraphicsContext* context ) | |
679 | { | |
680 | cairo_t * ctext = (cairo_t*) context->GetNativeContext(); | |
681 | cairo_set_line_width(ctext,m_width); | |
682 | cairo_set_source_rgba(ctext,m_red,m_green, m_blue,m_alpha); | |
683 | cairo_set_line_cap(ctext,m_cap); | |
684 | cairo_set_line_join(ctext,m_join); | |
685 | cairo_set_dash(ctext,(double*)m_lengths,m_count,0.0); | |
686 | } | |
687 | ||
688 | //----------------------------------------------------------------------------- | |
689 | // wxCairoBrushData implementation | |
690 | //----------------------------------------------------------------------------- | |
691 | ||
692 | wxCairoBrushData::wxCairoBrushData( wxGraphicsRenderer* renderer ) | |
693 | : wxGraphicsObjectRefData( renderer ) | |
694 | { | |
695 | Init(); | |
696 | } | |
697 | ||
698 | wxCairoBrushData::wxCairoBrushData( wxGraphicsRenderer* renderer, const wxBrush &brush ) | |
699 | : wxGraphicsObjectRefData(renderer) | |
700 | { | |
701 | Init(); | |
702 | ||
703 | m_red = brush.GetColour().Red()/255.0; | |
704 | m_green = brush.GetColour().Green()/255.0; | |
705 | m_blue = brush.GetColour().Blue()/255.0; | |
706 | m_alpha = brush.GetColour().Alpha()/255.0; | |
707 | /* | |
708 | if ( brush.GetStyle() == wxBRUSHSTYLE_SOLID) | |
709 | { | |
710 | m_brush = new SolidBrush( Color( brush.GetColour().Alpha() , brush.GetColour().Red() , | |
711 | brush.GetColour().Green() , brush.GetColour().Blue() ) ); | |
712 | } | |
713 | else if ( brush.IsHatch() ) | |
714 | { | |
715 | HatchStyle style = HatchStyleHorizontal; | |
716 | switch( brush.GetStyle() ) | |
717 | { | |
718 | case wxBRUSHSTYLE_BDIAGONAL_HATCH : | |
719 | style = HatchStyleBackwardDiagonal; | |
720 | break ; | |
721 | case wxBRUSHSTYLE_CROSSDIAG_HATCH : | |
722 | style = HatchStyleDiagonalCross; | |
723 | break ; | |
724 | case wxBRUSHSTYLE_FDIAGONAL_HATCH : | |
725 | style = HatchStyleForwardDiagonal; | |
726 | break ; | |
727 | case wxBRUSHSTYLE_CROSS_HATCH : | |
728 | style = HatchStyleCross; | |
729 | break ; | |
730 | case wxBRUSHSTYLE_HORIZONTAL_HATCH : | |
731 | style = HatchStyleHorizontal; | |
732 | break ; | |
733 | case wxBRUSHSTYLE_VERTICAL_HATCH : | |
734 | style = HatchStyleVertical; | |
735 | break ; | |
736 | ||
737 | } | |
738 | m_brush = new HatchBrush(style,Color( brush.GetColour().Alpha() , brush.GetColour().Red() , | |
739 | brush.GetColour().Green() , brush.GetColour().Blue() ), Color.Transparent ); | |
740 | } | |
741 | else | |
742 | { | |
743 | wxBitmap* bmp = brush.GetStipple(); | |
744 | if ( bmp && bmp->IsOk() ) | |
745 | { | |
746 | wxDELETE( m_brushImage ); | |
747 | m_brushImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE()); | |
748 | m_brush = new TextureBrush(m_brushImage); | |
749 | } | |
750 | } | |
751 | */ | |
752 | } | |
753 | ||
754 | wxCairoBrushData::~wxCairoBrushData () | |
755 | { | |
756 | if (m_brushPattern) | |
757 | cairo_pattern_destroy(m_brushPattern); | |
758 | } | |
759 | ||
760 | void wxCairoBrushData::Apply( wxGraphicsContext* context ) | |
761 | { | |
762 | cairo_t * ctext = (cairo_t*) context->GetNativeContext(); | |
763 | if ( m_brushPattern ) | |
764 | { | |
765 | cairo_set_source(ctext,m_brushPattern); | |
766 | } | |
767 | else | |
768 | { | |
769 | cairo_set_source_rgba(ctext,m_red,m_green, m_blue,m_alpha); | |
770 | } | |
771 | } | |
772 | ||
773 | void wxCairoBrushData::AddGradientStops(const wxGraphicsGradientStops& stops) | |
774 | { | |
775 | // loop over all the stops, they include the beginning and ending ones | |
776 | const unsigned numStops = stops.GetCount(); | |
777 | for ( unsigned n = 0; n < numStops; n++ ) | |
778 | { | |
779 | const wxGraphicsGradientStop stop = stops.Item(n); | |
780 | ||
781 | const wxColour col = stop.GetColour(); | |
782 | ||
783 | cairo_pattern_add_color_stop_rgba | |
784 | ( | |
785 | m_brushPattern, | |
786 | stop.GetPosition(), | |
787 | col.Red()/255.0, | |
788 | col.Green()/255.0, | |
789 | col.Blue()/255.0, | |
790 | col.Alpha()/255.0 | |
791 | ); | |
792 | } | |
793 | ||
794 | wxASSERT_MSG(cairo_pattern_status(m_brushPattern) == CAIRO_STATUS_SUCCESS, | |
795 | wxT("Couldn't create cairo pattern")); | |
796 | } | |
797 | ||
798 | void | |
799 | wxCairoBrushData::CreateLinearGradientBrush(wxDouble x1, wxDouble y1, | |
800 | wxDouble x2, wxDouble y2, | |
801 | const wxGraphicsGradientStops& stops) | |
802 | { | |
803 | m_brushPattern = cairo_pattern_create_linear(x1,y1,x2,y2); | |
804 | ||
805 | AddGradientStops(stops); | |
806 | } | |
807 | ||
808 | void | |
809 | wxCairoBrushData::CreateRadialGradientBrush(wxDouble xo, wxDouble yo, | |
810 | wxDouble xc, wxDouble yc, | |
811 | wxDouble radius, | |
812 | const wxGraphicsGradientStops& stops) | |
813 | { | |
814 | m_brushPattern = cairo_pattern_create_radial(xo,yo,0.0,xc,yc,radius); | |
815 | ||
816 | AddGradientStops(stops); | |
817 | } | |
818 | ||
819 | void wxCairoBrushData::Init() | |
820 | { | |
821 | m_brushPattern = NULL; | |
822 | } | |
823 | ||
824 | //----------------------------------------------------------------------------- | |
825 | // wxCairoFontData implementation | |
826 | //----------------------------------------------------------------------------- | |
827 | ||
828 | void wxCairoFontData::InitColour(const wxColour& col) | |
829 | { | |
830 | m_red = col.Red()/255.0; | |
831 | m_green = col.Green()/255.0; | |
832 | m_blue = col.Blue()/255.0; | |
833 | m_alpha = col.Alpha()/255.0; | |
834 | } | |
835 | ||
836 | void | |
837 | wxCairoFontData::InitFontComponents(const wxString& facename, | |
838 | cairo_font_slant_t slant, | |
839 | cairo_font_weight_t weight) | |
840 | { | |
841 | m_fontName = facename.mb_str(wxConvUTF8); | |
842 | m_slant = slant; | |
843 | m_weight = weight; | |
844 | } | |
845 | ||
846 | wxCairoFontData::wxCairoFontData( wxGraphicsRenderer* renderer, const wxFont &font, | |
847 | const wxColour& col ) | |
848 | : wxGraphicsObjectRefData(renderer) | |
849 | #ifdef __WXGTK__ | |
850 | , m_wxfont(font) | |
851 | #endif | |
852 | { | |
853 | InitColour(col); | |
854 | ||
855 | m_size = font.GetPointSize(); | |
856 | ||
857 | #ifdef __WXMAC__ | |
858 | m_font = cairo_quartz_font_face_create_for_cgfont( font.OSXGetCGFont() ); | |
859 | #elif defined(__WXGTK__) | |
860 | #else | |
861 | InitFontComponents | |
862 | ( | |
863 | font.GetFaceName(), | |
864 | font.GetStyle() == wxFONTSTYLE_ITALIC ? CAIRO_FONT_SLANT_ITALIC | |
865 | : CAIRO_FONT_SLANT_NORMAL, | |
866 | font.GetWeight() == wxFONTWEIGHT_BOLD ? CAIRO_FONT_WEIGHT_BOLD | |
867 | : CAIRO_FONT_WEIGHT_NORMAL | |
868 | ); | |
869 | #endif | |
870 | } | |
871 | ||
872 | wxCairoFontData::wxCairoFontData(wxGraphicsRenderer* renderer, | |
873 | double sizeInPixels, | |
874 | const wxString& facename, | |
875 | int flags, | |
876 | const wxColour& col) : | |
877 | wxGraphicsObjectRefData(renderer) | |
878 | { | |
879 | InitColour(col); | |
880 | ||
881 | // Resolution for Cairo image surfaces is 72 DPI meaning that the sizes in | |
882 | // points and pixels are identical, so we can just pass the size in pixels | |
883 | // directly to cairo_set_font_size(). | |
884 | m_size = sizeInPixels; | |
885 | ||
886 | #if defined(__WXMAC__) | |
887 | m_font = NULL; | |
888 | #endif | |
889 | ||
890 | // There is no need to set m_underlined under wxGTK in this case, it can | |
891 | // only be used if m_font != NULL. | |
892 | ||
893 | InitFontComponents | |
894 | ( | |
895 | facename, | |
896 | flags & wxFONTFLAG_ITALIC ? CAIRO_FONT_SLANT_ITALIC | |
897 | : CAIRO_FONT_SLANT_NORMAL, | |
898 | flags & wxFONTFLAG_BOLD ? CAIRO_FONT_WEIGHT_BOLD | |
899 | : CAIRO_FONT_WEIGHT_NORMAL | |
900 | ); | |
901 | } | |
902 | ||
903 | wxCairoFontData::~wxCairoFontData() | |
904 | { | |
905 | #ifdef __WXMAC__ | |
906 | if ( m_font ) | |
907 | cairo_font_face_destroy( m_font ); | |
908 | #endif | |
909 | } | |
910 | ||
911 | bool wxCairoFontData::Apply( wxGraphicsContext* context ) | |
912 | { | |
913 | cairo_t * ctext = (cairo_t*) context->GetNativeContext(); | |
914 | cairo_set_source_rgba(ctext,m_red,m_green, m_blue,m_alpha); | |
915 | #ifdef __WXGTK__ | |
916 | if (m_wxfont.IsOk()) | |
917 | { | |
918 | // Nothing to do, the caller uses Pango layout functions to do | |
919 | // everything. | |
920 | return true; | |
921 | } | |
922 | #elif defined(__WXMAC__) | |
923 | if ( m_font ) | |
924 | { | |
925 | cairo_set_font_face(ctext, m_font); | |
926 | cairo_set_font_size(ctext, m_size ); | |
927 | return true; | |
928 | } | |
929 | #endif | |
930 | ||
931 | // If we get here, we must be on a platform without native font support or | |
932 | // we're using toy Cairo API even under wxGTK/wxMac. | |
933 | cairo_select_font_face(ctext, m_fontName, m_slant, m_weight ); | |
934 | cairo_set_font_size(ctext, m_size ); | |
935 | ||
936 | // Indicate that we don't use native fonts for the platforms which care | |
937 | // about this (currently only wxGTK). | |
938 | return false; | |
939 | } | |
940 | ||
941 | //----------------------------------------------------------------------------- | |
942 | // wxCairoPathData implementation | |
943 | //----------------------------------------------------------------------------- | |
944 | ||
945 | wxCairoPathData::wxCairoPathData( wxGraphicsRenderer* renderer, cairo_t* pathcontext) | |
946 | : wxGraphicsPathData(renderer) | |
947 | { | |
948 | if (pathcontext) | |
949 | { | |
950 | m_pathContext = pathcontext; | |
951 | } | |
952 | else | |
953 | { | |
954 | cairo_surface_t* surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,1,1); | |
955 | m_pathContext = cairo_create(surface); | |
956 | cairo_surface_destroy (surface); | |
957 | } | |
958 | } | |
959 | ||
960 | wxCairoPathData::~wxCairoPathData() | |
961 | { | |
962 | cairo_destroy(m_pathContext); | |
963 | } | |
964 | ||
965 | wxGraphicsObjectRefData *wxCairoPathData::Clone() const | |
966 | { | |
967 | cairo_surface_t* surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,1,1); | |
968 | cairo_t* pathcontext = cairo_create(surface); | |
969 | cairo_surface_destroy (surface); | |
970 | ||
971 | cairo_path_t* path = cairo_copy_path(m_pathContext); | |
972 | cairo_append_path(pathcontext, path); | |
973 | cairo_path_destroy(path); | |
974 | return new wxCairoPathData( GetRenderer() ,pathcontext); | |
975 | } | |
976 | ||
977 | ||
978 | void* wxCairoPathData::GetNativePath() const | |
979 | { | |
980 | return cairo_copy_path(m_pathContext) ; | |
981 | } | |
982 | ||
983 | void wxCairoPathData::UnGetNativePath(void *p) const | |
984 | { | |
985 | cairo_path_destroy((cairo_path_t*)p); | |
986 | } | |
987 | ||
988 | // | |
989 | // The Primitives | |
990 | // | |
991 | ||
992 | void wxCairoPathData::MoveToPoint( wxDouble x , wxDouble y ) | |
993 | { | |
994 | cairo_move_to(m_pathContext,x,y); | |
995 | } | |
996 | ||
997 | void wxCairoPathData::AddLineToPoint( wxDouble x , wxDouble y ) | |
998 | { | |
999 | cairo_line_to(m_pathContext,x,y); | |
1000 | } | |
1001 | ||
1002 | void wxCairoPathData::AddPath( const wxGraphicsPathData* path ) | |
1003 | { | |
1004 | cairo_path_t* p = (cairo_path_t*)path->GetNativePath(); | |
1005 | cairo_append_path(m_pathContext, p); | |
1006 | UnGetNativePath(p); | |
1007 | } | |
1008 | ||
1009 | void wxCairoPathData::CloseSubpath() | |
1010 | { | |
1011 | cairo_close_path(m_pathContext); | |
1012 | } | |
1013 | ||
1014 | void wxCairoPathData::AddCurveToPoint( wxDouble cx1, wxDouble cy1, wxDouble cx2, wxDouble cy2, wxDouble x, wxDouble y ) | |
1015 | { | |
1016 | cairo_curve_to(m_pathContext,cx1,cy1,cx2,cy2,x,y); | |
1017 | } | |
1018 | ||
1019 | // gets the last point of the current path, (0,0) if not yet set | |
1020 | void wxCairoPathData::GetCurrentPoint( wxDouble* x, wxDouble* y) const | |
1021 | { | |
1022 | double dx,dy; | |
1023 | cairo_get_current_point(m_pathContext,&dx,&dy); | |
1024 | if (x) | |
1025 | *x = dx; | |
1026 | if (y) | |
1027 | *y = dy; | |
1028 | } | |
1029 | ||
1030 | void wxCairoPathData::AddArc( wxDouble x, wxDouble y, wxDouble r, double startAngle, double endAngle, bool clockwise ) | |
1031 | { | |
1032 | // as clockwise means positive in our system (y pointing downwards) | |
1033 | // TODO make this interpretation dependent of the | |
1034 | // real device trans | |
1035 | if ( clockwise||(endAngle-startAngle)>=2*M_PI) | |
1036 | cairo_arc(m_pathContext,x,y,r,startAngle,endAngle); | |
1037 | else | |
1038 | cairo_arc_negative(m_pathContext,x,y,r,startAngle,endAngle); | |
1039 | } | |
1040 | ||
1041 | // transforms each point of this path by the matrix | |
1042 | void wxCairoPathData::Transform( const wxGraphicsMatrixData* matrix ) | |
1043 | { | |
1044 | // as we don't have a true path object, we have to apply the inverse | |
1045 | // matrix to the context | |
1046 | cairo_matrix_t m = *((cairo_matrix_t*) matrix->GetNativeMatrix()); | |
1047 | cairo_matrix_invert( &m ); | |
1048 | cairo_transform(m_pathContext,&m); | |
1049 | } | |
1050 | ||
1051 | // gets the bounding box enclosing all points (possibly including control points) | |
1052 | void wxCairoPathData::GetBox(wxDouble *x, wxDouble *y, wxDouble *w, wxDouble *h) const | |
1053 | { | |
1054 | double x1,y1,x2,y2; | |
1055 | ||
1056 | cairo_stroke_extents( m_pathContext, &x1, &y1, &x2, &y2 ); | |
1057 | if ( x2 < x1 ) | |
1058 | { | |
1059 | *x = x2; | |
1060 | *w = x1-x2; | |
1061 | } | |
1062 | else | |
1063 | { | |
1064 | *x = x1; | |
1065 | *w = x2-x1; | |
1066 | } | |
1067 | ||
1068 | if( y2 < y1 ) | |
1069 | { | |
1070 | *y = y2; | |
1071 | *h = y1-y2; | |
1072 | } | |
1073 | else | |
1074 | { | |
1075 | *y = y1; | |
1076 | *h = y2-y1; | |
1077 | } | |
1078 | } | |
1079 | ||
1080 | bool wxCairoPathData::Contains( wxDouble x, wxDouble y, wxPolygonFillMode fillStyle ) const | |
1081 | { | |
1082 | cairo_set_fill_rule(m_pathContext,fillStyle==wxODDEVEN_RULE ? CAIRO_FILL_RULE_EVEN_ODD : CAIRO_FILL_RULE_WINDING); | |
1083 | return cairo_in_fill( m_pathContext, x, y) != 0; | |
1084 | } | |
1085 | ||
1086 | //----------------------------------------------------------------------------- | |
1087 | // wxCairoMatrixData implementation | |
1088 | //----------------------------------------------------------------------------- | |
1089 | ||
1090 | wxCairoMatrixData::wxCairoMatrixData(wxGraphicsRenderer* renderer, const cairo_matrix_t* matrix ) | |
1091 | : wxGraphicsMatrixData(renderer) | |
1092 | { | |
1093 | if ( matrix ) | |
1094 | m_matrix = *matrix; | |
1095 | } | |
1096 | ||
1097 | wxCairoMatrixData::~wxCairoMatrixData() | |
1098 | { | |
1099 | // nothing to do | |
1100 | } | |
1101 | ||
1102 | wxGraphicsObjectRefData *wxCairoMatrixData::Clone() const | |
1103 | { | |
1104 | return new wxCairoMatrixData(GetRenderer(),&m_matrix); | |
1105 | } | |
1106 | ||
1107 | // concatenates the matrix | |
1108 | void wxCairoMatrixData::Concat( const wxGraphicsMatrixData *t ) | |
1109 | { | |
1110 | cairo_matrix_multiply( &m_matrix, &m_matrix, (cairo_matrix_t*) t->GetNativeMatrix()); | |
1111 | } | |
1112 | ||
1113 | // sets the matrix to the respective values | |
1114 | void wxCairoMatrixData::Set(wxDouble a, wxDouble b, wxDouble c, wxDouble d, | |
1115 | wxDouble tx, wxDouble ty) | |
1116 | { | |
1117 | cairo_matrix_init( &m_matrix, a, b, c, d, tx, ty); | |
1118 | } | |
1119 | ||
1120 | // gets the component valuess of the matrix | |
1121 | void wxCairoMatrixData::Get(wxDouble* a, wxDouble* b, wxDouble* c, | |
1122 | wxDouble* d, wxDouble* tx, wxDouble* ty) const | |
1123 | { | |
1124 | if (a) *a = m_matrix.xx; | |
1125 | if (b) *b = m_matrix.yx; | |
1126 | if (c) *c = m_matrix.xy; | |
1127 | if (d) *d = m_matrix.yy; | |
1128 | if (tx) *tx= m_matrix.x0; | |
1129 | if (ty) *ty= m_matrix.y0; | |
1130 | } | |
1131 | ||
1132 | // makes this the inverse matrix | |
1133 | void wxCairoMatrixData::Invert() | |
1134 | { | |
1135 | cairo_matrix_invert( &m_matrix ); | |
1136 | } | |
1137 | ||
1138 | // returns true if the elements of the transformation matrix are equal ? | |
1139 | bool wxCairoMatrixData::IsEqual( const wxGraphicsMatrixData* t) const | |
1140 | { | |
1141 | const cairo_matrix_t* tm = (cairo_matrix_t*) t->GetNativeMatrix(); | |
1142 | return ( | |
1143 | m_matrix.xx == tm->xx && | |
1144 | m_matrix.yx == tm->yx && | |
1145 | m_matrix.xy == tm->xy && | |
1146 | m_matrix.yy == tm->yy && | |
1147 | m_matrix.x0 == tm->x0 && | |
1148 | m_matrix.y0 == tm->y0 ) ; | |
1149 | } | |
1150 | ||
1151 | // return true if this is the identity matrix | |
1152 | bool wxCairoMatrixData::IsIdentity() const | |
1153 | { | |
1154 | return ( m_matrix.xx == 1 && m_matrix.yy == 1 && | |
1155 | m_matrix.yx == 0 && m_matrix.xy == 0 && m_matrix.x0 == 0 && m_matrix.y0 == 0); | |
1156 | } | |
1157 | ||
1158 | // | |
1159 | // transformation | |
1160 | // | |
1161 | ||
1162 | // add the translation to this matrix | |
1163 | void wxCairoMatrixData::Translate( wxDouble dx , wxDouble dy ) | |
1164 | { | |
1165 | cairo_matrix_translate( &m_matrix, dx, dy) ; | |
1166 | } | |
1167 | ||
1168 | // add the scale to this matrix | |
1169 | void wxCairoMatrixData::Scale( wxDouble xScale , wxDouble yScale ) | |
1170 | { | |
1171 | cairo_matrix_scale( &m_matrix, xScale, yScale) ; | |
1172 | } | |
1173 | ||
1174 | // add the rotation to this matrix (radians) | |
1175 | void wxCairoMatrixData::Rotate( wxDouble angle ) | |
1176 | { | |
1177 | cairo_matrix_rotate( &m_matrix, angle) ; | |
1178 | } | |
1179 | ||
1180 | // | |
1181 | // apply the transforms | |
1182 | // | |
1183 | ||
1184 | // applies that matrix to the point | |
1185 | void wxCairoMatrixData::TransformPoint( wxDouble *x, wxDouble *y ) const | |
1186 | { | |
1187 | double lx = *x, ly = *y ; | |
1188 | cairo_matrix_transform_point( &m_matrix, &lx, &ly); | |
1189 | *x = lx; | |
1190 | *y = ly; | |
1191 | } | |
1192 | ||
1193 | // applies the matrix except for translations | |
1194 | void wxCairoMatrixData::TransformDistance( wxDouble *dx, wxDouble *dy ) const | |
1195 | { | |
1196 | double lx = *dx, ly = *dy ; | |
1197 | cairo_matrix_transform_distance( &m_matrix, &lx, &ly); | |
1198 | *dx = lx; | |
1199 | *dy = ly; | |
1200 | } | |
1201 | ||
1202 | // returns the native representation | |
1203 | void * wxCairoMatrixData::GetNativeMatrix() const | |
1204 | { | |
1205 | return (void*) &m_matrix; | |
1206 | } | |
1207 | ||
1208 | // ---------------------------------------------------------------------------- | |
1209 | // wxCairoBitmap implementation | |
1210 | // ---------------------------------------------------------------------------- | |
1211 | ||
1212 | int wxCairoBitmapData::InitBuffer(int width, int height, cairo_format_t format) | |
1213 | { | |
1214 | wxUnusedVar(format); // Only really unused with Cairo < 1.6. | |
1215 | ||
1216 | // Determine the stride: use cairo_format_stride_for_width() if available | |
1217 | // but fall back to 4*width for the earlier versions as this is what that | |
1218 | // function always returns, even in latest Cairo, anyhow. | |
1219 | int stride; | |
1220 | #if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 6, 0) | |
1221 | if ( cairo_version() >= CAIRO_VERSION_ENCODE(1, 6, 0) ) | |
1222 | { | |
1223 | stride = cairo_format_stride_for_width(format, width); | |
1224 | ||
1225 | // All our code would totally break if stride were not a multiple of 4 | |
1226 | // so ensure this is the case. | |
1227 | if ( stride % 4 ) | |
1228 | { | |
1229 | wxFAIL_MSG("Unexpected Cairo image surface stride."); | |
1230 | ||
1231 | stride += 4 - stride % 4; | |
1232 | } | |
1233 | } | |
1234 | else | |
1235 | #endif | |
1236 | stride = 4*width; | |
1237 | ||
1238 | m_width = width; | |
1239 | m_height = height; | |
1240 | m_buffer = new unsigned char[height*stride]; | |
1241 | ||
1242 | return stride; | |
1243 | } | |
1244 | ||
1245 | void wxCairoBitmapData::InitSurface(cairo_format_t format, int stride) | |
1246 | { | |
1247 | m_surface = cairo_image_surface_create_for_data( | |
1248 | m_buffer, format, m_width, m_height, stride); | |
1249 | m_pattern = cairo_pattern_create_for_surface(m_surface); | |
1250 | } | |
1251 | ||
1252 | wxCairoBitmapData::wxCairoBitmapData( wxGraphicsRenderer* renderer, cairo_surface_t* bitmap ) : | |
1253 | wxGraphicsObjectRefData( renderer ) | |
1254 | { | |
1255 | m_surface = bitmap; | |
1256 | m_pattern = cairo_pattern_create_for_surface(m_surface); | |
1257 | } | |
1258 | ||
1259 | wxCairoBitmapData::wxCairoBitmapData( wxGraphicsRenderer* renderer, const wxBitmap& bmp ) : wxGraphicsObjectRefData( renderer ) | |
1260 | { | |
1261 | wxCHECK_RET( bmp.IsOk(), wxT("Invalid bitmap in wxCairoContext::DrawBitmap")); | |
1262 | ||
1263 | #ifdef wxHAS_RAW_BITMAP | |
1264 | // Create a surface object and copy the bitmap pixel data to it. if the | |
1265 | // image has alpha (or a mask represented as alpha) then we'll use a | |
1266 | // different format and iterator than if it doesn't... | |
1267 | cairo_format_t bufferFormat = bmp.GetDepth() == 32 | |
1268 | #ifdef __WXGTK__ | |
1269 | || bmp.GetMask() | |
1270 | #endif | |
1271 | ? CAIRO_FORMAT_ARGB32 | |
1272 | : CAIRO_FORMAT_RGB24; | |
1273 | ||
1274 | int stride = InitBuffer(bmp.GetWidth(), bmp.GetHeight(), bufferFormat); | |
1275 | ||
1276 | wxBitmap bmpSource = bmp; // we need a non-const instance | |
1277 | wxUint32* data = (wxUint32*)m_buffer; | |
1278 | ||
1279 | if ( bufferFormat == CAIRO_FORMAT_ARGB32 ) | |
1280 | { | |
1281 | // use the bitmap's alpha | |
1282 | wxAlphaPixelData | |
1283 | pixData(bmpSource, wxPoint(0, 0), wxSize(m_width, m_height)); | |
1284 | wxCHECK_RET( pixData, wxT("Failed to gain raw access to bitmap data.")); | |
1285 | ||
1286 | wxAlphaPixelData::Iterator p(pixData); | |
1287 | for (int y=0; y<m_height; y++) | |
1288 | { | |
1289 | wxAlphaPixelData::Iterator rowStart = p; | |
1290 | wxUint32* const rowStartDst = data; | |
1291 | for (int x=0; x<m_width; x++) | |
1292 | { | |
1293 | // Each pixel in CAIRO_FORMAT_ARGB32 is a 32-bit quantity, | |
1294 | // with alpha in the upper 8 bits, then red, then green, then | |
1295 | // blue. The 32-bit quantities are stored native-endian. | |
1296 | // Pre-multiplied alpha is used. | |
1297 | unsigned char alpha = p.Alpha(); | |
1298 | if (alpha == 0) | |
1299 | *data = 0; | |
1300 | else | |
1301 | *data = ( alpha << 24 | |
1302 | | (p.Red() * alpha/255) << 16 | |
1303 | | (p.Green() * alpha/255) << 8 | |
1304 | | (p.Blue() * alpha/255) ); | |
1305 | ++data; | |
1306 | ++p; | |
1307 | } | |
1308 | ||
1309 | data = rowStartDst + stride / 4; | |
1310 | p = rowStart; | |
1311 | p.OffsetY(pixData, 1); | |
1312 | } | |
1313 | } | |
1314 | else // no alpha | |
1315 | { | |
1316 | wxNativePixelData | |
1317 | pixData(bmpSource, wxPoint(0, 0), wxSize(m_width, m_height)); | |
1318 | wxCHECK_RET( pixData, wxT("Failed to gain raw access to bitmap data.")); | |
1319 | ||
1320 | wxNativePixelData::Iterator p(pixData); | |
1321 | for (int y=0; y<m_height; y++) | |
1322 | { | |
1323 | wxNativePixelData::Iterator rowStart = p; | |
1324 | wxUint32* const rowStartDst = data; | |
1325 | for (int x=0; x<m_width; x++) | |
1326 | { | |
1327 | // Each pixel in CAIRO_FORMAT_RGB24 is a 32-bit quantity, with | |
1328 | // the upper 8 bits unused. Red, Green, and Blue are stored in | |
1329 | // the remaining 24 bits in that order. The 32-bit quantities | |
1330 | // are stored native-endian. | |
1331 | *data = ( p.Red() << 16 | p.Green() << 8 | p.Blue() ); | |
1332 | ++data; | |
1333 | ++p; | |
1334 | } | |
1335 | ||
1336 | data = rowStartDst + stride / 4; | |
1337 | p = rowStart; | |
1338 | p.OffsetY(pixData, 1); | |
1339 | } | |
1340 | } | |
1341 | #ifdef __WXMSW__ | |
1342 | // if there is a mask, set the alpha bytes in the target buffer to | |
1343 | // fully transparent or fully opaque | |
1344 | if (bmpSource.GetMask()) | |
1345 | { | |
1346 | wxBitmap bmpMask = bmpSource.GetMaskBitmap(); | |
1347 | bufferFormat = CAIRO_FORMAT_ARGB32; | |
1348 | data = (wxUint32*)m_buffer; | |
1349 | wxNativePixelData | |
1350 | pixData(bmpMask, wxPoint(0, 0), wxSize(m_width, m_height)); | |
1351 | wxCHECK_RET( pixData, wxT("Failed to gain raw access to mask data.")); | |
1352 | ||
1353 | wxNativePixelData::Iterator p(pixData); | |
1354 | for (int y=0; y<m_height; y++) | |
1355 | { | |
1356 | wxNativePixelData::Iterator rowStart = p; | |
1357 | wxUint32* const rowStartDst = data; | |
1358 | for (int x=0; x<m_width; x++) | |
1359 | { | |
1360 | if (p.Red()+p.Green()+p.Blue() == 0) | |
1361 | *data = 0; | |
1362 | else | |
1363 | *data = (wxALPHA_OPAQUE << 24) | (*data & 0x00FFFFFF); | |
1364 | ++data; | |
1365 | ++p; | |
1366 | } | |
1367 | ||
1368 | data = rowStartDst + stride / 4; | |
1369 | p = rowStart; | |
1370 | p.OffsetY(pixData, 1); | |
1371 | } | |
1372 | } | |
1373 | #endif | |
1374 | ||
1375 | InitSurface(bufferFormat, stride); | |
1376 | #endif // wxHAS_RAW_BITMAP | |
1377 | } | |
1378 | ||
1379 | #if wxUSE_IMAGE | |
1380 | ||
1381 | // Helper functions for dealing with alpha pre-multiplication. | |
1382 | namespace | |
1383 | { | |
1384 | ||
1385 | inline unsigned char Premultiply(unsigned char alpha, unsigned char data) | |
1386 | { | |
1387 | return alpha ? (data * alpha)/0xff : data; | |
1388 | } | |
1389 | ||
1390 | inline unsigned char Unpremultiply(unsigned char alpha, unsigned char data) | |
1391 | { | |
1392 | return alpha ? (data * 0xff)/alpha : data; | |
1393 | } | |
1394 | ||
1395 | } // anonymous namespace | |
1396 | ||
1397 | wxCairoBitmapData::wxCairoBitmapData(wxGraphicsRenderer* renderer, | |
1398 | const wxImage& image) | |
1399 | : wxGraphicsObjectRefData(renderer) | |
1400 | { | |
1401 | const cairo_format_t bufferFormat = image.HasAlpha() | |
1402 | ? CAIRO_FORMAT_ARGB32 | |
1403 | : CAIRO_FORMAT_RGB24; | |
1404 | ||
1405 | int stride = InitBuffer(image.GetWidth(), image.GetHeight(), bufferFormat); | |
1406 | ||
1407 | // Copy wxImage data into the buffer. Notice that we work with wxUint32 | |
1408 | // values and not bytes becase Cairo always works with buffers in native | |
1409 | // endianness. | |
1410 | wxUint32* dst = reinterpret_cast<wxUint32*>(m_buffer); | |
1411 | const unsigned char* src = image.GetData(); | |
1412 | ||
1413 | if ( bufferFormat == CAIRO_FORMAT_ARGB32 ) | |
1414 | { | |
1415 | const unsigned char* alpha = image.GetAlpha(); | |
1416 | ||
1417 | for ( int y = 0; y < m_height; y++ ) | |
1418 | { | |
1419 | wxUint32* const rowStartDst = dst; | |
1420 | ||
1421 | for ( int x = 0; x < m_width; x++ ) | |
1422 | { | |
1423 | const unsigned char a = *alpha++; | |
1424 | ||
1425 | *dst++ = a << 24 | | |
1426 | Premultiply(a, src[0]) << 16 | | |
1427 | Premultiply(a, src[1]) << 8 | | |
1428 | Premultiply(a, src[2]); | |
1429 | src += 3; | |
1430 | } | |
1431 | ||
1432 | dst = rowStartDst + stride / 4; | |
1433 | } | |
1434 | } | |
1435 | else // RGB | |
1436 | { | |
1437 | for ( int y = 0; y < m_height; y++ ) | |
1438 | { | |
1439 | wxUint32* const rowStartDst = dst; | |
1440 | ||
1441 | for ( int x = 0; x < m_width; x++ ) | |
1442 | { | |
1443 | *dst++ = src[0] << 16 | | |
1444 | src[1] << 8 | | |
1445 | src[2]; | |
1446 | src += 3; | |
1447 | } | |
1448 | ||
1449 | dst = rowStartDst + stride / 4; | |
1450 | } | |
1451 | } | |
1452 | ||
1453 | InitSurface(bufferFormat, stride); | |
1454 | } | |
1455 | ||
1456 | wxImage wxCairoBitmapData::ConvertToImage() const | |
1457 | { | |
1458 | wxImage image(m_width, m_height, false /* don't clear */); | |
1459 | ||
1460 | // Get the surface type and format. | |
1461 | wxCHECK_MSG( cairo_surface_get_type(m_surface) == CAIRO_SURFACE_TYPE_IMAGE, | |
1462 | wxNullImage, | |
1463 | wxS("Can't convert non-image surface to image.") ); | |
1464 | ||
1465 | switch ( cairo_image_surface_get_format(m_surface) ) | |
1466 | { | |
1467 | case CAIRO_FORMAT_ARGB32: | |
1468 | image.SetAlpha(); | |
1469 | break; | |
1470 | ||
1471 | case CAIRO_FORMAT_RGB24: | |
1472 | // Nothing to do, we don't use alpha by default. | |
1473 | break; | |
1474 | ||
1475 | case CAIRO_FORMAT_A8: | |
1476 | case CAIRO_FORMAT_A1: | |
1477 | wxFAIL_MSG(wxS("Unsupported Cairo image surface type.")); | |
1478 | return wxNullImage; | |
1479 | ||
1480 | default: | |
1481 | wxFAIL_MSG(wxS("Unknown Cairo image surface type.")); | |
1482 | return wxNullImage; | |
1483 | } | |
1484 | ||
1485 | // Prepare for copying data. | |
1486 | const wxUint32* src = (wxUint32*)cairo_image_surface_get_data(m_surface); | |
1487 | wxCHECK_MSG( src, wxNullImage, wxS("Failed to get Cairo surface data.") ); | |
1488 | ||
1489 | int stride = cairo_image_surface_get_stride(m_surface); | |
1490 | wxCHECK_MSG( stride > 0, wxNullImage, | |
1491 | wxS("Failed to get Cairo surface stride.") ); | |
1492 | ||
1493 | // As we work with wxUint32 pointers and not char ones, we need to adjust | |
1494 | // the stride accordingly. This should be lossless as the stride must be a | |
1495 | // multiple of pixel size. | |
1496 | wxASSERT_MSG( !(stride % sizeof(wxUint32)), wxS("Unexpected stride.") ); | |
1497 | stride /= sizeof(wxUint32); | |
1498 | ||
1499 | unsigned char* dst = image.GetData(); | |
1500 | unsigned char *alpha = image.GetAlpha(); | |
1501 | if ( alpha ) | |
1502 | { | |
1503 | // We need to also copy alpha and undo the pre-multiplication as Cairo | |
1504 | // stores pre-multiplied values in this format while wxImage does not. | |
1505 | for ( int y = 0; y < m_height; y++ ) | |
1506 | { | |
1507 | const wxUint32* const rowStart = src; | |
1508 | for ( int x = 0; x < m_width; x++ ) | |
1509 | { | |
1510 | const wxUint32 argb = *src++; | |
1511 | ||
1512 | *alpha++ = (argb & 0xff000000) >> 24; | |
1513 | ||
1514 | // Copy the RGB data undoing the pre-multiplication. | |
1515 | *dst++ = Unpremultiply(*alpha, (argb & 0x00ff0000) >> 16); | |
1516 | *dst++ = Unpremultiply(*alpha, (argb & 0x0000ff00) >> 8); | |
1517 | *dst++ = Unpremultiply(*alpha, (argb & 0x000000ff)); | |
1518 | } | |
1519 | ||
1520 | src = rowStart + stride; | |
1521 | } | |
1522 | } | |
1523 | else // RGB | |
1524 | { | |
1525 | // Things are pretty simple in this case, just copy RGB bytes. | |
1526 | for ( int y = 0; y < m_height; y++ ) | |
1527 | { | |
1528 | const wxUint32* const rowStart = src; | |
1529 | for ( int x = 0; x < m_width; x++ ) | |
1530 | { | |
1531 | const wxUint32 argb = *src++; | |
1532 | ||
1533 | *dst++ = (argb & 0x00ff0000) >> 16; | |
1534 | *dst++ = (argb & 0x0000ff00) >> 8; | |
1535 | *dst++ = (argb & 0x000000ff); | |
1536 | } | |
1537 | ||
1538 | src = rowStart + stride; | |
1539 | } | |
1540 | } | |
1541 | ||
1542 | return image; | |
1543 | } | |
1544 | ||
1545 | #endif // wxUSE_IMAGE | |
1546 | ||
1547 | wxCairoBitmapData::~wxCairoBitmapData() | |
1548 | { | |
1549 | if (m_pattern) | |
1550 | cairo_pattern_destroy(m_pattern); | |
1551 | ||
1552 | if (m_surface) | |
1553 | cairo_surface_destroy(m_surface); | |
1554 | ||
1555 | delete [] m_buffer; | |
1556 | } | |
1557 | ||
1558 | //----------------------------------------------------------------------------- | |
1559 | // wxCairoContext implementation | |
1560 | //----------------------------------------------------------------------------- | |
1561 | ||
1562 | class wxCairoOffsetHelper | |
1563 | { | |
1564 | public : | |
1565 | wxCairoOffsetHelper( cairo_t* ctx , bool offset ) | |
1566 | { | |
1567 | m_ctx = ctx; | |
1568 | m_offset = offset; | |
1569 | if ( m_offset ) | |
1570 | cairo_translate( m_ctx, 0.5, 0.5 ); | |
1571 | } | |
1572 | ~wxCairoOffsetHelper( ) | |
1573 | { | |
1574 | if ( m_offset ) | |
1575 | cairo_translate( m_ctx, -0.5, -0.5 ); | |
1576 | } | |
1577 | public : | |
1578 | cairo_t* m_ctx; | |
1579 | bool m_offset; | |
1580 | } ; | |
1581 | ||
1582 | wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, const wxPrinterDC& dc ) | |
1583 | : wxGraphicsContext(renderer) | |
1584 | { | |
1585 | #ifdef __WXMSW__ | |
1586 | // wxMSW contexts always use MM_ANISOTROPIC, which messes up | |
1587 | // text rendering when printing using Cairo. Switch it to MM_TEXT | |
1588 | // map mode to avoid this problem. | |
1589 | HDC hdc = (HDC)dc.GetHDC(); | |
1590 | ::SetMapMode(hdc, MM_TEXT); | |
1591 | m_mswSurface = cairo_win32_printing_surface_create(hdc); | |
1592 | Init( cairo_create(m_mswSurface) ); | |
1593 | #endif | |
1594 | ||
1595 | #ifdef __WXGTK20__ | |
1596 | const wxDCImpl *impl = dc.GetImpl(); | |
1597 | Init( (cairo_t*) impl->GetCairoContext() ); | |
1598 | #endif | |
1599 | wxSize sz = dc.GetSize(); | |
1600 | m_width = sz.x; | |
1601 | m_height = sz.y; | |
1602 | ||
1603 | wxPoint org = dc.GetDeviceOrigin(); | |
1604 | cairo_translate( m_context, org.x, org.y ); | |
1605 | ||
1606 | double sx,sy; | |
1607 | dc.GetUserScale( &sx, &sy ); | |
1608 | ||
1609 | // TODO: Determine if these fixes are needed on other platforms too. | |
1610 | // On MSW, without this the printer context will not respect wxDC SetMapMode calls. | |
1611 | // For example, using dc.SetMapMode(wxMM_POINTS) can let us share printer and screen | |
1612 | // drawing code | |
1613 | #ifdef __WXMSW__ | |
1614 | double lsx,lsy; | |
1615 | dc.GetLogicalScale( &lsx, &lsy ); | |
1616 | sx *= lsx; | |
1617 | sy *= lsy; | |
1618 | #endif | |
1619 | cairo_scale( m_context, sx, sy ); | |
1620 | ||
1621 | org = dc.GetLogicalOrigin(); | |
1622 | cairo_translate( m_context, -org.x, -org.y ); | |
1623 | } | |
1624 | ||
1625 | wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, const wxWindowDC& dc ) | |
1626 | : wxGraphicsContext(renderer) | |
1627 | { | |
1628 | int width, height; | |
1629 | dc.GetSize( &width, &height ); | |
1630 | m_width = width; | |
1631 | m_height = height; | |
1632 | ||
1633 | m_enableOffset = true; | |
1634 | ||
1635 | #ifdef __WXMSW__ | |
1636 | m_mswSurface = cairo_win32_surface_create((HDC)dc.GetHDC()); | |
1637 | Init( cairo_create(m_mswSurface) ); | |
1638 | #endif | |
1639 | ||
1640 | #ifdef __WXGTK20__ | |
1641 | wxGTKDCImpl *impldc = (wxGTKDCImpl*) dc.GetImpl(); | |
1642 | Init( gdk_cairo_create( impldc->GetGDKWindow() ) ); | |
1643 | ||
1644 | #if 0 | |
1645 | wxGraphicsMatrix matrix = CreateMatrix(); | |
1646 | ||
1647 | wxPoint org = dc.GetDeviceOrigin(); | |
1648 | matrix.Translate( org.x, org.y ); | |
1649 | ||
1650 | org = dc.GetLogicalOrigin(); | |
1651 | matrix.Translate( -org.x, -org.y ); | |
1652 | ||
1653 | double sx,sy; | |
1654 | dc.GetUserScale( &sx, &sy ); | |
1655 | matrix.Scale( sx, sy ); | |
1656 | ||
1657 | ConcatTransform( matrix ); | |
1658 | #endif | |
1659 | #endif | |
1660 | ||
1661 | #ifdef __WXMAC__ | |
1662 | CGContextRef cgcontext = (CGContextRef)dc.GetWindow()->MacGetCGContextRef(); | |
1663 | cairo_surface_t* surface = cairo_quartz_surface_create_for_cg_context(cgcontext, width, height); | |
1664 | Init( cairo_create( surface ) ); | |
1665 | cairo_surface_destroy( surface ); | |
1666 | #endif | |
1667 | } | |
1668 | ||
1669 | wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, const wxMemoryDC& dc ) | |
1670 | : wxGraphicsContext(renderer) | |
1671 | { | |
1672 | int width, height; | |
1673 | dc.GetSize( &width, &height ); | |
1674 | m_width = width; | |
1675 | m_height = height; | |
1676 | ||
1677 | m_enableOffset = true; | |
1678 | ||
1679 | #ifdef __WXMSW__ | |
1680 | m_mswSurface = cairo_win32_surface_create((HDC)dc.GetHDC()); | |
1681 | Init( cairo_create(m_mswSurface) ); | |
1682 | #endif | |
1683 | ||
1684 | #ifdef __WXGTK20__ | |
1685 | wxGTKDCImpl *impldc = (wxGTKDCImpl*) dc.GetImpl(); | |
1686 | Init( gdk_cairo_create( impldc->GetGDKWindow() ) ); | |
1687 | ||
1688 | #if 0 | |
1689 | wxGraphicsMatrix matrix = CreateMatrix(); | |
1690 | ||
1691 | wxPoint org = dc.GetDeviceOrigin(); | |
1692 | matrix.Translate( org.x, org.y ); | |
1693 | ||
1694 | org = dc.GetLogicalOrigin(); | |
1695 | matrix.Translate( -org.x, -org.y ); | |
1696 | ||
1697 | double sx,sy; | |
1698 | dc.GetUserScale( &sx, &sy ); | |
1699 | matrix.Scale( sx, sy ); | |
1700 | ||
1701 | ConcatTransform( matrix ); | |
1702 | #endif | |
1703 | #endif | |
1704 | ||
1705 | #ifdef __WXMAC__ | |
1706 | CGContextRef cgcontext = (CGContextRef)dc.GetWindow()->MacGetCGContextRef(); | |
1707 | cairo_surface_t* surface = cairo_quartz_surface_create_for_cg_context(cgcontext, width, height); | |
1708 | Init( cairo_create( surface ) ); | |
1709 | cairo_surface_destroy( surface ); | |
1710 | #endif | |
1711 | } | |
1712 | ||
1713 | #ifdef __WXGTK20__ | |
1714 | wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, GdkDrawable *drawable ) | |
1715 | : wxGraphicsContext(renderer) | |
1716 | { | |
1717 | Init( gdk_cairo_create( drawable ) ); | |
1718 | ||
1719 | int width, height; | |
1720 | gdk_drawable_get_size( drawable, &width, &height ); | |
1721 | m_width = width; | |
1722 | m_height = height; | |
1723 | } | |
1724 | #endif | |
1725 | ||
1726 | #ifdef __WXMSW__ | |
1727 | wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, HDC handle ) | |
1728 | : wxGraphicsContext(renderer) | |
1729 | { | |
1730 | m_mswSurface = cairo_win32_surface_create(handle); | |
1731 | Init( cairo_create(m_mswSurface) ); | |
1732 | m_width = | |
1733 | m_height = 0; | |
1734 | } | |
1735 | #endif | |
1736 | ||
1737 | ||
1738 | wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, cairo_t *context ) | |
1739 | : wxGraphicsContext(renderer) | |
1740 | { | |
1741 | Init( context ); | |
1742 | m_width = | |
1743 | m_height = 0; | |
1744 | } | |
1745 | ||
1746 | wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, wxWindow *window) | |
1747 | : wxGraphicsContext(renderer) | |
1748 | { | |
1749 | m_enableOffset = true; | |
1750 | #ifdef __WXGTK__ | |
1751 | // something along these lines (copied from dcclient) | |
1752 | ||
1753 | // Some controls don't have m_wxwindow - like wxStaticBox, but the user | |
1754 | // code should still be able to create wxClientDCs for them, so we will | |
1755 | // use the parent window here then. | |
1756 | if (window->m_wxwindow == NULL) | |
1757 | { | |
1758 | window = window->GetParent(); | |
1759 | } | |
1760 | ||
1761 | wxASSERT_MSG( window->m_wxwindow, wxT("wxCairoContext needs a widget") ); | |
1762 | ||
1763 | Init(gdk_cairo_create(window->GTKGetDrawingWindow())); | |
1764 | ||
1765 | wxSize sz = window->GetSize(); | |
1766 | m_width = sz.x; | |
1767 | m_height = sz.y; | |
1768 | #endif | |
1769 | ||
1770 | #ifdef __WXMSW__ | |
1771 | m_mswSurface = cairo_win32_surface_create((HDC)window->GetHandle()); | |
1772 | Init(cairo_create(m_mswSurface)); | |
1773 | #endif | |
1774 | ||
1775 | } | |
1776 | ||
1777 | wxCairoContext::wxCairoContext(wxGraphicsRenderer* renderer) : | |
1778 | wxGraphicsContext(renderer) | |
1779 | { | |
1780 | m_context = NULL; | |
1781 | } | |
1782 | ||
1783 | wxCairoContext::~wxCairoContext() | |
1784 | { | |
1785 | if ( m_context ) | |
1786 | { | |
1787 | PopState(); | |
1788 | PopState(); | |
1789 | cairo_destroy(m_context); | |
1790 | } | |
1791 | #ifdef __WXMSW__ | |
1792 | if ( m_mswSurface ) | |
1793 | cairo_surface_destroy(m_mswSurface); | |
1794 | #endif | |
1795 | } | |
1796 | ||
1797 | void wxCairoContext::Init(cairo_t *context) | |
1798 | { | |
1799 | m_context = context ; | |
1800 | PushState(); | |
1801 | PushState(); | |
1802 | } | |
1803 | ||
1804 | ||
1805 | void wxCairoContext::Clip( const wxRegion& region ) | |
1806 | { | |
1807 | // Create a path with all the rectangles in the region | |
1808 | wxGraphicsPath path = GetRenderer()->CreatePath(); | |
1809 | wxRegionIterator ri(region); | |
1810 | while (ri) | |
1811 | { | |
1812 | path.AddRectangle(ri.GetX(), ri.GetY(), ri.GetW(), ri.GetH()); | |
1813 | ++ri; | |
1814 | } | |
1815 | ||
1816 | // Put it in the context | |
1817 | cairo_path_t* cp = (cairo_path_t*) path.GetNativePath() ; | |
1818 | cairo_append_path(m_context, cp); | |
1819 | ||
1820 | // clip to that path | |
1821 | cairo_clip(m_context); | |
1822 | path.UnGetNativePath(cp); | |
1823 | } | |
1824 | ||
1825 | void wxCairoContext::Clip( wxDouble x, wxDouble y, wxDouble w, wxDouble h ) | |
1826 | { | |
1827 | // Create a path with this rectangle | |
1828 | wxGraphicsPath path = GetRenderer()->CreatePath(); | |
1829 | path.AddRectangle(x,y,w,h); | |
1830 | ||
1831 | // Put it in the context | |
1832 | cairo_path_t* cp = (cairo_path_t*) path.GetNativePath() ; | |
1833 | cairo_append_path(m_context, cp); | |
1834 | ||
1835 | // clip to that path | |
1836 | cairo_clip(m_context); | |
1837 | path.UnGetNativePath(cp); | |
1838 | } | |
1839 | ||
1840 | void wxCairoContext::ResetClip() | |
1841 | { | |
1842 | cairo_reset_clip(m_context); | |
1843 | } | |
1844 | ||
1845 | ||
1846 | void wxCairoContext::StrokePath( const wxGraphicsPath& path ) | |
1847 | { | |
1848 | if ( !m_pen.IsNull() ) | |
1849 | { | |
1850 | wxCairoOffsetHelper helper( m_context, ShouldOffset() ) ; | |
1851 | cairo_path_t* cp = (cairo_path_t*) path.GetNativePath() ; | |
1852 | cairo_append_path(m_context,cp); | |
1853 | ((wxCairoPenData*)m_pen.GetRefData())->Apply(this); | |
1854 | cairo_stroke(m_context); | |
1855 | path.UnGetNativePath(cp); | |
1856 | } | |
1857 | } | |
1858 | ||
1859 | void wxCairoContext::FillPath( const wxGraphicsPath& path , wxPolygonFillMode fillStyle ) | |
1860 | { | |
1861 | if ( !m_brush.IsNull() ) | |
1862 | { | |
1863 | wxCairoOffsetHelper helper( m_context, ShouldOffset() ) ; | |
1864 | cairo_path_t* cp = (cairo_path_t*) path.GetNativePath() ; | |
1865 | cairo_append_path(m_context,cp); | |
1866 | ((wxCairoBrushData*)m_brush.GetRefData())->Apply(this); | |
1867 | cairo_set_fill_rule(m_context,fillStyle==wxODDEVEN_RULE ? CAIRO_FILL_RULE_EVEN_ODD : CAIRO_FILL_RULE_WINDING); | |
1868 | cairo_fill(m_context); | |
1869 | path.UnGetNativePath(cp); | |
1870 | } | |
1871 | } | |
1872 | ||
1873 | void wxCairoContext::Rotate( wxDouble angle ) | |
1874 | { | |
1875 | cairo_rotate(m_context,angle); | |
1876 | } | |
1877 | ||
1878 | void wxCairoContext::Translate( wxDouble dx , wxDouble dy ) | |
1879 | { | |
1880 | cairo_translate(m_context,dx,dy); | |
1881 | } | |
1882 | ||
1883 | void wxCairoContext::Scale( wxDouble xScale , wxDouble yScale ) | |
1884 | { | |
1885 | cairo_scale(m_context,xScale,yScale); | |
1886 | } | |
1887 | ||
1888 | // concatenates this transform with the current transform of this context | |
1889 | void wxCairoContext::ConcatTransform( const wxGraphicsMatrix& matrix ) | |
1890 | { | |
1891 | cairo_transform(m_context,(const cairo_matrix_t *) matrix.GetNativeMatrix()); | |
1892 | } | |
1893 | ||
1894 | // sets the transform of this context | |
1895 | void wxCairoContext::SetTransform( const wxGraphicsMatrix& matrix ) | |
1896 | { | |
1897 | cairo_set_matrix(m_context,(const cairo_matrix_t*) matrix.GetNativeMatrix()); | |
1898 | } | |
1899 | ||
1900 | // gets the matrix of this context | |
1901 | wxGraphicsMatrix wxCairoContext::GetTransform() const | |
1902 | { | |
1903 | wxGraphicsMatrix matrix = CreateMatrix(); | |
1904 | cairo_get_matrix(m_context,(cairo_matrix_t*) matrix.GetNativeMatrix()); | |
1905 | return matrix; | |
1906 | } | |
1907 | ||
1908 | ||
1909 | ||
1910 | void wxCairoContext::PushState() | |
1911 | { | |
1912 | cairo_save(m_context); | |
1913 | } | |
1914 | ||
1915 | void wxCairoContext::PopState() | |
1916 | { | |
1917 | cairo_restore(m_context); | |
1918 | } | |
1919 | ||
1920 | void wxCairoContext::DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h ) | |
1921 | { | |
1922 | wxGraphicsBitmap bitmap = GetRenderer()->CreateBitmap(bmp); | |
1923 | DrawBitmap(bitmap, x, y, w, h); | |
1924 | ||
1925 | } | |
1926 | ||
1927 | void wxCairoContext::DrawBitmap(const wxGraphicsBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h ) | |
1928 | { | |
1929 | PushState(); | |
1930 | ||
1931 | // In case we're scaling the image by using a width and height different | |
1932 | // than the bitmap's size create a pattern transformation on the surface and | |
1933 | // draw the transformed pattern. | |
1934 | wxCairoBitmapData* data = static_cast<wxCairoBitmapData*>(bmp.GetRefData()); | |
1935 | cairo_pattern_t* pattern = data->GetCairoPattern(); | |
1936 | wxSize size = data->GetSize(); | |
1937 | ||
1938 | wxDouble scaleX = w / size.GetWidth(); | |
1939 | wxDouble scaleY = h / size.GetHeight(); | |
1940 | ||
1941 | // prepare to draw the image | |
1942 | cairo_translate(m_context, x, y); | |
1943 | cairo_scale(m_context, scaleX, scaleY); | |
1944 | cairo_set_source(m_context, pattern); | |
1945 | // use the original size here since the context is scaled already... | |
1946 | cairo_rectangle(m_context, 0, 0, size.GetWidth(), size.GetHeight()); | |
1947 | // fill the rectangle using the pattern | |
1948 | cairo_fill(m_context); | |
1949 | ||
1950 | PopState(); | |
1951 | } | |
1952 | ||
1953 | void wxCairoContext::DrawIcon( const wxIcon &icon, wxDouble x, wxDouble y, wxDouble w, wxDouble h ) | |
1954 | { | |
1955 | // An icon is a bitmap on wxGTK, so do this the easy way. When we want to | |
1956 | // start using the Cairo backend on other platforms then we may need to | |
1957 | // fiddle with this... | |
1958 | DrawBitmap(icon, x, y, w, h); | |
1959 | } | |
1960 | ||
1961 | ||
1962 | void wxCairoContext::DoDrawText(const wxString& str, wxDouble x, wxDouble y) | |
1963 | { | |
1964 | wxCHECK_RET( !m_font.IsNull(), | |
1965 | wxT("wxCairoContext::DrawText - no valid font set") ); | |
1966 | ||
1967 | if ( str.empty()) | |
1968 | return; | |
1969 | ||
1970 | const wxCharBuffer data = str.utf8_str(); | |
1971 | if ( !data ) | |
1972 | return; | |
1973 | ||
1974 | if ( ((wxCairoFontData*)m_font.GetRefData())->Apply(this) ) | |
1975 | { | |
1976 | #ifdef __WXGTK__ | |
1977 | PangoLayout *layout = pango_cairo_create_layout (m_context); | |
1978 | const wxFont& font = static_cast<wxCairoFontData*>(m_font.GetRefData())->GetFont(); | |
1979 | pango_layout_set_font_description(layout, font.GetNativeFontInfo()->description); | |
1980 | pango_layout_set_text(layout, data, data.length()); | |
1981 | font.GTKSetPangoAttrs(layout); | |
1982 | ||
1983 | cairo_move_to(m_context, x, y); | |
1984 | pango_cairo_show_layout (m_context, layout); | |
1985 | ||
1986 | g_object_unref (layout); | |
1987 | ||
1988 | // Don't use Cairo text API, we already did everything. | |
1989 | return; | |
1990 | #endif | |
1991 | } | |
1992 | ||
1993 | // Cairo's x,y for drawing text is at the baseline, so we need to adjust | |
1994 | // the position we move to by the ascent. | |
1995 | cairo_font_extents_t fe; | |
1996 | cairo_font_extents(m_context, &fe); | |
1997 | cairo_move_to(m_context, x, y+fe.ascent); | |
1998 | ||
1999 | cairo_show_text(m_context, data); | |
2000 | } | |
2001 | ||
2002 | void wxCairoContext::GetTextExtent( const wxString &str, wxDouble *width, wxDouble *height, | |
2003 | wxDouble *descent, wxDouble *externalLeading ) const | |
2004 | { | |
2005 | wxCHECK_RET( !m_font.IsNull(), wxT("wxCairoContext::GetTextExtent - no valid font set") ); | |
2006 | ||
2007 | if ( width ) | |
2008 | *width = 0; | |
2009 | if ( height ) | |
2010 | *height = 0; | |
2011 | if ( descent ) | |
2012 | *descent = 0; | |
2013 | if ( externalLeading ) | |
2014 | *externalLeading = 0; | |
2015 | ||
2016 | if ( str.empty()) | |
2017 | return; | |
2018 | ||
2019 | if ( ((wxCairoFontData*)m_font.GetRefData())->Apply((wxCairoContext*)this) ) | |
2020 | { | |
2021 | #ifdef __WXGTK__ | |
2022 | int w, h; | |
2023 | ||
2024 | PangoLayout *layout = pango_cairo_create_layout (m_context); | |
2025 | const wxFont& font = static_cast<wxCairoFontData*>(m_font.GetRefData())->GetFont(); | |
2026 | pango_layout_set_font_description(layout, font.GetNativeFontInfo()->description); | |
2027 | const wxCharBuffer data = str.utf8_str(); | |
2028 | if ( !data ) | |
2029 | { | |
2030 | return; | |
2031 | } | |
2032 | pango_layout_set_text(layout, data, data.length()); | |
2033 | pango_layout_get_pixel_size (layout, &w, &h); | |
2034 | if ( width ) | |
2035 | *width = w; | |
2036 | if ( height ) | |
2037 | *height = h; | |
2038 | if (descent) | |
2039 | { | |
2040 | PangoLayoutIter *iter = pango_layout_get_iter(layout); | |
2041 | int baseline = pango_layout_iter_get_baseline(iter); | |
2042 | pango_layout_iter_free(iter); | |
2043 | *descent = h - PANGO_PIXELS(baseline); | |
2044 | } | |
2045 | g_object_unref (layout); | |
2046 | return; | |
2047 | #endif | |
2048 | } | |
2049 | ||
2050 | if (width) | |
2051 | { | |
2052 | const wxWX2MBbuf buf(str.mb_str(wxConvUTF8)); | |
2053 | cairo_text_extents_t te; | |
2054 | cairo_text_extents(m_context, buf, &te); | |
2055 | *width = te.width; | |
2056 | } | |
2057 | ||
2058 | if (height || descent || externalLeading) | |
2059 | { | |
2060 | cairo_font_extents_t fe; | |
2061 | cairo_font_extents(m_context, &fe); | |
2062 | ||
2063 | // some backends have negative descents | |
2064 | ||
2065 | if ( fe.descent < 0 ) | |
2066 | fe.descent = -fe.descent; | |
2067 | ||
2068 | if ( fe.height < (fe.ascent + fe.descent ) ) | |
2069 | { | |
2070 | // some backends are broken re height ... (eg currently ATSUI) | |
2071 | fe.height = fe.ascent + fe.descent; | |
2072 | } | |
2073 | ||
2074 | if (height) | |
2075 | *height = fe.height; | |
2076 | if ( descent ) | |
2077 | *descent = fe.descent; | |
2078 | if ( externalLeading ) | |
2079 | *externalLeading = wxMax(0, fe.height - (fe.ascent + fe.descent)); | |
2080 | } | |
2081 | } | |
2082 | ||
2083 | void wxCairoContext::GetPartialTextExtents(const wxString& text, wxArrayDouble& widths) const | |
2084 | { | |
2085 | widths.Empty(); | |
2086 | widths.Add(0, text.length()); | |
2087 | ||
2088 | wxCHECK_RET( !m_font.IsNull(), wxT("wxCairoContext::GetPartialTextExtents - no valid font set") ); | |
2089 | ||
2090 | if (text.empty()) | |
2091 | return; | |
2092 | ||
2093 | // TODO | |
2094 | } | |
2095 | ||
2096 | void * wxCairoContext::GetNativeContext() | |
2097 | { | |
2098 | return m_context; | |
2099 | } | |
2100 | ||
2101 | bool wxCairoContext::SetAntialiasMode(wxAntialiasMode antialias) | |
2102 | { | |
2103 | if (m_antialias == antialias) | |
2104 | return true; | |
2105 | ||
2106 | m_antialias = antialias; | |
2107 | ||
2108 | cairo_antialias_t antialiasMode; | |
2109 | switch (antialias) | |
2110 | { | |
2111 | case wxANTIALIAS_DEFAULT: | |
2112 | antialiasMode = CAIRO_ANTIALIAS_DEFAULT; | |
2113 | break; | |
2114 | case wxANTIALIAS_NONE: | |
2115 | antialiasMode = CAIRO_ANTIALIAS_NONE; | |
2116 | break; | |
2117 | default: | |
2118 | return false; | |
2119 | } | |
2120 | cairo_set_antialias(m_context, antialiasMode); | |
2121 | return true; | |
2122 | } | |
2123 | ||
2124 | bool wxCairoContext::SetInterpolationQuality(wxInterpolationQuality WXUNUSED(interpolation)) | |
2125 | { | |
2126 | // placeholder | |
2127 | return false; | |
2128 | } | |
2129 | ||
2130 | bool wxCairoContext::SetCompositionMode(wxCompositionMode op) | |
2131 | { | |
2132 | if ( m_composition == op ) | |
2133 | return true; | |
2134 | ||
2135 | m_composition = op; | |
2136 | cairo_operator_t cop; | |
2137 | switch (op) | |
2138 | { | |
2139 | case wxCOMPOSITION_CLEAR: | |
2140 | cop = CAIRO_OPERATOR_CLEAR; | |
2141 | break; | |
2142 | case wxCOMPOSITION_SOURCE: | |
2143 | cop = CAIRO_OPERATOR_SOURCE; | |
2144 | break; | |
2145 | case wxCOMPOSITION_OVER: | |
2146 | cop = CAIRO_OPERATOR_OVER; | |
2147 | break; | |
2148 | case wxCOMPOSITION_IN: | |
2149 | cop = CAIRO_OPERATOR_IN; | |
2150 | break; | |
2151 | case wxCOMPOSITION_OUT: | |
2152 | cop = CAIRO_OPERATOR_OUT; | |
2153 | break; | |
2154 | case wxCOMPOSITION_ATOP: | |
2155 | cop = CAIRO_OPERATOR_ATOP; | |
2156 | break; | |
2157 | case wxCOMPOSITION_DEST: | |
2158 | cop = CAIRO_OPERATOR_DEST; | |
2159 | break; | |
2160 | case wxCOMPOSITION_DEST_OVER: | |
2161 | cop = CAIRO_OPERATOR_DEST_OVER; | |
2162 | break; | |
2163 | case wxCOMPOSITION_DEST_IN: | |
2164 | cop = CAIRO_OPERATOR_DEST_IN; | |
2165 | break; | |
2166 | case wxCOMPOSITION_DEST_OUT: | |
2167 | cop = CAIRO_OPERATOR_DEST_OUT; | |
2168 | break; | |
2169 | case wxCOMPOSITION_DEST_ATOP: | |
2170 | cop = CAIRO_OPERATOR_DEST_ATOP; | |
2171 | break; | |
2172 | case wxCOMPOSITION_XOR: | |
2173 | cop = CAIRO_OPERATOR_XOR; | |
2174 | break; | |
2175 | case wxCOMPOSITION_ADD: | |
2176 | cop = CAIRO_OPERATOR_ADD; | |
2177 | break; | |
2178 | default: | |
2179 | return false; | |
2180 | } | |
2181 | cairo_set_operator(m_context, cop); | |
2182 | return true; | |
2183 | } | |
2184 | ||
2185 | void wxCairoContext::BeginLayer(wxDouble opacity) | |
2186 | { | |
2187 | m_layerOpacities.push_back(opacity); | |
2188 | cairo_push_group(m_context); | |
2189 | } | |
2190 | ||
2191 | void wxCairoContext::EndLayer() | |
2192 | { | |
2193 | float opacity = m_layerOpacities.back(); | |
2194 | m_layerOpacities.pop_back(); | |
2195 | cairo_pop_group_to_source(m_context); | |
2196 | cairo_paint_with_alpha(m_context,opacity); | |
2197 | } | |
2198 | ||
2199 | //----------------------------------------------------------------------------- | |
2200 | // wxCairoRenderer declaration | |
2201 | //----------------------------------------------------------------------------- | |
2202 | ||
2203 | class WXDLLIMPEXP_CORE wxCairoRenderer : public wxGraphicsRenderer | |
2204 | { | |
2205 | public : | |
2206 | wxCairoRenderer() {} | |
2207 | ||
2208 | virtual ~wxCairoRenderer() {} | |
2209 | ||
2210 | // Context | |
2211 | ||
2212 | virtual wxGraphicsContext * CreateContext( const wxWindowDC& dc); | |
2213 | virtual wxGraphicsContext * CreateContext( const wxMemoryDC& dc); | |
2214 | virtual wxGraphicsContext * CreateContext( const wxPrinterDC& dc); | |
2215 | ||
2216 | virtual wxGraphicsContext * CreateContextFromNativeContext( void * context ); | |
2217 | ||
2218 | virtual wxGraphicsContext * CreateContextFromNativeWindow( void * window ); | |
2219 | #if wxUSE_IMAGE | |
2220 | virtual wxGraphicsContext * CreateContextFromImage(wxImage& image); | |
2221 | #endif // wxUSE_IMAGE | |
2222 | ||
2223 | virtual wxGraphicsContext * CreateContext( wxWindow* window ); | |
2224 | ||
2225 | virtual wxGraphicsContext * CreateMeasuringContext(); | |
2226 | #ifdef __WXMSW__ | |
2227 | #if wxUSE_ENH_METAFILE | |
2228 | virtual wxGraphicsContext * CreateContext( const wxEnhMetaFileDC& dc); | |
2229 | #endif | |
2230 | #endif | |
2231 | // Path | |
2232 | ||
2233 | virtual wxGraphicsPath CreatePath(); | |
2234 | ||
2235 | // Matrix | |
2236 | ||
2237 | virtual wxGraphicsMatrix CreateMatrix( wxDouble a=1.0, wxDouble b=0.0, wxDouble c=0.0, wxDouble d=1.0, | |
2238 | wxDouble tx=0.0, wxDouble ty=0.0); | |
2239 | ||
2240 | ||
2241 | virtual wxGraphicsPen CreatePen(const wxPen& pen) ; | |
2242 | ||
2243 | virtual wxGraphicsBrush CreateBrush(const wxBrush& brush ) ; | |
2244 | ||
2245 | virtual wxGraphicsBrush | |
2246 | CreateLinearGradientBrush(wxDouble x1, wxDouble y1, | |
2247 | wxDouble x2, wxDouble y2, | |
2248 | const wxGraphicsGradientStops& stops); | |
2249 | ||
2250 | virtual wxGraphicsBrush | |
2251 | CreateRadialGradientBrush(wxDouble xo, wxDouble yo, | |
2252 | wxDouble xc, wxDouble yc, | |
2253 | wxDouble radius, | |
2254 | const wxGraphicsGradientStops& stops); | |
2255 | ||
2256 | // sets the font | |
2257 | virtual wxGraphicsFont CreateFont( const wxFont &font , const wxColour &col = *wxBLACK ) ; | |
2258 | virtual wxGraphicsFont CreateFont(double sizeInPixels, | |
2259 | const wxString& facename, | |
2260 | int flags = wxFONTFLAG_DEFAULT, | |
2261 | const wxColour& col = *wxBLACK); | |
2262 | ||
2263 | // create a native bitmap representation | |
2264 | virtual wxGraphicsBitmap CreateBitmap( const wxBitmap &bitmap ); | |
2265 | #if wxUSE_IMAGE | |
2266 | virtual wxGraphicsBitmap CreateBitmapFromImage(const wxImage& image); | |
2267 | virtual wxImage CreateImageFromBitmap(const wxGraphicsBitmap& bmp); | |
2268 | #endif // wxUSE_IMAGE | |
2269 | ||
2270 | // create a graphics bitmap from a native bitmap | |
2271 | virtual wxGraphicsBitmap CreateBitmapFromNativeBitmap( void* bitmap ); | |
2272 | ||
2273 | // create a subimage from a native image representation | |
2274 | virtual wxGraphicsBitmap CreateSubBitmap( const wxGraphicsBitmap &bitmap, wxDouble x, wxDouble y, wxDouble w, wxDouble h ); | |
2275 | ||
2276 | protected : | |
2277 | bool EnsureIsLoaded(); | |
2278 | void Load(); | |
2279 | void Unload(); | |
2280 | friend class wxCairoModule; | |
2281 | private : | |
2282 | int m_loaded; | |
2283 | DECLARE_DYNAMIC_CLASS_NO_COPY(wxCairoRenderer) | |
2284 | } ; | |
2285 | ||
2286 | //----------------------------------------------------------------------------- | |
2287 | // wxCairoRenderer implementation | |
2288 | //----------------------------------------------------------------------------- | |
2289 | ||
2290 | IMPLEMENT_DYNAMIC_CLASS(wxCairoRenderer,wxGraphicsRenderer) | |
2291 | ||
2292 | static wxCairoRenderer gs_cairoGraphicsRenderer; | |
2293 | // temporary hack to allow creating a cairo context on any platform | |
2294 | extern wxGraphicsRenderer* gCairoRenderer; | |
2295 | wxGraphicsRenderer* gCairoRenderer = &gs_cairoGraphicsRenderer; | |
2296 | ||
2297 | bool wxCairoRenderer::EnsureIsLoaded() | |
2298 | { | |
2299 | #ifndef __WXGTK__ | |
2300 | Load(); | |
2301 | return wxCairoInit(); | |
2302 | #else | |
2303 | return true; | |
2304 | #endif | |
2305 | } | |
2306 | ||
2307 | void wxCairoRenderer::Load() | |
2308 | { | |
2309 | wxCairoInit(); | |
2310 | } | |
2311 | ||
2312 | void wxCairoRenderer::Unload() | |
2313 | { | |
2314 | wxCairoCleanUp(); | |
2315 | } | |
2316 | ||
2317 | // call EnsureIsLoaded() and return returnOnFail value if it fails | |
2318 | #define ENSURE_LOADED_OR_RETURN(returnOnFail) \ | |
2319 | if ( !EnsureIsLoaded() ) \ | |
2320 | return (returnOnFail) | |
2321 | ||
2322 | wxGraphicsContext * wxCairoRenderer::CreateContext( const wxWindowDC& dc) | |
2323 | { | |
2324 | ENSURE_LOADED_OR_RETURN(NULL); | |
2325 | return new wxCairoContext(this,dc); | |
2326 | } | |
2327 | ||
2328 | wxGraphicsContext * wxCairoRenderer::CreateContext( const wxMemoryDC& dc) | |
2329 | { | |
2330 | ENSURE_LOADED_OR_RETURN(NULL); | |
2331 | return new wxCairoContext(this,dc); | |
2332 | } | |
2333 | ||
2334 | wxGraphicsContext * wxCairoRenderer::CreateContext( const wxPrinterDC& dc) | |
2335 | { | |
2336 | ENSURE_LOADED_OR_RETURN(NULL); | |
2337 | return new wxCairoContext(this, dc); | |
2338 | } | |
2339 | ||
2340 | #ifdef __WXMSW__ | |
2341 | #if wxUSE_ENH_METAFILE | |
2342 | wxGraphicsContext * wxCairoRenderer::CreateContext( const wxEnhMetaFileDC& WXUNUSED(dc) ) | |
2343 | { | |
2344 | ENSURE_LOADED_OR_RETURN(NULL); | |
2345 | return NULL; | |
2346 | } | |
2347 | #endif | |
2348 | #endif | |
2349 | ||
2350 | wxGraphicsContext * wxCairoRenderer::CreateContextFromNativeContext( void * context ) | |
2351 | { | |
2352 | ENSURE_LOADED_OR_RETURN(NULL); | |
2353 | #ifdef __WXMSW__ | |
2354 | return new wxCairoContext(this,(HDC)context); | |
2355 | #else | |
2356 | return new wxCairoContext(this,(cairo_t*)context); | |
2357 | #endif | |
2358 | } | |
2359 | ||
2360 | ||
2361 | wxGraphicsContext * wxCairoRenderer::CreateContextFromNativeWindow( void * window ) | |
2362 | { | |
2363 | ENSURE_LOADED_OR_RETURN(NULL); | |
2364 | #ifdef __WXGTK__ | |
2365 | return new wxCairoContext(this,(GdkDrawable*)window); | |
2366 | #else | |
2367 | wxUnusedVar(window); | |
2368 | return NULL; | |
2369 | #endif | |
2370 | } | |
2371 | ||
2372 | #if wxUSE_IMAGE | |
2373 | wxGraphicsContext * wxCairoRenderer::CreateContextFromImage(wxImage& image) | |
2374 | { | |
2375 | return new wxCairoImageContext(this, image); | |
2376 | } | |
2377 | #endif // wxUSE_IMAGE | |
2378 | ||
2379 | wxGraphicsContext * wxCairoRenderer::CreateMeasuringContext() | |
2380 | { | |
2381 | ENSURE_LOADED_OR_RETURN(NULL); | |
2382 | #ifdef __WXGTK__ | |
2383 | return CreateContextFromNativeWindow(gdk_get_default_root_window()); | |
2384 | #endif | |
2385 | return NULL; | |
2386 | // TODO | |
2387 | } | |
2388 | ||
2389 | wxGraphicsContext * wxCairoRenderer::CreateContext( wxWindow* window ) | |
2390 | { | |
2391 | ENSURE_LOADED_OR_RETURN(NULL); | |
2392 | return new wxCairoContext(this, window ); | |
2393 | } | |
2394 | ||
2395 | // Path | |
2396 | ||
2397 | wxGraphicsPath wxCairoRenderer::CreatePath() | |
2398 | { | |
2399 | ENSURE_LOADED_OR_RETURN(wxNullGraphicsPath); | |
2400 | wxGraphicsPath path; | |
2401 | path.SetRefData( new wxCairoPathData(this) ); | |
2402 | return path; | |
2403 | } | |
2404 | ||
2405 | ||
2406 | // Matrix | |
2407 | ||
2408 | wxGraphicsMatrix wxCairoRenderer::CreateMatrix( wxDouble a, wxDouble b, wxDouble c, wxDouble d, | |
2409 | wxDouble tx, wxDouble ty) | |
2410 | ||
2411 | { | |
2412 | ENSURE_LOADED_OR_RETURN(wxNullGraphicsMatrix); | |
2413 | wxGraphicsMatrix m; | |
2414 | wxCairoMatrixData* data = new wxCairoMatrixData( this ); | |
2415 | data->Set( a,b,c,d,tx,ty ) ; | |
2416 | m.SetRefData(data); | |
2417 | return m; | |
2418 | } | |
2419 | ||
2420 | wxGraphicsPen wxCairoRenderer::CreatePen(const wxPen& pen) | |
2421 | { | |
2422 | ENSURE_LOADED_OR_RETURN(wxNullGraphicsPen); | |
2423 | if ( !pen.IsOk() || pen.GetStyle() == wxPENSTYLE_TRANSPARENT ) | |
2424 | return wxNullGraphicsPen; | |
2425 | else | |
2426 | { | |
2427 | wxGraphicsPen p; | |
2428 | p.SetRefData(new wxCairoPenData( this, pen )); | |
2429 | return p; | |
2430 | } | |
2431 | } | |
2432 | ||
2433 | wxGraphicsBrush wxCairoRenderer::CreateBrush(const wxBrush& brush ) | |
2434 | { | |
2435 | ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush); | |
2436 | if ( !brush.IsOk() || brush.GetStyle() == wxBRUSHSTYLE_TRANSPARENT ) | |
2437 | return wxNullGraphicsBrush; | |
2438 | else | |
2439 | { | |
2440 | wxGraphicsBrush p; | |
2441 | p.SetRefData(new wxCairoBrushData( this, brush )); | |
2442 | return p; | |
2443 | } | |
2444 | } | |
2445 | ||
2446 | wxGraphicsBrush | |
2447 | wxCairoRenderer::CreateLinearGradientBrush(wxDouble x1, wxDouble y1, | |
2448 | wxDouble x2, wxDouble y2, | |
2449 | const wxGraphicsGradientStops& stops) | |
2450 | { | |
2451 | ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush); | |
2452 | wxGraphicsBrush p; | |
2453 | wxCairoBrushData* d = new wxCairoBrushData( this ); | |
2454 | d->CreateLinearGradientBrush(x1, y1, x2, y2, stops); | |
2455 | p.SetRefData(d); | |
2456 | return p; | |
2457 | } | |
2458 | ||
2459 | wxGraphicsBrush | |
2460 | wxCairoRenderer::CreateRadialGradientBrush(wxDouble xo, wxDouble yo, | |
2461 | wxDouble xc, wxDouble yc, wxDouble r, | |
2462 | const wxGraphicsGradientStops& stops) | |
2463 | { | |
2464 | ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush); | |
2465 | wxGraphicsBrush p; | |
2466 | wxCairoBrushData* d = new wxCairoBrushData( this ); | |
2467 | d->CreateRadialGradientBrush(xo, yo, xc, yc, r, stops); | |
2468 | p.SetRefData(d); | |
2469 | return p; | |
2470 | } | |
2471 | ||
2472 | wxGraphicsFont wxCairoRenderer::CreateFont( const wxFont &font , const wxColour &col ) | |
2473 | { | |
2474 | ENSURE_LOADED_OR_RETURN(wxNullGraphicsFont); | |
2475 | if ( font.IsOk() ) | |
2476 | { | |
2477 | wxGraphicsFont p; | |
2478 | p.SetRefData(new wxCairoFontData( this , font, col )); | |
2479 | return p; | |
2480 | } | |
2481 | else | |
2482 | return wxNullGraphicsFont; | |
2483 | } | |
2484 | ||
2485 | wxGraphicsFont | |
2486 | wxCairoRenderer::CreateFont(double sizeInPixels, | |
2487 | const wxString& facename, | |
2488 | int flags, | |
2489 | const wxColour& col) | |
2490 | { | |
2491 | ENSURE_LOADED_OR_RETURN(wxNullGraphicsFont); | |
2492 | ||
2493 | wxGraphicsFont font; | |
2494 | font.SetRefData(new wxCairoFontData(this, sizeInPixels, facename, flags, col)); | |
2495 | return font; | |
2496 | } | |
2497 | ||
2498 | wxGraphicsBitmap wxCairoRenderer::CreateBitmap( const wxBitmap& bmp ) | |
2499 | { | |
2500 | ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap); | |
2501 | if ( bmp.IsOk() ) | |
2502 | { | |
2503 | wxGraphicsBitmap p; | |
2504 | p.SetRefData(new wxCairoBitmapData( this , bmp )); | |
2505 | return p; | |
2506 | } | |
2507 | else | |
2508 | return wxNullGraphicsBitmap; | |
2509 | } | |
2510 | ||
2511 | #if wxUSE_IMAGE | |
2512 | ||
2513 | wxGraphicsBitmap wxCairoRenderer::CreateBitmapFromImage(const wxImage& image) | |
2514 | { | |
2515 | wxGraphicsBitmap bmp; | |
2516 | ||
2517 | ENSURE_LOADED_OR_RETURN(bmp); | |
2518 | ||
2519 | if ( image.IsOk() ) | |
2520 | { | |
2521 | bmp.SetRefData(new wxCairoBitmapData(this, image)); | |
2522 | } | |
2523 | ||
2524 | return bmp; | |
2525 | } | |
2526 | ||
2527 | wxImage wxCairoRenderer::CreateImageFromBitmap(const wxGraphicsBitmap& bmp) | |
2528 | { | |
2529 | ENSURE_LOADED_OR_RETURN(wxNullImage); | |
2530 | ||
2531 | const wxCairoBitmapData* const | |
2532 | data = static_cast<wxCairoBitmapData*>(bmp.GetGraphicsData()); | |
2533 | ||
2534 | return data ? data->ConvertToImage() : wxNullImage; | |
2535 | } | |
2536 | ||
2537 | #endif // wxUSE_IMAGE | |
2538 | ||
2539 | ||
2540 | wxGraphicsBitmap wxCairoRenderer::CreateBitmapFromNativeBitmap( void* bitmap ) | |
2541 | { | |
2542 | ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap); | |
2543 | if ( bitmap != NULL ) | |
2544 | { | |
2545 | wxGraphicsBitmap p; | |
2546 | p.SetRefData(new wxCairoBitmapData( this , (cairo_surface_t*) bitmap )); | |
2547 | return p; | |
2548 | } | |
2549 | else | |
2550 | return wxNullGraphicsBitmap; | |
2551 | } | |
2552 | ||
2553 | wxGraphicsBitmap | |
2554 | wxCairoRenderer::CreateSubBitmap(const wxGraphicsBitmap& WXUNUSED(bitmap), | |
2555 | wxDouble WXUNUSED(x), | |
2556 | wxDouble WXUNUSED(y), | |
2557 | wxDouble WXUNUSED(w), | |
2558 | wxDouble WXUNUSED(h)) | |
2559 | { | |
2560 | ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap); | |
2561 | wxFAIL_MSG("wxCairoRenderer::CreateSubBitmap is not implemented."); | |
2562 | return wxNullGraphicsBitmap; | |
2563 | } | |
2564 | ||
2565 | wxGraphicsRenderer* wxGraphicsRenderer::GetCairoRenderer() | |
2566 | { | |
2567 | return &gs_cairoGraphicsRenderer; | |
2568 | } | |
2569 | ||
2570 | #else // !wxUSE_CAIRO | |
2571 | ||
2572 | wxGraphicsRenderer* wxGraphicsRenderer::GetCairoRenderer() | |
2573 | { | |
2574 | return NULL; | |
2575 | } | |
2576 | ||
2577 | #endif // wxUSE_CAIRO/!wxUSE_CAIRO | |
2578 | ||
2579 | // MSW and OS X have their own native default renderers, but the other ports | |
2580 | // use Cairo by default | |
2581 | #if !(defined(__WXMSW__) || defined(__WXOSX__)) | |
2582 | wxGraphicsRenderer* wxGraphicsRenderer::GetDefaultRenderer() | |
2583 | { | |
2584 | return GetCairoRenderer(); | |
2585 | } | |
2586 | #endif // !(__WXMSW__ || __WXOSX__) | |
2587 | ||
2588 | #endif // wxUSE_GRAPHICS_CONTEXT |