RSyntaxTextArea icon indicating copy to clipboard operation
RSyntaxTextArea copied to clipboard

Avoid loading fonts when first creating an RSyntaxTextArea

Open Sothatsit opened this issue 3 years ago • 2 comments

Hello,

Currently whenever I create an RSyntaxTextArea it will always load its default font (Consolas on my system). This causes a brief stall when it has to load the font from disk. It would be nice if there was a way to avoid this.

Currently I use the following hack during startup to work-around this:

Style defaultStyles = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);
defaultStyles.addAttribute("FONT_ATTRIBUTE_KEY", new Font("Consolas", Font.PLAIN, 13));

However this just moves the stall to a more acceptable time. It still has to load the font, even though we don't use it.

I think one solution would be to make RTextAreaBase.getDefaultFont look in the UIManager first for its default font. It could look for a font using a key like "RTextArea.font", and then just fallback to its other font loading if that doesn't exist. That way we could set the default font universally, and avoid loading any other fonts that aren't needed.

Thanks for this great library!

Sothatsit avatar Jul 16 '21 03:07 Sothatsit

Sorry, just so I'm clear - there is a user-noticeable pause when Consolas is loaded in RTextAreaBase.getDefaultFont()? Is this true of any font, or just Consolas (or just how RTextArea initializes the font)? Why would setting it (presumably) early in StyleContext mitigate things?

Not that I don't believe you - just trying to understand the issue. Fonts really should load lightning fast these days!

Is your suggestion something like this?

public static Font getDefaultFont() {
  Font font = UIManager.getFont("RTextARea.defaultFont");
  if (font != null) {
    return font;
  }
  // Otherwise do what we currently do
}

bobbylight avatar Jul 20 '21 02:07 bobbylight

Sorry, just so I'm clear - there is a user-noticeable pause when Consolas is loaded in RTextAreaBase.getDefaultFont()? Is this true of any font, or just Consolas (or just how RTextArea initializes the font)? Why would setting it (presumably) early in StyleContext mitigate things?

Not that I don't believe you - just trying to understand the issue. Fonts really should load lightning fast these days!

Is your suggestion something like this?

public static Font getDefaultFont() {
  Font font = UIManager.getFont("RTextARea.defaultFont");
  if (font != null) {
    return font;
  }
  // Otherwise do what we currently do
}

I don’t think font loading speed would be a widespread issue, no. However, our file system can have a high latency as it loads files off of a shared file system. That shared file system is great for handling massive files, but it can cause hiccups when loading small files. Therefore, we try to avoid it as much as we can, as for us it causes a noticeable stutter.

Also yes, that’s exactly what I had in mind :) I think it’s a nice feature in general on top of our very specific use case, as it aligns the API with the Swing look and feel API.

Sothatsit avatar Jul 23 '21 08:07 Sothatsit