/* PROGRAM FOR THE CONVERSION OF THE FONTS FOR ANNEX32 */

/* HOW USE IT

   Before the procedure, you need a font file in Adafruit GFX format into a .h file.
	 This can be obtained in different ways, for example using 
	 this page https://rop.nl/truetype2gfx/ for the creation of the file from a ttf font
	 and then using 
	 this page https://tchapi.github.io/Adafruit-GFX-Font-Customiser/ for the customisation

	 As soon as the fontname.h file is obtained, you can follow this simple procedure

   1) Copy the content of your GFX format font (fontxx.h) in the tabe "myfont.h"
   2) Copy the name of the font from the line <const GFXfont ....>
      example for the line "const GFXfont pixel5pt7b PROGMEM = {" the name is pixel5pt7b
   3) Modify the line "#define FONTNAME pixel5pt7b" with the name of the font
   4) Run the program; after the compilation a hex string will be printed in the console
   5) Copy the hex string and paste in this web site
      https://tomeko.net/online_tools/hex_to_file.php?lang=en
   6) Set the name of your file as fontname.bin (pixel5pt7b.bin in this example)
   7) Click on convert and the file will be downloaded ready to use for annex32
*/

/* SET THE FONT NAME in the following line */
#define FONTNAME pixel5pt7b

#include "Free_Fonts.h" // Include the header file attached to this sketch

#include "SPI.h"
#include "TFT_eSPI.h"
#include "myfont.h"


// Use hardware SPI
TFT_eSPI tft = TFT_eSPI();

#define STICK(name, param) name.param
#define PPCAT_NX(A, B) A ## B
#define PPCAT(A, B) PPCAT_NX(A, B)
#define STRINGIZE_NX(A) #A
#define STRINGIZE(A) STRINGIZE_NX(A)

// format proposed
// size bitmap  - 4 bytes
// size glyphs  - 4 bytes
// first gliph  - 2 byes
// last glyph   - 2 bytes
// yAdvance     - 1 byte
// Not Used     - 3 bytes
// ---------------- total 16 bytes
// bitmap       - xx bytes
// glyph        - yy bytes

// to allocate : myfont = malloc(size bitmap + size glyphs + 16)
// then cast the address to GFXfont and set it with
// myfont.bitmap = myfont + 16
// myfont.glyphs = myfont + 16 + size bitmap
// ......
int cnt = 0;
int line_width = 16;
#define create_fontFile(MYFONT) \
{ \
	Serial.printf("Dimensions bitmap %d glyph %d, GFXfont %d\n", sizeof(PPCAT(MYFONT, Bitmaps)), sizeof(PPCAT(MYFONT, Glyphs)), sizeof(PPCAT(MYFONT, Bitmaps)) + sizeof(PPCAT(MYFONT, Glyphs))+16); \
	int size = sizeof(PPCAT(MYFONT, Bitmaps)) + sizeof(PPCAT(MYFONT, Glyphs)) + sizeof(GFXfont); \
  Serial.printf("Content of the font %s.bin\n", STRINGIZE(MYFONT)); \
  Serial.println("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-="); \
 \
	uint32_t i; \
	uint16_t s; \
	uint8_t  b; \
	i = sizeof(PPCAT(MYFONT, Bitmaps)); \
	file_write((uint8_t*)&i, 4); \
	i = sizeof(PPCAT(MYFONT, Glyphs)); \
	file_write((uint8_t*)&i, 4); \
	s = STICK(MYFONT, first);  \
	file_write((uint8_t*)&s, 2); \
	s = STICK(MYFONT, last);  \
	file_write((uint8_t*)&s, 2); \
	b = STICK(MYFONT, yAdvance); \
	file_byte(b); \
	i = 0; \
	file_write((uint8_t*)&i, 3);  \
	file_write((uint8_t*)PPCAT(MYFONT, Bitmaps), sizeof(PPCAT(MYFONT, Bitmaps))); \
	file_write((uint8_t*)PPCAT(MYFONT, Glyphs), sizeof(PPCAT(MYFONT, Glyphs))); \
  Serial.println("*******************************************************************");\
} 

