{"record":{"id":"b4198346547b5d3b","repo":"dotnet/wpf","slug":"fileformatexception-glyphtypeface","errorCode":null,"errorMessage":"FileFormatException","messagePattern":"FileFormatException","errorType":"exception","errorClass":"FileFormatException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/GlyphTypeface.cs","lineNumber":145,"sourceCode":"                && 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;\n\n            MS.Internal.Text.TextInterface.FontCollection fontCollection = DWriteFactory.GetFontCollectionFromFile(fontSourceUri);\n            using (MS.Internal.Text.TextInterface.FontFace fontFaceDWrite = DWriteFactory.Instance.CreateFontFace(fontSourceUri,\n                                                                                                                  (uint)faceIndex,\n                                                                                                                  (MS.Internal.Text.TextInterface.FontSimulations)styleSimulations))\n            {\n                // This is the same behavior as 3.*. If we pass, for example, a path to a composite font file then a \n                // FileFormatException will be thrown!\n                if (fontFaceDWrite == null)\n                {\n                    throw new System.IO.FileFormatException(typefaceSource);             \n                }\n                _font = fontCollection.GetFontFromFontFace(fontFaceDWrite);\n            }\n\n            _fontFace = new FontFaceLayoutInfo(_font);\n\n            _fontSource = new FontSource(fontSourceUri);\n\n\n            _initializationState = InitializationState.IsInitialized; // fully initialized\n        }        \n\n        #endregion Constructors\n\n        //------------------------------------------------------\n        //\n        //  Public Methods\n        //","sourceCodeStart":127,"sourceCodeEnd":163,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/GlyphTypeface.cs#L127-L163","documentation":"GlyphTypeface.Initialize throws System.IO.FileFormatException when DirectWrite fails to create a font face from the given URI source (fontFaceDWrite == null). This mirrors WPF 3.x behavior: passing an unsupported file — e.g. a composite font file (.compositeFont) or a path that is not a real TrueType/OpenType font — produces this exception. It means the file exists and was opened, but its contents are not a usable font face.","triggerScenarios":"Calling new GlyphTypeface(uri) or ISupportInitialize EndInit with a URI pointing to a composite font file, a corrupted/truncated font file, a non-font file with a font extension, or a font format DirectWrite cannot parse.","commonSituations":"Deploying an app where the font file got corrupted or replaced during build/packaging; pointing GlyphTypeface at a .compositeFont definition (which is a WPF-level reference list, not a binary font); shipping a placeholder or zero-byte file; passing a resource path that resolves to an HTML error page or manifest instead of a font.","solutions":["Verify the URI points to a genuine TrueType/OpenType (.ttf/.otf/.ttc) binary font, not a composite font definition file","Open the file bytes and check for valid font signatures (0x00010000, 'OTTO', 'ttcf', 'true') before constructing GlyphTypeface","Re-copy the font from a known-good source — the file may be truncated or corrupted","Use a Pack URI that actually embeds the font resource in the assembly (Build Action: Resource) and confirm the resource exists","Catch FileFormatException around GlyphTypeface construction and fall back to a bundled, verified font"],"exampleFix":"// before\nglyphTypeface = new GlyphTypeface(new Uri(\"pack://application:,,,/Fonts/MyFont.compositeFont\"));\n// after\nglyphTypeface = new GlyphTypeface(new Uri(\"pack://application:,,,/Fonts/MyFont.ttf\"));","handlingStrategy":"validation","validationCode":"bool IsValidFontFile(Uri fontUri)\n{\n    byte[] sig = new byte[4];\n    using (var s = File.OpenRead(fontUri.LocalPath))\n    {\n        if (s.Read(sig, 0, 4) < 4) return false;\n    }\n    return sig[0] == 0x00 && sig[1] == 0x01 && sig[2] == 0x00 && sig[3] == 0x00\n        || Encoding.ASCII.GetString(sig) is \"OTTO\" or \"ttcf\" or \"true\";\n}","typeGuard":null,"tryCatchPattern":"try { glyphTypeface = new GlyphTypeface(fontUri); }\ncatch (System.IO.FileFormatException ex)\n{\n    log.Warn($\"Font file {fontUri} is not a usable font face\", ex);\n    glyphTypeface = new GlyphTypeface(fallbackFontUri);\n}","preventionTips":["Never point GlyphTypeface at .compositeFont files; resolve them to actual .ttf/.otf entries","Verify font file signature bytes before loading","Ensure font resources are embedded as Build Action: Resource and referenced via pack URIs","Checksum bundled fonts in CI to catch corrupted packaging"],"tags":["wpf","fonts","file-format","glyphtypeface"],"backgroundTag":"incompatible-source-type","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"}