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

Re: [pygame] freetype module



Hi Marcus,

On 13/01/11 11:13 PM, Marcus von Appen wrote:
On, Fri Jan 14, 2011, Lenard Lindstrom wrote:

Hi everyone,

A few months back I had a look at the new freetype pygame module. It is
meant to replace the SDL_font based font module, and adds new features
such as text rotation and full Unicode character support. Unfortunately
it also has a serious bug in determining the text bounding rectangle
when the text contains a mix of character with ascenders and descenders.
The bug can be solved by storing the relative text bbox width and
height.

I'm not sure how you get the bboxes. I could only get reliable bounding boxes by calling FT_Glyph_Get_CBox on loaded and transformed glyphs. At this point much of the work is done, with just rendering and blitting left. So the freetype.get_size method becomes extra expensive. Saving transformed glyphs would add extra complexity to the cache mechanism. So the architecture of the freetype module class(es) will need to be rethought.

This can cause a Python interpreter crash.
I did not encounter that so far. Can you provide an example for that?

The render_raw method crashed on "gh" when returning a bytes string. Byte strings have less padding so the python instance and malloc headers are corrupted. The boundary problem is not noticeable when rendering to an existing surface with render. But the attached chop.py program displays "gn", chopping off the h's accender. I would have tried this with pygame2, but only have SDL_mixer 1.2.8 available for Debian squeeze. (The pygame2 build fails with these errors:

src/sdlmixer/constantsmod.c: In function ‘initconstants’:
src/sdlmixer/constantsmod.c:72: error: ‘MIX_INIT_FLAC’ undeclared (first use in this function) src/sdlmixer/constantsmod.c:72: error: (Each undeclared identifier is reported only once
src/sdlmixer/constantsmod.c:72: error: for each function it appears in.)
src/sdlmixer/constantsmod.c:73: error: ‘MIX_INIT_MOD’ undeclared (first use in this function) src/sdlmixer/constantsmod.c:74: error: ‘MIX_INIT_MP3’ undeclared (first use in this function) src/sdlmixer/constantsmod.c:75: error: ‘MIX_INIT_OGG’ undeclared (first use in this function)
error: command 'gcc' failed with exit status 1

And I do have the development packages installed.)

In the pygame freetype branch I added a method which correctly
calculates a bounding rect. But it uses a different approach than
pygame.freetype. The fix may require a significant rewrite. So it is
worthwhile to consider other possibilities. One would be to wrap pango,
then all formatting woes are taken care of. A pango for SDL library
already exits -http://sourceforge.net/projects/sdlpango  - but nothing
has been done with it for over a year. I don't suggest using it
directly, but rather as a template for writing Pygame's own wrapper as
an extension module. Using pango is just an idea. It may turn out
building pango on Windows is too involved to be practical. Any thoughts?
Pulling in pango means pulling in Glib20 and friends. Personally I would
not recommend that.

Noted, along with Stas Zytkiewicz's post. Wrapping pango can go on the "Things to *not* do" list.

Lenard Lindstrom

import pygame
display = pygame.display.set_mode((300, 200))
display.fill(pygame.Color('white'))
pygame.display.flip()

import pygame.freetype
pygame.freetype.init()
text = "gh"
f = pygame.freetype.Font(None)
s, r = f.render(None, text, pygame.Color('black'), ptsize=90)
display.blit(s, (10, 30))
pygame.display.flip()

while (1):
    e = pygame.event.wait()
    if e.type == pygame.QUIT:
        break
    if (e.type == pygame.KEYDOWN and 
        e.key == pygame.K_ESCAPE     ):
        break
    pygame.display.flip()
pygame.quit()