k-omura / truetype_Arduino

display truetype font for Arduino
MIT License
40 stars 14 forks source link
arduino arduino-library bezier-curve bitmap e-paper esp32 font font-files stm32 stm32f103 truetype

Display truetype font for Arduino

Read truetype(.ttf) from FS(ex. SD/SPIFFS/FATFS) and write framebuffer.

TrueType™ Reference Manual
https://developer.apple.com/fonts/TrueType-Reference-Manual/

The native library for STM32 is here.

Standard code

//TrueType class declaration
truetypeClass truetype = truetypeClass();
uint8_t *framebuffer; 

void setup() {
  //Prepare a frame buffer
  framebuffer = (uint8_t *)calloc(sizeof(uint8_t), FRAMEBUFFER_SIZE);

  //Read TrueType file
  //Example in SPIFFS
  //I think that SD, FATFS and other codes will be almost the same
  SPIFFS.begin(true);
  File fontFile = SPIFFS.open("/FONTFILE.ttf", "r");

  //Set framebuffer array in TrueType class
  //Pay attention to the format of the framebuffer
  truetype.setFramebuffer(DISPLAY_WIDTH, DISPLAY_HEIGHT, 4, 0, framebuffer);

  //Initial reading of ttf files
  if (!truetype.setTtfFile(fontFile)) {
    Serial.println("read ttf failed");
    return;
  }

  //TrueType class string parameter settings
  truetype.setCharacterSize(100);
  truetype.setCharacterSpacing(0);
  truetype.setTextBoundary(10, DISPLAY_WIDTH, DISPLAY_HEIGHT);
  truetype.setTextColor(0x00, 0x00);

  //Write a string to the framebuffer
  truetype.textDraw(10, 10, "The quick brown fox jumps over the lazy dog");

  //Export framebuffer to screen
  FLASH_TO_SCREEN();

  //end
  truetype.end();
}

API

Framebuffer format

Bit orientation when storing information for multiple pixels per byte of the framebuffer.
The types of framebuffers are broadly divided according to this direction.
Currently supported: Horizontal - 1,4,8bit

Horizontal

Example with 1bit / 1pixel

Vertical

Example with 1bit / 1pixel

Originality

Future work

TrueType

Confirmed controller

Demo

Note

Feel free to post any bugs or ideas for fixes and improvements!
Confirm the copyright of the font file. I did not distribute font files.

It is based on the code by garretlab and changed.
https://github.com/garretlab/truetype
https://garretlab.web.fc2.com/arduino/lab/truetype/