在 Cairo 中使用运算符而不移除背景
Use operator without removing the background in Cairo
我只想绘制开罗路径的有限部分,尤其是(但不限于)文本。所以我查看了运算符并尝试了 DEST_IN 运算符。
考虑以下示例代码
#include <cairo/cairo.h>
int main (int argc, char *argv[])
{
cairo_surface_t *surface =
cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 300, 300);
cairo_t *cr = cairo_create (surface);
//black background
cairo_set_source_rgb(cr, 0, 0, 0);
cairo_paint(cr);
//blue text
cairo_set_source_rgb(cr, 0, 0, 1);
cairo_set_font_size(cr, 50);
cairo_move_to(cr, 75, 160);
cairo_text_path(cr, "foobar");
cairo_fill(cr);
//this should remove all parts of the blue text that
//is not in the following rectangle
cairo_set_operator(cr, CAIRO_OPERATOR_DEST_IN);
cairo_rectangle(cr, 125, 125, 50, 50);
cairo_fill(cr);
cairo_destroy (cr);
cairo_surface_write_to_png (surface, "output.png");
cairo_surface_destroy (surface);
return 0;
}
这是输出的样子:
运算符有效,但未达到预期效果(即:仅显示绘制的 50x50 矩形内的文本部分,但背景的其余部分未触及)。相反,整个背景(矩形区域除外)都被移除,图片变得透明。
将黑色背景视为任意复杂的绘图。有没有办法根据需要使用操作(从路径中提取范围),而不删除背景的任何部分?
有没有更好的方法来切割路径,以便只绘制提供的矩形内的部分?
cairo 怎么知道你的"arbitrary complex drawing"(你想保留的)和你的蓝色文本(你想部分擦除的)?
这样的事情怎么样? (未经测试!):
#include <cairo/cairo.h>
int main (int argc, char *argv[])
{
cairo_surface_t *surface =
cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 300, 300);
cairo_t *cr = cairo_create (surface);
//black background
cairo_set_source_rgb(cr, 0, 0, 0);
cairo_paint(cr);
// Redirect drawing to a temporary surface
cairo_push_group(cr);
//blue text
cairo_set_source_rgb(cr, 0, 0, 1);
cairo_set_font_size(cr, 50);
cairo_move_to(cr, 75, 160);
cairo_text_path(cr, "foobar");
cairo_fill(cr);
// Draw part of the blue text
cairo_pop_group_to_source(cr);
cairo_rectangle(cr, 125, 125, 50, 50);
cairo_fill(cr);
cairo_destroy (cr);
cairo_surface_write_to_png (surface, "output.png");
cairo_surface_destroy (surface);
return 0;
}
我只想绘制开罗路径的有限部分,尤其是(但不限于)文本。所以我查看了运算符并尝试了 DEST_IN 运算符。
考虑以下示例代码
#include <cairo/cairo.h>
int main (int argc, char *argv[])
{
cairo_surface_t *surface =
cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 300, 300);
cairo_t *cr = cairo_create (surface);
//black background
cairo_set_source_rgb(cr, 0, 0, 0);
cairo_paint(cr);
//blue text
cairo_set_source_rgb(cr, 0, 0, 1);
cairo_set_font_size(cr, 50);
cairo_move_to(cr, 75, 160);
cairo_text_path(cr, "foobar");
cairo_fill(cr);
//this should remove all parts of the blue text that
//is not in the following rectangle
cairo_set_operator(cr, CAIRO_OPERATOR_DEST_IN);
cairo_rectangle(cr, 125, 125, 50, 50);
cairo_fill(cr);
cairo_destroy (cr);
cairo_surface_write_to_png (surface, "output.png");
cairo_surface_destroy (surface);
return 0;
}
这是输出的样子:
运算符有效,但未达到预期效果(即:仅显示绘制的 50x50 矩形内的文本部分,但背景的其余部分未触及)。相反,整个背景(矩形区域除外)都被移除,图片变得透明。
将黑色背景视为任意复杂的绘图。有没有办法根据需要使用操作(从路径中提取范围),而不删除背景的任何部分?
有没有更好的方法来切割路径,以便只绘制提供的矩形内的部分?
cairo 怎么知道你的"arbitrary complex drawing"(你想保留的)和你的蓝色文本(你想部分擦除的)?
这样的事情怎么样? (未经测试!):
#include <cairo/cairo.h>
int main (int argc, char *argv[])
{
cairo_surface_t *surface =
cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 300, 300);
cairo_t *cr = cairo_create (surface);
//black background
cairo_set_source_rgb(cr, 0, 0, 0);
cairo_paint(cr);
// Redirect drawing to a temporary surface
cairo_push_group(cr);
//blue text
cairo_set_source_rgb(cr, 0, 0, 1);
cairo_set_font_size(cr, 50);
cairo_move_to(cr, 75, 160);
cairo_text_path(cr, "foobar");
cairo_fill(cr);
// Draw part of the blue text
cairo_pop_group_to_source(cr);
cairo_rectangle(cr, 125, 125, 50, 50);
cairo_fill(cr);
cairo_destroy (cr);
cairo_surface_write_to_png (surface, "output.png");
cairo_surface_destroy (surface);
return 0;
}