using System;
using Amphora;
using Azimuth;
using Blacklight.Core.Drawables;
using Glass.Text;
using Prism;
using Tessera.Text;
namespace Glass.Blacklight
{
///
/// The tint and coverage gamma that text draws with where no span says otherwise.
///
public struct BaseTint
{
public Color Color;
public Scalar Gamma;
// Null for no tint texture, in which case its transform and space make no difference.
public ITexture Texture;
public AffineMatrix Transform;
public TintSpace Space;
}
///
/// Works out the tint color and coverage gamma the glyphs of each span of some text draw with.
///
public class SpanTints
{
private Color[] colors = Array.Empty();
private Scalar[] gammas = Array.Empty();
private BaseTint baseTint;
///
/// Gets the per-glyph properties that some span draws differently from the base, which a sprite batch has to
/// vary. A span that sets one to what the base already is doesn't count.
///
public SpriteBatch.Varying Varying { get; private set; }
///
/// Returns the tint color of a span's glyphs, where -1 stands for the glyphs no span covers.
///
public Color ColorOf(int span)
{
return span < 0 ? baseTint.Color : colors[span];
}
///
/// Returns the coverage gamma of a span's glyphs, where -1 stands for the glyphs no span covers.
///
public Scalar GammaOf(int span)
{
return span < 0 ? baseTint.Gamma : gammas[span];
}
///
/// Works out each span's tint over `baseTint`, replacing whatever was worked out before.
///
/// Whether the base color beats every span's, as a render override does.
public void Resolve(DynamicArray spans, in BaseTint baseTint, bool colorOverridden)
{
this.baseTint = baseTint;
Varying = SpriteBatch.Varying.None;
int count = spans?.Count ?? 0;
if (colors.Length < count)
{
int length = Math.Max(count, 2 * colors.Length);
colors = new Color[length];
gammas = new Scalar[length];
}
for (int i = 0; i < count; ++i)
{
TextStyle style = spans[i].Style;
Color color = baseTint.Color;
if (!colorOverridden && style.TintColor is Color spanColor)
{
color = spanColor;
}
if (color.ToArgb() != baseTint.Color.ToArgb())
{
Varying |= SpriteBatch.Varying.TintColor;
}
Scalar gamma = style.CoverageGamma ?? baseTint.Gamma;
if (gamma != baseTint.Gamma)
{
Varying |= SpriteBatch.Varying.CoverageGamma;
}
colors[i] = color;
gammas[i] = gamma;
}
}
}
}