cairomm: text-rotate.cc

cairomm 1.19.1

text-rotate.cc

A simple example of using text in cairomm.

A simple example of using text in cairomm

/* Copyright (C) 2008 The cairomm Development Team
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see <https://www.gnu.org/licenses/>.
*/
/* M_PI is defined in math.h in the case of Microsoft Visual C++, and
* Solaris needs math.h for M_PI and floor()
*/
#include <string>
#include <iostream>
#include <cairommconfig.h>
#include <cairomm/cairomm.h>
#include <cmath>
// This example is based on the C cairo example of the same name
const int WIDTH = 150;
const int HEIGHT = 150;
const int NUM_TEXT = 20;
const int TEXT_SIZE = 12;
/* Draw the word cairo at NUM_TEXT different angles */
void draw(Cairo::RefPtr<Cairo::Context> cr, int width, int height)
{
int i, x_off, y_off;
Cairo::TextExtents extents;
std::string text("cairo");
cr->select_font_face("Bitstream Vera Sans", Cairo::ToyFontFace::Slant::NORMAL,
Cairo::ToyFontFace::Weight::NORMAL);
cr->set_font_size(TEXT_SIZE);
Cairo::FontOptions font_options;
font_options. set_hint_style(Cairo::FontOptions::HintStyle::NONE);
font_options. set_hint_metrics(Cairo::FontOptions::HintMetrics::OFF);
font_options. set_antialias(Cairo::ANTIALIAS_GRAY);
cr->set_font_options(font_options);
cr->set_source_rgb(0.0, 0.0, 0.0);
cr->translate(width / 2.0, height / 2.0);
cr->get_text_extents(text, extents);
if (NUM_TEXT == 1)
{
x_off = y_off = 0;
}
else
{
y_off = (int) - floor(0.5 + extents.height / 2.0);
x_off = (int) floor(0.5 + (extents.height + 1.0) / (2.0 * tan (M_PI / NUM_TEXT)));
}
for (i=0; i < NUM_TEXT; i++)
{
cr->save();
cr->rotate(2 * M_PI * i / NUM_TEXT);
cr->set_line_width(1.0);
cr->rectangle(x_off - 0.5, y_off - 0.5, extents.width + 1,
extents.height + 1);
cr->set_source_rgb(1, 0, 0);
cr->stroke();
cr->move_to(x_off - extents.x_bearing, y_off - extents.y_bearing);
cr->set_source_rgb(0, 0, 0);
cr->show_text("cairo");
cr->restore();
}
}
int main (void)
{
auto surface = Cairo::ImageSurface::create(Cairo::Surface::Format::ARGB32, WIDTH, HEIGHT);
auto cr = Cairo::Context::create(surface);
draw(cr, WIDTH, HEIGHT);
#ifdef CAIRO_HAS_PNG_FUNCTIONS
const char* filename = "text-rotate.png";
surface->write_to_png(filename);
std::cout << "Wrote file " << filename << std::endl;
#else
std::cout << "You must compile cairo with PNG support for this example to work" << std::endl;
#endif
}
iostream
string
cmath
floor
constexpr __enable_if_is_duration< _ToDur > floor(const duration< _Rep, _Period > &__d)
tan
complex< _Tp > tan(const complex< _Tp > &)
std::string
basic_string< char > string
std::endl
basic_ostream< _CharT, _Traits > & endl(basic_ostream< _CharT, _Traits > &__os)
std::cout
ostream cout
Cairo::Context::create
static RefPtr< Context > create(const RefPtr< Surface > &target)
Cairo::FontOptions
The font options specify how fonts should be rendered.
Definition fontoptions.h:39
Cairo::FontOptions::HintStyle::NONE
@ NONE
Do not hint outlines.
Definition fontoptions.h:60
Cairo::FontOptions::set_hint_style
void set_hint_style(HintStyle hint_style)
Sets the hint style for font outlines for the font options object.
Cairo::FontOptions::set_antialias
void set_antialias(Antialias antialias)
Sets the antialiasing mode for the font options object.
Cairo::FontOptions::set_hint_metrics
void set_hint_metrics(HintMetrics hint_metrics)
Sets the metrics hinting mode for the font options object.
Cairo::FontOptions::HintMetrics::OFF
@ OFF
Do not hint font metrics.
Definition fontoptions.h:96
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::NORMAL
@ NORMAL
Upright font style.
Definition fontface.h:118
Cairo::ToyFontFace::Weight::NORMAL
@ NORMAL
Normal font weight.
Definition fontface.h:139
Cairo::ANTIALIAS_GRAY
@ ANTIALIAS_GRAY
Perform single-color antialiasing (using shades of gray for black text on white background,...
Definition enums.h:56
Cairo::RefPtr
std::shared_ptr< T_CppObject > RefPtr
RefPtr<> is a reference-counting shared smartpointer.
Definition refptr.h:39
Cairo::TextExtents
cairo_text_extents_t TextExtents
See the cairo_text_extents_t reference in the cairo manual for more information.
Definition types.h:48