空输出png文件格式开罗

问题描述

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

<p>ampm true:   <span id="time1"></span> (default)</p>
<p>ampm false:  <span id="time2"></span></p>

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

#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); /* 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); /* void cairo_line_to(cairo_t *cr,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,0); cairo_line_to (cr,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 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,height); * cairo_rel_line_to (cr,-width,0); * cairo_close_path (cr); * *************************************/ cairo_rectangle (cr,0.5,0.5); /* cairo_set_source_rgba(cairo_t *cr,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,1.0)). * *****************************************************************************************************************************/ cairo_set_source_rgba (cr,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.5); cairo_set_source_rgba (cr,0.60); cairo_fill (cr); cairo_rectangle (cr,0.40); cairo_fill (cr); cairo_surface_write_to_png(surface,"image.png"); cairo_destroy(cr); cairo_surface_destroy(surface); return 0; }

我的cc -o file $(pkg-config --cflags --libs cairo) file.c超出了这个范围:

brew info Cairo

pkg-config可以解决这个问题:

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-configrequired: fontconfig ✔,freetype ✔,glib ✔,libpng ✔,lzo ✔,pixman ✔ ==> Options --HEAD Install HEAD version ==> Analytics install: 110,282 (30 days),348,240 (90 days),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 Cflags:

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

有什么看不见的错误吗?一切看起来很棒。我不知道会发生什么。

解决方法

cairo_rectangle (cr,0.5,0.5);

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

这不是事实。取而代之的是,坐标织布来自0..width0..height

您的指令将创建一个在每个方向上都有半个像素的矩形,并且线宽之前设置为1/5像素。这些坐标不会让您看到太多。

您链接的样本使用大得多的值。试试看。