Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Need some way to center text on screen #18

Closed
MikeyMoMo opened this issue Jan 13, 2023 · 6 comments
Closed

Need some way to center text on screen #18

MikeyMoMo opened this issue Jan 13, 2023 · 6 comments
Labels
progress: waiting for confirmation and close Waiting for confirmation and close

Comments

@MikeyMoMo
Copy link

MikeyMoMo commented Jan 13, 2023

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
Copy link

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
Copy link
Author

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
Copy link
Owner

takkaO commented Jan 15, 2023

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
Copy link
Author

MikeyMoMo commented Jan 28, 2023

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 FT_BBox_
// {
// 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 added a commit that referenced this issue Feb 2, 2023
- Support vertical alignment (Top, Middle, Bottom)
@takkaO
Copy link
Owner

takkaO commented May 1, 2023

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🙇‍♂️🙇‍♂️🙇‍♂️

@takkaO takkaO added the progress: waiting for confirmation and close Waiting for confirmation and close label May 1, 2023
@MikeyMoMo
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
progress: waiting for confirmation and close Waiting for confirmation and close
Projects
None yet
Development

No branches or pull requests

3 participants