Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TrueType Font file not found error on Mac with bold/italic font and AUTOSIZE_METHOD_EXACT #3190

Closed
3 of 11 tasks
dregad opened this issue Nov 19, 2022 · 6 comments · Fixed by #3326
Closed
3 of 11 tasks

Comments

@dregad
Copy link

dregad commented Nov 19, 2022

This is:

What is the expected behavior?

Spreadsheet generated with text formatted as defined in default style, with autosized column

What is the current behavior?

Exception: TrueType Font file not found in PhpSpreadsheet/Shared/Font.php on line 557

But the font file is there... it's just named differently. On my Mac, fonts are in /System/Library/Fonts/Supplemental, Arial files are as follows (naming and behaviour is identical with other fonts):

Arial Bold Italic.ttf
Arial Bold.ttf
Arial Italic.ttf
Arial.ttf

instead of the expected

arialbi.ttf
arialbd.ttf
ariali.ttf 
arial.ttf 

which are generated by PhpOffice\PhpSpreadsheet\Shared\Font::getTrueTypeFontFileFromFont().

As a workaround, I aliased the font files, but that's a pain...
Another workaround is to not use AUTOSIZE_METHOD_EXACT, but in this case I actually need it.

It would be nice, considering that this naming is standard on Mac if PhpSpreadsheet were able to automatically use the Bold/Italic/Bold Italic suffix variants in addition to the existing bd/i/bi.

What are the steps to reproduce?

<?php
require_once('vendor/autoload.php');
use PhpOffice\PhpSpreadsheet\Shared\Font;

$xl = new PhpOffice\PhpSpreadsheet\Spreadsheet();
// Set Bold or Italic font (works OK with plain font)
$xl->getDefaultStyle()->getFont()->setName('Arial')->setBold(true);
$xlWS = $xl->getSheet(0);
$xlWS->setCellValue('A1', 'Hello world');
$xlWS->getColumnDimension('A')->setAutoSize(true);
Font::setTrueTypeFontPath('/System/Library/Fonts/Supplemental/');
Font::setAutoSizeMethod(Font::AUTOSIZE_METHOD_EXACT);

$xlWrite = new PhpOffice\PhpSpreadsheet\Writer\Xlsx($xl);
$xlWrite->save('/tmp/test.xlsx');

What features do you think are causing the issue

  • Reader
  • Writer
  • Styles
  • Data Validations
  • Formula Calculations
  • Charts
  • AutoFilter
  • Form Elements

Does an issue affect all spreadsheet file formats? If not, which formats are affected?

Probably yes, but tested only with Xlsx.

Which versions of PhpSpreadsheet and PHP are affected?

Tested with 1.24.1 and 1.25.2 on PHP 8.1.11.

@oleibman
Copy link
Collaborator

I think I see a way we might be able to implement this in a reasonable and testable manner. It would help if you can give me a list of what the file names for each of the entries in Shared\Font::FONT_FILE_NAMES is on your system. I can obviously make a reasonable guess based on the information you've already supplied for Arial, but having the actual names is better than guessing.

@dregad
Copy link
Author

dregad commented Jan 27, 2023

Thanks for looking into it !

list of what the file names for each of the entries in Shared\Font::FONT_FILE_NAMES is on your system

Here you go

<?php
include("vendor/autoload.php"); 
use PhpOffice\PhpSpreadsheet\Shared\Font; 
chdir("/System/Library/Fonts/Supplemental/"); 
foreach(array_keys(Font::FONT_FILE_NAMES) as $f) {
    echo "### $f*\n";
    foreach(glob($f . "*") as $n)
        echo "- $n\n"; 
}

Arial*

  • Arial Black.ttf
  • Arial Bold Italic.ttf
  • Arial Bold.ttf
  • Arial Italic.ttf
  • Arial Narrow Bold Italic.ttf
  • Arial Narrow Bold.ttf
  • Arial Narrow Italic.ttf
  • Arial Narrow.ttf
  • Arial Rounded Bold.ttf
  • Arial Unicode.ttf
  • Arial.ttf

Calibri*

Comic Sans MS*

  • Comic Sans MS Bold.ttf
  • Comic Sans MS.ttf

