Re: Whitespace at line-wrap spot preserved



On 10/3/06, Damon Chaplin <damon karuna uklinux net> wrote:
On Tue, 2006-10-03 at 08:54 -0500, Matt Hoosier wrote:
> 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?

I noticed the same bug when working on justification for Pango.

My new layout code fixes this as well as adding justification:
  http://bugzilla.gnome.org/show_bug.cgi?id=64538



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

You forgot this.

Ah, right... this was a repeat posting of the message after I formally
joined gtk-i18n-list; the first one containing the attachment is stuck
somewhere in a moderation queue.

The demo is attached now.


Damon



#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]