ImageSharp.Drawing icon indicating copy to clipboard operation
ImageSharp.Drawing copied to clipboard

Draw text wrapped inside rectangle

Open kolpav opened this issue 4 years ago • 8 comments

It would be nice to be able to draw text inside of rectangle basically replicate feature present in System.Drawing.

image

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.

kolpav avatar Jul 18 '19 12:07 kolpav

This looks like a very useful feature, but I doubt we can make it into 1.0, moving to Future.

antonfirsov avatar Jun 19 '21 21:06 antonfirsov

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

SirusDoma avatar Oct 05 '21 17:10 SirusDoma

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?

JimBobSquarePants avatar Oct 05 '21 22:10 JimBobSquarePants

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.

tocsoft avatar Oct 06 '21 09:10 tocsoft

Ah yes, I’d forgotten about that.

JimBobSquarePants avatar Oct 06 '21 11:10 JimBobSquarePants

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 avatar Oct 07 '21 04:10 SirusDoma

@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.

antonfirsov avatar Oct 07 '21 07:10 antonfirsov

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

tocsoft avatar Oct 07 '21 07:10 tocsoft

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.

JimBobSquarePants avatar Aug 13 '23 11:08 JimBobSquarePants