consolidate NSGLView
[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 double
88 Text::width () const
89 {
90         if (_need_redraw) {
91                 _redraw  ();
92         }
93         return _width;
94 }
95
96 double
97 Text::height () const
98 {
99         if (_need_redraw) {
100                 _redraw  ();
101         }
102         return _height;
103 }
104
105 void
106 Text::_redraw () const
107 {
108         assert (!_text.empty());
109         assert (_canvas);
110         Glib::RefPtr<Pango::Context> context = _canvas->get_pango_context();
111         Glib::RefPtr<Pango::Layout> layout = Pango::Layout::create (context);
112
113 #if 0 // def __APPLE__ // Looks like this is no longer needed 2017-03-11, pango 1.36.8, pangomm 2.34.0
114         if (_width_correction < 0.0) {
115                 // Pango returns incorrect text width on some OS X
116                 // So we have to make a correction
117                 // To determine the correct indent take the largest symbol for which the width is correct
118                 // and make the calculation
119                 Gtk::Window win;
120                 Gtk::Label foo;
121                 win.add (foo);
122                 win.ensure_style ();
123
124                 int width = 0;
125                 int height = 0;
126                 Glib::RefPtr<Pango::Layout> test_layout = foo.create_pango_layout ("H");
127                 if (_font_description) {
128                         test_layout->set_font_description (*_font_description);
129                 }
130                 test_layout->get_pixel_size (width, height);
131
132                 _width_correction = width*1.5;
133         }
134 #else
135         /* don't bother with a conditional here */
136         _width_correction = 0.0;
137 #endif
138
139         layout->set_text (_text);
140
141         if (_font_description) {
142                 layout->set_font_description (*_font_description);
143         }
144
145         layout->set_alignment (_alignment);
146
147         int w;
148         int h;
149
150         layout->get_pixel_size (w, h);
151
152         _width = w + _width_correction;
153         _height = h;
154
155 #ifdef __APPLE__
156         _image = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, _width * 2, _height * 2);
157 #else
158         _image = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, _width, _height);
159 #endif
160
161         Cairo::RefPtr<Cairo::Context> img_context = Cairo::Context::create (_image);
162
163 #ifdef __APPLE__
164         /* Below, the rendering scaling is set to support retina display
165          */
166         img_context->scale (2, 2);
167 #endif
168
169         /* and draw, in the appropriate color of course */
170
171         if (_outline) {
172                 set_source_rgba (img_context, _outline_color);
173                 layout->update_from_cairo_context (img_context);
174                 pango_cairo_layout_path (img_context->cobj(), layout->gobj());
175                 img_context->stroke_preserve ();
176                 set_source_rgba (img_context, _color);
177                 img_context->fill ();
178         } else {
179                 set_source_rgba (img_context, _color);
180                 layout->show_in_cairo_context (img_context);
181         }
182
183         /* text has now been rendered in _image and is ready for blit in
184          * ::render
185          */
186
187         _need_redraw = false;
188 }
189
190 void
191 Text::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
192 {
193         if (_text.empty()) {
194                 return;
195         }
196
197         Rect self = item_to_window (Rect (0, 0, min (_clamped_width, (double)_image->get_width ()), _image->get_height ()));
198         Rect i = self.intersection (area);
199
200         if (!i) {
201                 return;
202         }
203
204         if (_need_redraw) {
205                 _redraw ();
206         }
207
208         Rect intersection (i);
209
210         context->rectangle (intersection.x0, intersection.y0, intersection.width(), intersection.height());
211 #ifdef __APPLE__
212         /* Below, the rendering scaling is set to support retina display
213          */
214         Cairo::Matrix original_matrix = context->get_matrix();
215         context->scale (0.5, 0.5);
216         context->set_source (_image, self.x0 * 2, self.y0 * 2);
217         context->fill ();
218         context->set_matrix (original_matrix);
219 #else
220         context->set_source (_image, self.x0, self.y0);
221         context->fill ();
222 #endif
223 }
224
225 void
226 Text::clamp_width (double w)
227 {
228         if (_clamped_width == w) {
229                 return;
230         }
231         begin_change ();
232         _clamped_width = w;
233         _bounding_box_dirty = true;
234         end_change ();
235 }
236
237 void
238 Text::compute_bounding_box () const
239 {
240         if (!_canvas || _text.empty()) {
241                 _bounding_box = Rect ();
242                 _bounding_box_dirty = false;
243                 return;
244         }
245
246         if (_bounding_box_dirty) {
247 #ifdef __APPLE__
248                 const float retina_factor = 0.5;
249 #else
250                 const float retina_factor = 1.0;
251 #endif
252                 if (_need_redraw || !_image) {
253                         _redraw ();
254                 }
255                 _bounding_box = Rect (0, 0, min (_clamped_width, (double) _image->get_width() * retina_factor), _image->get_height() * retina_factor);
256                 _bounding_box_dirty = false;
257         }
258 }
259
260 void
261 Text::set_alignment (Pango::Alignment alignment)
262 {
263         if (alignment == _alignment) {
264                 return;
265         }
266
267         begin_change ();
268
269         _alignment = alignment;
270         _need_redraw = true;
271         _bounding_box_dirty = true;
272         end_change ();
273 }
274
275 void
276 Text::set_font_description (Pango::FontDescription font_description)
277 {
278         begin_change ();
279
280         _font_description = new Pango::FontDescription (font_description);
281         _need_redraw = true;
282         _width_correction = -1.0;
283
284         _bounding_box_dirty = true;
285         end_change ();
286 }
287
288 void
289 Text::set_color (Color color)
290 {
291         if (color == _color) {
292                 return;
293         }
294
295         begin_change ();
296
297         _color = color;
298         if (_outline) {
299                 set_outline_color (contrasting_text_color (_color));
300         }
301         _need_redraw = true;
302
303         end_change ();
304 }
305
306
307 void
308 Text::dump (ostream& o) const
309 {
310         Item::dump (o);
311
312         o << _canvas->indent() << '\t' << " text = " << _text << endl
313           << _canvas->indent() << " color = " << _color;
314
315         o << endl;
316 }
317
318
319 double
320 Text::text_width() const
321 {
322     if (_need_redraw) {
323         redraw ();
324     }
325
326     return _width;
327 }