// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using System;
using System.Diagnostics;
using System.Globalization;
using SixLabors.Fonts.Unicode;
namespace SixLabors.Fonts.Rendering {
///
/// The combined set of properties that uniquely identify the glyph that is to be rendered
/// at a particular size and dpi.
///
[DebuggerDisplay("GlyphId = {GlyphId}, CodePoint = {CodePoint}, PointSize = {PointSize}, Dpi = {Dpi}")]
public readonly struct GlyphRendererParameters : IEquatable
{
internal GlyphRendererParameters(
FontGlyphMetrics metrics,
TextRun textRun,
float pointSize,
float dpi,
GlyphLayoutMode layoutMode,
int graphemeIndex)
{
this.Font = metrics.FontMetrics.Description.FontNameInvariantCulture?.ToUpper(CultureInfo.InvariantCulture) ?? string.Empty;
this.FontStyle = metrics.FontMetrics.Description.Style;
this.GlyphId = metrics.GlyphId;
this.GraphemeIndex = graphemeIndex;
this.PointSize = pointSize;
this.Dpi = dpi;
this.GlyphType = metrics.GlyphType;
this.TextRun = textRun;
this.CodePoint = metrics.CodePoint;
this.LayoutMode = layoutMode;
}
///
/// Gets the name of the Font this glyph belongs to.
///
public string Font { get; }
///
/// Gets the type of this glyph.
///
public GlyphType GlyphType { get; }
///
/// Gets the style of the font this glyph belongs to.
///
public FontStyle FontStyle { get; }
///
/// Gets the id of the glyph within the font tables.
///
public ushort GlyphId { get; }
///
/// Gets the id of the composite glyph if the is ;
///
public ushort CompositeGlyphId { get; }
///
/// Gets the zero-based grapheme index in the original text.
///
public int GraphemeIndex { get; }
///
/// Gets the codepoint represented by this glyph.
///
public CodePoint CodePoint { get; }
///
/// Gets the rendered point size.
///
public float PointSize { get; }
///
/// Gets the dots-per-inch the glyph is to be rendered at.
///
public float Dpi { get; }
///
/// Gets the layout mode applied to the glyph.
///
public GlyphLayoutMode LayoutMode { get; }
///
/// Gets the text run that this glyph belongs to.
///
public TextRun TextRun { get; }
///
/// Compares two objects for equality.
///
///
/// The on the left side of the operand.
///
///
/// The on the right side of the operand.
///
///
/// True if the current left is equal to the parameter; otherwise, false.
///
public static bool operator ==(GlyphRendererParameters left, GlyphRendererParameters right)
=> left.Equals(right);
///
/// Compares two objects for inequality.
///
///
/// The on the left side of the operand.
///
///
/// The on the right side of the operand.
///
///
/// True if the current left is unequal to the parameter; otherwise, false.
///
public static bool operator !=(GlyphRendererParameters left, GlyphRendererParameters right)
=> !left.Equals(right);
///
public bool Equals(GlyphRendererParameters other)
=> other.PointSize == this.PointSize
&& other.FontStyle == this.FontStyle
&& other.Dpi == this.Dpi
&& other.GlyphId == this.GlyphId
&& other.CompositeGlyphId == this.CompositeGlyphId
&& this.GraphemeIndex == other.GraphemeIndex
&& other.GlyphType == this.GlyphType
&& other.TextRun.TextAttributes == this.TextRun.TextAttributes
&& other.TextRun.TextDecorations == this.TextRun.TextDecorations
&& other.LayoutMode == this.LayoutMode
&& ((other.Font is null && this.Font is null)
|| (other.Font?.Equals(this.Font, StringComparison.OrdinalIgnoreCase) == true));
///
public override bool Equals(object? obj)
=> obj is GlyphRendererParameters parameters && this.Equals(parameters);
///
public override int GetHashCode()
{
int a = HashCode.Combine(
this.Font,
this.PointSize,
this.GlyphId,
this.GlyphType,
this.FontStyle);
int b = HashCode.Combine(
this.Dpi,
this.TextRun.TextAttributes,
this.TextRun.TextDecorations,
this.LayoutMode);
int c = HashCode.Combine(
this.CompositeGlyphId,
this.GraphemeIndex);
return HashCode.Combine(a, b, c);
}
}
}