Skip to content

Commit 730cc15

Browse files
committed
Support fontSources object as well as array of strings or objects
1 parent 6467690 commit 730cc15

File tree

2 files changed

+55
-29
lines changed

2 files changed

+55
-29
lines changed

web/packages/core/src/internal/player/inner.tsx

+43-25
Original file line numberDiff line numberDiff line change
@@ -597,39 +597,57 @@ export class InnerPlayer {
597597
builder.setVolume(this.volumeSettings.get_volume());
598598

599599
if (this.loadedConfig?.fontSources) {
600-
for (const key in this.loadedConfig.fontSources) {
601-
const fontSource = this.loadedConfig.fontSources[key];
602-
if (typeof fontSource === "string") {
603-
// Handle old format (array of strings)
604-
try {
605-
const response = await fetch(fontSource);
606-
builder.addFont(
607-
fontSource,
608-
null, // No custom name
609-
null, // No bold override
610-
null, // No italic override
611-
new Uint8Array(await response.arrayBuffer()),
612-
);
613-
} catch (error) {
614-
console.warn(`Couldn't download font source from ${fontSource}`, error);
615-
}
616-
} else if (typeof fontSource === "object") {
617-
// Handle new format (object with additional properties)
618-
for (const [url, fontInfo] of Object.entries(fontSource)) {
600+
const fontSources = this.loadedConfig.fontSources;
601+
if (Array.isArray(fontSources)) {
602+
for (const fontSource of fontSources) {
603+
if (typeof fontSource === "string") {
604+
// Handle old format (array of strings)
619605
try {
620-
const response = await fetch(url);
606+
const response = await fetch(fontSource);
621607
builder.addFont(
622-
url,
623-
fontInfo?.['name'] || null,
624-
fontInfo?.['bold'] ?? null,
625-
fontInfo?.['italics'] ?? null,
608+
fontSource,
609+
null, // No custom name
610+
null, // No bold override
611+
null, // No italic override
626612
new Uint8Array(await response.arrayBuffer()),
627613
);
628614
} catch (error) {
629-
console.warn(`Couldn't download font source from ${key}`, error);
615+
console.warn(`Couldn't download font source from ${fontSource}`, error);
616+
}
617+
} else if (typeof fontSource === "object") {
618+
// Handle new format (object with additional properties)
619+
for (const [url, fontInfo] of Object.entries(fontSource)) {
620+
try {
621+
const response = await fetch(url);
622+
builder.addFont(
623+
url,
624+
fontInfo?.['name'] || null,
625+
fontInfo?.['bold'] ?? null,
626+
fontInfo?.['italics'] ?? null,
627+
new Uint8Array(await response.arrayBuffer()),
628+
);
629+
} catch (error) {
630+
console.warn(`Couldn't download font source from ${url}`, error);
631+
}
630632
}
631633
}
632634
}
635+
} else if (typeof fontSources === "object") {
636+
// Handle the case where fontSources is a plain object
637+
for (const [url, fontInfo] of Object.entries(fontSources)) {
638+
try {
639+
const response = await fetch(url);
640+
builder.addFont(
641+
url,
642+
fontInfo?.['name'] || null,
643+
fontInfo?.['bold'] ?? null,
644+
fontInfo?.['italics'] ?? null,
645+
new Uint8Array(await response.arrayBuffer()),
646+
);
647+
} catch (error) {
648+
console.warn(`Couldn't download font source from ${url}`, error);
649+
}
650+
}
633651
}
634652
}
635653

web/packages/core/src/public/config/load-options.ts

+12-4
Original file line numberDiff line numberDiff line change
@@ -648,22 +648,30 @@ export interface BaseLoadOptions {
648648
*
649649
* These will be fetched by the browser as part of the loading of Flash content, which may slow down load times.
650650
*
651-
* When using an SWF source, and each font embedded within that SWF will be used as device font by Flash content.
651+
* When using an SWF source, each font embedded within that SWF will be used as device font by Flash content.
652652
*
653653
* If any URL fails to load (either it's an invalid file, or a network error occurs), Ruffle will log an error but continue without it.
654654
*
655655
* @default []
656656
*/
657-
fontSources?: Array<
658-
| string
657+
fontSources?:
659658
| {
660659
[key: string]: {
661660
name?: string;
662661
bold?: boolean;
663662
italics?: boolean;
664663
};
665664
}
666-
>;
665+
| Array<
666+
| string
667+
| {
668+
[key: string]: {
669+
name?: string;
670+
bold?: boolean;
671+
italics?: boolean;
672+
};
673+
}
674+
>;
667675

668676
/**
669677
* The font names to use for each "default" Flash device font.

0 commit comments

Comments
 (0)