cairomm: ps-surface.cc

cairomm 1.19.0

ps-surface.cc

An example of using Cairo::PsSurface class to render to PostScript.

An example of using Cairo::PsSurface class to render to PostScript

/* M_PI is defined in math.h in the case of Microsoft Visual C++, Solaris,
* et. al.
*/
#include <string>
#include <iostream>
#include <cairommconfig.h>
#include <cairomm/context.h>
#include <cairomm/surface.h>
#include <cmath>
int main()
{
#ifdef CAIRO_HAS_PS_SURFACE
std::string filename = "image.ps";
double width = 600;
double height = 400;
auto surface =
Cairo::PsSurface::create(filename, width, height);
auto cr = Cairo::Context::create(surface);
cr->save(); // save the state of the context
cr->set_source_rgb(0.86, 0.85, 0.47);
cr->paint(); // fill image with the color
cr->restore(); // color is back to black now
cr->save();
// draw a border around the image
cr->set_line_width(20.0); // make the line wider
cr->rectangle(0.0, 0.0, cairo_image_surface_get_width(surface->cobj()), height);
cr->stroke();
cr->set_source_rgba(0.0, 0.0, 0.0, 0.7);
// draw a circle in the center of the image
cr->arc(width / 2.0, height / 2.0,
height / 4.0, 0.0, 2.0 * M_PI);
cr->stroke();
// draw a diagonal line
cr->move_to(width / 4.0, height / 4.0);
cr->line_to(width * 3.0 / 4.0, height * 3.0 / 4.0);
cr->stroke();
cr->restore();
cr->show_page();
std::cout << "Wrote PostScript file \"" << filename << "\"" << std::endl;
return 0;
#else
std::cout << "You must compile cairo with PS (Postscript) support for this example to work."
<< std::endl;
return 1;
#endif
}
iostream
string
cmath
std::endl
basic_ostream< _CharT, _Traits > & endl(basic_ostream< _CharT, _Traits > &__os)
std::cout
ostream cout
std::basic_string
Cairo::Context::create
static RefPtr< Context > create(const RefPtr< Surface > &target)
Cairo::PsSurface::create
static RefPtr< PsSurface > create(std::string filename, double width_in_points, double height_in_points)
Creates a PsSurface with a specified dimensions that will be saved as the given filename.