Skip to content

Commit 5e7c9f0

Browse files
committed
avoid duplicate font loading
1 parent 4f8c618 commit 5e7c9f0

File tree

6 files changed

+35
-18
lines changed

6 files changed

+35
-18
lines changed

assets/sass/components/_button.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
gap: var(--button-gap);
3636
font-size: var(--button-font-size);
3737
font-weight: var(--button-font-weight);
38-
font-family: 'ButtonFont', 'DefaultFont', 'Dosis', sans-serif;
38+
font-family: var(--font-family-button, --font-family-default), 'Dosis', sans-serif;
3939
font-style: var(--button-font-style, normal);
4040
color: var(--button-color);
4141
text-align: center;

assets/sass/components/_buzzer.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
.buzzer-message {
2-
font-family: 'BuzzerMessageFont', 'DefaultFont', 'Dosis', sans-serif;
2+
font-family: var(--font-family-button_buzzer_message, --font-family-default), 'Dosis', sans-serif;
33
font-weight: var(--buzzer-message-font-weight, var(--font-weight, 400));
44
font-style: var(--buzzer-message-font-style, var(--font-style, normal));
55
color: var(--buzzer-message-font-color, var(--font-color));

assets/sass/components/_gallery.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
flex-grow: 1;
5353
h1 {
5454
margin: 0;
55-
font-family: 'GalleryFont', 'DefaultFont', 'Dosis', sans-serif;
55+
font-family: var(--font-family-gallery-title, --font-family-default), 'Dosis', sans-serif;
5656
color: var(--gallery-title-color, inherit);
5757
font-weight: var(--gallery-title-weight, 400);
5858
font-style: var(--gallery-title-style, normal);

assets/sass/components/_screensaver.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
width: 100%;
4444
text-align: center;
4545
color: var(--screensaver-text-color, #ffffff);
46-
font-family: 'ScreensaverFont', 'DefaultFont', 'Dosis', sans-serif;
46+
font-family: var(--font-family-screensaver-text, --font-family-default), 'Dosis', sans-serif;
4747
font-weight: var(--screensaver-text-weight, 400);
4848
font-style: var(--screensaver-text-style, normal);
4949
text-shadow: 0 2px 10px rgba(0, 0, 0, 0.7);

assets/sass/components/_stage.scss

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,14 +187,14 @@
187187
}
188188

189189
.stage--start .names .start-text {
190-
font-family: 'StartScreenFont', 'DefaultFont', 'Dosis', sans-serif;
190+
font-family: var(--font-family-start-text, --font-family-default), 'Dosis', sans-serif;
191191
color: var(--start-text-color, inherit);
192192
font-weight: var(--start-text-weight, 400);
193193
font-style: var(--start-text-style, normal);
194194
}
195195

196196
.stage--start .names .event-text {
197-
font-family: 'EventFont', 'DefaultFont', 'Dosis', sans-serif;
197+
font-family: var(--font-family-event-text, --font-family-default), 'Dosis', sans-serif;
198198
color: var(--event-text-color, inherit);
199199
font-weight: var(--event-text-weight, 400);
200200
font-style: var(--event-text-style, normal);

src/Utility/ThemeUtility.php

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,24 @@ public static function renderCustomUserStyle(array $config): string
3535

3636
$fontCss = '';
3737

38+
$loadedFontUrls = [];
39+
40+
$appendFontFace = static function (string $family, string $path) use (&$fontCss, &$loadedFontUrls): string {
41+
$url = PathUtility::getPublicPath($path);
42+
43+
// Avoid emitting duplicate @font-face rules when the same font file is used in multiple slots.
44+
if (!isset($loadedFontUrls[$url])) {
45+
$loadedFontUrls[$url] = $family;
46+
$fontCss .= "@font-face {font-family:'{$family}';src:url('{$url}') format('truetype');font-display:swap;}\n";
47+
}
48+
49+
return $loadedFontUrls[$url];
50+
};
51+
3852
// Default font
3953
if (!empty($config['fonts']['default'])) {
40-
$fontCss .= "@font-face {font-family:'DefaultFont';src:url('" . PathUtility::getPublicPath($config['fonts']['default']) . "') format('truetype');font-display:swap;}\n";
54+
$fontFamily = $appendFontFace('DefaultFont', $config['fonts']['default']);
55+
$properties['--font-family-default'] = $fontFamily;
4156
}
4257
if (!empty($config['fonts']['default_color'])) {
4358
$properties['--font-color'] = $config['fonts']['default_color'];
@@ -47,51 +62,53 @@ public static function renderCustomUserStyle(array $config): string
4762

4863
// Start screen font
4964
if (!empty($config['fonts']['start_screen_title'])) {
50-
$fontCss .= "@font-face {font-family:'StartScreenFont';src:url('" . PathUtility::getPublicPath($config['fonts']['start_screen_title']) . "') format('truetype');font-display:swap;}\n";
65+
$fontFamily = $appendFontFace('StartScreenFont', $config['fonts']['start_screen_title']);
66+
$properties['--font-family-start-text'] = $fontFamily;
5167
}
5268
$properties['--start-text-color'] = $config['fonts']['start_screen_title_color'] ?? '__UNSET__';
5369
$properties['--start-text-weight'] = !empty($config['fonts']['start_screen_title_bold']) ? '700' : '400';
5470
$properties['--start-text-style'] = !empty($config['fonts']['start_screen_title_italic']) ? 'italic' : 'normal';
5571

5672
// Event font
5773
if (!empty($config['fonts']['event_text'])) {
58-
$fontCss .= "@font-face {font-family:'EventFont';src:url('" . PathUtility::getPublicPath($config['fonts']['event_text']) . "') format('truetype');font-display:swap;}\n";
74+
$fontFamily = $appendFontFace('EventFont', $config['fonts']['event_text']);
75+
$properties['--font-family-event-text'] = $fontFamily;
5976
}
6077
$properties['--event-text-color'] = $config['fonts']['event_text_color'] ?? '__UNSET__';
6178
$properties['--event-text-weight'] = !empty($config['fonts']['event_text_bold']) ? '700' : '400';
6279
$properties['--event-text-style'] = !empty($config['fonts']['event_text_italic']) ? 'italic' : 'normal';
6380

6481
// Gallery title font
6582
if (!empty($config['fonts']['gallery_title'])) {
66-
$fontCss .= "@font-face {font-family:'GalleryFont';src:url('" . PathUtility::getPublicPath($config['fonts']['gallery_title']) . "') format('truetype');font-display:swap;}\n";
83+
$fontFamily = $appendFontFace('GalleryFont', $config['fonts']['gallery_title']);
84+
$properties['--font-family-gallery-title'] = $fontFamily;
6785
}
6886
$properties['--gallery-title-color'] = $config['fonts']['gallery_title_color'] ?? '__UNSET__';
6987
$properties['--gallery-title-weight'] = !empty($config['fonts']['gallery_title_bold']) ? '700' : '400';
7088
$properties['--gallery-title-style'] = !empty($config['fonts']['gallery_title_italic']) ? 'italic' : 'normal';
7189

7290
// Screensaver font
7391
if (!empty($config['fonts']['screensaver_text'])) {
74-
$fontCss .= "@font-face {font-family:'ScreensaverFont';src:url('" . PathUtility::getPublicPath($config['fonts']['screensaver_text']) . "') format('truetype');font-display:swap;}\n";
75-
} elseif (!empty($config['screensaver']['text_font'])) {
76-
// fallback to legacy location
77-
$fontCss .= "@font-face {font-family:'ScreensaverFont';src:url('" . PathUtility::getPublicPath($config['screensaver']['text_font']) . "') format('truetype');font-display:swap;}\n";
92+
$fontFamily = $appendFontFace('ScreensaverFont', $config['fonts']['screensaver_text']);
93+
$properties['--font-family-screensaver-text'] = $fontFamily;
7894
}
79-
$properties['--screensaver-text-color'] = $config['fonts']['screensaver_text_color']
80-
?? ($config['screensaver']['text_color'] ?? '#ffffff');
95+
$properties['--screensaver-text-color'] = $config['fonts']['screensaver_text_color'] ?? '__UNSET__';
8196
$properties['--screensaver-text-weight'] = !empty($config['fonts']['screensaver_text_bold']) ? '700' : '400';
8297
$properties['--screensaver-text-style'] = !empty($config['fonts']['screensaver_text_italic']) ? 'italic' : 'normal';
8398

8499
// Font variables (button)
85100
if (!empty($config['fonts']['button_font'])) {
86-
$fontCss .= "@font-face {font-family:'ButtonFont';src:url('" . PathUtility::getPublicPath($config['fonts']['button_font']) . "') format('truetype');font-display:swap;}\n";
101+
$fontFamily = $appendFontFace('ButtonFont', $config['fonts']['button_font']);
102+
$properties['--font-family-button'] = $fontFamily;
87103
}
88104
$properties['--button-font-color'] = $config['fonts']['button_font_color'] ?? '__UNSET__';
89105
$properties['--button-font-weight'] = !empty($config['fonts']['button_font_bold']) ? '700' : '400';
90106
$properties['--button-font-style'] = !empty($config['fonts']['button_font_italic']) ? 'italic' : 'normal';
91107

92108
// Font variables (buzzer message)
93109
if (!empty($config['fonts']['button_buzzer_message_font'])) {
94-
$fontCss .= "@font-face {font-family:'BuzzerMessageFont';src:url('" . PathUtility::getPublicPath($config['fonts']['button_buzzer_message_font']) . "') format('truetype');font-display:swap;}\n";
110+
$fontFamily = $appendFontFace('BuzzerMessageFont', $config['fonts']['button_buzzer_message_font']);
111+
$properties['--font-family-button_buzzer_message'] = $fontFamily;
95112
}
96113
$properties['--buzzer-message-font-color'] = $config['fonts']['button_buzzer_message_font_color'] ?? '__UNSET__';
97114
$properties['--buzzer-message-font-weight'] = !empty($config['fonts']['button_buzzer_message_font_bold']) ? '700' : '400';

0 commit comments

Comments
 (0)