]> git.saurik.com Git - wxWidgets.git/blobdiff - src/gtk/print.cpp
[ 1793012 ] wxSpinCtrl::SetValue() doesn't update internal counter
[wxWidgets.git] / src / gtk / print.cpp
index d3c20a3d0b5af47ab903b6f21f280ee9a081dc6f..3ee388df0e990e9e1863a28d4048296b698e4fd2 100644 (file)
@@ -48,6 +48,8 @@ wxFORCE_LINK_THIS_MODULE(gtk_print)
 static const double RAD2DEG  = 180.0 / M_PI;
 static const double DEG2RAD  = M_PI / 180.0;
 
+static wxCairoLibrary* gs_cairo = NULL;
+
 //----------------------------------------------------------------------------
 // wxGtkPrintModule
 // Initialized when starting the app : if it successfully load the gtk-print framework,
@@ -74,19 +76,19 @@ private:
 
 bool wxGtkPrintModule::OnInit()
 {
-    if (gtk_check_version(2,10,0) == NULL)
+    gs_cairo = wxCairoLibrary::Get();
+    if (gs_cairo && gtk_check_version(2,10,0) == NULL)
         wxPrintFactory::SetPrintFactory( new wxGtkPrintFactory );
-        
     return true;
 }
 
 void wxGtkPrintModule::OnExit()
 {
+    gs_cairo = NULL;
 }
 
 IMPLEMENT_DYNAMIC_CLASS(wxGtkPrintModule, wxModule)
 
-
 //----------------------------------------------------------------------------
 // wxGtkPrintFactory
 //----------------------------------------------------------------------------
@@ -667,21 +669,29 @@ int wxGtkPrintDialog::ShowModal()
         case GTK_PRINT_PAGES_CURRENT:
             m_printDialogData.SetSelection( true );
             break;
-        case GTK_PRINT_PAGES_ALL:
-            m_printDialogData.SetAllPages( true );
-            m_printDialogData.SetFromPage( 0 );
-            m_printDialogData.SetToPage( 9999 );
-            break;
         case GTK_PRINT_PAGES_RANGES:
-        default:
-            // wxWidgets doesn't support multiple ranges, so we can only save the first one even if the user wants to print others.
+            {// wxWidgets doesn't support multiple ranges, so we can only save the first one even if the user wants to print others.
             // For example, the user enters "1-3;5-7" in the dialog: pages 1-3 and 5-7 will be correctly printed when the user
             // will hit "OK" button. However we can only save 1-3 in the print data.
             gint num_ranges = 0;
             GtkPageRange* range;
             range = gtk_print_settings_get_page_ranges (newSettings, &num_ranges);
+            if (num_ranges >= 1)
+            {
             m_printDialogData.SetFromPage( range[0].start );
             m_printDialogData.SetToPage( range[0].end );
+            }
+            else {
+                m_printDialogData.SetAllPages( true );
+                m_printDialogData.SetFromPage( 0 );
+                m_printDialogData.SetToPage( 9999 );
+            }
+            break;}
+        case GTK_PRINT_PAGES_ALL:
+        default:
+            m_printDialogData.SetAllPages( true );
+            m_printDialogData.SetFromPage( 0 );
+            m_printDialogData.SetToPage( 9999 );
             break;
     }
 
