| 47 | * MSDF 字体加载器 |
| 48 | */ |
| 49 | export class MSDFFontLoader { |
| 50 | private _textureService: ITextureService; |
| 51 | private _fontCache: Map<string, MSDFFont> = new Map(); |
| 52 | |
| 53 | constructor(textureService: ITextureService) { |
| 54 | this._textureService = textureService; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Load MSDF font from JSON and texture URLs |
| 59 | * 从 JSON 和纹理 URL 加载 MSDF 字体 |
| 60 | * |
| 61 | * @param name Font name for registration | 注册用的字体名称 |
| 62 | * @param jsonUrl URL to font JSON file | 字体 JSON 文件 URL |
| 63 | * @param textureUrl URL to font atlas texture | 字体图集纹理 URL |
| 64 | * @param bRegisterGlobal Register to global font manager | 是否注册到全局字体管理器 |
| 65 | */ |
| 66 | public async load( |
| 67 | name: string, |
| 68 | jsonUrl: string, |
| 69 | textureUrl: string, |
| 70 | bRegisterGlobal: boolean = true |
| 71 | ): Promise<IFontLoadResult> { |
| 72 | // Check cache |
| 73 | const cached = this._fontCache.get(name); |
| 74 | if (cached) { |
| 75 | return { |
| 76 | font: cached, |
| 77 | textureId: cached.textureId, |
| 78 | name |
| 79 | }; |
| 80 | } |
| 81 | |
| 82 | // Load JSON first |
| 83 | const fontData = await this.loadFontData(jsonUrl); |
| 84 | |
| 85 | // Load texture (synchronous API - returns ID immediately, loading happens async internally) |
| 86 | const textureId = this._textureService.loadTextureByPath(textureUrl); |
| 87 | |
| 88 | // Create font |
| 89 | const font = new MSDFFont(name, fontData); |
| 90 | font.textureId = textureId; |
| 91 | |
| 92 | // Cache |
| 93 | this._fontCache.set(name, font); |
| 94 | |
| 95 | // Register to global manager |
| 96 | if (bRegisterGlobal) { |
| 97 | const manager = getMSDFFontManager(); |
| 98 | manager.registerFont(font); |
| 99 | } |
| 100 | |
| 101 | return { font, textureId, name }; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Load font data from JSON URL |
| 106 | * 从 JSON URL 加载字体数据 |
nothing calls this directly
no outgoing calls
no test coverage detected