Skip to content

Commit db32b13

Browse files
Merge pull request #331 from SixLabors/issue330-offset-outline
Align font outline and fill while calling DrawText
2 parents c8ae268 + 16d10c4 commit db32b13

9 files changed

+103
-13
lines changed

.github/workflows/build-and-test.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ jobs:
2323
sdk-preview: true
2424
runtime: -x64
2525
codecov: false
26-
- os: macos-latest
26+
- os: macos-13 # macos-latest runs on arm64 runners where libgdiplus is unavailable
2727
framework: net7.0
2828
sdk: 7.0.x
2929
sdk-preview: true
@@ -46,7 +46,7 @@ jobs:
4646
sdk: 6.0.x
4747
runtime: -x64
4848
codecov: false
49-
- os: macos-latest
49+
- os: macos-13 # macos-latest runs on arm64 runners where libgdiplus is unavailable
5050
framework: net6.0
5151
sdk: 6.0.x
5252
runtime: -x64

src/ImageSharp.Drawing/Processing/Processors/Text/RichTextGlyphRenderer.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -367,9 +367,10 @@ protected override void EndGlyph()
367367

368368
if (renderData.OutlineMap != null)
369369
{
370+
int offset = (int)((this.currentPen?.StrokeWidth ?? 0) / 2);
370371
this.DrawingOperations.Add(new DrawingOperation
371372
{
372-
RenderLocation = renderLocation,
373+
RenderLocation = renderLocation - new Size(offset, offset),
373374
Map = renderData.OutlineMap,
374375
Brush = this.currentPen?.StrokeFill ?? this.currentBrush!,
375376
RenderPass = RenderOrderOutline
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright (c) Six Labors.
2+
// Licensed under the Six Labors Split License.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Text;
8+
using System.Threading.Tasks;
9+
using SixLabors.Fonts;
10+
using SixLabors.ImageSharp.Drawing.Processing;
11+
using SixLabors.ImageSharp.PixelFormats;
12+
13+
namespace SixLabors.ImageSharp.Drawing.Tests.Issues;
14+
public class Issue_330
15+
{
16+
[Theory]
17+
[WithSolidFilledImages(2084, 2084, "BlueViolet", PixelTypes.Rgba32)]
18+
public void OffsetTextOutlines<TPixel>(TestImageProvider<TPixel> provider)
19+
where TPixel : unmanaged, IPixel<TPixel>
20+
{
21+
FontFamily fontFamily = TestFontUtilities.GetFontFamily(TestFonts.OpenSans);
22+
23+
Font bibfont = fontFamily.CreateFont(600, FontStyle.Bold);
24+
Font namefont = fontFamily.CreateFont(140, FontStyle.Bold);
25+
26+
provider.RunValidatingProcessorTest(p =>
27+
{
28+
p.DrawText(
29+
new RichTextOptions(bibfont)
30+
{
31+
VerticalAlignment = VerticalAlignment.Center,
32+
HorizontalAlignment = HorizontalAlignment.Center,
33+
TextAlignment = TextAlignment.Center,
34+
TextDirection = TextDirection.LeftToRight,
35+
Origin = new Point(1156, 1024),
36+
},
37+
"9999",
38+
Brushes.Solid(Color.White),
39+
Pens.Solid(Color.Black, 20));
40+
41+
p.DrawText(
42+
new RichTextOptions(namefont)
43+
{
44+
VerticalAlignment = VerticalAlignment.Center,
45+
HorizontalAlignment = HorizontalAlignment.Center,
46+
TextAlignment = TextAlignment.Center,
47+
TextDirection = TextDirection.LeftToRight,
48+
Origin = new Point(1156, 713),
49+
},
50+
"JOHAN",
51+
Brushes.Solid(Color.White),
52+
Pens.Solid(Color.Black, 5));
53+
54+
p.DrawText(
55+
new RichTextOptions(namefont)
56+
{
57+
VerticalAlignment = VerticalAlignment.Center,
58+
HorizontalAlignment = HorizontalAlignment.Center,
59+
TextAlignment = TextAlignment.Center,
60+
TextDirection = TextDirection.LeftToRight,
61+
Origin = new Point(1156, 1381),
62+
},
63+
"TIGERTECH",
64+
Brushes.Solid(Color.White),
65+
Pens.Solid(Color.Black, 5));
66+
});
67+
}
68+
}

tests/ImageSharp.Drawing.Tests/TestFontUtilities.cs

+20-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Six Labors Split License.
33

4+
using System.Globalization;
45
using System.Reflection;
56
using SixLabors.Fonts;
67
using IOPath = System.IO.Path;
@@ -19,7 +20,15 @@ public static class TestFontUtilities
1920
/// <param name="size">The font size.</param>
2021
/// <returns>The <see cref="Font"/></returns>
2122
public static Font GetFont(string name, float size)
22-
=> GetFont(new FontCollection(), name, size);
23+
=> GetFontFamily(name).CreateFont(size);
24+
25+
/// <summary>
26+
/// Gets a font family with the given name.
27+
/// </summary>
28+
/// <param name="name">The name of the font.</param>
29+
/// <returns>The <see cref="Font"/></returns>
30+
public static FontFamily GetFontFamily(string name)
31+
=> GetFontFamily(new FontCollection(), name);
2332

2433
/// <summary>
2534
/// Gets a font with the given name and size.
@@ -29,7 +38,16 @@ public static Font GetFont(string name, float size)
2938
/// <param name="size">The font size.</param>
3039
/// <returns>The <see cref="Font"/></returns>
3140
public static Font GetFont(FontCollection collection, string name, float size)
32-
=> collection.Add(GetPath(name)).CreateFont(size);
41+
=> GetFontFamily(collection, name).CreateFont(size);
42+
43+
/// <summary>
44+
/// Gets a font family with the given name
45+
/// </summary>
46+
/// <param name="collection">The collection to add the font to</param>
47+
/// <param name="name">The name of the font.</param>
48+
/// <returns>The <see cref="Font"/></returns>
49+
public static FontFamily GetFontFamily(FontCollection collection, string name)
50+
=> collection.Add(GetPath(name), CultureInfo.InvariantCulture);
3351

3452
/// <summary>
3553
/// The formats directory.

0 commit comments

Comments
 (0)