{"record":{"id":"53f490a65e0afb38","repo":"moodle/moodle","slug":"unknown-font-name-name-cannot-map-to-truetype-font-file","errorCode":null,"errorMessage":"Unknown font name \"$name\". Cannot map to TrueType font file","messagePattern":"Unknown font name \"\\$name\"\\. Cannot map to TrueType font file","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"public/lib/phpspreadsheet/phpspreadsheet/src/PhpSpreadsheet/Shared/Font.php","lineNumber":550,"sourceCode":"    {\n        return $sizeInCm * 37.795275591;\n    }\n\n    /**\n     * Returns the font path given the font.\n     *\n     * @return string Path to TrueType font file\n     */\n    public static function getTrueTypeFontFileFromFont(FontStyle $font, bool $checkPath = true): string\n    {\n        if ($checkPath && (!file_exists(self::$trueTypeFontPath) || !is_dir(self::$trueTypeFontPath))) {\n            throw new PhpSpreadsheetException('Valid directory to TrueType Font files not specified');\n        }\n\n        $name = $font->getName();\n        $fontArray = array_merge(self::FONT_FILE_NAMES, self::$extraFontArray);\n        if (!isset($fontArray[$name])) {\n            throw new PhpSpreadsheetException('Unknown font name \"' . $name . '\". Cannot map to TrueType font file');\n        }\n        $bold = $font->getBold();\n        $italic = $font->getItalic();\n        $index = 'x';\n        if ($bold) {\n            $index .= 'b';\n        }\n        if ($italic) {\n            $index .= 'i';\n        }\n        $fontFile = $fontArray[$name][$index];\n\n        $separator = '';\n        if (mb_strlen(self::$trueTypeFontPath) > 1 && mb_substr(self::$trueTypeFontPath, -1) !== '/' && mb_substr(self::$trueTypeFontPath, -1) !== '\\\\') {\n            $separator = DIRECTORY_SEPARATOR;\n        }\n        $fontFileAbsolute = preg_match('~^([A-Za-z]:)?[/\\\\\\]~', $fontFile) === 1;\n        if (!$fontFileAbsolute) {","sourceCodeStart":532,"sourceCodeEnd":568,"githubUrl":"https://github.com/moodle/moodle/blob/48ea8c33227277e916cdd4079dbecef448b37f2c/public/lib/phpspreadsheet/phpspreadsheet/src/PhpSpreadsheet/Shared/Font.php#L532-L568","documentation":"getTrueTypeFontFileFromFont() maps font names (Arial, Calibri, Times New Roman, ...) to TTF filenames via FONT_FILE_NAMES plus any user-registered extras (extraFontArray). A font name outside that map throws this exception.","triggerScenarios":"Setting a cell font to a name like 'Helvetica Neue', 'Lato', or a theme font (e.g. 'Aptos' in newer XLSX) and then performing GD-based width calculation/text measurement with $checkPath path enabled.","commonSituations":"Reading modern XLSX files whose theme fonts aren't in PhpSpreadsheet's mapping; user-set custom fonts; newer PhpSpreadsheet readers writing theme fonts the GD measurement layer doesn't know.","solutions":["Register the font mapping: Font::setExtraFontArray(['MyFont' => ['x' => 'myfont.ttf', ...]])","Use a standard font name present in FONT_FILE_NAMES (Arial, Calibri, Verdana, Times New Roman, etc.)","Catch the exception and fall back to approximate (non-GD) width calculation","Upgrade PhpSpreadsheet — newer releases extend the font map"],"exampleFix":"// before\n$sheet->getStyle('A1')->getFont()->setName('Lato');\n$width = $font->calculateColumnWidths(); // throws\n// after\nFont::setExtraFontArray([\n    'Lato' => ['x' => 'Lato-Regular.ttf', 'xb' => 'Lato-Bold.ttf', 'xi' => 'Lato-Italic.ttf', 'xbi' => 'Lato-BoldItalic.ttf'],\n]);\n$width = $font->calculateColumnWidths();","handlingStrategy":"fallback","validationCode":"use PhpOffice\\PhpSpreadsheet\\Shared\\Font;\n$name = $font->getName();\n$known = array_merge(Font::FONT_FILE_NAMES, Font::getAllFonts() ?? []);\nif (!isset($known[$name])) {\n    $font->setName('Calibri'); // safe fallback\n}","typeGuard":"function isMappedFont(string $name): bool {\n    $map = \\PhpOffice\\PhpSpreadsheet\\Shared\\Font::FONT_FILE_NAMES;\n    return isset($map[$name]);\n}","tryCatchPattern":"try {\n    $widths = Font::calculateColumnWidths($sheet);\n} catch (\\PhpOffice\\PhpSpreadsheet\\Exception $e) {\n    if (str_starts_with($e->getMessage(), 'Unknown font name')) {\n        // replace font name or register mapping, then retry\n    }\n    throw $e;\n}","preventionTips":["Register custom fonts with Font::setExtraFontArray() before measuring","Restrict allowed font names in user-facing styling options","Normalize theme fonts (Aptos, Calibri variants) to mapped families after loading files","Keep PhpSpreadsheet updated for extended FONT_FILE_NAMES coverage"],"tags":["phpspreadsheet","font","configuration"],"backgroundTag":"invalid-enum-value","analyzedSha":"48ea8c33227277e916cdd4079dbecef448b37f2c","analyzedAt":"2026-09-14T20:34:48.527Z","contentChangedAt":"2026-09-14T20:34:48.527Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}