|
Post by NeX on Feb 10, 2015 14:51:40 GMT -8
Hi,
i want to make a banner with scrolling text. i have used the scissor option to create a space for the text to occupy, and i have got the text to scroll, but what i want to have is some way to calculate the length of the text.
currently the text exists outside the scissored area, and then its x value is reduced until it appears on the screen and off the other side. then i want to loop it back around but to do this i need to know when the text has left the area.
because the text is drawn from the centre, i can't use the text position as an indicator. if it was right justified then this would be possible.
but if i know how long the text is i can calculate when it is off the screen by halving the length and measuring from the X coord. but as the text is dynamic i can't just take the width of a letter and calculate how many letters there are because the result depends on if the text is full of thin letters like "i" or wide letters like "w"
any help would be great thanks!
|
|
|
Post by jamesbowman on Feb 11, 2015 16:55:52 GMT -8
The FT800 ROM has the font tables built-in. So after some data-sheet digging, here is a function "text_width" that computes the pixel width of a given string in a given font. Just to try it out, the program uses the width to right-justify some text: #include <EEPROM.h> #include <SPI.h> #include <GD2.h>
void setup() { GD.begin(0); }
int text_width(byte font, const char *s) { unsigned char widths[128]; uint32_t font_addr = GD.rd32(0xffffc); font_addr += 148 * (font - 16); for (int i = 0; i < 128; i++) widths[i] = GD.rd(font_addr + i); int w = 0; while (*s) w += widths[*s++]; return w; }
void loop() { GD.ClearColorRGB(0x103000); GD.Clear(); const char *msg = "Right-justified text"; int w = text_width(31, msg); GD.cmd_number(240, 100, 31, OPT_CENTER, w); GD.cmd_text(480 - w, 136, 31, 0, msg); GD.swap(); }
|
|
|
Post by NeX on Feb 20, 2015 14:03:55 GMT -8
that worked flawlessly, you are a genius! thanks
|
|