From 2e10ea952a319db08edf0986dea15de42b14d92f Mon Sep 17 00:00:00 2001 From: buliabyak Date: Wed, 11 Mar 2009 17:57:56 +0000 Subject: [PATCH] move canvastext to its own file (no linking error for me anymore), add outline to fix 334758 --- src/display/Makefile_insert | 2 + src/display/canvas-bpath.cpp | 247 ---------------------------- src/display/canvas-bpath.h | 39 ----- src/display/canvas-text.cpp | 58 ++++--- src/display/canvas-text.h | 13 +- src/display/snap-indicator.cpp | 2 +- src/live_effects/parameter/text.cpp | 1 + src/live_effects/parameter/text.h | 3 +- src/lpe-tool-context.cpp | 1 + 9 files changed, 50 insertions(+), 316 deletions(-) diff --git a/src/display/Makefile_insert b/src/display/Makefile_insert index ef8310a01..ac930d799 100644 --- a/src/display/Makefile_insert +++ b/src/display/Makefile_insert @@ -31,6 +31,8 @@ ink_common_sources += \ display/canvas-temporary-item.h \ display/canvas-temporary-item-list.cpp \ display/canvas-temporary-item-list.h \ + display/canvas-text.h \ + display/canvas-text.cpp \ display/curve.cpp \ display/curve.h \ display/display-forward.h \ diff --git a/src/display/canvas-bpath.cpp b/src/display/canvas-bpath.cpp index b0ada1e68..c47806615 100644 --- a/src/display/canvas-bpath.cpp +++ b/src/display/canvas-bpath.cpp @@ -25,18 +25,6 @@ #include #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 #include #include @@ -287,241 +275,6 @@ sp_canvas_bpath_set_stroke (SPCanvasBPath *cbp, guint32 rgba, gdouble width, SPS sp_canvas_item_request_update (SP_CANVAS_ITEM (cbp)); } - - -/* - * 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) -{ - 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; -} - -static void -sp_canvastext_class_init (SPCanvasTextClass *klass) -{ - GtkObjectClass *object_class = (GtkObjectClass *) klass; - SPCanvasItemClass *item_class = (SPCanvasItemClass *) klass; - - 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); - } - 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)); - } - 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++ diff --git a/src/display/canvas-bpath.h b/src/display/canvas-bpath.h index 2001bfa7d..b97bbcc6b 100644 --- a/src/display/canvas-bpath.h +++ b/src/display/canvas-bpath.h @@ -95,45 +95,6 @@ void sp_canvas_bpath_set_bpath (SPCanvasBPath *cbp, SPCurve *curve); void 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, double dash=0, double gap=0); - -/* - * FIXME: The following code should actually be in a separate file called display/canvas-text.h. It - * temporarily had to be moved here because of linker errors. - */ - -struct SPItem; -struct SPDesktop; - -#define SP_TYPE_CANVASTEXT (sp_canvastext_get_type ()) -#define SP_CANVASTEXT(obj) (GTK_CHECK_CAST ((obj), SP_TYPE_CANVASTEXT, SPCanvasText)) -#define SP_IS_CANVASTEXT(obj) (GTK_CHECK_TYPE ((obj), SP_TYPE_CANVASTEXT)) - -struct SPCanvasText : public SPCanvasItem{ - SPItem *item; // the item to which this line belongs in some sense; may be NULL for some users - guint32 rgba; - SPDesktop *desktop; // the desktop to which this text is attached; needed for coordinate transforms (TODO: these should be eliminated) - - gchar* text; - Geom::Point s; - Geom::Matrix affine; - double fontsize; - double anchor_x; - double anchor_y; -}; -struct SPCanvasTextClass : public SPCanvasItemClass{}; - -GtkType sp_canvastext_get_type (void); - -SPCanvasItem *sp_canvastext_new(SPCanvasGroup *parent, SPDesktop *desktop, Geom::Point pos, gchar const *text); - -void sp_canvastext_set_rgba32 (SPCanvasText *ct, guint32 rgba); -void sp_canvastext_set_coords (SPCanvasText *ct, gdouble x0, gdouble y0); -void sp_canvastext_set_coords (SPCanvasText *ct, const Geom::Point start); -void sp_canvastext_set_text (SPCanvasText *ct, gchar const* new_text); -void sp_canvastext_set_number_as_text (SPCanvasText *ct, int num); -void sp_canvastext_set_fontsize (SPCanvasText *ct, double size); -void sp_canvastext_set_anchor (SPCanvasText *ct, double anchor_x, double anchor_y); - #endif /* diff --git a/src/display/canvas-text.cpp b/src/display/canvas-text.cpp index e46112a09..351aca639 100644 --- a/src/display/canvas-text.cpp +++ b/src/display/canvas-text.cpp @@ -19,6 +19,7 @@ #include "display/inkscape-cairo.h" #include #include +#include "desktop.h" #ifdef HAVE_CONFIG_H # include "config.h" @@ -72,11 +73,13 @@ sp_canvastext_class_init (SPCanvasTextClass *klass) static void sp_canvastext_init (SPCanvasText *canvastext) { - canvastext->rgba = 0x0000ff7f; + canvastext->rgba = 0x33337fff; + canvastext->rgba_stroke = 0xffffffff; 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; } @@ -95,7 +98,7 @@ sp_canvastext_destroy (GtkObject *object) } // FIXME: remove this as soon as we know how to correctly determine the text extent -static const double arbitrary_factor = 0.7; +static const double arbitrary_factor = 0.8; // 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; @@ -109,9 +112,6 @@ sp_canvastext_render (SPCanvasItem *item, SPCanvasBuf *buf) 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; @@ -120,8 +120,13 @@ sp_canvastext_render (SPCanvasItem *item, SPCanvasBuf *buf) 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_text_path(buf->ct, cl->text); + + cairo_set_source_rgba(buf->ct, SP_RGBA32_B_F(cl->rgba_stroke), SP_RGBA32_G_F(cl->rgba_stroke), SP_RGBA32_R_F(cl->rgba_stroke), SP_RGBA32_A_F(cl->rgba_stroke)); + cairo_set_line_width (buf->ct, 2.0); + cairo_stroke_preserve(buf->ct); + cairo_set_source_rgba(buf->ct, SP_RGBA32_B_F(cl->rgba), SP_RGBA32_G_F(cl->rgba), SP_RGBA32_R_F(cl->rgba), SP_RGBA32_A_F(cl->rgba)); + cairo_fill(buf->ct); cairo_new_path(buf->ct); } @@ -167,12 +172,14 @@ sp_canvastext_update (SPCanvasItem *item, Geom::Matrix const &affine, unsigned i } SPCanvasItem * -sp_canvastext_new(SPCanvasGroup *parent, Geom::Point pos, gchar const *new_text) +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); @@ -184,18 +191,17 @@ sp_canvastext_new(SPCanvasGroup *parent, Geom::Point pos, gchar const *new_text) void -sp_canvastext_set_rgba32 (SPCanvasText *ct, guint32 rgba) +sp_canvastext_set_rgba32 (SPCanvasText *ct, guint32 rgba, guint32 rgba_stroke) { g_return_if_fail (ct != NULL); g_return_if_fail (SP_IS_CANVASTEXT (ct)); - if (rgba != ct->rgba) { - SPCanvasItem *item; + if (rgba != ct->rgba || rgba_stroke != ct->rgba_stroke) { 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); + ct->rgba_stroke = rgba_stroke; + SPCanvasItem *item = SP_CANVAS_ITEM (ct); + sp_canvas_item_request_update (SP_CANVAS_ITEM (ct)); } - sp_canvas_item_request_update (SP_CANVAS_ITEM (ct)); } #define EPSILON 1e-6 @@ -204,25 +210,26 @@ sp_canvastext_set_rgba32 (SPCanvasText *ct, guint32 rgba) void sp_canvastext_set_coords (SPCanvasText *ct, gdouble x0, gdouble y0) { - g_return_if_fail (ct != NULL); - g_return_if_fail (SP_IS_CANVASTEXT (ct)); - - if (DIFFER (x0, ct->s[Geom::X]) || DIFFER (y0, ct->s[Geom::Y])) { - ct->s[Geom::X] = x0; - ct->s[Geom::Y] = y0; - sp_canvas_item_request_update (SP_CANVAS_ITEM (ct)); - } - sp_canvas_item_request_update (SP_CANVAS_ITEM (ct)); + sp_canvastext_set_coords(ct, Geom::Point(x0, y0)); } void sp_canvastext_set_coords (SPCanvasText *ct, const Geom::Point start) { - sp_canvastext_set_coords(ct, start[0], start[1]); + 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)); + } } void -sp_canvastext_set_text (SPCanvasText *ct, gchar const* new_text) +sp_canvastext_set_text (SPCanvasText *ct, gchar const * new_text) { g_free (ct->text); ct->text = g_strdup(new_text); @@ -250,6 +257,7 @@ sp_canvastext_set_anchor (SPCanvasText *ct, double anchor_x, double anchor_y) ct->anchor_y = anchor_y; } + /* Local Variables: mode:c++ diff --git a/src/display/canvas-text.h b/src/display/canvas-text.h index 0e4724d92..e5d634985 100644 --- a/src/display/canvas-text.h +++ b/src/display/canvas-text.h @@ -14,9 +14,13 @@ * Released under GNU GPL, read the file 'COPYING' for more information */ -#include "sp-canvas.h" +/* + * FIXME: The following code should actually be in a separate file called display/canvas-text.h. It + * temporarily had to be moved here because of linker errors. + */ struct SPItem; +struct SPDesktop; #define SP_TYPE_CANVASTEXT (sp_canvastext_get_type ()) #define SP_CANVASTEXT(obj) (GTK_CHECK_CAST ((obj), SP_TYPE_CANVASTEXT, SPCanvasText)) @@ -25,6 +29,8 @@ struct SPItem; struct SPCanvasText : public SPCanvasItem{ SPItem *item; // the item to which this line belongs in some sense; may be NULL for some users guint32 rgba; + guint32 rgba_stroke; + SPDesktop *desktop; // the desktop to which this text is attached; needed for coordinate transforms (TODO: these should be eliminated) gchar* text; Geom::Point s; @@ -37,9 +43,9 @@ struct SPCanvasTextClass : public SPCanvasItemClass{}; GtkType sp_canvastext_get_type (void); -SPCanvasItem *sp_canvastext_new(SPCanvasGroup *parent, Geom::Point pos, gchar const *text); +SPCanvasItem *sp_canvastext_new(SPCanvasGroup *parent, SPDesktop *desktop, Geom::Point pos, gchar const *text); -void sp_canvastext_set_rgba32 (SPCanvasText *ct, guint32 rgba); +void sp_canvastext_set_rgba32 (SPCanvasText *ct, guint32 rgba, guint32 rgba_stroke); void sp_canvastext_set_coords (SPCanvasText *ct, gdouble x0, gdouble y0); void sp_canvastext_set_coords (SPCanvasText *ct, const Geom::Point start); void sp_canvastext_set_text (SPCanvasText *ct, gchar const* new_text); @@ -49,6 +55,7 @@ void sp_canvastext_set_anchor (SPCanvasText *ct, double anchor_x, double anchor_ #endif + /* Local Variables: mode:c++ diff --git a/src/display/snap-indicator.cpp b/src/display/snap-indicator.cpp index a5d7b77f0..901bee6ad 100644 --- a/src/display/snap-indicator.cpp +++ b/src/display/snap-indicator.cpp @@ -16,7 +16,7 @@ #include "desktop.h" #include "desktop-handles.h" #include "display/sodipodi-ctrl.h" -#include "display/canvas-bpath.h" +#include "display/canvas-text.h" #include "knot.h" #include "preferences.h" #include diff --git a/src/live_effects/parameter/text.cpp b/src/live_effects/parameter/text.cpp index 5ba4bcfc7..0c0c3d69c 100644 --- a/src/live_effects/parameter/text.cpp +++ b/src/live_effects/parameter/text.cpp @@ -19,6 +19,7 @@ #include "ui/widget/registered-widget.h" #include "inkscape.h" #include "verbs.h" +#include "display/canvas-text.h" #include <2geom/sbasis-geometric.h> namespace Inkscape { diff --git a/src/live_effects/parameter/text.h b/src/live_effects/parameter/text.h index 7ce525542..8539a8046 100644 --- a/src/live_effects/parameter/text.h +++ b/src/live_effects/parameter/text.h @@ -15,10 +15,11 @@ #include -//#include "display/canvas-text.h" #include "display/canvas-bpath.h" #include "live_effects/parameter/parameter.h" +struct SPCanvasText; + namespace Inkscape { namespace LivePathEffect { diff --git a/src/lpe-tool-context.cpp b/src/lpe-tool-context.cpp index 30248edd0..d3fb332e5 100644 --- a/src/lpe-tool-context.cpp +++ b/src/lpe-tool-context.cpp @@ -34,6 +34,7 @@ #include "document.h" #include "display/curve.h" #include "display/canvas-bpath.h" +#include "display/canvas-text.h" #include "message-stack.h" #include "sp-path.h" #include "helper/units.h" -- 2.30.2