{"record":{"id":"7821c6b381c94aed","repo":"AvaloniaUI/Avalonia","slug":"font-weight-must-be-0","errorCode":null,"errorMessage":"Font weight must be > 0.","messagePattern":"Font weight must be > 0\\.","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Avalonia.Base/Media/Typeface.cs","lineNumber":28,"sourceCode":"    /// </summary>\n    [DebuggerDisplay(\"Name = {FontFamily.Name}, Weight = {Weight}, Style = {Style}\")]\n    public readonly struct Typeface : IEquatable<Typeface>\n    {\n        /// <summary>\n        /// Initializes a new instance of the <see cref=\"Typeface\"/> class.\n        /// </summary>\n        /// <param name=\"fontFamily\">The font family.</param>\n        /// <param name=\"style\">The font style.</param>\n        /// <param name=\"weight\">The font weight.</param>\n        /// <param name=\"stretch\">The font stretch.</param>\n        public Typeface(FontFamily fontFamily,\n            FontStyle style = FontStyle.Normal,\n            FontWeight weight = FontWeight.Normal,\n            FontStretch stretch = FontStretch.Normal)\n        {\n            if (weight <= 0)\n            {\n                throw new ArgumentException(\"Font weight must be > 0.\");\n            }\n            \n            if ((int)stretch < 1)\n            {\n                throw new ArgumentException(\"Font stretch must be > 1.\");\n            }\n\n            FontFamily = fontFamily ?? FontFamily.Default;\n            Style = style;\n            Weight = weight;\n            Stretch = stretch;\n        }\n\n        /// <summary>\n        /// Initializes a new instance of the <see cref=\"Typeface\"/> class.\n        /// </summary>\n        /// <param name=\"fontFamilyName\">The name of the font family.</param>\n        /// <param name=\"style\">The font style.</param>","sourceCodeStart":10,"sourceCodeEnd":46,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Avalonia.Base/Media/Typeface.cs#L10-L46","documentation":"Thrown by the Typeface constructor when the FontWeight argument is zero or negative. FontWeight must be a positive integer (typical values 100–900 in steps of 100). Avalonia validates this up front because the font matching and glyph pipeline require a meaningful weight.","triggerScenarios":"Constructing a Typeface with weight <= 0, e.g. new Typeface(family, weight: (FontWeight)0) or passing a default/uninitialized FontWeight enum that resolves to 0. Also triggered by casting an arbitrary int to FontWeight that is non-positive.","commonSituations":"Casting a config-driven integer to FontWeight where the value is 0 or negative; default(FontWeight) used before assignment (FontWeight.Normal is 400, but an explicit 0 cast bypasses it); deserialization of a weight field that was not populated.","solutions":["Pass a valid FontWeight such as FontWeight.Normal (400) or FontWeight.Bold (700).","Validate any int-derived weight: if ((int)weight <= 0) weight = FontWeight.Normal.","Ensure deserialization/config provides a weight in the 1–999 range."],"exampleFix":"// before\nvar typeface = new Typeface(family, weight: (FontWeight)0); // throws\n\n// after\nvar typeface = new Typeface(family, weight: FontWeight.Normal);","handlingStrategy":"validation","validationCode":"static Typeface SafeTypeface(FontFamily f, FontStyle st, FontWeight w, FontStretch sr)\n{\n    if ((int)w <= 0) w = FontWeight.Normal;\n    return new Typeface(f, st, w, sr);\n}","typeGuard":"static bool IsValidWeight(FontWeight w) => (int)w > 0;","tryCatchPattern":null,"preventionTips":["Never cast arbitrary non-positive ints to FontWeight.","Default any unconfigured weight to FontWeight.Normal (400).","Validate deserialized weight values before constructing a Typeface."],"tags":["font","typeface","argument-validation","avalonia"],"backgroundTag":null,"analyzedSha":"11c542726898ae954a1ef668c65ec79ec92ab17d","analyzedAt":"2026-08-13T11:57:40.261Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}