13
0

consistent pango-text layout size - fixes #6490

Use gdk_pango_layout() for all text-size calculations.
In Canvas::Text always use the same context (local image surface)
for layouting (prior to this, local and window-context mix resulted in 
different sizes).
This commit is contained in:
Robin Gareus 2015-09-06 16:39:08 +02:00
parent 787fe69ff8
commit 57321bef35
3 changed files with 37 additions and 36 deletions

View File

@ -66,9 +66,7 @@ private:
mutable double _width_correction;
double _clamped_width;
void _redraw (Cairo::RefPtr<Cairo::Context>) const;
void _redraw (Glib::RefPtr<Pango::Context>) const;
void __redraw (Glib::RefPtr<Pango::Layout>) const;
void _redraw () const;
};
}

View File

@ -81,31 +81,12 @@ Text::set (string const & text)
}
void
Text::_redraw (Cairo::RefPtr<Cairo::Context> context) const
Text::_redraw () const
{
if (_text.empty()) {
return;
}
assert (!_text.empty());
Glib::RefPtr<Pango::Context> context = Glib::wrap (gdk_pango_context_get());
Glib::RefPtr<Pango::Layout> layout = Pango::Layout::create (context);
__redraw (layout);
}
void
Text::_redraw (Glib::RefPtr<Pango::Context> context) const
{
if (_text.empty()) {
return;
}
Glib::RefPtr<Pango::Layout> layout = Pango::Layout::create (context);
__redraw (layout);
}
void
Text::__redraw (Glib::RefPtr<Pango::Layout> layout) const
{
#ifdef __APPLE__
if (_width_correction < 0.0) {
// Pango returns incorrect text width on some OS X
@ -115,6 +96,7 @@ Text::__redraw (Glib::RefPtr<Pango::Layout> layout) const
Gtk::Window win;
Gtk::Label foo;
win.add (foo);
win.ensure_style ();
int width = 0;
int height = 0;
@ -197,7 +179,7 @@ Text::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
}
if (_need_redraw) {
_redraw (context);
_redraw ();
}
Rect intersection (i.get());
@ -220,6 +202,9 @@ Text::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
void
Text::clamp_width (double w)
{
if (_clamped_width == w) {
return;
}
begin_change ();
_clamped_width = w;
_bounding_box_dirty = true;
@ -236,11 +221,15 @@ Text::compute_bounding_box () const
}
if (_bounding_box_dirty) {
#ifdef __APPLE__
const float retina_factor = 0.5;
#else
const float retina_factor = 1.0;
#endif
if (_need_redraw || !_image) {
Glib::RefPtr<Pango::Context> context = Glib::wrap (gdk_pango_context_get()); // context now owns C object and will free it
_redraw (context);
_redraw ();
}
_bounding_box = Rect (0, 0, min (_clamped_width, (double) _image->get_width()), _image->get_height());
_bounding_box = Rect (0, 0, min (_clamped_width, (double) _image->get_width() * retina_factor), _image->get_height() * retina_factor);
_bounding_box_dirty = false;
}
}

View File

@ -54,8 +54,8 @@ Gtkmm2ext::get_ink_pixel_size (Glib::RefPtr<Pango::Layout> layout,
{
Pango::Rectangle ink_rect = layout->get_ink_extents ();
width = (ink_rect.get_width() + PANGO_SCALE / 2) / PANGO_SCALE;
height = (ink_rect.get_height() + PANGO_SCALE / 2) / PANGO_SCALE;
width = PANGO_PIXELS(ink_rect.get_width());
height = PANGO_PIXELS(ink_rect.get_height());
}
void
@ -704,14 +704,28 @@ Gtkmm2ext::window_to_draw_on (Gtk::Widget& w, Gtk::Widget** parent)
int
Gtkmm2ext::pixel_width (const string& str, Pango::FontDescription& font)
{
Gtk::Label foo;
Glib::RefPtr<Pango::Layout> layout = foo.create_pango_layout ("");
Glib::RefPtr<Pango::Context> context = Glib::wrap (gdk_pango_context_get());
Glib::RefPtr<Pango::Layout> layout = Pango::Layout::create (context);
layout->set_font_description (font);
layout->set_text (str);
int width, height;
Gtkmm2ext::get_ink_pixel_size (layout, width, height);
#ifdef __APPLE__
// Pango returns incorrect text width on some OS X
// So we have to make a correction
// To determine the correct indent take the largest symbol for which the width is correct
// and make the calculation
//
// see also libs/canvas/text.cc
int cor_width;
layout->set_text ("H");
layout->get_pixel_size (cor_width, height);
width += cor_width * 1.5;
#endif
return width;
}