Skip to content

Commit 77b6640

Browse files
Merge pull request #278 from SixLabors/js/update-fonts
Update to latest Fonts and implement upstream clipper fix
2 parents 7b19f39 + 3ba5a75 commit 77b6640

File tree

76 files changed

+182
-166
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+182
-166
lines changed

src/ImageSharp.Drawing/ImageSharp.Drawing.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<None Include="..\..\shared-infrastructure\branding\icons\imagesharp.drawing\sixlabors.imagesharp.drawing.128.png" Pack="true" PackagePath="" />
1919
</ItemGroup>
2020
<ItemGroup>
21-
<PackageReference Include="SixLabors.Fonts" Version="1.0.0-beta19.13" />
21+
<PackageReference Include="SixLabors.Fonts" Version="1.0.0-beta19.19" />
2222
<PackageReference Include="SixLabors.ImageSharp" Version="2.1.3" />
2323
</ItemGroup>
2424
<Import Project="..\..\shared-infrastructure\src\SharedInfrastructure\SharedInfrastructure.projitems" Label="Shared" />

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ internal sealed class RichTextGlyphRenderer : BaseGlyphBuilder, IColorGlyphRende
3939
private TextDecorationDetails? currentUnderline;
4040
private TextDecorationDetails? currentStrikout;
4141
private TextDecorationDetails? currentOverline;
42-
private bool currentDecorationRotated;
42+
private bool currentDecorationIsVertical;
4343

