Pyraylib not loading custom font

I am trying to load custom font but it doesn’t load at first i tried this code

from pyray import *
init_window(900,900,"Hello")
font = load_font_ex("main.ttf", 32, 0, 250)

while not window_should_close():
    begin_drawing()
    clear_background(WHITE)
    draw_text_ex(font, "Hello", Vector2(100, 100), 68, 10, BLACK)
    end_drawing()

It throwed the error font = load_font_ex("main.ttf", 32, 0, 250) File "/home/himanshu/.local/lib/python3.10/site-packages/pyray/__init__.py", line 76, in wrapped_func raise TypeError( TypeError: Argument must be a ctype int, please create one with: pyray.ffi.new('int *', 1)

Then I tried the below thing

from pyray import *
import pyray
from raylib import *

init_window(900,900,"Hello")


font_size = pyray.ffi.new('int *', 32)
font_style = pyray.ffi.new('int *', 0)
font_spacing = pyray.ffi.new('int *', 250)

font = load_font_ex("main.ttf".encode(),font_size, font_style, font_spacing)
while not window_should_close():
    begin_drawing()
    clear_background(WHITE)
    draw_text_ex(font, "Hello", Vector2(100, 100), 68, 10, BLACK)
    end_drawing()

then it throws this error

I want to just load a simple ttf file which is clear and not blurr because when i tried on windows it was blurr. someone please help

Hi,

first, begin by not importing libraries open-ended. That is, don’t import libraries using the * sign. The reason is that you want to avoid potential race conditions between the objects being imported from libraries and names already in your current program/script.

At the top, use the following:

import pyray as pr
import raylib as rlib

Then, update your code to account for these changes.

From the error generated, there is an issue with the following line:

font = load_font_ex("main.ttf".encode(),font_size, font_style, font_spacing)

From documentation:

Thus, the load_font_ex class/function takes in four argument types:

  1. str
  2. int
  3. any
  4. int

A quick way to check that the correct argument types are being passed in is to include some temporary print statements to verify the types generated by the font centric variables:
print(font_size); print(type(font_size))
print(font_style) ; print(type(font_style))
print(font_spacing) ; print(type(font_spacing))

I also referenced the library here:
https://electronstudio.github.io/raylib-python-cffi/pyray.html#pyray.load_font_ex

I did not find a reference for ffi. Can you double check that this is a valid attribute and that your code is using the correct variable names.

Regarding the first argument passed in, is the .encode() necessary? Can you try only passing in main.ttf?

UPDATE:

Here is an example that I found on GitHub:

It is similar in form to the original example that you provided. Test it to see if it works and see if you can modify it to suit your needs.

1 Like

Right. That doesn’t mean every argument. It means whichever argument was the first one to be affected by the problem. I.e., at least one, possibly others. An exception can really only ever tell you about one problem at a time.

To know which argument, we need to read the documentation.

thanks this worked i think the issues was draw_text_ex and with that i need not use pyray.ffi.new('int *', some_number) this is the final code that gave the desired output

import pyray as pr, raylib as rl, os

rl.SetConfigFlags(rl.FLAG_WINDOW_RESIZABLE)
pr.init_window(800, 450, "Hello")

font = font = pr.load_font_ex((os.getcwd()+"/main.ttf").encode(),30,None,0)

while not pr.window_should_close():
    pr.begin_drawing()
    pr.clear_background(pr.WHITE)
    pr.draw_text_ex(font, "hellow font", (300, 300), 30, 0, pr.BLACK)
    pr.draw_text("Hello world", 190, 200, 20, pr.VIOLET)
    pr.end_drawing()
pr.close_window()

But one issue still persists is the blurry font when the size is set to 90 or something greater which is clearly visible by user.

UPDATE :
i found that i must load the text in higher size e.g. 200 and then draw_text_ex in lower size e.g. 90 . This solved the blurry text problem

1 Like