TFT_eSPI icon indicating copy to clipboard operation
TFT_eSPI copied to clipboard

Single character can't printToSprite

Open quentin-cy opened this issue 1 year ago • 0 comments

Issue: A string of a single character is not printed by printToSprite when using a custom font created using the given script

To reproduce: As far as I could tell, the minimal configuration to see the issue is:

  1. Create a font as a header file ( Arial in this case)
  2. Use it in the code provided (based on "SmoothFont/FlashArray")
  3. Try to print a single character (here a 'C')

Voila ! The C is not printed... Since the usage of a custom font file is required, I believe it is related to the creation of the font file maybe the characters included... My changes in the create_font script used with processing:

  • Size 12
  • Arial
  • character range: Basic Latin 0x0021, 0x007E

Code: To create an example I started from the example "SmoothFont/FlashArray" and changed a few things to simplify it

#include "Arial12.h"

// The font names are arrays references, thus must NOT be in quotes ""
// #define AA_FONT_SMALL NotoSansBold15
#define AA_FONT_SMALL Arial12

#include <SPI.h>
#include <TFT_eSPI.h>       // Hardware-specific library

TFT_eSPI    tft = TFT_eSPI();
TFT_eSprite spr = TFT_eSprite(&tft); // Sprite class needs to be invoked

void setup(void) {

  Serial.begin(250000);

  tft.begin();

  tft.setRotation(1);

  spr.setColorDepth(16); // 16-bit colour needed to show anti-aliased fonts

  // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  // Setup done
  // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

  tft.fillScreen(TFT_BLACK);

  tft.setTextColor(TFT_WHITE, TFT_BLACK); // Set the font colour and the background colour

  tft.setTextDatum(TC_DATUM); // Top Centre datum

  int xcenter = tft.width() / 2; 
  int ycenter = tft.height() / 2;


  // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  // Small font
  // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

  spr.loadFont(AA_FONT_SMALL); // Must load the font first into the sprite class
  
  spr.setTextColor(TFT_YELLOW, TFT_BLACK); // Set the sprite font colour and the background colour

  tft.setCursor(xcenter - 100, ycenter-20);          // Set the tft cursor position, yes tft position!
  spr.printToSprite("Print a single char under");    // Prints to tft cursor position, tft cursor NOT moved

  // Single character print to sprite

  tft.setCursor(xcenter, ycenter);      
  spr.printToSprite("C");        
  // spr.printToSprite("CC");

  spr.unloadFont(); // Remove the font from sprite class to recover memory used

  delay(4000);
}

void loop() {

  delay(8000);
}

quentin-cy avatar Jul 25 '24 08:07 quentin-cy