Pango get_extents returning the wrong widths



I am trying to use Pango with Cairo on a Windows 7 machine.
I am interfacing to this with Python using pyCairo.
My installation is via this file
pygtk-all-in-one-2.24.0.win32-py2.7
 
I have tried Verdana and Arial.
 
I am trying to write my own textbox control.  So I need to be able to take a piece of text and locate where the cursor should fall between every character.  So I have a function that finds every substring and then computes the length of each of these substrings.  So
'hello' -> ['h', 'he', 'hel', 'hell', 'hello']
 
I then get the widths of each of these substrings.
As the strings get longer, the widths become too big in progressive amounts.
For example if I have a string of all 1s.
The 1s will appear to be spaced 11 pixels apart.  But every few characters, a spacing of 12 will appear.
However, the extra pixel is not present in the actual output.  The extra pixels keep accumulating until the
length is many pixels off.
 
This is the code I use to get the widths with <self.label> being modified before each call to get_size()
 
def _create_layout(self, ctx):
        pctx = pangocairo.CairoContext(ctx)
        pctx.set_antialias(cairo.ANTIALIAS_SUBPIXEL)
        layout = pctx.create_layout()
        fontname = "%s %d %s" % (self.ctrl.font_face, self.ctrl.get_font_height(), Language.get_lang_code())
        font = pango.FontDescription(fontname)
        layout.set_font_description(font)
        layout.set_text(self.label)
        pctx.update_layout(layout)
        w, h = layout.get_extents()[0][2:]
        w /= 1000.0
        h /= 1000.0
        return pctx, layout, w, h-1
   
def get_size(self, ctx):
        pctx, layout, w, h = self._create_layout(ctx)
        return w, h
 
The actual output of the following functions is a mystery to me.
get_extents()
get_pixel_extents()
 
get_extents() returns -> ( (?, ?, w, h), (0, 0, w, h) )
get_pixel_extents() returns -> ( (?, ?, w1, h1), (0, 0, w2, h2) ) where w1 is very different from w2 with w1 being closer to correct.  w1 is roughly w/1000.  w2 appears really wrong.


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]