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