tweak position of pango-rendered text image on canvas by 2 pixels upward
[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
94         layout->show_in_cairo_context (img_context);
95
96         /* text has now been rendered in _image and is ready for blit in
97          * ::render 
98          */
99
100         _need_redraw = false;
101 }
102
103 void
104 Text::render (Rect const & /*area*/, Cairo::RefPtr<Cairo::Context> context) const
105 {
106         if (_text.empty()) {
107                 return;
108         }
109
110         if (_need_redraw) {
111                 redraw (context);
112         }
113         
114         Rect self = item_to_window (Rect (0, 0, min (_clamped_width, _width), _height));
115         context->rectangle (self.x0, self.y0, self.width(), self.height());
116         context->set_source (_image, self.x0, self.y0 - 2);
117         context->fill ();
118 }
119
120 void
121 Text::clamp_width (double w)
122 {
123         _clamped_width = w;
124 }
125
126 void
127 Text::compute_bounding_box () const
128 {
129         if (!_canvas || _text.empty()) {
130                 _bounding_box = boost::optional<Rect> ();
131                 _bounding_box_dirty = false;
132                 return;
133         }
134
135         PangoContext* _pc = gdk_pango_context_get ();
136         Glib::RefPtr<Pango::Context> context = Glib::wrap (_pc); // context now owns _pc and will free it
137         Glib::RefPtr<Pango::Layout> layout = Pango::Layout::create (context);
138         
139         layout->set_text (_text);
140         if (_font_description) {
141                 layout->set_font_description (*_font_description);
142         }
143         layout->set_alignment (_alignment);
144         Pango::Rectangle const r = layout->get_ink_extents ();
145         
146         _bounding_box = Rect (
147                 r.get_x() / Pango::SCALE,
148                 r.get_y() / Pango::SCALE,
149                 (r.get_x() + r.get_width()) / Pango::SCALE,
150                 (r.get_y() + r.get_height()) / Pango::SCALE
151                 );
152                 
153         _bounding_box_dirty = false;
154 }
155
156 void
157 Text::set_alignment (Pango::Alignment alignment)
158 {
159         begin_change ();
160         
161         _alignment = alignment;
162         _need_redraw = true;
163         _bounding_box_dirty = true;
164         end_change ();
165 }
166
167 void
168 Text::set_font_description (Pango::FontDescription font_description)
169 {
170         begin_change ();
171         
172         _font_description = new Pango::FontDescription (font_description);
173         _need_redraw = true;
174
175         _bounding_box_dirty = true;
176         end_change ();
177 }
178
179 void
180 Text::set_color (Color color)
181 {
182         begin_change ();
183
184         _color = color;
185         _need_redraw = true;
186
187         end_change ();
188 }
189
190                 
191 void
192 Text::dump (ostream& o) const
193 {
194         Item::dump (o);
195
196         o << _canvas->indent() << '\t' << " text = " << _text << endl
197           << _canvas->indent() << " color = " << _color;
198
199         o << endl;
200 }