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> = kScoreFont:getTextWidth(text)
    local h <const> = kScoreFont: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!

1 Like

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.)