c - 开罗的空输出png文件
问题描述
我已经为 C 渲染了 hello 示例。我的意思是,使用 Cairo 显示Here的最小 C 程序。但是,当我尝试渲染时:
#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..width
和0..height
。
您的指令将创建一个在每个方向上都有半个像素的矩形,并且之前将线宽设置为 1/5 像素。这些坐标你不会看到太多。
您链接的示例使用更大的值。试试看。
推荐阅读
- c++ - 使用模板化模板进行多重继承
- node.js - Mongoose 通过 id 查找递归嵌套对象
- python-3.x - 为什么kaggle API下载的-f开关不能识别传递给它的文件名作为字符串
- mysql - 如何查找在 Mysql 中执行失败事务的所有语句
- psutil - psutil 和 du 给出不同的输出
- python - 选择在单个模块中对方法进行 BC 重大更改的方法
- docker - docker vscode 操作超时
- tableau-api - 我将我的运行总计除以某个值,但总计不正确
- c# - 如何保存这些随机生成的项目
- android - 当视图的父级有边距时,Android View 的 offsetLeftAndRight 和 offsetTopAndBottom 不起作用