olikraus / u8g2

U8glib library for monochrome displays, version 2
Other
5.07k stars 1.05k forks source link

u8x8 rotate 90 degrees with fonts bigger than 8x8 pixels (1 tile) #2358

Open clara2g opened 8 months ago

clara2g commented 8 months ago

Hi, congrats on the good work! I've been using this library for a time and I really like it.

Now I'm trying to rotate 90 (and 270) degrees the text using u8x8 (not u8g2) and I know there is an example about it as mentioned in #2269 but I noticed it only works for 8x8 pixels (1 tile) fonts. I need to use bigger fonts rotated but I'm struggling to adapt the code. Has anyone found a way to do this? Thank you in advance.

olikraus commented 8 months ago

hmm... it shouldn't be that complicated. Let's assume u8x8_font_courB18_2x3_f. This font has six 8x8 tiles: 0 1 2 3 4 5 And now you want the tiles like this: 4 2 0 5 3 1

So... I suggest to create a custom draw procedure for exactly this font. Based on the procedures from https://github.com/olikraus/u8g2/blob/f50cb994708eb1e63c2efcc68685a08f7fcc988f/sys/arduino/u8x8/Rotate90/Rotate90.ino the draw procedure may look like this:

 void u8x8_draw_2x3_glyph_90(u8x8_t *u8x8, uint8_t x, uint8_t y, uint8_t encoding) 
 { 
   static uint8_t buf[8]; 
   u8x8_get_glyph_data(u8x8, encoding, buf, 0); 
   u8x8_DrawTile(u8x8, x, y, 1, rotate90(buf)); 
   u8x8_get_glyph_data(u8x8, encoding, buf, 1); 
   u8x8_DrawTile(u8x8, x, y+1, 1, rotate90(buf)); 
   u8x8_get_glyph_data(u8x8, encoding, buf, 2); 
   u8x8_DrawTile(u8x8, x-1, y, 1, rotate90(buf)); 
   u8x8_get_glyph_data(u8x8, encoding, buf, 3); 
   u8x8_DrawTile(u8x8, x-1, y+1, 1, rotate90(buf)); 
   u8x8_get_glyph_data(u8x8, encoding, buf, 4); 
   u8x8_DrawTile(u8x8, x-2, y, 1, rotate90(buf)); 
   u8x8_get_glyph_data(u8x8, encoding, buf, 5); 
   u8x8_DrawTile(u8x8, x-2, y+1, 1, rotate90(buf)); 
 } 

Maybe I made some thinking mistake but it should more or less work.

clara2g commented 8 months ago

That worked, thank you very much for your help!

The only thing left to do was to adapt the draw string method so the characters do not overlap when the string is longer than one character:

void u8x8_draw_2x3_string_90(u8x8_t *u8x8, uint8_t x, uint8_t y, const char *s) { while(*s != '\0') u8x8_draw_2x3_glyph_90(u8x8, x, y++*2, *s++); }

Many thanks.