Skip to content

Display.text_pixel and Display.text_grid laggy with non-default font. #600

Closed

Description

@fractal13
  • ev3dev version: 4.14.80-ev3dev-2.3.1-ev3
  • ev3dev-lang-python version: 1.2.0, 2.0.0~beta2

When drawing text to the display, each Display.text_*() call with a font string specified takes nearly 0.5 seconds to execute. This is problematic for programs that want to use the screen for status updates, while time sensitive actions are happening, such as driving while watching for a color sensor value to change, or a button to be pressed, etc.

I was able to trace the lag to the loading of the font file. I hacked a local change that directly uses the ImageDraw object methods to load the font once, and use it directly when calling the ImageDraw.text() method. This eliminated the large lag.

I don't have time right now to create a pull request with a proposed change, but wanted to document this in case anyone else sees the lag. I would also like to know if the authors are willing to consider a change that would allow a "current" font to be loaded and cached as part of the Display() object so that the Display.text_*() calls can still be used? Or maybe, there is a different mechanism that would fit better with your overall design?

Thanks for a great system.

Activity

  1. WasabiFan commented on Mar 20, 2019

    @WasabiFan
    Member

    Thanks for reporting! We'd definitely be interested in such a change (at least, for the v2.0 series). If there's an API change that would make this better we can consider it; the v2.0 is still labelled as beta but the next release probably won't be, so if we're going to make breaking changes we should do it now. Alternately, is this a reasonable place for some caching logic? Perhaps a table of recently used fonts?

    I'll have to take a look at the APIs here and see how it works. I recall some of our APIs for the display could accept a pre-loaded font object — is that true here?

  2. ddemidov commented on Mar 20, 2019

    @ddemidov
    Member

    I think it would be enough to add check for isinstance(font, str) here:

    if font is not None:
    assert font in fonts.available(), "%s is an invalid font" % font
    return self.draw.text((x, y), text, fill=text_color, font=fonts.load(font))
    else:
    return self.draw.text((x, y), text, fill=text_color)

    and only use fonts.load() if that is the case. Something like:

    if isinstance(font, str):
        font = fonts.load(font)

    Then users could pass preloaded fonts into the function.

  3. fractal13 commented on Mar 20, 2019

    @fractal13
    ContributorAuthor

    I like this strategy. I created a pull request with the change implemented. I tested it with a font string and with a preloaded font and obtained the expected behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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