Whitespace at line-wrap spot preserved



Hi,

I'm doing some rendering with a PangoLayout, and it seems that when
wrapping is enabled the space at which wrapping is performed, is still
allocated physical horizontal space (for Latin languages, anyway).
This works okay for PANGO_ALIGN_LEFT layouts, but the empty space
causes a sort of ragged effect with PANGO_ALIGN_RIGHT, and off-center
calculations for PANGO_ALIGN_CENTER.

Is there some simple thing that I'm missing?

Example source is attached which paints the whitespace in a different
color, so that the effect is a little more visible.

Thanks,

Matt
#include <gtkmm.h>

#define LONG_TEXT "This is some really long text that should wrap to at " \
                  "least more than one line."

static bool
expose_cb (GdkEventExpose *event, Gtk::Widget *w)
{
    Gtk::Allocation alloc = w->get_allocation ();

    Glib::RefPtr<Pango::Layout> layout = w->create_pango_layout (LONG_TEXT);
    layout->set_wrap (Pango::WRAP_WORD_CHAR);
    layout->set_width (alloc.get_width () * PANGO_SCALE);
    layout->set_alignment (Pango::ALIGN_RIGHT);

    Pango::Rectangle rect = layout->get_logical_extents ();

    Glib::RefPtr<Gdk::Window> window = w->get_window ();

    // Paint spaces in a different color
    Pango::AttrList attr_list;
    Glib::ustring remaining_text = layout->get_text ();
    Glib::ustring::size_type loc = (Glib::ustring::size_type) -1;

    while ((loc = remaining_text.find_first_of (' ', loc + 1)) != (Glib::ustring::size_type) -1)
    {
        Pango::AttrColor attr = Pango::Attribute::create_attr_background (65535, 0, 0);
        attr.set_start_index (loc);
        attr.set_end_index (loc + 1);

        attr_list.insert (attr);
    }

    layout->set_attributes (attr_list);

    window->draw_layout (w->get_style ()->get_black_gc (),
                         alloc.get_x (),
                         alloc.get_y (),
                         layout);
    return false;
}

int
main (int argc, char *argv[])
{
    Gtk::Main loop (argc, argv);

    Gtk::Window w (Gtk::WINDOW_TOPLEVEL);
    Gtk::Label l ("");

    w.add (l);
    w.show_all ();

    l.signal_expose_event ().connect (sigc::bind (sigc::ptr_fun (&expose_cb), &l));

    loop.run (w);
    return 0;
}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]