text drawing artifact
Carl Worth
cworth at cworth.org
Wed May 29 21:26:51 UTC 2024
Steven,
Can you describe what you're seeing and what you're expecting to see?
Or show images of the good and bad results?
I compiled and ran your code, with and without the change you suggested,
and saw the same result in both cases, (or else I missed something).
So I'm not clear if I'm just not getting the same behavior you are, or
if I'm not seeing something.
-Carl
On Wed, May 29 2024, Steven J. Abner wrote:
> I've gotten frustrated and need help. I've tried different combos of
> matrix, cairo_t, clip
> and saves. I just can't make this work: draw text then drawing. Can
> switch order:
> drawing then draw text to make it work, but in creating objects parsing
> order for draw
> events is not really an option.
> What am I doing wrong?????
>
> demo: compile
> gcc -x c -std=c99 -pipe -fno-builtin -fmessage-length=0 -g `pkg-config
> --cflags gtk+-3.0` -o a artifact.c `pkg-config --libs gtk+-3.0`
> demo: run
> ./a
> then try altering draw() so draw_wmgr() runs before text drawing code
> to see difference.
> Steve
>
> //gcc -x c -std=c99 -pipe -fno-builtin -fmessage-length=0 -g `pkg-config --cflags gtk+-3.0` -o a artifact.c `pkg-config --libs gtk+-3.0`
> #ifndef M_PI
> #define M_PI 3.14159265358979323846
> #endif
> #include <gtk/gtk.h>
>
> typedef struct _PhxObject PhxObject;
> typedef struct { int x, y, w, h; } PhxRectangle;
> struct _PhxObject {
> PhxRectangle mete_box; /* allocate box */
> PhxRectangle draw_box; /* clip box */
> };
>
> static void
> draw_wmgr(PhxObject *b, cairo_t *cr) {
>
> cairo_save(cr);
>
> double bottom = b->mete_box.y + b->draw_box.y + b->draw_box.h;
> double top = b->mete_box.y + b->draw_box.y;
> double radius = b->draw_box.h / 2;
> double xc = b->mete_box.x + b->draw_box.x + (b->draw_box.w / 2);
> double yc = b->mete_box.y + b->draw_box.y + (b->draw_box.h / 2);
>
> cairo_arc(cr, xc, yc, radius, 0, M_PI * 2);
> cairo_clip_preserve(cr);
>
> cairo_matrix_t matrix;
> cairo_pattern_t *r1;
> cairo_get_matrix(cr, &matrix);
> // fill in circle
> cairo_translate(cr, xc, bottom + 6.5);
> cairo_scale(cr, 1, 0.7);
> cairo_translate(cr, -xc, -(bottom + 6.5));
> r1 = cairo_pattern_create_radial(xc, yc, 6,
> xc, yc, (double)(b->draw_box.h + 8.5));
> cairo_pattern_add_color_stop_rgba(r1, 0, .7, .7, 1, 1);
> cairo_pattern_add_color_stop_rgba(r1, 1, 0, 0, .4, 1);
> cairo_set_source(cr, r1);
> cairo_fill_preserve(cr);
> cairo_pattern_destroy(r1);
>
> cairo_set_matrix(cr, &matrix);
>
> r1 = cairo_pattern_create_radial(xc - 0.7, top + 3.5, .4,
> xc - 0.7, top + 3.5, 8);
> cairo_pattern_add_color_stop_rgba(r1, 0, .8, .8, 1, 1);
> cairo_pattern_add_color_stop_rgba(r1, 1, .4, .4, 1, .05);
> cairo_set_source(cr, r1);
> cairo_fill_preserve(cr);
> cairo_pattern_destroy(r1);
>
> cairo_set_source_rgba(cr, 0, 0, 0, 1);
> cairo_set_line_width(cr, .5);
> cairo_stroke(cr);
>
> cairo_restore(cr);
> }
>
> static _Bool
> draw(void *widget, cairo_t *cr) {
>
> cairo_save(cr);
> cairo_select_font_face(cr, "DejaVu Sans", 0, 0);
> cairo_set_font_size(cr, 9.5);
> cairo_rectangle(cr, 108, 7, 8, 12);
> cairo_clip(cr);
>
> cairo_set_source_rgba(cr, 0, 0, 0, 1);
> cairo_move_to(cr, 108, 7 + 9);
> cairo_show_text(cr, "O");
> cairo_restore(cr);
>
> PhxObject b = { {554, 5, 14, 14}, {0, 0, 14, 14} };
> draw_wmgr(&b, cr);
>
> }
>
> int
> main(int argc, char *argv[]) {
>
> gtk_init(&argc, &argv); // initialize Gdk
>
> int window_width = 576;
> int window_height = 200;
>
> GtkWidget *main_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
> GtkWindow *window = GTK_WINDOW(main_window);
> gtk_window_set_default_size(window, window_width, window_height);
> gtk_window_set_decorated(GTK_WINDOW(window), FALSE);
> gtk_widget_realize(main_window);
>
> g_signal_connect(G_OBJECT(main_window), "delete-event",
> G_CALLBACK(gtk_main_quit), NULL);
>
> GtkWidget *box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
> gtk_container_add(GTK_CONTAINER(main_window), box);
>
> GtkWidget *da = gtk_drawing_area_new();
> gtk_box_pack_start((GtkBox*)box, da, TRUE, TRUE, 0);
> gtk_widget_realize(da);
>
> g_signal_connect(G_OBJECT(da), "draw", G_CALLBACK(draw), NULL);
>
> gtk_widget_show_all(main_window);
> gtk_main();
> return EXIT_SUCCESS;
> }
More information about the cairo
mailing list