Skip to content

Commit

Permalink
Refactor font loading (#597)
Browse files Browse the repository at this point in the history
* Refactor font loading

We now only provide a mechanism for *loading* fonts from Google.
To *set* a font, the user needs to provide custom CSS.
The documentation gives an example.

* Fix Hero font

---------

Co-authored-by: Jarrod Millman <jarrod.millman@gmail.com>
  • Loading branch information
stefanv and jarrodmillman authored May 3, 2024
1 parent 0bb187f commit fa5570b
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 27 deletions.
2 changes: 1 addition & 1 deletion assets/theme-css/hero.css
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
align-items: center;
display: flex;
flex-direction: row;
font-family: var(--fontFamily), sans-serif;
font-family: var(--pst-font-family-heading), sans-serif;
font-weight: 900;
font-size: 6em;
color: var(--pst-color-text-base);
Expand Down
2 changes: 0 additions & 2 deletions assets/theme-css/vars.css
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
/* prettier-ignore */
:root {
--fontFamily: "{{ .Site.Params.font.name }}", "Noto Sans Symbols 2";

--spht-color-dark: rgb(1, 50, 67);
--spht-color-light: rgb(255, 255, 255);
--spht-color-link-hover: rgb(77, 171, 207);
Expand Down
3 changes: 0 additions & 3 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ params:
images:
- /images/logo.svg
navColor: blue
font:
name: "Lato"
sizes: [400, 900]
plausible:
dataDomain: null
javaScript: "https://views.scientific-python.org/js/script.js"
1 change: 1 addition & 0 deletions doc/assets/css/custom.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.hero-title,
.navbar-logo-text {
font-family: "Lato";
}
3 changes: 3 additions & 0 deletions doc/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ params:
image: logo.svg
text: Scientific Python Hugo Theme
link: /
fonts:
- name: "Lato"
weights: [400, 900]
hero:
title: Scientific Python Theme
image: logo.svg
Expand Down
37 changes: 37 additions & 0 deletions doc/content/user_guide/styling.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,43 @@ Custom styles should be added to `/assets/css/my_css.css` (where

## CSS theme variables

## Fonts

To override the fonts used, add a custom CSS file and set the following variables:

```css
// General text
:root {
--pst-font-family-base: "Open Sans";
--pst-font-family-heading: "Open Sans";
--pst-font-family-monospace: "Open Sans";
}

// Navigation bar
.navbar-logo-text {
font-family: "Open Sans";
}
```

After setting the font, we still need to ensure that it is loaded.
If you are happy loading your fonts from Google Fonts, add them to your `config.yaml`:

```yaml
params:
fonts:
- name: "Lato"
weights: [400, 900]
```
Alternatively, create `layouts/partials/head.html` and add what you need to load the font, e.g.:

```html
<link
rel="stylesheet"
href="https://fonts.googleapis.com/css2?family=Lato:wght@400;900"
/>
```

### Color variables

We mostly rely on PyData Sphinx Theme's
Expand Down
3 changes: 3 additions & 0 deletions layouts/_default/baseof.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
<!-- Render theme css -->
{{ partial "css.html" . -}}

<!-- Add MathJax, if needed -->
{{ partial "mathjax.html" }}

<!-- Render custom header -->
{{ partial "head.html" . -}}

Expand Down
17 changes: 10 additions & 7 deletions layouts/partials/css.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,19 @@
{{- $cssOpts := collections.Merge $sassCompiler $serverOpts -}}

<!-- Fonts -->
{{- $fontName := .Site.Params.font.name | default "Open Sans" -}}
{{- $fontFace := replace $fontName " " "+" -}}
{{- $fontSizes := delimit (.Site.Params.font.sizes | default (slice 300 400 600 700)) ";" -}}
{{- $fontUrl := printf "https://fonts.googleapis.com/css2?family=%s:wght@%s" $fontFace $fontSizes -}}

<link rel="icon" href="{{ "images/favicon.ico" | relURL }}" />

{{ with $fonts := .Site.Params.fonts -}}
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>

{{ range $fonts }}
{{- $fontFace := replace .name " " "+" -}}
{{- $fontSizes := delimit (.weights | default (slice 300 400 600 700)) ";" -}}
{{- $fontUrl := printf "https://fonts.googleapis.com/css2?family=%s:wght@%s" $fontFace $fontSizes -}}
<link rel="stylesheet" href="{{ $fontUrl }}">
{{- end }}
{{- end }}

<link rel="icon" href="{{ "images/favicon.ico" | relURL }}" />

<!-- Fallback font for symbols (such as "🛈"). --
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Noto+Sans+Symbols+2">
Expand Down
14 changes: 0 additions & 14 deletions layouts/partials/head.html
Original file line number Diff line number Diff line change
@@ -1,14 +0,0 @@
{{ if (findRE `(\$.*\$)|(\\\\\[.*\\\\\])|(\\\\\(.*\\\\\))` .Content) }}
<script>
MathJax = {
tex: {
displayMath: [['$$', '$$'], ['\\[', '\\]']],
inlineMath: [['$', '$'], ['\\(', '\\)']],
}
};
</script>
<script type="text/javascript" id="MathJax-script" async
src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml.js">
</script>
{{ end }}
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Fira+Sans">
12 changes: 12 additions & 0 deletions layouts/partials/mathjax.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{{ if (findRE `(\$.*\$)|(\\\\\[.*\\\\\])|(\\\\\(.*\\\\\))` .Content) }}
<script>
MathJax = {
tex: {
displayMath: [['$$', '$$'], ['\\[', '\\]']],
inlineMath: [['$', '$'], ['\\(', '\\)']],
}
};
</script>
<script type="text/javascript" id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml.js">
</script>
{{ end }}

0 comments on commit fa5570b

Please sign in to comment.