Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/SixLabors/Fonts
Browse files Browse the repository at this point in the history
  • Loading branch information
JimBobSquarePants committed Oct 25, 2024
2 parents c7ab88e + fec131d commit 5a479b8
Show file tree
Hide file tree
Showing 73 changed files with 1,263 additions and 580 deletions.
18 changes: 9 additions & 9 deletions .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ jobs:
run: git lfs ls-files -l | cut -d' ' -f1 | sort > .lfs-assets-id

- name: Git Setup LFS Cache
uses: actions/cache@v3
uses: actions/cache@v4
id: lfs-cache
with:
path: .git/lfs
Expand All @@ -96,10 +96,10 @@ jobs:
run: git lfs pull

- name: NuGet Install
uses: NuGet/setup-nuget@v1
uses: NuGet/setup-nuget@v2

- name: NuGet Setup Cache
uses: actions/cache@v3
uses: actions/cache@v4
id: nuget-cache
with:
path: ~/.nuget
Expand All @@ -108,14 +108,14 @@ jobs:

- name: DotNet Setup
if: ${{ matrix.options.sdk-preview != true }}
uses: actions/setup-dotnet@v3
uses: actions/setup-dotnet@v4
with:
dotnet-version: |
6.0.x
- name: DotNet Setup Preview
if: ${{ matrix.options.sdk-preview == true }}
uses: actions/setup-dotnet@v3
uses: actions/setup-dotnet@v4
with:
dotnet-version: |
7.0.x
Expand Down Expand Up @@ -151,14 +151,14 @@ jobs:
XUNIT_PATH: .\tests\SixLabors.Fonts.Tests # Required for xunit

- name: Export Failed Output
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
if: failure()
with:
name: actual_output_${{ runner.os }}_${{ matrix.options.framework }}${{ matrix.options.runtime }}.zip
path: tests/Images/ActualOutput/

- name: Codecov Update
uses: codecov/codecov-action@v3
uses: codecov/codecov-action@v4
if: matrix.options.codecov == true && startsWith(github.repository, 'SixLabors')
with:
flags: unittests
Expand All @@ -185,10 +185,10 @@ jobs:
submodules: recursive

- name: NuGet Install
uses: NuGet/setup-nuget@v1
uses: NuGet/setup-nuget@v2

- name: NuGet Setup Cache
uses: actions/cache@v3
uses: actions/cache@v4
id: nuget-cache
with:
path: ~/.nuget
Expand Down
40 changes: 24 additions & 16 deletions samples/DrawWithImageSharp/BoundingBoxes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,49 @@
using SixLabors.ImageSharp.Drawing.Processing;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using IOPath = System.IO.Path;

namespace DrawWithImageSharp;

public static class BoundingBoxes
{
public static void Generate(string text, Font font)
public static void Generate(string text, TextOptions options)
{
using var img = new Image<Rgba32>(1000, 1000);
img.Mutate(x => x.Fill(Color.White));
FontRectangle bounds = TextMeasurer.MeasureBounds(text, options);
FontRectangle advance = TextMeasurer.MeasureAdvance(text, options);
using Image<Rgba32> image = new((int)Math.Ceiling(options.Origin.X + (Math.Max(advance.Width, bounds.Width) + 1)), (int)Math.Ceiling(options.Origin.Y + (Math.Max(advance.Height, bounds.Height) + 1)));
image.Mutate(x => x.Fill(Color.White));

TextOptions options = new(font);
FontRectangle box = TextMeasurer.MeasureBounds(text, options);
(IPathCollection paths, IPathCollection boxes) = GenerateGlyphsWithBox(text, options);

Rgba32 f = Color.Fuchsia;
f.A = 128;
Vector2 origin = options.Origin;

img.Mutate(x => x.Fill(Color.Black, paths)
.Draw(f, 1, boxes)
.Draw(Color.Lime, 1, new RectangularPolygon(box.Location, box.Size)));
FontRectangle size = TextMeasurer.MeasureSize(text, options);

img.Save("Output/Boxed.png");
(IPathCollection paths, IPathCollection boxes) = GenerateGlyphsWithBox(text, options);
image.Mutate(
x => x.Fill(Color.Black, paths)
.Draw(Color.Yellow, 1, boxes)
.Draw(Color.Purple, 1, new RectangularPolygon(bounds.X, bounds.Y, bounds.Width, bounds.Height))
.Draw(Color.Green, 1, new RectangularPolygon(size.X + bounds.X, size.Y + bounds.Y, size.Width, size.Height))
.Draw(Color.Red, 1, new RectangularPolygon(advance.X + origin.X, advance.Y + origin.Y, advance.Width, advance.Height)));

string path = IOPath.GetInvalidFileNameChars().Aggregate(text, (x, c) => x.Replace($"{c}", "-"));
string fullPath = IOPath.GetFullPath(IOPath.Combine($"Output/Boxed/{options.Font.Name}", IOPath.Combine(path)));
Directory.CreateDirectory(IOPath.GetDirectoryName(fullPath));

image.Save($"{fullPath}.png");
}

/// <summary>
/// Generates the shapes corresponding the glyphs described by the font and with the setting ing withing the FontSpan
/// Generates the shapes corresponding the glyphs described by the font and settings.
/// </summary>
/// <param name="text">The text to generate glyphs for</param>
/// <param name="options">The style and settings to use while rendering the glyphs</param>
/// <returns>The paths, boxes, and text box.</returns>
private static (IPathCollection Paths, IPathCollection Boxes) GenerateGlyphsWithBox(string text, TextOptions options)
{
var glyphBuilder = new CustomGlyphBuilder(Vector2.Zero);
CustomGlyphBuilder glyphBuilder = new();

var renderer = new TextRenderer(glyphBuilder);
TextRenderer renderer = new(glyphBuilder);

renderer.RenderText(text, options);

Expand Down
8 changes: 4 additions & 4 deletions samples/DrawWithImageSharp/CustomGlyphBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@ public CustomGlyphBuilder(Vector2 origin)
/// <summary>
/// Gets the paths that have been rendered by this.
/// </summary>
public IPathCollection Boxes => new PathCollection(this.glyphBounds.Select(x => new RectangularPolygon(x.Location, x.Size)));
public IPathCollection Boxes => new PathCollection(this.glyphBounds.Select(x => new RectangularPolygon(x.X, x.Y, x.Width, x.Height)));

/// <summary>
/// Gets the paths that have been rendered by this builder.
/// </summary>
public IPath TextBox { get; private set; }

protected override void BeginText(in FontRectangle rect)
protected override void BeginText(in FontRectangle bounds)
{
this.TextBox = new RectangularPolygon(rect.Location, rect.Size);
base.BeginText(rect);
this.TextBox = new RectangularPolygon(bounds.X, bounds.Y, bounds.Width, bounds.Height);
base.BeginText(bounds);
}

protected override void BeginGlyph(in FontRectangle bounds, in GlyphRendererParameters parameters)
Expand Down
4 changes: 2 additions & 2 deletions samples/DrawWithImageSharp/DrawWithImageSharp.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<DebugType>portable</DebugType>
Expand Down Expand Up @@ -46,7 +46,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="SixLabors.ImageSharp.Drawing" Version="2.0.0" />
<PackageReference Include="SixLabors.ImageSharp.Drawing" Version="2.1.3" />
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
</ItemGroup>

Expand Down
Loading

0 comments on commit 5a479b8

Please sign in to comment.