-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Version 2.0.0_1 breaks OpenType font character spacing on PDF save #8050
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
Comments
Might be related to #7937? |
FWIW, I tried to reproduce this bug (on CentOS 7) with ComicNeue_Light.otf (in the current directory) and the following script import matplotlib as mpl
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
plt.style.use('classic') # to be ~ consistent between mpl versions
fig, ax = plt.subplots(num="mwe")
ax.plot([-5, 0, 5], [0.9, -0.85, 0.7])
# Add some labels with OTF font
fprops = FontProperties(fname='./ComicNeue_Light.otf', weight='light', size=32)
ax.set_xlabel("X-axis testing",fontproperties=fprops)
ax.set_ylabel("Y-axis testing",fontproperties=fprops)
ax.set_title("mpl v" + mpl.__version__, fontproperties=fprops, fontsize=12)
# Adjust tick labels to the OTF font
for label in ax.get_xticklabels() + ax.get_yticklabels():
label.set_fontproperties(fprops)
plt.tight_layout()
fig.savefig(fig.get_label() + "_" + mpl.__version__ + ".pdf") What I get is:
Except the missing “-” glyph, nothing does really looks wrong with 1.5.1, and I get the same text rendering as with 2.0.0rc1. With a recent mpl version from master, the spacing between the glyphs is a bit smaller than with the former mpl versions |
Oh I thought it would be difficult to get the Canter font, but actually it was not (freely available here). So the results are similar to the ones with ComicNeue, i.e. I cannot reproduce the bug :/. What I get is:
|
Might need to delete the mpl font cache? |
I have tried to delete the font cache and that was not successful. I have made a minimal example that continues to work in version 1.5.3 and fail in version 2.0.0. I was not using FontProperties, though. import matplotlib as mpl
import matplotlib.pyplot as plt
f = {'family':'Canter', 'weight':'light', 'size':20}
mpl.rc('font', **f)
x = [-4,-3,-2,-1.0001,0,1,2,3,4]
y = [0.019,-0.097,-0.343,-0.521,-0.327,-0.362,-0.129,-0.168,-0.078]
fig = plt.figure(figsize=(7.5, 6))
ax = plt.subplot(111)
ax.plot(x, y, linewidth=0.5, alpha=0.2, color='#666666')
ax.set_xlabel('LONG XLABEL TESTING')
ax.set_ylabel('LONGER YLABEL TESTING')
plt.savefig('test.pdf', transparent=True) |
NB: I am using FontProperties because Canter fonts are not installed on my OS (and they do not seem to be available in CentOS 7 repositories). I tried @andermannlab 's example with FontProperties (else I fallback to DejaVu or Bitstream fonts…) and the results are the same as my previous trials. @andermannlab Could you test it using FontProperties please? Just to see if you still have the issue on your side. |
I have just tried the FontProperties version and it works (with multiple OpenType fonts, including Canter). Thank you. Is the long-term solution to transition legacy code to FontProperties? |
Honestly I know almost nothing about how fonts are handled in matplotlib, so currently I would simply consider this as a workaround to your problem. Maybe one of the core devs will know more. |
It is related to #7937, I've been solving this with a monkeypatch from the patch mentioned in that issue. I'm using otf fonts, and confirm that it works:
BUT, the whole darned font gets added to the pdf since subsetting is broken. I run the pdf through ghostscript afterwards, which properly subsets. I use an ipython shell command to do this for me:
|
Did subsetting ever work for otf fonts? |
That is a @mdboom question, but I think the answer is no. |
Ping @andermannlab and @grahamrow; please try with #7961 or I'm going to close this eventually. |
See https://github.com/matplotlib/freetypy and
#5414 for @mdboom's work
that could potentially lead to better subsetting and other font-related
improvements. It's definitely a nontrivial task.
|
I'm going to close this since at least one reporter has confirmed it's working. |
Bug report
Bug summary
I know that Matplotlib does not officially support OpenType fonts, but I have successfully been using them for axis labels in version 1.5.3 and earlier. I just updated Matplotlib via Macports and the character spacing and alignment broke, especially with larger font sizes. For example, X axis titles that are centered begin at the same position, but have extra wide spacing. This is found with all OpenType fonts that I have tried.
Code for reproduction
Actual outcome
170208-correlation-version 2.pdf
Expected outcome
170208-correlation-version 1.5.pdf
Matplotlib version
The text was updated successfully, but these errors were encountered: