Skip to content

Commit

Permalink
Merge pull request #379 from SixLabors/js/fix-378
Browse files Browse the repository at this point in the history
Prevent key collision in glyphmetrics cache.
  • Loading branch information
JimBobSquarePants authored Feb 7, 2024
2 parents d5d4a3b + c63913c commit fa8a4c9
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 6 deletions.
3 changes: 1 addition & 2 deletions src/SixLabors.Fonts/ArrayBuilder{T}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,13 @@ internal struct ArrayBuilder<T>
/// <summary>
/// Initializes a new instance of the <see cref="ArrayBuilder{T}"/> struct.
/// </summary>
/// <param name="capacity">The intitial capacity of the array.</param>
/// <param name="capacity">The initial capacity of the array.</param>
public ArrayBuilder(int capacity)
: this()
{
Guard.MustBeGreaterThanOrEqualTo(capacity, 0, nameof(capacity));

this.data = new T[capacity];
this.size = capacity;
}

/// <summary>
Expand Down
8 changes: 4 additions & 4 deletions src/SixLabors.Fonts/StreamFontMetrics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ internal partial class StreamFontMetrics : FontMetrics
private readonly OutlineType outlineType;

// https://docs.microsoft.com/en-us/typography/opentype/spec/otff#font-tables
private readonly ConcurrentDictionary<(ushort Id, TextAttributes Attributes, bool IsVerticalLayout), GlyphMetrics[]> glyphCache;
private readonly ConcurrentDictionary<(ushort Id, TextAttributes Attributes, bool IsVerticalLayout), GlyphMetrics[]>? colorGlyphCache;
private readonly ConcurrentDictionary<(int CodePoint, ushort Id, TextAttributes Attributes, bool IsVerticalLayout), GlyphMetrics[]> glyphCache;
private readonly ConcurrentDictionary<(int CodePoint, ushort Id, TextAttributes Attributes, bool IsVerticalLayout), GlyphMetrics[]>? colorGlyphCache;
private readonly FontDescription description;
private readonly HorizontalMetrics horizontalMetrics;
private readonly VerticalMetrics verticalMetrics;
Expand Down Expand Up @@ -529,12 +529,12 @@ public static StreamFontMetrics[] LoadFontCollection(Stream stream)
return fonts;
}

private static (ushort Id, TextAttributes Attributes, bool IsVerticalLayout) CreateCacheKey(
private static (int CodePoint, ushort Id, TextAttributes Attributes, bool IsVerticalLayout) CreateCacheKey(
CodePoint codePoint,
ushort glyphId,
TextAttributes textAttributes,
LayoutMode layoutMode)
=> (glyphId, textAttributes, AdvancedTypographicUtils.IsVerticalGlyph(codePoint, layoutMode));
=> (codePoint.Value, glyphId, textAttributes, AdvancedTypographicUtils.IsVerticalGlyph(codePoint, layoutMode));

private bool TryGetColoredMetrics(
CodePoint codePoint,
Expand Down
Binary file not shown.
19 changes: 19 additions & 0 deletions tests/SixLabors.Fonts.Tests/Issues/Issues_378.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.

namespace SixLabors.Fonts.Tests.Issues;
public class Issues_378
{
[Fact]
public void DoesNotBreakIncorrectly()
{
Font font = new FontCollection().Add(TestFonts.PlantinStdRegularFile).CreateFont(2048);

TextOptions options = new(font) { WrappingLength = float.MaxValue };
FontRectangle size = TextMeasurer.MeasureSize("D\r\nD", options);

FontRectangle size2 = TextMeasurer.MeasureSize("D D", options);

Assert.NotEqual(size, size2);
}
}
2 changes: 2 additions & 0 deletions tests/SixLabors.Fonts.Tests/TestFonts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,8 @@ public static class TestFonts

public static string BNazaninFile => GetFullPath("BNazanin.ttf");

public static string PlantinStdRegularFile => GetFullPath("PlantinStdRegular.otf");

public static Stream TwemojiMozillaData() => OpenStream(TwemojiMozillaFile);

public static Stream SegoeuiEmojiData() => OpenStream(SegoeuiEmojiFile);
Expand Down

0 comments on commit fa8a4c9

Please sign in to comment.