Using fonts

edited August 26 in Python Mode

I've just started trying python mode, so I'm probably missing something very obvious, but essentially, my problem is that I can't seem to set the font. The code that pertains to my font looks something like this:

def setup (): font = createFont ("Numbers.ttf", width / 62)

def draw (): textFont (font, width / 62)

However, this gives me an error saying that font is not defined. I assumed that I couldn't use font outside of setup, so I tried putting "font = None" at the beginning, so that it would be a global variable, but then it just says that a null font was passed to textFont. I also can't put "font = createFont ("Numbers.ttf", width / 62)" at the beginning, because it must be in setup, and putting textFont in setup doesn't seem to work either. What am I doing wrong?

Answers

  • edited August 26

    Well I sort of got it to work, using the create font tool, loadFont, and putting textFont in setup, but I would still like to know if it's possible to use a ttf, because from my understanding, ttf works better if the font will be scaled, and I do plan on scaling.

  • edited August 26

    https://Forum.Processing.org/two/discussion/15473/how-to-format-code-and-text

    In Python, when we assign a value to a variable, it's assumed to be local to its function, unless we declare it as global. :-B

    https://Py.Processing.org/reference/globals.html

    def setup():
        global font
        font = createFont('Numbers.ttf', width/62, True)
    

    But in case 'Numbers.ttf' is the only font you're gonna need in your whole sketch, you can simply use textFont() right there within setup(): :ar!

    def setup():
        textFont(createFont('Numbers.ttf', width/62, True))
    

    No need for any global font variable at all this time! :)>-

  • Answer ✓

    Well now I feel really dumb. I just realized that "numbers.ttf" is not capitalized...

Sign In or Register to comment.