altmany / export_fig

A MATLAB toolbox for exporting publication quality figures
BSD 3-Clause "New" or "Revised" License
1.28k stars 368 forks source link

Can't include font in eps/pdf #336

Closed them3chanic closed 3 years ago

them3chanic commented 3 years ago

Hi altmany,

thanks for your great work. I tried to get a custom font included with export_fig without any results. I'd like to include the font "Frutiger 45 Light" into my eps/pdf files, but I seem to encounter a bug:

Got any ideas what happens here? Shouldn't be there font names in the EPS file when I use the option -nofontswap?

The Matlab code:

figure(1);
subplot(1,2,1);
plot(x,xy,'k');
title('Plot1')
xlabel('x')
ylabel('y')
set(gcf, 'Color', 'w');
set(gca, 'FontName', 'Frutiger 45 Light')

subplot(1,2,2);
plot(y,yy,'k');
title('Plot2')
xlabel('x')
ylabel('y')
set(gca, 'FontName', 'Frutiger 45 Light')

% export_fig('test', figure(1), '-eps', '-pdf', '-nofontswap', '-font_space', '-');
export_fig('test', figure(1), '-eps', '-pdf');

Best regards themech

them3chanic commented 3 years ago

Found the answer after all:

  1. You have to use the Filename without .tff
  2. For Fonts that won't use a standard name space for bold, italic and so on you have to replace wrong namings manually. Therefore I used the -regexprep command
altmany commented 3 years ago

@them3chanic - thanks for the update. Please let me know exactly what code changes you made and which export_fig parameters you've used to fix the problem, so that I could detect and correct such issues automatically within export_fig.

them3chanic commented 3 years ago

Hi Altmany,

MATLAB recognizes the Font as"Frutiger 45 Light". Ghostscript will find it with "Frutiger-Light". If you use the font and want to use the bold version, it would have to use "Frutiger-Bold". Your script tries to use Frutiger-Light-Bold, therefore I replaced it with:

export_fig('TestFile', fig1, '-eps', '-pdf', '-font_space', '', '-regexprep', 'Frutiger-Light-Bold', 'Frutiger-Bold');

I believe this is rather a workaround than a real bug fix. As you mentioned yourself in a code comment a real fix is a bit more sophisticated. One solution might be to get all fonts with "listfonts" and write an adaptive fontmap for ghostscript.

Maybe I missunderstood your argument "-nofontswap", but for me its works the other way around. Meaning that fonts are not swapped without it and swapped with the option active.

BR m3ch

altmany commented 3 years ago

Thanks, there's an automatic removal of "-Roman" from Times-Roman font names in print2eps line 488, but it's impossible to anticipate in advance all such replacements, and Frutiger is not as widely used as Times-Roman. Font replacements are one of the main reasons that I added the -regexprep option to export_fig. I agree that the -nofontswap option is confusing, I'll consider improving the documentation.

durack1 commented 3 years ago

@altmany it would be great if, in the case that a font is not identified and used, a warning is thrown for novice users to at least get some heads up about a failed attempt