Skip to content

Commit 4f3b83e

Browse files
authored
Package fonts with niceplots (#43)
* Package fonts with niceplots * Add fonts on import * Don't need to install fonts in GHA * `black -l 120 .` * update actions/upload-artifact * Remove font installation from docs build * Fix `plot_opt_prob` * Make plot_opt_prob work for old and new matplotlib versions * Make doc requirements consistent with what we test against * Don't need custom scraper any more * Maybe we do * Update readme * typo
1 parent b23b4d8 commit 4f3b83e

File tree

148 files changed

+195
-99
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

148 files changed

+195
-99
lines changed

.github/workflows/niceplots.yml

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,11 @@ jobs:
3232
- name: Install apt dependencies
3333
uses: awalsh128/cache-apt-pkgs-action@latest
3434
with:
35-
packages: fonts-cmu nodejs npm
35+
packages: nodejs npm
3636
version: 1.0
3737
- name: Install node dependencies
3838
run: |
3939
sudo npm install -g odiff-bin
40-
- name: Install Prompt font
41-
run: |
42-
mkdir Prompt
43-
cd Prompt
44-
wget "https://fonts.google.com/download?family=Prompt" -O Prompt.zip
45-
unzip Prompt.zip
46-
mkdir ~/.fonts
47-
cp *.ttf ~/.fonts
4840
- name: Install python dependencies
4941
run: |
5042
pip install --upgrade pip wheel
@@ -61,7 +53,7 @@ jobs:
6153
cd examples
6254
bash ImageComparisonTest.sh
6355
- name: Upload examples if failed
64-
uses: actions/upload-artifact@v3
56+
uses: actions/upload-artifact@v4
6557
if: ${{ failure() && matrix.python-version == '3.11' && matrix.numpy-version == '1.26.*' && matrix.mpl-version == '3.8.*' }}
6658
with:
6759
name: Examples

.readthedocs.yaml

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,6 @@ build:
1010
os: ubuntu-20.04
1111
tools:
1212
python: "3.10"
13-
apt_packages:
14-
- fonts-cmu
15-
# Install the fonts for the james style
16-
jobs:
17-
pre_install:
18-
- mkdir Prompt
19-
- cd Prompt
20-
- wget "https://fonts.google.com/download?family=Prompt" -O Prompt.zip
21-
- unzip Prompt.zip
22-
- mkdir ~/.fonts
23-
- cp *.ttf ~/.fonts
24-
- cd ..
2513

2614
# Build documentation in the docs/ directory with Sphinx
2715
sphinx:

README.md

Lines changed: 4 additions & 31 deletions

doc/conf.py

Lines changed: 5 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,13 @@
22
from glob import glob
33
import shutil
44
import os
5+
import sys
56
from sphinx_gallery.scrapers import figure_rst
67
from pathlib import PurePosixPath
78

8-
9-
class svgScraper(object):
10-
"""This is a custom scraper for sphinx-gallery that allows us to use the svg files written by our examples. It is
11-
almost entirely copied from the PNGScraper shown at:
12-
https://sphinx-gallery.github.io/dev/advanced.html#example-2-detecting-image-files-on-disk
13-
"""
14-
15-
def __init__(self):
16-
self.seen = set()
17-
18-
def __repr__(self):
19-
return "svgScraper"
20-
21-
def __call__(self, block, block_vars, gallery_conf):
22-
# Find all svg files in the directory of this example.
23-
path_current_example = os.path.dirname(block_vars["src_file"])
24-
svgs = sorted(glob(os.path.join(path_current_example, "*.svg")))
25-
26-
# Get the name of the current example, e.g if the file is called "plot_nested_pie_chart.py",
27-
# then example_name = "nested_pie_chart"
28-
example_name = os.path.splitext(os.path.basename(block_vars["src_file"]))[0].split("plot_")[0]
29-
30-
# Iterate through svgs, copy them to the sphinx-gallery output directory
31-
image_names = list()
32-
image_path_iterator = block_vars["image_path_iterator"]
33-
for svg in svgs:
34-
if svg not in self.seen and example_name.lower() in svg.lower():
35-
self.seen |= set(svg)
36-
this_image_path = image_path_iterator.next()
37-
image_path = PurePosixPath(this_image_path)
38-
image_path = image_path.with_suffix(".svg")
39-
image_names.append(image_path)
40-
shutil.move(svg, image_path)
41-
# Use the `figure_rst` helper function to generate rST for image files
42-
return figure_rst(image_names, gallery_conf["src_dir"])
43-
9+
# This is necessary for sphinx_gallery to be able to import sphinxext.matplotlib_svg_scraper
10+
# (see https://sphinx-gallery.github.io/stable/configuration.html#importing-callables)
11+
sys.path.insert(0, os.path.dirname(__file__))
4412

4513
# -- Project information -----------------------------------------------------
4614
project = "niceplots"
@@ -50,5 +18,5 @@ def __call__(self, block, block_vars, gallery_conf):
5018
sphinx_gallery_conf = {
5119
"examples_dirs": "../examples", # path to your example scripts
5220
"gallery_dirs": "auto_examples", # path to where to save gallery generated output
53-
"image_scrapers": (svgScraper(),),
21+
"image_scrapers": ("sphinxext.matplotlib_svg_scraper",),
5422
}

doc/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
sphinx_mdolab_theme
22
sphinx-gallery
3-
matplotlib>=3.6
3+
matplotlib>=3.6,<=3.8

doc/sphinxext.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# For some reason, this is needed to make sphinx gallery use the svg files generated by our examples
2+
# See https://sphinx-gallery.github.io/stable/advanced.html#example-3-matplotlib-with-svg-format for more details
3+
4+
from sphinx_gallery.scrapers import matplotlib_scraper
5+
6+
7+
def matplotlib_svg_scraper(*args, **kwargs):
8+
return matplotlib_scraper(*args, format="svg", **kwargs)

examples/plot_bar_chart.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
=========
44
An example of a bar chart.
55
"""
6+
67
import matplotlib.pyplot as plt
78
import niceplots
89

examples/plot_nested_pie_chart.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
================
44
An example of a nested pie chart.
55
"""
6+
67
import matplotlib.pyplot as plt
78
import niceplots
89

niceplots/__init__.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
1+
import os
2+
from matplotlib import font_manager
3+
14
__version__ = "2.5.1"
25

6+
7+
def addFonts():
8+
font_dirs = [os.path.join(os.path.dirname(__file__), "fonts")]
9+
font_files = font_manager.findSystemFonts(fontpaths=font_dirs)
10+
11+
for font_file in font_files:
12+
font_manager.fontManager.addfont(font_file)
13+
14+
15+
addFonts()
16+
317
from .utils import *
418
from .parula import *

niceplots/fonts/OFL.txt

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
Copyright (c) 2015, Cadson Demak ([email protected])
2+
3+
This Font Software is licensed under the SIL Open Font License, Version 1.1.
4+
This license is copied below, and is also available with a FAQ at:
5+
https://openfontlicense.org
6+
7+
8+
-----------------------------------------------------------
9+
SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
10+
-----------------------------------------------------------
11+
12+
PREAMBLE
13+
The goals of the Open Font License (OFL) are to stimulate worldwide
14+
development of collaborative font projects, to support the font creation
15+
efforts of academic and linguistic communities, and to provide a free and
16+
open framework in which fonts may be shared and improved in partnership
17+
with others.
18+
19+
The OFL allows the licensed fonts to be used, studied, modified and
20+
redistributed freely as long as they are not sold by themselves. The
21+
fonts, including any derivative works, can be bundled, embedded,
22+
redistributed and/or sold with any software provided that any reserved
23+
names are not used by derivative works. The fonts and derivatives,
24+
however, cannot be released under any other type of license. The
25+
requirement for fonts to remain under this license does not apply
26+
to any document created using the fonts or their derivatives.
27+
28+
DEFINITIONS
29+
"Font Software" refers to the set of files released by the Copyright
30+
Holder(s) under this license and clearly marked as such. This may
31+
include source files, build scripts and documentation.
32+
33+
"Reserved Font Name" refers to any names specified as such after the
34+
copyright statement(s).
35+
36+
"Original Version" refers to the collection of Font Software components as
37+
distributed by the Copyright Holder(s).
38+
39+
"Modified Version" refers to any derivative made by adding to, deleting,
40+
or substituting -- in part or in whole -- any of the components of the
41+
Original Version, by changing formats or by porting the Font Software to a
42+
new environment.
43+
44+
"Author" refers to any designer, engineer, programmer, technical
45+
writer or other person who contributed to the Font Software.
46+
47+
PERMISSION & CONDITIONS
48+
Permission is hereby granted, free of charge, to any person obtaining
49+
a copy of the Font Software, to use, study, copy, merge, embed, modify,
50+
redistribute, and sell modified and unmodified copies of the Font
51+
Software, subject to the following conditions:
52+
53+
1) Neither the Font Software nor any of its individual components,
54+
in Original or Modified Versions, may be sold by itself.
55+
56+
2) Original or Modified Versions of the Font Software may be bundled,
57+
redistributed and/or sold with any software, provided that each copy
58+
contains the above copyright notice and this license. These can be
59+
included either as stand-alone text files, human-readable headers or
60+
in the appropriate machine-readable metadata fields within text or
61+
binary files as long as those fields can be easily viewed by the user.
62+
63+
3) No Modified Version of the Font Software may use the Reserved Font
64+
Name(s) unless explicit written permission is granted by the corresponding
65+
Copyright Holder. This restriction only applies to the primary font name as
66+
presented to the users.
67+
68+
4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
69+
Software shall not be used to promote, endorse or advertise any
70+
Modified Version, except to acknowledge the contribution(s) of the
71+
Copyright Holder(s) and the Author(s) or with their explicit written
72+
permission.
73+
74+
5) The Font Software, modified or unmodified, in part or in whole,
75+
must be distributed entirely under this license, and must not be
76+
distributed under any other license. The requirement for fonts to
77+
remain under this license does not apply to any document created
78+
using the Font Software.
79+
80+
TERMINATION
81+
This license becomes null and void if any of the above conditions are
82+
not met.
83+
84+
DISCLAIMER
85+
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
86+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
87+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
88+
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
89+
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
90+
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
91+
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
92+
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
93+
OTHER DEALINGS IN THE FONT SOFTWARE.

0 commit comments

Comments
 (0)