Skip to content

Commit

Permalink
Merge pull request #323 from lukelbd/mathtext-font
Browse files Browse the repository at this point in the history
Support using the current font for 'custom' mathtext styles
  • Loading branch information
lukelbd committed Jan 20, 2022
2 parents 0b0431b + 7004b8a commit dc84abe
Show file tree
Hide file tree
Showing 40 changed files with 448 additions and 39 deletions.
62 changes: 45 additions & 17 deletions docs/fonts.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,32 +61,60 @@
# :rcraw:`font.fantasy` = ``'TeX Gyre Adventor'``.
#
# After importing proplot, the default matplotlib font will be
# `TeX Gyre Heros <https://ctan.org/pkg/tex-gyre-heros>`__,
# which emulates the more conventional and (in our opinion) aesthetically pleasing
# font `Helvetica <https://en.wikipedia.org/wiki/Helvetica>`__. The new default font
# `TeX Gyre Heros <https://ctan.org/pkg/tex-gyre-heros>`__, which
# emulates the more conventional and (in our opinion) aesthetically pleasing
# font `Helvetica <https://en.wikipedia.org/wiki/Helvetica>`__. The default font
# family lists are shown in the :ref:`default proplotrc file <ug_proplotrc>`.
#
# To compare different fonts, use the `~proplot.demos.show_fonts` command with the
# `family` keyword (default behavior is ``family='sans-serif'``). Tables of the TeX
# Gyre and sans-serif fonts packaged with proplot are shown below. In order to show how
# the fonts perform as math text, the demo is rendered with :rcraw:`mathtext.default`
# = ``'regular'``, :rcraw:`mathtext.fontset` = ``'custom'``, and
# :rcraw:`mathtext.fallback` = ``None``. This uses the global :rcraw:`font.family`
# for the math text and shows a dummy glyph "陇" is shown where math characters
# are unavailable (see the matplotlib `math text guide
# <https://matplotlib.org/stable/tutorials/text/mathtext.html#custom-fonts>`__
# for details). In practice, proplot uses a standard non-italicized math
# text everywhere by setting :rc:`mathtext.fontset` = ``'stixsans'`` and
# :rcraw:`mathtext.default = 'rm'` (see the :ref:`proplotrc file <ug_proplotrc>`
# for details).
# Gyre and sans-serif fonts packaged with proplot are shown below.

# %%
import proplot as pplt
fig, axs = pplt.show_fonts(family='tex-gyre')
fig, axs = pplt.show_fonts(family='tex-gyre', show_math=False)

# %%
import proplot as pplt
fig, axs = pplt.show_fonts(family='sans-serif', show_math=False)

# %% [raw] raw_mimetype="text/restructuredtext"
# .. _ug_fonts_math:
#
# Math fonts
# ----------
#
# Proplot changes the default :rcraw:`mathtext.fontset` from ``'dejavusans'`` to
# ``'custom'``. This means that LaTeX-generated math text in a given string (i.e.,
# text surrounded by ``'$dollar signs$'``) uses the same font
# family as non-math text (see the matplotlib `math text guide
# <https://matplotlib.org/stable/tutorials/text/mathtext.html#custom-fonts>`__ for
# details). This improves the consistency of figure text when switching between
# different fonts. However if you are rendering very complex equations,
# you may want to change the default font to the matplotlib-packaged
# font ``'DejaVu Sans'`` or the proplot-packaged font ``'Fira Math'`` (see
# `this page <https://github.com/firamath/firamath>`__ for more on Fira Math).
# Tables of the TeX Gyre and sans-serif fonts packaged with proplot are
# shown below. A dummy glyph "陇" is shown where math characters are unavailable
# for a particular font (in practice, the fallback font :rc:`mathtext.fallback`
# is used whenever a math character is unavailable, but the demo function
# disables the fallback font to highlight which characters are unavailable).
#
# .. note::
#
# Proplot modifies the matplotlib math text internals so that the ``'custom'``
# font set can be applied with modifications to the currently active non-math
# font rather than only a global font family. This works by changing the default
# values of :rcraw:`mathtext.bf`, :rcraw:`mathtext.it`, :rcraw:`mathtext.rm`,
# :rcraw:`mathtext.sf` from the global default font family ``'sans'`` to the local
# font family ``'regular'``, where ``'regular'`` is a dummy name permitted by
# proplot (see the :ref:`proplotrc file <ug_proplotrc>` for details). This means
# that if :rcraw:`mathtext.fontset` is ``'custom'`` and the font family is changed
# for an arbitrary `~matplotlib.text.Text` instance, then any LaTeX-generated math
# in the text string will also use this font family.

