Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Please add support for ttc font files (PDF/PS output) #3135

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

Open
mjanikas opened this issue Jun 13, 2014 · 21 comments
Open

Please add support for ttc font files (PDF/PS output) #3135

mjanikas opened this issue Jun 13, 2014 · 21 comments
Labels
keep Items to be ignored by the “Stale” Github Action topic: text/fonts

Comments

@mjanikas
Copy link

The code posted below will return the following: RuntimeError: TrueType font is missing table . It looks like TTC files have replaced TTF files for many fonts in Windows 8. Please consider adding support. Thanks so much,

MJ

import matplotlib.pyplot as PLT
import pylab as PYLAB
from matplotlib.backends.backend_pdf import PdfPages as PDF
import matplotlib.font_manager as fm

fontFile = r'C:\Windows\Fonts\msyh.ttc'
fp1 = fm.FontProperties(fname=fontFile)

reportFile = r'C:\Temp\TestUnicodeJA.pdf'
pdfOutput = PDF(reportFile)
vals = range(100)

PLT.plot(vals, vals, color = "r", linestyle = "-")
mess = u'\u30c7\u30fc\u30bf\u3092\u8aad\u307f\u8fbc\u3093\u3067\u3044\u307e\u3059....'
PYLAB.xlabel(mess, fontproperties = fp1)
PLT.savefig(pdfOutput, format='pdf')
PLT.close()
pdfOutput.close()
@tacaswell tacaswell added this to the v1.5.x milestone Jun 25, 2014
@tacaswell tacaswell added the text label Jun 25, 2014
@petehuang
Copy link
Contributor

Hi,

This issue has been inactive for a while. Does this continue to represent a useful enhancement? I believe the PR associated with this has stalled a little. Thanks!

@tacaswell tacaswell modified the milestones: 2.1 (next point release), 2.2 (next next feature release) Oct 3, 2017
@spinicist
Copy link

A lot of the fonts I want to use on Mac (principally Gill Sans) are also TTC, so I would love to see #9787 merged.

@lukelbd
Copy link
Contributor

lukelbd commented Jan 3, 2020

Friendly ping. I ran into this issue when testing various sans-serif fonts. Who would have the expertise to address this?

@jklymak
Copy link
Member

jklymak commented Jan 3, 2020

#9897 is in Matplotlib 3.1. Does it not go far enough? I take it there are some issues with pdf/ps.

@lukelbd
Copy link
Contributor

lukelbd commented Jan 3, 2020

I have matplotlib 3.1.1, issue is still present.

In [1]: import matplotlib
In [2]: matplotlib.__version__
Out[2]: '3.1.1'

This is an issue specifically with the PDF backend (not sure about PS). The title should probably be changed to reflect this.

@lukelbd
Copy link
Contributor

lukelbd commented Jan 3, 2020

Also, I am on macOS, not windows.

@jklymak
Copy link
Member

jklymak commented Jan 3, 2020

In #9897 @anntzer (who is probably best suited to looking at this) said there were technical limitations to getting ttc files to work with pdf/ps. So probably someone who strongly needed this would need to dig into things, some of which may be quite arcane.

@jklymak jklymak changed the title Please add support for ttc font files Please add support for ttc font files (PDF/PS output) Jan 3, 2020
@lukelbd
Copy link
Contributor

lukelbd commented Jan 3, 2020

In the meantime here is a working example for macOS:

import matplotlib.font_manager as fm
import matplotlib.pyplot as plt
props = fm.FontProperties(fname='/System/Library/Fonts/Helvetica.ttc')
f, ax = plt.subplots()
ax.set_title('Title', fontproperties=props)
f.savefig('tmp.pdf')

Note that fontfamily='Helvetica' still seems to work on macOS (Catalina) because there are also Helvetica .ttf files in /Library/Fonts, and the OSXFontDirectories variable in font_manager.py gives these files the priority (this was on my system only!).

@jklymak
Copy link
Member

jklymak commented Jan 3, 2020

I have Catalina, and I don't have anything in /Library/Fonts except in /Library/Fonts/Microsoft. ... which I only bring up to point out what a PITA font support is.

EDIT:... oops, as I guess you realize...

@lukelbd
Copy link
Contributor

lukelbd commented Jan 3, 2020

Haha yep, I discovered this by restoring the default system fonts.

@anntzer
Copy link
Contributor

anntzer commented Jan 3, 2020

Basically one would need to extend https://github.com/matplotlib/matplotlib/blob/master/src/_ttconv.cpp and https://github.com/matplotlib/matplotlib/tree/master/extern/ttconv to support ttc files. Probably not actually that hard (likely by first porting the thing to pure python and using the ft2font interface) -- my guess is that it's at most a couple of days' work -- but not particularly fun either. Edit: hum, perhaps not so easy; adapting #5414 may be a better place to start.
Meanwhile you could use https://github.com/matplotlib/mplcairo/, which does support ttc for pdf/ps :)

@anntzer
Copy link
Contributor

anntzer commented Jan 6, 2020

