jrmuizel / raqote

Rust 2D graphics library
BSD 3-Clause "New" or "Revised" License
1.03k stars 59 forks source link

Get width of rendered text #169

Open l4l opened 3 years ago

l4l commented 3 years ago

Is it possible to get a width of rendered text (i.e. draw_text). This particularly useful if there's a need to draw a text with mixing different colors (or perhaps there's an easier way?) so the offset for drawing a block of differently colored text is known.

jrmuizel commented 3 years ago

It shouldn't be too hard to add something like this, a la measureText() from HTML canvas.

tiye commented 2 years ago

noticed we don't have align: center for text yet, can we rely on this to center?

J-Cake commented 1 year ago

I ended up using my own text rendering system using rusttype. It works well and it's decently fast

l4l commented 1 year ago

I encountered quite poor performance with rusttype but probably it was due tofill_rect (versus draw_image) and didn't spend much time figuring out why. So I ended up with fontdue crate instead. Here is all that you need to draw text with it.

J-Cake commented 1 year ago

I ran into a few quality issues as well, but I found that to be the font face I was using. I tested mine with Raleway, Rasa, Lora, Inconsolata and DejaVu from Google Fonts, and they all worked well. Only NotoSans didn't really work that well

ion232 commented 1 year ago

I tried both methods and found that the fontdue crate worked best in my case. The font I was using (PokemonGB) was crashing due to a negative min bounding box in the rusttype method.