Debian Codegraph @

examples/book/drawingarea/pango_text/myarea.cc

raw
1 /* gtkmm example Copyright (C) 2012 gtkmm development team
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program 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
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, see <https://www.gnu.org/licenses/>.
15 */
16
17 #include "myarea.h"
18
19 MyArea::MyArea()
20 {
21 set_draw_func(sigc::mem_fun(*this, &MyArea::on_draw));
22 }
23
24 MyArea::~MyArea()
25 {
26 }
27
28 void MyArea::on_draw(const Cairo::RefPtr<Cairo::Context>& cr, int width, int height)
29 {
30 const int rectangle_width = width;
31 const int rectangle_height = height / 2;
32
33 // Draw a black rectangle
34 cr->set_source_rgb(0.0, 0.0, 0.0);
35 draw_rectangle(cr, rectangle_width, rectangle_height);
36
37 // and some white text
38 cr->set_source_rgb(1.0, 1.0, 1.0);
39 draw_text(cr, rectangle_width, rectangle_height);
40
41 // flip the image vertically
42 // see http://www.cairographics.org/documentation/cairomm/reference/classCairo_1_1Matrix.html
43 // the -1 corresponds to the yy part (the flipping part)
44 // the height part is a translation (we could have just called cr->translate(0, height) instead)
45 // it's height and not height / 2, since we want this to be on the second part of our drawing
46 // (otherwise, it would draw over the previous part)
47 Cairo::Matrix matrix(1.0, 0.0, 0.0, -1.0, 0.0, height);
48
49 // apply the matrix
50 cr->transform(matrix);
51
52 // white rectangle
53 cr->set_source_rgb(1.0, 1.0, 1.0);
54 draw_rectangle(cr, rectangle_width, rectangle_height);
55
56 // black text
57 cr->set_source_rgb(0.0, 0.0, 0.0);
58 draw_text(cr, rectangle_width, rectangle_height);
59 }
60
61 void MyArea::draw_rectangle(const Cairo::RefPtr<Cairo::Context>& cr,
62 int width, int height)
63 {
64 cr->rectangle(0, 0, width, height);
65 cr->fill();
66 }
67
68 void MyArea::draw_text(const Cairo::RefPtr<Cairo::Context>& cr,
69 int rectangle_width, int rectangle_height)
70 {
71 // https://gnome.pages.gitlab.gnome.org/pangomm/classPango_1_1FontDescription.html
72 Pango::FontDescription font;
73
74 font.set_family("Monospace");
75 font.set_weight(Pango::Weight::BOLD);
76
77 // https://gnome.pages.gitlab.gnome.org/pangomm/classPango_1_1Layout.html
78 auto layout = create_pango_layout("Hi there!");
79
80 layout->set_font_description(font);
81
82 int text_width;
83 int text_height;
84
85 //get the text dimensions (it updates the variables -- by reference)
86 layout->get_pixel_size(text_width, text_height);
87
88 // Position the text in the middle
89 cr->move_to((rectangle_width-text_width)/2, (rectangle_height-text_height)/2);
90
91 layout->show_in_cairo_context(cr);
92 }