Code

get rid of a lot of no longer needed "libnr/nr-..." includes.
[inkscape.git] / src / display / canvas-bpath.cpp
index 17c88001795514f6a59c83afe495b031b3c44768..aa4b40e175568999ddb07b52a01162792a967752 100644 (file)
 #ifdef HAVE_CONFIG_H
 # include "config.h"
 #endif
+#include "color.h"
 #include "sp-canvas-util.h"
+#include "inkscape-cairo.h"
 #include "canvas-bpath.h"
 #include "display/display-forward.h"
 #include "display/curve.h"
-#include <livarot/Shape.h>
-#include <livarot/Path.h>
-#include <livarot/int-line.h>
-#include <livarot/BitLigne.h>
+#include "display/inkscape-cairo.h"
 #include <libnr/nr-pixops.h>
+#include "helper/geom.h"
+
+/*
+ * FIXME: The following #includes and some other content in this file (see below) should actually
+ * be in a separate file called display/canvas-text.cpp. It temporarily had to be moved here
+ * because of linker errors.
+ */
+
+/**
+#include "display-forward.h"
+#include "sp-canvas-util.h"
+#include "canvas-text.h"
+#include "display/inkscape-cairo.h"
+**/
+#include <sstream>
+#include <string.h>
+#include <desktop.h>
+
+/**
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+#include <color.h>
+
+#include <libnr/nr-matrix-fns.h>
+#include <libnr/nr-pixops.h>
+**/
 
 void nr_pixblock_render_bpath_rgba (Shape* theS,uint32_t color,NRRectL &area,char* destBuf,int stride);
 
@@ -31,9 +57,9 @@ static void sp_canvas_bpath_class_init (SPCanvasBPathClass *klass);
 static void sp_canvas_bpath_init (SPCanvasBPath *path);
 static void sp_canvas_bpath_destroy (GtkObject *object);
 
-static void sp_canvas_bpath_update (SPCanvasItem *item, NR::Matrix const &affine, unsigned int flags);
+static void sp_canvas_bpath_update (SPCanvasItem *item, Geom::Matrix const &affine, unsigned int flags);
 static void sp_canvas_bpath_render (SPCanvasItem *item, SPCanvasBuf *buf);
-static double sp_canvas_bpath_point (SPCanvasItem *item, NR::Point p, SPCanvasItem **actual_item);
+static double sp_canvas_bpath_point (SPCanvasItem *item, Geom::Point p, SPCanvasItem **actual_item);
 
 static SPCanvasItemClass *parent_class;
 
@@ -42,15 +68,15 @@ sp_canvas_bpath_get_type (void)
 {
     static GtkType type = 0;
     if (!type) {
-       GtkTypeInfo info = {
-           "SPCanvasBPath",
-           sizeof (SPCanvasBPath),
-           sizeof (SPCanvasBPathClass),
-           (GtkClassInitFunc) sp_canvas_bpath_class_init,
-           (GtkObjectInitFunc) sp_canvas_bpath_init,
-           NULL, NULL, NULL
-       };
-       type = gtk_type_unique (SP_TYPE_CANVAS_ITEM, &info);
+        GtkTypeInfo info = {
+            (gchar *)"SPCanvasBPath",
+            sizeof (SPCanvasBPath),
+            sizeof (SPCanvasBPathClass),
+            (GtkClassInitFunc) sp_canvas_bpath_class_init,
+            (GtkObjectInitFunc) sp_canvas_bpath_init,
+            NULL, NULL, NULL
+        };
+        type = gtk_type_unique (SP_TYPE_CANVAS_ITEM, &info);
     }
     return type;
 }
