{"record":{"id":"91c64cbc80a774df","repo":"stride3d/stride","slug":"failed-to-load-font-fontsource-into-msdfgen","errorCode":null,"errorMessage":"Failed to load font '{fontSource}' into msdfgen.","messagePattern":"Failed to load font '(.+?)' into msdfgen\\.","errorType":"exception","errorClass":"AssetException","httpStatus":null,"severity":"error","filePath":"sources/engine/Stride.Assets/SpriteFont/Compiler/SignedDistanceFieldFontImporter.cs","lineNumber":64,"sourceCode":"\n            int err = FreeTypeNative.FT_Init_FreeType(out var library);\n            if (err != 0)\n                throw new InvalidOperationException($\"Failed to initialize FreeType library (error {err})\");\n\n            msdfgenContext = MsdfgenNative.msdfgenContextCreate();\n            if (msdfgenContext == IntPtr.Zero)\n            {\n                FreeTypeNative.FT_Done_FreeType(library);\n                throw new InvalidOperationException(\"Failed to initialize msdfgen context\");\n            }\n\n            msdfgenFont = MsdfgenNative.msdfgenLoadFont(msdfgenContext, fontSource);\n            if (msdfgenFont == IntPtr.Zero)\n            {\n                MsdfgenNative.msdfgenContextDestroy(msdfgenContext);\n                msdfgenContext = IntPtr.Zero;\n                FreeTypeNative.FT_Done_FreeType(library);\n                throw new AssetException($\"Failed to load font '{fontSource}' into msdfgen.\");\n            }\n\n            try\n            {\n                var fontData = File.ReadAllBytes(fontSource);\n                var handle = GCHandle.Alloc(fontData, GCHandleType.Pinned);\n\n                try\n                {\n                    FT_FaceRec* face;\n                    fixed (byte* ptr = fontData)\n                    {\n                        err = FreeTypeNative.FT_New_Memory_Face(library, ptr, new CLong(fontData.Length), new CLong(0), out face);\n                        if (err != 0)\n                            throw new InvalidOperationException($\"Failed to load font '{fontSource}' (FreeType error {err})\");\n                    }\n\n                    try","sourceCodeStart":46,"sourceCodeEnd":82,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/engine/Stride.Assets/SpriteFont/Compiler/SignedDistanceFieldFontImporter.cs#L46-L82","documentation":"The SignedDistanceFieldFontImporter calls msdfgenLoadFont to hand a font file to the native msdfgen library; when it returns IntPtr.Zero the font could not be parsed/loaded into msdfgen. The importer destroys the msdfgen context, frees FreeType, and throws an AssetException. This means the font file is missing, unreadable, or in a format msdfgen cannot handle.","triggerScenarios":"Asset compilation of a spritefont with FontType = SDF (offline rasterized signed distance field) where MsdfgenNative.msdfgenLoadFont returns a null handle for the given fontSource path (e.g. file does not exist, corrupt TTF/OTF, unsupported font format).","commonSituations":"FontSource path wrong or file deleted from the project; font file with wrong extension or corrupted download; using a font format FreeType/msdfgen cannot parse (e.g. some COLR/CFF2 variable fonts); packaging/copy-to-output rules stripped the .ttf so the path is invalid at build time.","solutions":["Verify the FontSource path is correct and the file exists relative to the asset (check it is included in the build and copied).","Open the font in another tool (e.g. Windows font viewer, fonttools) to confirm it is a valid TTF/OTF.","Re-download/replace the font with a known-good standard TrueType/OpenType file.","Check build logs above this error for FreeType errors that pinpoint the format problem."],"exampleFix":"// before\nFontSource = \"fonts/MyFont.ttf\", // file not in project\n// after\nFontSource = \"fonts/MyFont-Regular.ttf\", // file exists on disk and is included as content","handlingStrategy":"validation","validationCode":"var fontPath = UPath.Combine(assetDirectory, asset.FontSource);\nif (!File.Exists(fontPath.ToOSPath()))\n    throw new FileNotFoundException($\"FontSource not found: {fontPath}\");\nusing var fs = File.OpenRead(fontPath.ToOSPath());\nif (fs.Length < 4) throw new InvalidDataException(\"Font file too small to be TTF/OTF\");","typeGuard":"static bool IsValidFontFile(string path) =>\n    File.Exists(path) && new FileInfo(path).Length > 100;","tryCatchPattern":"try\n{\n    CompileSdfFont(asset);\n}\ncatch (AssetException ex) when (ex.Message.StartsWith(\"Failed to load font\"))\n{\n    logger.Error($\"Check FontSource '{asset.FontSource}': {ex.Message}\");\n}","preventionTips":["Include font files in the project and verify copy-to-output/content settings","Validate fonts with fonttools or a viewer before committing them","Use relative, package-rooted FontSource paths and check them after refactoring"],"tags":["asset-compiler","font","msdfgen","native-interop"],"backgroundTag":"resource-not-found","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}