【问题标题】:cairo pdf bounding box开罗pdf边界框
【发布时间】:2019-05-19 04:16:26
【问题描述】:

我正在使用带有 C++ 的 cairo 图形并输出为 pdf。但是,当图形包含在文档 (LaTeX) 中时,图形周围的空白区域过多。一个程序 cairo 如何在图形周围放置一个紧密的边界框?

【问题讨论】:

    标签: c++ pdf cairo bounding-box


    【解决方案1】:

    在调用 cairo_pdf_surface_create() 时传递所需的宽度和高度。之后,您可以自行选择要用绘图填充多少空间。如果你让 Cairo 一直画到边缘,它会的。

    我唯一能想到的另一件事是 LaTeX 添加了边框。但是,这超出了我的专业领域。

    【讨论】:

      【解决方案2】:
      /*
      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);
        }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-07-13
        • 1970-01-01
        • 1970-01-01
        • 2016-07-06
        • 2012-03-23
        • 2012-08-23
        • 2013-01-21
        相关资源
        最近更新 更多