Mon 16 Apr 2012 09:57:19 AM UTC, comment #6:
This is turning out to be a little more complicated than I first thought.
Initially I believed that indexlib/commons.py:draw could be modified to use draw_utils:adjust_font_size before calling draw_text_center, which would've meant a nice short elegant patch.
It turns out this didn't scale the text the way I wanted and also didn't prevent the text from wrapping.
Then I tried saving the layout width, calling set_width(-1) before adjusting the font size - with a then slightly modified adjust_font_size that would scale down or up depending on an extra parameter. Sadly as soon as I restore set_width to its proper header width after changing the font size, the text wraps again like it did before.
Then I tried (from http://www.pygtk.org/docs/pygtk/class-pangofontmetrics.html) to get the approximate character width from a font metrics object, but soon found out that the PangoCairo context doesn't seem to have the required get_metrics method. Alas, foiled again.
I do have another idea to get the needed metrics to rescale the text properly:
draw_utils:draw_text and friends return the requisite information after drawing that could be used to scale with fd.set_size before making the draw_text_center call. Of course we don't want to draw the text twice, so I'm proposing this:
def draw_text(ctx, pc, layout, fascent, fheight,
baseline_x, baseline_y, text, pango_alignment, draw = True):
When draw == False, it omits the last two actual draw operations and just returns the size tuple.
Back in indexlib:draw it would first measure the result by calling draw_text with draw set to false. If the result overflows the target area, the font description is retrieved, the font scaled. Only then is draw_text_center actually called.
So sadly no patch to fix at least the overflowing single-line yesterday. I'll work on implementing the solution outlined above to see if it actually has the desired effect. I'll try and get this done today, inbetween or after work work.
I do realise it effectively doubles the number of draw calls for the index, but luckily this is a very small part of the overall run time. Also, given that get_metrics doesn't seem to work, I don't seen another way. Ideas/comments welcome!
|