<h1>Fonts in Matplotlib text engine<aclass="headerlink" href="#fonts-in-matplotlib-text-engine" title="Permalink to this headline">#</a></h1>
<p>Matplotlib needs fonts to work with its text engine, some of which are shipped
alongside the installation. However, users can configure the default fonts, or
even provide their own custom fonts! For more details, see <aclass="reference internal" href="../../tutorials/text/text_props.html"><spanclass="doc">Customizing
text properties</span></a>.</p>
<p>However, Matplotlib also provides an option to offload text rendering to a TeX
(in theory, even render fractals when rasterizing!)</p></td>
<td><p>Include a virtual machine that can execute code!</p></td>
</tr>
<trclass="row-even"><td><p>These fonts support font hinting</p></td>
<td><p>Do not support font hinting</p></td>
<td><p>Hinting supported (virtual machine processes the "hints")</p></td>
</tr>
<trclass="row-odd"><td><p>Non-subsetted through Matplotlib</p></td>
<td><p>Subsetted via external module <aclass="reference external" href="https://github.com/sandflow/ttconv">ttconv</a></p></td>
<td><p>Subsetted via external module <aclass="reference external" href="https://github.com/fonttools/fonttools">fonttools</a></p></td>
</tr>
</tbody>
</table>
<p>NOTE: Adobe will disable support for authoring with Type 1 fonts in
January 2023. <aclass="reference external" href="https://helpx.adobe.com/fonts/kb/postscript-type-1-fonts-end-of-support.html">Read more here.</a></p>
<sectionid="special-mentions">
<h3>Special mentions<aclass="headerlink" href="#special-mentions" title="Permalink to this headline">#</a></h3>
<p>Other font specifications which Matplotlib supports:</p>
<ulclass="simple">
<li><p>Type 42 fonts (PS):</p>
<ul>
<li><p>PostScript wrapper around TrueType fonts</p></li>
<li><p>42 is the <aclass="reference external" href="https://en.wikipedia.org/wiki/Answer_to_Life,_the_Universe,_and_Everything">Answer to Life, the Universe, and Everything!</a></p></li>
<li><p>Matplotlib uses an external library called <aclass="reference external" href="https://github.com/fonttools/fonttools">fonttools</a>
to subset these types of fonts</p></li>
</ul>
</li>
<li><p>OpenType fonts:</p>
<ul>
<li><p>OpenType is a new standard for digital type fonts, developed jointly by
Adobe and Microsoft</p></li>
<li><p>Generally contain a much larger character set!</p></li>
<li><p>Limited Support with Matplotlib</p></li>
</ul>
</li>
</ul>
</section>
</section>
<sectionid="subsetting">
<h2>Subsetting<aclass="headerlink" href="#subsetting" title="Permalink to this headline">#</a></h2>
<p>Matplotlib is able to generate documents in multiple different formats. Some of
those formats (for example, PDF, PS/EPS, SVG) allow embedding font data in such
a way that when these documents are visually scaled, the text does not appear
pixelated.</p>
<p>This can be achieved by embedding the <em>whole</em> font file within the
output document. However, this can lead to very large documents, as some
fonts (for instance, CJK - Chinese/Japanese/Korean fonts) can contain a large
number of glyphs, and thus their embedded size can be quite huge.</p>
<p>Font Subsetting can be used before generating documents, to embed only the
<em>required</em> glyphs within the documents. Fonts can be considered as a collection
of glyphs, so ultimately the goal is to find out <em>which</em> glyphs are required
for a certain array of characters, and embed only those within the output.</p>
<divclass="admonition note">
<pclass="admonition-title">Note</p>
<p>The role of subsetter really shines when we encounter characters like <strong>ä</strong>
(composed by calling subprograms for <strong>a</strong> and <strong>¨</strong>); since the subsetter
has to find out <em>all</em> such subprograms being called by every glyph included
in the subset, this is a generally difficult problem!</p>
</div>
<p>Luckily, Matplotlib uses a fork of an external dependency called
<aclass="reference external" href="https://github.com/sandflow/ttconv">ttconv</a>, which helps in embedding and
subsetting font data. (however, recent versions have moved away from ttconv to
pure Python for certain types: for more details visit
<divclass="line"><em>Type 1 fonts are still non-subsetted</em> through Matplotlib. (though one will encounter these mostly via <em>usetex</em>/<em>dviread</em> in PDF backend)</div>
<divclass="line"><strong>Type 3 and Type 42 fonts are subsetted</strong>, with a fair amount of exceptions and bugs for the latter.</div>
</div>
</section>
<sectionid="what-to-use">
<h2>What to use?<aclass="headerlink" href="#what-to-use" title="Permalink to this headline">#</a></h2>
<p>Practically, most fonts that are readily available on most operating systems or
are readily available on the internet to download include <em>TrueType fonts</em> and
its "extensions" such as MacOS-resource fork fonts and the newer OpenType
fonts.</p>
<p>PS and PDF backends provide support for yet another type of fonts, which remove
the need of subsetting altogether! These are called <strong>Core Fonts</strong>, and
Matplotlib calls them via the keyword <strong>AFM</strong>; all that is supplied from
Matplotlib to such documents are font metrics (specified in AFM format), and it
is the job of the viewer applications to supply the glyph definitions.</p>
<p>This is especially helpful to generate <em>really lightweight</em> documents.:</p>
<divclass="highlight-default notranslate"><divclass="highlight"><pre><span></span><spanclass="c1"># trigger core fonts for PDF backend</span>
<p>These core fonts are limited to PDF and PS backends only; they can not be
rendered in other backends.</p>
<p>Another downside to this is that while the font metrics are standardized,
different PDF viewer applications will have different fonts to render these
metrics. In other words, the <strong>output might look different on different
viewers</strong>, as well as (let's say) Windows and Linux, if Linux tools included
free versions of the proprietary fonts.</p>
<p>This also violates the <em>what-you-see-is-what-you-get</em> feature of Matplotlib.</p>
</div>
</section>
</section>
</div>
</main>
</div>
</div>
<!-- Scripts loaded after <body> so the DOM is not blocked -->
<divid="unreleased-message"> You are reading an old version of the documentation (v3.5.2). For the latest version see <ahref="https://matplotlib.org/stable/users/explain/fonts.html">https://matplotlib.org/stable/users/explain/fonts.html</a></div>