Courier New*

  • Courier New Bold Italic.ttf
  • Courier New Bold.ttf
  • Courier New Italic.ttf
  • Courier New.ttf

Georgia*

  • Georgia Bold Italic.ttf
  • Georgia Bold.ttf
  • Georgia Italic.ttf
  • Georgia.ttf

Impact*

  • Impact.ttf

Liberation Sans*

Lucida Console*

Lucida Sans Unicode*

Microsoft Sans Serif*

  • Microsoft Sans Serif.ttf

Palatino Linotype*

Symbol*

Tahoma*

  • Tahoma Bold.ttf
  • Tahoma.ttf

Times New Roman*

  • Times New Roman Bold Italic.ttf
  • Times New Roman Bold.ttf
  • Times New Roman Italic.ttf
  • Times New Roman.ttf

Trebuchet MS*

  • Trebuchet MS Bold Italic.ttf
  • Trebuchet MS Bold.ttf
  • Trebuchet MS Italic.ttf
  • Trebuchet MS.ttf

Verdana*

  • Verdana Bold Italic.ttf
  • Verdana Bold.ttf
  • Verdana Italic.ttf
  • Verdana.ttf

@oleibman
Copy link
Collaborator

Thank you. No surprises there, except for the additional Arial fonts (I plan to support "user-defined" fonts in my PR, so I may or may not add them to the "known" list). Also, some are missing from the Mac directory (which won't be any worse a situation than they're in now). I will push my PR tonight, or at least sometime over the weekend,

@dregad
Copy link
Author

dregad commented Jan 27, 2023 via email

oleibman added a commit to oleibman/PhpSpreadsheet that referenced this issue Jan 28, 2023
Fix PHPOffice#3190. A limited set of explicitly-named font files can be used when an exact width calculation is required. User noted that font files are named differently on Mac than on Windows (if the fonts are installed on Linux, the font names probably match Windows). Since the algorithm for generating the Mac file name from the font name seems easy, that algorithm is invoked when the Windows-named file is not found.

Moving on from there, it seems odd that only a small set of fonts are supported. It is, of course, impossible to support all possible fonts out of the box. However, it is possible to allow the user to supply additional mappings from font name to file name (or override existing mappings if neither the Windows nor Mac name matches the user's system), and doing so is permitted with this change:
```php
        \PhpOffice\PhpSpreadsheet\Shared\Font::setExtraFontArray([
            'fontname' => [ /* More than 1 can be specified */
                'x' => 'fontfilenamefornormal.ttf',
                'xb' => 'fontfilenameforbold.ttf',
                'xi' => 'fontfilenameforitalic.ttf',
                'xbi' => 'fontfilenameforbolditalic.ttf',
            ],
        ]);
```
@oleibman
Copy link
Collaborator

My tests are fairly artificial. Can you test against PR #3326 and verify that it meets your needs?

oleibman added a commit that referenced this issue Feb 4, 2023
* Allow More Fonts/Fontnames for Exact Width Calculation

Fix #3190. A limited set of explicitly-named font files can be used when an exact width calculation is required. User noted that font files are named differently on Mac than on Windows (if the fonts are installed on Linux, the font names probably match Windows). Since the algorithm for generating the Mac file name from the font name seems easy, that algorithm is invoked when the Windows-named file is not found.

Moving on from there, it seems odd that only a small set of fonts are supported. It is, of course, impossible to support all possible fonts out of the box. However, it is possible to allow the user to supply additional mappings from font name to file name (or override existing mappings if neither the Windows nor Mac name matches the user's system), and doing so is permitted with this change:
```php
        \PhpOffice\PhpSpreadsheet\Shared\Font::setExtraFontArray([
            'fontname' => [ /* More than 1 can be specified */
                'x' => 'fontfilenamefornormal.ttf',
                'xb' => 'fontfilenameforbold.ttf',
                'xi' => 'fontfilenameforitalic.ttf',
                'xbi' => 'fontfilenameforbolditalic.ttf',
            ],
        ]);
```

* Unexpected Test Difference

... between Windows and Linux.
@dregad
Copy link
Author

dregad commented Feb 4, 2023

@oleibman Apologies, I had a busy week and did not find the time to test the PR in time.

Many thanks for the fix !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

2 participants