Skip to content

Commit

Permalink
web: Add defaultFonts config option for specifying the default _sans etc
Browse files Browse the repository at this point in the history
  • Loading branch information
Dinnerbone committed Oct 30, 2023
1 parent 917ce74 commit 3758e97
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 1 deletion.
1 change: 1 addition & 0 deletions web/packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,5 @@ export const DEFAULT_CONFIG: Required<BaseLoadOptions> = {
openInNewTab: null,
socketProxy: [],
fontSources: [],
defaultFonts: {},
};
31 changes: 31 additions & 0 deletions web/packages/core/src/load-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,30 @@ export interface SocketProxy {
proxyUrl: string;
}

/**
* Defines the names of the fonts to use for each "default" Flash device font.
*
* The name of each font provided will be used, in priority order.
*
* For example, defining `sans: ["Helvetica", "Arial"]` would use Helvetica if present, before trying Arial.
*/
export interface DefaultFonts {
/**
* `_sans`, a Sans-Serif font (similar to Helvetica or Arial)
*/
sans?: Array<string>;

/**
* `_serif`, a Serif font (similar to Times Roman)
*/
serif?: Array<string>;

/**
* `_typewriter`, a Monospace font (similar to Courier)
*/
typewriter?: Array<string>;
}

/**
* Any options used for loading a movie.
*/
Expand Down Expand Up @@ -574,6 +598,13 @@ export interface BaseLoadOptions {
* @default []
*/
fontSources?: Array<string>;

/**
* The font names to use for each "default" Flash device font.
*
* @default {}
*/
defaultFonts?: DefaultFonts;
}

/**
Expand Down
19 changes: 19 additions & 0 deletions web/packages/core/src/ruffle-player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,25 @@ export class RufflePlayer extends HTMLElement {
}
}

if (this.loadedConfig?.defaultFonts?.sans) {
this.instance!.set_default_font(
"sans",
this.loadedConfig?.defaultFonts.sans,
);
}
if (this.loadedConfig?.defaultFonts?.serif) {
this.instance!.set_default_font(
"serif",
this.loadedConfig?.defaultFonts.serif,
);
}
if (this.loadedConfig?.defaultFonts?.typewriter) {
this.instance!.set_default_font(
"typewriter",
this.loadedConfig?.defaultFonts.typewriter,
);
}

this.instance!.set_volume(this.volumeSettings.get_volume());

this.rendererDebugInfo = this.instance!.renderer_debug_info();
Expand Down
23 changes: 22 additions & 1 deletion web/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ use ruffle_core::external::{
ExternalInterfaceMethod, ExternalInterfaceProvider, FsCommandProvider, Value as ExternalValue,
Value,
};
use ruffle_core::swf;
use ruffle_core::tag_utils::SwfMovie;
use ruffle_core::{swf, DefaultFont};
use ruffle_core::{
Color, Player, PlayerBuilder, PlayerEvent, SandboxType, StageAlign, StageScaleMode,
StaticCallstack, ViewportDimensions,
Expand Down Expand Up @@ -494,6 +494,27 @@ impl Ruffle {
});
}

pub fn set_default_font(&mut self, default_name: &str, fonts: Vec<JsValue>) {
let _ = self.with_core_mut(|core| {
let default = match default_name {
"sans" => DefaultFont::Sans,
"serif" => DefaultFont::Serif,
"typewriter" => DefaultFont::Typewriter,
name => {
tracing::error!("Unknown default font name '{name}'");
return;
}
};
core.set_default_font(
default,
fonts
.into_iter()
.flat_map(|value| value.as_string())
.collect(),
);
});
}

#[allow(clippy::boxed_local)] // for js_bind
pub fn call_exposed_callback(&self, name: &str, args: Box<[JsValue]>) -> JsValue {
let args: Vec<ExternalValue> = args.iter().map(js_to_external_value).collect();
Expand Down

0 comments on commit 3758e97

Please sign in to comment.