Table of Contents

Struct CssFontWeight

Namespace
Egami.Css
Assembly
Egami.Css.dll

A font-weight value.

public readonly record struct CssFontWeight : IEquatable<CssFontWeight>
Implements
Inherited Members

Examples

bool isFont = CssFont.TryParse("italic bold 14px/1.4 \"Source Sans 3\", sans-serif", out CssFont? font);
CssFontWeight weight = font!.Weight;                     // 700
foreach (CssFontFamily family in font.Families!.Families)
{
    Console.WriteLine($"{family.Name} (generic: {family.IsGeneric})");
}

bool isFamilyList = CssFontFamilyList.TryParse("Georgia, serif", out CssFontFamilyList? families);
bool isWeight = CssFontWeight.TryParse("bolder", out CssFontWeight bolder);
double resolved = bolder.Kind == CssFontWeightKind.Bolder ? bolder.Resolve(400) : bolder.Value; // 700

// @font-face descriptors.
CssStyleSheet sheet = CssStyleSheet.Parse("""
    @font-face {
      font-family: "Inter";
      src: local("Inter"), url(inter.woff2) format("woff2") tech(variations);
      font-weight: 100 900;
      unicode-range: U+0000-00FF, U+20AC;
    }
    """);
CssFontFace face = ((CssFontFaceRule)sheet.Rules[0]).FontFace;
CssFontSource source = face.Sources[1];                  // inter.woff2, format woff2, tech variations
CssRange weights = face.Weight!.Value;                   // 100 to 900
CssUnicodeRange latin = face.UnicodeRanges[0];           // U+0-FF

Constructors

CssFontWeight(double, CssFontWeightKind)

A font-weight value.

public CssFontWeight(double Value, CssFontWeightKind Kind = CssFontWeightKind.Absolute)

Parameters

Value double

The weight from 1 to 1000 for Absolute; 0 otherwise.

Kind CssFontWeightKind

Whether the weight is absolute or relative.

Examples

bool isFont = CssFont.TryParse("italic bold 14px/1.4 \"Source Sans 3\", sans-serif", out CssFont? font);
CssFontWeight weight = font!.Weight;                     // 700
foreach (CssFontFamily family in font.Families!.Families)
{
    Console.WriteLine($"{family.Name} (generic: {family.IsGeneric})");
}

bool isFamilyList = CssFontFamilyList.TryParse("Georgia, serif", out CssFontFamilyList? families);
bool isWeight = CssFontWeight.TryParse("bolder", out CssFontWeight bolder);
double resolved = bolder.Kind == CssFontWeightKind.Bolder ? bolder.Resolve(400) : bolder.Value; // 700

// @font-face descriptors.
CssStyleSheet sheet = CssStyleSheet.Parse("""
    @font-face {
      font-family: "Inter";
      src: local("Inter"), url(inter.woff2) format("woff2") tech(variations);
      font-weight: 100 900;
      unicode-range: U+0000-00FF, U+20AC;
    }
    """);
CssFontFace face = ((CssFontFaceRule)sheet.Rules[0]).FontFace;
CssFontSource source = face.Sources[1];                  // inter.woff2, format woff2, tech variations
CssRange weights = face.Weight!.Value;                   // 100 to 900
CssUnicodeRange latin = face.UnicodeRanges[0];           // U+0-FF

Properties

Bold

Gets bold: 700.

public static CssFontWeight Bold { get; }

Property Value

CssFontWeight

Kind

Whether the weight is absolute or relative.

public CssFontWeightKind Kind { get; init; }

Property Value

CssFontWeightKind

Normal

Gets normal: 400.

public static CssFontWeight Normal { get; }

Property Value

CssFontWeight

Value

The weight from 1 to 1000 for Absolute; 0 otherwise.

public double Value { get; init; }

Property Value

double

Methods

Resolve(double)

Resolves the weight against the parent's computed weight (CSS Fonts 4, "Determination of bolder and lighter").

public double Resolve(double parentWeight)

Parameters

parentWeight double

The parent element's weight.

Returns

double

The absolute weight.

ToString()

Formats the weight as CSS text.

public override string ToString()

Returns

string

The text.

TryParse(CssComponentValue, out CssFontWeight)

Parses one component value as a font weight.

public static bool TryParse(CssComponentValue value, out CssFontWeight weight)

Parameters

value CssComponentValue

The component value.

weight CssFontWeight

The weight.

Returns

bool

True when valid.

TryParse(IReadOnlyList<CssComponentValue>, out CssFontWeight)

Parses component values as a font weight.

public static bool TryParse(IReadOnlyList<CssComponentValue> values, out CssFontWeight weight)

Parameters

values IReadOnlyList<CssComponentValue>

The component values.

weight CssFontWeight

The weight.

Returns

bool

True when valid.

TryParse(string, out CssFontWeight)

Parses normal, bold, bolder, lighter or a number from 1 to 1000.

public static bool TryParse(string text, out CssFontWeight weight)

Parameters

text string

The CSS text.

weight CssFontWeight

The weight.

Returns

bool

True when valid.