diff --git a/sources/application/Helper/WebResourcesHelper.php b/sources/application/Helper/WebResourcesHelper.php index 7f9db93da..061793485 100644 --- a/sources/application/Helper/WebResourcesHelper.php +++ b/sources/application/Helper/WebResourcesHelper.php @@ -20,6 +20,31 @@ use utils; */ class WebResourcesHelper { + //--------------------------------- + // Fonts + //--------------------------------- + + /** + * Preload necessary fonts to display them as soon as possible when CSS rules are interpreted + * + * @return string[] + * + * @throws \Exception + */ + public static function GetPreloadedFonts(): array + { + return [ + ['font' => utils::GetAbsoluteUrlAppRoot().'css/font-combodo/combodo-webfont.woff2', 'type' => 'woff2'], + ['font' => utils::GetAbsoluteUrlAppRoot().'css/font-awesome/webfonts/fa-solid-900.woff2', 'type' => 'woff2'], + ['font' => utils::GetAbsoluteUrlAppRoot().'node_modules/@fontsource/raleway/files/raleway-all-400-normal.woff', 'type' => 'woff'], + ['font' => utils::GetAbsoluteUrlAppRoot().'node_modules/@fontsource/raleway/files/raleway-all-500-normal.woff', 'type' => 'woff'], + ['font' => utils::GetAbsoluteUrlAppRoot().'node_modules/@fontsource/raleway/files/raleway-all-600-normal.woff', 'type' => 'woff'], + ['font' => utils::GetAbsoluteUrlAppRoot().'node_modules/@fontsource/raleway/files/raleway-all-700-normal.woff', 'type' => 'woff'], + ['font' => utils::GetAbsoluteUrlAppRoot().'node_modules/@fontsource/raleway/files/raleway-all-400-italic.woff', 'type' => 'woff'], + ['font' => utils::GetAbsoluteUrlAppRoot().'node_modules/@fontsource/raleway/files/raleway-all-500-italic.woff', 'type' => 'woff'], + ]; + } + //--------------------------------- // CKEditor //--------------------------------- diff --git a/sources/application/WebPage/WebPage.php b/sources/application/WebPage/WebPage.php index 7ebf9be0d..c214ddc9d 100644 --- a/sources/application/WebPage/WebPage.php +++ b/sources/application/WebPage/WebPage.php @@ -5,6 +5,7 @@ */ use Combodo\iTop\Application\Helper\Session; +use Combodo\iTop\Application\Helper\WebResourcesHelper; use Combodo\iTop\Application\TwigBase\Twig\TwigHelper; use Combodo\iTop\Application\UI\Base\Component\Alert\AlertUIBlockFactory; use Combodo\iTop\Application\UI\Base\Component\DataTable\DataTableUIBlockFactory; @@ -146,6 +147,10 @@ class WebPage implements Page * @since 3.0.0 */ protected $aBlockParams; + /** @var array Contain fonts that needs to be preloaded + * @since 3.0.0 + */ + protected $aPreloadedFonts; protected $a_headers; protected $a_base; protected $iNextId; @@ -196,6 +201,7 @@ class WebPage implements Page $this->InitializeStyles(); $this->InitializeLinkedStylesheets(); $this->InitializeCompatibilityLinkedStylesheets(); + $this->aPreloadedFonts = WebResourcesHelper::GetPreloadedFonts(); $this->a_headers = []; $this->a_base = ['href' => '', 'target' => '']; $this->iNextId = 0; @@ -1298,6 +1304,7 @@ JS; 'sCharset' => static::PAGES_CHARSET, 'sLang' => $this->GetLanguageForMetadata(), ], + 'aPreloadedFonts' => $this->aPreloadedFonts, 'aCssFiles' => $this->a_linked_stylesheets, 'aCssInline' => $this->a_styles, 'aJsInlineEarly' => $this->a_early_scripts, @@ -1750,4 +1757,14 @@ EOD { return $this->aBlockParams; } + + /** + * @param array $aFonts + * + * @since 3.0.0 + */ + public function AddPreloadedFonts(array $aFonts) + { + $this->aPreloadedFonts = array_merge($this->aPreloadedFonts, $aFonts); + } } diff --git a/sources/application/WebPage/iTopWebPage.php b/sources/application/WebPage/iTopWebPage.php index 67c54d379..e8b1cd52c 100644 --- a/sources/application/WebPage/iTopWebPage.php +++ b/sources/application/WebPage/iTopWebPage.php @@ -904,6 +904,7 @@ HTML; $aData['aPage'] = array_merge( $aData['aPage'], [ + 'aPreloadedFonts' => $this->aPreloadedFonts, 'aCssFiles' => $this->a_linked_stylesheets, 'aCssInline' => $this->a_styles, 'aJsInlineEarly' => $this->a_early_scripts, diff --git a/templates/pages/backoffice/webpage/layout.html.twig b/templates/pages/backoffice/webpage/layout.html.twig index adb480360..eb051ff8e 100644 --- a/templates/pages/backoffice/webpage/layout.html.twig +++ b/templates/pages/backoffice/webpage/layout.html.twig @@ -15,7 +15,9 @@ {% endif %} - + {% for sPreloadedFont in aPage.aPreloadedFonts %} + + {% endfor %} {# Stylesheets MUST be loaded before any scripts otherwise we may face problems such as - Visual glitches - jQuery scripts spurious problems (like failing on a 'reload') #}