Drawing text onto an image using a typeface
⚓ Rust 📅 2026-01-13 👤 surdeus 👁️ 1Hi, I am in the process of teaching myself Rust by rewriting a project I previously created in Python, so I apologize if this is a simple and/or already answered question.
I'm trying to create a program that is able to take in a string of text of unknown length and write it to an image file (e.g., text.png, but preferably text.bmp) with the Bookerly typeface, adjusting the font size to the largest possible for the given string's length and the image's dimensions (which are 800 by 480).
There were plenty of examples to go off in Python, so it was easy enough to use the Pillow library to pass in a TrueType file, calculate & wrap the text, and write it to the image.
- I didn't have to worry about rasterizing the text, glyphs, ascender/descender heights, calculating the individual glyph placements, etc. (all of which are terms/concepts I have since learned about).
So far, the two best crates I have found that may work for what I'm trying to do are Fontdue (I have also looked into rusttype and ab_glyph, but Fontdue is a replacement for them) and COSMIC Text, but I've seemed to run into dead ends for both of them.
-
Fontdue has good support for doing the work of calculating font size and wrapping text for me, but it didn't seem to have good support for easily drawing the text to an image.
-
Cosmic provides plenty of support for manually calculating font size and wrapping text (i.e., it makes the process more hands-on but also easy), but I can't figure out how to write the text to the image. I know that in their docs they have the
buffer.drawfunction, but I don't think that it's what I'm looking for (I can't figure out how to do it).
Any advice for either of these crates, or any alternatives, would be much appreciated ![]()
2 posts - 2 participants
🏷️ Rust_feed