{"record":{"id":"798790fc39addd3b","repo":"dotnet/wpf","slug":"sr-urinotabsolute-glyphtypeface","errorCode":null,"errorMessage":"SR.UriNotAbsolute","messagePattern":"SR\\.UriNotAbsolute","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/GlyphTypeface.cs","lineNumber":116,"sourceCode":"            _fontFace = new FontFaceLayoutInfo(font);\n            _fontSource = new FontSource(typefaceSource);\n\n            Invariant.Assert(  styleSimulations == StyleSimulations.None \n                            || styleSimulations == StyleSimulations.ItalicSimulation \n                            || styleSimulations == StyleSimulations.BoldSimulation\n                            || styleSimulations == StyleSimulations.BoldItalicSimulation);\n            \n            _styleSimulations = styleSimulations;\n\n            _initializationState = InitializationState.IsInitialized; // fully initialized\n        }\n\n        private void Initialize(Uri typefaceSource, StyleSimulations styleSimulations)\n        {\n            ArgumentNullException.ThrowIfNull(typefaceSource);\n\n            if (!typefaceSource.IsAbsoluteUri)\n                throw new ArgumentException(SR.UriNotAbsolute, nameof(typefaceSource));\n\n            // remember the original Uri that contains face index\n            _originalUri = typefaceSource;\n\n            // split the Uri into the font source Uri and face index\n            Uri fontSourceUri;\n            int faceIndex;\n            Util.SplitFontFaceIndex(typefaceSource, out fontSourceUri, out faceIndex);\n\n            if (   styleSimulations != StyleSimulations.None \n                && styleSimulations != StyleSimulations.ItalicSimulation \n                && styleSimulations != StyleSimulations.BoldSimulation\n                && styleSimulations != StyleSimulations.BoldItalicSimulation)\n            {\n                throw new InvalidEnumArgumentException(\"styleSimulations\", (int)styleSimulations, typeof(StyleSimulations));\n            }\n\n            _styleSimulations = styleSimulations;","sourceCodeStart":98,"sourceCodeEnd":134,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/GlyphTypeface.cs#L98-L134","documentation":"GlyphTypeface.Initialize (called from the constructor and from ISupportInitialize.EndInit) requires typefaceSource to be an absolute URI (file:///C:/Fonts/segoeui.ttf or pack://...). A relative URI like \"fonts/my.ttf\" or \"segoeui.ttf\" leaves the font unresolvable, so the library throws ArgumentException(SR.UriNotAbsolute, nameof(typefaceSource)) immediately.","triggerScenarios":"new GlyphTypeface(new Uri(\"myfont.ttf\")) or new GlyphTypeface(new Uri(relativePath, UriKind.Relative)); passing a URI built from a config string that is relative to the app directory; EndInit on a GlyphTypeface whose Source was assigned a relative URI.","commonSituations":"Loading fonts from config or app settings where only a file name was stored; project files that used relative content paths; copying samples where the font path string happened to be absolute on the sample machine but not yours; Linux/macOS paths without the file:// scheme.","solutions":["Resolve the font path to an absolute URI before constructing: new GlyphTypeface(new Uri(Path.GetFullPath(fontPath))) or new Uri(fontPath, UriKind.Absolute).","If the path comes from configuration, combine it with AppContext.BaseDirectory: new Uri(Path.Combine(AppContext.BaseDirectory, relativeFontPath)).","Validate with uri.IsAbsoluteUri before calling and surface a clear error otherwise."],"exampleFix":"// before\nvar typeface = new GlyphTypeface(new Uri(\"Fonts/segoeui.ttf\")); // ArgumentException: UriNotAbsolute\n\n// after\nstring fontPath = Path.Combine(AppContext.BaseDirectory, \"Fonts\", \"segoeui.ttf\");\nvar typeface = new GlyphTypeface(new Uri(fontPath, UriKind.Absolute));","handlingStrategy":"validation","validationCode":"static Uri ToAbsoluteFontUri(string fontPath)\n{\n    var uri = new Uri(fontPath, UriKind.RelativeOrAbsolute);\n    if (!uri.IsAbsoluteUri)\n        uri = new Uri(Path.Combine(AppContext.BaseDirectory, fontPath), UriKind.Absolute);\n    return uri;\n}\n// call before: var typeface = new GlyphTypeface(ToAbsoluteFontUri(fontPath));","typeGuard":"static bool IsAbsoluteFontUri(Uri uri) => uri is not null && uri.IsAbsoluteUri;","tryCatchPattern":"try { var typeface = new GlyphTypeface(fontUri); }\ncatch (ArgumentException ex) when (ex.ParamName == \"typefaceSource\")\n{\n    // resolve the configured path against the app base directory and retry once\n}","preventionTips":["Store absolute font paths (or resolve against AppContext.BaseDirectory) wherever font paths come from config.","Always call new Uri(path, UriKind.Absolute) or check IsAbsoluteUri before constructing GlyphTypeface.","On cross-platform apps, build URIs with Path.Combine plus a base directory rather than hard-coded absolute paths."],"tags":["wpf","glyphtypeface","uri","fonts","argument-validation"],"backgroundTag":"invalid-url-format","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}