65 lines
1.7 KiB
C++
Raw Normal View History

2016-07-09 11:21:54 +02:00
// Taken from
// http://cgit.freedesktop.org/cairomm/plain/examples/surfaces/image-surface.cc
2014-08-03 19:52:23 +02:00
/* M_PI is defined in math.h in the case of Microsoft Visual C++, Solaris,
* et. al.
*/
#if defined(_MSC_VER)
2018-08-09 18:06:22 +02:00
# define _USE_MATH_DEFINES
2014-08-03 19:52:23 +02:00
#endif
2020-02-01 23:06:01 +01:00
#include <cmath>
2016-07-09 11:21:54 +02:00
#include <iostream>
#include <string>
2014-08-03 19:52:23 +02:00
2020-02-01 23:06:01 +01:00
#include <cairomm/context.h>
#include <cairomm/surface.h>
#include <cairommconfig.h>
2014-08-03 19:52:23 +02:00
int main()
{
2016-07-09 11:21:54 +02:00
Cairo::RefPtr<Cairo::ImageSurface> surface =
Cairo::ImageSurface::create(Cairo::FORMAT_ARGB32, 600, 400);
Cairo::RefPtr<Cairo::Context> 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, surface->get_width(), surface->get_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(surface->get_width() / 2.0, surface->get_height() / 2.0,
surface->get_height() / 4.0, 0.0, 2.0 * M_PI);
cr->stroke();
// draw a diagonal line
cr->move_to(surface->get_width() / 4.0, surface->get_height() / 4.0);
cr->line_to(surface->get_width() * 3.0 / 4.0,
surface->get_height() * 3.0 / 4.0);
cr->stroke();
cr->restore();
2014-08-03 19:52:23 +02:00
#ifdef CAIRO_HAS_PNG_FUNCTIONS
2016-07-09 11:21:54 +02:00
std::string filename = "image.png";
surface->write_to_png(filename);
2014-08-03 19:52:23 +02:00
2016-07-09 11:21:54 +02:00
std::cout << "Wrote png file \"" << filename << "\"" << std::endl;
2014-08-03 19:52:23 +02:00
#else
2016-07-09 11:21:54 +02:00
std::cout
<< "You must compile cairo with PNG support for this example to work."
<< std::endl;
2014-08-03 19:52:23 +02:00
#endif
}