Pygame text displays wrong fonts - python

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)

Related

How to display an utf-8 character in pygame?

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)

Specifying a Font from a file location ptext in pygame

I understand how to use a font from the .ttf file in normal python.
font1 = pygame.font.Font("/resources/fonts/robotoThin.ttf")
but I could not find in the documentation of ptext, or on the web how to specify the font when using
ptext.draw(text,(x,y))
Using pycharm
According to the ptext repo's README, you can specify the fontname parameter in your call to ptext.draw like so:
ptext.draw(text, (x,y), fontname="fonts/Viga.ttf")
There are also options for creating templates for the fontname (see the link above) but I'm not seeing a way to save the font as a variable to avoid reloading it every time.

Get font file from font family

Having a difficult time trying to get the proper .ttf file from the font family.
Currently trying to use pygame to work on some graphics and it has its own internal search function to find a font file from the Font Family but it's not correct.
Also tried using matplotlib.font_manager. This also is not correct.
import matplotlib.font_manager as font_manager
import pygame
pygame.font.init()
print(font_manager.findfont('Segoe UI'))
> C:\WINDOWS\Fonts\seguisb.ttf
print(pygame.font.match_font('Segoe UI'))
> C:\WINDOWS\Fonts\segoeuil.ttf
print(font_manager.findfont('Arial'))
> C:\WINDOWS\Fonts\arial.ttf
print(pygame.font.match_font('Arial'))
> C:\WINDOWS\Fonts\ARIALN.TTF
I'm hoping there's something I missed and there is a way to get a complete match on the search, not a partial match (I'm guessing both methods used are reading the byte array and returning the closest matching font based on the Family. I.e., 'Segoe UI' is in 'Segoe UI Bold' so it was returned as the match).
I recommend to use pygame.font.SysFont():
Return a new Font object that is loaded from the system fonts. The font will match the requested bold and italic flags. Pygame uses a small set of common font aliases. If the specific font you ask for is not available, a reasonable alternative may be used. If a suitable system font is not found this will fall back on loading the default pygame font.

What's the name of this font used in this figure?

I want to use this font in matplotlib plotting, but I can not find out the name. Does anyone know?
This figure is got by IDL plotting on Mac OS (10.9) like:
filename = 'name.eps'
myDevice = !D.NAME
SET_PLOT,'ps'
DEVICE,DECOMPOSED=1,ENCAPSULATED=1,/COLOR,FILENAME=filename
......
DEVICE, /CLOSE
SET_PLOT, myDevice
I'm not a font expert, but this looks a lot to me like the font that was used with pen plotters. Looking around for "pen plotter font", turns up "Hershey Vector Font", which looks quite close.
The font is indeed a Hershey vector font: Hershey Simplex Light. This user on Github has the font definition files (otf) that you can copy/clone into your system and set as the default fonts in your matplotlibrc. Not all the characters are included, but you can use fontforge to open the otf and/or sfd files and merge/copy/create missing glyphs.
(updated in 2022)
I think this font is a kind of vector font belonging to the Hershey family.
Here is a set of TTF fonts that are based on the Hershey font https://github.com/yangcht/Hershey_font_TTF.
You can download them and load them when you make plots.

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