@@ -76,7 +102,7 @@ sp_canvas_bpath_class_init (SPCanvasBPathClass *klass)
 static void
 sp_canvas_bpath_init (SPCanvasBPath * bpath)
 {
-    bpath->fill_rgba = 0x000000ff;
+    bpath->fill_rgba = 0x00000000;
     bpath->fill_rule = SP_WIND_RULE_EVENODD;
 
     bpath->stroke_rgba = 0x00000000;
@@ -84,125 +110,50 @@ sp_canvas_bpath_init (SPCanvasBPath * bpath)
     bpath->stroke_linejoin = SP_STROKE_LINEJOIN_MITER;
     bpath->stroke_linecap = SP_STROKE_LINECAP_BUTT;
     bpath->stroke_miterlimit = 11.0;
-
-    bpath->fill_shp=NULL;
-    bpath->stroke_shp=NULL;
 }
 
 static void
 sp_canvas_bpath_destroy (GtkObject *object)
 {
     SPCanvasBPath *cbp = SP_CANVAS_BPATH (object);
-    if (cbp->fill_shp) {
-       delete cbp->fill_shp;
-       cbp->fill_shp = NULL;
-    }
 
-    if (cbp->stroke_shp) {
-       delete cbp->stroke_shp;
-       cbp->stroke_shp = NULL;
-    }
     if (cbp->curve) {
-       cbp->curve = sp_curve_unref (cbp->curve);
+        cbp->curve = cbp->curve->unref();
     }
 
     if (GTK_OBJECT_CLASS (parent_class)->destroy)
-       (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
+        (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
 }
 
 static void
-sp_canvas_bpath_update (SPCanvasItem *item, NR::Matrix const &affine, unsigned int flags)
+sp_canvas_bpath_update (SPCanvasItem *item, Geom::Matrix const &affine, unsigned int flags)
 {
     SPCanvasBPath *cbp = SP_CANVAS_BPATH (item);
 
     sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)item->x2, (int)item->y2);
 
     if (((SPCanvasItemClass *) parent_class)->update)
-       ((SPCanvasItemClass *) parent_class)->update (item, affine, flags);
+        ((SPCanvasItemClass *) parent_class)->update (item, affine, flags);
 
     sp_canvas_item_reset_bounds (item);
 
-    if (cbp->fill_shp) {
-       delete cbp->fill_shp;
-       cbp->fill_shp = NULL;
-    }
-
-    if (cbp->stroke_shp) {
-       delete cbp->stroke_shp;
-       cbp->stroke_shp = NULL;
-    }
     if (!cbp->curve) return;
 
-    NRRect dbox;
-
-    dbox.x0 = dbox.y0 = 0.0;
-    dbox.x1 = dbox.y1 = -1.0;
-
-    if ((cbp->fill_rgba & 0xff) || (cbp->stroke_rgba & 0xff)) {
-       Path*  thePath=new Path;
-       thePath->LoadArtBPath(SP_CURVE_BPATH(cbp->curve), affine, true);
-       thePath->Convert(0.25);
-       if ((cbp->fill_rgba & 0xff) && (cbp->curve->end > 2)) {
-           Shape* theShape=new Shape;
-           thePath->Fill(theShape,0);
-           if ( cbp->fill_shp == NULL ) cbp->fill_shp=new Shape;
-           if ( cbp->fill_rule == SP_WIND_RULE_EVENODD ) {
-               cbp->fill_shp->ConvertToShape(theShape,fill_oddEven);
-           } else {
-               cbp->fill_shp->ConvertToShape(theShape,fill_nonZero);
-           }
-           delete theShape;
-           cbp->fill_shp->CalcBBox();
-           if ( cbp->fill_shp->leftX < cbp->fill_shp->rightX ) {
-               if ( dbox.x0 >= dbox.x1 ) {
-                   dbox.x0 = cbp->fill_shp->leftX; dbox.x1 = cbp->fill_shp->rightX;
-                   dbox.y0 = cbp->fill_shp->topY; dbox.y1 = cbp->fill_shp->bottomY;
-               } else {
-                   if ( cbp->fill_shp->leftX < dbox.x0 ) dbox.x0=cbp->fill_shp->leftX;
-                   if ( cbp->fill_shp->rightX > dbox.x1 ) dbox.x1=cbp->fill_shp->rightX;
-                   if ( cbp->fill_shp->topY < dbox.y0 ) dbox.y0=cbp->fill_shp->topY;
-                   if ( cbp->fill_shp->bottomY > dbox.y1 ) dbox.y1=cbp->fill_shp->bottomY;
-               }
-           }
-       }
-       if ((cbp->stroke_rgba & 0xff) && (cbp->curve->end > 1)) {
-           JoinType join=join_straight;
-//      Shape* theShape=new Shape;
-           ButtType butt=butt_straight;
-           if ( cbp->stroke_shp == NULL ) cbp->stroke_shp=new Shape;
-           if ( cbp->stroke_linecap == SP_STROKE_LINECAP_BUTT ) butt=butt_straight;
-           if ( cbp->stroke_linecap == SP_STROKE_LINECAP_ROUND ) butt=butt_round;
-           if ( cbp->stroke_linecap == SP_STROKE_LINECAP_SQUARE ) butt=butt_square;
-           if ( cbp->stroke_linejoin == SP_STROKE_LINEJOIN_MITER ) join=join_pointy;
-           if ( cbp->stroke_linejoin == SP_STROKE_LINEJOIN_ROUND ) join=join_round;
-           if ( cbp->stroke_linejoin == SP_STROKE_LINEJOIN_BEVEL ) join=join_straight;
-           thePath->Stroke(cbp->stroke_shp,false,0.5*cbp->stroke_width, join,butt,cbp->stroke_width*cbp->stroke_miterlimit );
-           //     thePath->Stroke(theShape,false,0.5*cbp->stroke_width, join,butt,cbp->stroke_width*cbp->stroke_miterlimit );
-           //     cbp->stroke_shp->ConvertToShape(theShape,fill_nonZero);
-
-           cbp->stroke_shp->CalcBBox();
-           if ( cbp->stroke_shp->leftX < cbp->stroke_shp->rightX ) {
-               if ( dbox.x0 >= dbox.x1 ) {
-                   dbox.x0 = cbp->stroke_shp->leftX;dbox.x1 = cbp->stroke_shp->rightX;
-                   dbox.y0 = cbp->stroke_shp->topY;dbox.y1 = cbp->stroke_shp->bottomY;
-               } else {
-                   if ( cbp->stroke_shp->leftX < dbox.x0 ) dbox.x0 = cbp->stroke_shp->leftX;
-                   if ( cbp->stroke_shp->rightX > dbox.x1 ) dbox.x1 = cbp->stroke_shp->rightX;
-                   if ( cbp->stroke_shp->topY < dbox.y0 ) dbox.y0 = cbp->stroke_shp->topY;
-                   if ( cbp->stroke_shp->bottomY > dbox.y1 ) dbox.y1 = cbp->stroke_shp->bottomY;
-               }
-           }
-//      delete theShape;
-       }
-       delete thePath;
-    }
+    cbp->affine = affine;
 
+    Geom::OptRect bbox = bounds_exact_transformed(cbp->curve->get_pathvector(), affine);
 
-    item->x1 = (int)dbox.x0;
-    item->y1 = (int)dbox.y0;
-    item->x2 = (int)dbox.x1;
-    item->y2 = (int)dbox.y1;
-
+    if (bbox) {
+        item->x1 = (int)bbox->min()[Geom::X] - 1;
+        item->y1 = (int)bbox->min()[Geom::Y] - 1;
+        item->x2 = (int)bbox->max()[Geom::X] + 1;
+        item->y2 = (int)bbox->max()[Geom::Y] + 1;
+    } else {
+        item->x1 = 0;
+        item->y1 = 0;
+        item->x2 = 0;
+        item->y2 = 0;
+    }
     sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)item->x2, (int)item->y2);
 }
 
