toptensoftware / RichTextKit

Rich text rendering for SkiaSharp
Other
367 stars 73 forks source link

Added Halo for FontRun #44

Closed charlenni closed 2 years ago

charlenni commented 2 years ago

Added Halo to IStyle and Style Added handling of Halo in FontRun Added examples to SandboxDriver

This isn't far from perfect. The sizes aren't corrected for the halo. But I assume, that this isn't very important ;-)

Further, I don't know if there are other parts in RichTextKit, that draw anything to canvas.

The result looks like this:

grafik

m1dst commented 2 years ago

This is exactly what I was looking for however I applied your changes but saw no halo applied.

My text is created using a RichString. I see no reference to FontRun in the RichString file so suspect further digging is required.... or I've screwed up.

An example of the style I tested..

var styleNormal = new Style
                                          {
                                            FontFamily = "OpenSans-Regular",
                                            FontSize = textSize,
                                            TextColor = SKColors.Yellow,
                                            HaloBlur = 5,
                                            HaloColor = SKColors.Red,
                                            HaloWidth = 5,
                                            LineHeight = 1.2f
                                        };

An example of the RichString

var rs = new RichString { DefaultStyle = styleNormal, MaxHeight = bitmap.Height, MaxWidth = bitmap.Width }
                        .Alignment(textAlignment)
                        .MarginLeft(textSize).MarginTop(textSize).MarginRight(textSize).MarginBottom(textSize)
                        .Add(pui.OverlayContent, fontSize: textSize);

Hope that helps.

charlenni commented 2 years ago

You are right. Because I don't use RichString, I didn't see this problem. I added now the code to handle halo in RichString too. Perhaps you can test it?

m1dst commented 2 years ago

image

You nailed it. Thanks!

charlenni commented 2 years ago

@m1dst Be careful. The halo isn't added to the measurements. If we want to do it correct, then width and height should be increased by Math.Max(haloWidth, haloBlur). But I assume, that halo is only used by small texts for special reasons.

m1dst commented 2 years ago

@charlenni Makes sense. In my use case I am using it to watermark images. Plenty of padding around it, all looks good.

toptensoftware commented 2 years ago

This isn't far from perfect

What exactly are the issues here? Is it just that the returned measured sizes don't include the halo size?

charlenni commented 2 years ago

Yes. The halo isn't used to calculate the sizes. I'm still not sure, if this is a good or bad idea. The problem is, that the measured sizes aren't correct. I didn't look through RichTextKit to find the places, where this happens. If you say, we need it, then I could try to find them.