fix crash when copy'ing latent plugins
[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/window.h>
24 #include <gtkmm/label.h>
25
26 #include "pbd/stacktrace.h"
27
28 #include "canvas/text.h"
29 #include "canvas/canvas.h"
30 #include "canvas/utils.h"
31 #include "canvas/colors.h"
32
33 using namespace std;
34 using namespace ArdourCanvas;
35
36
37 Text::Text (Canvas* c)
38         : Item (c)
39         , _color (0x000000ff)
40         , _font_description (0)
41         , _alignment (Pango::ALIGN_LEFT)
42         , _width (0)
43         , _height (0)
44         , _need_redraw (false)
45         , _width_correction (-1)
46         , _clamped_width (COORD_MAX)
47 {
48         _outline = false;
49 }
50
51 Text::Text (Item* parent)
52         : Item (parent)
53         , _color (0x000000ff)
54         , _font_description (0)
55         , _alignment (Pango::ALIGN_LEFT)
56         , _width (0)
57         , _height (0)
58         , _need_redraw (false)
59         , _width_correction (-1)
60         , _clamped_width (COORD_MAX)
61 {
62         _outline = false;
63 }
64
65 Text::~Text ()
66 {
67         delete _font_description;
68 }
69
70 void
71 Text::set (string const & text)
72 {
73         if (text == _text) {
74                 return;
75         }
76
77         begin_change ();
78
79         _text = text;
80
81         _need_redraw = true;
82         _bounding_box_dirty = true;
83
84         end_change ();
85 }
86
87 void
88 Text::_redraw () const
89 {
90         assert (!_text.empty());
91         Glib::RefPtr<Pango::Context> context = Glib::wrap (gdk_pango_context_get());
92         Glib::RefPtr<Pango::Layout> layout = Pango::Layout::create (context);
93
94 #ifdef __APPLE__
95         if (_width_correction < 0.0) {
96                 // Pango returns incorrect text width on some OS X
97                 // So we have to make a correction
98                 // To determine the correct indent take the largest symbol for which the width is correct
99                 // and make the calculation
100                 Gtk::Window win;
101                 Gtk::Label foo;
102                 win.add (foo);
103                 win.ensure_style ();
104
105                 int width = 0;
106                 int height = 0;
107                 Glib::RefPtr<Pango::Layout> test_layout = foo.create_pango_layout ("H");
108                 if (_font_description) {
109                         test_layout->set_font_description (*_font_description);
110                 }
111                 test_layout->get_pixel_size (width, height);
112
113                 _width_correction = width*1.5;
114         }
115 #else
116         /* don't bother with a conditional here */
117         _width_correction = 0.0;
118 #endif
119
120         layout->set_text (_text);
121
122         if (_font_description) {
123                 layout->set_font_description (*_font_description);
124         }
125
126         layout->set_alignment (_alignment);
127
128         int w;
129         int h;
130
131         layout->get_pixel_size (w, h);
132
133         _width = w + _width_correction;
134         _height = h;
135
136 #ifdef __APPLE__
137         _image = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, _width * 2, _height * 2);
138 #else
139         _image = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, _width, _height);
140 #endif
141
142         Cairo::RefPtr<Cairo::Context> img_context = Cairo::Context::create (_image);
143
144 #ifdef __APPLE__
145         /* Below, the rendering scaling is set to support retina display
146          */
147         img_context->scale (2, 2);
148 #endif
149
150         /* and draw, in the appropriate color of course */
151
152         if (_outline) {
153                 set_source_rgba (img_context, _outline_color);
154                 layout->update_from_cairo_context (img_context);
155                 pango_cairo_layout_path (img_context->cobj(), layout->gobj());
156                 img_context->stroke_preserve ();
157                 set_source_rgba (img_context, _color);
158                 img_context->fill ();
159         } else {
160                 set_source_rgba (img_context, _color);
161                 layout->show_in_cairo_context (img_context);
162         }
163
164         /* text has now been rendered in _image and is ready for blit in
165          * ::render
166          */
167
168         _need_redraw = false;
169 }
170
171 void
172 Text::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
173 {
174         if (_text.empty()) {
175                 return;
176         }
177
178         Rect self = item_to_window (Rect (0, 0, min (_clamped_width, (double)_image->get_width ()), _image->get_height ()));
179         boost::optional<Rect> i = self.intersection (area);
180
181         if (!i) {
182                 return;
183         }
184
185         if (_need_redraw) {
186                 _redraw ();
187         }
188
189         Rect intersection (i.get());
190
191         context->rectangle (intersection.x0, intersection.y0, intersection.width(), intersection.height());
192 #ifdef __APPLE__
193         /* Below, the rendering scaling is set to support retina display
194          */
195         Cairo::Matrix original_matrix = context->get_matrix();
196         context->scale (0.5, 0.5);
197         context->set_source (_image, self.x0 * 2, self.y0 * 2);
198         context->fill ();
199         context->set_matrix (original_matrix);
200 #else
201         context->set_source (_image, self.x0, self.y0);
202         context->fill ();
203 #endif
204 }
205
206 void
207 Text::clamp_width (double w)
208 {
209         if (_clamped_width == w) {
210                 return;
211         }
212         begin_change ();
213         _clamped_width = w;
214         _bounding_box_dirty = true;
215         end_change ();
216 }
217
218 void
219 Text::compute_bounding_box () const
220 {
221         if (!_canvas || _text.empty()) {
222                 _bounding_box = boost::optional<Rect> ();
223                 _bounding_box_dirty = false;
224                 return;
225         }
226
227         if (_bounding_box_dirty) {
228 #ifdef __APPLE__
229                 const float retina_factor = 0.5;
230 #else
231                 const float retina_factor = 1.0;
232 #endif
233                 if (_need_redraw || !_image) {
234                         _redraw ();
235                 }
236                 _bounding_box = Rect (0, 0, min (_clamped_width, (double) _image->get_width() * retina_factor), _image->get_height() * retina_factor);
237                 _bounding_box_dirty = false;
238         }
239 }
240
241 void
242 Text::set_alignment (Pango::Alignment alignment)
243 {
244         if (alignment == _alignment) {
245                 return;
246         }
247
248         begin_change ();
249
250         _alignment = alignment;
251         _need_redraw = true;
252         _bounding_box_dirty = true;
253         end_change ();
254 }
255
256 void
257 Text::set_font_description (Pango::FontDescription font_description)
258 {
259         begin_change ();
260
261         _font_description = new Pango::FontDescription (font_description);
262         _need_redraw = true;
263         _width_correction = -1.0;
264
265         _bounding_box_dirty = true;
266         end_change ();
267 }
268
269 void
270 Text::set_color (Color color)
271 {
272         if (color == _color) {
273                 return;
274         }
275
276         begin_change ();
277
278         _color = color;
279         if (_outline) {
280                 set_outline_color (contrasting_text_color (_color));
281         }
282         _need_redraw = true;
283
284         end_change ();
285 }
286
287
288 void
289 Text::dump (ostream& o) const
290 {
291         Item::dump (o);
292
293         o << _canvas->indent() << '\t' << " text = " << _text << endl
294           << _canvas->indent() << " color = " << _color;
295
296         o << endl;
297 }
298
299
300 double
301 Text::text_width() const
302 {
303     if (_need_redraw) {
304         redraw ();
305     }
306
307     return _width;
308 }