Skip to content

Commit

Permalink
Add Factory region and move methods before Methods region, to be cons…
Browse files Browse the repository at this point in the history
…istent with Image.cs
  • Loading branch information
cinthamo committed Jul 7, 2024
1 parent 42c07d3 commit 4acbf04
Showing 1 changed file with 36 additions and 31 deletions.
67 changes: 36 additions & 31 deletions src/Common/FontFamily.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,36 +44,6 @@ private FontFamily(FontFamily[] families)
if (typefaces.Count > 0)
m_typefaces = typefaces.ToArray();
}

internal static FontFamily FromFile(string filePath)
{
SKData data = SKData.Create(filePath) ?? throw new FileNotFoundException();
return FromData(data);
}

internal static FontFamily FromStream(Stream stream)
{
SKData data = SKData.Create(stream);
return FromData(data);
}

private static FontFamily FromData(SKData data)
{
int index = 0;
List<SKTypeface> list = new();
while (true)
{
SKTypeface typeface = SKFontManager.Default.CreateTypeface(data, index++);
if (typeface == null)
break;
list.Add(typeface);
}

if (list.Count == 0)
throw new ArgumentException("No typefaces were found.", nameof(data));

return new FontFamily(list.ToArray());
}

/// <summary>
/// Initializes a new <see cref='FontFamily'/> from the specified generic font family.
Expand Down Expand Up @@ -128,7 +98,7 @@ public FontFamily(string name)
/// <exception cref='ArgumentException'><see cref='name'/> is an empty string or the font is not in the collection.</exception>
public FontFamily(string name, FontCollection collection)
: this(Match(name, collection.Families) ?? throw new ArgumentException("missing family from collection", nameof(name))) { }

private static FontFamily[] Match(string name, FontFamily[] families)
{
FontFamily[] matched = families.Where(f => f.Name.Equals(name, StringComparison.OrdinalIgnoreCase)).ToArray();
Expand Down Expand Up @@ -216,7 +186,42 @@ private void Dispose(bool disposing)

#endregion


#region Factory

internal static FontFamily FromFile(string filePath)
{
SKData data = SKData.Create(filePath) ?? throw new FileNotFoundException();
return FromData(data);
}

internal static FontFamily FromStream(Stream stream)
{
SKData data = SKData.Create(stream);
return FromData(data);
}

private static FontFamily FromData(SKData data)
{
int index = 0;
List<SKTypeface> list = new();
while (true)
{
SKTypeface typeface = SKFontManager.Default.CreateTypeface(data, index++);
if (typeface == null)
break;
list.Add(typeface);
}

if (list.Count == 0)
throw new ArgumentException("No typefaces were found.", nameof(data));

return new FontFamily(list.ToArray());
}

#endregion


#region Methods

internal SKTypeface GetTypeface(FontStyle style)
Expand Down

0 comments on commit 4acbf04

Please sign in to comment.