我在C ++中使用cairo图形,并输出为pdf。但是,当图形包含在文档(LaTeX)中时,图形周围会有太多空白。一个程序如何开罗在图形周围放置一个紧密的边框?
答案 0 :(得分:1)
在调用cairo_pdf_surface_create()时传递所需的宽度和高度。之后,您几乎可以自行选择要用图形填充多少空间。如果您要求开罗一直画到边缘,就可以。
我唯一想到的另一件事是LaTeX添加了边框。但是,这不在我的专业知识范围内。
答案 1 :(得分:0)
/*
https://cairographics.org/manual/cairo-Recording-Surfaces.html
*/
#include <stdio.h>
#include <math.h>
#include <cairo.h>
#include <cairo-svg.h>
#include <cairo-ps.h>
#include <cairo-pdf.h>
void star(cairo_t* cr, double radius)
{
double theta = 0.8*M_PI;
cairo_save(cr);
cairo_move_to(cr, 0.0, -radius);
for(int i=0; i<5; i++)
{
cairo_rotate(cr, theta);
cairo_line_to(cr, 0.0, -radius);
}
cairo_fill(cr);
cairo_restore(cr);
}
int main()
{
// set recording surface
cairo_surface_t* record = cairo_recording_surface_create(CAIRO_CONTENT_COLOR_ALPHA, NULL);
cairo_t* cr = cairo_create(record);
// start image
cairo_set_source_rgb(cr, 0.0, 0.0, 0.0); // set color to black
star(cr, 100); // big star
cairo_set_source_rgb(cr, 0.0, 0.0, 1.0); // set color to blue
star(cr, 95); // smaller star
// end image
double x0, y0, width, height;
cairo_recording_surface_ink_extents(record, &x0, &y0, &width, &height);
// printf("Size %lf by %lf at (%lf, %lf)\n", width, height, x0, y0);
// create pdf image
const char* outputfile = "bb.pdf";
cairo_surface_t* target = cairo_pdf_surface_create(outputfile, width, height);
cairo_t* crt = cairo_create(target);
// copy recorded image to target and paint
cairo_set_source_surface(crt, record, -x0, -y0);
cairo_paint(crt);
// clean up
cairo_destroy (cr);
cairo_surface_destroy (record);
cairo_destroy (crt);
cairo_surface_destroy (target);
}