moodle/moodle · error

Unknown font name "$name". Cannot map to TrueType font file

Error message

Unknown font name "$name". Cannot map to TrueType font file

What it means

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.

Solutions

  1. Register the font mapping: Font::setExtraFontArray(['MyFont' => ['x' => 'myfont.ttf', ...]])
  2. Use a standard font name present in FONT_FILE_NAMES (Arial, Calibri, Verdana, Times New Roman, etc.)
  3. Catch the exception and fall back to approximate (non-GD) width calculation
  4. Upgrade PhpSpreadsheet — newer releases extend the font map

Example fix

// before
$sheet->getStyle('A1')->getFont()->setName('Lato');
$width = $font->calculateColumnWidths(); // throws
// after
Font::setExtraFontArray([
    'Lato' => ['x' => 'Lato-Regular.ttf', 'xb' => 'Lato-Bold.ttf', 'xi' => 'Lato-Italic.ttf', 'xbi' => 'Lato-BoldItalic.ttf'],
]);
$width = $font->calculateColumnWidths();
Defensive patterns

Strategy: fallback

Validate before calling

use PhpOffice\PhpSpreadsheet\Shared\Font;
$name = $font->getName();
$known = array_merge(Font::FONT_FILE_NAMES, Font::getAllFonts() ?? []);
if (!isset($known[$name])) {
    $font->setName('Calibri'); // safe fallback
}

Type guard

function isMappedFont(string $name): bool {
    $map = \PhpOffice\PhpSpreadsheet\Shared\Font::FONT_FILE_NAMES;
    return isset($map[$name]);
}

Try / catch

try {
    $widths = Font::calculateColumnWidths($sheet);
} catch (\PhpOffice\PhpSpreadsheet\Exception $e) {
    if (str_starts_with($e->getMessage(), 'Unknown font name')) {
        // replace font name or register mapping, then retry
    }
    throw $e;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


AI-assisted analysis of moodle/moodle@48ea8c3322 (2026-09-14). Data as JSON: /api/errors/53f490a65e0afb38. Report an issue: GitHub.

Appendix: source

Thrown at public/lib/phpspreadsheet/phpspreadsheet/src/PhpSpreadsheet/Shared/Font.php:550

    {
        return $sizeInCm * 37.795275591;
    }

    /**
     * Returns the font path given the font.
     *
     * @return string Path to TrueType font file
     */
    public static function getTrueTypeFontFileFromFont(FontStyle $font, bool $checkPath = true): string
    {
        if ($checkPath && (!file_exists(self::$trueTypeFontPath) || !is_dir(self::$trueTypeFontPath))) {
            throw new PhpSpreadsheetException('Valid directory to TrueType Font files not specified');
        }

        $name = $font->getName();
        $fontArray = array_merge(self::FONT_FILE_NAMES, self::$extraFontArray);
        if (!isset($fontArray[$name])) {
            throw new PhpSpreadsheetException('Unknown font name "' . $name . '". Cannot map to TrueType font file');
        }
        $bold = $font->getBold();
        $italic = $font->getItalic();
        $index = 'x';
        if ($bold) {
            $index .= 'b';
        }
        if ($italic) {
            $index .= 'i';
        }
        $fontFile = $fontArray[$name][$index];

        $separator = '';
        if (mb_strlen(self::$trueTypeFontPath) > 1 && mb_substr(self::$trueTypeFontPath, -1) !== '/' && mb_substr(self::$trueTypeFontPath, -1) !== '\\') {
            $separator = DIRECTORY_SEPARATOR;
        }
        $fontFileAbsolute = preg_match('~^([A-Za-z]:)?[/\\\]~', $fontFile) === 1;
        if (!$fontFileAbsolute) {

View on GitHub (pinned to 48ea8c3322)