Skip to content

Commit

Permalink
remove loaders from FontBook
Browse files Browse the repository at this point in the history
  • Loading branch information
mggower committed Feb 6, 2024
1 parent a038c64 commit fb086f0
Showing 1 changed file with 2 additions and 63 deletions.
65 changes: 2 additions & 63 deletions src/renderers/webgl/textures/font.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,5 @@
import { BitmapFont, TextStyle } from 'pixi.js'
import { MIN_ZOOM } from '../utils'
import { throttle } from '../../../utils'
import FontFaceObserver from 'fontfaceobserver'

const warn = throttle((err) => console.warn(err), 0)

const GENERIC_FONT_FAMILIES = new Set([
'serif',
'sans-serif',
'monospace',
'cursive',
'fantasy',
'system-ui',
'emoji',
'math',
'fangsong',
'ui-serif',
'ui-sans-serif',
'ui-monospace'
])

export class FontBook {
static defaultResolution = 2
Expand All @@ -31,9 +12,6 @@ export class FontBook {
scaleFactor: number
chars: string | (string | string[])[]

private cache: { [family: string]: boolean } = {}
private loading: { [family: string]: Promise<void> } = {}

constructor(
resolution = FontBook.defaultResolution,
maxFontSize = FontBook.defaultMaxFontSize,
Expand All @@ -50,40 +28,6 @@ export class FontBook {
return BitmapFont.available[fontName]
}

available(fontFamily: string | undefined, fontWeight: string | number | undefined = 'normal') {
const family = fontFamily?.split(', ')[0]
return (
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) {
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
const font = new FontFaceObserver(family, { weight })
this.loading[family] = font.load(null, timeout)
}

await this.loading[family]
this.cache[family] = true
} catch (error) {
warn(error)
return false
}

return this.cache[family]
}

create(fontName: string, style: TextStyle) {
const font = FontBook.find(fontName)

Expand All @@ -95,12 +39,7 @@ export class FontBook {
return font
}

delete(fontName?: string) {
if (fontName === undefined) {
this.cache = {}
this.loading = {}
} else {
FontBook.find(fontName)?.destroy()
}
delete(fontName: string) {
FontBook.find(fontName)?.destroy()
}
}

0 comments on commit fb086f0

Please sign in to comment.