7

I have several fonts that I would like to use that are basically outlines of letters, but the insides are transparent. How would I go about filling only the inside areas of these fonts with with a color? I suspect it would be using the special blitting RGBA_BLEND modes, but I am not familiar with their functionality.

Here is an example of the font I am using: https://www.dafont.com/fipps.font?back=bitmap

Right now, I am simply rendering the font onto a surface, and I've written a helper function for that. Ideally I would be able to integrate this into my function.

def renderText(surface, text, font, color, position):
    x, y = position[0], position[1]
    width, height = font.size(text)
    position = x-width//2, y-height//2
    render = font.render(text, 1, color)
    surface.blit(render, position)

Thank you so much for any help you can give me!

  • I don't know if there's an easy solution for this problem. It would be pretty easy, if you also had an opaque version of the font. Then you could just render the opaque (colored) text first and afterwards render the transparent text and blit it onto the first surface. – skrx May 02 '18 at 20:14

1 Answers1

3

An option is to define a surface the size of the text, fill that with the color you want, and blit the text on that. For example you could do this:

text = font.render('Hello World!', True, (255, 255, 255)
temp_surface = pygame.Surface(text.get_size())
temp_surface.fill((192, 192, 192))
temp_surface.blit(text, (0, 0))
screen.blit(temp_surface, (0, 0))

This will create a temporary surface that should fill in the transparent pixels of a text surface. There is another option of using set_at() but its too expensive in processing power for what you are doing and is best used for pre-processing surfaces.

Im certain that a better option using BLEND_RGBA_MULT will come from a more experienced user. Im not too good at blending modes either.

Mercury Platinum
  • 1,549
  • 1
  • 15
  • 28