From 4c6ebb11198e7987a415375691aa2af27112f030 Mon Sep 17 00:00:00 2001 From: Mikey Gower Date: Wed, 8 Nov 2023 10:32:02 -0500 Subject: [PATCH] cleanup assets PR --- src/renderers/webgl/objects/icon.ts | 34 ++++++++++++---------------- src/renderers/webgl/textures/font.ts | 17 +++++++++----- 2 files changed, 26 insertions(+), 25 deletions(-) diff --git a/src/renderers/webgl/objects/icon.ts b/src/renderers/webgl/objects/icon.ts index 5c54a5dc..b45044f1 100644 --- a/src/renderers/webgl/objects/icon.ts +++ b/src/renderers/webgl/objects/icon.ts @@ -18,6 +18,14 @@ export class Icon { private style: NodeIcon private icon: Sprite + private static async createTexture(style: NodeIcon, imageIconTexture: ImageIconTexture, textIconTexture: TextIconTexture) { + if (style.type === 'imageIcon') { + return await imageIconTexture.create(style) + } else { + return await textIconTexture.create(style) + } + } + static async init( container: Container, textIconTexture: TextIconTexture, @@ -25,15 +33,9 @@ export class Icon { nodeFill: NodeFill, style: NodeIcon ) { - let texture: Texture | RenderTexture | null + const texture = await Icon.createTexture(style, imageIconTexture, textIconTexture) - if (style.type === 'imageIcon') { - texture = await imageIconTexture.create(style) - } else { - texture = await textIconTexture.create(style) - } - - if (texture !== null) { + if (texture) { return new Icon(texture, container, textIconTexture, imageIconTexture, nodeFill, style) } } @@ -57,19 +59,13 @@ export class Icon { async update(style: NodeIcon) { if (!Trellis.equals(this.style, style)) { - let texture: Texture | RenderTexture | null - if (style.type === 'imageIcon') { - texture = await this.imageIconTexture.create(style) - } else { - texture = await this.textIconTexture.create(style) - } + const texture = await Icon.createTexture(style, this.imageIconTexture, this.textIconTexture) - if (texture !== null) { - this.texture = texture + if (texture) { const isMounted = this.mounted - this.delete() this.style = style + this.texture = texture this.icon = this.create() if (isMounted) { @@ -125,8 +121,8 @@ export class Icon { private create() { const icon = new Sprite(this.texture) - const scale = this.style.type === 'imageIcon' ? this.style.scale : 1 / this.textIconTexture.scaleFactor - icon.scale.set(scale ?? 1) + const scale = this.style.type === 'imageIcon' ? this.style.scale ?? 1 : 1 / this.textIconTexture.scaleFactor + icon.scale.set(scale) icon.anchor.set(0.5) icon.x = this.x ?? 0 icon.y = this.y ?? 0 diff --git a/src/renderers/webgl/textures/font.ts b/src/renderers/webgl/textures/font.ts index b550a3d8..44cb8f55 100644 --- a/src/renderers/webgl/textures/font.ts +++ b/src/renderers/webgl/textures/font.ts @@ -1,8 +1,7 @@ -/* eslint-disable no-console */ import { BitmapFont, TextStyle } from 'pixi.js' import { MIN_ZOOM } from '../utils' -import FontFaceObserver from 'fontfaceobserver' import { throttle } from '../../../utils' +import FontFaceObserver from 'fontfaceobserver' const warn = throttle((err) => console.warn(err), 0) @@ -51,18 +50,23 @@ export class FontBook { return BitmapFont.available[fontName] } - available(fontFamily: string, fontWeight: string | number | undefined = 'normal') { + available(fontFamily: string | undefined, fontWeight: string | number | undefined = 'normal') { + const family = fontFamily?.split(', ')[0] return ( - this.cache[fontFamily] === true || GENERIC_FONT_FAMILIES.has(fontFamily) || document.fonts.check(`${fontWeight} 1em ${fontFamily}`) + family === undefined || + this.cache[family] === true || + GENERIC_FONT_FAMILIES.has(family) || + document.fonts.check(`${fontWeight} 1em ${family}`) ) } async load(fontFamily: string | undefined, fontWeight: string | number | undefined = 'normal', timeout?: number) { - const family = fontFamily?.split(', ')[0] - if (family === undefined || this.available(family, fontWeight)) { + if (fontFamily === undefined || this.available(fontFamily, fontWeight)) { return true } + const family = fontFamily.split(', ')[0] + try { if (!this.loading[family]) { const weight = typeof fontWeight === 'string' && !isNaN(+fontWeight) ? +fontWeight : fontWeight @@ -94,6 +98,7 @@ export class FontBook { delete(fontName?: string) { if (fontName === undefined) { this.cache = {} + this.loading = {} } else { FontBook.find(fontName)?.destroy() }