Skip to content

Characters doesn't display correctly when figure saved as pdf with a custom font #12636

Closed
@gepcel

Description

@gepcel

Problem description:

When using a custom font, saving the figure as pdf format, and then opening the pdf with adobe reader (or many other pdf readers except SumatraPDF), some characters don't display correctly.

I use windows 10 operating system. The font is for the Chinese language, and already install into C:\windows\fonts

Check the font

from matplotlib.font_manager import fontManager
for f in fontManager.ttflist:
    if 'Source Han Serif' in f.name:
        print(f.name, ' : ', f.fname)

Output:

Source Han Serif CN  :  C:\Windows\Fonts\SourceHanSerifCN-Regular.otf
Source Han Serif CN  :  C:\Windows\Fonts\SourceHanSerifCN-Bold.otf

Generate the figure

Code (a minimal and fully example)

import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
plt.text(0.5, 0.5, '图像', ha='center', va='center', 
         fontdict={'family': 'Source Han Serif CN', 'size': 18})
plt.savefig('figure_with_font.pdf')
plt.savefig('figure_with_font.png')

There will be a warning message:

'SourceHanSerifCN-Regular.otf' cannot be subsetted into a Type 3 font. The entire font will be embedded in the output.

And the output in the notebook and the png format both displays correctly:
figure_with_font

But when open figure_with_font.pdf with "adobe reader", it displays like:
image

When I check the document properties, it seems like the font is already embedded in the pdf:
image

I've tried some other readers, only Sumatra PDF works.

**I'm using: **

  1. Windows 10
  2. matplotlib: 3.0.0

And The output pdf file:

figure_with_font.pdf

Question:

  • Can matplotlib save a figure to pdf without embedding the font? Sometimes when saving thousands of figures, it will help to reduce the file size.
  • What's wrong with the pdf not displaying correctly?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions