1 /* Copyright (C) 2008 The cairomm Development Team
2 *
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Library General Public
5 * License as published by the Free Software Foundation; either
6 * version 2 of the License, or (at your option) any later version.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Library General Public License for more details.
12 *
13 * You should have received a copy of the GNU Library General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 * 02110-1301, USA.
17 */
18
19 /* M_PI is defined in math.h in the case of Microsoft Visual C++, and
20 * Solaris needs math.h for M_PI and floor()
21 */
22 #if defined(_MSC_VER)
23 #define _USE_MATH_DEFINES
24 #endif
25
26 #include <string>
27 #include <iostream>
28 #include <cairommconfig.h>
29 #include <cairomm/cairomm.h>
30
31 #include <cmath>
32
33 // This example is based on the C cairo example of the same name
34
35 const int WIDTH = 150;
36 const int HEIGHT = 150;
37 const int NUM_TEXT = 20;
38 const int TEXT_SIZE = 12;
39
40 /* Draw the word cairo at NUM_TEXT different angles */
draw(Cairo::RefPtr<Cairo::Context> cr,int width,int height)41 void draw(Cairo::RefPtr<Cairo::Context> cr, int width, int height)
42 {
43 int i, x_off, y_off;
44 Cairo::TextExtents extents;
45 std::string text("cairo");
46
47 cr->select_font_face("Bitstream Vera Sans", Cairo::FONT_SLANT_NORMAL,
48 Cairo::FONT_WEIGHT_NORMAL);
49 cr->set_font_size(TEXT_SIZE);
50
51 Cairo::FontOptions font_options;
52
53 font_options.set_hint_style(Cairo::HINT_STYLE_NONE);
54 font_options.set_hint_metrics(Cairo::HINT_METRICS_OFF);
55 font_options.set_antialias(Cairo::ANTIALIAS_GRAY);
56
57 cr->set_font_options(font_options);
58
59 cr->set_source_rgb(0.0, 0.0, 0.0);
60
61 cr->translate(width / 2.0, height / 2.0);
62
63 cr->get_text_extents(text, extents);
64
65 if (NUM_TEXT == 1)
66 {
67 x_off = y_off = 0;
68 }
69 else
70 {
71 y_off = (int) - floor(0.5 + extents.height / 2.0);
72 x_off = (int) floor(0.5 + (extents.height + 1.0) / (2.0 * tan (M_PI / NUM_TEXT)));
73 }
74
75 for (i=0; i < NUM_TEXT; i++)
76 {
77 cr->save();
78 cr->rotate(2 * M_PI * i / NUM_TEXT);
79 cr->set_line_width(1.0);
80 cr->rectangle(x_off - 0.5, y_off - 0.5, extents.width + 1,
81 extents.height + 1);
82 cr->set_source_rgb(1, 0, 0);
83 cr->stroke();
84 cr->move_to(x_off - extents.x_bearing, y_off - extents.y_bearing);
85 cr->set_source_rgb(0, 0, 0);
86 cr->show_text("cairo");
87 cr->restore();
88 }
89 }
90
main(void)91 int main (void)
92 {
93 auto surface = Cairo::ImageSurface::create(Cairo::FORMAT_ARGB32, WIDTH, HEIGHT);
94 auto cr = Cairo::Context::create(surface);
95 draw(cr, WIDTH, HEIGHT);
96 #ifdef CAIRO_HAS_PNG_FUNCTIONS
97 const char* filename = "text-rotate.png";
98 surface->write_to_png(filename);
99 std::cout << "Wrote file " << filename << std::endl;
100 #else
101 std::cout << "You must compile cairo with PNG support for this example to work" << std::endl;
102 #endif
103 }
104