Skip to content

Commit

Permalink
cleanup assets PR
Browse files Browse the repository at this point in the history
  • Loading branch information
mggower committed Nov 8, 2023
1 parent 94fc274 commit 4c6ebb1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 25 deletions.
34 changes: 15 additions & 19 deletions src/renderers/webgl/objects/icon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,24 @@ 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,
imageIconTexture: ImageIconTexture,
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)
}
}
Expand All @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
17 changes: 11 additions & 6 deletions src/renderers/webgl/textures/font.ts
Original file line number Diff line number Diff line change
@@ -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)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -94,6 +98,7 @@ export class FontBook {
delete(fontName?: string) {
if (fontName === undefined) {
this.cache = {}
this.loading = {}
} else {
FontBook.find(fontName)?.destroy()
}
Expand Down

0 comments on commit 4c6ebb1

Please sign in to comment.