Perhaps a better idea: fonttools (https://github.com/fonttools/fonttools) is a pure-python lib that can -- in particular -- convert individual ttc subfaces to ttf (and do plenty of other things with fonts) -- it's a straightforward TTFont("/path/to/font.ttc", fontNumber=idx).save("/path/to/font-idx.ttf"). A bit ugly, but we can then pipe that into ttconv.

However, we also need to add more machinery on mpl's side, namely to handle all subfaces in ttcs (a ttc is a collection of multiple ttfs). In mplcairo (which fully supports ttcs) I hacked that by specifying the nth font in a ttc as "/path/to/font.ttc[n]", but the proper approach would probably to instead represent that as ("/path/to/font.ttc", n). FontManager also now needs to record the subface indices, so that a query to findfont would now return ("/path/to/font.ttc", n) instead of just "/path/to/font.ttc".

Now I think this is actually doable... just a lot of work. If someone is interested in doing this, I can try to write up a more detailed todo, but you'll need to get yourself familiar with matplotlib.ft2font and matplotlib.font_manager first.

@ain-soph
Copy link
Contributor

ain-soph commented Dec 23, 2020

So many years passing by, and I'm facing that again for using palatino-bold from the ttc collection in mac system.
I'm not very familiar with font_manager, so I hope someone could work on this.

Currently, my solution is to extract ttf from ttc manually and get them installed in the system. (or get them loaded and extend font_manager.fontManager.ttflist)

An example snippet (hope it could help others):

import os
import matplotlib.font_manager
from matplotlib import rc

dirname = os.path.dirname(__file__)

for style in ['normal', 'italic', 'bold', 'bold_italic']:
    file_path = os.path.normpath(os.path.join(dirname, f'palatino_{style}.ttf'))
    matplotlib.font_manager.fontManager.addfont(file_path)

rc('font', family='serif', serif='Palatino', weight='bold')

@K-gihu
Copy link

K-gihu commented May 7, 2022

Having a problem reading "Noto Serif CJK SC" font in /usr/share/fonts/noto-cjk/NotoSerifCJK-Regular.ttc as I find there are just "Noto Serif CJK JP"s in ~/.cache/matplotlib/fontlist-v330.json after clearing cache and rerun my matplotlib program. Then I find this issue and #9787 and it looks like we just support using the first font in ttc files so far. Maybe this should be put into the documentation?

matplotlib version 3.5.2
python version 3.9.11

@PaleNeutron
Copy link

PaleNeutron commented Jun 29, 2022

Missing fonts in ttc EXCEPT the first one.

Install font by : sudo apt-get install fonts-noto-cjk, then

>>> f = matplotlib.font_manager.get_font("/usr/share/fonts/opentype/noto/NotoSerifCJK-Regular.ttc")
>>> f.family_name
'Noto Serif CJK JP'

But in fact:

$ fc-list :lang=zh | grep NotoSerifCJK-Regular
/usr/share/fonts/opentype/noto/NotoSerifCJK-Regular.ttc: Noto Serif CJK SC:style=Regular
/usr/share/fonts/opentype/noto/NotoSerifCJK-Regular.ttc: Noto Serif CJK TC:style=Regular
/usr/share/fonts/opentype/noto/NotoSerifCJK-Regular.ttc: Noto Serif CJK JP:style=Regular
/usr/share/fonts/opentype/noto/NotoSerifCJK-Regular.ttc: Noto Serif CJK KR:style=Regular

@ain-soph
Copy link
Contributor

Missing fonts in ttc EXCEPT the first one.

Install font by : sudo apt-get install fonts-noto-cjk, then

>>> f = matplotlib.font_manager.get_font("/usr/share/fonts/opentype/noto/NotoSerifCJK-Regular.ttc")
>>> f.family_name
'Noto Serif CJK JP'

But in fact:

$ fc-list :lang=zh | grep NotoSerifCJK-Regular
/usr/share/fonts/opentype/noto/NotoSerifCJK-Regular.ttc: Noto Serif CJK SC:style=Regular
/usr/share/fonts/opentype/noto/NotoSerifCJK-Regular.ttc: Noto Serif CJK TC:style=Regular
/usr/share/fonts/opentype/noto/NotoSerifCJK-Regular.ttc: Noto Serif CJK JP:style=Regular
/usr/share/fonts/opentype/noto/NotoSerifCJK-Regular.ttc: Noto Serif CJK KR:style=Regular

That's their current implementation. So you better use ttf rather than ttc.

You may use this snippet to extract ttf from ttc collection file:
https://github.com/ain-soph/alpsplot/blob/main/fonts/split_ttc.py

@PaleNeutron
Copy link

PaleNeutron commented Jun 29, 2022

@ain-soph , thanks for solution! I know I can solve it with a lot of hack ways, but use only the first font in ttc file is clearly a bug.

@ain-soph
Copy link
Contributor

@ain-soph , thanks for solution! I know I can solve it with a lot of hack ways, but use only the first font in ttc file is clearly a bug.

It's a known issue for over 5 years (and perhaps another 5 years lol). There seems to be some technical challenge. I'm not familiar with those stuff though.
The implementation for using only the first font in ttc file is provided in #9787, which is the original intention of that PR.

@github-actions
Copy link

This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!

@github-actions github-actions bot added the status: inactive Marked by the “Stale” Github Action label Jun 30, 2023
@anntzer anntzer added keep Items to be ignored by the “Stale” Github Action and removed status: inactive Marked by the “Stale” Github Action labels Jun 30, 2023
@dhyanKaro
Copy link

Emojis!

@Artoria2e5
Copy link

Artoria2e5 commented Feb 20, 2024

Emojis in PDF might need to wait, since the color formats are also an issue beyond just TTC. I heard that Clerk Ma is doing something to XeTeX to allow them -- maybe we can use his work if the license and stuff works.

The current PR works, but converting by splitting is, at the end of the day, less than perfect -- especially if you have something like a 35-in-one Source Han Sans SuperOTC installed (I used to have that. I probably still do, but I'm too lazy to check.). The ideal is to find an alternative to _ttconv somewhere -- the TeX people might have the answer, I don't know.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
keep Items to be ignored by the “Stale” Github Action topic: text/fonts
Projects
None yet
Development

No branches or pull requests