@@ -864,7 +874,8 @@ void wxGtkPrinter::BeginPrint(wxPrintout *printout, GtkPrintOperation *operation
     SetPrintContext(context);
     native->SetPrintContext( context );
 
-    m_dc = new wxGtkPrintDC( printdata );
+    wxGtkPrintDC *printDC = new wxGtkPrintDC( printdata );
+    m_dc = printDC;
 
     if (!m_dc->IsOk())
     {
@@ -880,8 +891,8 @@ void wxGtkPrinter::BeginPrint(wxPrintout *printout, GtkPrintOperation *operation
 
     printout->SetPPIScreen( (int) ((ScreenPixels.GetWidth() * 25.4) / ScreenMM.GetWidth()),
                             (int) ((ScreenPixels.GetHeight() * 25.4) / ScreenMM.GetHeight()) );
-    printout->SetPPIPrinter( wxGtkPrintDC::GetResolution(),
-                             wxGtkPrintDC::GetResolution() );
+    printout->SetPPIPrinter( printDC->GetResolution(),
+                             printDC->GetResolution() );
 
     printout->SetDC(m_dc);
 
@@ -968,8 +979,15 @@ void wxGtkPrinter::DrawPage(wxPrintout *printout, GtkPrintOperation *operation,
             GtkPageRange* range;
             range = gtk_print_settings_get_page_ranges (settings, &num_ranges);
             // We don't need to verify these values as it has already been done in wxGtkPrinter::BeginPrint.
+            if (num_ranges >= 1)
+            {
             startPage = range[0].start + 1;
             endPage = range[0].end + 1;
+            }
+            else {
+                startPage = minPage;
+                endPage = maxPage;
+            }
             break;}
         case GTK_PRINT_PAGES_ALL:
         default:
@@ -1039,10 +1057,12 @@ bool wxGtkPrinter::Setup( wxWindow *parent )
 // wxGtkPrintDC
 //-----------------------------------------------------------------------------
 
-IMPLEMENT_CLASS(wxGtkPrintDC, wxDC)
+#define XLOG2DEV(x)     ((double)(LogicalToDeviceX(x)) * m_DEV2PS)
+#define XLOG2DEVREL(x)  ((double)(LogicalToDeviceXRel(x)) * m_DEV2PS)
+#define YLOG2DEV(x)     ((double)(LogicalToDeviceY(x)) * m_DEV2PS)
+#define YLOG2DEVREL(x)  ((double)(LogicalToDeviceYRel(x)) * m_DEV2PS)
 
-// Define the default resolution for this DC. This resolution is just used for positioning as the cairo context is scalable.
-int wxGtkPrintDC::ms_resolution = 72;
+IMPLEMENT_CLASS(wxGtkPrintDC, wxDC)
 
 wxGtkPrintDC::wxGtkPrintDC( const wxPrintData& data )
 {
@@ -1053,7 +1073,16 @@ wxGtkPrintDC::wxGtkPrintDC( const wxPrintData& data )
 
     m_gpc = native->GetPrintContext();
 
-    ms_resolution = (int) gtk_print_context_get_dpi_x(m_gpc);
+    // RR: what does this do?
+    m_resolution = m_printData.GetQuality(); // (int) gtk_print_context_get_dpi_x( m_gpc );
+    if (m_resolution < 0) 
+        m_resolution = (1 << (m_resolution+4)) *150;
+
+    wxPrintf( "resolution %d\n", m_resolution );
+    
+    m_PS2DEV = (double)m_resolution / 72.0;
+    m_DEV2PS = 72.0 / (double)m_resolution;
+    
     m_context = gtk_print_context_create_pango_context( m_gpc );
     m_layout = gtk_print_context_create_pango_layout ( m_gpc );
     m_fontdesc = pango_font_description_from_string( "Sans 12" );
@@ -1067,7 +1096,13 @@ wxGtkPrintDC::wxGtkPrintDC( const wxPrintData& data )
     m_signX =  1;  // default x-axis left to right.
     m_signY = 1;  // default y-axis bottom up -> top down.
 
-    GetSize( &m_deviceOffsetX, &m_deviceOffsetY );
+    // By default the origine of cairo contexte is in the upper left corner of the printable area.
+    // We need to translate it so that it is in the upper left corner of the paper (i.e. doesn't care about margins)
+    GtkPageSetup *setup = gtk_print_context_get_page_setup( m_gpc );
+    gdouble ml, mt;
+    ml = gtk_page_setup_get_left_margin (setup, GTK_UNIT_POINTS);
+    mt = gtk_page_setup_get_top_margin (setup, GTK_UNIT_POINTS);
+    gs_cairo->cairo_translate(m_cairo, -ml, -mt);
 }
 
 wxGtkPrintDC::~wxGtkPrintDC()
@@ -1078,17 +1113,7 @@ wxGtkPrintDC::~wxGtkPrintDC()
 
 bool wxGtkPrintDC::IsOk() const
 {
-    return true;
-}
-
-void wxGtkPrintDC::ComputeScaleAndOrigin()
-{
-    // Called when the scale and/or origin of the context has to be changed.
-    m_scaleX = m_logicalScaleX * m_userScaleX;
-    m_scaleY = m_logicalScaleY * m_userScaleY;
-
-    cairo_translate(m_cairo, m_deviceOriginX, m_deviceOriginY);
-    cairo_scale(m_cairo, m_scaleX, m_scaleY );
+    return (m_gpc != NULL);
 }
 
 bool wxGtkPrintDC::DoFloodFill(wxCoord x1, wxCoord y1, const wxColour &col, int style )
@@ -1129,16 +1154,16 @@ void wxGtkPrintDC::DoGradientFillConcentric(const wxRect& rect, const wxColour&
 
     // Create a pattern with the gradient.
     cairo_pattern_t* gradient;
-    gradient = cairo_pattern_create_radial (LogicalToDeviceX(xC+xR), LogicalToDeviceY(yC+yR), 0, LogicalToDeviceX(xC+xR), LogicalToDeviceY(yC+yR), radius);
-    cairo_pattern_add_color_stop_rgba (gradient, 0.0, redIPS, greenIPS, blueIPS, alphaIPS);
-    cairo_pattern_add_color_stop_rgba (gradient, 1.0, redDPS, greenDPS, blueDPS, alphaDPS);
+    gradient = gs_cairo->cairo_pattern_create_radial (XLOG2DEV(xC+xR), YLOG2DEV(yC+yR), 0, XLOG2DEV(xC+xR), YLOG2DEV(yC+yR), radius * m_DEV2PS );
+    gs_cairo->cairo_pattern_add_color_stop_rgba (gradient, 0.0, redIPS, greenIPS, blueIPS, alphaIPS);
+    gs_cairo->cairo_pattern_add_color_stop_rgba (gradient, 1.0, redDPS, greenDPS, blueDPS, alphaDPS);
 
     // Fill the rectangle with this pattern.
-    cairo_set_source(m_cairo, gradient);
-    cairo_rectangle (m_cairo, LogicalToDeviceX(xR), LogicalToDeviceY(yR), LogicalToDeviceXRel(w), LogicalToDeviceYRel(h) );
-    cairo_fill(m_cairo);
+    gs_cairo->cairo_set_source(m_cairo, gradient);
+    gs_cairo->cairo_rectangle (m_cairo, XLOG2DEV(xR), YLOG2DEV(yR), XLOG2DEVREL(w), YLOG2DEVREL(h) );
+    gs_cairo->cairo_fill(m_cairo);
 
-    cairo_pattern_destroy(gradient);
+    gs_cairo->cairo_pattern_destroy(gradient);
 
     CalcBoundingBox(xR, yR);
     CalcBoundingBox(xR+w, yR+h);
@@ -1171,24 +1196,24 @@ void wxGtkPrintDC::DoGradientFillLinear(const wxRect& rect, const wxColour& init
 
     // Create a pattern with the gradient.
     cairo_pattern_t* gradient;
-    gradient = cairo_pattern_create_linear (LogicalToDeviceX(x), LogicalToDeviceY(y), LogicalToDeviceX(x+w), LogicalToDeviceY(y));
+    gradient = gs_cairo->cairo_pattern_create_linear (XLOG2DEV(x), YLOG2DEV(y), XLOG2DEV(x+w), YLOG2DEV(y));
 
     if (nDirection == wxWEST)
     {
-        cairo_pattern_add_color_stop_rgba (gradient, 0.0, redDPS, greenDPS, blueDPS, alphaDPS);
-        cairo_pattern_add_color_stop_rgba (gradient, 1.0, redIPS, greenIPS, blueIPS, alphaIPS);
+        gs_cairo->cairo_pattern_add_color_stop_rgba (gradient, 0.0, redDPS, greenDPS, blueDPS, alphaDPS);
+        gs_cairo->cairo_pattern_add_color_stop_rgba (gradient, 1.0, redIPS, greenIPS, blueIPS, alphaIPS);
     }
     else {
-        cairo_pattern_add_color_stop_rgba (gradient, 0.0, redIPS, greenIPS, blueIPS, alphaIPS);
-        cairo_pattern_add_color_stop_rgba (gradient, 1.0, redDPS, greenDPS, blueDPS, alphaDPS);
+        gs_cairo->cairo_pattern_add_color_stop_rgba (gradient, 0.0, redIPS, greenIPS, blueIPS, alphaIPS);
+        gs_cairo->cairo_pattern_add_color_stop_rgba (gradient, 1.0, redDPS, greenDPS, blueDPS, alphaDPS);
     }
 
     // Fill the rectangle with this pattern.
-    cairo_set_source(m_cairo, gradient);
-    cairo_rectangle (m_cairo, LogicalToDeviceX(x), LogicalToDeviceY(y), LogicalToDeviceXRel(w), LogicalToDeviceYRel(h) );
-    cairo_fill(m_cairo);
+    gs_cairo->cairo_set_source(m_cairo, gradient);
+    gs_cairo->cairo_rectangle (m_cairo, XLOG2DEV(x), YLOG2DEV(y), XLOG2DEVREL(w), YLOG2DEVREL(h) );
+    gs_cairo->cairo_fill(m_cairo);
 
-    cairo_pattern_destroy(gradient);
+    gs_cairo->cairo_pattern_destroy(gradient);
 
     CalcBoundingBox(x, y);
     CalcBoundingBox(x+w, y+h);
@@ -1206,9 +1231,9 @@ void wxGtkPrintDC::DoDrawLine(wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2)
     if  (m_pen.GetStyle() == wxTRANSPARENT) return;
 
     SetPen( m_pen );
-    cairo_move_to ( m_cairo, LogicalToDeviceX(x1), LogicalToDeviceY(y1) );
-    cairo_line_to ( m_cairo, LogicalToDeviceX(x2), LogicalToDeviceY(y2) );
-    cairo_stroke ( m_cairo );
+    gs_cairo->cairo_move_to ( m_cairo, XLOG2DEV(x1), YLOG2DEV(y1) );
+    gs_cairo->cairo_line_to ( m_cairo, XLOG2DEV(x2), YLOG2DEV(y2) );
+    gs_cairo->cairo_stroke ( m_cairo );
 
     CalcBoundingBox( x1, y1 );
     CalcBoundingBox( x2, y2 );
@@ -1216,24 +1241,19 @@ void wxGtkPrintDC::DoDrawLine(wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2)
 
 void wxGtkPrintDC::DoCrossHair(wxCoord x, wxCoord y)
 {
-    int *w, *h;
-    w = new int;
-    h = new int;
-    DoGetSize(w, h);
+    int w, h;
+    DoGetSize(&w, &h);
 
     SetPen(m_pen);
 
-    cairo_move_to (m_cairo, LogicalToDeviceX(x), 0);
-    cairo_line_to (m_cairo, LogicalToDeviceX(x), *h);
-    cairo_move_to (m_cairo, 0, LogicalToDeviceY(y));
-    cairo_line_to (m_cairo, *w, LogicalToDeviceY(y));
+    gs_cairo->cairo_move_to (m_cairo, XLOG2DEV(x), 0);
+    gs_cairo->cairo_line_to (m_cairo, XLOG2DEV(x), YLOG2DEVREL(h));
+    gs_cairo->cairo_move_to (m_cairo, 0, YLOG2DEV(y));
+    gs_cairo->cairo_line_to (m_cairo, XLOG2DEVREL(w), YLOG2DEV(y));
 
-    cairo_stroke (m_cairo);
+    gs_cairo->cairo_stroke (m_cairo);
     CalcBoundingBox( 0, 0 );
-    CalcBoundingBox( *w, *h );
-
-    delete w;
-    delete h;
+    CalcBoundingBox( w, h );
 }
 
 void wxGtkPrintDC::DoDrawArc(wxCoord x1,wxCoord y1,wxCoord x2,wxCoord y2,wxCoord xc,wxCoord yc)
@@ -1271,15 +1291,17 @@ void wxGtkPrintDC::DoDrawArc(wxCoord x1,wxCoord y1,wxCoord x2,wxCoord y2,wxCoord
     alpha1 *= DEG2RAD;
     alpha2 *= DEG2RAD;
 
-    cairo_arc_negative ( m_cairo, LogicalToDeviceX(xc), LogicalToDeviceY(yc), LogicalToDeviceXRel((int)radius), alpha1, alpha2);
-    cairo_line_to(m_cairo, LogicalToDeviceX(xc), LogicalToDeviceY(yc));
-    cairo_close_path (m_cairo);
+    gs_cairo->cairo_new_path(m_cairo);
+
+    gs_cairo->cairo_arc_negative ( m_cairo, XLOG2DEV(xc), YLOG2DEV(yc), XLOG2DEVREL((int)radius), alpha1, alpha2);
+    gs_cairo->cairo_line_to(m_cairo, XLOG2DEV(xc), YLOG2DEV(yc));
+    gs_cairo->cairo_close_path (m_cairo);
 
     SetBrush( m_brush );
-    cairo_fill_preserve( m_cairo );
+    gs_cairo->cairo_fill_preserve( m_cairo );
 
     SetPen (m_pen);
-    cairo_stroke( m_cairo );
+    gs_cairo->cairo_stroke( m_cairo );
 
     CalcBoundingBox (x1, y1);
     CalcBoundingBox (xc, yc);
@@ -1288,23 +1310,25 @@ void wxGtkPrintDC::DoDrawArc(wxCoord x1,wxCoord y1,wxCoord x2,wxCoord y2,wxCoord
 
 void wxGtkPrintDC::DoDrawEllipticArc(wxCoord x,wxCoord y,wxCoord w,wxCoord h,double sa,double ea)
 {
-    cairo_save( m_cairo );
+    gs_cairo->cairo_save( m_cairo );
 
-    cairo_translate( m_cairo, LogicalToDeviceX((wxCoord) (x + w / 2.)), LogicalToDeviceX((wxCoord) (y + h / 2.)) );
-    double scale = (double)LogicalToDeviceYRel(h) / (double) LogicalToDeviceXRel(w);
-    cairo_scale( m_cairo, 1.0, scale );
+    gs_cairo->cairo_new_path(m_cairo);
 
-    cairo_arc_negative ( m_cairo, 0, 0, LogicalToDeviceXRel(w/2), -sa*DEG2RAD, -ea*DEG2RAD);
+    gs_cairo->cairo_translate( m_cairo, XLOG2DEV((wxCoord) (x + w / 2.)), XLOG2DEV((wxCoord) (y + h / 2.)) );
+    double scale = (double)YLOG2DEVREL(h) / (double) XLOG2DEVREL(w);
+    gs_cairo->cairo_scale( m_cairo, 1.0, scale );
+
+    gs_cairo->cairo_arc_negative ( m_cairo, 0, 0, XLOG2DEVREL(w/2), -sa*DEG2RAD, -ea*DEG2RAD);
 
     SetPen (m_pen);
-    cairo_stroke_preserve( m_cairo );
+    gs_cairo->cairo_stroke_preserve( m_cairo );
 
-    cairo_line_to(m_cairo, 0,0);
+    gs_cairo->cairo_line_to(m_cairo, 0,0);
 
     SetBrush( m_brush );
-    cairo_fill( m_cairo );
+    gs_cairo->cairo_fill( m_cairo );
 
-    cairo_restore( m_cairo );
+    gs_cairo->cairo_restore( m_cairo );
 
     CalcBoundingBox( x, y);
     CalcBoundingBox( x+w, y+h );
@@ -1316,9 +1340,9 @@ void wxGtkPrintDC::DoDrawPoint(wxCoord x, wxCoord y)
 
     SetPen( m_pen );
 
-    cairo_move_to ( m_cairo, LogicalToDeviceX(x), LogicalToDeviceY(y) );
-    cairo_line_to ( m_cairo, LogicalToDeviceX(x), LogicalToDeviceY(y) );
-    cairo_stroke ( m_cairo );
+    gs_cairo->cairo_move_to ( m_cairo, XLOG2DEV(x), YLOG2DEV(y) );
+    gs_cairo->cairo_line_to ( m_cairo, XLOG2DEV(x), YLOG2DEV(y) );
+    gs_cairo->cairo_stroke ( m_cairo );
 
     CalcBoundingBox( x, y );
 }
@@ -1335,46 +1359,46 @@ void wxGtkPrintDC::DoDrawLines(int n, wxPoint points[], wxCoord xoffset, wxCoord
     for ( i =0; i<n ; i++ )
         CalcBoundingBox( points[i].x+xoffset, points[i].y+yoffset);
 
-    cairo_move_to ( m_cairo, LogicalToDeviceX(points[0].x+xoffset), LogicalToDeviceY(points[0].y+yoffset) );
+    gs_cairo->cairo_move_to ( m_cairo, XLOG2DEV(points[0].x+xoffset), YLOG2DEV(points[0].y+yoffset) );
 
     for (i = 1; i < n; i++)
-        cairo_line_to ( m_cairo, LogicalToDeviceX(points[i].x+xoffset), LogicalToDeviceY(points[i].y+yoffset) );
+        gs_cairo->cairo_line_to ( m_cairo, XLOG2DEV(points[i].x+xoffset), YLOG2DEV(points[i].y+yoffset) );
 
-    cairo_stroke ( m_cairo);
+    gs_cairo->cairo_stroke ( m_cairo);
 }
 
 void wxGtkPrintDC::DoDrawPolygon(int n, wxPoint points[], wxCoord xoffset, wxCoord yoffset, int fillStyle)
 {
     if (n==0) return;
 
-    cairo_save(m_cairo);
+    gs_cairo->cairo_save(m_cairo);
     if (fillStyle == wxWINDING_RULE)
-        cairo_set_fill_rule( m_cairo, CAIRO_FILL_RULE_WINDING);
+        gs_cairo->cairo_set_fill_rule( m_cairo, CAIRO_FILL_RULE_WINDING);
     else
-        cairo_set_fill_rule( m_cairo, CAIRO_FILL_RULE_EVEN_ODD);
+        gs_cairo->cairo_set_fill_rule( m_cairo, CAIRO_FILL_RULE_EVEN_ODD);
 
     int x = points[0].x + xoffset;
     int y = points[0].y + yoffset;
-    cairo_new_path(m_cairo);
-    cairo_move_to( m_cairo, LogicalToDeviceX(x), LogicalToDeviceY(y) );
+    gs_cairo->cairo_new_path(m_cairo);
+    gs_cairo->cairo_move_to( m_cairo, XLOG2DEV(x), YLOG2DEV(y) );
     int i;
     for (i = 1; i < n; i++)
     {
         int x = points[i].x + xoffset;
         int y = points[i].y + yoffset;
-        cairo_line_to( m_cairo, LogicalToDeviceX(x), LogicalToDeviceY(y) );
+        gs_cairo->cairo_line_to( m_cairo, XLOG2DEV(x), YLOG2DEV(y) );
     }
-    cairo_close_path(m_cairo);
+    gs_cairo->cairo_close_path(m_cairo);
 
     SetBrush( m_brush );
-    cairo_fill_preserve( m_cairo );
+    gs_cairo->cairo_fill_preserve( m_cairo );
 
     SetPen (m_pen);
-    cairo_stroke( m_cairo );
+    gs_cairo->cairo_stroke( m_cairo );
 
     CalcBoundingBox( x, y );
 
-    cairo_restore(m_cairo);
+    gs_cairo->cairo_restore(m_cairo);
 }
 
 void wxGtkPrintDC::DoDrawPolyPolygon(int n, int count[], wxPoint points[], wxCoord xoffset, wxCoord yoffset, int fillStyle)
@@ -1384,13 +1408,14 @@ void wxGtkPrintDC::DoDrawPolyPolygon(int n, int count[], wxPoint points[], wxCoo
 
 void wxGtkPrintDC::DoDrawRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
 {
-    cairo_rectangle ( m_cairo, LogicalToDeviceX(x), LogicalToDeviceY(y), LogicalToDeviceXRel(width), LogicalToDeviceYRel(height));
+    gs_cairo->cairo_new_path(m_cairo);
+    gs_cairo->cairo_rectangle ( m_cairo, XLOG2DEV(x), YLOG2DEV(y), XLOG2DEVREL(width), YLOG2DEVREL(height));
 
     SetBrush( m_brush );
-    cairo_fill_preserve( m_cairo );
+    gs_cairo->cairo_fill_preserve( m_cairo );
 
     SetPen (m_pen);
-    cairo_stroke( m_cairo );
+    gs_cairo->cairo_stroke( m_cairo );
 
     CalcBoundingBox( x, y );
     CalcBoundingBox( x + width, y + height );
@@ -1407,35 +1432,35 @@ void wxGtkPrintDC::DoDrawRoundedRectangle(wxCoord x, wxCoord y, wxCoord width, w
 
     wxCoord rad = (wxCoord) radius;
 
-    cairo_new_path(m_cairo);
-    cairo_move_to(m_cairo,LogicalToDeviceX(x + rad),LogicalToDeviceY(y));
-    cairo_curve_to(m_cairo,
-                                LogicalToDeviceX(x + rad),LogicalToDeviceY(y),
-                                LogicalToDeviceX(x),LogicalToDeviceY(y),
-                                LogicalToDeviceX(x),LogicalToDeviceY(y + rad));
-    cairo_line_to(m_cairo,LogicalToDeviceX(x),LogicalToDeviceY(y + height - rad));
-    cairo_curve_to(m_cairo,
-                                LogicalToDeviceX(x),LogicalToDeviceY(y + height - rad),
-                                LogicalToDeviceX(x),LogicalToDeviceY(y + height),
-                                LogicalToDeviceX(x + rad),LogicalToDeviceY(y + height));
-    cairo_line_to(m_cairo,LogicalToDeviceX(x + width - rad),LogicalToDeviceY(y + height));
-    cairo_curve_to(m_cairo,
-                                LogicalToDeviceX(x + width - rad),LogicalToDeviceY(y + height),
-                                LogicalToDeviceX(x + width),LogicalToDeviceY(y + height),
-                                LogicalToDeviceX(x + width),LogicalToDeviceY(y + height - rad));
-    cairo_line_to(m_cairo,LogicalToDeviceX(x + width),LogicalToDeviceY(y + rad));
-    cairo_curve_to(m_cairo,
-                                LogicalToDeviceX(x + width),LogicalToDeviceY(y + rad),
-                                LogicalToDeviceX(x + width),LogicalToDeviceY(y),
-                                LogicalToDeviceX(x + width - rad),LogicalToDeviceY(y));
-    cairo_line_to(m_cairo,LogicalToDeviceX(x + rad),LogicalToDeviceY(y));
-    cairo_close_path(m_cairo);
+    gs_cairo->cairo_new_path(m_cairo);
+    gs_cairo->cairo_move_to(m_cairo,XLOG2DEV(x + rad),YLOG2DEV(y));
+    gs_cairo->cairo_curve_to(m_cairo,
+                                XLOG2DEV(x + rad),YLOG2DEV(y),
+                                XLOG2DEV(x),YLOG2DEV(y),
+                                XLOG2DEV(x),YLOG2DEV(y + rad));
+    gs_cairo->cairo_line_to(m_cairo,XLOG2DEV(x),YLOG2DEV(y + height - rad));
+    gs_cairo->cairo_curve_to(m_cairo,
+                                XLOG2DEV(x),YLOG2DEV(y + height - rad),
+                                XLOG2DEV(x),YLOG2DEV(y + height),
+                                XLOG2DEV(x + rad),YLOG2DEV(y + height));
+    gs_cairo->cairo_line_to(m_cairo,XLOG2DEV(x + width - rad),YLOG2DEV(y + height));
+    gs_cairo->cairo_curve_to(m_cairo,
+                                XLOG2DEV(x + width - rad),YLOG2DEV(y + height),
+                                XLOG2DEV(x + width),YLOG2DEV(y + height),
+                                XLOG2DEV(x + width),YLOG2DEV(y + height - rad));
+    gs_cairo->cairo_line_to(m_cairo,XLOG2DEV(x + width),YLOG2DEV(y + rad));
+    gs_cairo->cairo_curve_to(m_cairo,
+                                XLOG2DEV(x + width),YLOG2DEV(y + rad),
+                                XLOG2DEV(x + width),YLOG2DEV(y),
+                                XLOG2DEV(x + width - rad),YLOG2DEV(y));
+    gs_cairo->cairo_line_to(m_cairo,XLOG2DEV(x + rad),YLOG2DEV(y));
+    gs_cairo->cairo_close_path(m_cairo);
 
     SetBrush(m_brush);
-    cairo_fill_preserve(m_cairo);
+    gs_cairo->cairo_fill_preserve(m_cairo);
 
     SetPen(m_pen);
-    cairo_stroke(m_cairo);
+    gs_cairo->cairo_stroke(m_cairo);
 
     CalcBoundingBox(x,y);
     CalcBoundingBox(x+width,y+height);
@@ -1443,22 +1468,24 @@ void wxGtkPrintDC::DoDrawRoundedRectangle(wxCoord x, wxCoord y, wxCoord width, w
 
 void wxGtkPrintDC::DoDrawEllipse(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
 {
-    cairo_save (m_cairo);
+    gs_cairo->cairo_save (m_cairo);
+
+    gs_cairo->cairo_new_path(m_cairo);
 
-    cairo_translate (m_cairo, LogicalToDeviceX((wxCoord) (x + width / 2.)), LogicalToDeviceY((wxCoord) (y + height / 2.)));
-    cairo_scale(m_cairo, 1, (double)LogicalToDeviceYRel(height)/(double)LogicalToDeviceXRel(width));
-    cairo_arc ( m_cairo, 0, 0, LogicalToDeviceXRel(width/2), 0, 2 * M_PI);
+    gs_cairo->cairo_translate (m_cairo, XLOG2DEV((wxCoord) (x + width / 2.)), YLOG2DEV((wxCoord) (y + height / 2.)));
+    gs_cairo->cairo_scale(m_cairo, 1, (double)YLOG2DEVREL(height)/(double)XLOG2DEVREL(width));
+    gs_cairo->cairo_arc ( m_cairo, 0, 0, XLOG2DEVREL(width/2), 0, 2 * M_PI);
 
     SetBrush( m_brush );
-    cairo_fill_preserve( m_cairo );
+    gs_cairo->cairo_fill_preserve( m_cairo );
 
     SetPen (m_pen);
-    cairo_stroke( m_cairo );
+    gs_cairo->cairo_stroke( m_cairo );
 
     CalcBoundingBox( x, y );
     CalcBoundingBox( x + width, y + height );
 
-    cairo_restore (m_cairo);
+    gs_cairo->cairo_restore (m_cairo);
 }
 
 #if wxUSE_SPLINES
@@ -1483,9 +1510,9 @@ void wxGtkPrintDC::DoDrawSpline(wxList *points)
     y3 =
          (double)(y1 + d) / 2;
 
-    cairo_new_path( m_cairo );
-    cairo_move_to( m_cairo, LogicalToDeviceX((wxCoord)x1), LogicalToDeviceY((wxCoord)y1) );
-    cairo_line_to( m_cairo, LogicalToDeviceX((wxCoord)x3), LogicalToDeviceY((wxCoord)y3) );
+    gs_cairo->cairo_new_path( m_cairo );
+    gs_cairo->cairo_move_to( m_cairo, XLOG2DEV((wxCoord)x1), YLOG2DEV((wxCoord)y1) );
+    gs_cairo->cairo_line_to( m_cairo, XLOG2DEV((wxCoord)x3), YLOG2DEV((wxCoord)y3) );
 
     CalcBoundingBox( (wxCoord)x1, (wxCoord)y1 );
     CalcBoundingBox( (wxCoord)x3, (wxCoord)y3 );
@@ -1504,10 +1531,10 @@ void wxGtkPrintDC::DoDrawSpline(wxList *points)
         x3 = (double)(x2 + c) / 2;
         y3 = (double)(y2 + d) / 2;
 
-        cairo_curve_to(m_cairo,
-            LogicalToDeviceX((wxCoord)x1), LogicalToDeviceY((wxCoord)y1),
-            LogicalToDeviceX((wxCoord)x2), LogicalToDeviceY((wxCoord)y2),
-            LogicalToDeviceX((wxCoord)x3), LogicalToDeviceY((wxCoord)y3) );
+        gs_cairo->cairo_curve_to(m_cairo,
+            XLOG2DEV((wxCoord)x1), YLOG2DEV((wxCoord)y1),
+            XLOG2DEV((wxCoord)x2), YLOG2DEV((wxCoord)y2),
+            XLOG2DEV((wxCoord)x3), YLOG2DEV((wxCoord)y3) );
 
         CalcBoundingBox( (wxCoord)x1, (wxCoord)y1 );
         CalcBoundingBox( (wxCoord)x3, (wxCoord)y3 );
@@ -1515,9 +1542,9 @@ void wxGtkPrintDC::DoDrawSpline(wxList *points)
         node = node->GetNext();
     }
 
-    cairo_line_to ( m_cairo, LogicalToDeviceX((wxCoord)c), LogicalToDeviceY((wxCoord)d) );
+    gs_cairo->cairo_line_to ( m_cairo, XLOG2DEV((wxCoord)c), YLOG2DEV((wxCoord)d) );
 
-    cairo_stroke( m_cairo );
+    gs_cairo->cairo_stroke( m_cairo );
 }
 #endif // wxUSE_SPLINES
 
@@ -1550,8 +1577,8 @@ void wxGtkPrintDC::DoDrawBitmap( const wxBitmap& bitmap, wxCoord x, wxCoord y, b
     wxCHECK_RET( bitmap.IsOk(), wxT("Invalid bitmap in wxGtkPrintDC::DoDrawBitmap"));
 
     cairo_surface_t* surface;
-    x = LogicalToDeviceX(x);
-    y = LogicalToDeviceY(y);
+    x = wxCoord(XLOG2DEV(x));
+    y = wxCoord(YLOG2DEV(y));
     int bw = bitmap.GetWidth();
     int bh = bitmap.GetHeight();
     wxBitmap bmpSource = bitmap;  // we need a non-const instance.
@@ -1565,7 +1592,7 @@ void wxGtkPrintDC::DoDrawBitmap( const wxBitmap& bitmap, wxCoord x, wxCoord y, b
     // then we'll use a different format and iterator than if it doesn't.
     if (bmpSource.HasAlpha() || mask)
     {
-        surface = cairo_image_surface_create_for_data(
+        surface = gs_cairo->cairo_image_surface_create_for_data(
             buffer, CAIRO_FORMAT_ARGB32, bw, bh, bw*4);
         wxAlphaPixelData pixData(bmpSource, wxPoint(0,0), wxSize(bw, bh));
         wxCHECK_RET( pixData, wxT("Failed to gain raw access to bitmap data."));
@@ -1582,111 +1609,10 @@ void wxGtkPrintDC::DoDrawBitmap( const wxBitmap& bitmap, wxCoord x, wxCoord y, b
                 // blue. The 32-bit quantities are stored native-endian.
                 // Pre-multiplied alpha is used.
                 unsigned char alpha = p.Alpha();
-                if (alpha == 0)
-                    *data = 0;
-                else
-                    *data = ( alpha/255                  << 24
-                              | (p.Red() * alpha/255)    << 16
-                              | (p.Green() * alpha/255)  <<  8
-                              | (p.Blue() * alpha/255) );
-                ++data;
-                ++p;
-            }
-            p = rowStart;
-            p.OffsetY(pixData, 1);
-        }
-    }
-    else  // no alpha
-    {
-        surface = cairo_image_surface_create_for_data(
-            buffer, CAIRO_FORMAT_RGB24, bw, bh, bw*4);
-        wxNativePixelData pixData(bmpSource, wxPoint(0,0), wxSize(bw, bh));
-        wxCHECK_RET( pixData, wxT("Failed to gain raw access to bitmap data."));
-
-        wxNativePixelData::Iterator p(pixData);
-        int y, x;
-        for (y=0; y<bh; y++)
-        {
-            wxNativePixelData::Iterator rowStart = p;
-            for (x=0; x<bw; x++)
-            {
-                // Each pixel in CAIRO_FORMAT_RGB24 is a 32-bit quantity, with
-                // the upper 8 bits unused. Red, Green, and Blue are stored in
-                // the remaining 24 bits in that order.  The 32-bit quantities
-                // are stored native-endian.
-                *data = ( p.Red() << 16 | p.Green() << 8 | p.Blue() );
-                ++data;
-                ++p;
-            }
-            p = rowStart;
-            p.OffsetY(pixData, 1);
-        }
-    }
-
-
-    cairo_save(m_cairo);
-    // In case we're scaling the image by using a width and height different
-    // than the bitmap's size create a pattern transformation on the surface and
-    // draw the transformed pattern.
-    cairo_pattern_t* pattern = cairo_pattern_create_for_surface(surface);
-
-    // Prepare to draw the image.
-    cairo_translate(m_cairo, x, y);
-    cairo_set_source(m_cairo, pattern);
-    // Use the original size here since the context is scaled already.
-    cairo_rectangle(m_cairo, 0, 0, bw, bh);
-    // Fill the rectangle using the pattern.
-    cairo_fill(m_cairo);
-
-    // Clean up.
-    cairo_pattern_destroy(pattern);
-    cairo_surface_destroy(surface);
-    delete [] buffer;
 
-    CalcBoundingBox(0,0);
-    CalcBoundingBox(bw,bh);
-
-    cairo_restore(m_cairo);
-}
-
-// wxGtkPrintDC has a constant resolution of 72dpi. If we want an higher resolution for printing
-// an image, the scaling has to be done by cairo.
-void wxGtkPrintDC::DoDrawScaledBitmap( const wxBitmap& bitmap, wxCoord x, wxCoord y, wxCoord w, wxCoord h, bool useMask, int quality )
-{
-    wxCHECK_RET( bitmap.IsOk(), wxT("Invalid bitmap in wxGtkPrintDC::DoDrawBitmap"));
+                if (!bmpSource.HasAlpha() && mask)
+                    alpha = 255;
 
-    cairo_surface_t* surface;
-    int bw = bitmap.GetWidth();
-    int bh = bitmap.GetHeight();
-    x = LogicalToDeviceX(x);
-    y = LogicalToDeviceY(y);
-    w = LogicalToDeviceXRel(w);
-    h = LogicalToDeviceYRel(h);
-    wxBitmap bmpSource = bitmap;  // we need a non-const instance.
-    unsigned char* buffer = new unsigned char[bw*bh*4];
-    wxUint32* data = (wxUint32*)buffer;
-
-    // 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())
-    {
-        surface = cairo_image_surface_create_for_data(
-            buffer, CAIRO_FORMAT_ARGB32, bw, bh, bw*4);
-        wxAlphaPixelData pixData(bmpSource, wxPoint(0,0), wxSize(bw, bh));
-        wxCHECK_RET( pixData, wxT("Failed to gain raw access to bitmap data."));
-
-        wxAlphaPixelData::Iterator p(pixData);
-        int y, x;
-        for (y=0; y<bh; y++)
-        {
-            wxAlphaPixelData::Iterator rowStart = p;
-            for (x=0; x<bw; x++)
-            {
-                // Each pixel in CAIRO_FORMAT_ARGB32 is a 32-bit quantity,
-                // with alpha in the upper 8 bits, then red, then green, then
-                // blue. The 32-bit quantities are stored native-endian.
-                // Pre-multiplied alpha is used.
-                unsigned char alpha = p.Alpha();
                 if (alpha == 0)
                     *data = 0;
                 else
@@ -1703,7 +1629,7 @@ void wxGtkPrintDC::DoDrawScaledBitmap( const wxBitmap& bitmap, wxCoord x, wxCoor
     }
     else  // no alpha
     {
-        surface = cairo_image_surface_create_for_data(
+        surface = gs_cairo->cairo_image_surface_create_for_data(
             buffer, CAIRO_FORMAT_RGB24, bw, bh, bw*4);
         wxNativePixelData pixData(bmpSource, wxPoint(0,0), wxSize(bw, bh));
         wxCHECK_RET( pixData, wxT("Failed to gain raw access to bitmap data."));
@@ -1729,38 +1655,34 @@ void wxGtkPrintDC::DoDrawScaledBitmap( const wxBitmap& bitmap, wxCoord x, wxCoor
     }
 
 
-    cairo_save(m_cairo);
+    gs_cairo->cairo_save(m_cairo);
 
     // Prepare to draw the image.
-    cairo_translate(m_cairo, x, y);
-
-    // In case we're scaling the image by using a width and height different
-    // than the bitmap's size create a pattern transformation on the surface and
-    // draw the transformed pattern.
-    cairo_filter_t filter;
-    if (quality == wxIMAGE_QUALITY_HIGH) filter = CAIRO_FILTER_BILINEAR;
-    else filter = CAIRO_FILTER_GOOD;
+    gs_cairo->cairo_translate(m_cairo, x, y);
+
+    // Scale the image
+    cairo_filter_t filter = CAIRO_FILTER_BILINEAR;
     cairo_pattern_t* pattern = cairo_pattern_create_for_surface(surface);
     cairo_pattern_set_filter(pattern,filter);
-    wxDouble scaleX = (wxDouble) w / (wxDouble) bw;
-    wxDouble scaleY = (wxDouble) h / (wxDouble) bh;
+    wxDouble scaleX = (wxDouble) XLOG2DEVREL(bw) / (wxDouble) bw;
+    wxDouble scaleY = (wxDouble) YLOG2DEVREL(bh) / (wxDouble) bh;
     cairo_scale(m_cairo, scaleX, scaleY);
 
-    cairo_set_source(m_cairo, pattern);
+    gs_cairo->cairo_set_source(m_cairo, pattern);
     // Use the original size here since the context is scaled already.
-    cairo_rectangle(m_cairo, 0, 0, bw, bh);
+    gs_cairo->cairo_rectangle(m_cairo, 0, 0, bw, bh);
     // Fill the rectangle using the pattern.
-    cairo_fill(m_cairo);
+    gs_cairo->cairo_fill(m_cairo);
 
     // Clean up.
-    cairo_pattern_destroy(pattern);
-    cairo_surface_destroy(surface);
+    gs_cairo->cairo_pattern_destroy(pattern);
+    gs_cairo->cairo_surface_destroy(surface);
     delete [] buffer;
 
     CalcBoundingBox(0,0);
     CalcBoundingBox(bw,bh);
 
-    cairo_restore(m_cairo);
+    gs_cairo->cairo_restore(m_cairo);
 }
 
 void wxGtkPrintDC::DoDrawText(const wxString& text, wxCoord x, wxCoord y )
@@ -1770,8 +1692,8 @@ void wxGtkPrintDC::DoDrawText(const wxString& text, wxCoord x, wxCoord y )
 
 void wxGtkPrintDC::DoDrawRotatedText(const wxString& text, wxCoord x, wxCoord y, double angle)
 {
-    x = LogicalToDeviceX(x);
-    y = LogicalToDeviceY(y);
+    double xx = XLOG2DEV(x);
+    double yy = YLOG2DEV(y);
 
     angle = -angle;
 
@@ -1812,7 +1734,7 @@ void wxGtkPrintDC::DoDrawRotatedText(const wxString& text, wxCoord x, wxCoord y,
             double greenPS = (double)(green) / 255.0;
             double alphaPS = (double)(alpha) / 255.0;
 
-            cairo_set_source_rgba( m_cairo, redPS, greenPS, bluePS, alphaPS );
+            gs_cairo->cairo_set_source_rgba( m_cairo, redPS, greenPS, bluePS, alphaPS );
 
             m_currentRed = red;
             m_currentBlue = blue;
@@ -1822,70 +1744,17 @@ void wxGtkPrintDC::DoDrawRotatedText(const wxString& text, wxCoord x, wxCoord y,
     }
 
     int w,h;
+    
+    // Scale font description.
+    gint oldSize = pango_font_description_get_size( m_fontdesc );
+    double size = oldSize;
+    size = size * m_scaleX;
+    pango_font_description_set_size( m_fontdesc, (gint)size );
 
-    if (fabs(m_scaleY - 1.0) > 0.00001)
-    {
-        // If there is a user or actually any scale applied to the device context, scale the font.
-
-        // Scale font description.
-        gint oldSize = pango_font_description_get_size( m_fontdesc );
-        double size = oldSize;
-        size = size * m_scaleY;
-        pango_font_description_set_size( m_fontdesc, (gint)size );
-
-        // Actually apply scaled font.
-        pango_layout_set_font_description( m_layout, m_fontdesc );
-
-        pango_layout_get_pixel_size( m_layout, &w, &h );
-        w = LogicalToDeviceXRel(w);
-        h = LogicalToDeviceYRel(h);
-
-        if ( m_backgroundMode == wxSOLID )
-        {
-            unsigned char red = m_textBackgroundColour.Red();
-            unsigned char blue = m_textBackgroundColour.Blue();
-            unsigned char green = m_textBackgroundColour.Green();
-            unsigned char alpha = m_textBackgroundColour.Alpha();
-
-            double redPS = (double)(red) / 255.0;
-            double bluePS = (double)(blue) / 255.0;
-            double greenPS = (double)(green) / 255.0;
-            double alphaPS = (double)(alpha) / 255.0;
-
-            cairo_save(m_cairo);
-            cairo_translate(m_cairo, x, y);
-            cairo_set_source_rgba( m_cairo, redPS, greenPS, bluePS, alphaPS );
-            cairo_rotate(m_cairo,angle*DEG2RAD);
-            cairo_rectangle(m_cairo, 0, 0, w, h);
-            cairo_fill(m_cairo);
-            cairo_restore(m_cairo);
-        }
-
-        // Draw layout.
-        cairo_move_to (m_cairo, x, y);
-        if (fabs(angle) > 0.00001)
-        {
-            cairo_save( m_cairo );
-            cairo_rotate( m_cairo, angle*DEG2RAD );
-            pango_cairo_update_layout (m_cairo, m_layout);
-            pango_cairo_show_layout (m_cairo, m_layout);
-            cairo_restore( m_cairo );
-        }
-        else
-        {
-            pango_cairo_update_layout (m_cairo, m_layout);
-            pango_cairo_show_layout (m_cairo, m_layout);
-        }
-
-        // Reset unscaled size.
-        pango_font_description_set_size( m_fontdesc, oldSize );
+    // Actually apply scaled font.
+    pango_layout_set_font_description( m_layout, m_fontdesc );
 
-        // Actually apply unscaled font.
-        pango_layout_set_font_description( m_layout, m_fontdesc );
-    }
-    else
-    {
-        pango_layout_get_pixel_size( m_layout, &w, &h );
+    pango_layout_get_pixel_size( m_layout, &w, &h );
 
         if ( m_backgroundMode == wxSOLID )
         {
@@ -1899,49 +1768,56 @@ void wxGtkPrintDC::DoDrawRotatedText(const wxString& text, wxCoord x, wxCoord y,
             double greenPS = (double)(green) / 255.0;
             double alphaPS = (double)(alpha) / 255.0;
 
-            cairo_save(m_cairo);
-            cairo_translate(m_cairo, x, y);
-            cairo_set_source_rgba( m_cairo, redPS, greenPS, bluePS, alphaPS );
-            cairo_rotate(m_cairo,angle*DEG2RAD);
-            cairo_rectangle(m_cairo, 0, 0, w, h);
-            cairo_fill(m_cairo);
-            cairo_restore(m_cairo);
+            gs_cairo->cairo_save(m_cairo);
+            gs_cairo->cairo_translate(m_cairo, xx, yy);
+            gs_cairo->cairo_set_source_rgba( m_cairo, redPS, greenPS, bluePS, alphaPS );
+            gs_cairo->cairo_rotate(m_cairo,angle*DEG2RAD);
+            gs_cairo->cairo_rectangle(m_cairo, 0, 0, w, h);   // still in cairo units
+            gs_cairo->cairo_fill(m_cairo);
+            gs_cairo->cairo_restore(m_cairo);
         }
 
-        // Draw layout.
-        cairo_move_to (m_cairo, x, y);
-        if (fabs(angle) > 0.00001)
-        {
-            cairo_save( m_cairo );
-            cairo_rotate( m_cairo, angle*DEG2RAD );
-            pango_cairo_update_layout (m_cairo, m_layout);
-            pango_cairo_show_layout (m_cairo, m_layout);
-            cairo_restore( m_cairo );
-        }
-        else
-        {
-            pango_cairo_update_layout (m_cairo, m_layout);
-            pango_cairo_show_layout (m_cairo, m_layout);
-        }
-    }
+    // Draw layout.
+    gs_cairo->cairo_move_to (m_cairo, xx, yy);
+    
+    gs_cairo->cairo_save( m_cairo );
+    
+    if (fabs(angle) > 0.00001)
+        gs_cairo->cairo_rotate( m_cairo, angle*DEG2RAD );
+        
+    gs_cairo->pango_cairo_update_layout (m_cairo, m_layout);
+    gs_cairo->pango_cairo_show_layout (m_cairo, m_layout);
+    
+    gs_cairo->cairo_restore( m_cairo );
 
     if (underlined)
     {
         // Undo underline attributes setting
         pango_layout_set_attributes(m_layout, NULL);
     }
+    
+    // Reset unscaled size.
+    pango_font_description_set_size( m_fontdesc, oldSize );
 
-    CalcBoundingBox (x,y);
+    // Actually apply unscaled font.
+    pango_layout_set_font_description( m_layout, m_fontdesc );
+
+    // Back to device units:
+    CalcBoundingBox (x, y);
     CalcBoundingBox (x + w, y + h);
 }
 
 void wxGtkPrintDC::Clear()
 {
-    cairo_save(m_cairo);
-    cairo_set_operator (m_cairo, CAIRO_OPERATOR_SOURCE);
+// Clear does nothing for printing, but keep the code
+// for later reuse 
+/*
+    gs_cairo->cairo_save(m_cairo);
+    gs_cairo->cairo_set_operator (m_cairo, CAIRO_OPERATOR_SOURCE);
     SetBrush(m_backgroundBrush);
-    cairo_paint(m_cairo);
-    cairo_restore(m_cairo);
+    gs_cairo->cairo_paint(m_cairo);
+    gs_cairo->cairo_restore(m_cairo);
+*/
 }
 
 void wxGtkPrintDC::SetFont( const wxFont& font )
@@ -1953,8 +1829,14 @@ void wxGtkPrintDC::SetFont( const wxFont& font )
         if (m_fontdesc)
             pango_font_description_free( m_fontdesc );
 
-        m_fontdesc = pango_font_description_copy( m_font.GetNativeFontInfo()->description );
+        m_fontdesc = pango_font_description_copy( m_font.GetNativeFontInfo()->description ); // m_fontdesc is now set to device units
+
+        // Scale font description from device units to pango units
+        gint oldSize = pango_font_description_get_size( m_fontdesc ); 
+        double size = oldSize *m_DEV2PS;                          // scale to cairo units
+        pango_font_description_set_size( m_fontdesc, (gint)size );    // apply to description
 
+        // Actually apply scaled font.
         pango_layout_set_font_description( m_layout, m_fontdesc );
     }
 }
@@ -1968,7 +1850,7 @@ void wxGtkPrintDC::SetPen( const wxPen& pen )
     double width = (double) m_pen.GetWidth();
     if (width == 0) width = 0.1;
 
-    cairo_set_line_width( m_cairo, LogicalToDeviceXRel( (wxCoord) (1000 * width )) / 1000.0f );
+    gs_cairo->cairo_set_line_width( m_cairo, (width * m_PS2DEV) / m_scaleX );
     static const double dotted[] = {2.0, 5.0};
     static const double short_dashed[] = {4.0, 4.0};
     static const double long_dashed[] = {4.0, 8.0};
@@ -1976,41 +1858,41 @@ void wxGtkPrintDC::SetPen( const wxPen& pen )
 
     switch (m_pen.GetStyle())
     {
-        case wxDOT:           cairo_set_dash( m_cairo, dotted, 1, 0 ); break;
-        case wxSHORT_DASH:    cairo_set_dash( m_cairo, short_dashed, 1, 0 ); break;
-        case wxLONG_DASH:     cairo_set_dash( m_cairo, long_dashed, 1, 0 ); break;
-        case wxDOT_DASH:      cairo_set_dash( m_cairo, dotted_dashed, 3, 0 );  break;
+        case wxDOT:           gs_cairo->cairo_set_dash( m_cairo, dotted, 2, 0 ); break;
+        case wxSHORT_DASH:    gs_cairo->cairo_set_dash( m_cairo, short_dashed, 2, 0 ); break;
+        case wxLONG_DASH:     gs_cairo->cairo_set_dash( m_cairo, long_dashed, 2, 0 ); break;
+        case wxDOT_DASH:      gs_cairo->cairo_set_dash( m_cairo, dotted_dashed, 4, 0 );  break;
         case wxUSER_DASH:
         {
             wxDash *wx_dashes;
-            int num = m_pen.GetDashes (&wx_dashes) - 1;
+            int num = m_pen.GetDashes (&wx_dashes);
             gdouble *g_dashes = g_new( gdouble, num );
             int i;
             for (i = 0; i < num; ++i)
                 g_dashes[i] = (gdouble) wx_dashes[i];
-            cairo_set_dash( m_cairo, g_dashes, num, 0);
+            gs_cairo->cairo_set_dash( m_cairo, g_dashes, num, 0);
             g_free( g_dashes );
         }
         break;
         case wxSOLID:
         case wxTRANSPARENT:
-        default:              cairo_set_dash( m_cairo, NULL, 0, 0 );   break;
+        default:              gs_cairo->cairo_set_dash( m_cairo, NULL, 0, 0 );   break;
     }
 
     switch (m_pen.GetCap())
     {
-        case wxCAP_PROJECTING:  cairo_set_line_cap (m_cairo, CAIRO_LINE_CAP_SQUARE); break;
-        case wxCAP_BUTT:        cairo_set_line_cap (m_cairo, CAIRO_LINE_CAP_BUTT); break;
+        case wxCAP_PROJECTING:  gs_cairo->cairo_set_line_cap (m_cairo, CAIRO_LINE_CAP_SQUARE); break;
+        case wxCAP_BUTT:        gs_cairo->cairo_set_line_cap (m_cairo, CAIRO_LINE_CAP_BUTT); break;
         case wxCAP_ROUND:
-        default:                cairo_set_line_cap (m_cairo, CAIRO_LINE_CAP_ROUND); break;
+        default:                gs_cairo->cairo_set_line_cap (m_cairo, CAIRO_LINE_CAP_ROUND); break;
     }
 
     switch (m_pen.GetJoin())
     {
-        case wxJOIN_BEVEL:  cairo_set_line_join (m_cairo, CAIRO_LINE_JOIN_BEVEL); break;
-        case wxJOIN_MITER:  cairo_set_line_join (m_cairo, CAIRO_LINE_JOIN_MITER); break;
+        case wxJOIN_BEVEL:  gs_cairo->cairo_set_line_join (m_cairo, CAIRO_LINE_JOIN_BEVEL); break;
+        case wxJOIN_MITER:  gs_cairo->cairo_set_line_join (m_cairo, CAIRO_LINE_JOIN_MITER); break;
         case wxJOIN_ROUND:
-        default:            cairo_set_line_join (m_cairo, CAIRO_LINE_JOIN_ROUND); break;
+        default:            gs_cairo->cairo_set_line_join (m_cairo, CAIRO_LINE_JOIN_ROUND); break;
     }
 
     unsigned char red = m_pen.GetColour().Red();
@@ -2025,7 +1907,7 @@ void wxGtkPrintDC::SetPen( const wxPen& pen )
         double greenPS = (double)(green) / 255.0;
         double alphaPS = (double)(alpha) / 255.0;
 
-        cairo_set_source_rgba( m_cairo, redPS, greenPS, bluePS, alphaPS );
+        gs_cairo->cairo_set_source_rgba( m_cairo, redPS, greenPS, bluePS, alphaPS );
 
         m_currentRed = red;
         m_currentBlue = blue;
@@ -2040,6 +1922,16 @@ void wxGtkPrintDC::SetBrush( const wxBrush& brush )
 
     m_brush = brush;
 
+    if (m_brush.GetStyle() == wxTRANSPARENT)
+    {
+        gs_cairo->cairo_set_source_rgba( m_cairo, 0, 0, 0, 0 );
+        m_currentRed = 0;
+        m_currentBlue = 0;
+        m_currentGreen = 0;
+        m_currentAlpha = 0;
+        return;
+    }
+
     // Brush colour.
     unsigned char red = m_brush.GetColour().Red();
     unsigned char blue = m_brush.GetColour().Blue();
@@ -2053,7 +1945,7 @@ void wxGtkPrintDC::SetBrush( const wxBrush& brush )
 
     if (!(red == m_currentRed && green == m_currentGreen && blue == m_currentBlue && alpha == m_currentAlpha))
     {
-        cairo_set_source_rgba( m_cairo, redPS, greenPS, bluePS, alphaPS );
+        gs_cairo->cairo_set_source_rgba( m_cairo, redPS, greenPS, bluePS, alphaPS );
 
         m_currentRed = red;
         m_currentBlue = blue;
@@ -2065,85 +1957,85 @@ void wxGtkPrintDC::SetBrush( const wxBrush& brush )
     {
         cairo_t * cr;
         cairo_surface_t *surface;
-        surface = cairo_surface_create_similar(cairo_get_target(m_cairo),CAIRO_CONTENT_COLOR_ALPHA,10,10);
-        cr = cairo_create(surface);
-        cairo_set_line_cap(cr, CAIRO_LINE_CAP_SQUARE);
-        cairo_set_line_width(cr, 1);
-        cairo_set_line_join(cr,CAIRO_LINE_JOIN_MITER);
+        surface = gs_cairo->cairo_surface_create_similar(gs_cairo->cairo_get_target(m_cairo),CAIRO_CONTENT_COLOR_ALPHA,10,10);
+        cr = gs_cairo->cairo_create(surface);
+        gs_cairo->cairo_set_line_cap(cr, CAIRO_LINE_CAP_SQUARE);
+        gs_cairo->cairo_set_line_width(cr, 1);
+        gs_cairo->cairo_set_line_join(cr,CAIRO_LINE_JOIN_MITER);
 
         switch (m_brush.GetStyle())
         {
             case wxCROSS_HATCH:
-                cairo_move_to(cr, 5, 0);
-                cairo_line_to(cr, 5, 10);
-                cairo_move_to(cr, 0, 5);
-                cairo_line_to(cr, 10, 5);
+                gs_cairo->cairo_move_to(cr, 5, 0);
+                gs_cairo->cairo_line_to(cr, 5, 10);
+                gs_cairo->cairo_move_to(cr, 0, 5);
+                gs_cairo->cairo_line_to(cr, 10, 5);
                 break;
             case wxBDIAGONAL_HATCH:
-                cairo_move_to(cr, 0, 10);
-                cairo_line_to(cr, 10, 0);
+                gs_cairo->cairo_move_to(cr, 0, 10);
+                gs_cairo->cairo_line_to(cr, 10, 0);
                 break;
             case wxFDIAGONAL_HATCH:
-                cairo_move_to(cr, 0, 0);
-                cairo_line_to(cr, 10, 10);
+                gs_cairo->cairo_move_to(cr, 0, 0);
+                gs_cairo->cairo_line_to(cr, 10, 10);
                 break;
             case wxCROSSDIAG_HATCH:
-                cairo_move_to(cr, 0, 0);
-                cairo_line_to(cr, 10, 10);
-                cairo_move_to(cr, 10, 0);
-                cairo_line_to(cr, 0, 10);
+                gs_cairo->cairo_move_to(cr, 0, 0);
+                gs_cairo->cairo_line_to(cr, 10, 10);
+                gs_cairo->cairo_move_to(cr, 10, 0);
+                gs_cairo->cairo_line_to(cr, 0, 10);
                 break;
             case wxHORIZONTAL_HATCH:
-                cairo_move_to(cr, 0, 5);
-                cairo_line_to(cr, 10, 5);
+                gs_cairo->cairo_move_to(cr, 0, 5);
+                gs_cairo->cairo_line_to(cr, 10, 5);
                 break;
             case wxVERTICAL_HATCH:
-                cairo_move_to(cr, 5, 0);
-                cairo_line_to(cr, 5, 10);
+                gs_cairo->cairo_move_to(cr, 5, 0);
+                gs_cairo->cairo_line_to(cr, 5, 10);
                 break;
             default:
                 wxFAIL_MSG(_("Couldn't get hatch style from wxBrush."));
         }
 
-        cairo_set_source_rgba(cr, redPS, greenPS, bluePS, alphaPS);
-        cairo_stroke (cr);
+        gs_cairo->cairo_set_source_rgba(cr, redPS, greenPS, bluePS, alphaPS);
+        gs_cairo->cairo_stroke (cr);
 
-        cairo_destroy(cr);
-        cairo_pattern_t * pattern = cairo_pattern_create_for_surface (surface);
-        cairo_surface_destroy(surface);
-        cairo_pattern_set_extend (pattern, CAIRO_EXTEND_REPEAT);
-        cairo_set_source(m_cairo, pattern);
-        cairo_pattern_destroy(pattern);
+        gs_cairo->cairo_destroy(cr);
+        cairo_pattern_t * pattern = gs_cairo->cairo_pattern_create_for_surface (surface);
+        gs_cairo->cairo_surface_destroy(surface);
+        gs_cairo->cairo_pattern_set_extend (pattern, CAIRO_EXTEND_REPEAT);
+        gs_cairo->cairo_set_source(m_cairo, pattern);
+        gs_cairo->cairo_pattern_destroy(pattern);
     }
 }
 
 void wxGtkPrintDC::SetLogicalFunction( int function )
 {
     if (function == wxCLEAR)
-        cairo_set_operator (m_cairo, CAIRO_OPERATOR_CLEAR);
+        gs_cairo->cairo_set_operator (m_cairo, CAIRO_OPERATOR_CLEAR);
     else if (function == wxOR)
-        cairo_set_operator (m_cairo, CAIRO_OPERATOR_OUT);
+        gs_cairo->cairo_set_operator (m_cairo, CAIRO_OPERATOR_OUT);
     else if (function == wxNO_OP)
-        cairo_set_operator (m_cairo, CAIRO_OPERATOR_DEST);
+        gs_cairo->cairo_set_operator (m_cairo, CAIRO_OPERATOR_DEST);
     else if (function == wxAND)
-        cairo_set_operator (m_cairo, CAIRO_OPERATOR_ADD);
+        gs_cairo->cairo_set_operator (m_cairo, CAIRO_OPERATOR_ADD);
     else if (function == wxSET)
-        cairo_set_operator (m_cairo, CAIRO_OPERATOR_SATURATE);
+        gs_cairo->cairo_set_operator (m_cairo, CAIRO_OPERATOR_SATURATE);
     else if (function == wxXOR)
-        cairo_set_operator (m_cairo, CAIRO_OPERATOR_XOR);
+        gs_cairo->cairo_set_operator (m_cairo, CAIRO_OPERATOR_XOR);
     else // wxCOPY or anything else.
-        cairo_set_operator (m_cairo, CAIRO_OPERATOR_SOURCE);
+        gs_cairo->cairo_set_operator (m_cairo, CAIRO_OPERATOR_SOURCE);
 }
 
 void wxGtkPrintDC::SetBackground( const wxBrush& brush )
 {
     m_backgroundBrush = brush;
-    cairo_save(m_cairo);
-    cairo_set_operator (m_cairo, CAIRO_OPERATOR_DEST_OVER);
+    gs_cairo->cairo_save(m_cairo);
+    gs_cairo->cairo_set_operator (m_cairo, CAIRO_OPERATOR_DEST_OVER);
 
     SetBrush(m_backgroundBrush);
-    cairo_paint(m_cairo);
-    cairo_restore(m_cairo);
+    gs_cairo->cairo_paint(m_cairo);
+    gs_cairo->cairo_restore(m_cairo);
 }
 
 void wxGtkPrintDC::SetBackgroundMode(int mode)
@@ -2154,13 +2046,13 @@ void wxGtkPrintDC::SetBackgroundMode(int mode)
 
 void wxGtkPrintDC::DoSetClippingRegion(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
 {
-    cairo_rectangle ( m_cairo, LogicalToDeviceX(x), LogicalToDeviceY(y), LogicalToDeviceXRel(width), LogicalToDeviceYRel(height));
-    cairo_clip(m_cairo);
+    gs_cairo->cairo_rectangle ( m_cairo, XLOG2DEV(x), YLOG2DEV(y), XLOG2DEVREL(width), YLOG2DEVREL(height));
+    gs_cairo->cairo_clip(m_cairo);
 }
 
 void wxGtkPrintDC::DestroyClippingRegion()
 {
-    cairo_reset_clip(m_cairo);
+    gs_cairo->cairo_reset_clip(m_cairo);
 }
 
 bool wxGtkPrintDC::StartDoc(const wxString& message)
@@ -2190,7 +2082,7 @@ wxCoord wxGtkPrintDC::GetCharHeight() const
     int w,h;
     pango_layout_get_pixel_size( m_layout, &w, &h );
 
-    return DeviceToLogicalYRel(h);
+    return wxRound( h * m_PS2DEV );
 }
 
 wxCoord wxGtkPrintDC::GetCharWidth() const
@@ -2200,7 +2092,7 @@ wxCoord wxGtkPrintDC::GetCharWidth() const
     int w,h;
     pango_layout_get_pixel_size( m_layout, &w, &h );
 
-    return DeviceToLogicalXRel(w);
+    return wxRound( w * m_PS2DEV );
 }
 
 void wxGtkPrintDC::DoGetTextExtent(const wxString& string, wxCoord *width, wxCoord *height,
@@ -2247,15 +2139,16 @@ void wxGtkPrintDC::DoGetTextExtent(const wxString& string, wxCoord *width, wxCoo
     pango_layout_get_pixel_size( m_layout, &w, &h );
 
     if (width)
-        *width = (wxCoord)(w / m_scaleX);
+        *width = wxRound( (double)w / m_scaleX * m_PS2DEV );
     if (height)
-        *height = (wxCoord)(h / m_scaleY);
+        *height = wxRound( (double)h / m_scaleY * m_PS2DEV );
+        
     if (descent)
     {
         PangoLayoutIter *iter = pango_layout_get_iter(m_layout);
         int baseline = pango_layout_iter_get_baseline(iter);
         pango_layout_iter_free(iter);
-        *descent = h - PANGO_PIXELS(baseline);
+        *descent = wxRound( (h - PANGO_PIXELS(baseline)) * m_PS2DEV );
     }
 
     // Reset unscaled size.
@@ -2267,60 +2160,43 @@ void wxGtkPrintDC::DoGetTextExtent(const wxString& string, wxCoord *width, wxCoo
 
 void wxGtkPrintDC::DoGetSize(int* width, int* height) const
 {
+    GtkPageSetup *setup = gtk_print_context_get_page_setup( m_gpc );
+
     if (width)
-        *width = (int) (gtk_print_context_get_width( m_gpc ) + 0.5);
+        *width = wxRound( gtk_page_setup_get_paper_width( setup, GTK_UNIT_POINTS ) * m_PS2DEV );
     if (height)
-        *height = (int) (gtk_print_context_get_height( m_gpc ) + 0.5);
+        *height = wxRound( gtk_page_setup_get_paper_height( setup, GTK_UNIT_POINTS ) * m_PS2DEV );
 }
 
 void wxGtkPrintDC::DoGetSizeMM(int *width, int *height) const
 {
-    // This function takes margins into consideration.
-    gdouble w = gtk_page_setup_get_page_width( gtk_print_context_get_page_setup( m_gpc ), GTK_UNIT_MM);
-    gdouble h = gtk_page_setup_get_page_height( gtk_print_context_get_page_setup( m_gpc ), GTK_UNIT_MM);
+    GtkPageSetup *setup = gtk_print_context_get_page_setup( m_gpc );
 
     if (width)
-        *width = (int) (w + 0.5);
+        *width = wxRound( gtk_page_setup_get_paper_width( setup, GTK_UNIT_MM ) );
     if (height)
-        *height = (int) (h + 0.5);
+        *height = wxRound( gtk_page_setup_get_paper_height( setup, GTK_UNIT_MM ) );
 }
 
 wxSize wxGtkPrintDC::GetPPI() const
 {
-    gdouble xDpi = gtk_print_context_get_dpi_x( m_gpc );
-    gdouble yDpi = gtk_print_context_get_dpi_y( m_gpc );
-    return wxSize((int) xDpi,(int) yDpi);
-}
-
-void wxGtkPrintDC::SetLogicalOrigin( wxCoord x, wxCoord y )
-{
-    wxDC::SetLogicalOrigin( x, y );
-}
-
-void wxGtkPrintDC::SetDeviceOrigin( wxCoord x, wxCoord y )
-{
-    wxDC::SetDeviceOrigin( x, y );
+    return wxSize( (int)m_resolution, (int)m_resolution );
 }
 
 void wxGtkPrintDC::SetPrintData(const wxPrintData& data)
 {
     m_printData = data;
-
-    if (m_printData.GetOrientation() == wxPORTRAIT)
-        GetSize( &m_deviceOffsetX, &m_deviceOffsetY );
-    else
-        GetSize( &m_deviceOffsetY, &m_deviceOffsetX );
 }
 
 void wxGtkPrintDC::SetResolution(int ppi)
 {
     // We can't change ppi of the GtkPrintContext.
-    ms_resolution = ppi;
+    // TODO: should we really support this?
 }
 
 int wxGtkPrintDC::GetResolution()
 {
-    return ms_resolution;
+    return m_resolution;
 }
 
 // ----------------------------------------------------------------------------
@@ -2379,12 +2255,16 @@ void wxGtkPrintPreview::DetermineScaling()
 
         m_previewPrintout->SetPPIScreen( (int) ((ScreenPixels.GetWidth() * 25.4) / ScreenMM.GetWidth()),
                                          (int) ((ScreenPixels.GetHeight() * 25.4) / ScreenMM.GetHeight()) );
-        m_previewPrintout->SetPPIPrinter(wxGtkPrintDC::GetResolution(), wxGtkPrintDC::GetResolution());
+                         
+        // TODO !!!!!!!!!!!!!!!      
+        int resolution = 600;
+        m_previewPrintout->SetPPIPrinter( resolution, resolution );
+        
         // Get width and height in points (1/72th of an inch)
         wxSize sizeDevUnits(paper->GetSizeDeviceUnits());
 
-        sizeDevUnits.x = (wxCoord)((float)sizeDevUnits.x * wxGtkPrintDC::GetResolution() / 72.0);
-        sizeDevUnits.y = (wxCoord)((float)sizeDevUnits.y * wxGtkPrintDC::GetResolution() / 72.0);
+        sizeDevUnits.x = wxRound((double)sizeDevUnits.x * (double)resolution / 72.0);
+        sizeDevUnits.y = wxRound((double)sizeDevUnits.y * (double)resolution / 72.0);
         wxSize sizeTenthsMM(paper->GetSize());
         wxSize sizeMM(sizeTenthsMM.x / 10, sizeTenthsMM.y / 10);
 
@@ -2405,7 +2285,7 @@ void wxGtkPrintPreview::DetermineScaling()
         m_previewPrintout->SetPaperRectPixels(wxRect(0, 0, m_pageWidth, m_pageHeight));
 
         // At 100%, the page should look about page-size on the screen.
-        m_previewScaleX = (float)0.8 * 72.0 / (float)wxGtkPrintDC::GetResolution();
+        m_previewScaleX = 0.8 * 72.0 / (double)resolution;
         m_previewScaleY = m_previewScaleX;
     }
 }