2

How do I render a emoji in a string in python 3.6 using pygame(font.render(), font.freetype/font.font)? If not possible anymore, I need a code for render in Pillow Draw.text(). I would be grateful. Discard this: (Filling this question of nothing because looks like my post is mostly code, so I'm adding some nothings to enable the post)

# -*- coding: UTF-8 -*-

import pygame, emoji
import pygame.freetype

pygame.init()
screen = pygame.display.set_mode((640, 480))
clock = pygame.time.Clock()
done = False

pygame.font.init()
font = pygame.freetype.SysFont("segoe-ui-symbol.ttf", size=50)
#font = pygame.font.SysFont("segoe-ui-symbol.ttf", 72)
font_color = (255,255,255, 255)
font_background = (0,0,0, 0)

text_string = emoji.emojize('')
#text_string = u'ujiHelloÁpQ|, World '
#text_string = u'ujiHelloÁpQ|\U0001f44d, World '
#print(emoji.emojize('Python is :thumbs_up_sign:'))
#text = font.render("♛", True, font_color, (0,0))
#text = font.render(text_string, True, font_color, (0,0))
text = font.render(text_string, fgcolor=font_color, size=0)

image_size = list(text[0].get_size())
text_image = pygame.Surface(image_size)
text_image.fill(font_background)
pygame.display.flip()

text_image.blit(text[0], (0,0))

#print(dir(text_image))

while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
            done = True

    screen.fill((0, 0, 0))
    #screen.blit(text,
    screen.blit(text_image,
        (320 - text[0].get_width() // 2, 240 - text[0].get_height() // 2))

    pygame.display.flip()
    clock.tick(60)

1 Answers1

1

I see two possible problems here.

First, if nothing is showing up or the app seems to stall, then Pygame cannot find the SysFont. This was happening on Mac, because there is no segoe-ui-symbol.ttf as system font. As such, I downloaded a version from the internet and created a normal freetype.Font from it.

It works fine.

The second problem may be that the font itself doesn't have those characters in it. Sometimes the OS replaces characters that a font cannot render with a different font.

Ciprian Tomoiagă
  • 3,773
  • 4
  • 41
  • 65