xlatb / ra8876

Arduino library for interfacing with RA8876 graphics chip
MIT License
13 stars 3 forks source link

Text dimension APIs #6

Open ImpulseAdventure opened 4 years ago

ImpulseAdventure commented 4 years ago

In order to support text justification (eg. centered text in a GUI), one often needs an ability to query the size of a string/characters in the active font. With RA8876_FONT_FAMILY_FIXED, it is relatively easy to calculate the width/height of individual characters. However, when a proportional font is selected (eg. RA8876_FONT_FAMILY_ARIAL in the External ROM font chip), it was not clear to me that we have a means of determining a character/string's width.

Do you see a way that one could support an API that returns a character/string width?

I had a quick look at the RA8876 & ER3304-1 datasheets but it wasn't obvious if there was an easy means to fetch a character's horizontal extent. Per the ER3304-1 datasheet, it is easy to compute a character's address in the ROM, but I couldn't locate a description of the proportional font width encoding.

thanks!

xlatb commented 4 years ago

I believe the RA8876 will let us read the font ROM directly, so it should be theoretically possible to do the same calculations that the chip does on the font data.

A "quick and dirty" method that might be worth exploring is writing the string to off-screen memory and calculating the dimensions from the new cursor position compared to the original.

ImpulseAdventure commented 4 years ago

Thank you @xlatb -- yes, it looks like it may be possible to read the ROM directly and attempt to decode the format. I will probably give this a try. Writing to an offscreen bitmap would have a performance penalty, but would be an interesting idea to consider if one fails to retrieve the metadata directly. thx

ImpulseAdventure commented 4 years ago

I tried a quick test to see if I could get the RA8876 SPI master to read from an external ROM font chip. I'm not sure if I read the datasheet correctly, but tried the following. If you happen to know what I am missing, that would be great.

Your existing external Font test example worked nicely with the ER3304-1 after some minor mods.

To start with, I was attempting to read an ER3304-1 font chip, pulling the data associated with an Arial proportional 16px space character (ASCII 0x20). I calculated the base address (per ER3304-1 datasheet section 4.2.9) as 0x1DE580. Note the reference code in RA8876 datasheet section 16.2 The following is the sequence of commands I tried:

  tft.initExternalFontRom(0, rom);
  tft.selectExternalFont(RA8876_FONT_FAMILY_ARIAL, RA8876_FONT_SIZE_16, RA8876_FONT_ENCODING_ASCII);

  SPI.beginTransaction(m_spiSettings);
  writeReg(0xB9,0x1F);
  writeReg(0xB8,0x03); // READ
  writeReg(0xB8,0x1D); // 24b Addr
  writeReg(0xB8,0xE5); // 24b Addr
  writeReg(0xB8,0x80); // 24b Addr

  acc = readReg(0xBA); // Poll for TX FIFO not empty
  while (acc != 0x80) { // Expect int set and check for 0x84 instead? (I see 0x80)
    acc = readReg(0xBA);
    SerialUSB.println(acc);
  }
  writeReg(0xBA,0x04); // Clear int

  SerialUSB.println("READ=");
  val = readReg(0xB8);
  SerialUSB.println(val,HEX);
  val = readReg(0xB8);
  SerialUSB.println(val,HEX);
  val = readReg(0xB8);
  SerialUSB.println(val,HEX);
  val = readReg(0xB8);
  SerialUSB.println(val,HEX);

  writeReg(0xB9,0x0F);

  SPI.endTransaction();  

The above returned 0xFFFFFFFF, which suggests it is not reading the SPI slave correctly.

Any guidance would be greatly appreciated.