def text_width(self, string, font):
            x = 0
            prev_char = None
            for c in string:
                if c < font.start_char or c > font.end_char:
                    if prev_char != None:
                        x += font.space_width + prev_width + font.gap_width
                    prev_char = None
                else:
                    pos = ord(c) - ord(font.start_char)
                    (width, offset) = font.descriptors[pos]
                    if prev_char != None:
                        x += font.kerning[prev_char][pos] + font.gap_width
                    prev_char = pos
                    prev_width = width

            if prev_char != None:
                x += prev_width

            return x