Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add changes needed to use this library in GeneXus BL #11

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 64 additions & 25 deletions src/Common/Font.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,38 @@
using System;
using System.ComponentModel;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using SkiaSharp;

namespace GeneXus.Drawing;

[TypeConverter(typeof(FontConverter))]
[Serializable]
public class Font : IDisposable, ICloneable
{
internal readonly FontFamily m_family;
internal readonly float m_size;
internal readonly string m_original;

internal readonly string m_original = null;

/// <summary>
/// Initializes a new System.Drawing.Font that uses the specified existing <see cref='Font'/>
/// and <see cref='FontStyle'/> enumeration.
/// </summary>
/// <param name="prototype">The existing <see cref='Font'/> from which to create the new <see cref='Font'/></param>
/// <param name="newStyle">
/// The <see cref='FontStyle'/> to apply to the new S<see cref='Font'/>. Multiple
/// values of the <see cref='FontStyle'/> enumeration can be combined with the OR operator.
/// </param>
public Font(Font prototype, FontStyle newStyle)
{
m_family = prototype.FontFamily;
// TODO set newStyle
}

/// <summary>
/// Initializes a new <see cref='Font'/> using the specified <see cref='Drawing.FontFamily'/> and size.
/// </summary>
Expand All @@ -29,12 +46,20 @@ public Font(FontFamily family, float size = 12)
/// Initializes a new <see cref='Font'/> using the specified family name and size.
/// </summary>
public Font(string familyName, float size = 12)
: this(SystemFonts.Select(f => f.m_family).FirstOrDefault(f => f.MatchFamily(familyName))
?? throw new ArgumentException("missing font family", nameof(familyName)), size)
: this(SystemFonts.Select(f => f.m_family).FirstOrDefault(ff => ff is SkiaFontFamily sff && sff.MatchFamily(familyName)) ?? new UnknownFontFamily(familyName), size)
{
m_original = familyName;
}

/// <summary>
/// Initializes a new <see cref='Font'/> using the specified family name, size, styl.
/// </summary>
public Font(string familyName, float size, FontStyle style, GraphicsUnit unit)
: this(familyName, size)
{
// TODO set style and unit
}

/// <summary>
/// Cleans up resources for this <see cref='Font'/>.
/// </summary>
Expand All @@ -45,7 +70,7 @@ public Font(string familyName, float size = 12)
/// </summary>
public override string ToString()
{
string suffix = m_family.m_index > 0 ? $"#{m_family.m_index}" : string.Empty;
string suffix = m_family is SkiaFontFamily { m_index: > 0 } sff ? $"#{sff.m_index}" : string.Empty;
return $"[{GetType().Name}: Name={Name}{suffix}, Size={Size}]";
}

Expand All @@ -69,16 +94,9 @@ public void Dispose()
#region IClonable

/// <summary>
/// Creates an exact copy of this <see cref='Font'/>.
/// Creates an exact copy of this <see cref='Font'/>.
/// </summary>
public object Clone()
{
var index = m_family.m_index;
var bytes = m_family.m_data.ToArray();
var data = SKData.CreateCopy(bytes);
var family = new FontFamily(data, index);
return new Font(family);
}
public object Clone() => new Font((FontFamily)m_family.Clone());

#endregion

Expand All @@ -88,19 +106,17 @@ public object Clone()
/// <summary>
/// Creates a <see cref='SKFont'/> with the coordinates of the specified <see cref='Font'/> .
/// </summary>
public static explicit operator SKFont(Font font) => font.m_family.GetFont(font.m_size);
public static explicit operator SKFont(Font font) => (font.m_family as SkiaFontFamily)?.GetFont(font.m_size);

#endregion


#region Properties

private SKFont m_font => m_family.GetFont(m_size);

/// <summary>
/// Gets the face name of this <see cref='Font'/>.
/// </summary>
public string Name => $"{m_family.Name} {m_family.Face}";
public string Name => m_family is SkiaFontFamily sff ? $"{sff.Name} {sff.Face}" : m_family.Name;

/// <summary>
/// Gets the name of the <see cref='Font'/> originally specified.
Expand Down Expand Up @@ -176,22 +192,24 @@ public FontStyle Style
/// <summary>
/// Gets a value that indicates whether this <see cref='Font'/> has the italic style applied.
/// </summary>
public bool Italic => m_family.m_typeface.IsItalic;
public bool Italic => m_family.IsItalic;

/// <summary>
/// Gets a value that indicates whether this <see cref='Font'/> is bold.
/// </summary>
public bool Bold => m_family.m_typeface.IsBold;
public bool Bold => m_family.IsBold;

private SKFontMetrics Metrics => m_family is SkiaFontFamily sff ? sff.GetFont(m_size).Metrics : new SKFontMetrics();

/// <summary>
/// Gets a value indicating whether this <see cref='Font'/> is underlined.
/// </summary>
public bool Underline => m_font.Metrics.UnderlineThickness > 0 && m_font.Metrics.UnderlinePosition == 0f;
public bool Underline => Metrics is { UnderlineThickness: > 0, UnderlinePosition: 0f };

/// <summary>
/// Gets a value indicating whether this <see cref='Font'/> is strikeout (has a line through it).
/// </summary>
public bool Strikeout => m_font.Metrics.StrikeoutThickness > 0 && m_font.Metrics.StrikeoutPosition == 0f;
public bool Strikeout => Metrics is { StrikeoutThickness: > 0, StrikeoutPosition: 0f };

/// <summary>
/// Gets a value indicating whether the <see cref='Font'/> is a member of SystemFonts.
Expand All @@ -217,8 +235,29 @@ public static ICollection<Font> SystemFonts
}
}

private static ICollection<Font> s_SystemFonts = null;
private static ICollection<Font> s_SystemFonts;

/// <summary>
/// Gets the em-size, in points, of this <see cref='Font'/>.
/// </summary>
/// <returns>The em-size, in points, of this <see cref='Font'/></returns>
[Browsable(false)]
public float SizeInPoints
{
get
{
// TODO calculate it for other units
Debug.Assert(Unit == GraphicsUnit.Point);
return Size;
}
}

/// <summary>
/// Gets the unit of measure for this <see cref='Font'/>.
/// </summary>
/// <returns>A <see cref='GraphicsUnit'/> that represents the unit of measure for this <see cref='Font'/>.</returns>
public GraphicsUnit Unit => GraphicsUnit.Point;

#endregion


Expand All @@ -227,7 +266,7 @@ public static ICollection<Font> SystemFonts
/// <summary>
/// Returns the line spacing of this <see cref='Font'/>.
/// </summary>
public float GetHeight() => m_font.Metrics.Descent - m_font.Metrics.Ascent + m_font.Metrics.Leading;
public float GetHeight() => Metrics.Descent - Metrics.Ascent + Metrics.Leading;

/// <summary>
/// Returns a <see cref='Font'/> collection in the specified location.
Expand All @@ -240,7 +279,7 @@ public static ICollection<Font> GetFonts(string location)
{
if (FONT_EXTENSIONS.Contains(Path.GetExtension(fontFile)))
{
var family = new FontFamily(fontFile);
var family = FontFamilyFactory.Create(fontFile);
var font = new Font(family);
fonts.Add(font);
}
Expand Down
Loading
Loading