From a249cbd90ab37fdc02a463b45e0bba0dcf3cf483 Mon Sep 17 00:00:00 2001 From: su_v Date: Sun, 23 Sep 2012 18:55:14 +0200 Subject: Fixes bug #1029584: support custom dasharrays in 'Fill and Stroke > Stroke style' (bzr r11668.1.2) --- src/widgets/dash-selector.cpp | 86 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 75 insertions(+), 11 deletions(-) (limited to 'src/widgets/dash-selector.cpp') diff --git a/src/widgets/dash-selector.cpp b/src/widgets/dash-selector.cpp index 901d3b68c..51483a9c4 100644 --- a/src/widgets/dash-selector.cpp +++ b/src/widgets/dash-selector.cpp @@ -39,6 +39,7 @@ static double dash_4_1[] = {4.0, 1.0, -1.0}; static double dash_1_2[] = {1.0, 2.0, -1.0}; static double dash_1_4[] = {1.0, 4.0, -1.0}; +#define bd_len 7 // must correspond to the number of entries in the next line static double *builtin_dashes[] = {dash_0, dash_1_1, dash_2_1, dash_4_1, dash_1_2, dash_1_4, NULL}; static double **dashes = NULL; @@ -79,12 +80,18 @@ SPDashSelector::SPDashSelector() this->pack_start(*sb, false, false, 0); - for (int i = 0; dashes[i]; i++) { + int np=0; + while (dashes[np]){ np++;} + for (int i = 0; iappend()); row[dash_columns.dash] = dashes[i]; row[dash_columns.pixbuf] = Glib::wrap(sp_dash_to_pixbuf(dashes[i])); } + // add the custom one + Gtk::TreeModel::Row row = *(dash_store->append()); + row[dash_columns.dash] = dashes[np-1]; + row[dash_columns.pixbuf] = Glib::wrap(sp_text_to_pixbuf((char *)"Custom")); this->set_data("pattern", dashes[0]); } @@ -109,10 +116,10 @@ void SPDashSelector::init_dashes() { Inkscape::Preferences *prefs = Inkscape::Preferences::get(); std::vector dash_prefs = prefs->getAllDirs(_prefs_path); + int pos = 0; if (!dash_prefs.empty()) { - int pos = 0; SPStyle *style = sp_style_new (NULL); - dashes = g_new (double *, dash_prefs.size() + 1); + dashes = g_new (double *, dash_prefs.size() + 2); // +1 for custom slot, +1 for terminator slot for (std::vector::iterator i = dash_prefs.begin(); i != dash_prefs.end(); ++i) { sp_style_read_from_prefs(style, *i); @@ -130,23 +137,36 @@ void SPDashSelector::init_dashes() { } pos += 1; } - dashes[pos] = NULL; - } else { - dashes = builtin_dashes; + } else { // This code may never execute - a new preferences.xml is created for a new user. Maybe if the user deletes dashes from preferences.xml? + dashes = g_new (double *, bd_len + 2); // +1 for custom slot, +1 for terminator slot + int i; + for(i=0;i 0) { double delta = 0.0; for (int i = 0; i < ndash; i++) delta += dash[i]; delta /= 1000.0; - for (int i = 0; dashes[i]; i++) { + for (int i = 0; dashes[i]; i++,count++) { double *pattern = dashes[i]; int np = 0; while (pattern[np] >= 0.0) @@ -154,6 +174,7 @@ void SPDashSelector::set_dash (int ndash, double *dash, double o) if (np == ndash) { int j; for (j = 0; j < ndash; j++) { + if (!Geom::are_near(dash[j], pattern[j], delta)) break; } @@ -164,10 +185,27 @@ void SPDashSelector::set_dash (int ndash, double *dash, double o) } } } + else if(ndash==0) { + pos = 0; + } - this->set_data("pattern", dashes[pos]); - this->dash_combo.set_active(pos); - this->offset->set_value(o); + if(pos>=0){ + this->set_data("pattern", dashes[pos]); + this->dash_combo.set_active(pos); + this->offset->set_value(o); + } + else { // Hit a custom pattern in the SVG, write it into the combobox. + count--; // the one slot for custom patterns + double *d = dashes[count]; + int i=0; + for(i=0;i< (ndash > 15 ? 15 : ndash) ;i++) { + d[i]=dash[i]; + } // store the custom pattern + d[ndash]=-1.0; //terminate it + this->set_data("pattern", dashes[count]); + this->dash_combo.set_active(count); + this->offset->set_value(o); // what does this do???? + } } void SPDashSelector::get_dash(int *ndash, double **dash, double *off) @@ -227,6 +265,32 @@ GdkPixbuf* SPDashSelector::sp_dash_to_pixbuf(double *pattern) { return pixbuf; } +/** + * Fill a pixbuf with a text label using standard cairo drawing + */ +GdkPixbuf* SPDashSelector::sp_text_to_pixbuf(char *text) { + + cairo_surface_t *s = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, preview_width, preview_height); + cairo_t *ct = cairo_create(s); + + cairo_select_font_face (ct, "Sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL); + cairo_set_font_size (ct, 12.0); + cairo_set_source_rgb (ct, 0.0, 0.0, 0.0); + cairo_move_to (ct, 16.0, 13.0); + cairo_show_text (ct, text); + + cairo_stroke (ct); + + cairo_destroy(ct); + cairo_surface_flush(s); + + GdkPixbuf* pixbuf = gdk_pixbuf_new_from_data( cairo_image_surface_get_data(s), + GDK_COLORSPACE_RGB, TRUE, 8, + preview_width, preview_height, cairo_image_surface_get_stride(s), + ink_cairo_pixbuf_cleanup, s); + convert_pixbuf_argb32_to_normal(pixbuf); + return pixbuf; +} void SPDashSelector::on_selection () { -- cgit v1.2.3 From 0697088a2e3fbb3f7777db721e8c5e4661311dbe Mon Sep 17 00:00:00 2001 From: Krzysztof Kosi??ski Date: Fri, 13 Sep 2013 23:14:45 +0200 Subject: Improve the functions which create GdkPixbuf from Cairo surface and vice versa. Simplifies some code. Also introduce proper refcounting into svg_preview_cache.cpp and fix its users. (bzr r12512) --- src/widgets/dash-selector.cpp | 78 +++++++++++++++++++------------------------ 1 file changed, 35 insertions(+), 43 deletions(-) (limited to 'src/widgets/dash-selector.cpp') diff --git a/src/widgets/dash-selector.cpp b/src/widgets/dash-selector.cpp index 51483a9c4..afc81e574 100644 --- a/src/widgets/dash-selector.cpp +++ b/src/widgets/dash-selector.cpp @@ -238,58 +238,50 @@ void SPDashSelector::get_dash(int *ndash, double **dash, double *off) /** * Fill a pixbuf with the dash pattern using standard cairo drawing */ -GdkPixbuf* SPDashSelector::sp_dash_to_pixbuf(double *pattern) { - - int n_dashes; - for (n_dashes = 0; pattern[n_dashes] >= 0.0; n_dashes ++) ; - - cairo_surface_t *s = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, preview_width, preview_height); - cairo_t *ct = cairo_create(s); - - cairo_set_line_width (ct, preview_lineheight); - cairo_scale (ct, preview_lineheight, 1); - //cairo_set_source_rgb (ct, 0, 0, 0); - cairo_move_to (ct, 0, preview_height/2); - cairo_line_to (ct, preview_width, preview_height/2); - cairo_set_dash(ct, pattern, n_dashes, 0); - cairo_stroke (ct); - - cairo_destroy(ct); - cairo_surface_flush(s); - - GdkPixbuf* pixbuf = gdk_pixbuf_new_from_data( cairo_image_surface_get_data(s), - GDK_COLORSPACE_RGB, TRUE, 8, - preview_width, preview_height, cairo_image_surface_get_stride(s), - ink_cairo_pixbuf_cleanup, s); - convert_pixbuf_argb32_to_normal(pixbuf); - return pixbuf; +GdkPixbuf* SPDashSelector::sp_dash_to_pixbuf(double *pattern) +{ + int n_dashes; + for (n_dashes = 0; pattern[n_dashes] >= 0.0; n_dashes ++) ; + + cairo_surface_t *s = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, preview_width, preview_height); + cairo_t *ct = cairo_create(s); + + cairo_set_line_width (ct, preview_lineheight); + cairo_scale (ct, preview_lineheight, 1); + //cairo_set_source_rgb (ct, 0, 0, 0); + cairo_move_to (ct, 0, preview_height/2); + cairo_line_to (ct, preview_width, preview_height/2); + cairo_set_dash(ct, pattern, n_dashes, 0); + cairo_stroke (ct); + + cairo_destroy(ct); + cairo_surface_flush(s); + + GdkPixbuf* pixbuf = ink_pixbuf_create_from_cairo_surface(s); + return pixbuf; } /** * Fill a pixbuf with a text label using standard cairo drawing */ -GdkPixbuf* SPDashSelector::sp_text_to_pixbuf(char *text) { - - cairo_surface_t *s = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, preview_width, preview_height); - cairo_t *ct = cairo_create(s); +GdkPixbuf* SPDashSelector::sp_text_to_pixbuf(char *text) +{ + cairo_surface_t *s = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, preview_width, preview_height); + cairo_t *ct = cairo_create(s); - cairo_select_font_face (ct, "Sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL); - cairo_set_font_size (ct, 12.0); - cairo_set_source_rgb (ct, 0.0, 0.0, 0.0); - cairo_move_to (ct, 16.0, 13.0); - cairo_show_text (ct, text); + cairo_select_font_face (ct, "Sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL); + cairo_set_font_size (ct, 12.0); + cairo_set_source_rgb (ct, 0.0, 0.0, 0.0); + cairo_move_to (ct, 16.0, 13.0); + cairo_show_text (ct, text); - cairo_stroke (ct); + cairo_stroke (ct); - cairo_destroy(ct); - cairo_surface_flush(s); + cairo_destroy(ct); + cairo_surface_flush(s); - GdkPixbuf* pixbuf = gdk_pixbuf_new_from_data( cairo_image_surface_get_data(s), - GDK_COLORSPACE_RGB, TRUE, 8, - preview_width, preview_height, cairo_image_surface_get_stride(s), - ink_cairo_pixbuf_cleanup, s); - convert_pixbuf_argb32_to_normal(pixbuf); - return pixbuf; + GdkPixbuf* pixbuf = ink_pixbuf_create_from_cairo_surface(s); + return pixbuf; } void SPDashSelector::on_selection () -- cgit v1.2.3 From 1d31728fb7399f48d272560a290dc990b75a197e Mon Sep 17 00:00:00 2001 From: Tavmjong Bah Date: Tue, 11 Mar 2014 15:52:08 +0100 Subject: Change stroke-dasharray and stroke-dashoffset handling to match other properties. Split style.h into more manageable size files. (bzr r13135) --- src/widgets/dash-selector.cpp | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'src/widgets/dash-selector.cpp') diff --git a/src/widgets/dash-selector.cpp b/src/widgets/dash-selector.cpp index afc81e574..fce7a9d9f 100644 --- a/src/widgets/dash-selector.cpp +++ b/src/widgets/dash-selector.cpp @@ -39,7 +39,7 @@ static double dash_4_1[] = {4.0, 1.0, -1.0}; static double dash_1_2[] = {1.0, 2.0, -1.0}; static double dash_1_4[] = {1.0, 4.0, -1.0}; -#define bd_len 7 // must correspond to the number of entries in the next line +static size_t BD_LEN = 7; // must correspond to the number of entries in the next line static double *builtin_dashes[] = {dash_0, dash_1_1, dash_2_1, dash_4_1, dash_1_2, dash_1_4, NULL}; static double **dashes = NULL; @@ -124,12 +124,12 @@ void SPDashSelector::init_dashes() { for (std::vector::iterator i = dash_prefs.begin(); i != dash_prefs.end(); ++i) { sp_style_read_from_prefs(style, *i); - if (style->stroke_dash.n_dash > 0) { - dashes[pos] = g_new (double, style->stroke_dash.n_dash + 1); + if (!style->stroke_dasharray.values.empty()) { + dashes[pos] = g_new (double, style->stroke_dasharray.values.size() + 1); double *d = dashes[pos]; - int i = 0; - for (; i < style->stroke_dash.n_dash; i++) { - d[i] = style->stroke_dash.dash[i]; + unsigned i = 0; + for (; i < style->stroke_dasharray.values.size(); i++) { + d[i] = style->stroke_dasharray.values[i]; } d[i] = -1; } else { @@ -138,12 +138,12 @@ void SPDashSelector::init_dashes() { pos += 1; } } else { // This code may never execute - a new preferences.xml is created for a new user. Maybe if the user deletes dashes from preferences.xml? - dashes = g_new (double *, bd_len + 2); // +1 for custom slot, +1 for terminator slot - int i; - for(i=0;i