pyfonts 0.0.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pyfonts-0.0.1/PKG-INFO +186 -0
- pyfonts-0.0.1/README.md +169 -0
- pyfonts-0.0.1/pyfonts/__init__.py +3 -0
- pyfonts-0.0.1/pyfonts/main.py +44 -0
- pyfonts-0.0.1/pyfonts/utils.py +72 -0
- pyfonts-0.0.1/pyfonts.egg-info/PKG-INFO +186 -0
- pyfonts-0.0.1/pyfonts.egg-info/SOURCES.txt +12 -0
- pyfonts-0.0.1/pyfonts.egg-info/dependency_links.txt +1 -0
- pyfonts-0.0.1/pyfonts.egg-info/requires.txt +1 -0
- pyfonts-0.0.1/pyfonts.egg-info/top_level.txt +2 -0
- pyfonts-0.0.1/pyproject.toml +26 -0
- pyfonts-0.0.1/setup.cfg +4 -0
- pyfonts-0.0.1/setup.py +16 -0
- pyfonts-0.0.1/tests/__init__.py +0 -0
pyfonts-0.0.1/PKG-INFO
ADDED
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: pyfonts
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: A simple way to load fonts for matplotlib
|
|
5
|
+
Home-page: https://github.com/JosephBARBIERDARNAL/pyfonts/blob/main/README.md
|
|
6
|
+
Author: Joseph Barbier
|
|
7
|
+
Author-email: Joseph Barbier <joseph.barbierdarnal@gmail.com>
|
|
8
|
+
Project-URL: Homepage, https://github.com/JosephBARBIERDARNAL/pyfonts
|
|
9
|
+
Project-URL: Issues, https://github.com/JosephBARBIERDARNAL/pyfonts/issues
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Classifier: Framework :: Matplotlib
|
|
14
|
+
Requires-Python: >=3.8
|
|
15
|
+
Description-Content-Type: text/markdown
|
|
16
|
+
Requires-Dist: matplotlib
|
|
17
|
+
|
|
18
|
+
# PyFonts
|
|
19
|
+
|
|
20
|
+
<img src="https://github.com/JosephBARBIERDARNAL/pyfonts/blob/main/logo.png?raw=true" alt="pyfonts logo" align="right" width="200px"/>
|
|
21
|
+
|
|
22
|
+
A simple way to load fonts for matplotlib.
|
|
23
|
+
|
|
24
|
+
<br>
|
|
25
|
+
|
|
26
|
+
Check out the [online documentation](https://pyfonts.streamlit.app/).
|
|
27
|
+
|
|
28
|
+
Table of content:
|
|
29
|
+
|
|
30
|
+
- [Quick Start](#quick-start)
|
|
31
|
+
- [Find fonts](#how-to-find-fonts)
|
|
32
|
+
- [Fonts in Matplotlib](#pyfonts-and-matplotlib)
|
|
33
|
+
- [Other feature: download fonts](#other-feature-download-a-font-locally)
|
|
34
|
+
|
|
35
|
+
<br><br>
|
|
36
|
+
|
|
37
|
+
# Installation
|
|
38
|
+
|
|
39
|
+
You can install `pyfonts` directly from PyPI with:
|
|
40
|
+
|
|
41
|
+
```
|
|
42
|
+
pip install pyfonts
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
Alternatively you can install the **development version** with:
|
|
46
|
+
|
|
47
|
+
```
|
|
48
|
+
pip install git+https://github.com/JosephBARBIERDARNAL/pyfonts.git
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
<br><br>
|
|
52
|
+
|
|
53
|
+
# Quick start
|
|
54
|
+
|
|
55
|
+
```python
|
|
56
|
+
from pyfonts import load_font
|
|
57
|
+
import matplotlib.pyplot as plt
|
|
58
|
+
|
|
59
|
+
# load font
|
|
60
|
+
font = load_font(
|
|
61
|
+
font_url="https://github.com/google/fonts/raw/main/apache/ultra/Ultra-Regular.ttf"
|
|
62
|
+
)
|
|
63
|
+
|
|
64
|
+
# check how the font looks on a minimalist example
|
|
65
|
+
fig, ax = plt.subplots(figsize=(10, 6))
|
|
66
|
+
ax.text(
|
|
67
|
+
x=0.5,
|
|
68
|
+
y=0.5,
|
|
69
|
+
s=f"What an easy way to load fonts, isn't it?",
|
|
70
|
+
font=font,
|
|
71
|
+
fontsize=20,
|
|
72
|
+
ha="center",
|
|
73
|
+
)
|
|
74
|
+
plt.show()
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+

|
|
78
|
+
|
|
79
|
+
<br><br>
|
|
80
|
+
|
|
81
|
+
# How to find fonts?
|
|
82
|
+
|
|
83
|
+
### Google font
|
|
84
|
+
|
|
85
|
+
There are many fonts available on the web. The **easiest way** to find one is to follow these steps:
|
|
86
|
+
|
|
87
|
+
- Browse [Google Font website](https://fonts.google.com/) to find a font that you like. Let's say you want to use [Amaranth](https://fonts.google.com/specimen/Amaranth?query=amaranth).
|
|
88
|
+
- Go to [Google font github repository](https://github.com/google/fonts) and type the name of your desired font in the search bar. We find that Urbanist font file in **Bold** is named `https://github.com/google/fonts/blob/main/ofl/amaranth/Amaranth-Bold.ttf`.
|
|
89
|
+
- copy the url and add `?raw=true` at the end, which gives us `https://github.com/google/fonts/blob/main/ofl/amaranth/Amaranth-Bold.ttf?raw=true`
|
|
90
|
+
- and that's it! Just pass this to `load_font()` to use it in your matplotlib charts
|
|
91
|
+
|
|
92
|
+
```python
|
|
93
|
+
from pyfonts import load_font
|
|
94
|
+
import matplotlib.pyplot as plt
|
|
95
|
+
|
|
96
|
+
font = load_font(
|
|
97
|
+
font_url="https://github.com/google/fonts/blob/main/ofl/amaranth/Amaranth-Bold.ttf?raw=true"
|
|
98
|
+
)
|
|
99
|
+
|
|
100
|
+
fig, ax = plt.subplots(figsize=(10, 6))
|
|
101
|
+
ax.text(
|
|
102
|
+
x=0.5,
|
|
103
|
+
y=0.5,
|
|
104
|
+
s=f"Congrats, you now have a cool font!",
|
|
105
|
+
font=font,
|
|
106
|
+
fontsize=20,
|
|
107
|
+
ha="center",
|
|
108
|
+
)
|
|
109
|
+
plt.show()
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
For the url to be readable by `PyFonts` when using a Github url, it must be in one of (the following 3 urls are completely equivalent):
|
|
113
|
+
|
|
114
|
+
- `https://github.com/google/fonts/blob/main/apache/ultra/Ultra-Regular.ttf?raw=true`
|
|
115
|
+
- `https://github.com/google/fonts/raw/main/apache/ultra/Ultra-Regular.ttf`
|
|
116
|
+
- `https://raw.githubusercontent.com/google/fonts/raw/main/apache/ultra/Ultra-Regular.ttf`
|
|
117
|
+
|
|
118
|
+
The **recommended** is the first (`https://github.com/google/fonts/blob/main/apache/ultra/Ultra-Regular.ttf?raw=true`) because you just need to add `?raw=true` after the end of the Github url.
|
|
119
|
+
|
|
120
|
+
### Other places for fonts
|
|
121
|
+
|
|
122
|
+
Github is the ideal place to find fonts under a free licence. You can find many fonts on the web. Just make sure that the licence of the font allows you to use it in your project.
|
|
123
|
+
|
|
124
|
+
You can find other fonts at:
|
|
125
|
+
|
|
126
|
+
- [Awesome fonts](https://github.com/brabadu/awesome-fonts)
|
|
127
|
+
- [Nerd fonts](https://github.com/ryanoasis/nerd-fonts)
|
|
128
|
+
|
|
129
|
+
<br><br>
|
|
130
|
+
|
|
131
|
+
# PyFonts and Matplotlib
|
|
132
|
+
|
|
133
|
+
### How it works
|
|
134
|
+
|
|
135
|
+
In order to work with any font, `PyFonts` creates a temporary file and uses this file to create a [FontProperties](https://matplotlib.org/stable/api/font_manager_api.html#matplotlib.font_manager.FontProperties) object. Once the object has been created with your font, the program deletes the temporary file as it no longer needs it.
|
|
136
|
+
|
|
137
|
+
### Different weight and style
|
|
138
|
+
|
|
139
|
+
When you load a font, **you don't load all its extensions**: bold, italic, thin etc, but only the one from the url. If you want to be able to use a font and its **bold** version, for example, you need to load both fonts:
|
|
140
|
+
|
|
141
|
+
```python
|
|
142
|
+
from pyfonts import load_font
|
|
143
|
+
import matplotlib.pyplot as plt
|
|
144
|
+
|
|
145
|
+
font = load_font(
|
|
146
|
+
font_url="https://github.com/google/fonts/blob/main/ofl/amaranth/Amaranth-Regular.ttf?raw=true"
|
|
147
|
+
)
|
|
148
|
+
bold_font = load_font(
|
|
149
|
+
font_url="https://github.com/google/fonts/blob/main/ofl/amaranth/Amaranth-Bold.ttf?raw=true"
|
|
150
|
+
)
|
|
151
|
+
|
|
152
|
+
fig, ax = plt.subplots(figsize=(10, 6))
|
|
153
|
+
ax.text(
|
|
154
|
+
x=0.5,
|
|
155
|
+
y=0.5,
|
|
156
|
+
s=f"Congrats, you now have a cool font!",
|
|
157
|
+
font=font,
|
|
158
|
+
fontsize=20,
|
|
159
|
+
ha="center",
|
|
160
|
+
)
|
|
161
|
+
ax.text(x=0.5, y=0.3, s=f"And now it's bold", font=bold_font, fontsize=20, ha="center")
|
|
162
|
+
plt.show()
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+

|
|
166
|
+
|
|
167
|
+
<br><br>
|
|
168
|
+
|
|
169
|
+
# Other feature: download a font locally
|
|
170
|
+
|
|
171
|
+
If for some reason you want to store the fonts you're working with, simply use the `download_font()` function. It just needs the arguments `font_url` (as described above) and `destination_path` (where you want to store them, by default in the current directory).
|
|
172
|
+
|
|
173
|
+
You can suppress the output message by adding `verbose=False` to it.
|
|
174
|
+
|
|
175
|
+
```python
|
|
176
|
+
from pyfonts import download_font
|
|
177
|
+
|
|
178
|
+
download_font(
|
|
179
|
+
font_url="https://github.com/google/fonts/raw/main/apache/ultra/Ultra-Regular.ttf",
|
|
180
|
+
destination_path="/Users/josephbarbier/Desktop/myfont.ttf", # optional
|
|
181
|
+
)
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
`Font installed at: /Users/josephbarbier/Desktop/myfont.ttf`
|
|
185
|
+
|
|
186
|
+
<br><br>
|
pyfonts-0.0.1/README.md
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
# PyFonts
|
|
2
|
+
|
|
3
|
+
<img src="https://github.com/JosephBARBIERDARNAL/pyfonts/blob/main/logo.png?raw=true" alt="pyfonts logo" align="right" width="200px"/>
|
|
4
|
+
|
|
5
|
+
A simple way to load fonts for matplotlib.
|
|
6
|
+
|
|
7
|
+
<br>
|
|
8
|
+
|
|
9
|
+
Check out the [online documentation](https://pyfonts.streamlit.app/).
|
|
10
|
+
|
|
11
|
+
Table of content:
|
|
12
|
+
|
|
13
|
+
- [Quick Start](#quick-start)
|
|
14
|
+
- [Find fonts](#how-to-find-fonts)
|
|
15
|
+
- [Fonts in Matplotlib](#pyfonts-and-matplotlib)
|
|
16
|
+
- [Other feature: download fonts](#other-feature-download-a-font-locally)
|
|
17
|
+
|
|
18
|
+
<br><br>
|
|
19
|
+
|
|
20
|
+
# Installation
|
|
21
|
+
|
|
22
|
+
You can install `pyfonts` directly from PyPI with:
|
|
23
|
+
|
|
24
|
+
```
|
|
25
|
+
pip install pyfonts
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
Alternatively you can install the **development version** with:
|
|
29
|
+
|
|
30
|
+
```
|
|
31
|
+
pip install git+https://github.com/JosephBARBIERDARNAL/pyfonts.git
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
<br><br>
|
|
35
|
+
|
|
36
|
+
# Quick start
|
|
37
|
+
|
|
38
|
+
```python
|
|
39
|
+
from pyfonts import load_font
|
|
40
|
+
import matplotlib.pyplot as plt
|
|
41
|
+
|
|
42
|
+
# load font
|
|
43
|
+
font = load_font(
|
|
44
|
+
font_url="https://github.com/google/fonts/raw/main/apache/ultra/Ultra-Regular.ttf"
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
# check how the font looks on a minimalist example
|
|
48
|
+
fig, ax = plt.subplots(figsize=(10, 6))
|
|
49
|
+
ax.text(
|
|
50
|
+
x=0.5,
|
|
51
|
+
y=0.5,
|
|
52
|
+
s=f"What an easy way to load fonts, isn't it?",
|
|
53
|
+
font=font,
|
|
54
|
+
fontsize=20,
|
|
55
|
+
ha="center",
|
|
56
|
+
)
|
|
57
|
+
plt.show()
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+

|
|
61
|
+
|
|
62
|
+
<br><br>
|
|
63
|
+
|
|
64
|
+
# How to find fonts?
|
|
65
|
+
|
|
66
|
+
### Google font
|
|
67
|
+
|
|
68
|
+
There are many fonts available on the web. The **easiest way** to find one is to follow these steps:
|
|
69
|
+
|
|
70
|
+
- Browse [Google Font website](https://fonts.google.com/) to find a font that you like. Let's say you want to use [Amaranth](https://fonts.google.com/specimen/Amaranth?query=amaranth).
|
|
71
|
+
- Go to [Google font github repository](https://github.com/google/fonts) and type the name of your desired font in the search bar. We find that Urbanist font file in **Bold** is named `https://github.com/google/fonts/blob/main/ofl/amaranth/Amaranth-Bold.ttf`.
|
|
72
|
+
- copy the url and add `?raw=true` at the end, which gives us `https://github.com/google/fonts/blob/main/ofl/amaranth/Amaranth-Bold.ttf?raw=true`
|
|
73
|
+
- and that's it! Just pass this to `load_font()` to use it in your matplotlib charts
|
|
74
|
+
|
|
75
|
+
```python
|
|
76
|
+
from pyfonts import load_font
|
|
77
|
+
import matplotlib.pyplot as plt
|
|
78
|
+
|
|
79
|
+
font = load_font(
|
|
80
|
+
font_url="https://github.com/google/fonts/blob/main/ofl/amaranth/Amaranth-Bold.ttf?raw=true"
|
|
81
|
+
)
|
|
82
|
+
|
|
83
|
+
fig, ax = plt.subplots(figsize=(10, 6))
|
|
84
|
+
ax.text(
|
|
85
|
+
x=0.5,
|
|
86
|
+
y=0.5,
|
|
87
|
+
s=f"Congrats, you now have a cool font!",
|
|
88
|
+
font=font,
|
|
89
|
+
fontsize=20,
|
|
90
|
+
ha="center",
|
|
91
|
+
)
|
|
92
|
+
plt.show()
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
For the url to be readable by `PyFonts` when using a Github url, it must be in one of (the following 3 urls are completely equivalent):
|
|
96
|
+
|
|
97
|
+
- `https://github.com/google/fonts/blob/main/apache/ultra/Ultra-Regular.ttf?raw=true`
|
|
98
|
+
- `https://github.com/google/fonts/raw/main/apache/ultra/Ultra-Regular.ttf`
|
|
99
|
+
- `https://raw.githubusercontent.com/google/fonts/raw/main/apache/ultra/Ultra-Regular.ttf`
|
|
100
|
+
|
|
101
|
+
The **recommended** is the first (`https://github.com/google/fonts/blob/main/apache/ultra/Ultra-Regular.ttf?raw=true`) because you just need to add `?raw=true` after the end of the Github url.
|
|
102
|
+
|
|
103
|
+
### Other places for fonts
|
|
104
|
+
|
|
105
|
+
Github is the ideal place to find fonts under a free licence. You can find many fonts on the web. Just make sure that the licence of the font allows you to use it in your project.
|
|
106
|
+
|
|
107
|
+
You can find other fonts at:
|
|
108
|
+
|
|
109
|
+
- [Awesome fonts](https://github.com/brabadu/awesome-fonts)
|
|
110
|
+
- [Nerd fonts](https://github.com/ryanoasis/nerd-fonts)
|
|
111
|
+
|
|
112
|
+
<br><br>
|
|
113
|
+
|
|
114
|
+
# PyFonts and Matplotlib
|
|
115
|
+
|
|
116
|
+
### How it works
|
|
117
|
+
|
|
118
|
+
In order to work with any font, `PyFonts` creates a temporary file and uses this file to create a [FontProperties](https://matplotlib.org/stable/api/font_manager_api.html#matplotlib.font_manager.FontProperties) object. Once the object has been created with your font, the program deletes the temporary file as it no longer needs it.
|
|
119
|
+
|
|
120
|
+
### Different weight and style
|
|
121
|
+
|
|
122
|
+
When you load a font, **you don't load all its extensions**: bold, italic, thin etc, but only the one from the url. If you want to be able to use a font and its **bold** version, for example, you need to load both fonts:
|
|
123
|
+
|
|
124
|
+
```python
|
|
125
|
+
from pyfonts import load_font
|
|
126
|
+
import matplotlib.pyplot as plt
|
|
127
|
+
|
|
128
|
+
font = load_font(
|
|
129
|
+
font_url="https://github.com/google/fonts/blob/main/ofl/amaranth/Amaranth-Regular.ttf?raw=true"
|
|
130
|
+
)
|
|
131
|
+
bold_font = load_font(
|
|
132
|
+
font_url="https://github.com/google/fonts/blob/main/ofl/amaranth/Amaranth-Bold.ttf?raw=true"
|
|
133
|
+
)
|
|
134
|
+
|
|
135
|
+
fig, ax = plt.subplots(figsize=(10, 6))
|
|
136
|
+
ax.text(
|
|
137
|
+
x=0.5,
|
|
138
|
+
y=0.5,
|
|
139
|
+
s=f"Congrats, you now have a cool font!",
|
|
140
|
+
font=font,
|
|
141
|
+
fontsize=20,
|
|
142
|
+
ha="center",
|
|
143
|
+
)
|
|
144
|
+
ax.text(x=0.5, y=0.3, s=f"And now it's bold", font=bold_font, fontsize=20, ha="center")
|
|
145
|
+
plt.show()
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+

|
|
149
|
+
|
|
150
|
+
<br><br>
|
|
151
|
+
|
|
152
|
+
# Other feature: download a font locally
|
|
153
|
+
|
|
154
|
+
If for some reason you want to store the fonts you're working with, simply use the `download_font()` function. It just needs the arguments `font_url` (as described above) and `destination_path` (where you want to store them, by default in the current directory).
|
|
155
|
+
|
|
156
|
+
You can suppress the output message by adding `verbose=False` to it.
|
|
157
|
+
|
|
158
|
+
```python
|
|
159
|
+
from pyfonts import download_font
|
|
160
|
+
|
|
161
|
+
download_font(
|
|
162
|
+
font_url="https://github.com/google/fonts/raw/main/apache/ultra/Ultra-Regular.ttf",
|
|
163
|
+
destination_path="/Users/josephbarbier/Desktop/myfont.ttf", # optional
|
|
164
|
+
)
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
`Font installed at: /Users/josephbarbier/Desktop/myfont.ttf`
|
|
168
|
+
|
|
169
|
+
<br><br>
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
from matplotlib.font_manager import FontProperties
|
|
2
|
+
import os
|
|
3
|
+
|
|
4
|
+
from .utils import _get_font, _add_font_locally
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def load_font(font_url: str) -> FontProperties:
|
|
8
|
+
"""
|
|
9
|
+
Loads a font from the exact URL provided.
|
|
10
|
+
|
|
11
|
+
Parameters:
|
|
12
|
+
- font_url (str): The exact URL of a font file.
|
|
13
|
+
|
|
14
|
+
Returns:
|
|
15
|
+
- matplotlib.font_manager.FontProperties: A FontProperties object containing the loaded font.
|
|
16
|
+
"""
|
|
17
|
+
font = _get_font(font_url=font_url)
|
|
18
|
+
return font
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def download_font(
|
|
22
|
+
font_url: str,
|
|
23
|
+
destination_path: str | None = None,
|
|
24
|
+
verbose = True,
|
|
25
|
+
) -> None:
|
|
26
|
+
"""
|
|
27
|
+
Download a font from the exact URL provided.
|
|
28
|
+
|
|
29
|
+
Parameters:
|
|
30
|
+
- font_url (str): The exact URL of a font file.
|
|
31
|
+
- destination_path (str): The path where the font file will be saved. If None, it will be saved in the current working directory.
|
|
32
|
+
|
|
33
|
+
Returns:
|
|
34
|
+
- None
|
|
35
|
+
"""
|
|
36
|
+
if destination_path is None:
|
|
37
|
+
destination_path = os.path.join(
|
|
38
|
+
os.getcwd(), f"{font_url.split('/')[-1]}"
|
|
39
|
+
)
|
|
40
|
+
os.makedirs(os.path.dirname(destination_path), exist_ok=True)
|
|
41
|
+
|
|
42
|
+
_add_font_locally(
|
|
43
|
+
font_url=font_url, destination_path=destination_path, verbose=verbose
|
|
44
|
+
)
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
from urllib.request import urlopen
|
|
2
|
+
from urllib.error import URLError, HTTPError
|
|
3
|
+
from urllib.parse import urlparse
|
|
4
|
+
from tempfile import NamedTemporaryFile
|
|
5
|
+
from matplotlib.font_manager import FontProperties
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def _get_font(font_url: str) -> FontProperties:
|
|
9
|
+
with NamedTemporaryFile(delete=False, suffix=".ttf") as temp_file:
|
|
10
|
+
try:
|
|
11
|
+
if not _is_valid_font_url(font_url=font_url):
|
|
12
|
+
raise ValueError(
|
|
13
|
+
f"""
|
|
14
|
+
The URL provided ({font_url}) does not appear to be valid.
|
|
15
|
+
If you think this is an error, please open an issue at
|
|
16
|
+
https://github.com/JosephBARBIERDARNAL/pyfonts/issues
|
|
17
|
+
"""
|
|
18
|
+
)
|
|
19
|
+
else:
|
|
20
|
+
response = urlopen(font_url)
|
|
21
|
+
temp_file.write(response.read())
|
|
22
|
+
except HTTPError as e:
|
|
23
|
+
if e.code == 404:
|
|
24
|
+
raise Exception(
|
|
25
|
+
f"""
|
|
26
|
+
Font file not found.
|
|
27
|
+
"""
|
|
28
|
+
)
|
|
29
|
+
except URLError:
|
|
30
|
+
raise Exception(
|
|
31
|
+
f"Failed to load font. This may be due to a lack of internet connection"
|
|
32
|
+
)
|
|
33
|
+
font = FontProperties(fname=temp_file.name)
|
|
34
|
+
return font
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def _add_font_locally(font_url: str, destination_path: str, verbose: bool) -> None:
|
|
38
|
+
if not destination_path.endswith(".ttf"):
|
|
39
|
+
destination_path += ".ttf"
|
|
40
|
+
try:
|
|
41
|
+
if not _is_valid_font_url(font_url=font_url):
|
|
42
|
+
raise ValueError(
|
|
43
|
+
f"""
|
|
44
|
+
The URL provided ({font_url}) does not appear to be valid.
|
|
45
|
+
If you think this is an error, please open a folder at the following address
|
|
46
|
+
https://github.com/JosephBARBIERDARNAL/pyfonts/issues
|
|
47
|
+
"""
|
|
48
|
+
)
|
|
49
|
+
else:
|
|
50
|
+
response = urlopen(font_url)
|
|
51
|
+
with open(destination_path, "wb") as out_file:
|
|
52
|
+
out_file.write(response.read())
|
|
53
|
+
if verbose:
|
|
54
|
+
print(f"Font installed at: {destination_path}")
|
|
55
|
+
except HTTPError as e:
|
|
56
|
+
if e.code == 404:
|
|
57
|
+
raise ValueError(f"No font file found at {font_url}")
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def _is_valid_font_url(font_url: str) -> bool:
|
|
61
|
+
|
|
62
|
+
try:
|
|
63
|
+
result = urlparse(font_url)
|
|
64
|
+
if not all([result.scheme, result.netloc, result.path]):
|
|
65
|
+
return False
|
|
66
|
+
except:
|
|
67
|
+
return False
|
|
68
|
+
|
|
69
|
+
if result.scheme not in ['http', 'https']:
|
|
70
|
+
return False
|
|
71
|
+
|
|
72
|
+
return True
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: pyfonts
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: A simple way to load fonts for matplotlib
|
|
5
|
+
Home-page: https://github.com/JosephBARBIERDARNAL/pyfonts/blob/main/README.md
|
|
6
|
+
Author: Joseph Barbier
|
|
7
|
+
Author-email: Joseph Barbier <joseph.barbierdarnal@gmail.com>
|
|
8
|
+
Project-URL: Homepage, https://github.com/JosephBARBIERDARNAL/pyfonts
|
|
9
|
+
Project-URL: Issues, https://github.com/JosephBARBIERDARNAL/pyfonts/issues
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Classifier: Framework :: Matplotlib
|
|
14
|
+
Requires-Python: >=3.8
|
|
15
|
+
Description-Content-Type: text/markdown
|
|
16
|
+
Requires-Dist: matplotlib
|
|
17
|
+
|
|
18
|
+
# PyFonts
|
|
19
|
+
|
|
20
|
+
<img src="https://github.com/JosephBARBIERDARNAL/pyfonts/blob/main/logo.png?raw=true" alt="pyfonts logo" align="right" width="200px"/>
|
|
21
|
+
|
|
22
|
+
A simple way to load fonts for matplotlib.
|
|
23
|
+
|
|
24
|
+
<br>
|
|
25
|
+
|
|
26
|
+
Check out the [online documentation](https://pyfonts.streamlit.app/).
|
|
27
|
+
|
|
28
|
+
Table of content:
|
|
29
|
+
|
|
30
|
+
- [Quick Start](#quick-start)
|
|
31
|
+
- [Find fonts](#how-to-find-fonts)
|
|
32
|
+
- [Fonts in Matplotlib](#pyfonts-and-matplotlib)
|
|
33
|
+
- [Other feature: download fonts](#other-feature-download-a-font-locally)
|
|
34
|
+
|
|
35
|
+
<br><br>
|
|
36
|
+
|
|
37
|
+
# Installation
|
|
38
|
+
|
|
39
|
+
You can install `pyfonts` directly from PyPI with:
|
|
40
|
+
|
|
41
|
+
```
|
|
42
|
+
pip install pyfonts
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
Alternatively you can install the **development version** with:
|
|
46
|
+
|
|
47
|
+
```
|
|
48
|
+
pip install git+https://github.com/JosephBARBIERDARNAL/pyfonts.git
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
<br><br>
|
|
52
|
+
|
|
53
|
+
# Quick start
|
|
54
|
+
|
|
55
|
+
```python
|
|
56
|
+
from pyfonts import load_font
|
|
57
|
+
import matplotlib.pyplot as plt
|
|
58
|
+
|
|
59
|
+
# load font
|
|
60
|
+
font = load_font(
|
|
61
|
+
font_url="https://github.com/google/fonts/raw/main/apache/ultra/Ultra-Regular.ttf"
|
|
62
|
+
)
|
|
63
|
+
|
|
64
|
+
# check how the font looks on a minimalist example
|
|
65
|
+
fig, ax = plt.subplots(figsize=(10, 6))
|
|
66
|
+
ax.text(
|
|
67
|
+
x=0.5,
|
|
68
|
+
y=0.5,
|
|
69
|
+
s=f"What an easy way to load fonts, isn't it?",
|
|
70
|
+
font=font,
|
|
71
|
+
fontsize=20,
|
|
72
|
+
ha="center",
|
|
73
|
+
)
|
|
74
|
+
plt.show()
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+

|
|
78
|
+
|
|
79
|
+
<br><br>
|
|
80
|
+
|
|
81
|
+
# How to find fonts?
|
|
82
|
+
|
|
83
|
+
### Google font
|
|
84
|
+
|
|
85
|
+
There are many fonts available on the web. The **easiest way** to find one is to follow these steps:
|
|
86
|
+
|
|
87
|
+
- Browse [Google Font website](https://fonts.google.com/) to find a font that you like. Let's say you want to use [Amaranth](https://fonts.google.com/specimen/Amaranth?query=amaranth).
|
|
88
|
+
- Go to [Google font github repository](https://github.com/google/fonts) and type the name of your desired font in the search bar. We find that Urbanist font file in **Bold** is named `https://github.com/google/fonts/blob/main/ofl/amaranth/Amaranth-Bold.ttf`.
|
|
89
|
+
- copy the url and add `?raw=true` at the end, which gives us `https://github.com/google/fonts/blob/main/ofl/amaranth/Amaranth-Bold.ttf?raw=true`
|
|
90
|
+
- and that's it! Just pass this to `load_font()` to use it in your matplotlib charts
|
|
91
|
+
|
|
92
|
+
```python
|
|
93
|
+
from pyfonts import load_font
|
|
94
|
+
import matplotlib.pyplot as plt
|
|
95
|
+
|
|
96
|
+
font = load_font(
|
|
97
|
+
font_url="https://github.com/google/fonts/blob/main/ofl/amaranth/Amaranth-Bold.ttf?raw=true"
|
|
98
|
+
)
|
|
99
|
+
|
|
100
|
+
fig, ax = plt.subplots(figsize=(10, 6))
|
|
101
|
+
ax.text(
|
|
102
|
+
x=0.5,
|
|
103
|
+
y=0.5,
|
|
104
|
+
s=f"Congrats, you now have a cool font!",
|
|
105
|
+
font=font,
|
|
106
|
+
fontsize=20,
|
|
107
|
+
ha="center",
|
|
108
|
+
)
|
|
109
|
+
plt.show()
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
For the url to be readable by `PyFonts` when using a Github url, it must be in one of (the following 3 urls are completely equivalent):
|
|
113
|
+
|
|
114
|
+
- `https://github.com/google/fonts/blob/main/apache/ultra/Ultra-Regular.ttf?raw=true`
|
|
115
|
+
- `https://github.com/google/fonts/raw/main/apache/ultra/Ultra-Regular.ttf`
|
|
116
|
+
- `https://raw.githubusercontent.com/google/fonts/raw/main/apache/ultra/Ultra-Regular.ttf`
|
|
117
|
+
|
|
118
|
+
The **recommended** is the first (`https://github.com/google/fonts/blob/main/apache/ultra/Ultra-Regular.ttf?raw=true`) because you just need to add `?raw=true` after the end of the Github url.
|
|
119
|
+
|
|
120
|
+
### Other places for fonts
|
|
121
|
+
|
|
122
|
+
Github is the ideal place to find fonts under a free licence. You can find many fonts on the web. Just make sure that the licence of the font allows you to use it in your project.
|
|
123
|
+
|
|
124
|
+
You can find other fonts at:
|
|
125
|
+
|
|
126
|
+
- [Awesome fonts](https://github.com/brabadu/awesome-fonts)
|
|
127
|
+
- [Nerd fonts](https://github.com/ryanoasis/nerd-fonts)
|
|
128
|
+
|
|
129
|
+
<br><br>
|
|
130
|
+
|
|
131
|
+
# PyFonts and Matplotlib
|
|
132
|
+
|
|
133
|
+
### How it works
|
|
134
|
+
|
|
135
|
+
In order to work with any font, `PyFonts` creates a temporary file and uses this file to create a [FontProperties](https://matplotlib.org/stable/api/font_manager_api.html#matplotlib.font_manager.FontProperties) object. Once the object has been created with your font, the program deletes the temporary file as it no longer needs it.
|
|
136
|
+
|
|
137
|
+
### Different weight and style
|
|
138
|
+
|
|
139
|
+
When you load a font, **you don't load all its extensions**: bold, italic, thin etc, but only the one from the url. If you want to be able to use a font and its **bold** version, for example, you need to load both fonts:
|
|
140
|
+
|
|
141
|
+
```python
|
|
142
|
+
from pyfonts import load_font
|
|
143
|
+
import matplotlib.pyplot as plt
|
|
144
|
+
|
|
145
|
+
font = load_font(
|
|
146
|
+
font_url="https://github.com/google/fonts/blob/main/ofl/amaranth/Amaranth-Regular.ttf?raw=true"
|
|
147
|
+
)
|
|
148
|
+
bold_font = load_font(
|
|
149
|
+
font_url="https://github.com/google/fonts/blob/main/ofl/amaranth/Amaranth-Bold.ttf?raw=true"
|
|
150
|
+
)
|
|
151
|
+
|
|
152
|
+
fig, ax = plt.subplots(figsize=(10, 6))
|
|
153
|
+
ax.text(
|
|
154
|
+
x=0.5,
|
|
155
|
+
y=0.5,
|
|
156
|
+
s=f"Congrats, you now have a cool font!",
|
|
157
|
+
font=font,
|
|
158
|
+
fontsize=20,
|
|
159
|
+
ha="center",
|
|
160
|
+
)
|
|
161
|
+
ax.text(x=0.5, y=0.3, s=f"And now it's bold", font=bold_font, fontsize=20, ha="center")
|
|
162
|
+
plt.show()
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+

|
|
166
|
+
|
|
167
|
+
<br><br>
|
|
168
|
+
|
|
169
|
+
# Other feature: download a font locally
|
|
170
|
+
|
|
171
|
+
If for some reason you want to store the fonts you're working with, simply use the `download_font()` function. It just needs the arguments `font_url` (as described above) and `destination_path` (where you want to store them, by default in the current directory).
|
|
172
|
+
|
|
173
|
+
You can suppress the output message by adding `verbose=False` to it.
|
|
174
|
+
|
|
175
|
+
```python
|
|
176
|
+
from pyfonts import download_font
|
|
177
|
+
|
|
178
|
+
download_font(
|
|
179
|
+
font_url="https://github.com/google/fonts/raw/main/apache/ultra/Ultra-Regular.ttf",
|
|
180
|
+
destination_path="/Users/josephbarbier/Desktop/myfont.ttf", # optional
|
|
181
|
+
)
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
`Font installed at: /Users/josephbarbier/Desktop/myfont.ttf`
|
|
185
|
+
|
|
186
|
+
<br><br>
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
README.md
|
|
2
|
+
pyproject.toml
|
|
3
|
+
setup.py
|
|
4
|
+
pyfonts/__init__.py
|
|
5
|
+
pyfonts/main.py
|
|
6
|
+
pyfonts/utils.py
|
|
7
|
+
pyfonts.egg-info/PKG-INFO
|
|
8
|
+
pyfonts.egg-info/SOURCES.txt
|
|
9
|
+
pyfonts.egg-info/dependency_links.txt
|
|
10
|
+
pyfonts.egg-info/requires.txt
|
|
11
|
+
pyfonts.egg-info/top_level.txt
|
|
12
|
+
tests/__init__.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
matplotlib
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "pyfonts"
|
|
3
|
+
version = "0.0.1"
|
|
4
|
+
authors = [
|
|
5
|
+
{ name="Joseph Barbier", email="joseph.barbierdarnal@gmail.com" },
|
|
6
|
+
]
|
|
7
|
+
description = "A simple way to load fonts for matplotlib"
|
|
8
|
+
readme = "README.md"
|
|
9
|
+
requires-python = ">=3.8"
|
|
10
|
+
classifiers = [
|
|
11
|
+
"Programming Language :: Python :: 3",
|
|
12
|
+
"License :: OSI Approved :: MIT License",
|
|
13
|
+
"Operating System :: OS Independent",
|
|
14
|
+
"Framework :: Matplotlib"
|
|
15
|
+
]
|
|
16
|
+
dependencies = [
|
|
17
|
+
"matplotlib"
|
|
18
|
+
]
|
|
19
|
+
|
|
20
|
+
[build-system]
|
|
21
|
+
requires = ["setuptools"]
|
|
22
|
+
build-backend = "setuptools.build_meta"
|
|
23
|
+
|
|
24
|
+
[project.urls]
|
|
25
|
+
Homepage = "https://github.com/JosephBARBIERDARNAL/pyfonts"
|
|
26
|
+
Issues = "https://github.com/JosephBARBIERDARNAL/pyfonts/issues"
|
pyfonts-0.0.1/setup.cfg
ADDED
pyfonts-0.0.1/setup.py
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
from setuptools import setup, find_packages
|
|
2
|
+
|
|
3
|
+
setup(
|
|
4
|
+
name="pyfonts",
|
|
5
|
+
version="0.0.1",
|
|
6
|
+
packages=find_packages(),
|
|
7
|
+
description="A simple way to load fonts for matplotlib",
|
|
8
|
+
long_description=open("README.md").read(),
|
|
9
|
+
long_description_content_type="text/markdown",
|
|
10
|
+
author="Joseph Barbier",
|
|
11
|
+
author_email="joseph.barbierdarnal@gmail.com",
|
|
12
|
+
url="https://github.com/JosephBARBIERDARNAL/pyfonts/blob/main/README.md",
|
|
13
|
+
install_requires=[
|
|
14
|
+
"matplotlib"
|
|
15
|
+
],
|
|
16
|
+
)
|
|
File without changes
|