Centre Text Anomaly

I have a really frustrating issue...

I am using the adafruit GFX lib with SH1106 driver, and use the centre text helper function shown here previously:

void drawCentreString(const char *buf, int x, int y)
{
    int16_t x1, y1;
    uint16_t w, h;
    display.getTextBounds(buf, x, y, &x1, &y1, &w, &h); //calc width of new string
    display.setCursor(x - w / 2, y);
    display.print(buf);
}

If I send it a day of the week held in a cont char array:

drawCentreString(days_full[tm.tm_wday],64,0);

works fine... text is 'counted' as 42.

However, when I try to use something that is not a const char - like:

drawCentreString("23rd September",64,10);

it places it at the extreme LH side of the display - and 'counts' the text as 124.

(What I want to print is a char array constructed to be the date and month. )

Tearing my hair out!

Any thoughts (please....)

Andy

You might need to change the w and h to int16_t as

display.setCursor(x - w / 2, y);

has mixed signed and unsigned math.

Have you set the compiler warnings on?

What does the compiler report?

Does that text fill the entire line of the display?

Thank you.... compiler gives no errors.

I will try what you suggest, but wht is strange is that it seems to work perfectly when a const char is used, but gives an incorrect width with an ortinary char array....

Hi David - no its just left justified, as if I hadnt called drawCentreString at all.....

Try ending this with a null, for example, "23 September\0"... something is stepping on the "x" value, making it a zero (the left position of your boundary box), causing the "left-justify."

And, maybe try a shorter array of characters, like "23 May" (without "\0") to see if the first parameter has limited space for characters.

Hi XFPD....

So terminating with \0 made no difference.

BUT trying with "23rd" centered perfectly.

So ... why? is there a limit on getTextBounds?

I cant find anything....

The thread title is "Centre Text Anomoly" .
Did you mean "anomaly" instead?

Guessing: Name too long?

Try shortening "September" and all month names, down to three, like "SEP", "MAY", "JUN", "JUL"... and my guess for this is "over-run of array length"... in the particular font you are using (the library probably knows or counts the with of each character)

You can dig into the library to see how it does the math for aligning the boundary box.

For what day of the week did the function return 42?

Hi b707 - Both Sunday and Monday....

I think the @xfpd guess is correct. The weekdays names are 6 chars only and returns 42. Your date string is a 14 chars long and does not fit on half of the 128 pixel screen.

To resolve the issue you need to calculate a text width by placing it on a whole width screen

void drawCentreString(const char *buf, int x, int y)
{
    int16_t x1, y1;
    uint16_t w, h;
    display.getTextBounds(buf, 0, y, &x1, &y1, &w, &h); //calc width of new string
    display.setCursor(x - w / 2, y);
    display.print(buf);
}

note the parameter 0 in the third line of the function

Try Wednesday... and if that causes the left-justify, simplify all month and weekday names to three letters. MON, TUE, WED, THU, FRI, SAT, SUN, JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC.

Or change font.

My first thought is that when I'm having a problem figuring something, I don't show a snippet that omits the context in which the problem is occurring.

If I show a sketch that illustrates the problem happening, others can try it for themselves and see if they see the same issue.

For example:

#include <Wire.h>
#include <Adafruit_SH110X.h>

#define i2c_Address 0x3c

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SH1106G display = Adafruit_SH1106G(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

void setup() {
   const char buf[] = "23rd September";
   
   Serial.begin(115200);
   delay(250);
   display.begin(i2c_Address, true);
   display.setTextSize(1);
   display.setTextColor(SH110X_WHITE);
   display.clearDisplay();

   drawCentreString(buf, 64, 10);
   drawCentreString("23rd September", 64, 18);
   display.display();
}

void loop() {}

void drawCentreString(const char *buf, int x, int y) {
   int16_t x1, y1;
   uint16_t w, h;
   display.getTextBounds(buf, x, y, &x1, &y1, &w, &h);
   display.setCursor(x - w / 2, y);
   display.print(buf);
   Serial.println(w);
}

Which demonstrates that the text width is calculated as 124 for both string literals and a const char buf containing the same text. Thereby demonstrating that the "when I try to use something that is not a const char" was a wild goose chase.

I then might modify the example and discover that a short string literal centred just fine.

I then might gradually increase the length of the string literal until the problem occurred again. And wonder what was special about 11 characters when that length triggered the problem?

Hmm. Default font width is 6 pixels. So 6 x 11 = 66. getTextBounds was asked to find the text bounds of an 11 character string, beginning printing at x=64, and got confused when the string ended up extending past the right hand side of the screen.

Hmm again. I wonder what would happen if I changed the x & y passed to getTextBounds to 0 and 0? display.getTextBounds(buf, 0, 0, &x1, &y1, &w, &h);

Well, look a that, it centred properly. Now let's try it with the full "23rd September".

Hey that works too. And it works with both a string literal and a const char buffer.

Turns out it was just me using the getTextBounds function incorrectly. It was trying (and failing) to calculate the bounds of a text string that started printing at x=64 that was longer than the remaining number of pixels left (64<66).

But that's just me. YMMV

(not answered yet)

Hi All....

"Wednesday" centres correctly.

So does "23rd Septe" Anything longer left justifies

I am using the default font with the GFX lib - so nothing special.

But more strange behaviour. If I set the x as 0 . the width is counted as 84, and only the end half is visible on the left.

And .. If I set the x as 128 (full width) the width is also cointed as 84, and the first half is displayed on the right, overflowing to the next line!

This is weird.....

Sorry - i did reply that I had - and no warnings.....

Hi van_der_decken

Thank you for your help. I didnt include the entire code, as it runs to several hundred lines....

I didnt think of producing a short snippet as you have to demonstrate the problem. I will bear that in mind in future. I have done more testing and will report back...

charlen = 9

charlen = 10

maxcharlen = 10

if (charlen > 10) charlen = 10;

the math becomes negative, which in computer numbers, could be a HUGE positive number, or wrapped around the screen width at the negative side, and onto the positive side.

Hi again van_der_decken

I took your snippet above, and ried what you said..

I wonder what would happen if I changed the x & y passed to getTextBounds to 0 and 0? display.getTextBounds(buf, 0, 0, &x1, &y1, &w, &h);

Didnt work for me! Returned a length of 84, but left it centered on 0 - so oly the 2nd half was visible.

Have I missed something?