1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
|
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="highlight.min.css">
<script src="highlight.min.js"></script><script>
hljs.configure({languages: ['cpp']});
hljs.highlightAll();
</script><title>Drawing Text</title>
<link rel="stylesheet" type="text/css" href="style.css">
<meta name="generator" content="DocBook XSL Stylesheets Vsnapshot">
<link rel="home" href="index.html" title="Programming with gtkmm 4">
<link rel="up" href="chapter-drawingarea.html" title="Chapter 18. The DrawingArea Widget">
<link rel="prev" href="sec-cairo-drawing-arcs.html" title="Drawing Arcs and Circles">
<link rel="next" href="sec-draw-images.html" title="Drawing Images">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<div class="navheader">
<table width="100%" summary="Navigation header">
<tr><th colspan="3" align="center">Drawing Text</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="sec-cairo-drawing-arcs.html"><img src="icons/prev.png" alt="Prev"></a> </td>
<th width="60%" align="center">Chapter 18. The DrawingArea Widget</th>
<td width="20%" align="right"> <a accesskey="n" href="sec-draw-images.html"><img src="icons/next.png" alt="Next"></a>
</td>
</tr>
</table>
<hr>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="sec-drawing-text"></a>Drawing Text</h2></div></div></div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="drawing-text-pango"></a>Drawing Text with Pango</h3></div></div></div>
<p>
Text is drawn via Pango Layouts. The easiest way to create a
<code class="classname">Pango::Layout</code> is to use
<code class="methodname">Gtk::Widget::create_pango_layout()</code>.
Once created, the layout can be manipulated in various ways,
including changing the text, font, etc. Finally, the layout can
be rendered using the
<code class="methodname">Pango::Layout::show_in_cairo_context()</code> method.
</p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="pango-text-example"></a>Example</h3></div></div></div>
<p>
Here is an example of a program that draws some text, some of it
upside-down. The Printing chapter contains another
<a class="link" href="sec-printing-examples.html" title="Examples">example</a> of drawing text.
</p>
<div class="figure">
<a name="figure-drawingarea-pango-text"></a><p class="title"><b>Figure 18.6. Drawing Area - Text</b></p>
<div class="figure-contents">
<div class="screenshot">
<div class="mediaobject"><img src="figures/drawingarea_pango_text.png" alt="Drawing Area - Text"></div>
</div>
</div>
</div>
<br class="figure-break">
<p><a class="ulink" href="https://gitlab.gnome.org/GNOME/gtkmm-documentation/tree/master/examples/book/drawingarea/pango_text" target="_top">Source Code</a></p>
<p>File: <code class="filename">myarea.h</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">#ifndef GTKMM_EXAMPLE_MYAREA_H
#define GTKMM_EXAMPLE_MYAREA_H
#include <gtkmm/drawingarea.h>
class MyArea : public Gtk::DrawingArea
{
public:
MyArea();
virtual ~MyArea();
protected:
void on_draw(const Cairo::RefPtr<Cairo::Context>& cr, int width, int height);
private:
void draw_rectangle(const Cairo::RefPtr<Cairo::Context>& cr, int width, int height);
void draw_text(const Cairo::RefPtr<Cairo::Context>& cr, int rectangle_width, int rectangle_height);
};
#endif // GTKMM_EXAMPLE_MYAREA_H
</code></pre>
<p>File: <code class="filename">main.cc</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">#include "myarea.h"
#include <gtkmm/application.h>
#include <gtkmm/window.h>
class ExampleWindow : public Gtk::Window
{
public:
ExampleWindow();
protected:
MyArea m_area;
};
ExampleWindow::ExampleWindow()
{
set_title("Drawing text example");
set_child(m_area);
}
int main(int argc, char* argv[])
{
auto app = Gtk::Application::create("org.gtkmm.example");
return app->make_window_and_run<ExampleWindow>(argc, argv);
}
</code></pre>
<p>File: <code class="filename">myarea.cc</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">#include "myarea.h"
MyArea::MyArea()
{
set_draw_func(sigc::mem_fun(*this, &MyArea::on_draw));
}
MyArea::~MyArea()
{
}
void MyArea::on_draw(const Cairo::RefPtr<Cairo::Context>& cr, int width, int height)
{
const int rectangle_width = width;
const int rectangle_height = height / 2;
// Draw a black rectangle
cr->set_source_rgb(0.0, 0.0, 0.0);
draw_rectangle(cr, rectangle_width, rectangle_height);
// and some white text
cr->set_source_rgb(1.0, 1.0, 1.0);
draw_text(cr, rectangle_width, rectangle_height);
// flip the image vertically
// see http://www.cairographics.org/documentation/cairomm/reference/classCairo_1_1Matrix.html
// the -1 corresponds to the yy part (the flipping part)
// the height part is a translation (we could have just called cr->translate(0, height) instead)
// it's height and not height / 2, since we want this to be on the second part of our drawing
// (otherwise, it would draw over the previous part)
Cairo::Matrix matrix(1.0, 0.0, 0.0, -1.0, 0.0, height);
// apply the matrix
cr->transform(matrix);
// white rectangle
cr->set_source_rgb(1.0, 1.0, 1.0);
draw_rectangle(cr, rectangle_width, rectangle_height);
// black text
cr->set_source_rgb(0.0, 0.0, 0.0);
draw_text(cr, rectangle_width, rectangle_height);
}
void MyArea::draw_rectangle(const Cairo::RefPtr<Cairo::Context>& cr,
int width, int height)
{
cr->rectangle(0, 0, width, height);
cr->fill();
}
void MyArea::draw_text(const Cairo::RefPtr<Cairo::Context>& cr,
int rectangle_width, int rectangle_height)
{
// https://gnome.pages.gitlab.gnome.org/pangomm/classPango_1_1FontDescription.html
Pango::FontDescription font;
font.set_family("Monospace");
font.set_weight(Pango::Weight::BOLD);
// https://gnome.pages.gitlab.gnome.org/pangomm/classPango_1_1Layout.html
auto layout = create_pango_layout("Hi there!");
layout->set_font_description(font);
int text_width;
int text_height;
//get the text dimensions (it updates the variables -- by reference)
layout->get_pixel_size(text_width, text_height);
// Position the text in the middle
cr->move_to((rectangle_width-text_width)/2, (rectangle_height-text_height)/2);
layout->show_in_cairo_context(cr);
}
</code></pre>
</div>
</div>
<div class="navfooter">
<hr>
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left">
<a accesskey="p" href="sec-cairo-drawing-arcs.html"><img src="icons/prev.png" alt="Prev"></a> </td>
<td width="20%" align="center"><a accesskey="u" href="chapter-drawingarea.html"><img src="icons/up.png" alt="Up"></a></td>
<td width="40%" align="right"> <a accesskey="n" href="sec-draw-images.html"><img src="icons/next.png" alt="Next"></a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">Drawing Arcs and Circles </td>
<td width="20%" align="center"><a accesskey="h" href="index.html"><img src="icons/home.png" alt="Home"></a></td>
<td width="40%" align="right" valign="top"> Drawing Images</td>
</tr>
</table>
</div>
</body>
</html>
|