开罗的空输出 png 文件

Empty output png file form Cairo

我已经为 C 渲染了 hello 示例。我的意思是,使用 Cairo 的最小 C 程序显示 Here。 但是,当我尝试渲染时:

#include <cairo.h>

int main(void){
    /* Where we gonna draw. The image to print. */
    cairo_surface_t *surface;
    /* The context. The printed layer by a surface */
    cairo_t *cr;

    /* The format (in this case ARGB), width and height of surface */
    surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 120, 120);
    /* The context behind of surface */
    cr = cairo_create(surface);

    /* void cairo_set_source_rgb(cairo_t *cr, double red, double green, double blue); */
    cairo_set_source_rgb(cr, 0, 0, 0);
    /* void cairo_move_to(cairo_t *cr, double x, double y); */
    /* After this call the current point will be (x,y). */
    cairo_move_to (cr, 0, 0);
    /* void cairo_line_to(cairo_t *cr, double x, double y); */
    /* Adds a line to the path from the current position (x,y). After this call the current point will be (x,y). */
    /* Must be a current point, otherwise the beahavior gonna be like move_to */
    cairo_line_to (cr, 1, 1);
    cairo_move_to (cr, 1, 0);
    cairo_line_to (cr, 0, 1);
    /* void cairo_set_line_width(cairo_t *cr, double width); */
    /* Sets the current line width within the cairo context. The line width value specifies the diameter of a pen is circular */
    cairo_set_line_width (cr, 0.2);
    /* void cairo_stroke(cairo_t *cr); */
    /* A drawing operator that strokes the current path according to the current line width, line join, line cap, and dash settings. */
    cairo_stroke (cr);

    /* void cairo_rectangle(cairo_t *cr, double x, double y, double width, double height); */
    /* Adds a closed sub-path rectangle of the given size to the current path at position (x,y) in user-space coordinates. */
    /* This function is logically equivalent to:
     * cairo_move_to (cr, x, y);
     * cairo_rel_line_to (cr, width, 0);
     * cairo_rel_line_to (cr, 0, height);
     * cairo_rel_line_to (cr, -width, 0);
     * cairo_close_path (cr);
     * *************************************/
    cairo_rectangle (cr, 0, 0, 0.5, 0.5);
    /* cairo_set_source_rgba(cairo_t *cr, double red, double green, double blue, double alpha); */
    /* Sets the source pattern within cr to a translucent color. This color will then be used for any subsequent drawing operation 
     * until a new source pattern is set.
     * The color and alpha components are floating point numbers in the range 0 to 1. If the values passed in are outside that range, 
     * they will be clamped.
     * The default source pattern is opaque black, (that is, it is equivalent to cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0)).
     * *****************************************************************************************************************************/
    cairo_set_source_rgba (cr, 1, 0, 0, 0.80);
    /* void cairo_fill(cairo_t *cr); */
    /* A drawing operator that fills the current path according to the current fill rule, (each sub-path is implicitly closed before 
     * being filled). After cairo_fill(), the current path will be cleared from the cairo context.
     * *****************************************************************************************************************************/
    cairo_fill (cr);

    cairo_rectangle (cr, 0, 0.5, 0.5, 0.5);
    cairo_set_source_rgba (cr, 0, 1, 0, 0.60);
    cairo_fill (cr);

    cairo_rectangle (cr, 0.5, 0, 0.5, 0.5);
    cairo_set_source_rgba (cr, 0, 0, 1, 0.40);
    cairo_fill (cr);

    cairo_surface_write_to_png(surface, "image.png");
    cairo_destroy(cr);
    cairo_surface_destroy(surface);

    return 0;
}

我得到一个空的png。看起来那里有东西,但它没有画出来。 我编译:

cc -o file $(pkg-config --cflags --libs cairo) file.c

我的 brew info Cairo 是这样的:

cairo: stable 1.16.0 (bottled), HEAD
Vector graphics library with cross-device output support
https://cairographics.org/
/usr/local/Cellar/cairo/1.16.0_3 (117 files, 5.7MB) *
  Poured from bottle on 2020-10-12 at 00:17:34
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/cairo.rb
License: LGPL-2.1
==> Dependencies
Build: pkg-config ✔
Required: fontconfig ✔, freetype ✔, glib ✔, libpng ✔, lzo ✔, pixman ✔
==> Options
--HEAD
    Install HEAD version
==> Analytics
install: 110,282 (30 days), 348,240 (90 days), 1,254,903 (365 days)
install-on-request: 13,651 (30 days), 54,037 (90 days), 132,345 (365 days)
build-error: 0 (30 days)

pkg-config 库列出了这个:

-L/usr/local/Cellar/cairo/1.16.0_3/lib -lcairo

和 pkg-config Cflags:

-I/usr/local/Cellar/libffi/3.3/include -I/usr/local/Cellar/cairo/1.16.0_3/include/cairo 
-I/usr/local/Cellar/glib/2.66.1/include -I/usr/local/Cellar/glib/2.66.1/include/glib-2.0 
-I/usr/local/Cellar/glib/2.66.1/lib/glib-2.0/include -I/usr/local/opt/gettext/include 
-I/usr/local/Cellar/pcre/8.44/include -I/usr/local/Cellar/pixman/0.40.0/include/pixman-1 
-I/usr/local/Cellar/fontconfig/2.13.1/include -I/usr/local/opt/freetype/include/freetype2 
-I/usr/local/Cellar/libpng/1.6.37/include/libpng16

有什么我看不到的问题吗?一切看起来都很棒。不知道怎么回事。

cairo_rectangle (cr, 0, 0, 0.5, 0.5);

您似乎假设坐标在 0..1 范围内以覆盖整个表面。

这不是真的。相反,坐标 ragnes 来自 0..width0..height.

您的指令将创建一个矩形,每个方向各有半个像素,线宽之前设置为 1/5 像素。你不会看到这些坐标。

您链接的示例使用了更大的值。试试看。