Monday, 26 October 2009

Argh. My first snag with my lovely Lua/Cairo!

So I've been working on making a lovely little eye-candy calendar widget to display with my Conky Widgets script, but I ran into a snag...Here's the best version I got before giving up (for now!):
From My Little Desktop Photos
Can you spot the annoyingness yet?

The text is misaligned!

As it turns out, Cairo draws text from an anchor point roughly at the lower left corner of the text being displayed. In this code, the month name/year, days of the week and day numbers are all evenly spaced on a square grid, but they look uneven because they are anchored at that lower-left position.

What I really want is for each element to be centered in a block. So the month/year is centered in a block that spans the top of the widget, and each of the day numbers is centered in a small square, like on a printed calendar.

However, in Cairo, you can't change where a text element is anchored, but you CAN use a function called cairo_text_extents() to give you some size information about the text block you want to manipulate, and then move to an appropriate start point. (See the Cairo tutorial for a more detailed explanation.)

I must have spent hours trying to get this function to work before I finally gave in and asked the Conky devs...and got this response: "tolua++ doesn't export any creation functions (or constructors) for C structures, only C++ classes." Basically, the third argument in the cairo_text_extents() function is of the structure cairo_text_extents_t, which isn't recognised by Lua.

All this roughly translated: as it stands now, we can't do any sophisticated text manipulation using Cairo in Lua. Booooo.

So the calendar is going to one side for the time being. Incidentally, if you would like to grab the Lua code for this widget, here you go.

3 comments:

  1. I wonder what about Pango bindings to Cairo bindings to LUA? ;P
    Maybe that way we could have sofisticated text routine in ConkyLUACairo?

    ReplyDelete
  2. +1 for Pango. I wanted to do text-on-a-path in Conky, but without Pango, it's not doable.

    ReplyDelete
  3. Whenever this happened to me, I just switched the font used to display the days of the week and week numbers to a monospaced font, and added leading spaces to single-character days and week-day letters.

    ReplyDelete