< Summary

Information
Class: Allyaria.Theming.Styles.AllyariaTypography
Assembly: Allyaria.Theming
File(s): /home/runner/work/allyaria/allyaria/src/Allyaria.Theming/Styles/AllyariaTypography.cs
Line coverage
100%
Covered lines: 69
Uncovered lines: 0
Coverable lines: 69
Total lines: 169
Line coverage: 100%
Branch coverage
100%
Covered branches: 26
Total branches: 26
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_FontFamily()100%11100%
get_FontSize()100%11100%
get_FontStyle()100%11100%
get_FontWeight()100%11100%
get_LetterSpacing()100%11100%
get_LineHeight()100%11100%
get_TextAlign()100%11100%
get_TextDecoration()100%11100%
get_TextTransform()100%11100%
get_VerticalAlign()100%11100%
get_WordSpacing()100%11100%
AppendIfNotNull(...)100%22100%
Cascade(...)100%2222100%
ToCss()100%11100%
ToCssVars(...)100%22100%

File(s)

/home/runner/work/allyaria/allyaria/src/Allyaria.Theming/Styles/AllyariaTypography.cs

#LineLine coverage
 1using Allyaria.Theming.Values;
 2using System.Text;
 3using System.Text.RegularExpressions;
 4
 5namespace Allyaria.Theming.Styles;
 6
 7/// <summary>
 8/// Represents a strongly typed typography definition for Allyaria theming. Provides conversion to CSS inline styles or 
 9/// variables.
 10/// </summary>
 11/// <param name="FontFamily">The font family to use (e.g., <c>"Inter, Segoe UI, sans-serif"</c>).</param>
 12/// <param name="FontSize">The font size.</param>
 13/// <param name="FontStyle">The font style (e.g., normal, italic).</param>
 14/// <param name="FontWeight">The font weight (e.g., bold, 400).</param>
 15/// <param name="LetterSpacing">The letter spacing.</param>
 16/// <param name="LineHeight">The line height.</param>
 17/// <param name="TextAlign">The text alignment.</param>
 18/// <param name="TextDecoration">The text decoration.</param>
 19/// <param name="TextTransform">The text transform (e.g., uppercase).</param>
 20/// <param name="VerticalAlign">The vertical alignment.</param>
 21/// <param name="WordSpacing">The word spacing.</param>
 22public readonly record struct AllyariaTypography(
 9423    AllyariaStringValue? FontFamily = null,
 9624    AllyariaStringValue? FontSize = null,
 9225    AllyariaStringValue? FontStyle = null,
 9226    AllyariaStringValue? FontWeight = null,
 9227    AllyariaStringValue? LetterSpacing = null,
 9028    AllyariaStringValue? LineHeight = null,
 9229    AllyariaStringValue? TextAlign = null,
 9230    AllyariaStringValue? TextDecoration = null,
 9231    AllyariaStringValue? TextTransform = null,
 9232    AllyariaStringValue? VerticalAlign = null,
 9033    AllyariaStringValue? WordSpacing = null
 34)
 35{
 36    /// <summary>Appends a CSS declaration to the builder if the value is not null.</summary>
 37    /// <param name="builder">The string builder to append to.</param>
 38    /// <param name="value">The optional string value.</param>
 39    /// <param name="propertyName">The CSS property name.</param>
 40    private static void AppendIfNotNull(StringBuilder builder, AllyariaStringValue? value, string propertyName)
 41    {
 52842        if (value is not null)
 43        {
 9644            builder.Append(value.ToCss(propertyName));
 45        }
 52846    }
 47
 48    /// <summary>
 49    /// Creates a new <see cref="AllyariaTypography" /> instance by cascading the current values with the provided overr
 50    /// Any parameter left <see langword="null" /> will keep the existing value from this instance.
 51    /// </summary>
 52    /// <param name="fontFamily">Optional override for <see cref="AllyariaTypography.FontFamily" />.</param>
 53    /// <param name="fontSize">Optional override for <see cref="AllyariaTypography.FontSize" />.</param>
 54    /// <param name="fontStyle">Optional override for <see cref="AllyariaTypography.FontStyle" />.</param>
 55    /// <param name="fontWeight">Optional override for <see cref="AllyariaTypography.FontWeight" />.</param>
 56    /// <param name="letterSpacing">Optional override for <see cref="AllyariaTypography.LetterSpacing" />.</param>
 57    /// <param name="lineHeight">Optional override for <see cref="AllyariaTypography.LineHeight" />.</param>
 58    /// <param name="textAlign">Optional override for <see cref="AllyariaTypography.TextAlign" />.</param>
 59    /// <param name="textDecoration">Optional override for <see cref="AllyariaTypography.TextDecoration" />.</param>
 60    /// <param name="textTransform">Optional override for <see cref="AllyariaTypography.TextTransform" />.</param>
 61    /// <param name="verticalAlign">Optional override for <see cref="AllyariaTypography.VerticalAlign" />.</param>
 62    /// <param name="wordSpacing">Optional override for <see cref="AllyariaTypography.WordSpacing" />.</param>
 63    /// <returns>A new <see cref="AllyariaTypography" /> instance with the combined values.</returns>
 64    public AllyariaTypography Cascade(AllyariaStringValue? fontFamily = null,
 65        AllyariaStringValue? fontSize = null,
 66        AllyariaStringValue? fontStyle = null,
 67        AllyariaStringValue? fontWeight = null,
 68        AllyariaStringValue? letterSpacing = null,
 69        AllyariaStringValue? lineHeight = null,
 70        AllyariaStringValue? textAlign = null,
 71        AllyariaStringValue? textDecoration = null,
 72        AllyariaStringValue? textTransform = null,
 73        AllyariaStringValue? verticalAlign = null,
 74        AllyariaStringValue? wordSpacing = null)
 75    {
 1676        var newFontFamily = fontFamily ?? FontFamily;
 1677        var newFontSize = fontSize ?? FontSize;
 1678        var newFontStyle = fontStyle ?? FontStyle;
 1679        var newFontWeight = fontWeight ?? FontWeight;
 1680        var newLetterSpacing = letterSpacing ?? LetterSpacing;
 1681        var newLineHeight = lineHeight ?? LineHeight;
 1682        var newTextAlign = textAlign ?? TextAlign;
 1683        var newTextDecoration = textDecoration ?? TextDecoration;
 1684        var newTextTransform = textTransform ?? TextTransform;
 1685        var newVerticalAlign = verticalAlign ?? VerticalAlign;
 1686        var newWordSpacing = wordSpacing ?? WordSpacing;
 87
 1688        return new AllyariaTypography
 1689        {
 1690            FontFamily = newFontFamily,
 1691            FontSize = newFontSize,
 1692            FontStyle = newFontStyle,
 1693            FontWeight = newFontWeight,
 1694            LetterSpacing = newLetterSpacing,
 1695            LineHeight = newLineHeight,
 1696            TextAlign = newTextAlign,
 1697            TextDecoration = newTextDecoration,
 1698            TextTransform = newTextTransform,
 1699            VerticalAlign = newVerticalAlign,
 16100            WordSpacing = newWordSpacing
 16101        };
 102    }
 103
 104    /// <summary>
 105    /// Builds a CSS style string representing this typography. Only appends declarations for non-null properties.
 106    /// </summary>
 107    /// <returns>A concatenated CSS style string.</returns>
 108    public string ToCss()
 109    {
 12110        var builder = new StringBuilder();
 111
 12112        AppendIfNotNull(builder, FontFamily, "font-family");
 12113        AppendIfNotNull(builder, FontSize, "font-size");
 12114        AppendIfNotNull(builder, FontStyle, "font-style");
 12115        AppendIfNotNull(builder, FontWeight, "font-weight");
 12116        AppendIfNotNull(builder, LetterSpacing, "letter-spacing");
 12117        AppendIfNotNull(builder, LineHeight, "line-height");
 12118        AppendIfNotNull(builder, TextAlign, "text-align");
 12119        AppendIfNotNull(builder, TextDecoration, "text-decoration");
 12120        AppendIfNotNull(builder, TextTransform, "text-transform");
 12121        AppendIfNotNull(builder, VerticalAlign, "vertical-align");
 12122        AppendIfNotNull(builder, WordSpacing, "word-spacing");
 123
 12124        return builder.ToString();
 125    }
 126
 127    /// <summary>
 128    /// Builds a CSS custom properties (variables) string representing this typography. The method normalizes the option
 129    /// <paramref name="prefix" /> by trimming whitespace and dashes, converting to lowercase, and replacing spaces with
 130    /// hyphens. If no usable prefix remains, variables are emitted with the default <c>--aa-</c> prefix; otherwise, the
 131    /// computed prefix is applied (e.g., <c>--mytheme-font-size</c>).
 132    /// </summary>
 133    /// <param name="prefix">
 134    /// An optional string used to namespace the CSS variables. May contain spaces or leading/trailing dashes, which are
 135    /// normalized before use. If empty or whitespace, defaults to <c>--aa-</c>.
 136    /// </param>
 137    /// <returns>
 138    /// A concatenated CSS variables string containing only non-null typography properties (e.g., <c>--{prefix}-font-fam
 139    /// , <c>--{prefix}-line-height</c>).
 140    /// </returns>
 141    /// <remarks>
 142    /// Each variable is only appended if its corresponding property is non-null. This keeps the resulting CSS concise a
 143    /// avoids redundant declarations.
 144    /// </remarks>
 145    public string ToCssVars(string prefix = "")
 146    {
 36147        var basePrefix = Regex.Replace(prefix, @"[\s-]+", "-").Trim('-').ToLowerInvariant();
 148
 36149        basePrefix = string.IsNullOrWhiteSpace(prefix)
 36150            ? "--aa-"
 36151            : $"--{basePrefix}-";
 152
 36153        var builder = new StringBuilder();
 154
 36155        AppendIfNotNull(builder, FontFamily, $"{basePrefix}font-family");
 36156        AppendIfNotNull(builder, FontSize, $"{basePrefix}font-size");
 36157        AppendIfNotNull(builder, FontStyle, $"{basePrefix}font-style");
 36158        AppendIfNotNull(builder, FontWeight, $"{basePrefix}font-weight");
 36159        AppendIfNotNull(builder, LetterSpacing, $"{basePrefix}letter-spacing");
 36160        AppendIfNotNull(builder, LineHeight, $"{basePrefix}line-height");
 36161        AppendIfNotNull(builder, TextAlign, $"{basePrefix}text-align");
 36162        AppendIfNotNull(builder, TextDecoration, $"{basePrefix}text-decoration");
 36163        AppendIfNotNull(builder, TextTransform, $"{basePrefix}text-transform");
 36164        AppendIfNotNull(builder, VerticalAlign, $"{basePrefix}vertical-align");
 36165        AppendIfNotNull(builder, WordSpacing, $"{basePrefix}word-spacing");
 166
 36167        return builder.ToString();
 168    }
 169}