簡體   English   中英

使用運算符而不刪除開羅的背景

[英]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矩形內的部分文本,而其余的背景保持不變)。 取而代之的是,整個背景(矩形區域除外)均被刪除,並且圖片變為透明。

將黑色背景視為任意任意復雜的圖形。 有沒有一種方法可以根據需要使用該操作(從路徑中提取一個范圍),而不刪除背景的任何部分?

有沒有更好的方法來剪切路徑,因此只繪制提供的矩形內的零件?

開羅如何知道您的“任意復雜圖形”(要保留的部分)和藍色文本(要部分擦除的部分)是哪一部分?

這樣的事情怎么樣? (未經測試!):

#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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM