takkaO / OpenFontRender

TTF font render support library for microcomputer.
Other
105 stars 16 forks source link

Need some way to center text on screen #18

Closed MikeyMoMo closed 1 year ago

MikeyMoMo commented 1 year ago

You don't honor or allow setTextDatum so I am left without any way to center my string on the display. Can you tell what calls to make to enable that? When I am using bit coded text, I choose the centered Datum and it does it for me. I cannot figure out how to make cdrawString work. I can't figure out how to code the Layout item to get past the compiler. I am not even sure that will work for me since there is no specification of the size to center in. Maybe it uses the display width. Surely would like a valid example. Your samples are very basic and leave out a lot.

I am sure I am missing the obvious. Thanks for coding the library. I like having the ability to specify font size by number rather than building a whole new bitcoded font file.

martinberlin commented 1 year ago

Hi @MikeyMoMo ,

if you check #5 you can use the getTextWidth / height to calculate this and center the text yourself making a function where you send (x, y, text) and then you can calculate it.

MikeyMoMo commented 1 year ago

Martin,

Thank you. I have used getTextWidth in the past but a lot of time, it has not been available and I forgot to try it this time. Thanks for reminding me. That is working. At least, it is returning an answer and it is trivial to center the text from here.

However, I would still like how to make cdrawString work. Any example of a valid call? I cannot figure out how to code that last parameter, the Horizontal or Vertical emum value. It is probably bleedingly simple but I just can't make anything pass muster!

Mike

takkaO commented 1 year ago

Hi @MikeyMoMo . Thank you for using this library 😊 and sorry for the late reply.

For an example of centering, see this example code.

Layout::Horizontal or Layout::Vertical is used to specify the text writing direction. English is generally written only horizontally, but some languages, such as Japanese, use both horizontal and vertical writing styles. However, the Layout::Vertical writing option is intended for future expansion and is not currently supported.

Text alignment (centered or right-aligned) is specified in the Align enumeration.

enum class Align {
    Left,
    Center,
    Right
};

Currently only horizontal align is supported, vertical align is fixed at the top (just don't have time to implement it). If you want the text to be perfectly centered vertically, horizontally, and vertically, you must use calculateBoundingBox to get the rectangular area used by the text, and the user must adjust it himself.

However, it is almost never necessary to actually use the Align enumerator. I recommend using the following functions

For left-alignment, use drawString or printf. For centering, use cdrawString or cprintf. For right-alignment, use rdrawString or rprintf.

For more information on the functions, see the API documentation.

Thank you.

MikeyMoMo commented 1 year ago

Here is a much more complete answer that I finally found by just trying stuff till it fit and doing a lot of digging around. This stuff is complex. All praise to the author of this library. It was not easy to create!!

  FT_BBox BBOX; FT_Error FTERR;
  Drawing doDraw = Drawing::Execute;
  Align myAlign = Align::Left;
  render.drawHString("Hello\nWorld", tft.width() / 2, 0, TFT_MAGENTA, TFT_BLACK, myAlign, doDraw, BBOX, FTERR);

FT_Pos typedef signed long FT_Pos;

FT_BBox BBOX; // This is a structure // typedef struct FTBBox // { // FT_Pos xMin, yMin; // FT_Pos xMax, yMax; // // } FT_BBox;

Drawing doDraw = Drawing::Execute; // This is an emum // enum class Drawing { // Execute, // Skip // };

Align myAlign = Align::Left; // This is an emum // enum class Align { // Left, // Center, // Right // };

typedef int FT_Error;

FT_Error FTERR; // This seems to be an integer but I can't find any doc on it.

takkaO commented 1 year ago

More detailed alignment settings are available in v1.1. Try using the setAlignment() function. See GitHub Pages for more information.

I am really sorry for the late updateπŸ™‡β€β™‚οΈπŸ™‡β€β™‚οΈπŸ™‡β€β™‚οΈ

MikeyMoMo commented 1 year ago

cprintf works. There are still problems with length calculations in many places.