@@ -213,43 +164,70 @@ sp_canvas_bpath_render (SPCanvasItem *item, SPCanvasBuf *buf)
 
     sp_canvas_prepare_buffer(buf);
 
-    NRRectL  area;
-    area.x0=buf->rect.x0;
-    area.x1=buf->rect.x1;
-    area.y0=buf->rect.y0;
-    area.y1=buf->rect.y1;
-    if ( cbp->fill_shp ) {
-       nr_pixblock_render_bpath_rgba (cbp->fill_shp,cbp->fill_rgba,area,(char*)buf->buf, buf->buf_rowstride);
-    }
-    if ( cbp->stroke_shp ) {
-       nr_pixblock_render_bpath_rgba (cbp->stroke_shp,cbp->stroke_rgba,area,(char*)buf->buf, buf->buf_rowstride);
+    Geom::Rect area (Geom::Point(buf->rect.x0, buf->rect.y0), Geom::Point(buf->rect.x1, buf->rect.y1));
+
+    if ( !cbp->curve  || 
+         ((cbp->stroke_rgba & 0xff) == 0 && (cbp->fill_rgba & 0xff) == 0 ) || 
+         cbp->curve->get_segment_count() < 1)
+        return;
+
+    if (!buf->ct)
+        return;
+
+    bool dofill = ((cbp->fill_rgba & 0xff) != 0);
+    bool dostroke = ((cbp->stroke_rgba & 0xff) != 0);
+
+    cairo_set_tolerance(buf->ct, 1.25); // low quality, but good enough for canvas items
+    cairo_new_path(buf->ct);
+
+    if (!dofill)
+        feed_pathvector_to_cairo (buf->ct, cbp->curve->get_pathvector(), cbp->affine, area, true, 1);
+    else
+        feed_pathvector_to_cairo (buf->ct, cbp->curve->get_pathvector(), cbp->affine, area, false, 1);
+
+    if (dofill) {
+        // RGB / BGR
+        cairo_set_source_rgba(buf->ct, SP_RGBA32_B_F(cbp->fill_rgba), SP_RGBA32_G_F(cbp->fill_rgba), SP_RGBA32_R_F(cbp->fill_rgba), SP_RGBA32_A_F(cbp->fill_rgba));
+        cairo_set_fill_rule(buf->ct, cbp->fill_rule == SP_WIND_RULE_EVENODD? CAIRO_FILL_RULE_EVEN_ODD
+                            : CAIRO_FILL_RULE_WINDING);
+        if (dostroke)
+            cairo_fill_preserve(buf->ct);
+        else 
+            cairo_fill(buf->ct);
     }
 
+    if (dostroke) {
+        // RGB / BGR
+        cairo_set_source_rgba(buf->ct, SP_RGBA32_B_F(cbp->stroke_rgba), SP_RGBA32_G_F(cbp->stroke_rgba), SP_RGBA32_R_F(cbp->stroke_rgba), SP_RGBA32_A_F(cbp->stroke_rgba));
+        cairo_set_line_width(buf->ct, 1);
+        if (cbp->dashes[0] != 0 && cbp->dashes[1] != 0) {
+            cairo_set_dash (buf->ct, cbp->dashes, 2, 0);
+        }
+        cairo_stroke(buf->ct);
+    }
 }
 