void file_byte(uint8_t b) {
  if (!(cnt++ % line_width)) Serial.println("");
  Serial.printf("%02X ", b);
}

void file_write(uint8_t* address, int length) {
  for (int i=0; i<length; i++) {
    if (!(cnt++ % line_width)) Serial.println("");
    Serial.printf("%02X ", address[i]);
  }
}

void setup(void) {

  Serial.begin(115200);
  Serial.println("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");
  create_fontFile(FONTNAME);
}


void loop() {
delay(1);
}


/* 
OTHER FONTS that can be converted as part of the library

	  create_fontFile(TomThumb);
	  
	  create_fontFile(FreeMono9pt7b);
	  create_fontFile(FreeMono12pt7b);
	  create_fontFile(FreeMono18pt7b);
	  create_fontFile(FreeMono24pt7b);
	  
	  create_fontFile(FreeMonoBold9pt7b);
	  create_fontFile(FreeMonoBold12pt7b);
	  create_fontFile(FreeMonoBold18pt7b);
	  create_fontFile(FreeMonoBold24pt7b);
	  
	  create_fontFile(FreeMonoBoldOblique9pt7b);
	  create_fontFile(FreeMonoBoldOblique12pt7b);
	  create_fontFile(FreeMonoBoldOblique18pt7b);
	  create_fontFile(FreeMonoBoldOblique24pt7b);
	  
	  create_fontFile(FreeMonoOblique9pt7b);
	  create_fontFile(FreeMonoOblique12pt7b);
	  create_fontFile(FreeMonoOblique18pt7b);
	  create_fontFile(FreeMonoOblique24pt7b);
	  
	  create_fontFile(FreeSans9pt7b);
	  create_fontFile(FreeSans12pt7b);
	  create_fontFile(FreeSans18pt7b);
	  create_fontFile(FreeSans24pt7b);
	  
	  create_fontFile(FreeSansBold9pt7b);
	  create_fontFile(FreeSansBold12pt7b);
	  create_fontFile(FreeSansBold18pt7b);
	  create_fontFile(FreeSansBold24pt7b);
	  
	  create_fontFile(FreeSansBoldOblique9pt7b);
	  create_fontFile(FreeSansBoldOblique12pt7b);
	  create_fontFile(FreeSansBoldOblique18pt7b);
	  create_fontFile(FreeSansBoldOblique24pt7b);
	  
	  create_fontFile(FreeSansOblique9pt7b);
	  create_fontFile(FreeSansOblique12pt7b);
	  create_fontFile(FreeSansOblique18pt7b);
	  create_fontFile(FreeSansOblique24pt7b);
	  
	  create_fontFile(FreeSerif9pt7b);
	  create_fontFile(FreeSerif12pt7b);
	  create_fontFile(FreeSerif18pt7b);
	  create_fontFile(FreeSerif24pt7b);
	  
	  create_fontFile(FreeSerifBold9pt7b);
	  create_fontFile(FreeSerifBold12pt7b);
	  create_fontFile(FreeSerifBold18pt7b);
	  create_fontFile(FreeSerifBold24pt7b);
	  
	  create_fontFile(FreeSerifBoldItalic9pt7b);
	  create_fontFile(FreeSerifBoldItalic12pt7b);
	  create_fontFile(FreeSerifBoldItalic18pt7b);
	  create_fontFile(FreeSerifBoldItalic24pt7b);
	  
	  create_fontFile(FreeSerifItalic9pt7b);
	  create_fontFile(FreeSerifItalic12pt7b);
	  create_fontFile(FreeSerifItalic18pt7b);
	  create_fontFile(FreeSerifItalic24pt7b);

	  create_fontFile(Orbitron_Light_24);
	  create_fontFile(Orbitron_Light_32);
	  create_fontFile(Roboto_Thin_24);
	  create_fontFile(Satisfy_24);
	  create_fontFile(Yellowtail_32);
	  create_fontFile(Yellowtail_32);
	  
*/