Browse Source
* Add MVAR for variation-aware font-wide metrics GlyphTypeface.Metrics on a varied typeface now reflects the variation point: ascender, descender, line gap, underline thickness/position, and strikeout thickness/position can each carry a per-axis delta. For fonts where these metrics vary substantially (e.g. opsz axis on optical- size fonts, where ascent/descent change meaningfully across sizes), text layout that consults Metrics gets the right values. For fonts like Inter Variable that hold ascent/descent/line gap constant across the weight axis, only the metrics that actually vary (underline + strikeout sizes and positions) are affected — others inherit the source's default-instance value via the "missing record returns no delta" semantics. New MvarTable parser under Tables/Variation: - Header parsing with proper layout: majorVersion, minorVersion, reserved, valueRecordSize, valueRecordCount, itemVariationStoreOffset (the reserved field is a real 16-bit slot in MVAR's header, not the packing detail one might assume — a draft of this PR initially missed it and read every subsequent field at the wrong offset). - ValueRecord array decoded into a Dictionary<OpenTypeTag, (outer, inner)> for O(1) tag lookups. - Wraps the embedded ItemVariationStore (shared infrastructure from pr4d). - TryGetMetricDelta(tag, activeCoords, out delta) returns the delta for a given tag; false when the font doesn't declare a record for it (which is fine — that metric is constant across axis space). - MvarTags companion class with the seven well-known tags this PR maps to FontMetrics fields: hasc, hdsc, hlgp, unds, undo, strs, stro. GlyphTypeface wiring: - Source ctor loads _mvarTable alongside the other variation tables. - Clone ctor reference-shares the table and builds its FontMetrics via ApplyMvarDeltas — a new private static helper that walks each known tag, applies the delta with the correct sign convention (Avalonia stores Ascent/Descent/UnderlinePosition/StrikethroughPosition negated relative to the OpenType raw values, so those tags get subtracted; LineGap and thicknesses are added as-is), and returns a new FontMetrics record. The clone's variation coords need to be projected inside the clone ctor itself, which runs before _activeCoords is assigned — so ApplyMvarDeltas does its own one-shot stackalloc projection. The field's then populated later in the constructor for the gvar / HVAR hot paths. Tested with 13 new tests: - MvarTableTests (6): static-font rejection, variable-font load, axis-count mismatch, Inter Variable's 5 records exactly (stro / strs / undo / unds / xhgt), tags Inter doesn't declare return false, zero-delta at default variation. - GlyphTypefaceMvarMetricsTests (7): static-font no-regression, source metrics at default instance, underline + strikeout thickness grow with weight, ascent/descent/line gap unchanged across Inter weights (verifies the missing-record fallback), default-variation clone returns source, intermediate weight has intermediate metrics. All 508 Media tests + 9 render tests still pass. * Add failing test for MVAR value records larger than 8 bytes A later minor version of MVAR may append fields to each value record, and the spec tells readers to step through the records by valueRecordSize. MvarTable rejects any size other than 8. * Step through MVAR value records by valueRecordSize The MVAR spec allows later minor versions to append fields to each value record and requires readers to locate records by valueRecordSize. MvarTable rejected every size other than 8 and stepped by a fixed 8 bytes. - Accept any valueRecordSize of at least 8 and read the first 8 bytes of each record. - Size the bounds check and the record stride by the declared size.pull/22656/head
committed by
GitHub
4 changed files with 568 additions and 2 deletions
@ -0,0 +1,164 @@ |
|||
using System; |
|||
using System.Buffers.Binary; |
|||
using System.Collections.Generic; |
|||
using System.Diagnostics.CodeAnalysis; |
|||
|
|||
namespace Avalonia.Media.Fonts.Tables.Variation |
|||
{ |
|||
/// <summary>
|
|||
/// Parses the OpenType 'MVAR' (Metrics Variations) table. Provides font-wide metric
|
|||
/// deltas (ascender, descender, line gap, x-height, cap height, underline, strikeout,
|
|||
/// etc.) at a given active variation point.
|
|||
/// </summary>
|
|||
/// <remarks>
|
|||
/// <para>
|
|||
/// MVAR is the cousin of HVAR: where HVAR varies per-glyph horizontal metrics, MVAR
|
|||
/// varies the font-wide constants that line up multiple glyphs into a row of text
|
|||
/// (ascent + descent + line gap → line height; underline / strikeout positions and
|
|||
/// thicknesses; x-height / cap height for vertical alignment).
|
|||
/// </para>
|
|||
/// <para>
|
|||
/// Each MVAR value is addressed by a 4-character tag (e.g. <c>hasc</c> = horizontal
|
|||
/// ascender from OS/2's <c>sTypoAscender</c>, <c>unds</c> = underline thickness from
|
|||
/// post's <c>underlineThickness</c>). The header lists which tags are present along
|
|||
/// with their <c>(outer, inner)</c> coordinates into the embedded
|
|||
/// <see cref="ItemVariationStore"/>; the delta math is identical to HVAR.
|
|||
/// </para>
|
|||
/// <para>
|
|||
/// Reference: <see href="https://learn.microsoft.com/en-us/typography/opentype/spec/mvar"/>.
|
|||
/// </para>
|
|||
/// </remarks>
|
|||
/// <summary>
|
|||
/// Well-known OpenType MVAR value tags. Mapped to <see cref="FontMetrics"/> fields
|
|||
/// in <see cref="GlyphTypeface"/>'s clone constructor. The full list of standard
|
|||
/// tags is larger (~30 entries spanning sub/superscript offsets, caret slope, gasp
|
|||
/// ranges, vertical metrics); the set surfaced here is the one that maps onto our
|
|||
/// public FontMetrics surface today.
|
|||
/// </summary>
|
|||
internal static class MvarTags |
|||
{ |
|||
public static readonly OpenTypeTag HorizontalAscender = OpenTypeTag.Parse("hasc"); |
|||
public static readonly OpenTypeTag HorizontalDescender = OpenTypeTag.Parse("hdsc"); |
|||
public static readonly OpenTypeTag HorizontalLineGap = OpenTypeTag.Parse("hlgp"); |
|||
public static readonly OpenTypeTag UnderlineSize = OpenTypeTag.Parse("unds"); |
|||
public static readonly OpenTypeTag UnderlineOffset = OpenTypeTag.Parse("undo"); |
|||
public static readonly OpenTypeTag StrikeoutSize = OpenTypeTag.Parse("strs"); |
|||
public static readonly OpenTypeTag StrikeoutOffset = OpenTypeTag.Parse("stro"); |
|||
} |
|||
|
|||
internal sealed class MvarTable |
|||
{ |
|||
internal const string TableName = "MVAR"; |
|||
internal static OpenTypeTag Tag { get; } = OpenTypeTag.Parse(TableName); |
|||
|
|||
private const ushort SupportedMajorVersion = 1; |
|||
private const int HeaderSize = 12; |
|||
private const int MinValueRecordSize = 8; |
|||
|
|||
private readonly ItemVariationStore _store; |
|||
// Tag → (outer, inner) lookup map. Built at parse time; constant for the table's
|
|||
// lifetime. We use a Dictionary because the typical valueRecordCount is small
|
|||
// (~5-20) and consumers query a handful of well-known tags — a dictionary keeps
|
|||
// lookups O(1) without a more complex data structure.
|
|||
private readonly Dictionary<OpenTypeTag, (ushort outer, ushort inner)> _records; |
|||
|
|||
private MvarTable( |
|||
ItemVariationStore store, |
|||
Dictionary<OpenTypeTag, (ushort outer, ushort inner)> records) |
|||
{ |
|||
_store = store; |
|||
_records = records; |
|||
} |
|||
|
|||
public ItemVariationStore Store => _store; |
|||
|
|||
public int RecordCount => _records.Count; |
|||
|
|||
public static bool TryLoad( |
|||
GlyphTypeface glyphTypeface, |
|||
int expectedAxisCount, |
|||
[NotNullWhen(true)] out MvarTable? mvarTable) |
|||
{ |
|||
mvarTable = null; |
|||
|
|||
if (!glyphTypeface.PlatformTypeface.TryGetTable(Tag, out var data)) |
|||
{ |
|||
return false; |
|||
} |
|||
|
|||
var span = data.Span; |
|||
if (span.Length < HeaderSize) |
|||
{ |
|||
return false; |
|||
} |
|||
|
|||
var majorVersion = BinaryPrimitives.ReadUInt16BigEndian(span); |
|||
if (majorVersion != SupportedMajorVersion) |
|||
{ |
|||
return false; |
|||
} |
|||
|
|||
// Header layout: majorVersion(2) + minorVersion(2) + reserved(2) +
|
|||
// valueRecordSize(2) + valueRecordCount(2) + itemVariationStoreOffset(2).
|
|||
// minorVersion and reserved are skipped — we already validated majorVersion.
|
|||
var valueRecordSize = BinaryPrimitives.ReadUInt16BigEndian(span.Slice(6)); |
|||
var valueRecordCount = BinaryPrimitives.ReadUInt16BigEndian(span.Slice(8)); |
|||
var ivsOffset = BinaryPrimitives.ReadUInt16BigEndian(span.Slice(10)); |
|||
|
|||
// A version 1.x record starts with the 8 bytes this reader uses. Later minor
|
|||
// versions may append fields, so records are stepped through by
|
|||
// valueRecordSize; anything smaller cannot hold a record.
|
|||
if (valueRecordSize < MinValueRecordSize) |
|||
{ |
|||
return false; |
|||
} |
|||
|
|||
if (HeaderSize + valueRecordCount * valueRecordSize > span.Length) |
|||
{ |
|||
return false; |
|||
} |
|||
|
|||
if (ivsOffset == 0 || ivsOffset >= data.Length) |
|||
{ |
|||
return false; |
|||
} |
|||
|
|||
if (!ItemVariationStore.TryLoad(data.Slice(ivsOffset), expectedAxisCount, out var store)) |
|||
{ |
|||
return false; |
|||
} |
|||
|
|||
var records = new Dictionary<OpenTypeTag, (ushort outer, ushort inner)>(valueRecordCount); |
|||
for (var i = 0; i < valueRecordCount; i++) |
|||
{ |
|||
var recordPos = HeaderSize + i * valueRecordSize; |
|||
var tag = new OpenTypeTag(BinaryPrimitives.ReadUInt32BigEndian(span.Slice(recordPos, 4))); |
|||
var outer = BinaryPrimitives.ReadUInt16BigEndian(span.Slice(recordPos + 4, 2)); |
|||
var inner = BinaryPrimitives.ReadUInt16BigEndian(span.Slice(recordPos + 6, 2)); |
|||
|
|||
// Duplicate tags would be a malformed table; first-seen wins.
|
|||
records.TryAdd(tag, (outer, inner)); |
|||
} |
|||
|
|||
mvarTable = new MvarTable(store, records); |
|||
return true; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Computes the delta for the font-wide metric identified by
|
|||
/// <paramref name="valueTag"/> at the supplied active variation point. Returns
|
|||
/// <c>false</c> when the font doesn't declare a value record for that tag
|
|||
/// (a common case — most variable fonts only vary a subset of metrics).
|
|||
/// </summary>
|
|||
public bool TryGetMetricDelta(OpenTypeTag valueTag, ReadOnlySpan<float> activeCoords, out float delta) |
|||
{ |
|||
if (!_records.TryGetValue(valueTag, out var indices)) |
|||
{ |
|||
delta = 0f; |
|||
return false; |
|||
} |
|||
|
|||
return _store.TryGetDelta(indices.outer, indices.inner, activeCoords, out delta); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,186 @@ |
|||
using System; |
|||
using System.Buffers.Binary; |
|||
using Avalonia.Base.UnitTests.Media.Fonts.Tables; |
|||
using Avalonia.Media; |
|||
using Avalonia.Media.Fonts; |
|||
using Avalonia.Media.Fonts.Tables.Variation; |
|||
using Avalonia.Platform; |
|||
using Avalonia.UnitTests; |
|||
using Xunit; |
|||
|
|||
namespace Avalonia.Base.UnitTests.Media.Fonts.Tables |
|||
{ |
|||
public class MvarTableTests |
|||
{ |
|||
private const string InterRegularAsset = |
|||
"resm:Avalonia.Base.UnitTests.Assets.Inter-Regular.ttf?assembly=Avalonia.Base.UnitTests"; |
|||
|
|||
private const string InterVariableAsset = |
|||
"resm:Avalonia.Base.UnitTests.Assets.InterVariable.ttf?assembly=Avalonia.Base.UnitTests"; |
|||
|
|||
private static GlyphTypeface LoadTypeface(string assetUri) |
|||
{ |
|||
var assetLoader = new StandardAssetLoader(); |
|||
using var stream = assetLoader.Open(new Uri(assetUri)); |
|||
return new GlyphTypeface(new CustomPlatformTypeface(stream)); |
|||
} |
|||
|
|||
[Fact] |
|||
public void TryLoad_Returns_False_For_Static_Font() |
|||
{ |
|||
var typeface = LoadTypeface(InterRegularAsset); |
|||
|
|||
Assert.False(MvarTable.TryLoad(typeface, expectedAxisCount: 0, out var mvar)); |
|||
Assert.Null(mvar); |
|||
} |
|||
|
|||
[Fact] |
|||
public void TryLoad_Returns_True_For_Inter_Variable() |
|||
{ |
|||
var typeface = LoadTypeface(InterVariableAsset); |
|||
|
|||
Assert.True(MvarTable.TryLoad(typeface, expectedAxisCount: 2, out var mvar)); |
|||
Assert.NotNull(mvar); |
|||
Assert.NotNull(mvar!.Store); |
|||
Assert.Equal(2, mvar.Store.AxisCount); |
|||
} |
|||
|
|||
[Fact] |
|||
public void TryLoad_Rejects_Mismatched_Axis_Count() |
|||
{ |
|||
var typeface = LoadTypeface(InterVariableAsset); |
|||
|
|||
Assert.False(MvarTable.TryLoad(typeface, expectedAxisCount: 5, out _)); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Inter_Variable_Declares_Underline_And_Strikeout_Records() |
|||
{ |
|||
// Verified directly against Inter Variable's MVAR binary: 5 records
|
|||
// (stro, strs, undo, unds, xhgt). Ascent/descent/line gap are NOT in MVAR
|
|||
// for Inter — they're held constant across weights.
|
|||
var typeface = LoadTypeface(InterVariableAsset); |
|||
Assert.True(MvarTable.TryLoad(typeface, 2, out var mvar)); |
|||
|
|||
Assert.Equal(5, mvar!.RecordCount); |
|||
|
|||
Span<float> bold = stackalloc float[2] { 0f, 1f }; |
|||
|
|||
// Underline / strikeout deltas should be non-zero at wght=1.0 (Inter's
|
|||
// strokes thicken with weight, so the underline / strikeout sizes do too).
|
|||
Assert.True(mvar.TryGetMetricDelta(MvarTags.UnderlineSize, bold, out var unds)); |
|||
Assert.NotEqual(0f, unds); |
|||
|
|||
Assert.True(mvar.TryGetMetricDelta(MvarTags.StrikeoutSize, bold, out var strs)); |
|||
Assert.NotEqual(0f, strs); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Inter_Variable_Does_Not_Declare_Ascent_Record() |
|||
{ |
|||
// hasc, hdsc, hlgp are absent in Inter Variable's MVAR — TryGetMetricDelta
|
|||
// should return false rather than guess a delta.
|
|||
var typeface = LoadTypeface(InterVariableAsset); |
|||
Assert.True(MvarTable.TryLoad(typeface, 2, out var mvar)); |
|||
|
|||
Span<float> coords = stackalloc float[2] { 0f, 1f }; |
|||
|
|||
Assert.False(mvar!.TryGetMetricDelta(MvarTags.HorizontalAscender, coords, out var d)); |
|||
Assert.Equal(0f, d); |
|||
|
|||
Assert.False(mvar.TryGetMetricDelta(MvarTags.HorizontalDescender, coords, out d)); |
|||
Assert.False(mvar.TryGetMetricDelta(MvarTags.HorizontalLineGap, coords, out d)); |
|||
} |
|||
|
|||
[Fact] |
|||
public void TryGetMetricDelta_Returns_Zero_At_Default_Variation() |
|||
{ |
|||
// At the default-instance point every region's scaler is 0 (peaks are at
|
|||
// axis extrema), so every delta sum is 0.
|
|||
var typeface = LoadTypeface(InterVariableAsset); |
|||
Assert.True(MvarTable.TryLoad(typeface, 2, out var mvar)); |
|||
|
|||
Span<float> defaultCoords = stackalloc float[2] { 0f, 0f }; |
|||
|
|||
Assert.True(mvar!.TryGetMetricDelta(MvarTags.UnderlineSize, defaultCoords, out var delta)); |
|||
Assert.Equal(0f, delta); |
|||
} |
|||
|
|||
[Fact] |
|||
public void TryLoad_Reads_Records_Larger_Than_The_Version_1_0_Layout() |
|||
{ |
|||
// A later minor version may append fields to each value record; readers step
|
|||
// through the records by valueRecordSize and keep reading the first 8 bytes.
|
|||
// Re-encode Inter Variable's MVAR with 10-byte records and compare.
|
|||
var original = LoadTypeface(InterVariableAsset); |
|||
Assert.True(MvarTable.TryLoad(original, 2, out var expected)); |
|||
|
|||
var font = SyntheticFont.FromAsset(SyntheticFont.Assets.InterVariable); |
|||
Assert.True(original.PlatformTypeface.TryGetTable(OpenTypeTag.Parse("MVAR"), out var mvarData)); |
|||
var widened = WidenValueRecords(mvarData.Span, extraBytes: 2); |
|||
|
|||
var typeface = font.Replace("MVAR", widened).TryCreateGlyphTypeface(); |
|||
Assert.NotNull(typeface); |
|||
|
|||
Assert.True(MvarTable.TryLoad(typeface!, 2, out var mvar)); |
|||
Assert.Equal(expected!.RecordCount, mvar!.RecordCount); |
|||
|
|||
Span<float> bold = stackalloc float[2] { 0f, 1f }; |
|||
|
|||
foreach (var tag in new[] { MvarTags.UnderlineSize, MvarTags.StrikeoutSize }) |
|||
{ |
|||
Assert.True(expected.TryGetMetricDelta(tag, bold, out var expectedDelta)); |
|||
Assert.True(mvar.TryGetMetricDelta(tag, bold, out var actualDelta)); |
|||
Assert.Equal(expectedDelta, actualDelta); |
|||
} |
|||
} |
|||
|
|||
[Fact] |
|||
public void TryLoad_Rejects_Records_Smaller_Than_8_Bytes() |
|||
{ |
|||
var original = LoadTypeface(InterVariableAsset); |
|||
Assert.True(original.PlatformTypeface.TryGetTable(OpenTypeTag.Parse("MVAR"), out var mvarData)); |
|||
|
|||
var shrunk = mvarData.ToArray(); |
|||
BinaryPrimitives.WriteUInt16BigEndian(shrunk.AsSpan(6), 6); |
|||
|
|||
var typeface = SyntheticFont.FromAsset(SyntheticFont.Assets.InterVariable) |
|||
.Replace("MVAR", shrunk) |
|||
.TryCreateGlyphTypeface(); |
|||
|
|||
Assert.NotNull(typeface); |
|||
Assert.False(MvarTable.TryLoad(typeface!, 2, out _)); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Rewrites a version 1.0 MVAR table so every value record carries
|
|||
/// <paramref name="extraBytes"/> trailing zero bytes, moving the item variation store
|
|||
/// behind the larger record array.
|
|||
/// </summary>
|
|||
private static byte[] WidenValueRecords(ReadOnlySpan<byte> mvar, int extraBytes) |
|||
{ |
|||
const int headerSize = 12; |
|||
|
|||
var recordSize = BinaryPrimitives.ReadUInt16BigEndian(mvar.Slice(6)); |
|||
var recordCount = BinaryPrimitives.ReadUInt16BigEndian(mvar.Slice(8)); |
|||
var storeOffset = BinaryPrimitives.ReadUInt16BigEndian(mvar.Slice(10)); |
|||
var newRecordSize = recordSize + extraBytes; |
|||
var newStoreOffset = storeOffset + recordCount * extraBytes; |
|||
|
|||
var result = new byte[mvar.Length + recordCount * extraBytes]; |
|||
mvar.Slice(0, headerSize).CopyTo(result); |
|||
BinaryPrimitives.WriteUInt16BigEndian(result.AsSpan(6), (ushort)newRecordSize); |
|||
BinaryPrimitives.WriteUInt16BigEndian(result.AsSpan(10), (ushort)newStoreOffset); |
|||
|
|||
for (var i = 0; i < recordCount; i++) |
|||
{ |
|||
mvar.Slice(headerSize + i * recordSize, recordSize) |
|||
.CopyTo(result.AsSpan(headerSize + i * newRecordSize)); |
|||
} |
|||
|
|||
mvar.Slice(storeOffset).CopyTo(result.AsSpan(newStoreOffset)); |
|||
|
|||
return result; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,130 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using Avalonia.Base.UnitTests.Media.Fonts.Tables; |
|||
using Avalonia.Media; |
|||
using Avalonia.Media.Fonts; |
|||
using Avalonia.Platform; |
|||
using Xunit; |
|||
|
|||
namespace Avalonia.Base.UnitTests.Media |
|||
{ |
|||
/// <summary>
|
|||
/// End-to-end tests for MVAR: <see cref="GlyphTypeface.Metrics"/> on a typeface
|
|||
/// produced by <see cref="GlyphTypeface.WithVariation"/> reflects the variation
|
|||
/// point via MVAR.
|
|||
/// </summary>
|
|||
public class GlyphTypefaceMvarMetricsTests |
|||
{ |
|||
private const string InterRegularAsset = |
|||
"resm:Avalonia.Base.UnitTests.Assets.Inter-Regular.ttf?assembly=Avalonia.Base.UnitTests"; |
|||
|
|||
private const string InterVariableAsset = |
|||
"resm:Avalonia.Base.UnitTests.Assets.InterVariable.ttf?assembly=Avalonia.Base.UnitTests"; |
|||
|
|||
private static readonly OpenTypeTag s_wghtTag = OpenTypeTag.Parse("wght"); |
|||
|
|||
private static GlyphTypeface LoadTypeface(string assetUri) |
|||
{ |
|||
var assetLoader = new StandardAssetLoader(); |
|||
using var stream = assetLoader.Open(new Uri(assetUri)); |
|||
return new GlyphTypeface(new CustomPlatformTypeface(stream)); |
|||
} |
|||
|
|||
private static NormalizedVariationPosition WghtPosition(GlyphTypeface gt, double weight) |
|||
=> gt.CreateNormalizedPosition(new FontVariationSettings( |
|||
new[] { new FontVariation(s_wghtTag, weight) })); |
|||
|
|||
[Fact] |
|||
public void Static_Font_Metrics_Unchanged_Across_WithVariation() |
|||
{ |
|||
// WithVariation returns 'this' for static fonts, so Metrics is trivially
|
|||
// identical. Pins the no-regression promise.
|
|||
var gt = LoadTypeface(InterRegularAsset); |
|||
var alsoGt = gt.WithVariation(NormalizedVariationPosition.FromCoordinates( |
|||
new Dictionary<OpenTypeTag, float> { [s_wghtTag] = 0.5f })); |
|||
|
|||
Assert.Same(gt, alsoGt); |
|||
Assert.Equal(gt.Metrics, alsoGt.Metrics); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Variable_Font_Source_Metrics_Equal_Default_Instance() |
|||
{ |
|||
// The source typeface's Metrics is at the default instance — no MVAR delta
|
|||
// applied. This is what gives the existing PR2 default-instance render tests
|
|||
// their stability.
|
|||
var gt = LoadTypeface(InterVariableAsset); |
|||
|
|||
// Ascent / Descent values are non-zero (sanity).
|
|||
Assert.NotEqual(0, gt.Metrics.Ascent); |
|||
Assert.NotEqual(0, gt.Metrics.Descent); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Underline_Thickness_Grows_With_Weight() |
|||
{ |
|||
// Inter Variable's MVAR declares 'unds' (underline size). At wght=900 the
|
|||
// underline should be thicker than at wght=400.
|
|||
var gt = LoadTypeface(InterVariableAsset); |
|||
var black = gt.WithVariation(WghtPosition(gt, 900)); |
|||
|
|||
Assert.True(black.Metrics.UnderlineThickness > gt.Metrics.UnderlineThickness, |
|||
$"Expected wght=900 underline thickness ({black.Metrics.UnderlineThickness}) " + |
|||
$"> regular ({gt.Metrics.UnderlineThickness})"); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Strikeout_Thickness_Grows_With_Weight() |
|||
{ |
|||
// Same reasoning for strs (strikeout size). Inter Variable declares it.
|
|||
var gt = LoadTypeface(InterVariableAsset); |
|||
var black = gt.WithVariation(WghtPosition(gt, 900)); |
|||
|
|||
Assert.True(black.Metrics.StrikethroughThickness > gt.Metrics.StrikethroughThickness, |
|||
$"Expected wght=900 strikeout thickness ({black.Metrics.StrikethroughThickness}) " + |
|||
$"> regular ({gt.Metrics.StrikethroughThickness})"); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Ascent_And_Descent_Unchanged_Across_Inter_Weights() |
|||
{ |
|||
// Inter Variable doesn't declare hasc/hdsc in MVAR — those metrics are
|
|||
// designed to be constant across weights so all weights share a baseline.
|
|||
// Verifies that missing MVAR records leave the field at the source's value.
|
|||
var gt = LoadTypeface(InterVariableAsset); |
|||
var black = gt.WithVariation(WghtPosition(gt, 900)); |
|||
|
|||
Assert.Equal(gt.Metrics.Ascent, black.Metrics.Ascent); |
|||
Assert.Equal(gt.Metrics.Descent, black.Metrics.Descent); |
|||
Assert.Equal(gt.Metrics.LineGap, black.Metrics.LineGap); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Default_Variation_Clone_Returns_Source_With_Identical_Metrics() |
|||
{ |
|||
// WithVariation(default) on a variable font returns the source itself, so
|
|||
// Metrics is identical by reference equality of the underlying record struct.
|
|||
var gt = LoadTypeface(InterVariableAsset); |
|||
var defaultClone = gt.WithVariation(default); |
|||
|
|||
Assert.Same(gt, defaultClone); |
|||
Assert.Equal(gt.Metrics, defaultClone.Metrics); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Intermediate_Weight_Has_Intermediate_Underline_Thickness() |
|||
{ |
|||
// wght=700 lies between wght=400 (default) and wght=900 along ItemVariationStore's
|
|||
// linear region scaler. Underline thickness should follow the same
|
|||
// monotonic growth.
|
|||
var gt = LoadTypeface(InterVariableAsset); |
|||
var semiBold = gt.WithVariation(WghtPosition(gt, 700)); |
|||
var black = gt.WithVariation(WghtPosition(gt, 900)); |
|||
|
|||
Assert.True(semiBold.Metrics.UnderlineThickness >= gt.Metrics.UnderlineThickness, |
|||
$"semiBold {semiBold.Metrics.UnderlineThickness} should be >= regular {gt.Metrics.UnderlineThickness}"); |
|||
Assert.True(black.Metrics.UnderlineThickness >= semiBold.Metrics.UnderlineThickness, |
|||
$"black {black.Metrics.UnderlineThickness} should be >= semiBold {semiBold.Metrics.UnderlineThickness}"); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue