Skip to content

Commit

Permalink
Merge branch 'main' into sw/list-code-points
Browse files Browse the repository at this point in the history
  • Loading branch information
JimBobSquarePants authored Oct 3, 2023
2 parents e2a0364 + 63db8ad commit 270d0ab
Show file tree
Hide file tree
Showing 14 changed files with 61 additions and 28 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ SixLabors.Fonts

[![Build Status](https://img.shields.io/github/actions/workflow/status/SixLabors/Fonts/build-and-test.yml?branch=main)](https://github.com/SixLabors/Fonts/actions)
[![codecov](https://codecov.io/gh/SixLabors/Fonts/branch/main/graph/badge.svg)](https://codecov.io/gh/SixLabors/Fonts)
[![License: Apache 2.0](https://img.shields.io/badge/license-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)

[![License: Six Labors Split](https://img.shields.io/badge/license-Six%20Labors%20Split-%23e30183)](https://github.com/SixLabors/Fonts/blob/main/LICENSE)
[![GitHub issues](https://img.shields.io/github/issues/SixLabors/Fonts.svg)](https://github.com/SixLabors/Fonts/issues)
[![GitHub stars](https://img.shields.io/github/stars/SixLabors/Fonts.svg)](https://github.com/SixLabors/Fonts/stargazers)
[![GitHub forks](https://img.shields.io/github/forks/SixLabors/Fonts.svg)](https://github.com/SixLabors/Fonts/network)
Expand Down Expand Up @@ -92,7 +91,7 @@ git submodule update --init --recursive
- Loading [WOFF fonts](https://www.w3.org/Submission/WOFF/).
- Loading [WOFF2 fonts](https://www.w3.org/TR/WOFF2).
- Load all compatible fonts from local machine store.
- Suppord for line breaking based on [UAX 14](https://www.unicode.org/reports/tr14/)
- Support for line breaking based on [UAX 14](https://www.unicode.org/reports/tr14/)
- Support for rendering left to right, right to left and bidirectional text.
- Support for ligatures.
- Support for advanced OpenType features glyph substitution ([GSUB](https://docs.microsoft.com/en-us/typography/opentype/spec/gsub)) and glyph positioning ([GPOS](https://docs.microsoft.com/en-us/typography/opentype/spec/gpos))
Expand Down
2 changes: 1 addition & 1 deletion samples/DrawWithImageSharp/DrawWithImageSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="SixLabors.ImageSharp.Drawing" Version="1.0.0-beta15.14" />
<PackageReference Include="SixLabors.ImageSharp.Drawing" Version="2.0.0" />
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
</ItemGroup>

Expand Down
2 changes: 1 addition & 1 deletion samples/DrawWithImageSharp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ public static void RenderTextProcessorWithAlignment(

FontRectangle textSize = TextMeasurer.MeasureSize(text, textOptions);
using var img = new Image<Rgba32>(((int)textSize.Width * 2) + 20, ((int)textSize.Height * 2) + 20);
Size size = img.Size();
Size size = img.Size;
textOptions.Origin = new PointF(size.Width / 2F, size.Height / 2F);

img.Mutate(x => x.Fill(Color.Black).ApplyProcessor(
Expand Down
22 changes: 11 additions & 11 deletions src/SixLabors.Fonts/FontCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ public IEnumerable<FontFamily> AddCollection(Stream stream, out IEnumerable<Font

/// <inheritdoc/>
public FontFamily Get(string name)
=> this.GetImpl(name, CultureInfo.InvariantCulture);
=> this.Get(name, CultureInfo.InvariantCulture);

/// <inheritdoc/>
public bool TryGet(string name, out FontFamily family)
=> this.TryGetImpl(name, CultureInfo.InvariantCulture, out family);
=> this.TryGet(name, CultureInfo.InvariantCulture, out family);

/// <inheritdoc/>
public FontFamily Add(string path, CultureInfo culture)
Expand Down Expand Up @@ -195,14 +195,14 @@ internal void AddSearchDirectories(IEnumerable<string> directories)

private FontFamily AddImpl(string path, CultureInfo culture, out FontDescription description)
{
var instance = new FileFontMetrics(path);
FileFontMetrics instance = new(path);
description = instance.Description;
return ((IFontMetricsCollection)this).AddMetrics(instance, culture);
}

private FontFamily AddImpl(Stream stream, CultureInfo culture, out FontDescription description)
{
var metrics = StreamFontMetrics.LoadFont(stream);
StreamFontMetrics metrics = StreamFontMetrics.LoadFont(stream);
description = metrics.Description;

return ((IFontMetricsCollection)this).AddMetrics(metrics, culture);
Expand All @@ -215,8 +215,8 @@ private IEnumerable<FontFamily> AddCollectionImpl(
{
FileFontMetrics[] fonts = FileFontMetrics.LoadFontCollection(path);

var description = new FontDescription[fonts.Length];
var families = new HashSet<FontFamily>();
FontDescription[] description = new FontDescription[fonts.Length];
HashSet<FontFamily> families = new();
for (int i = 0; i < fonts.Length; i++)
{
description[i] = fonts[i].Description;
Expand All @@ -234,14 +234,14 @@ private IEnumerable<FontFamily> AddCollectionImpl(
out IEnumerable<FontDescription> descriptions)
{
long startPos = stream.Position;
var reader = new BigEndianBinaryReader(stream, true);
var ttcHeader = TtcHeader.Read(reader);
var result = new List<FontDescription>((int)ttcHeader.NumFonts);
var installedFamilies = new HashSet<FontFamily>();
BigEndianBinaryReader reader = new(stream, true);
TtcHeader ttcHeader = TtcHeader.Read(reader);
List<FontDescription> result = new((int)ttcHeader.NumFonts);
HashSet<FontFamily> installedFamilies = new();
for (int i = 0; i < ttcHeader.NumFonts; ++i)
{
stream.Position = startPos + ttcHeader.OffsetTable[i];
var instance = StreamFontMetrics.LoadFont(stream);
StreamFontMetrics instance = StreamFontMetrics.LoadFont(stream);
installedFamilies.Add(((IFontMetricsCollection)this).AddMetrics(instance, culture));
FontDescription fontDescription = instance.Description;
result.Add(fontDescription);
Expand Down
2 changes: 1 addition & 1 deletion src/SixLabors.Fonts/SixLabors.Fonts.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<PackageTags>font;truetype;opentype;woff;woff2</PackageTags>
<Description>A cross-platform library for loading and laying out fonts for processing and measuring; written in C#</Description>
<!--Prevent version conflicts in DrawWithImageSharp-->
<AssemblyVersion Condition="'$(IsContinuousIntegration)'==''">2.0.0.0</AssemblyVersion>
<AssemblyVersion Condition="'$(IsContinuousIntegration)'==''">3.0.0.0</AssemblyVersion>
</PropertyGroup>

<!--This enables the nullable analysis and treats all nullable warnings as error-->
Expand Down
2 changes: 1 addition & 1 deletion src/SixLabors.Fonts/SystemFontCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public SystemFontCollection()
public IEnumerable<string> SearchDirectories => this.searchDirectories;

/// <inheritdoc/>
public FontFamily Get(string name) => this.collection.Get(name);
public FontFamily Get(string name) => this.Get(name, CultureInfo.InvariantCulture);

/// <inheritdoc/>
public bool TryGet(string name, out FontFamily family)
Expand Down
2 changes: 1 addition & 1 deletion src/SixLabors.Fonts/SystemFonts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static class SystemFonts
public static IEnumerable<FontFamily> Families => Collection.Families;

/// <inheritdoc cref="IReadOnlyFontCollection.Get(string)"/>
public static FontFamily Get(string name) => LazySystemFonts.Value.Get(name);
public static FontFamily Get(string name) => Get(name, CultureInfo.InvariantCulture);

/// <inheritdoc cref="IReadOnlyFontCollection.TryGet(string, out FontFamily)" />
public static bool TryGet(string fontFamily, out FontFamily family)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static AttachmentListTable Load(BigEndianBinaryReader reader, long offset
Span<ushort> attachPointOffsets = attachPointOffsetsBuffer.GetSpan();
reader.ReadUInt16Array(attachPointOffsets);

var attachmentListTable = new AttachmentListTable
AttachmentListTable attachmentListTable = new()
{
CoverageTable = CoverageTable.Load(reader, offset + coverageOffset),
AttachPoints = new AttachPoint[glyphCount]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.

using System.Diagnostics.CodeAnalysis;

namespace SixLabors.Fonts.Tables.AdvancedTypographic;

/// <summary>
Expand All @@ -20,6 +22,26 @@ internal abstract class ClassDefinitionTable
/// <returns>The class id.</returns>
public abstract int ClassIndexOf(ushort glyphId);

public static bool TryLoad(BigEndianBinaryReader reader, long offset, [NotNullWhen(true)] out ClassDefinitionTable? table)
{
if (offset == 0)
{
table = null;
return false;
}

reader.Seek(offset, SeekOrigin.Begin);
ushort classFormat = reader.ReadUInt16();
table = classFormat switch
{
1 => ClassDefinitionFormat1Table.Load(reader),
2 => ClassDefinitionFormat2Table.Load(reader),
_ => null
};

return table is not null;
}

public static ClassDefinitionTable Load(BigEndianBinaryReader reader, long offset)
{
reader.Seek(offset, SeekOrigin.Begin);
Expand Down Expand Up @@ -97,7 +119,7 @@ public static ClassDefinitionFormat2Table Load(BigEndianBinaryReader reader)
// | | | startGlyphID |
// +------------------+------------------------------------+-----------------------------------------+
ushort classRangeCount = reader.ReadUInt16();
var records = new ClassRangeRecord[classRangeCount];
ClassRangeRecord[] records = new ClassRangeRecord[classRangeCount];
for (int i = 0; i < records.Length; ++i)
{
// +--------+--------------+------------------------------------+
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,22 +145,20 @@ public static GlyphDefinitionTable Load(BigEndianBinaryReader reader)
throw new InvalidFontFileException($"Invalid value for 'minor version' {minorVersion} of GDEF table. Should be '0', '2' or '3'.");
}

ClassDefinitionTable? classDefinitionTable = glyphClassDefOffset is 0 ? null : ClassDefinitionTable.Load(reader, glyphClassDefOffset);
ClassDefinitionTable.TryLoad(reader, glyphClassDefOffset, out ClassDefinitionTable? classDefinitionTable);
AttachmentListTable? attachmentListTable = attachListOffset is 0 ? null : AttachmentListTable.Load(reader, attachListOffset);
LigatureCaretList? ligatureCaretList = ligatureCaretListOffset is 0 ? null : LigatureCaretList.Load(reader, ligatureCaretListOffset);
ClassDefinitionTable? markAttachmentClassDef = markAttachClassDefOffset is 0 ? null : ClassDefinitionTable.Load(reader, markAttachClassDefOffset);
ClassDefinitionTable.TryLoad(reader, markAttachClassDefOffset, out ClassDefinitionTable? markAttachmentClassDef);
MarkGlyphSetsTable? markGlyphSetsTable = markGlyphSetsDefOffset is 0 ? null : MarkGlyphSetsTable.Load(reader, markGlyphSetsDefOffset);

var glyphDefinitionTable = new GlyphDefinitionTable()
// TODO: read itemVarStore.
return new GlyphDefinitionTable()
{
GlyphClassDefinition = classDefinitionTable,
AttachmentListTable = attachmentListTable,
LigatureCaretList = ligatureCaretList,
MarkAttachmentClassDef = markAttachmentClassDef,
MarkGlyphSetsTable = markGlyphSetsTable
};

// TODO: read itemVarStore.
return glyphDefinitionTable;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public static MarkGlyphSetsTable Load(BigEndianBinaryReader reader, long offset)
{
reader.Seek(offset, SeekOrigin.Begin);

var markGlyphSetsTable = new MarkGlyphSetsTable
MarkGlyphSetsTable markGlyphSetsTable = new()
{
Format = reader.ReadUInt16()
};
Expand Down
12 changes: 12 additions & 0 deletions tests/SixLabors.Fonts.Tests/FontLoaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,16 @@ public void LoadFontWoff()
// the test font only has characters .notdef, 'a' & 'b' defined
Assert.Equal(6, r.ControlPoints.Distinct().Count());
}

[Fact]
public void LoadFontWithIncorrectClassDefinitionTableOffset()
{
// The following font contains a ClassDefinitionTable with an invalid offset.
// See https://forum.stimulsoft.com/viewtopic.php?t=60972
Font font = new FontCollection().Add(TestFonts.THSarabunFile).CreateFont(12);

FontRectangle advance = TextMeasurer.MeasureAdvance("เราใช้คุกกี้เพื่อพัฒนาประสิทธิภาพ และประสบการณ์ที่ดีในการใช้เว็บไซต์ของคุณ คุณสามารถศึกษารายละเอียดได้ที่", new(font));

Assert.NotEqual(default, advance);
}
}
Binary file added tests/SixLabors.Fonts.Tests/Fonts/THSarabun.ttf
Binary file not shown.
2 changes: 2 additions & 0 deletions tests/SixLabors.Fonts.Tests/TestFonts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,8 @@ public static class TestFonts

public static string DFKaiSBFile => GetFullPath("kaiu.ttf");

public static string THSarabunFile => GetFullPath("THSarabun.ttf");

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

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

0 comments on commit 270d0ab

Please sign in to comment.