-#define BIGVAL 1e18
-
 static double
-sp_canvas_bpath_point (SPCanvasItem *item, NR::Point p, SPCanvasItem **actual_item)
+sp_canvas_bpath_point (SPCanvasItem *item, Geom::Point p, SPCanvasItem **actual_item)
 {
     SPCanvasBPath *cbp = SP_CANVAS_BPATH (item);
 
-    if (cbp->fill_shp && (cbp->fill_shp->PtWinding(p) > 0 )) {
-       *actual_item = item;
-       return 0.0;
-    }
-    if (cbp->stroke_shp ) {
-       if (cbp->stroke_shp->PtWinding(p) > 0 ) {
-           *actual_item = item;
-           return 0.0;
-       }
-       return distance(cbp->stroke_shp, p);
-    }
-    if (cbp->fill_shp) {
-        return distance(cbp->fill_shp, p);
+    if ( !cbp->curve  || 
+         ((cbp->stroke_rgba & 0xff) == 0 && (cbp->fill_rgba & 0xff) == 0 ) || 
+         cbp->curve->get_segment_count() < 1)
+        return NR_HUGE;
+
+    double width = 0.5;
+    Geom::Rect viewbox = item->canvas->getViewbox();
+    viewbox.expandBy (width);
+    double dist = NR_HUGE;
+    pathv_matrix_point_bbox_wind_distance(cbp->curve->get_pathvector(), cbp->affine, p, NULL, NULL, &dist, 0.5, &viewbox);
+
+    if (dist <= 1.0) {
+        *actual_item = item;
     }
 
-    return BIGVAL;
+    return dist;
 }
 
 SPCanvasItem *
@@ -272,11 +250,11 @@ sp_canvas_bpath_set_bpath (SPCanvasBPath *cbp, SPCurve *curve)
     g_return_if_fail (SP_IS_CANVAS_BPATH (cbp));
 
     if (cbp->curve) {
-       cbp->curve = sp_curve_unref (cbp->curve);
+        cbp->curve = cbp->curve->unref();
     }
 
     if (curve) {
-       cbp->curve = sp_curve_ref (curve);
+        cbp->curve = curve->ref();
     }
 
     sp_canvas_item_request_update (SP_CANVAS_ITEM (cbp));
@@ -295,7 +273,7 @@ sp_canvas_bpath_set_fill (SPCanvasBPath *cbp, guint32 rgba, SPWindRule rule)
 }
 
 void
