Skip to content

Commit 92bd90b

Browse files
feat: template file load and font inject
1 parent e138caf commit 92bd90b

File tree

4 files changed

+90
-0
lines changed

4 files changed

+90
-0
lines changed

packages/webgal/src/Core/initializeScript.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import PixiStage from '@/Core/controller/stage/pixi/PixiController';
1414
import axios from 'axios';
1515
import { __INFO } from '@/config/info';
1616
import { WebGAL } from '@/Core/WebGAL';
17+
import { loadTemplate } from '@/Core/util/coreInitialFunction/templateLoader';
1718

1819
const u = navigator.userAgent;
1920
export const isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); // 判断是否是 iOS终端
@@ -26,6 +27,7 @@ export const initializeScript = (): void => {
2627
logger.info(`WebGAL v${__INFO.version}`);
2728
logger.info('Github: https://github.com/OpenWebGAL/WebGAL ');
2829
logger.info('Made with ❤ by OpenWebGAL');
30+
loadTemplate();
2931
// 激活强制缩放
3032
// 在调整窗口大小时重新计算宽高,设计稿按照 1600*900。
3133
if (isIOS) {
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import axios from 'axios';
2+
import { logger } from '@/Core/util/logger';
3+
import { WebGAL } from '@/Core/WebGAL';
4+
import { TemplateFontDescriptor, WebgalTemplate } from '@/types/template';
5+
6+
const TEMPLATE_PATH = './game/template/template.json';
7+
const TEMPLATE_FONT_STYLE_SELECTOR = 'style[data-webgal-template-fonts]';
8+
9+
export async function loadTemplate(): Promise<WebgalTemplate | null> {
10+
try {
11+
const { data } = await axios.get<WebgalTemplate>(TEMPLATE_PATH);
12+
WebGAL.template = data;
13+
injectTemplateFonts(data.fonts ?? []);
14+
return data;
15+
} catch (error) {
16+
logger.warn('加载模板文件失败', error);
17+
return null;
18+
}
19+
}
20+
21+
function injectTemplateFonts(fonts: TemplateFontDescriptor[]): void {
22+
if (!fonts.length) return;
23+
const rules = fonts.map((font) => generateFontFaceRule(font)).filter((rule): rule is string => Boolean(rule));
24+
25+
if (!rules.length) return;
26+
27+
const styleElement = document.createElement('style');
28+
styleElement.setAttribute('data-webgal-template-fonts', 'true');
29+
styleElement.appendChild(document.createTextNode(rules.join('\n')));
30+
31+
const head = document.head;
32+
if (!head) return;
33+
34+
const existing = head.querySelector<HTMLStyleElement>(TEMPLATE_FONT_STYLE_SELECTOR);
35+
existing?.remove();
36+
37+
head.appendChild(styleElement);
38+
}
39+
40+
function generateFontFaceRule(font: TemplateFontDescriptor): string | null {
41+
const fontFamily = font['font-family'];
42+
if (!fontFamily || !font.url || !font.type) {
43+
logger.warn('忽略无效的模板字体配置', font);
44+
return null;
45+
}
46+
47+
const src = resolveTemplateAssetPath(font.url);
48+
const weight = font.weight !== undefined ? `font-weight: ${font.weight};` : '';
49+
const style = font.style ? `font-style: ${font.style};` : '';
50+
const display = `font-display: ${font.display ?? 'swap'};`;
51+
52+
return `@font-face { font-family: '${fontFamily}'; src: url('${src}') format('${font.type}'); ${weight} ${style} ${display} }`;
53+
}
54+
55+
function resolveTemplateAssetPath(path: string): string {
56+
if (/^(https?:)?\/\//i.test(path) || path.startsWith('data:')) {
57+
return path;
58+
}
59+
const normalized = path.replace(/^[./]+/, '');
60+
return `./game/template/${normalized}`;
61+
}

packages/webgal/src/Core/webgalCore.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { SceneManager } from '@/Core/Modules/scene';
44
import { AnimationManager } from '@/Core/Modules/animations';
55
import { Gameplay } from './Modules/gamePlay';
66
import { Events } from '@/Core/Modules/events';
7+
import { WebgalTemplate } from '@/types/template';
78

89
export class WebgalCore {
910
public sceneManager = new SceneManager();
@@ -13,4 +14,5 @@ export class WebgalCore {
1314
public gameName = '';
1415
public gameKey = '';
1516
public events = new Events();
17+
public template: WebgalTemplate | null = null;
1618
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
export type TemplateFontFormat =
2+
| 'truetype'
3+
| 'opentype'
4+
| 'woff'
5+
| 'woff2'
6+
| 'embedded-opentype'
7+
| 'svg'
8+
| 'collection';
9+
10+
export interface TemplateFontDescriptor {
11+
'font-family': string;
12+
url: string;
13+
type: TemplateFontFormat;
14+
weight?: string | number;
15+
style?: string;
16+
display?: 'auto' | 'block' | 'swap' | 'fallback' | 'optional';
17+
}
18+
19+
export interface WebgalTemplate {
20+
name: string;
21+
id?: string;
22+
'webgal-version': string;
23+
fonts?: TemplateFontDescriptor[];
24+
[key: string]: unknown;
25+
}

0 commit comments

Comments
 (0)