清除GTK窗口中的cairo文本

3

我遇到了一个cairo文本方面的问题。 我在gtk_window中写了几行:

cr = gdk_cairo_create(window->window);
        cairo_set_source_rgb(cr, 255, 255, 255);
        cairo_select_font_face(cr, "Sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
        cairo_set_font_size(cr, 14.0);

        cairo_move_to(cr, 90.0, 85.0);
        cairo_show_text(cr, "Terror");
        cairo_set_font_size(cr, 12.0);
        cairo_set_source_rgb(cr, 30, 254, 145);
        cairo_move_to(cr, 90.0, 105.0);
        cairo_show_text(cr, "Underdogs");
        cairo_move_to(cr, 90.0, 120.0);
        cairo_show_text(cr, "Disziplin");
        cairo_destroy(cr);

问题在于这段文本应该是动态的,但如果我调用写入文本的函数超过一次,行就会重叠。
是否有任何方法可以清除之前的文本?
谢谢!
2个回答

3

来自开罗常见问题解答

If you want to clear your surface to a uniform, opaque color then it is quite straightforward:

/* Set surface to opaque color (r, g, b) */
cairo_set_source_rgb (cr, r, g, b);
cairo_paint (cr);

However, what if you want to clear the surface to something other than an opaque color. Simply modifying the above code to use cairo_set_source_rgba (cr, 0, 0, 0, 0); will not work since Cairo uses the OVER compositing operator by default, and blending something entirely transparent OVER something else has no effect at all. Instead, you can use the SOURCE operator which copies both color and alpha values directly from the source to the destination instead of blending:

/* Set surface to translucent color (r, g, b, a) */
cairo_set_source_rgba (cr, r, g, b, a);
cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
cairo_paint (cr);

Of course, you won't want to forget to set the default CAIRO_OPERATOR_OVER again when you're finished. And the most convenient habit for doing that is to just use cairo_save/cairo_restore around the whole block:

/* Set surface to translucent color (r, g, b, a) without disturbing graphics state. */
cairo_save (cr);
cairo_set_source_rgba (cr, r, g, b, a);
cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
cairo_paint (cr);
cairo_restore (cr);

Finally, to clear a surface to all transparent, one could simply use CAIRO_OPERATOR_CLEAR instead of CAIRO_OPERATOR_SOURCE, in which case the call to cairo_set_source_rgba would not be needed at all, (the CLEAR operator always sets the destination to 0 in every channel regardless of what the source pattern contains). But the above approach with CAIRO_OPERATOR_SOURCE is a more general way to clear the surface since it allows for "clearing" to a translucent color such as 50% red rather than just clearing to entirely transparent.


2

您需要将文本与背景颜色一起覆盖 :)


但这很丑。我想要清除“表面”。用背景颜色覆盖文本意味着每次想要写另一个文本都需要重新加载我的主题。 - Leber

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接