4444
// Just enough accuracy to allow for 1/8 px differences which later are accumulated while rendering,
4545
// but do not grow into full px offsets.
@@ -100,7 +100,7 @@ protected override void BeginText(in FontRectangle bounds)
100100
protected override void BeginGlyph(in FontRectangle bounds, in GlyphRendererParameters parameters)
101101
{
102102
this.currentColor = null;
103-
this.currentDecorationRotated = parameters.LayoutMode.IsVertical() || parameters.LayoutMode.IsVerticalMixed();
103+
this.currentDecorationIsVertical = parameters.LayoutMode is GlyphLayoutMode.Vertical or GlyphLayoutMode.VerticalRotated;
104104
this.currentTextRun = parameters.TextRun;
105105
if (parameters.TextRun is RichTextRun drawingRun)
106106
{
@@ -232,7 +232,7 @@ public override void SetDecoration(TextDecorations textDecorations, Vector2 star
232232

233233
// Drawing is always centered around the point so we need to offset by half.
234234
Vector2 offset = Vector2.Zero;
235-
bool rotated = this.currentDecorationRotated;
235+
bool rotated = this.currentDecorationIsVertical;
236236
if (textDecorations == TextDecorations.Overline)
237237
{
238238
// CSS overline is drawn above the position, so we need to move it up.

src/ImageSharp.Drawing/Shapes/PolygonClipper/ClipperOffset.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace SixLabors.ImageSharp.Drawing.Shapes.PolygonClipper
1111
/// </summary>
1212
internal class ClipperOffset
1313
{
14-
// To make the floating point polygons compatable with clipper we have to scale them.
14+
// To make the floating point polygons compatible with clipper we have to scale them.
1515
private const float ScalingFactor = 1000F;
1616
private readonly PolygonOffsetter polygonClipperOffset;
1717

src/ImageSharp.Drawing/Shapes/PolygonClipper/PolygonClipper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public void Execute(ClippingOperation clipType, FillRule fillRule, PathsF soluti
8686
}
8787
catch (Exception ex)
8888
{
89-
throw new ClipperException("An error occured while attempting to clip the polygon. See the inner exception for details.", ex);
89+
throw new ClipperException("An error occurred while attempting to clip the polygon. See the inner exception for details.", ex);
9090
}
9191
finally
9292
{

src/ImageSharp.Drawing/Shapes/PolygonClipper/PolygonOffsetter.cs

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ namespace SixLabors.ImageSharp.Drawing.Shapes.PolygonClipper
1515
/// </summary>
1616
internal sealed class PolygonOffsetter
1717
{
18+
private const float Tolerance = 1.0E-6F;
1819
private readonly List<Group> groupList = new();
1920
private readonly PathF normals = new();
2021
private readonly PathsF solution = new();
@@ -102,10 +103,10 @@ public void Execute(float delta, PathsF solution)
102103

103104
Error:
104105

105-
// PolygonClipper will throw for unhandled exceptions but we need to explcitly capture an empty result.
106+
// PolygonClipper will throw for unhandled exceptions but we need to explicitly capture an empty result.
106107
if (solution.Count == 0)
107108
{
108-
throw new ClipperException("An error occured while attempting to clip the polygon. Check input for invalid entries.");
109+
throw new ClipperException("An error occurred while attempting to clip the polygon. Check input for invalid entries.");
109110
}
110111
}
111112

@@ -326,19 +327,27 @@ private void OffsetOpenPath(Group group, PathF path)
326327
group.OutPath = new(path.Count);
327328
int highI = path.Count - 1;
328329

329-
// do the line start cap
330-
switch (this.endType)
330+
// Further reduced extraneous vertices in solutions (#499)
331+
if (MathF.Abs(this.groupDelta) < Tolerance)
331332
{
332-
case EndCapStyle.Butt:
333-
group.OutPath.Add(path[0] - (this.normals[0] * this.groupDelta));
334-
group.OutPath.Add(this.GetPerpendic(path[0], this.normals[0]));
335-
break;
336-
case EndCapStyle.Round:
337-
this.DoRound(group, path, 0, 0, MathF.PI);
338-
break;
339-
default:
340-
this.DoSquare(group, path, 0, 0);
341-
break;
333+
group.OutPath.Add(path[0]);
334+
}
335+
else
336+
{
337+
// do the line start cap
338+
switch (this.endType)
339+
{
340+
case EndCapStyle.Butt:
341+
group.OutPath.Add(path[0] - (this.normals[0] * this.groupDelta));
342+
group.OutPath.Add(this.GetPerpendic(path[0], this.normals[0]));
343+
break;
344+
case EndCapStyle.Round:
345+
this.DoRound(group, path, 0, 0, MathF.PI);
346+
break;
347+
default:
348+
this.DoSquare(group, path, 0, 0);
349+
break;
350+
}
342351
}
343352

344353
// offset the left side going forward
@@ -410,6 +419,13 @@ private void OffsetPolygon(Group group, PathF path)
410419

411420
private void OffsetPoint(Group group, PathF path, int j, ref int k)
412421
{
422+
// Further reduced extraneous vertices in solutions (#499)
423+
if (MathF.Abs(this.groupDelta) < Tolerance)
424+
{
425+
group.OutPath.Add(path[j]);
426+
return;
427+
}
428+
413429
// Let A = change in angle where edges join
414430
// A == 0: ie no change in angle (flat join)
415431
// A == PI: edges 'spike'
@@ -426,7 +442,12 @@ private void OffsetPoint(Group group, PathF path, int j, ref int k)
426442
sinA = -1F;
427443
}
428444

429-
if (cosA > -0.99F && (sinA * this.groupDelta < 0F))
445+
// almost straight - less than 1 degree (#424)
446+
if (cosA > 0.99F)
447+
{
448+
this.DoMiter(group, path, j, k, cosA);
449+
}
450+
else if (cosA > -0.99F && (sinA * this.groupDelta < 0F))
430451
{
431452
// is concave
432453
group.OutPath.Add(this.GetPerpendic(path[j], this.normals[k]));
@@ -448,12 +469,7 @@ private void OffsetPoint(Group group, PathF path, int j, ref int k)
448469
this.DoSquare(group, path, j, k);
449470
}
450471
}
451-
else if (cosA > 0.9998F)
452-
{
453-
// almost straight - less than 1 degree (#424)
454-
this.DoMiter(group, path, j, k, cosA);
455-
}
456-
else if (cosA > 0.99F || this.joinType == JointStyle.Square)
472+
else if (this.joinType == JointStyle.Square)
457473
{
458474
// angle less than 8 degrees or a squared join
459475
this.DoSquare(group, path, j, k);
@@ -541,7 +557,7 @@ private void DoRound(Group group, PathF path, int j, int k, float angle)
541557
// avoid 180deg concave
542558
if (angle > -MathF.PI + .01F)
543559
{
544-
int steps = Math.Max(2, (int)Math.Ceiling(this.stepsPerRad * Math.Abs(angle)));
560+
int steps = Math.Max(2, (int)Math.Ceiling(this.stepsPerRad * MathF.Abs(angle)));
545561

546562
// ie 1 less than steps
547563
for (int i = 1; i < steps; i++)

src/ImageSharp.Drawing/Shapes/Text/BaseGlyphBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ public virtual void SetDecoration(TextDecorations textDecorations, Vector2 start
151151
var renderer = (IGlyphRenderer)this;
152152

153153
// Expand the points to create a rectangle centered around the line.
154-
bool rotated = this.parameters.LayoutMode.IsVertical() || this.parameters.LayoutMode.IsVerticalMixed();
154+
bool rotated = this.parameters.LayoutMode is GlyphLayoutMode.Vertical or GlyphLayoutMode.VerticalRotated;
155155
Vector2 pad = rotated ? new(thickness * .5F, 0) : new(0, thickness * .5F);
156156

157157
// Clamp the line to the pixel grid.

tests/ImageSharp.Drawing.Tests/Drawing/Text/DrawTextOnImageTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public void DoesntThrowExceptionWhenOverlappingRightEdge_Issue688<TPixel>(TestIm
108108
using Image<TPixel> img = provider.GetImage();
109109

110110
// Measure the text size
111-
FontRectangle size = TextMeasurer.Measure(text, new RichTextOptions(font));
111+
FontRectangle size = TextMeasurer.MeasureSize(text, new RichTextOptions(font));
112112

113113
// Find out how much we need to scale the text to fill the space (up or down)
114114
float scalingFactor = Math.Min(img.Width / size.Width, img.Height / size.Height);
@@ -428,7 +428,7 @@ public void CanDrawTextWithEmptyPath()
428428
Font font = CreateFont(TestFonts.WendyOne, 72);
429429
const string text = "Hello\0World";
430430
RichTextOptions textOptions = new(font);
431-
FontRectangle textSize = TextMeasurer.Measure(text, textOptions);
431+
FontRectangle textSize = TextMeasurer.MeasureSize(text, textOptions);
432432

433433
Assert.NotEqual(FontRectangle.Empty, textSize);
434434

@@ -455,7 +455,7 @@ public void CanRotateFilledFont_Issue175<TPixel>(
455455
AffineTransformBuilder builder = new AffineTransformBuilder().AppendRotationDegrees(angle);
456456

457457
RichTextOptions textOptions = new(font);
458-
FontRectangle bounds = TextMeasurer.Measure(text, textOptions);
458+
FontRectangle bounds = TextMeasurer.MeasureSize(text, textOptions);
459459
Matrix3x2 transform = builder.BuildMatrix(Rectangle.Round(new RectangleF(bounds.X, bounds.Y, bounds.Width, bounds.Height)));
460460

461461
provider.RunValidatingProcessorTest(
@@ -482,7 +482,7 @@ public void CanRotateOutlineFont_Issue175<TPixel>(
482482
AffineTransformBuilder builder = new AffineTransformBuilder().AppendRotationDegrees(angle);
483483

484484
RichTextOptions textOptions = new(font);
485-
FontRectangle bounds = TextMeasurer.Measure(text, textOptions);
485+
FontRectangle bounds = TextMeasurer.MeasureSize(text, textOptions);
486486
Matrix3x2 transform = builder.BuildMatrix(Rectangle.Round(new RectangleF(bounds.X, bounds.Y, bounds.Width, bounds.Height)));
487487

488488
provider.RunValidatingProcessorTest(

tests/ImageSharp.Drawing.Tests/Issues/Issue_46.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public void CanRenderCustomFont()
2828

2929
string iconText = char.ConvertFromUtf32(int.Parse("e926", NumberStyles.HexNumber));
3030

31-
FontRectangle rect = TextMeasurer.Measure(iconText, options);
31+
FontRectangle rect = TextMeasurer.MeasureSize(iconText, options);
3232

3333
float textX = ((imageSize - rect.Width) * 0.5F) + rect.Left;
3434
float textY = ((imageSize - rect.Height) * 0.5F) + (rect.Top * 0.25F);

tests/ImageSharp.Drawing.Tests/Shapes/SvgPath.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public void RenderSvgPath<TPixel>(TestImageProvider<TPixel> provider, string svg
2727
provider.RunValidatingProcessorTest(
2828
c => c.Fill(Color.White).Draw(Color.Red, 5, path),
2929
new { type = exampleImageKey },
30-
comparer: ImageComparer.TolerantPercentage(0.002f));
30+
comparer: ImageComparer.TolerantPercentage(0.0035F)); // NET 472 x86 requires higher percentage
3131
}
3232
}
3333
}
Lines changed: 2 additions & 2 deletions
Loading

0 commit comments

Comments
 (0)