-sp_canvas_bpath_set_stroke (SPCanvasBPath *cbp, guint32 rgba, gdouble width, SPStrokeJoinType join, SPStrokeCapType cap)
+sp_canvas_bpath_set_stroke (SPCanvasBPath *cbp, guint32 rgba, gdouble width, SPStrokeJoinType join, SPStrokeCapType cap, double dash, double gap)
 {
     g_return_if_fail (cbp != NULL);
     g_return_if_fail (SP_IS_CANVAS_BPATH (cbp));
@@ -304,182 +282,254 @@ sp_canvas_bpath_set_stroke (SPCanvasBPath *cbp, guint32 rgba, gdouble width, SPS
     cbp->stroke_width = MAX (width, 0.1);
     cbp->stroke_linejoin = join;
     cbp->stroke_linecap = cap;
+    cbp->dashes[0] = dash;
+    cbp->dashes[1] = gap;
 
     sp_canvas_item_request_update (SP_CANVAS_ITEM (cbp));
 }
 
 
 
-static void
-bpath_run_A8_OR (raster_info &dest,void *data,int st,float vst,int en,float ven)
+/*
+ * FIXME: The following code should actually be in a separate file called
+ * display/canvas-text.cpp. It temporarily had to be moved here because of linker errors.
+ */
+
+static void sp_canvastext_class_init (SPCanvasTextClass *klass);
+static void sp_canvastext_init (SPCanvasText *canvastext);
+static void sp_canvastext_destroy (GtkObject *object);
+
+static void sp_canvastext_update (SPCanvasItem *item, Geom::Matrix const &affine, unsigned int flags);
+static void sp_canvastext_render (SPCanvasItem *item, SPCanvasBuf *buf);
+
+static SPCanvasItemClass *parent_class_ct;
+
+GtkType
+sp_canvastext_get_type (void)
 {
-    union {
-       uint8_t  comp[4];
-       uint32_t col;
-    } tempCol;
-    if ( st >= en ) return;
-    tempCol.col=*(uint32_t*)data;
-
-    unsigned int r, g, b, a;
-    r = NR_RGBA32_R (tempCol.col);
-    g = NR_RGBA32_G (tempCol.col);
-    b = NR_RGBA32_B (tempCol.col);
-    a = NR_RGBA32_A (tempCol.col);
-    if (a == 0) return;
-
-    vst*=a;
-    ven*=a;
-
-    if ( vst < 0 ) vst=0;
-    if ( vst > 255 ) vst=255;
-    if ( ven < 0 ) ven=0;
-    if ( ven > 255 ) ven=255;
-    float      sv=vst;
-    float      dv=ven-vst;
-    int        len=en-st;
-    uint8_t*   d=(uint8_t*)dest.buffer;
-
-    d+=3*(st-dest.startPix);
-    if ( fabs(dv) < 0.001 ) {
-       if ( sv > 249.999 ) {
-           /* Simple copy */
-           while (len > 0) {
-               d[0] = NR_COMPOSEN11 (r, 255, d[0]);
-               d[1] = NR_COMPOSEN11 (g, 255, d[1]);
-               d[2] = NR_COMPOSEN11 (b, 255, d[2]);
-               d += 3;
-               len -= 1;
-           }
-       } else {
-           unsigned int c0_24=(int)sv;
-           c0_24&=0xFF;
-           while (len > 0) {
-               d[0] = NR_COMPOSEN11 (r, c0_24, d[0]);
-               d[1] = NR_COMPOSEN11 (g, c0_24, d[1]);
-               d[2] = NR_COMPOSEN11 (b, c0_24, d[2]);
-               d += 3;
-               len -= 1;
-           }
-       }
-    } else {
-       if ( en <= st+1 ) {
-           sv=0.5*(vst+ven);
-           unsigned int c0_24=(int)sv;
-           c0_24&=0xFF;
-           d[0] = NR_COMPOSEN11 (r, c0_24, d[0]);
-           d[1] = NR_COMPOSEN11 (g, c0_24, d[1]);
-           d[2] = NR_COMPOSEN11 (b, c0_24, d[2]);
-       } else {
-           dv/=len;
-           sv+=0.5*dv; // correction trapezoidale
-           sv*=65536;
-           dv*=65536;
-           int c0_24 = static_cast<int>(CLAMP(sv, 0, 16777216));
-           int s0_24 = static_cast<int>(dv);
-           while (len > 0) {
-               unsigned int ca;
-               /* Draw */
-               ca = c0_24 >> 16;
-               if ( ca > 255 ) ca=255;
-               d[0] = NR_COMPOSEN11 (r, ca, d[0]);
-               d[1] = NR_COMPOSEN11 (g, ca, d[1]);
-               d[2] = NR_COMPOSEN11 (b, ca, d[2]);
-               d += 3;
-               c0_24 += s0_24;
-               c0_24 = CLAMP (c0_24, 0, 16777216);
-               len -= 1;
-           }
-       }
+    static GtkType type = 0;
+
+    if (!type) {
+        GtkTypeInfo info = {
+            (gchar *)"SPCanvasText",
+            sizeof (SPCanvasText),
+            sizeof (SPCanvasTextClass),
+            (GtkClassInitFunc) sp_canvastext_class_init,
+            (GtkObjectInitFunc) sp_canvastext_init,
+            NULL, NULL, NULL
+        };
+        type = gtk_type_unique (SP_TYPE_CANVAS_ITEM, &info);
     }
+    return type;
 }
 
-void nr_pixblock_render_bpath_rgba (Shape* theS,uint32_t color,NRRectL &area,char* destBuf,int stride)
+static void
+sp_canvastext_class_init (SPCanvasTextClass *klass)
 {
+    GtkObjectClass *object_class = (GtkObjectClass *) klass;
+    SPCanvasItemClass *item_class = (SPCanvasItemClass *) klass;
 
-    theS->CalcBBox();
-    float  l=theS->leftX,r=theS->rightX,t=theS->topY,b=theS->bottomY;
-    int    il,ir,it,ib;
-    il=(int)floor(l);
-    ir=(int)ceil(r);
-    it=(int)floor(t);
-    ib=(int)ceil(b);
-
-    if ( il >= area.x1 || ir <= area.x0 || it >= area.y1 || ib <= area.y0 ) return;
-    if ( il < area.x0 ) il=area.x0;
-    if ( it < area.y0 ) it=area.y0;
-    if ( ir > area.x1 ) ir=area.x1;
-    if ( ib > area.y1 ) ib=area.y1;
-
-/*  // version par FloatLigne
-    int    curPt;
-    float  curY;
-    theS->BeginRaster(curY,curPt,1.0);
-
-    FloatLigne* theI=new FloatLigne();
-    IntLigne*   theIL=new IntLigne();
-
-    theS->Scan(curY,curPt,(float)(it),1.0);
-
-    char* mdata=(char*)destBuf;
-    uint32_t* ligStart=((uint32_t*)(mdata+(3*(il-area.x0)+stride*(it-area.y0))));
-    for (int y=it;y<ib;y++) {
-    theI->Reset();
-    if ( y&0x00000003 ) {
-    theS->Scan(curY,curPt,((float)(y+1)),theI,false,1.0);
-    } else {
-    theS->Scan(curY,curPt,((float)(y+1)),theI,true,1.0);
-    }
-    theI->Flatten();
-    theIL->Copy(theI);
-
-    raster_info  dest;
-    dest.startPix=il;
-    dest.endPix=ir;
-    dest.sth=il;
-    dest.stv=y;
-    dest.buffer=ligStart;
-    theIL->Raster(dest,&color,bpath_run_A8_OR);
-    ligStart=((uint32_t*)(((char*)ligStart)+stride));
+    parent_class_ct = (SPCanvasItemClass*)gtk_type_class (SP_TYPE_CANVAS_ITEM);
+
+    object_class->destroy = sp_canvastext_destroy;
+
+    item_class->update = sp_canvastext_update;
+    item_class->render = sp_canvastext_render;
+}
+
+static void
+sp_canvastext_init (SPCanvasText *canvastext)
+{
+    canvastext->rgba = 0x0000ff7f;
+    canvastext->s[Geom::X] = canvastext->s[Geom::Y] = 0.0;
+    canvastext->affine = Geom::identity();
+    canvastext->fontsize = 10.0;
+    canvastext->item = NULL;
+    canvastext->desktop = NULL;
+    canvastext->text = NULL;
+}
+
+static void
+sp_canvastext_destroy (GtkObject *object)
+{
+    g_return_if_fail (object != NULL);
+    g_return_if_fail (SP_IS_CANVASTEXT (object));
+
+    SPCanvasText *canvastext = SP_CANVASTEXT (object);
+
+    canvastext->item=NULL;
+
+    if (GTK_OBJECT_CLASS (parent_class_ct)->destroy)
+        (* GTK_OBJECT_CLASS (parent_class_ct)->destroy) (object);
+}
+
+// FIXME: remove this as soon as we know how to correctly determine the text extent
+static const double arbitrary_factor = 0.7;
+
+// these are set in sp_canvastext_update() and then re-used in sp_canvastext_render(), which is called afterwards
+static double anchor_offset_x = 0;
+static double anchor_offset_y = 0;
+
+static void
+sp_canvastext_render (SPCanvasItem *item, SPCanvasBuf *buf)
+{
+    SPCanvasText *cl = SP_CANVASTEXT (item);
+
+    if (!buf->ct)
+        return;
+
+    guint32 rgba = cl->rgba;
+    cairo_set_source_rgba(buf->ct, SP_RGBA32_B_F(rgba), SP_RGBA32_G_F(rgba), SP_RGBA32_R_F(rgba), SP_RGBA32_A_F(rgba));
+
+    Geom::Point s = cl->s * cl->affine;
+    double offsetx = s[Geom::X] - buf->rect.x0;
+    double offsety = s[Geom::Y] - buf->rect.y0;
+    offsetx -= anchor_offset_x;
+    offsety += anchor_offset_y;
+
+    cairo_move_to(buf->ct, offsetx, offsety);
+    cairo_set_font_size(buf->ct, cl->fontsize);
+    cairo_show_text(buf->ct, cl->text);
+    cairo_stroke(buf->ct);
+
+    cairo_new_path(buf->ct);
+}
+
+static void
+sp_canvastext_update (SPCanvasItem *item, Geom::Matrix const &affine, unsigned int flags)
+{
+    SPCanvasText *cl = SP_CANVASTEXT (item);
+
+    sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)item->x2, (int)item->y2);
+
+    if (parent_class_ct->update)
+        (* parent_class_ct->update) (item, affine, flags);
+
+    sp_canvas_item_reset_bounds (item);
+
+    cl->affine = affine;
+
+    Geom::Point s = cl->s * affine;
+
+    // set up a temporary cairo_t to measure the text extents; it would be better to compute this in the render()
+    // method but update() seems to be called before so we don't have the information available when we need it
+    /**
+    cairo_t tmp_buf;
+    cairo_text_extents_t bbox;
+    cairo_text_extents(&tmp_buf, cl->text, &bbox);
+    **/
+    item->x1 = s[Geom::X] + 0;
+    item->y1 = s[Geom::Y] - cl->fontsize;
+    item->x2 = s[Geom::X] + cl->fontsize * strlen(cl->text);
+    item->y2 = s[Geom::Y] + cl->fontsize * 0.5; // for letters below the baseline
+
+    // adjust update region according to anchor shift
+    // FIXME: use the correct text extent
+    anchor_offset_x = arbitrary_factor * cl->fontsize * strlen(cl->text) * (cl->anchor_x + 1.0) / 2.0;
+    anchor_offset_y = cl->fontsize * (cl->anchor_y + 1.0) / 2.0;
+    item->x1 -= anchor_offset_x;
+    item->x2 -= anchor_offset_x;
+    item->y1 += anchor_offset_y;
+    item->y2 += anchor_offset_y;
+
+    sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)item->x2, (int)item->y2);
+}
+
+SPCanvasItem *
+sp_canvastext_new(SPCanvasGroup *parent, SPDesktop *desktop, Geom::Point pos, gchar const *new_text)
+{
+    SPCanvasItem *item = sp_canvas_item_new(parent, SP_TYPE_CANVASTEXT, NULL);
+
+    SPCanvasText *ct = SP_CANVASTEXT(item);
+
+    ct->desktop = desktop;
+
+    ct->s = pos;
+    g_free(ct->text);
+    ct->text = g_strdup(new_text);
+
+    // TODO: anything else to do?
+
+    return item;
+}
+
+
+void
+sp_canvastext_set_rgba32 (SPCanvasText *ct, guint32 rgba)
+{
+    g_return_if_fail (ct != NULL);
+    g_return_if_fail (SP_IS_CANVASTEXT (ct));
+
+    if (rgba != ct->rgba) {
+        SPCanvasItem *item;
+        ct->rgba = rgba;
+        item = SP_CANVAS_ITEM (ct);
+        sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)item->x2, (int)item->y2);
     }