# %%
import proplot as pplt
fig, axs = pplt.show_fonts(family='sans-serif')
fig, axs = pplt.show_fonts(family='sans-serif', show_math=True)

# %% [raw] raw_mimetype="text/restructuredtext"
# .. _ug_fonts_user:
Expand Down
41 changes: 23 additions & 18 deletions proplot/demos.py
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,7 @@ def show_colors(*, nhues=17, minsat=10, unknown='User', include=None, ignore=Non


def show_fonts(
*args, family=None, text=None,
*args, family=None, text=None, show_math=True,
size=12, weight='normal', style='normal', stretch='normal',
):
"""
Expand Down Expand Up @@ -841,8 +841,8 @@ def show_fonts(
show_cycles
show_colors
"""
# Select fonts for plotting. Default is to show sans-serif fonts. Otherwise
# fonts can be specified as input arguments or with the family keyword
# Select fonts for plotting. Default is to show sans-serif and user fonts.
# Otherwise fonts can be specified as input arguments or with the family keyword
if not args and family is None:
args = sorted(
{
Expand Down Expand Up @@ -881,23 +881,28 @@ def show_fonts(

# The default sample text
if text is None:
text = (
'the quick brown fox jumps over a lazy dog' '\n'
'THE QUICK BROWN FOX JUMPS OVER A LAZY DOG' '\n'
'(0) + {1\N{DEGREE SIGN}} \N{MINUS SIGN} [2*] - <3> / 4,0 '
r'$\geq\gg$ 5.0 $\leq\ll$ ~6 $\times$ 7 '
r'$\equiv$ 8 $\approx$ 9 $\propto$' '\n'
r'$\alpha\beta$ $\Gamma\gamma$ $\Delta\delta$ '
r'$\epsilon\zeta\eta$ $\Theta\theta$ $\kappa\mu\nu$ '
r'$\Lambda\lambda$ $\Pi\pi$ $\xi\rho\tau\chi$ $\Sigma\sigma$ '
r'$\Phi\phi$ $\Psi\psi$ $\Omega\omega$ !?&#%'
)
if not show_math:
text = (
'the quick brown fox jumps over a lazy dog . , + -'
'\n'
'THE QUICK BROWN FOX JUMPS OVER A LAZY DOG ! ? & # %'
)
else:
text = (
r'$\alpha\beta$ $\Gamma\gamma$ $\Delta\delta$ '
r'$\epsilon\zeta\eta$ $\Theta\theta$ $\kappa\mu\nu$ '
r'$\Lambda\lambda$ $\Pi\pi$ $\xi\rho\tau\chi$ $\Sigma\sigma$ '
r'$\Phi\phi$ $\Psi\psi$ $\Omega\omega$ '
r'$\ll { }^k \gg [ ]_l \propto ( )^m \cdot \left<\right>_n$'
'\n'
r'$0^a + 1_b - 2^c \times 3_d = '
r'4.0^e \equiv 5.0_f \approx 6.0^g \sim 7_h \leq 8^i \geq 9_j'
r'\sum X \ll \int Y \gg \oint Z \propto \prod Q$'
'\n'
)

# Settings for rendering math text
ctx = {
'mathtext.default': 'regular',
'mathtext.fontset': 'custom',
}
ctx = {'mathtext.fontset': 'custom'}
if _version_mpl < 3.4:
ctx['mathtext.fallback_to_cm'] = False
else:
Expand Down
Binary file added proplot/fonts/FiraSans-Black.ttf
Binary file not shown.
Binary file added proplot/fonts/FiraSans-BlackItalic.ttf
Binary file not shown.
Binary file added proplot/fonts/FiraSans-Bold.ttf
Binary file not shown.
Binary file added proplot/fonts/FiraSans-BoldItalic.ttf
Binary file not shown.
Binary file added proplot/fonts/FiraSans-ExtraBold.ttf
Binary file not shown.
Binary file added proplot/fonts/FiraSans-ExtraBoldItalic.ttf
Binary file not shown.
Binary file added proplot/fonts/FiraSans-ExtraLight.ttf
Binary file not shown.
Binary file added proplot/fonts/FiraSans-ExtraLightItalic.ttf
Binary file not shown.
Binary file added proplot/fonts/FiraSans-Italic.ttf
Binary file not shown.
Binary file added proplot/fonts/FiraSans-Light.ttf
Binary file not shown.
Binary file added proplot/fonts/FiraSans-LightItalic.ttf
Binary file not shown.
Binary file added proplot/fonts/FiraSans-Medium.ttf
Binary file not shown.
Binary file added proplot/fonts/FiraSans-MediumItalic.ttf
Binary file not shown.
Binary file added proplot/fonts/FiraSans-Regular.ttf
Binary file not shown.
Binary file added proplot/fonts/FiraSans-SemiBold.ttf
Binary file not shown.
Binary file added proplot/fonts/FiraSans-SemiBoldItalic.ttf
Binary file not shown.
97 changes: 97 additions & 0 deletions proplot/fonts/LICENSE_FIRASANS.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
Copyright (c) <dates>, <Copyright Holder> (<URL|email>),
with Reserved Font Name <Reserved Font Name>.
Copyright (c) <dates>, <additional Copyright Holder> (<URL|email>),
with Reserved Font Name <additional Reserved Font Name>.
Copyright (c) <dates>, <additional Copyright Holder> (<URL|email>).

This Font Software is licensed under the SIL Open Font License, Version 1.1.
This license is copied below, and is also available with a FAQ at:
http://scripts.sil.org/OFL


-----------------------------------------------------------
SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
-----------------------------------------------------------

PREAMBLE
The goals of the Open Font License (OFL) are to stimulate worldwide
development of collaborative font projects, to support the font creation
efforts of academic and linguistic communities, and to provide a free and
open framework in which fonts may be shared and improved in partnership
with others.

The OFL allows the licensed fonts to be used, studied, modified and
redistributed freely as long as they are not sold by themselves. The
fonts, including any derivative works, can be bundled, embedded,
redistributed and/or sold with any software provided that any reserved
names are not used by derivative works. The fonts and derivatives,
however, cannot be released under any other type of license. The
requirement for fonts to remain under this license does not apply
to any document created using the fonts or their derivatives.

DEFINITIONS
"Font Software" refers to the set of files released by the Copyright
Holder(s) under this license and clearly marked as such. This may
include source files, build scripts and documentation.

"Reserved Font Name" refers to any names specified as such after the
copyright statement(s).

"Original Version" refers to the collection of Font Software components as
distributed by the Copyright Holder(s).

"Modified Version" refers to any derivative made by adding to, deleting,
or substituting -- in part or in whole -- any of the components of the
Original Version, by changing formats or by porting the Font Software to a
new environment.

"Author" refers to any designer, engineer, programmer, technical
writer or other person who contributed to the Font Software.

PERMISSION & CONDITIONS
Permission is hereby granted, free of charge, to any person obtaining
a copy of the Font Software, to use, study, copy, merge, embed, modify,
redistribute, and sell modified and unmodified copies of the Font
Software, subject to the following conditions:

1) Neither the Font Software nor any of its individual components,
in Original or Modified Versions, may be sold by itself.

2) Original or Modified Versions of the Font Software may be bundled,
redistributed and/or sold with any software, provided that each copy
contains the above copyright notice and this license. These can be
included either as stand-alone text files, human-readable headers or
in the appropriate machine-readable metadata fields within text or
binary files as long as those fields can be easily viewed by the user.

3) No Modified Version of the Font Software may use the Reserved Font
Name(s) unless explicit written permission is granted by the corresponding
Copyright Holder. This restriction only applies to the primary font name as
presented to the users.

4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
Software shall not be used to promote, endorse or advertise any
Modified Version, except to acknowledge the contribution(s) of the
Copyright Holder(s) and the Author(s) or with their explicit written
permission.

5) The Font Software, modified or unmodified, in part or in whole,
must be distributed entirely under this license, and must not be
distributed under any other license. The requirement for fonts to
remain under this license does not apply to any document created
using the Font Software.

