Skip to content

Commit

Permalink
Merge pull request #8756 from indirectlylit/fonts
Browse files Browse the repository at this point in the history
updates to fonts
rtibbles authored Dec 1, 2021
2 parents 5fd6077 + c5c2400 commit 36939a9
Showing 178 changed files with 32,803 additions and 27,900 deletions.
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,
},
@@ -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
@@ -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 });
@@ -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';
@@ -91,10 +85,11 @@ function addFontStylesheetLink(href) {
}

export default function setupAndLoadFonts() {
if (passesRequirements(browser, modernFontBrowsers)) {
if (passesRequirements(browser, fontSwapSupportingBrowsers)) {
addFontStylesheetLink(plugin_data.fullCSSFileModern);
loadFullFontsImmediately();
} else {
addFontStylesheetLink(plugin_data.fullCSSFileBasic);
loadFullFontsProgressively();
}
loadFullFonts();
}
Original file line number Diff line number Diff line change
@@ -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
@@ -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__
),
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

0 comments on commit 36939a9

Please sign in to comment.