cairomm: toy-text.cc

cairomm 1.18.0

toy-text.cc

A relatively simple example of using Cairo::ToyFontFace.

A relatively simple example of using Cairo::ToyFontFace

#include <cairomm/cairomm.h>
const double HEIGHT = 200.0;
const double WIDTH = 400.0;
const double FONT_SIZE = 64.0;
const double TEXT_ORIGIN_Y = (HEIGHT / 2.0) + (FONT_SIZE / 2.0);
const double TEXT_ORIGIN_X = 50.0; // arbitrary
int main(int, char**)
{
auto surface =
Cairo::ImageSurface::create(Cairo::Surface::Format::ARGB32, WIDTH, HEIGHT);
auto cr = Cairo::Context::create(surface);
// fill background in white
cr->set_source_rgb(1.0, 1.0, 1.0);
cr->paint();
// draw a little dot at the point where text will be drawn
cr->arc(TEXT_ORIGIN_X, TEXT_ORIGIN_Y, FONT_SIZE / 4.0, 0, 2*M_PI);
cr->set_source_rgba(0.0, 1.0, 0.0, 0.5);
cr->fill();
// draw the text
cr->move_to(TEXT_ORIGIN_X, TEXT_ORIGIN_Y);
cr->set_source_rgb(0.8, 0.2, 0.2);
auto font =
Cairo::ToyFontFace::create("Bitstream Charter",
Cairo::ToyFontFace::Slant::ITALIC,
Cairo::ToyFontFace::Weight::BOLD);
cr->set_font_face(font);
cr->set_font_size(FONT_SIZE);
cr->show_text("cairomm!");
surface->write_to_png("toy-text.png");
return 0;
}
Cairo::Context::create
static RefPtr< Context > create(const RefPtr< Surface > &target)
Cairo::ImageSurface::create
static RefPtr< ImageSurface > create(Format format, int width, int height)
Creates an image surface of the specified format and dimensions.
Cairo::Surface::Format::ARGB32
@ ARGB32
Each pixel is a 32-bit quantity, with alpha in the upper 8 bits, then red, then green,...
Definition surface.h:244
Cairo::ToyFontFace::Slant::ITALIC
@ ITALIC
Italic font style.
Definition fontface.h:121
Cairo::ToyFontFace::create
static RefPtr< ToyFontFace > create(const std::string &family, Slant slant, Weight weight)
Creates a font face from a triplet of family, slant, and weight.
Cairo::ToyFontFace::Weight::BOLD
@ BOLD
Bold font weight.
Definition fontface.h:142