TERMINATION
This license becomes null and void if any of the above conditions are
not met.

DISCLAIMER
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
OTHER DEALINGS IN THE FONT SOFTWARE.
93 changes: 93 additions & 0 deletions proplot/fonts/LICENSE_NOTOSERIF.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
Copyright 2012 Google Inc. All Rights Reserved.

This Font Software is licensed under the SIL Open Font License, Version 1.1.
This license is copied below, and is also available with a FAQ at:
http://scripts.sil.org/OFL


-----------------------------------------------------------
SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
-----------------------------------------------------------

PREAMBLE
The goals of the Open Font License (OFL) are to stimulate worldwide
development of collaborative font projects, to support the font creation
efforts of academic and linguistic communities, and to provide a free and
open framework in which fonts may be shared and improved in partnership
with others.

The OFL allows the licensed fonts to be used, studied, modified and
redistributed freely as long as they are not sold by themselves. The
fonts, including any derivative works, can be bundled, embedded,
redistributed and/or sold with any software provided that any reserved
names are not used by derivative works. The fonts and derivatives,
however, cannot be released under any other type of license. The
requirement for fonts to remain under this license does not apply
to any document created using the fonts or their derivatives.

DEFINITIONS
"Font Software" refers to the set of files released by the Copyright
Holder(s) under this license and clearly marked as such. This may
include source files, build scripts and documentation.

