Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use external fonts in code? #78

Open
MusabAybek opened this issue Sep 11, 2019 · 17 comments
Open

How to use external fonts in code? #78

MusabAybek opened this issue Sep 11, 2019 · 17 comments

Comments

@MusabAybek
Copy link

Hi. I'm new on idf coding and got an issue about fonts. I created my own .c file font via ttf2c_vc2003.exe program. But I don't know how to use this external font in my code. Can somebody explain please?
Thanks in advance...

@Mscichu
Copy link

Mscichu commented Sep 13, 2019

Hi!

First use program: https://github.com/loboris/ESP32_TFT_library/tree/master/tools
ttf2c_vc2003.exe 100 Zebulon.otf zebi.c
and edit as in readme.txt
Next you must write file to filesystem and convert .c file to .fon in program using command:
compile_font_file("/spiffs/fonts/zebi.c",1); // zebi.c for example

at last you can use new font:
TFT_setFont(USER_FONT, "/spiffs/fonts/zebi.fon");

@MusabAybek
Copy link
Author

Thank you so much for your helpful reply...
I converted my .ttf file to .c and edited as in readme.txt
But the command you provided didn't work. I located my terminal to the destination where my .c type font file is exist (C:\msys32\home\Musab\esp\workspace\ESP32_TFT_library-master\components\tft)
then I wrote; compile_font_file("/spiffs/fonts/oxygen.c",1); into terminal but didn't work, I got error says " 'compile_font_file' is not recognized as an internal "
Do I need to write my .c type font file location instead of "/spiffs/fonts/"?

@Mscichu
Copy link

Mscichu commented Sep 14, 2019

You must copy oxygen.c to ESP32_TFT_library\components\spiffs_image\image\fonts folder, then in command line use:
make makefs
and:
make flashfs

to write new filesystem data to ESP

@MusabAybek
Copy link
Author

After I copy .c file into \spiffs_image\image\fonts , is file will be automatically converted to the .fon type file? Because I'm stuck on converting from .c to .fon

@Mscichu
Copy link

Mscichu commented Sep 14, 2019

No, from this folder you copy files to ESP using flahfs command. Next you must use compile_font_file in your program to read .c file from ESP and make .fon file. Then you can use TFT_setFont(USER_FONT, "/spiffs/fonts/oxygen.fon");

@MusabAybek
Copy link
Author

Thank you for your really helpful replies. I got so far with your help. I did everything you wrote, everything on the code side looks okay, But I got still some issues..
I attached a part from my example code here, I'm trying to show different kinds of fonts one by one. But still see the same font every time sentences appear on the TFT.
example.txt

@Mscichu
Copy link

Mscichu commented Sep 15, 2019

Ok. Paste what debug shows

@MusabAybek
Copy link
Author

I'm really beginner on Eclipse and IDF coding, sorry I don't understand clearly. Is that what you wanted me to send? Copied from Debugger Console.
example1.txt

@MusabAybek
Copy link
Author

MusabAybek commented Sep 15, 2019

When I enter "make monitor", it says;
Error opening source file '/spiffs/fonts/bignoodle.c'
Error opening source file '/spiffs/fonts/codebold.c'
Error opening source file '/spiffs/fonts/heavitas.c'
Error opening source file '/spiffs/fonts/moonheavy.c'
Error opening source file '/spiffs/fonts/professor.c'

What the reason could be?

@Mscichu
Copy link

Mscichu commented Sep 15, 2019

You are not doing correctly make makefs and make flashfs so you don't have file in ESP.

@MusabAybek
Copy link
Author

This is the output of terminal, I tried to repeat all the process;

`Musab@MUSAB-PC MINGW32 ~
$ cd ~/esp/workspace/ESP32_TFT_library-master

Musab@MUSAB-PC MINGW32 ~/esp/workspace/ESP32_TFT_library-master
$ make makefs
/home/Musab/esp/workspace/ESP32_TFT_library-master/components/mkspiffs/Makefile.projbuild:13: warning: overriding recipe for target 'clean'
/home/Musab/esp/esp-idf/components/app_update/Makefile.projbuild:48: warning: ignoring old recipe for target 'clean'
Toolchain path: /opt/xtensa-esp32-elf/bin/xtensa-esp32-elf-gcc
Toolchain version: crosstool-ng-1.22.0-80-g6c4433a5
Compiler version: 5.2.0
Makefile:65: warning: undefined variable 'TARGET_CXXFLAGS'
Building mkspiffs ...
Making spiffs image ...
python /home/Musab/esp/esp-idf/components/esptool_py/esptool/esptool.py --chip esp32 --port COM5 --baud 115200 --before default_reset --after hard_reset write_flash -z --flash_mode dio --flash_freq 40m --flash_size detect
/.
/fonts/.
/fonts/BigFont.fon
/fonts/bignoodle.c
/fonts/codebold.c
/fonts/DejaVuSans12.fon
/fonts/DejaVuSans18.fon
/fonts/DejaVuSans24.fon
/fonts/DotMatrix_M.fon
/fonts/Grotesk24x48.fon
/fonts/heavitas.c
/fonts/moonheavy.c
/fonts/ocrfont.c
/fonts/oxygen.c
/fonts/professor.c
/fonts/swiss721_outline.fon
/images/.
/spiffs.info

Musab@MUSAB-PC MINGW32 ~/esp/workspace/ESP32_TFT_library-master
$ make flashfs
/home/Musab/esp/workspace/ESP32_TFT_library-master/components/mkspiffs/Makefile.projbuild:13: warning: overriding recipe for target 'clean'
/home/Musab/esp/esp-idf/components/app_update/Makefile.projbuild:48: warning: ignoring old recipe for target 'clean'
Toolchain path: /opt/xtensa-esp32-elf/bin/xtensa-esp32-elf-gcc
Toolchain version: crosstool-ng-1.22.0-80-g6c4433a5
Compiler version: 5.2.0
Making spiffs image ...
python /home/Musab/esp/esp-idf/components/esptool_py/esptool/esptool.py --chip esp32 --port COM5 --baud 115200 --before default_reset --after hard_reset write_flash -z --flash_mode dio --flash_freq 40m --flash_size detect
/.
/fonts/.
/fonts/BigFont.fon
/fonts/bignoodle.c
/fonts/codebold.c
/fonts/DejaVuSans12.fon
/fonts/DejaVuSans18.fon
/fonts/DejaVuSans24.fon
/fonts/DotMatrix_M.fon
/fonts/Grotesk24x48.fon
/fonts/heavitas.c
/fonts/moonheavy.c
/fonts/ocrfont.c
/fonts/oxygen.c
/fonts/professor.c
/fonts/swiss721_outline.fon
/images/.
/spiffs.info
esptool.py v2.8-dev
Serial port COM5
Connecting.................._____.
Chip is ESP32D0WDQ6 (revision 1)
Features: WiFi, BT, Dual Core, 240MHz, VRef calibration in efuse, Coding Scheme None
Crystal is 40MHz
MAC: 3c:71:bf:52:dc:98
Uploading stub...
Running stub...
Stub running...
Configuring flash size...
Auto-detected Flash size: 4MB
Compressed 1600576 bytes to 75090...
Wrote 1600576 bytes (75090 compressed) at 0x0002bf20 in 7.0 seconds (effective 1834.1 kbit/s)...
Hash of data verified.

Leaving...
Hard resetting via RTS pin...

Musab@MUSAB-PC MINGW32 ~/esp/workspace/ESP32_TFT_library-master
$ make flash
/home/Musab/esp/workspace/ESP32_TFT_library-master/components/mkspiffs/Makefile.projbuild:13: warning: overriding recipe for target 'clean'
/home/Musab/esp/esp-idf/components/app_update/Makefile.projbuild:48: warning: ignoring old recipe for target 'clean'
Toolchain path: /opt/xtensa-esp32-elf/bin/xtensa-esp32-elf-gcc
Toolchain version: crosstool-ng-1.22.0-80-g6c4433a5
Compiler version: 5.2.0
Python requirements from C:/msys32/home/Musab/esp/esp-idf/requirements.txt are satisfied.

Project is not inside a git repository, will not use 'git describe' to determine PROJECT_VER.
App "tft_demo" version: 1
Flashing binaries to serial port COM5 (app at offset 0x10000)...
esptool.py v2.8-dev
Serial port COM5
Connecting....
Chip is ESP32D0WDQ6 (revision 1)
Features: WiFi, BT, Dual Core, 240MHz, VRef calibration in efuse, Coding Scheme None
Crystal is 40MHz
MAC: 3c:71:bf:52:dc:98
Uploading stub...
Running stub...
Stub running...
Configuring flash size...
Auto-detected Flash size: 4MB
Compressed 24864 bytes to 14840...
Wrote 24864 bytes (14840 compressed) at 0x00001000 in 1.3 seconds (effective 150.7 kbit/s)...
Hash of data verified.
Compressed 245776 bytes to 130584...
Wrote 245776 bytes (130584 compressed) at 0x00010000 in 11.6 seconds (effective 169.2 kbit/s)...
Hash of data verified.
Compressed 3072 bytes to 117...
Wrote 3072 bytes (117 compressed) at 0x00008000 in 0.0 seconds (effective 985.7 kbit/s)...
Hash of data verified.

Leaving...
Hard resetting via RTS pin...

Musab@MUSAB-PC MINGW32 ~/esp/workspace/ESP32_TFT_library-master
$ make monitor
/home/Musab/esp/workspace/ESP32_TFT_library-master/components/mkspiffs/Makefile.projbuild:13: warning: overriding recipe for target 'clean'
/home/Musab/esp/esp-idf/components/app_update/Makefile.projbuild:48: warning: ignoring old recipe for target 'clean'
Toolchain path: /opt/xtensa-esp32-elf/bin/xtensa-esp32-elf-gcc
Toolchain version: crosstool-ng-1.22.0-80-g6c4433a5
Compiler version: 5.2.0
Python requirements from C:/msys32/home/Musab/esp/esp-idf/requirements.txt are satisfied.
MONITOR
--- idf_monitor on COM5 115200 ---
--- Quit: Ctrl+] | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H ---
ets Jun 8 2016 00:22:57

rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0018,len:4
load:0x3fff001c,len:6440
load:0x40078000,len:11624
load:0x40080400,len:6700
entry 0x40080768
I (28) boot: ESP-IDF v3.3 2nd stage bootloader
I (28) boot: compile time 19:54:17
I (37) boot: Enabling RNG early entropy source...
I (37) boot: SPI Speed : 40MHz
I (37) boot: SPI Mode : DIO
I (41) boot: SPI Flash Size : 4MB
I (45) boot: Partition Table:
I (48) boot: ## Label Usage Type ST Offset Length
I (56) boot: 0 nvs WiFi data 01 02 00009000 00006000
I (63) boot: 1 phy_init RF data 01 01 0000f000 00001000
I (71) boot: 2 factory factory app 00 00 00010000 00100000
I (78) boot: 3 storage Unknown data 01 82 00180000 00100000
I (86) boot: End of partition table
I (90) esp_image: segment 0: paddr=0x00010020 vaddr=0x3f400020 size=0x0e6c8 ( 59
080) map
I (119) esp_image: segment 1: paddr=0x0001e6f0 vaddr=0x3ffb0000 size=0x01920 (
6432) load
I (122) esp_image: segment 2: paddr=0x00020018 vaddr=0x400d0018 size=0x1da70 (12
1456) map
0x400d0018: _flash_cache_start at ??:?

I (169) esp_image: segment 3: paddr=0x0003da90 vaddr=0x3ffb1920 size=0x034e0 ( 1
3536) load
I (174) esp_image: segment 4: paddr=0x00040f78 vaddr=0x40080000 size=0x00400 (
1024) load
0x40080000: _WindowOverflow4 at C:/msys32/home/Musab/esp/esp-idf/components/free
rtos/xtensa_vectors.S:1779

I (176) esp_image: segment 5: paddr=0x00041380 vaddr=0x40080400 size=0x0ac68 ( 4
4136) load
I (210) boot: Loaded app from partition at offset 0x10000
I (210) boot: Disabling RNG early entropy source...
I (211) cpu_start: Pro cpu up.
I (214) cpu_start: Application information:
I (219) cpu_start: Project name: tft_demo
I (224) cpu_start: App version: 1
I (229) cpu_start: Compile time: Sep 15 2019 19:54:27
I (235) cpu_start: ELF file SHA256: 8b124d0f174d2172...
I (241) cpu_start: ESP-IDF: v3.3
I (245) cpu_start: Starting app cpu, entry point is 0x40081014
0x40081014: call_start_cpu1 at C:/msys32/home/Musab/esp/esp-idf/components/esp32
/cpu_start.c:270

I (0) cpu_start: App cpu up.
I (256) heap_init: Initializing. RAM available for dynamic allocation:
I (263) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM
I (269) heap_init: At 3FFB5F70 len 0002A090 (168 KiB): DRAM
I (275) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM
I (281) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (288) heap_init: At 4008B068 len 00014F98 (83 KiB): IRAM
I (294) cpu_start: Pro cpu start user code
I (88) cpu_start: Chip Revision: 1
W (89) cpu_start: Chip revision is higher than the one configured in menuconfig.
Suggest to upgrade it.
I (92) cpu_start: Starting scheduler on PRO CPU.
I (0) cpu_start: Starting scheduler on APP CPU.

==============================
TFT display DEMO, LoBo 11/2017

Pins used: miso=19, mosi=23, sck=18, cs=5

SPI: display device added to spi bus (1)
SPI: attached display device, speed=8000000
SPI: bus uses native pins: false
SPI: display init...
OK
SPI: Max rd speed = 8000000
SPI: Changed speed to 26666666


Graphics demo started

Error opening source file '/spiffs/fonts/oxygen.c'

I (3096) [SPIFFS]: Registering SPIFFS file system
I (3097) [SPIFFS]: Mounting SPIFFS files system
I (3097) [SPIFFS]: Start address: 0x180000; Size 1563 KB
I (3103) [SPIFFS]: Work buffer: 2048 B
I (3108) [SPIFFS]: FDS buffer: 384 B
I (3112) [SPIFFS]: Cache size: 2048 B
I (3173) [SPIFFS]: Mounted

==========================================
Display: ILI9341: PORTRAIT 240,320 Color

Error opening source file '/spiffs/fonts/bignoodle.c'
Error opening source file '/spiffs/fonts/codebold.c'
Error opening source file '/spiffs/fonts/heavitas.c'
Error opening source file '/spiffs/fonts/moonheavy.c'
Error opening source file '/spiffs/fonts/professor.c'
font demo bitti

==========================================
Display: ILI9341: LANDSCAPE 320,240 Color

Error opening source file '/spiffs/fonts/bignoodle.c'
Error opening source file '/spiffs/fonts/codebold.c'
Error opening source file '/spiffs/fonts/heavitas.c'
Error opening source file '/spiffs/fonts/moonheavy.c'
Error opening source file '/spiffs/fonts/professor.c'
font demo bitti
`

