How to display an utf-8 character in pygame? - python

Is there a way to render a tick mark using pygame? I tried using the unicode directly like this:
def write(screen, text, color, position, size):
font = pygame.font.Font(pygame.font.get_default_font(), size)# Defining a font with font and size
text_surface = font.render(text, True, color)# Defining the text color which will be rendered
screen.blit(text_surface, (position[0], position[1])) # Rendering the font
write(window, u'\u2713', self.color, position, size)
But this just draws a rectangle.

This 'rectangle' that is being written is in fact the representation of the ✓ sign using a pygame.font.get_default_font(). In order to display a ✓ sign you need to be sure, that this sign is included in a font that you are using. I propose the following solution:
Download the font that includes this symbol (e.g. Segoe UI Symbols from here)
Unzip the font package into the data folder (or main applicaation folder)
Change the font that you are using to the downloaded one
font = pygame.font.Font("seguisym.ttf", size)
Use a write function either using a write(window, u'\u2713', self.color, position, size) method or directly using write(screen, "✓", self.color, position, size)

Related

Pillow - detect if font contains glyph for a character

I'm drawing text into an image using a specific font, with Pillow for Python.
If the font I am using doesn't contain a glyph for a specific character, Pillow draws a ?. I'd like to get notified if the font is missing that specific item. Then I can either skip it or use a different font.
from PIL import Image, ImageDraw, ImageFont
image = Image.new("RGB", (100, 100), "white")
draw = ImageDraw.Draw(image)
font = ImageFont.truetype("unifont_upper-14.0.04.ttf", size=16)
draw.text((1, 1), "𒀀", font=font, fill="black")
image.save("cuneiform.png")
The specific font I'm using, doesn't contain "Cuneiform Sign A" so Pillow writes a ? - how can I get Pillow (or something else) to alert me that the character won't be drawn properly?

Pygame text displays wrong fonts

I am using pygame to make a game, and I want to display some text, but the font doesn't change to any fonts that I've downloaded. I am using a Mac and the font that I downloaded appears in Font Book. I've used pygame.font.init, define the font as font = pygame.font.SysFont('font name', size), and use font.render(message, False, colour).
When defining the font, I tried using both the absolute and relative paths. When I run the program, it displays the text in the default font instead of the font I have downloaded. Why does this happen?
Use pygame.font.Font() instead of SysFont to use downloaded fonts
Using this syntax:
font = pygame.font.Font("font name/path", size)
use the font.render() function to create a font surface you can display:
font.render(yourMessage, True/False, colour)

Python - Render a Slanted Font

This question is inline with the answer in my previous question in Stackoverflow.
I am creating a program that converts a text to image. I want to render it in using the font OCR A. But since OCR A font, has no corresponding font file for italics, I have to do the slanting of the upright font manually.
Upright Font
Slanted Font
Below is my initial code :
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
import numpy as np
#Returns the text size in terms of width and height.
def getSize(txt, font):
testImg = Image.new('RGB', (1, 1))
testDraw = ImageDraw.Draw(testImg)
return testDraw.textsize(txt, font)
text = 'CNN Font Recognition Model'
font_size = 12
font = ImageFont.truetype('ocra.ttf' , font_size)
#ocra.ttf is a font file for OCR A Regular font (I have to render it slanted)
width, height = getSize(text, font)
#Creates an image with white background of constant size.
img = Image.new('RGB',(width, height), 'white')
d = ImageDraw.Draw(img)
d.text((0,0), text, fill='black', font=font)
img.save('slanted_ocr_a.png')
How do I manually slant the upright OCR A font? Thanks.
You can download a sample ocra.ttf file here.
This answer to a similar question is probably the easiest way, although it is not exactly what you want to do. Basically you render the upright font into an image then slant (shear) the entire image using PIL's Image.transform method.
To accomplish exactly what you are asking (slanting the font before imaging it) is possible, but would require a lot of work overriding PIL's truetype scheme to have the underlying font engine (FreeType) perform the slant transformation at the font level. PIL's truetype interface simply doesn't have a way to express a transform, so you'd have to override/patch it to pass that all the way down to FreeType where the font is set up.
Another option might be to skip PIL's truetype scheme and use a Python font-rendering library that allows you to set the font transform directly, then scrape the pixel data from that into your PIL Image. One such library is freetype-py.

Replace Text On Pygame Window

I am making a score system on my python file. I tried using this:
font = pygame.font.SysFont(None, 25)
text = font.render('Score: '+str(score), True, black)
gameDisplay.blit(text, [0,0])
For some reason, the new score goes on top of the old score, and you can't read it. It makes a huge blob of numbers. Is there a way of replacing the text?
In your game loop, you have to clear what was previously drawn by filling the screen then painting:
gameDisplay.fill(your_color_tuple)
# afterwards add your drawing code

Python PIL using different font

I have an image I would like to add text to but I want to use both a different style font and a something bigger than the smallest thing known to mankind. Heck you can hardly read the text it's so darn small. I can print the text to the screen but I can't change the font style or the size of it as I don't know where the fonts are stored. I would think there would be more than one font/font size standardly available for use with Python/PIL. Where are they stored at? I am on a Linux.
Adapted from this answer:
image = Image.new("RGBA", (600,150), (255,255,255))
draw = ImageDraw.Draw(image)
font = ImageFont.truetype("resources/HelveticaNeueLight.ttf", fontsize)
draw.text((10, 0), txt, (0,0,0), font=font)
Looks like you can specify any .ttf file and any fontsize you want using the above modules and functions.

Categories

Resources