Skip to content

Separate font system from text rendering #52

@elongatedsquid

Description

@elongatedsquid

A suggestion to separate font handling from drawText to PixelWindow.

Currently, the drawText method creates a new font on every call, fonts in java are allocated on the heap, which is generally quite slow, so to de-allocate and allocate the same object over and over seems wasteful.

The suggestion then is to move font handling from drawText to PixelWindow in the form of three methods, a change to positioning, and possibly a new class member.

The suggested methods are loadFont, getFont, and measureText.
loadFont is for loading a font, taking parameters - name, style, size.
getFont is for getting the currently loaded font, this could be done using either canvas.g.getFont or a class member called font.
measureText is for helping with correctly aligning text, it would take in a size and give out a tuple of (width, height). Currently when drawing text it draws at (x, y + size), which for me was a bit counter-intuitive when trying the lib out preparing for the course.
The expected thing is to manually need to offset so when it is done for you some spacing calculations get weird. Also, the size property is font size, not pixel size so that is more reason for a real measureText method since the font size doesn't always corrolate 1:1 to pixels.

It is important to note that changing size within drawText should still be possible, both to keep compatibility in BlockGame but also for convenience.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions