nathanielfernandes / imagetext-py

A blazing fast text drawing library
https://pypi.org/project/imagetext-py/
MIT License
74 stars 5 forks source link

Font Fallbacks Stopped Working in 2.1.0 #4

Closed bai-yi-bai closed 1 year ago

bai-yi-bai commented 1 year ago

Hi, @nathanielfernandes. I appreciate your fixes and dedication to this project!

Today I wanted to try text_wrap to compare the WordStyle.Word against WordStyle.Character output, but I encountered a problem (not an error): Font fallbacks stopped working; the missing unicode glyph character was printed out. I reverted to 2.0.1, along with my code (text_wrap to word_wrap) and characters printed out correctly.

Here's a sample of my code:

from pathlib import Path
import os
root_folder = Path(os.getcwd()).absolute()
primary_font = str(Path(root_folder / "fonts" / "JetBrainsMono-Regular.ttf"))
fallback_fonts = [str(Path(Path(os.getcwd()).absolute() / "fonts" /"fallback_fonts" / f)) for f in os.listdir(Path(os.getcwd()).absolute() / "fonts" / "fallback_fonts" ) if os.path.isfile(os.path.join(Path(os.getcwd()).absolute() / "fonts" / "fallback_fonts", f))]
f = Font(primary_font, fallbacks=fallback_fonts)

I tried moving the fonts to the root directory and specified their name, but it didn't matter.

I really admire your work on this project. I have an idea for you thought. I learned a lot from Pytlicek (Tomas Pytel) when I contributed to https://github.com/Pytlicek/sheet2dict, the key thing I learned was implementing test cases. I think it would really help to build a battery of tests before deploying.

I have two ideas about potential test cases you could use:

  1. Generate a baseline of images from the previous version, or store the hashes of images for expected file outputs (You could also do a bit-by-bit comparison as well, but I think a hash would probably be fine.). Then, as part of the build process, test that the output matches what is expected. You may wish to use some 'simple' images, without things like the rainbow stroke. It might be easier to recognize less complex images.
  2. Use an OCR library to compare the OCR against the input text.

I'm not a great coder, I just piece together a lot of libraries and wish I had the time right now to do a PR to contribute. I don't know how Tomas set up his Github projects to perform the automated testing, but it would really benefit you long term (you have a great personal website!)

nathanielfernandes commented 1 year ago

Thanks for the kind words, I found and fixed the broken fallbacks, it was a small logic error in the rewrite. I'll be pushing the update now. As for setting up testing and CI, I do plan on adding it but right now my main priority is creating docs for this lib.

Also the library has a built in FontDB which can load fonts and provide them for u wherever u need in ur code. Using the db ensures that fonts are only ever loaded once, and allows u to mix and match font fallback combinations.

ex.

# loading fonts
# FontDB.SetDefaultEmojiOptions(EmojiOptions(parse_discord_emojis=True))
FontDB.LoadFromDir(".")
FontDB.LoadSystemFonts()

# getting a font (nothing is being copied or cloned)
font = FontDB.Query("mainfont fallback1 fallback2 fallback3...")