Merge branch 'master' into cairocanvas
[ardour.git] / libs / canvas / text.cc
1 /*
2     Copyright (C) 2011-2013 Paul Davis
3     Author: Carl Hetherington <cth@carlh.net>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #include <gdk/gdk.h>
21
22 #include <cairomm/cairomm.h>
23 #include <gtkmm/label.h>
24
25 #include "pbd/stacktrace.h"
26
27 #include "canvas/text.h"
28 #include "canvas/canvas.h"
29 #include "canvas/utils.h"
30
31 using namespace std;
32 using namespace ArdourCanvas;
33
34 Text::Text (Group* parent)
35         : Item (parent)
36         , _color (0x000000ff)
37         , _font_description (0)
38         , _alignment (Pango::ALIGN_LEFT)
39         , _width (0)
40         , _height (0)
41         , _need_redraw (false)
42         , _clamped_width (COORD_MAX)
43 {
44
45 }
46
47 Text::~Text ()
48 {
49         delete _font_description;
50 }
51
52 void
53 Text::set (string const & text)
54 {
55         begin_change ();
56         
57         _text = text;
58
59         _need_redraw = true;
60         _bounding_box_dirty = true;
61
62         end_change ();
63 }
64
65 void
66 Text::redraw (Cairo::RefPtr<Cairo::Context> context) const
67 {
68         if (_text.empty()) {
69                 return;
70         }
71
72         Glib::RefPtr<Pango::Layout> layout = Pango::Layout::create (context);
73
74         _redraw (layout);
75 }
76
77 void
78 Text::redraw (Glib::RefPtr<Pango::Context> context) const
79 {
80         if (_text.empty()) {
81                 return;
82         }
83
84         Glib::RefPtr<Pango::Layout> layout = Pango::Layout::create (context);
85         _redraw (layout);
86 }
87
88 void
89 Text::_redraw (Glib::RefPtr<Pango::Layout> layout) const
90 {
91         layout->set_text (_text);
92
93         if (_font_description) {
94                 layout->set_font_description (*_font_description);
95         }
96
97         layout->set_alignment (_alignment);
98         
99         int w;
100         int h;
101
102         layout->get_size (w, h);
103         
104         _width = w / Pango::SCALE;
105         _height = h / Pango::SCALE;
106         
107         _image = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, _width, _height);
108
109         Cairo::RefPtr<Cairo::Context> img_context = Cairo::Context::create (_image);
110
111         /* and draw, in the appropriate color of course */
112
113         set_source_rgba (img_context, _color);
114
115         layout->show_in_cairo_context (img_context);
116
117         /* text has now been rendered in _image and is ready for blit in
118          * ::render 
119          */
120
121         _need_redraw = false;
122 }
123
124 void
125 Text::render (Rect const & /*area*/, Cairo::RefPtr<Cairo::Context> context) const
126 {
127         if (_text.empty()) {
128                 return;
129         }
130
131         if (_need_redraw) {
132                 redraw (context);
133         }
134         
135         Rect self = item_to_window (Rect (0, 0, min (_clamped_width, _width), _height));
136         
137         context->rectangle (self.x0, self.y0, self.width(), self.height());
138         context->set_source (_image, self.x0, self.y0);
139         context->fill ();
140 }
141
142 void
143 Text::clamp_width (double w)
144 {
145         _clamped_width = w;
146 }
147
148 void
149 Text::compute_bounding_box () const
150 {
151         if (!_canvas || _text.empty()) {
152                 _bounding_box = boost::optional<Rect> ();
153                 _bounding_box_dirty = false;
154                 return;
155         }
156
157         if (_bounding_box_dirty) {
158                 if (_need_redraw || !_image) {
159                         Glib::RefPtr<Pango::Context> context = Glib::wrap (gdk_pango_context_get()); // context now owns C object and will free it
160                         redraw (context);
161                 }
162                 _bounding_box = Rect (0, 0, min (_clamped_width, (double) _image->get_width()), _image->get_height());
163                 _bounding_box_dirty = false;
164         }
165 }
166
167 void
168 Text::set_alignment (Pango::Alignment alignment)
169 {
170         begin_change ();
171         
172         _alignment = alignment;
173         _need_redraw = true;
174         _bounding_box_dirty = true;
175         end_change ();
176 }
177
178 void
179 Text::set_font_description (Pango::FontDescription font_description)
180 {
181         begin_change ();
182         
183         _font_description = new Pango::FontDescription (font_description);
184         _need_redraw = true;
185
186         _bounding_box_dirty = true;
187         end_change ();
188 }
189
190 void
191 Text::set_color (Color color)
192 {
193         begin_change ();
194
195         _color = color;
196         _need_redraw = true;
197
198         end_change ();
199 }
200
201                 
202 void
203 Text::dump (ostream& o) const
204 {
205         Item::dump (o);
206
207         o << _canvas->indent() << '\t' << " text = " << _text << endl
208           << _canvas->indent() << " color = " << _color;
209
210         o << endl;
211 }