1

The Problem: I cannot display the diacritics ('Umlaut points') in German text when they're upper case.

I would like to display a word like 'Übung' (Practice/Exercise in Engl.) But the tops gets cut off resulting in the the two dots not being displayed but comes out as 'Ubung'.

It cannot be the encoding since lower caser 'ü' does get displayed property. Trying to display 'Üü' results in 'Uü'.

I have this MWE from 'Invent your own Computer Games with Python', Chapter 17.

import pygame
from pygame.locals import *

# Set up pygame.
pygame.init()

# Set up the window.
windowSurface = pygame.display.set_mode((500, 400), 0, 32)
pygame.display.set_caption('Hello world!')

# Set up the colors.
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)

# Set up the fonts.
basicFont = pygame.font.SysFont(None, 48)

# Set up the text.
text = basicFont.render('Hello Übungswörld!', True, GREEN, BLUE)
textRect = text.get_rect()
textRect.centerx = windowSurface.get_rect().centerx
textRect.centery = windowSurface.get_rect().centery

# Draw the white background onto the surface.
windowSurface.fill(WHITE)

# Get a pixel array of the surface.
pixArray = pygame.PixelArray(windowSurface)
pixArray[480][380] = BLACK
del pixArray

# Draw the text onto the surface.
windowSurface.blit(text, textRect)

# Draw the window onto the screen.
pygame.display.update()

# Run the game loop.
while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

OS: MacOS 10.14.4 Fonts tried: System font (None), 'Palatino', 'helveticattc', 'comicsansmsttf'.

The odd thing is when I run this code in PyCharm, ComicSans ('comicsansmsttf') will render Ü but Helvetica and Palatino won't display the dots.

Mat
  • 515
  • 5
  • 10
  • 1
    Could be useful to add what OS are you using. You are using the default font of your system, maybe that font does not support dieresis on capital letters. – Valentino May 08 '19 at 13:02
  • 1
    Not sure will be helpful, but may be worth having a look here: https://stackoverflow.com/q/7535498/10426037 this question discuss the vertical alignment of the font, if you believe that the problem is that the dots are cut out from the rectangle. – Valentino May 08 '19 at 14:44

1 Answers1

2

The problem is not the rect of the text. pygame.font.Font.render() automatically set the surface size, no way it cuts part of the letters.

It's likely that the default pygame font (the one used when you pass None to SysFont) does not support dieresis on capital letters.

Try to pass a different font, such as Arial or Times New Roman.

basicFont = pygame.font.SysFont("Arial", 48)

I tried with Arial and Times New Roman. Both show the dieresis on the capital letter, but you can choose a different one of course, as long as it is installed on your system.

For more info:

  • pygame.font.get_default_font() tells you which is the default pygame font.
  • pygame.font.get_fonts() returns a list of fonts available for pygame.
Valentino
  • 7,291
  • 6
  • 18
  • 34
  • You're right – not all fonts work. Another issue is that I've been running my MWE in Pycharm and the actual code I was working on in Idle. Thanks! – Mat May 08 '19 at 14:51