@Mscichu
Copy link

Mscichu commented Sep 15, 2019

Hmmm... I don't know why it does't work. Try run standard fonts from flash, example:

TFT_setFont(USER_FONT, "/spiffs/fonts/BigFont.fon");

@MusabAybek
Copy link
Author

MusabAybek commented Sep 16, 2019

Okay, thank you so much for your help, even so I'll keep trying.
There is a code in the example tft_demo.c code;
static const char *file_fonts[3] = {"/spiffs/fonts/DotMatrix_M.fon", "/spiffs/fonts/Ubuntu.fon", "/spiffs/fonts/Grotesk24x48.fon"};

Do you think it's something important we need to consider? May can help?

@MusabAybek
Copy link
Author

(Update) Changed to new microcontroller, still getting the same error...
Error opening source file '/spiffs/fonts/bignoodle.c'
Error opening source file '/spiffs/fonts/codebold.c'
Error opening source file '/spiffs/fonts/heavitas.c'
Error opening source file '/spiffs/fonts/moonheavy.c'
Error opening source file '/spiffs/fonts/professor.c'

@jeremyjh
Copy link

jeremyjh commented Sep 17, 2019

Does the original demo work if you just do make copyfs (uses image binary in this repo) rather than build the image yourself?

@Mscichu
Copy link

Mscichu commented Sep 17, 2019

I have another solution.

  1. In tft.h around line 130 define your font name example:
    #define OXYGEN_FONT 11
  2. In tft.c around line 1515 add another line:
    else if (font == OXYGEN_FONT) cfont.font = oxygen32;
    where oxygen32 is name your table in oxygen.c file
  3. Add prepared oxygen.c file to components/tft
  4. Use TFT_setFont(OXYGEN_FONT, NULL); in project

Now you don't need to write filesystem to ESP flash, just use make flash

@MusabAybek
Copy link
Author

MusabAybek commented Sep 17, 2019

I have another solution.

  1. In tft.h around line 130 define your font name example:
    #define OXYGEN_FONT 11
  2. In tft.c around line 1515 add another line:
    else if (font == OXYGEN_FONT) cfont.font = oxygen32;
    where oxygen32 is name your table in oxygen.c file
  3. Add prepared oxygen.c file to components/tft
  4. Use TFT_setFont(OXYGEN_FONT, NULL); in project

Now you don't need to write filesystem to ESP flash, just use make flash

Thank you really much man... This solution works... You helped me a lot.
I had to add definitions for external fonts also in tft.c file, around line 45 like;
extern uint8_t oxygen32[]; //for example

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants