ImageSharp.Drawing
ImageSharp.Drawing copied to clipboard
Draw text wrapped inside rectangle
It would be nice to be able to draw text inside of rectangle basically replicate feature present in System.Drawing.
Currently it is possible to wrap text at certain width using WrapTextWidth
but I would like to have control over vertical axis as well. I want to precisely place text over image knowing it won't grow outside its defined bounds. Having ability to define text alignment would be super useful too like you can do with TextFormatFlags.
Please consider adding this feature.
This looks like a very useful feature, but I doubt we can make it into 1.0, moving to Future.
Hey, I know that you said that you might not be able to make it into 1.0 but is there any clear estimation on this? I'd really like to see this feature. Oh I'm not really experienced with text rendering, but is there any pointer where one could contribute on this?
Also is it possible to calculate DrawText
area before hand? that might be useful as well
We'll definitely have comprehensive text wrapping support with multiple word breaking options and text align. What I'm unsure of though is clipping in the vertical direction. @tocsoft That would just be a case of recognizing we've hit a vertical limit and stopping no?
you can make use of https://github.com/SixLabors/ImageSharp.Drawing/blob/5bbf20e6cff00d06e9a6c61ea79c31e48ed824df/src/ImageSharp.Drawing/Processing/Extensions/ClipPathExtensions.cs#L22 (I believe this is only in the nightlies feed) to apply the drawing operation and ensure its explicitly clipped.
Ah yes, I’d forgotten about that.
The Clip
extension sounds neat, I haven't tried by myself but I got the idea.
One more thing (I'm not sure it still relevant, hopefully it's not treated as hijacking issue / thread), while clipping sounds neat, my goal is actually fit the text nicely inside rectangular area instead of getting clipped; e.g if text is overflowing / getting clipped by the area of clip, consider reducing font size so it fits nicely.
I'm aware that this probably not the scope of the library, but how me as a lib consumer, approach this situation?
@SirusDoma dumb, slow heuristic approach, but might be good enough:
you can use repeated calls of TextMeasurer.Measure
to get a size that approximately fits your destination area, then maybe using resize to fine-tune it.
we in fact have a sample for exactly that. https://github.com/SixLabors/Samples/blob/38faba5caf866c0168ea67a5e7c37c06d3043775/ImageSharp/DrawWaterMarkOnImage/Program.cs#L91-L159
Note: samples repo might be out of date compared to latest nightlys (as we are in API churn territory at the moment) but it should be close enough for your needs
The combination of wrapping + clipping is what the System.Drawing example is doing above. We've had both methods for a long time now on NuGet so I'm closing this.