"Reserved Font Name" refers to any names specified as such after the
copyright statement(s).

"Original Version" refers to the collection of Font Software components as
distributed by the Copyright Holder(s).

"Modified Version" refers to any derivative made by adding to, deleting,
or substituting -- in part or in whole -- any of the components of the
Original Version, by changing formats or by porting the Font Software to a
new environment.

"Author" refers to any designer, engineer, programmer, technical
writer or other person who contributed to the Font Software.

PERMISSION & CONDITIONS
Permission is hereby granted, free of charge, to any person obtaining
a copy of the Font Software, to use, study, copy, merge, embed, modify,
redistribute, and sell modified and unmodified copies of the Font
Software, subject to the following conditions:

1) Neither the Font Software nor any of its individual components,
in Original or Modified Versions, may be sold by itself.

2) Original or Modified Versions of the Font Software may be bundled,
redistributed and/or sold with any software, provided that each copy
contains the above copyright notice and this license. These can be
included either as stand-alone text files, human-readable headers or
in the appropriate machine-readable metadata fields within text or
binary files as long as those fields can be easily viewed by the user.

3) No Modified Version of the Font Software may use the Reserved Font
Name(s) unless explicit written permission is granted by the corresponding
Copyright Holder. This restriction only applies to the primary font name as
presented to the users.

4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
Software shall not be used to promote, endorse or advertise any
Modified Version, except to acknowledge the contribution(s) of the
Copyright Holder(s) and the Author(s) or with their explicit written
permission.

5) The Font Software, modified or unmodified, in part or in whole,
must be distributed entirely under this license, and must not be
distributed under any other license. The requirement for fonts to
remain under this license does not apply to any document created
using the Font Software.

TERMINATION
This license becomes null and void if any of the above conditions are
not met.

DISCLAIMER
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
OTHER DEALINGS IN THE FONT SOFTWARE.

0 comments on commit dc84abe

Please sign in to comment.