Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updates to fonts #8756

Merged
merged 12 commits into from
Dec 1, 2021
41 changes: 18 additions & 23 deletions kolibri/core/assets/src/utils/setupAndLoadFonts.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import FontFaceObserver from 'fontfaceobserver';
import { availableLanguages, currentLanguage } from 'kolibri.utils.i18n';
import logger from 'kolibri.lib.logging';
import { browser, userAgent, passesRequirements } from './browserInfo';
import { browser, passesRequirements } from './browserInfo';
import plugin_data from 'plugin_data';

const logging = logger.getLogger(__filename);

const modernFontBrowsers = {
// These browsers fully support font-display: swap
// https://caniuse.com/?search=swap
const fontSwapSupportingBrowsers = {
Edge: {
major: 17,
},
Expand Down Expand Up @@ -35,7 +37,12 @@ const modernFontBrowsers = {
},
};

function loadFullFonts() {
// See
// https://github.com/learningequality/kolibri-design-system/blob/main/lib/styles/definitions.scss
const FULL_FONTS = 'full-fonts-loaded';
const PARTIAL_FONTS = 'partial-fonts-loaded';

function loadFullFontsProgressively() {
/*
* This function eagerly loads the full fonts for the current language asynchronously, but
* avoids referencing them until they've been fully loaded. This is done by adding a
Expand All @@ -44,26 +51,8 @@ function loadFullFonts() {
*
* This prevents the text from being invisible while the fonts are loading ("FOIT")
* and instead falls back on system fonts while they're loading ("FOUT").
*
* We need to do this even for 'modern' browsers, because not all browsers implement
* fall-back behaviors of the font stacks correctly. See:
*
* https://bugs.chromium.org/p/chromium/issues/detail?id=897539
*/

// We use the <html> element to store the CSS 'loaded' class
const htmlEl = document.documentElement;
const FULL_FONTS = 'full-fonts-loaded';
const PARTIAL_FONTS = 'partial-fonts-loaded';

// Skip partial font usage and observer for Edge browser as a workaround for
// https://github.com/learningequality/kolibri/issues/4515
// TODO: figure out exactly why this was happening and remove this logic.
if (/Edge/.test(userAgent)) {
htmlEl.classList.add(FULL_FONTS);
return;
}

htmlEl.classList.add(PARTIAL_FONTS);

const uiNormal = new FontFaceObserver('noto-full', { weight: 400 });
Expand All @@ -82,6 +71,11 @@ function loadFullFonts() {
});
}

function loadFullFontsImmediately() {
const htmlEl = document.documentElement;
htmlEl.classList.add(FULL_FONTS);
}

function addFontStylesheetLink(href) {
const fonts = document.createElement('link');
fonts.rel = 'stylesheet';
Expand All @@ -91,10 +85,11 @@ function addFontStylesheetLink(href) {
}

export default function setupAndLoadFonts() {
if (passesRequirements(browser, modernFontBrowsers)) {
if (passesRequirements(browser, fontSwapSupportingBrowsers)) {
addFontStylesheetLink(plugin_data.fullCSSFileModern);
loadFullFontsImmediately();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could return early here and avoid the else clause.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes! sounds like a comment I might usually leave :)

} else {
addFontStylesheetLink(plugin_data.fullCSSFileBasic);
loadFullFontsProgressively();
}
loadFullFonts();
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
.full-fonts-loaded & {
// Unlike in the 'font-family-noto' mixin, here 'noto-full' has the lowest priority
// to prevent downloading full font files simply to display a single language name.
font-family: 'noto-subset', 'noto-common', 'noto-full', sans-serif;
font-family: 'noto-common', 'noto-subset', 'noto-full', sans-serif;
}
}
6 changes: 6 additions & 0 deletions kolibri/core/kolibri_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,15 @@ def language_font_file_tags(self):
common_file = static("assets/fonts/noto-common.css")
subset_file = static("assets/fonts/noto-subset.{}.css".format(language_code))
return [
'<link type="text/css" href="{common_css_file}?v={version}" rel="preload" as="style"/>'.format(
common_css_file=common_file, version=kolibri.__version__
),
'<link type="text/css" href="{common_css_file}?v={version}" rel="stylesheet"/>'.format(
common_css_file=common_file, version=kolibri.__version__
),
'<link type="text/css" href="{common_css_file}?v={version}" rel="preload" as="style"/>'.format(
common_css_file=subset_file, version=kolibri.__version__
),
'<link type="text/css" href="{subset_css_file}?v={version}" rel="stylesheet"/>'.format(
subset_css_file=subset_file, version=kolibri.__version__
),
Expand Down
4,862 changes: 1,989 additions & 2,873 deletions kolibri/core/static/assets/fonts/noto-common.css

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions kolibri/core/static/assets/fonts/noto-full.NotoSans.400.woff
Git LFS file not shown
4 changes: 2 additions & 2 deletions kolibri/core/static/assets/fonts/noto-full.NotoSans.700.woff
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Loading