Skip to content

Commit fd959a7

Browse files
authored
feat: allow specifying a custom font for the card text (#110)
* feat: allow specifying a custom font for the card text * docs: write "Customize the text font" section * fix: apply black
1 parent 457e75d commit fd959a7

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

docs/source/socialcards.md

+21-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ See [the opengraph.xyz website](https://www.opengraph.xyz/) for a way to preview
77
Here's an example of what the card for this page looks like:
88

99
% This is auto-generated at build time
10+
1011
```{image} ../tmp//num_0.png
1112
:width: 500
1213
```
@@ -42,14 +43,33 @@ ogp_social_cards = {
4243
Matplotlib does not support easy plotting of SVG images, so ensure that your image is a PNG or JPEG file, not SVG.
4344
```
4445

46+
## Customize the text font
47+
48+
By default, the Roboto Flex font is used to render the card text.
49+
50+
You can specify the other font name via ``font`` key:
51+
52+
```{code-block} python
53+
:caption: conf.py
54+
55+
ogp_social_cards = {
56+
"font": "Noto Sans CJK JP",
57+
}
58+
```
59+
60+
You might need to install an additional font package on your environment. Also, note that the font name needs to be
61+
discoverable by Matplotlib FontManager.
62+
See [Matplotlib documentation](https://matplotlib.org/stable/tutorials/text/text_props.html#default-font)
63+
for the information about FontManager.
64+
4565
## Customize the card
4666

4767
There are several customization options to change the text and look of the social media preview card.
4868
Below is a summary of these options.
4969

5070
- **`site_url`**: Set a custom site URL.
5171
- **`line_color`**: Color of the border line at the bottom of the card, in hex format.
52-
% TODO: add an over-ride for each part of the card.
72+
% TODO: add an over-ride for each part of the card.
5373

5474
## Example social cards
5575

sphinxext/opengraph/socialcards.py

+10-9
Original file line numberDiff line numberDiff line change
@@ -178,16 +178,17 @@ def create_social_card_objects(
178178
site_url_color="#2f363d",
179179
background_color="white",
180180
line_color="#5A626B",
181-
font="Roboto",
181+
font=None,
182182
):
183183
"""Create the Matplotlib objects for the first time."""
184-
# Load the Roboto font
185-
# TODO: Currently the `font` parameter above does nothing
186-
# Should instead make it possible to load remote fonts or local fonts
187-
# if a user specifies.
188-
path_font = Path(__file__).parent / "_static/Roboto-flex.ttf"
189-
font = matplotlib.font_manager.FontEntry(fname=str(path_font), name="Roboto")
190-
matplotlib.font_manager.fontManager.ttflist.append(font)
184+
# If no font specified, load the Roboto Flex font as a fallback
185+
if font is None:
186+
path_font = Path(__file__).parent / "_static/Roboto-flex.ttf"
187+
roboto_font = matplotlib.font_manager.FontEntry(
188+
fname=str(path_font), name="Roboto"
189+
)
190+
matplotlib.font_manager.fontManager.ttflist.append(roboto_font)
191+
font = roboto_font.name
191192

192193
# Because Matplotlib doesn't let you specify figures in pixels, only inches
193194
# This `multiple` results in a scale of about 1146px by 600px
@@ -214,7 +215,7 @@ def create_social_card_objects(
214215

215216
# Axes configuration
216217
left_margin = 0.05
217-
with plt.rc_context({"font.family": font.name}):
218+
with plt.rc_context({"font.family": font}):
218219
# Site title
219220
# Smaller font, just above page title
220221
site_title_y_offset = 0.87

0 commit comments

Comments
 (0)