Add a drawTextScaled(...) API (see code example)

I'm sure the devs have already considered this but hear me out. I think there's room to offer some kind of utility for drawing scaled (in particular larger) text. Perhaps with some restrictions.

I've been using the following extension (with "font/Mini Sans 2X") which granted I haven't made fully generic, but it's been working great for 1.5x and 3x scales.

function playdate.graphics.drawTextScaled(text, x, y, scale, font)
    local padding = string.upper(text) == text and 6 or 0 -- Weird padding hack?
    local w <const> = font:getTextWidth(text)
    local h <const> = font:getHeight() - padding
    local img <const> = gfx.image.new(w, h, gfx.kColorClear)
    gfx.lockFocus(img)
    gfx.setFont(font)
    gfx.drawTextAligned(text, w / 2, 0, kTextAlignment.center)
    gfx.unlockFocus()
    img:drawScaled(x - (scale * w) / 2, y - (scale * h) / 2, scale)
end

gameover
(the numbers here are using the above function with 3x scale)

Otherwise, hopefully this helps the random stranger who stumbles upon this post.

Thoughts? Thanks!

6 Likes

I've handled that manually too, similarly to what you did, and it works fine--but FWIW here's another use case for font scaling: an intentionally pixellated low-res font.

I have made my "1UP" font out of 2x2 chunks of pixels, so it's ALWAYS scaled 200% by nature. Some fonts could use even lower res than that, as a stylistic choice. But it would be easier create such a font at 1x with "normal pixels."

(And if I had it do over, I would! But 1UP exists now and works fine at 2x, so I'm leaving it alone.)

I am a newbie, so this was EXACTLY what i was looking for. Thank you so much!
But i just don't get what kScoreFont is..I even looked up for it in documentation. Maybe I'll figure it out soon

@Patanatomiya Ah sorry, kScoreFont should have just been font (I updated the sample code to reflect the change).

Then you can call it like this (on second thought, it's probably not safe to put the function inside playdate.graphics though, incase the SDK eventually includes an API with the same name that could cause issues, but anyway...)

local kScoreFont <const> = playdate.graphics.font.new("font/Mini Sans 2X")
playdate.graphics.drawTextScaled("Hello", 100, 100, 3, kScoreFont)
1 Like

Oh thank you very much! I thought it may be some kind of constant, that I'm not familiar with yet cause I'm just a beginner. I appreciate your answer, it really helped me