X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/a1b806b98241ab649c169aaa1f134df85e80fb8b..a027a36f2c15b98dd3e2c42de406f4c4593cca76:/src/generic/graphicc.cpp diff --git a/src/generic/graphicc.cpp b/src/generic/graphicc.cpp index 280c56ad02..597fbe5a7e 100644 --- a/src/generic/graphicc.cpp +++ b/src/generic/graphicc.cpp @@ -21,6 +21,10 @@ #if wxUSE_CAIRO +// keep cairo.h from defining dllimport as we're defining the symbols inside +// the wx dll in order to load them dynamically. +#define cairo_public + #include "wx/cairo.h" #ifndef WX_PRECOMP @@ -301,9 +305,6 @@ private : cairo_font_slant_t m_slant; cairo_font_weight_t m_weight; #endif -#ifdef __WXMSW__ - wxCairoContext( wxGraphicsRenderer* renderer, HDC context ); -#endif }; class wxCairoBitmapData : public wxGraphicsObjectRefData @@ -332,6 +333,9 @@ public: wxCairoContext( wxGraphicsRenderer* renderer, const wxPrinterDC& dc ); #ifdef __WXGTK__ wxCairoContext( wxGraphicsRenderer* renderer, GdkDrawable *drawable ); +#endif +#ifdef __WXMSW__ + wxCairoContext( wxGraphicsRenderer* renderer, HDC context ); #endif wxCairoContext( wxGraphicsRenderer* renderer, cairo_t *context ); wxCairoContext( wxGraphicsRenderer* renderer, wxWindow *window); @@ -368,6 +372,8 @@ public: virtual bool SetAntialiasMode(wxAntialiasMode antialias); + virtual bool SetInterpolationQuality(wxInterpolationQuality interpolation); + virtual bool SetCompositionMode(wxCompositionMode op); virtual void BeginLayer(wxDouble opacity); @@ -1087,7 +1093,13 @@ wxCairoBitmapData::wxCairoBitmapData( wxGraphicsRenderer* renderer, const wxBitm // Create a surface object and copy the bitmap pixel data to it. if the // image has alpha (or a mask represented as alpha) then we'll use a // different format and iterator than if it doesn't... - if (bmpSource.HasAlpha() || bmpSource.GetMask()) + if (bmpSource.HasAlpha() || (bmpSource.GetMask() +#ifdef __WXMSW__ + // this check is needed under wxMSW, but adding this condition to wxGTK + // causes an assert when getting alpha pixel data, not sure about Mac. + && bmpSource.GetDepth() == 32 +#endif + )) { m_surface = cairo_image_surface_create_for_data( m_buffer, CAIRO_FORMAT_ARGB32, bw, bh, bw*4); @@ -1188,10 +1200,20 @@ public : wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, const wxPrinterDC& dc ) : wxGraphicsContext(renderer) { +#ifdef __WXMSW__ + // wxMSW contexts always use MM_ANISOTROPIC, which messes up + // text rendering when printing using Cairo. Switch it to MM_TEXT + // map mode to avoid this problem. + HDC hdc = (HDC)dc.GetHDC(); + ::SetMapMode(hdc, MM_TEXT); + m_mswSurface = cairo_win32_printing_surface_create(hdc); + Init( cairo_create(m_mswSurface) ); +#endif + #ifdef __WXGTK20__ const wxDCImpl *impl = dc.GetImpl(); Init( (cairo_t*) impl->GetCairoContext() ); - +#endif wxSize sz = dc.GetSize(); m_width = sz.x; m_height = sz.y; @@ -1201,11 +1223,21 @@ wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, const wxPrinterDC& double sx,sy; dc.GetUserScale( &sx, &sy ); + +// TODO: Determine if these fixes are needed on other platforms too. +// On MSW, without this the printer context will not respect wxDC SetMapMode calls. +// For example, using dc.SetMapMode(wxMM_POINTS) can let us share printer and screen +// drawing code +#ifdef __WXMSW__ + double lsx,lsy; + dc.GetLogicalScale( &lsx, &lsy ); + sx *= lsx; + sy *= lsy; +#endif cairo_scale( m_context, sx, sy ); org = dc.GetLogicalOrigin(); cairo_translate( m_context, -org.x, -org.y ); -#endif } wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, const wxWindowDC& dc ) @@ -1218,6 +1250,11 @@ wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, const wxWindowDC& m_enableOffset = true; +#ifdef __WXMSW__ + m_mswSurface = cairo_win32_surface_create((HDC)dc.GetHDC()); + Init( cairo_create(m_mswSurface) ); +#endif + #ifdef __WXGTK20__ wxGTKDCImpl *impldc = (wxGTKDCImpl*) dc.GetImpl(); Init( gdk_cairo_create( impldc->GetGDKWindow() ) ); @@ -1256,6 +1293,11 @@ wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, const wxMemoryDC& m_height = height; m_enableOffset = true; + +#ifdef __WXMSW__ + m_mswSurface = cairo_win32_surface_create((HDC)dc.GetHDC()); + Init( cairo_create(m_mswSurface) ); +#endif #ifdef __WXGTK20__ wxGTKDCImpl *impldc = (wxGTKDCImpl*) dc.GetImpl(); @@ -1342,6 +1384,12 @@ wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, wxWindow *window) m_width = sz.x; m_height = sz.y; #endif + +#ifdef __WXMSW__ + m_mswSurface = cairo_win32_surface_create((HDC)window->GetHandle()); + Init(cairo_create(m_mswSurface)); +#endif + } wxCairoContext::~wxCairoContext() @@ -1349,10 +1397,6 @@ wxCairoContext::~wxCairoContext() if ( m_context ) { PopState(); -#ifdef __WXMSW__ - m_mswSurface = cairo_win32_surface_create((HDC)window->GetHandle()); - m_context = cairo_create(m_mswSurface); -#endif PopState(); cairo_destroy(m_context); } @@ -1692,6 +1736,12 @@ bool wxCairoContext::SetAntialiasMode(wxAntialiasMode antialias) return true; } +bool wxCairoContext::SetInterpolationQuality(wxInterpolationQuality WXUNUSED(interpolation)) +{ + // placeholder + return false; +} + bool wxCairoContext::SetCompositionMode(wxCompositionMode op) { if ( m_composition == op ) @@ -1785,7 +1835,11 @@ public : virtual wxGraphicsContext * CreateContext( wxWindow* window ); virtual wxGraphicsContext * CreateMeasuringContext(); - +#ifdef __WXMSW__ +#if wxUSE_ENH_METAFILE + virtual wxGraphicsContext * CreateContext( const wxEnhMetaFileDC& dc); +#endif +#endif // Path virtual wxGraphicsPath CreatePath(); @@ -1823,7 +1877,13 @@ public : // create a subimage from a native image representation virtual wxGraphicsBitmap CreateSubBitmap( const wxGraphicsBitmap &bitmap, wxDouble x, wxDouble y, wxDouble w, wxDouble h ); +protected : + bool EnsureIsLoaded(); + void Load(); + void Unload(); + friend class wxCairoModule; private : + int m_loaded; DECLARE_DYNAMIC_CLASS_NO_COPY(wxCairoRenderer) } ; @@ -1838,18 +1898,46 @@ static wxCairoRenderer gs_cairoGraphicsRenderer; extern wxGraphicsRenderer* gCairoRenderer; wxGraphicsRenderer* gCairoRenderer = &gs_cairoGraphicsRenderer; +bool wxCairoRenderer::EnsureIsLoaded() +{ +#ifndef __WXGTK__ + Load(); + return wxCairoInit(); +#else + return true; +#endif +} + +void wxCairoRenderer::Load() +{ + wxCairoInit(); +} + +void wxCairoRenderer::Unload() +{ + wxCairoCleanUp(); +} + +// call EnsureIsLoaded() and return returnOnFail value if it fails +#define ENSURE_LOADED_OR_RETURN(returnOnFail) \ + if ( !EnsureIsLoaded() ) \ + return (returnOnFail) + wxGraphicsContext * wxCairoRenderer::CreateContext( const wxWindowDC& dc) { + ENSURE_LOADED_OR_RETURN(NULL); return new wxCairoContext(this,dc); } wxGraphicsContext * wxCairoRenderer::CreateContext( const wxMemoryDC& dc) { + ENSURE_LOADED_OR_RETURN(NULL); return new wxCairoContext(this,dc); } wxGraphicsContext * wxCairoRenderer::CreateContext( const wxPrinterDC& dc) { + ENSURE_LOADED_OR_RETURN(NULL); #ifdef __WXGTK20__ const wxDCImpl *impl = dc.GetImpl(); cairo_t* context = (cairo_t*) impl->GetCairoContext(); @@ -1857,11 +1945,22 @@ wxGraphicsContext * wxCairoRenderer::CreateContext( const wxPrinterDC& dc) return new wxCairoContext(this,dc); else #endif - return NULL; + return new wxCairoContext(this,dc); +} + +#ifdef __WXMSW__ +#if wxUSE_ENH_METAFILE +wxGraphicsContext * wxCairoRenderer::CreateContext( const wxEnhMetaFileDC& WXUNUSED(dc) ) +{ + ENSURE_LOADED_OR_RETURN(NULL); + return NULL; } +#endif +#endif wxGraphicsContext * wxCairoRenderer::CreateContextFromNativeContext( void * context ) { + ENSURE_LOADED_OR_RETURN(NULL); #ifdef __WXMSW__ return new wxCairoContext(this,(HDC)context); #else @@ -1872,15 +1971,18 @@ wxGraphicsContext * wxCairoRenderer::CreateContextFromNativeContext( void * cont wxGraphicsContext * wxCairoRenderer::CreateContextFromNativeWindow( void * window ) { + ENSURE_LOADED_OR_RETURN(NULL); #ifdef __WXGTK__ return new wxCairoContext(this,(GdkDrawable*)window); #else + wxUnusedVar(window); return NULL; #endif } wxGraphicsContext * wxCairoRenderer::CreateMeasuringContext() { + ENSURE_LOADED_OR_RETURN(NULL); #ifdef __WXGTK__ return CreateContextFromNativeWindow(gdk_get_default_root_window()); #endif @@ -1890,6 +1992,7 @@ wxGraphicsContext * wxCairoRenderer::CreateMeasuringContext() wxGraphicsContext * wxCairoRenderer::CreateContext( wxWindow* window ) { + ENSURE_LOADED_OR_RETURN(NULL); return new wxCairoContext(this, window ); } @@ -1897,6 +2000,7 @@ wxGraphicsContext * wxCairoRenderer::CreateContext( wxWindow* window ) wxGraphicsPath wxCairoRenderer::CreatePath() { + ENSURE_LOADED_OR_RETURN(wxNullGraphicsPath); wxGraphicsPath path; path.SetRefData( new wxCairoPathData(this) ); return path; @@ -1909,6 +2013,7 @@ wxGraphicsMatrix wxCairoRenderer::CreateMatrix( wxDouble a, wxDouble b, wxDouble wxDouble tx, wxDouble ty) { + ENSURE_LOADED_OR_RETURN(wxNullGraphicsMatrix); wxGraphicsMatrix m; wxCairoMatrixData* data = new wxCairoMatrixData( this ); data->Set( a,b,c,d,tx,ty ) ; @@ -1918,6 +2023,7 @@ wxGraphicsMatrix wxCairoRenderer::CreateMatrix( wxDouble a, wxDouble b, wxDouble wxGraphicsPen wxCairoRenderer::CreatePen(const wxPen& pen) { + ENSURE_LOADED_OR_RETURN(wxNullGraphicsPen); if ( !pen.IsOk() || pen.GetStyle() == wxPENSTYLE_TRANSPARENT ) return wxNullGraphicsPen; else @@ -1930,6 +2036,7 @@ wxGraphicsPen wxCairoRenderer::CreatePen(const wxPen& pen) wxGraphicsBrush wxCairoRenderer::CreateBrush(const wxBrush& brush ) { + ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush); if ( !brush.IsOk() || brush.GetStyle() == wxBRUSHSTYLE_TRANSPARENT ) return wxNullGraphicsBrush; else @@ -1945,6 +2052,7 @@ wxCairoRenderer::CreateLinearGradientBrush(wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2, const wxGraphicsGradientStops& stops) { + ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush); wxGraphicsBrush p; wxCairoBrushData* d = new wxCairoBrushData( this ); d->CreateLinearGradientBrush(x1, y1, x2, y2, stops); @@ -1957,6 +2065,7 @@ wxCairoRenderer::CreateRadialGradientBrush(wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble r, const wxGraphicsGradientStops& stops) { + ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush); wxGraphicsBrush p; wxCairoBrushData* d = new wxCairoBrushData( this ); d->CreateRadialGradientBrush(xo, yo, xc, yc, r, stops); @@ -1967,6 +2076,7 @@ wxCairoRenderer::CreateRadialGradientBrush(wxDouble xo, wxDouble yo, // sets the font wxGraphicsFont wxCairoRenderer::CreateFont( const wxFont &font , const wxColour &col ) { + ENSURE_LOADED_OR_RETURN(wxNullGraphicsFont); if ( font.IsOk() ) { wxGraphicsFont p; @@ -1979,6 +2089,7 @@ wxGraphicsFont wxCairoRenderer::CreateFont( const wxFont &font , const wxColour wxGraphicsBitmap wxCairoRenderer::CreateBitmap( const wxBitmap& bmp ) { + ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap); if ( bmp.IsOk() ) { wxGraphicsBitmap p; @@ -1991,6 +2102,7 @@ wxGraphicsBitmap wxCairoRenderer::CreateBitmap( const wxBitmap& bmp ) wxGraphicsBitmap wxCairoRenderer::CreateBitmapFromNativeBitmap( void* bitmap ) { + ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap); if ( bitmap != NULL ) { wxGraphicsBitmap p; @@ -2008,6 +2120,7 @@ wxCairoRenderer::CreateSubBitmap(const wxGraphicsBitmap& WXUNUSED(bitmap), wxDouble WXUNUSED(w), wxDouble WXUNUSED(h)) { + ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap); wxFAIL_MSG("wxCairoRenderer::CreateSubBitmap is not implemented."); return wxNullGraphicsBitmap; }