merge with master
[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         Glib::RefPtr<Pango::Layout> layout = Pango::Layout::create (context);
69
70         layout->set_text (_text);
71
72         if (_font_description) {
73                 layout->set_font_description (*_font_description);
74         }
75
76         layout->set_alignment (_alignment);
77         
78         Pango::Rectangle ink_rect = layout->get_ink_extents();
79         
80         _origin.x = ink_rect.get_x() / Pango::SCALE;
81         _origin.y = ink_rect.get_y() / Pango::SCALE;
82
83         _width = _origin.x + (ink_rect.get_width() / Pango::SCALE);
84         _height = _origin.y + (ink_rect.get_height() / Pango::SCALE);
85         
86         _image = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, _width, _height);
87
88         Cairo::RefPtr<Cairo::Context> img_context = Cairo::Context::create (_image);
89
90         /* and draw, in the appropriate color of course */
91
92         set_source_rgba (img_context, _color);
93         layout->show_in_cairo_context (img_context);
94
95         /* text has now been rendered in _image and is ready for blit in
96          * ::render 
97          */
98
99         _need_redraw = false;
100 }
101
102 void
103 Text::clamp_width (double w)
104 {
105         _clamped_width = w;
106 }
107
108 void
109 Text::compute_bounding_box () const
110 {
111         if (!_canvas || _text.empty()) {
112                 _bounding_box = boost::optional<Rect> ();
113                 _bounding_box_dirty = false;
114                 return;
115         }
116
117         PangoContext* _pc = gdk_pango_context_get ();
118         Glib::RefPtr<Pango::Context> context = Glib::wrap (_pc); // context now owns _pc and will free it
119         Glib::RefPtr<Pango::Layout> layout = Pango::Layout::create (context);
120         
121         layout->set_text (_text);
122         if (_font_description) {
123                 layout->set_font_description (*_font_description);
124         }
125         layout->set_alignment (_alignment);
126         Pango::Rectangle const r = layout->get_ink_extents ();
127         
128         _bounding_box = Rect (
129                 r.get_x() / Pango::SCALE,
130                 r.get_y() / Pango::SCALE,
131                 (r.get_x() + r.get_width()) / Pango::SCALE,
132                 (r.get_y() + r.get_height()) / Pango::SCALE
133                 );
134                 
135         _bounding_box_dirty = false;
136 }
137
138 void
139 Text::render (Rect const & /*area*/, Cairo::RefPtr<Cairo::Context> context) const
140 {
141         if (_text.empty()) {
142                 return;
143         }
144
145         if (_need_redraw) {
146                 redraw (context);
147         }
148         
149         context->set_source (_image, 0, 0);
150         context->rectangle (0, 0, min (_clamped_width, _width), _height);
151         context->fill ();
152 }
153
154 void
155 Text::set_alignment (Pango::Alignment alignment)
156 {
157         begin_change ();
158         
159         _alignment = alignment;
160         _need_redraw = true;
161         _bounding_box_dirty = true;
162         end_change ();
163 }
164
165 void
166 Text::set_font_description (Pango::FontDescription font_description)
167 {
168         begin_change ();
169         
170         _font_description = new Pango::FontDescription (font_description);
171         _need_redraw = true;
172
173         _bounding_box_dirty = true;
174         end_change ();
175 }
176
177 void
178 Text::set_color (Color color)
179 {
180         begin_change ();
181
182         _color = color;
183         _need_redraw = true;
184
185         end_change ();
186 }
187
188                 
189 void
190 Text::dump (ostream& o) const
191 {
192         Item::dump (o);
193
194         o << _canvas->indent() << '\t' << " text = " << _text << endl
195           << _canvas->indent() << " color = " << _color;
196
197         o << endl;
198 }