Code

add SPCurve::first_segment and last_segment
[inkscape.git] / src / display / nr-arena-glyphs.cpp
index a29c44ee14bc6bbb33073faf121c65aacf9b3544..7ca6d9aa157fb2a42ca33aa6c41b04dff9705df9 100644 (file)
 void nr_pixblock_render_shape_mask_or(NRPixBlock &m, Shape *theS);
 #endif
 
+#ifdef ENABLE_SVG_FONTS
+#include "nr-svgfonts.h"
+#endif //#ifdef ENABLE_SVG_FONTS
+
 static void nr_arena_glyphs_class_init(NRArenaGlyphsClass *klass);
 static void nr_arena_glyphs_init(NRArenaGlyphs *glyphs);
 static void nr_arena_glyphs_finalize(NRObject *object);
@@ -85,7 +89,7 @@ static void
 nr_arena_glyphs_init(NRArenaGlyphs *glyphs)
 {
     glyphs->style = NULL;
-    nr_matrix_set_identity(&glyphs->g_transform);
+    glyphs->g_transform.set_identity();
     glyphs->font = NULL;
     glyphs->glyph = 0;
 
@@ -122,34 +126,36 @@ nr_arena_glyphs_finalize(NRObject *object)
 }
 
 static guint
-nr_arena_glyphs_update(NRArenaItem *item, NRRectL *area, NRGC *gc, guint state, guint reset)
+nr_arena_glyphs_update(NRArenaItem *item, NRRectL */*area*/, NRGC *gc, guint /*state*/, guint /*reset*/)
 {
     NRArenaGlyphs *glyphs;
     raster_font *rfont;
 
     glyphs = NR_ARENA_GLYPHS(item);
 
-    if (!glyphs->font || !glyphs->style) return NR_ARENA_ITEM_STATE_ALL;
-    if ((glyphs->style->fill.type == SP_PAINT_TYPE_NONE) && (glyphs->style->stroke.type == SP_PAINT_TYPE_NONE)) return NR_ARENA_ITEM_STATE_ALL;
+    if (!glyphs->font || !glyphs->style)
+        return NR_ARENA_ITEM_STATE_ALL;
+    if ((glyphs->style->fill.isNone()) && (glyphs->style->stroke.isNone()))
+        return NR_ARENA_ITEM_STATE_ALL;
 
     NRRect bbox;
     bbox.x0 = bbox.y0 = NR_HUGE;
     bbox.x1 = bbox.y1 = -NR_HUGE;
 
-    float const scale = NR_MATRIX_DF_EXPANSION(&gc->transform);
+    float const scale = NR::expansion(gc->transform);
 
-    if (glyphs->style->fill.type != SP_PAINT_TYPE_NONE) {
-        NRMatrix t;
-        nr_matrix_multiply(&t, &glyphs->g_transform, &gc->transform);
-        glyphs->x = t.c[4];
-        glyphs->y = t.c[5];
-        t.c[4]=0;
-        t.c[5]=0;
+    if (!glyphs->style->fill.isNone()) {
+        NR::Matrix t;
+        t = glyphs->g_transform * gc->transform;
+        glyphs->x = t[4];
+        glyphs->y = t[5];
+        t[4]=0;
+        t[5]=0;
         rfont = glyphs->font->RasterFont(t, 0);
         if (glyphs->rfont) glyphs->rfont->Unref();
         glyphs->rfont = rfont;
 
-        if (glyphs->style->stroke.type == SP_PAINT_TYPE_NONE || fabs(glyphs->style->stroke_width.computed * scale) <= 0.01) { // Optimization: do fill bbox only if there's no stroke
+        if (glyphs->style->stroke.isNone() || fabs(glyphs->style->stroke_width.computed * scale) <= 0.01) { // Optimization: do fill bbox only if there's no stroke
             NRRect narea;
             if ( glyphs->rfont ) glyphs->rfont->BBox(glyphs->glyph, &narea);
             bbox.x0 = narea.x0 + glyphs->x;
@@ -157,16 +163,16 @@ nr_arena_glyphs_update(NRArenaItem *item, NRRectL *area, NRGC *gc, guint state,
             bbox.x1 = narea.x1 + glyphs->x;
             bbox.y1 = narea.y1 + glyphs->y;
         }
-    }                               
+    }
 
-    if (glyphs->style->stroke.type != SP_PAINT_TYPE_NONE) {
+    if (!glyphs->style->stroke.isNone()) {
         /* Build state data */
-        NRMatrix t;
-        nr_matrix_multiply(&t, &glyphs->g_transform, &gc->transform);
-        glyphs->x = t.c[4];
-        glyphs->y = t.c[5];
-        t.c[4]=0;
-        t.c[5]=0;
+        NR::Matrix t;
+        t = glyphs->g_transform * gc->transform;
+        glyphs->x = t[4];
+        glyphs->y = t[5];
+        t[4]=0;
+        t[5]=0;
 
         if ( fabs(glyphs->style->stroke_width.computed * scale) > 0.01 ) { // sinon c'est 0=oon veut pas de bord
             font_style nstyl;
@@ -180,8 +186,10 @@ nr_arena_glyphs_update(NRArenaItem *item, NRRectL *area, NRGC *gc, guint state,
             if ( glyphs->style->stroke_linejoin.computed == SP_STROKE_LINEJOIN_BEVEL ) nstyl.stroke_join=join_straight;
             nstyl.stroke_miter_limit = glyphs->style->stroke_miterlimit.value;
             nstyl.nbDash=0;
+            nstyl.dash_offset = 0;
             nstyl.dashes=NULL;
             if ( glyphs->style->stroke_dash.n_dash > 0 ) {
+                nstyl.dash_offset = glyphs->style->stroke_dash.offset * scale;
                 nstyl.nbDash=glyphs->style->stroke_dash.n_dash;
                 nstyl.dashes=(double*)malloc(nstyl.nbDash*sizeof(double));
                 for (int i = 0; i < nstyl.nbDash; i++) nstyl.dashes[i]= glyphs->style->stroke_dash.dash[i] * scale;
@@ -215,7 +223,7 @@ nr_arena_glyphs_update(NRArenaItem *item, NRRectL *area, NRGC *gc, guint state,
 }
 
 static guint
-nr_arena_glyphs_clip(NRArenaItem *item, NRRectL *area, NRPixBlock *pb)
+nr_arena_glyphs_clip(NRArenaItem *item, NRRectL */*area*/, NRPixBlock */*pb*/)
 {
     NRArenaGlyphs *glyphs;
 
@@ -229,7 +237,7 @@ nr_arena_glyphs_clip(NRArenaItem *item, NRRectL *area, NRPixBlock *pb)
 }
 
 static NRArenaItem *
-nr_arena_glyphs_pick(NRArenaItem *item, NR::Point p, gdouble delta, unsigned int sticky)
+nr_arena_glyphs_pick(NRArenaItem *item, NR::Point p, gdouble /*delta*/, unsigned int /*sticky*/)
 {
     NRArenaGlyphs *glyphs;
 
@@ -237,7 +245,7 @@ nr_arena_glyphs_pick(NRArenaItem *item, NR::Point p, gdouble delta, unsigned int
 
     if (!glyphs->font ) return NULL;
     if (!glyphs->style) return NULL;
-       
+
     double const x = p[NR::X];
     double const y = p[NR::Y];
     /* With text we take a simple approach: pick if the point is in a characher bbox */
@@ -247,19 +255,19 @@ nr_arena_glyphs_pick(NRArenaItem *item, NR::Point p, gdouble delta, unsigned int
 }
 
 void
-nr_arena_glyphs_set_path(NRArenaGlyphs *glyphs, SPCurve *curve, unsigned int lieutenant, font_instance *font, gint glyph, NRMatrix const *transform)
+nr_arena_glyphs_set_path(NRArenaGlyphs *glyphs, SPCurve */*curve*/, unsigned int /*lieutenant*/, font_instance *font, gint glyph, NR::Matrix const *transform)
 {
     nr_return_if_fail(glyphs != NULL);
     nr_return_if_fail(NR_IS_ARENA_GLYPHS(glyphs));
 
     nr_arena_item_request_render(NR_ARENA_ITEM(glyphs));
-  
+
     if (transform) {
         glyphs->g_transform = *transform;
     } else {
-        nr_matrix_set_identity(&glyphs->g_transform);
+        glyphs->g_transform.set_identity();
     }
-               
+
     if (font) font->Ref();
     if (glyphs->font) glyphs->font->Unref();
     glyphs->font=font;
@@ -313,7 +321,7 @@ static void nr_arena_glyphs_group_init(NRArenaGlyphsGroup *group);
 static void nr_arena_glyphs_group_finalize(NRObject *object);
 
 static guint nr_arena_glyphs_group_update(NRArenaItem *item, NRRectL *area, NRGC *gc, guint state, guint reset);
-static unsigned int nr_arena_glyphs_group_render(NRArenaItem *item, NRRectL *area, NRPixBlock *pb, unsigned int flags);
+static unsigned int nr_arena_glyphs_group_render(cairo_t *ct, NRArenaItem *item, NRRectL *area, NRPixBlock *pb, unsigned int flags);
 static unsigned int nr_arena_glyphs_group_clip(NRArenaItem *item, NRRectL *area, NRPixBlock *pb);
 static NRArenaItem *nr_arena_glyphs_group_pick(NRArenaItem *item, NR::Point p, gdouble delta, unsigned int sticky);
 
@@ -404,21 +412,21 @@ nr_arena_glyphs_group_update(NRArenaItem *item, NRRectL *area, NRGC *gc, guint s
     }
 
     item->render_opacity = TRUE;
-    if (group->style->fill.type == SP_PAINT_TYPE_PAINTSERVER) {
+    if (group->style->fill.isPaintserver()) {
         group->fill_painter = sp_paint_server_painter_new(SP_STYLE_FILL_SERVER(group->style),
-                                                          NR::Matrix(&gc->transform), NR::Matrix(&gc->parent->transform),
+                                                          gc->transform, gc->parent->transform,
                                                           &group->paintbox);
         item->render_opacity = FALSE;
     }
 
-    if (group->style->stroke.type == SP_PAINT_TYPE_PAINTSERVER) {
+    if (group->style->stroke.isPaintserver()) {
         group->stroke_painter = sp_paint_server_painter_new(SP_STYLE_STROKE_SERVER(group->style),
-                                                            NR::Matrix(&gc->transform), NR::Matrix(&gc->parent->transform),
+                                                            gc->transform, gc->parent->transform,
                                                             &group->paintbox);
         item->render_opacity = FALSE;
     }
 
-    if ( item->render_opacity == TRUE && group->style->stroke.type != SP_PAINT_TYPE_NONE && group->style->fill.type != SP_PAINT_TYPE_NONE ) {
+    if ( item->render_opacity == TRUE && !group->style->stroke.isNone() && !group->style->fill.isNone() ) {
         item->render_opacity=FALSE;
     }
 
@@ -430,7 +438,7 @@ nr_arena_glyphs_group_update(NRArenaItem *item, NRRectL *area, NRGC *gc, guint s
 
 
 static unsigned int
-nr_arena_glyphs_group_render(NRArenaItem *item, NRRectL *area, NRPixBlock *pb, unsigned int flags)
+nr_arena_glyphs_group_render(cairo_t *ct, NRArenaItem *item, NRRectL *area, NRPixBlock *pb, unsigned int /*flags*/)
 {
     NRArenaItem *child;
 
@@ -440,15 +448,15 @@ nr_arena_glyphs_group_render(NRArenaItem *item, NRRectL *area, NRPixBlock *pb, u
 
     guint ret = item->state;
 
-    if (item->arena->rendermode == RENDERMODE_OUTLINE) {
+    if (item->arena->rendermode == Inkscape::RENDERMODE_OUTLINE) {
 
-        cairo_t *ct = nr_create_cairo_context (area, pb);
-
-        if (!ct) 
+        if (!ct)
             return item->state;
 
         guint32 rgba = item->arena->outlinecolor;
-        cairo_set_source_rgba(ct, SP_RGBA32_R_F(rgba), SP_RGBA32_G_F(rgba), SP_RGBA32_B_F(rgba), SP_RGBA32_A_F(rgba));
+        // FIXME: we use RGBA buffers but cairo writes BGRA (on i386), so we must cheat
+        // by setting color channels in the "wrong" order
+        cairo_set_source_rgba(ct, SP_RGBA32_B_F(rgba), SP_RGBA32_G_F(rgba), SP_RGBA32_R_F(rgba), SP_RGBA32_A_F(rgba));
         cairo_set_tolerance(ct, 1.25); // low quality, but good enough for outline mode
 
         for (child = group->children; child != NULL; child = child->next) {
@@ -458,34 +466,28 @@ nr_arena_glyphs_group_render(NRArenaItem *item, NRRectL *area, NRPixBlock *pb, u
 
             cairo_new_path(ct);
             NR::Matrix g_t(g->g_transform);
-            feed_curve_to_cairo (ct, bpath, g_t * group->ctm, NR::Point(area->x0, area->y0));
+            feed_curve_to_cairo (ct, bpath, g_t * group->ctm, (pb->area).upgrade(), false, 0);
             cairo_fill(ct);
+            pb->empty = FALSE;
         }
 
-        cairo_surface_t *cst = cairo_get_target(ct);
-        cairo_destroy (ct);
-        cairo_surface_finish (cst);
-        cairo_surface_destroy (cst);
-
-        pb->empty = FALSE;
         return ret;
     }
 
 
 
-
     /* Fill */
-    if (style->fill.type != SP_PAINT_TYPE_NONE || item->arena->rendermode == RENDERMODE_OUTLINE) {
+    if (!style->fill.isNone() || item->arena->rendermode == Inkscape::RENDERMODE_OUTLINE) {
         NRPixBlock m;
         nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
 
-        // if memory allocation failed, abort 
+        // if memory allocation failed, abort
         if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
             nr_pixblock_release (&m);
             return (item->state);
         }
 
-        m.visible_area = pb->visible_area; 
+        m.visible_area = pb->visible_area;
 
         /* Render children fill mask */
         for (child = group->children; child != NULL; child = child->next) {
@@ -497,42 +499,41 @@ nr_arena_glyphs_group_render(NRArenaItem *item, NRRectL *area, NRPixBlock *pb, u
         }
 
         /* Composite into buffer */
-        if (style->fill.type == SP_PAINT_TYPE_COLOR || item->arena->rendermode == RENDERMODE_OUTLINE) {
+        if (style->fill.isPaintserver()) {
+            if (ggroup->fill_painter) {
+                nr_arena_render_paintserver_fill(pb, area, ggroup->fill_painter, SP_SCALE24_TO_FLOAT(style->fill_opacity.value), &m);
+            }
+        } else if (style->fill.isColor() || item->arena->rendermode == Inkscape::RENDERMODE_OUTLINE) {
             guint32 rgba;
-            if (item->arena->rendermode == RENDERMODE_OUTLINE) {
+            if (item->arena->rendermode == Inkscape::RENDERMODE_OUTLINE) {
                 // In outline mode, render fill only, using outlinecolor
                 rgba = item->arena->outlinecolor;
             } else if ( item->render_opacity ) {
-                rgba = sp_color_get_rgba32_falpha(&style->fill.value.color,
-                                                  SP_SCALE24_TO_FLOAT(style->fill_opacity.value) *
-                                                  SP_SCALE24_TO_FLOAT(style->opacity.value));
+                rgba = style->fill.value.color.toRGBA32( SP_SCALE24_TO_FLOAT(style->fill_opacity.value) *
+                                                         SP_SCALE24_TO_FLOAT(style->opacity.value) );
             } else {
-                rgba = sp_color_get_rgba32_falpha(&style->fill.value.color, SP_SCALE24_TO_FLOAT(style->fill_opacity.value));
+                rgba = style->fill.value.color.toRGBA32( SP_SCALE24_TO_FLOAT(style->fill_opacity.value) );
             }
             nr_blit_pixblock_mask_rgba32(pb, &m, rgba);
             pb->empty = FALSE;
-        } else if (style->fill.type == SP_PAINT_TYPE_PAINTSERVER) {
-            if (ggroup->fill_painter) {
-                nr_arena_render_paintserver_fill(pb, area, ggroup->fill_painter, SP_SCALE24_TO_FLOAT(style->fill_opacity.value), &m);
-            }
         }
 
         nr_pixblock_release(&m);
     }
 
     /* Stroke */
-    if (style->stroke.type != SP_PAINT_TYPE_NONE && !(item->arena->rendermode == RENDERMODE_OUTLINE)) {
+    if (!style->stroke.isNone() && !(item->arena->rendermode == Inkscape::RENDERMODE_OUTLINE)) {
         NRPixBlock m;
         guint32 rgba;
         nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
 
-        // if memory allocation failed, abort 
+        // if memory allocation failed, abort
         if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
             nr_pixblock_release (&m);
             return (item->state);
         }
 
-        m.visible_area = pb->visible_area; 
+        m.visible_area = pb->visible_area;
         /* Render children stroke mask */
         for (child = group->children; child != NULL; child = child->next) {
             ret = nr_arena_glyphs_stroke_mask(NR_ARENA_GLYPHS(child), area, &m);
@@ -542,26 +543,21 @@ nr_arena_glyphs_group_render(NRArenaItem *item, NRRectL *area, NRPixBlock *pb, u
             }
         }
         /* Composite into buffer */
-        switch (style->stroke.type) {
-            case SP_PAINT_TYPE_COLOR:
-                if ( item->render_opacity ) {
-                    rgba = sp_color_get_rgba32_falpha(&style->stroke.value.color,
-                                                      SP_SCALE24_TO_FLOAT(style->stroke_opacity.value) *
-                                                      SP_SCALE24_TO_FLOAT(style->opacity.value));
-                } else {
-                    rgba = sp_color_get_rgba32_falpha(&style->stroke.value.color,
-                                                      SP_SCALE24_TO_FLOAT(style->stroke_opacity.value));
-                }
-                nr_blit_pixblock_mask_rgba32(pb, &m, rgba);
-                pb->empty = FALSE;
-                break;
-            case SP_PAINT_TYPE_PAINTSERVER:
-                if (ggroup->stroke_painter) {
-                    nr_arena_render_paintserver_fill(pb, area, ggroup->stroke_painter, SP_SCALE24_TO_FLOAT(style->stroke_opacity.value), &m);
-                }
-                break;
-            default:
-                break;
+        if (style->stroke.isPaintserver()) {
+            if (ggroup->stroke_painter) {
+                nr_arena_render_paintserver_fill(pb, area, ggroup->stroke_painter, SP_SCALE24_TO_FLOAT(style->stroke_opacity.value), &m);
+            }
+        } else if (style->stroke.isColor()) {
+            if ( item->render_opacity ) {
+                rgba = style->stroke.value.color.toRGBA32( SP_SCALE24_TO_FLOAT(style->stroke_opacity.value) *
+                                                           SP_SCALE24_TO_FLOAT(style->opacity.value) );
+            } else {
+                rgba = style->stroke.value.color.toRGBA32( SP_SCALE24_TO_FLOAT(style->stroke_opacity.value) );
+            }
+            nr_blit_pixblock_mask_rgba32(pb, &m, rgba);
+            pb->empty = FALSE;
+        } else {
+            // nothing
         }
         nr_pixblock_release(&m);
     }
@@ -611,7 +607,7 @@ nr_arena_glyphs_group_clear(NRArenaGlyphsGroup *sg)
 }
 
 void
-nr_arena_glyphs_group_add_component(NRArenaGlyphsGroup *sg, font_instance *font, int glyph, NRMatrix const *transform)
+nr_arena_glyphs_group_add_component(NRArenaGlyphsGroup *sg, font_instance *font, int glyph, NR::Matrix const *transform)
 {
     NRArenaGroup *group;
     NRBPath bpath;