Centering Custom Fonts on Display

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
november_
 
Posts: 1
Joined: Fri Sep 30, 2022 6:32 pm

Centering Custom Fonts on Display

Post by november_ »

I'm using a helper function to center text on my 128x128 OLED display both vertically and horizontally that relies on the display.getTextBounds method. This function works perfectly with the standard default font that comes with the library.

I've used the fontconvert tool to enable use of the "Barlow Bold" font on my display.
Centering this new font works as expected horizontally, but the font will not center vertically on the display.

I am able to output the width and height of the rendered string with the new font, so it is able to calculate the bounds.
If I change the font to one of the GNU FreeFonts, the same issue occurs.

Is what I am trying to do something that is not allowed with the custom fonts? How can I get these strings centered vertically on the screen?
Any help is appreciated!

The sketch code I am using is below:

Code: Select all

// Include Libraries
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1327.h>

// Include Fonts
#include <Fonts/barlowblack8pt7b.h>
#include <Fonts/barlowblack24pt7b.h>

// Screen Dimensions in Pixels
#define screenWidth 128
#define screenHeight 128

// I2C Setup
Adafruit_SSD1327 display( screenWidth, screenHeight, &Wire, -1, 1000000 );

// Function to center text horizontally on the screen
void centerHorizontally( const String &textValue, int cursorXPos, int cursorYPos ) {
    int16_t x1, y1;
    uint16_t textWidth, textHeight;

    display.getTextBounds( textValue, 0, 0, &x1, &y1, &textWidth, &textHeight );
    display.setCursor( ( screenWidth - textWidth ) / 2, cursorYPos );

    display.print( textValue );
}

// Function to center text horizontally & vertically on the screen
void centerPerfectly( const String &textValue, int cursorXPos, int cursorYPos ) {
    int16_t x1, y1;
    uint16_t textWidth, textHeight;

    display.getTextBounds( textValue, 0, 0, &x1, &y1, &textWidth, &textHeight );
    display.setCursor( ( screenWidth - textWidth ) / 2, ( screenHeight - textHeight ) / 2 );

    display.print( textValue );
}

void setup() {

    /*
        #Initialization
    */
    Serial.begin( 9600 );
    //while (! Serial) delay(100);

    if (!display.begin( 0x3D )) {
        Serial.println( "Unable to initialize OLED" );
        while ( 1 ) yield();
    }

    display.clearDisplay();
    display.display();

    display.setTextWrap(false);
    display.setTextColor( 0xFFFF );

    /*
        #Gauge-Label-Text-Rendering
    */
    display.setFont( &Barlow_Black8pt7b );
    centerHorizontally( "AFR", 0, 16 );
    display.setFont();

    /*
        #Gauge-Value-Text-Rendering
    */
    display.setFont( &Barlow_Black24pt7b );
    centerPerfectly( "14.6", 0, 0 );
    display.setFont();

    display.display();
}

void loop() {

}

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”