Skip to content

Commit

Permalink
Merge pull request #331 from SixLabors/issue330-offset-outline
Browse files Browse the repository at this point in the history
Align font outline and fill while calling DrawText
  • Loading branch information
JimBobSquarePants authored May 10, 2024
2 parents c8ae268 + 16d10c4 commit db32b13
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 13 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
sdk-preview: true
runtime: -x64
codecov: false
- os: macos-latest
- os: macos-13 # macos-latest runs on arm64 runners where libgdiplus is unavailable
framework: net7.0
sdk: 7.0.x
sdk-preview: true
Expand All @@ -46,7 +46,7 @@ jobs:
sdk: 6.0.x
runtime: -x64
codecov: false
- os: macos-latest
- os: macos-13 # macos-latest runs on arm64 runners where libgdiplus is unavailable
framework: net6.0
sdk: 6.0.x
runtime: -x64
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,9 +367,10 @@ protected override void EndGlyph()

if (renderData.OutlineMap != null)
{
int offset = (int)((this.currentPen?.StrokeWidth ?? 0) / 2);
this.DrawingOperations.Add(new DrawingOperation
{
RenderLocation = renderLocation,
RenderLocation = renderLocation - new Size(offset, offset),
Map = renderData.OutlineMap,
Brush = this.currentPen?.StrokeFill ?? this.currentBrush!,
RenderPass = RenderOrderOutline
Expand Down
68 changes: 68 additions & 0 deletions tests/ImageSharp.Drawing.Tests/Issues/Issue_330.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SixLabors.Fonts;
using SixLabors.ImageSharp.Drawing.Processing;
using SixLabors.ImageSharp.PixelFormats;

namespace SixLabors.ImageSharp.Drawing.Tests.Issues;
public class Issue_330
{
[Theory]
[WithSolidFilledImages(2084, 2084, "BlueViolet", PixelTypes.Rgba32)]
public void OffsetTextOutlines<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel>
{
FontFamily fontFamily = TestFontUtilities.GetFontFamily(TestFonts.OpenSans);

Font bibfont = fontFamily.CreateFont(600, FontStyle.Bold);
Font namefont = fontFamily.CreateFont(140, FontStyle.Bold);

provider.RunValidatingProcessorTest(p =>
{
p.DrawText(
new RichTextOptions(bibfont)
{
VerticalAlignment = VerticalAlignment.Center,
HorizontalAlignment = HorizontalAlignment.Center,
TextAlignment = TextAlignment.Center,
TextDirection = TextDirection.LeftToRight,
Origin = new Point(1156, 1024),
},
"9999",
Brushes.Solid(Color.White),
Pens.Solid(Color.Black, 20));
p.DrawText(
new RichTextOptions(namefont)
{
VerticalAlignment = VerticalAlignment.Center,
HorizontalAlignment = HorizontalAlignment.Center,
TextAlignment = TextAlignment.Center,
TextDirection = TextDirection.LeftToRight,
Origin = new Point(1156, 713),
},
"JOHAN",
Brushes.Solid(Color.White),
Pens.Solid(Color.Black, 5));
p.DrawText(
new RichTextOptions(namefont)
{
VerticalAlignment = VerticalAlignment.Center,
HorizontalAlignment = HorizontalAlignment.Center,
TextAlignment = TextAlignment.Center,
TextDirection = TextDirection.LeftToRight,
Origin = new Point(1156, 1381),
},
"TIGERTECH",
Brushes.Solid(Color.White),
Pens.Solid(Color.Black, 5));
});
}
}
22 changes: 20 additions & 2 deletions tests/ImageSharp.Drawing.Tests/TestFontUtilities.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.

using System.Globalization;
using System.Reflection;
using SixLabors.Fonts;
using IOPath = System.IO.Path;
Expand All @@ -19,7 +20,15 @@ public static class TestFontUtilities
/// <param name="size">The font size.</param>
/// <returns>The <see cref="Font"/></returns>
public static Font GetFont(string name, float size)
=> GetFont(new FontCollection(), name, size);
=> GetFontFamily(name).CreateFont(size);

/// <summary>
/// Gets a font family with the given name.
/// </summary>
/// <param name="name">The name of the font.</param>
/// <returns>The <see cref="Font"/></returns>
public static FontFamily GetFontFamily(string name)
=> GetFontFamily(new FontCollection(), name);

/// <summary>
/// Gets a font with the given name and size.
Expand All @@ -29,7 +38,16 @@ public static Font GetFont(string name, float size)
/// <param name="size">The font size.</param>
/// <returns>The <see cref="Font"/></returns>
public static Font GetFont(FontCollection collection, string name, float size)
=> collection.Add(GetPath(name)).CreateFont(size);
=> GetFontFamily(collection, name).CreateFont(size);

/// <summary>
/// Gets a font family with the given name
/// </summary>
/// <param name="collection">The collection to add the font to</param>
/// <param name="name">The name of the font.</param>
/// <returns>The <see cref="Font"/></returns>
public static FontFamily GetFontFamily(FontCollection collection, string name)
=> collection.Add(GetPath(name), CultureInfo.InvariantCulture);

/// <summary>
/// The formats directory.
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit db32b13

Please sign in to comment.