-    theS->EndRaster();
-    delete theI;
-    delete theIL;  */
-
-    // version par BitLigne directe
-    int    curPt;
-    float  curY;
-    theS->BeginQuickRaster(curY, curPt);
-
-    BitLigne*   theI[4];
-    for (int i=0;i<4;i++) theI[i]=new BitLigne(il,ir);
-    IntLigne*   theIL=new IntLigne();
-
-    theS->DirectQuickScan(curY,curPt,(float)(it),true,0.25);
-
-    char* mdata=(char*)destBuf;
-    uint32_t* ligStart=((uint32_t*)(mdata+(3*(il-area.x0)+stride*(it-area.y0))));
-    for (int y=it;y<ib;y++) {
-       for (int i = 0; i < 4; i++)
-           theI[i]->Reset();
-
-       for (int i = 0; i < 4; i++)
-           theS->QuickScan(curY, curPt, ((float)(y+0.25*(i+1))),
-                           fill_oddEven, theI[i], 0.25);
-
-       theIL->Copy(4,theI);
-       //   theI[0]->Affiche();
-       //   theIL->Affiche();
-
-       raster_info  dest;
-       dest.startPix=il;
-       dest.endPix=ir;
-       dest.sth=il;
-       dest.stv=y;
-       dest.buffer=ligStart;
-       theIL->Raster(dest,&color,bpath_run_A8_OR);
-       ligStart=((uint32_t*)(((char*)ligStart)+stride));
+    sp_canvas_item_request_update (SP_CANVAS_ITEM (ct));
+}
+
+#define EPSILON 1e-6
+#define DIFFER(a,b) (fabs ((a) - (b)) > EPSILON)
+
+void
+sp_canvastext_set_coords (SPCanvasText *ct, gdouble x0, gdouble y0)
+{
+    sp_canvastext_set_coords(ct, Geom::Point(x0, y0));
+}
+
+void
+sp_canvastext_set_coords (SPCanvasText *ct, const Geom::Point start)
+{
+    Geom::Point pos = ct->desktop->doc2dt(start);
+
+    g_return_if_fail (ct != NULL);
+    g_return_if_fail (SP_IS_CANVASTEXT (ct));
+
+    if (DIFFER (pos[0], ct->s[Geom::X]) || DIFFER (pos[1], ct->s[Geom::Y])) {
+        ct->s[Geom::X] = pos[0];
+        ct->s[Geom::Y] = pos[1];
+        sp_canvas_item_request_update (SP_CANVAS_ITEM (ct));
     }
-    theS->EndQuickRaster();
-    for (int i=0;i<4;i++) delete theI[i];
-    delete theIL;
+    sp_canvas_item_request_update (SP_CANVAS_ITEM (ct));
+}
+
+void
+sp_canvastext_set_text (SPCanvasText *ct, gchar const * new_text)
+{
+    g_free (ct->text);
+    ct->text = g_strdup(new_text);
+    sp_canvas_item_request_update (SP_CANVAS_ITEM (ct));
+}
+
+void
+sp_canvastext_set_number_as_text (SPCanvasText *ct, int num)
+{
+    std::ostringstream number;
+    number << num;
+    sp_canvastext_set_text(ct, number.str().c_str());
+}
+
+void
+sp_canvastext_set_fontsize (SPCanvasText *ct, double size)
+{
+    ct->fontsize = size;
+}
+
+void
+sp_canvastext_set_anchor (SPCanvasText *ct, double anchor_x, double anchor_y)
+{
+    ct->anchor_x = anchor_x;
+    ct->anchor_y = anchor_y;
 }
+
+/*
+  Local Variables:
+  mode:c++
+  c-file-style:"stroustrup"
+  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
+  indent-tabs-mode:nil
+  fill-column:99
+  End:
+*/
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :