On 23.07.2011 00:24, W Wlourf wrote:
> I'm looking for an example for drawing a text with oocairo. Is it possible ?

Let's look at what awesome is doing (wibox/widget/textbox.lua):

The "magic" starts in the function draw(). I'll inline all the various functions
here, in the code there is more than one function, because similar code is
needed for getting the text extents:

local l = oopango.cairo.layout_create(cr) -- We are drawing to this context
l:set_alignment("left") -- Align left (not right or center)
l:set_ellipsize("end") -- Shorten the text at the end if too long
l:set_wrap("word_char") -- Try wrapping at words, but use chars if necessary
l;set_width(width) -- Available width for the text
l:set_height(height) -- Available height
l:set_font_description(beautiful.get_font("font name here, leave empty for 
default")
if markup then
   l:set_markup(text)
else
   l:set_text(text)
end

[Here comes some magic to align text vertically, can be found at the end of
setup_layout() if you really care]

oopango.cairo.update_layout(cr, layout)
oopango.cairo.show_layout(cr, layout)

And the text is drawn. :-)

All the various set_foo() calls can be skipped to use the defaults. So if you
just want to say "Hi":

local l = oopango.cairo.layout_create(cr)
l:set_text("Hi")
oopango.cairo.update_layout(cr, layout)
oopango.cairo.show_layout(cr, layout)


The difference between set_markup and set_text is that set_text doesn't
interpret pango markup. This means that <b>Hi</b> would be drawn as-is by
set_text() while set_markup() would show "Hi" in bold.

For the other set_foo() calls, check out the pango documentation:

http://developer.gnome.org/pango/stable/pango-Layout-Objects.html

This is what beautiful.get_font() basically does:

return oopango.font_description_from_string(name)

This is pango_font_description_from_string() in the C API:

http://developer.gnome.org/pango/stable/pango-Fonts.html#pango-font-description-from-string


I hope this helps you with drawing text with oopango. May I ask what you are
trying to do?

Uli

P.S.: There was an API change in oopango. Your version might use
oopango.cairo_update_layout and oopango.cairo_show_layout instead of the above
version with an extra dot.

P.P.S.: You asked about drawing text with oocairo. The cairo devs say their text
API is only a toy and shouldn't be used, because it got lots of shortcomings. If
you *really* want to draw text with oocairo, use:
  cr:show_text("Hi")

P.P.P.S: All the text drawing uses the current point for the text position. So
use cr:move_to(x, y) for positioning the text.

-- 
- Buck, when, exactly, did you lose your mind?
- Three months ago. I woke up one morning married to a pineapple.
  An ugly pineapple... But I loved her.

-- 
To unsubscribe, send mail to awesome-unsubscr...@naquadah.org.

Reply via email to