Hi,
On 31.01.2013 23:35, Vasya Belkin wrote:
> Hi, is it possible to find out text extents, e.g. width/height of string?
> Because the following code gives segmentation fault & I'm not sure, whether
> this is a bug or not:
>
> local lgi = require 'lgi'
> local cairo = lgi.cairo
> local cr = cairo.Context(nil)
> cr:text_extents("Hello")
I don't think it is a good idea to pass in nil as the surface to draw to.
However, this still crashes when given a proper cairo surface (at least with
latest lgi release):
https://github.com/pavouk/lgi/issues/37
> This is an example snippet, but the very same behaviour is observed in draw
> function of real widget.
Why are you using cairo's toy font API anyway? Text should be drawn through
Pango and querying the text extents here does work fine. Look at
/usr/share/awesome/lib/wibox/widget/textbox.lua for some examples.
> Actually I'm trying to create a button with both image (centered) and text
> (at the bottom), so if somebody has an implementation, please, share it.
Big hack:
local foo = wibox.widget.base.make_widget()
local img = wibox.widget.imagebox()
local txt = wibox.widget.textbox()
img:set_image("/tmp/foo.png")
txt:set_text("foo")
txt:set_valign("bottom")
foo.fit = function(foo, width, height)
local wi, hi = img:fit(width, height)
local wt, ht = txt:fit(width, height)
return math.min(wi, wt), math.min(hi, ht)
end
foo.draw = function(foo, wb, cr, width, height)
wibox.layout.base.draw_widget(wb, cr, img, 0, 0, width, height)
wibox.layout.base.draw_widget(wb, cr, txt, 0, 0, width, height)
end
This creates a new widget which just paints the img and txt widgets in the same
spot. Centering the imagebox might need some more magic since this code should
always draw it at the top, but I bet you'll manage to come up with something
based on this. :-)
Cheers,
Uli
--
- He made himself, me nothing, you nothing out of the dust
- Er machte sich mir nichts, dir nichts aus dem Staub
--
To unsubscribe, send mail to [email protected].