GFX library background text color?

Does anyone know what is GFX's classic built-in font that the background color fill works with?

Adafruit GFX Library

.setTextColor(uint16_t c, uint16_t bg)

I've tried using these fonts that are listed in the Fonts folder and none of them work with setting the background color of the text.

#include "Fonts/FreeSans12pt7b.h"
#include "Fonts/FreeSans18pt7b.h"
#include "Fonts/FreeSans24pt7b.h" 
#include "Fonts/FreeMonoBold12pt7b.h"
#include "Fonts/FreeMonoBold18pt7b.h"
#include "Fonts/FreeMonoBold24pt7b.h"
#include "Fonts/FreeMono9pt7b.h"

It is always a good idea to check the library documentation.

A quick glance at the extensive, very detailed and complete GFX library documentation reveals

One “gotcha” to be aware of with new fonts: there is no “background” color option…
you can set this value but it will be ignored.
This is on purpose and by design.

The default "classic" font is stored in the library file glcdfont.c, and is selected by calling setFont() with no argument, or NULL. The font is based on Code Page 437, the font used by the original IBM PC.

If you read over the link from @jremington, the documentation mentions a couple of ways to blank out the previous text in the background color before writing new text. It does not mention a very simple method, save the previous text that was written to the display, then re-write that in the background color before writing the new text.

1 Like

I don't consider this as a "very simple method".
Say, if you use a half of dozen texts in different screen areas - you will need to store them all for a future rewrites.
To draw a rectangle with a background color seems to be a much suitable.

This seems pretty simple, and is what I use (from the docs):

To replace previously-drawn text when using a custom font, either:

Use getTextBounds() to determine the smallest rectangle encompassing a string, erase the area using fillRect(), then draw new text:

2 Likes

We all have our preferences.

I tend to get a bit more flicker when drawing a rectangle than when overwriting the old text in the background color.

Using getTextBounds() has the disadvantage that you still need to either save the old text, or save the bounds of the old text, when using a proportional-spaced font. A bit less complex to just overwrite using a fixed size rectangle for the largest possible text area.

1 Like

Thanks @david_2018, initially I just wanted to know what the default was so I can see if it is applicable for my application... which it is not. Also, the background fill of the default font is not so great either. It kind of starts exactly at the same pixel of the font rather than encompassing the font.

Have you guys tried the drawBitMap function with some of the fonts available in the font's library to stop the blink as stated in the GFX library documentation. So far it works, but I cannot seem to get the font any bigger. Even when I change text size.

// In global declarations:
GFXcanvas1 canvas(240, 320); // 240x320 pixel canvas

// In code later:
tft.setFont(&FreeMono9pt7b);
tft.setTextSize(1);
canvas.println("Loading...");
tft.drawBitmap(30, 155, canvas.getBuffer(), 240, 320, ST77XX_WHITE, ST77XX_BLACK);

Changing the size supported for default font only. All custom fonts has a fixed sizes. If you need a bigger text - you need a bigger font.

The Adafruit GFX library has most of the font files in 9, 12, 18, and 24pt size. If you need another size, there is a folder with a fontconvert utility that can be used to generate the font in any size you want, you just need the source font ttf file.
Be careful, large fonts take a lot of memory.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.