36 lines
2.0 KiB
TypeScript
36 lines
2.0 KiB
TypeScript
import { DETAIL_FONT_FAMILY } from './types'
|
|
|
|
/** The same unmodified font files are loaded by Fabric in the browser and Node worker. */
|
|
export const DETAIL_FONT_REGISTRY = [
|
|
{ family: DETAIL_FONT_FAMILY, weight: 400, postScriptName: 'NotoSansSC-Regular', filename: 'NotoSansSC-Regular.otf', url: '/detail-generator/fonts/NotoSansSC-Regular.otf' },
|
|
{ family: DETAIL_FONT_FAMILY, weight: 700, postScriptName: 'NotoSansSC-Bold', filename: 'NotoSansSC-Bold.otf', url: '/detail-generator/fonts/NotoSansSC-Bold.otf' },
|
|
] as const
|
|
|
|
let fontLoading: Promise<void> | null = null
|
|
|
|
export function ensureDetailFontsLoaded(): Promise<void> {
|
|
if (typeof window === 'undefined') return Promise.resolve()
|
|
if (!fontLoading) {
|
|
fontLoading = Promise.all(DETAIL_FONT_REGISTRY.map(async font => {
|
|
const face = new FontFace(font.family, `url("${font.url}")`, { weight: String(font.weight), style: 'normal' })
|
|
document.fonts.add(await face.load())
|
|
})).then(() => undefined).catch(error => {
|
|
fontLoading = null
|
|
throw error
|
|
})
|
|
}
|
|
return fontLoading
|
|
}
|
|
|
|
/** Center-crop geometry shared with the exporter's FabricImage placement. */
|
|
export function getDetailImageCoverGeometry(sourceWidth: number, sourceHeight: number, width: number, height: number) {
|
|
const scale = Math.max(width / sourceWidth, height / sourceHeight)
|
|
return { cropX: (sourceWidth - width / scale) / 2, cropY: (sourceHeight - height / scale) / 2, width: width / scale, height: height / scale, scaleX: scale, scaleY: scale }
|
|
}
|
|
|
|
/** Brand marks fit completely inside the frame instead of losing letters to cropping. */
|
|
export function getDetailImageContainGeometry(sourceWidth: number, sourceHeight: number, width: number, height: number) {
|
|
const scale = Math.min(width / sourceWidth, height / sourceHeight)
|
|
return { cropX: 0, cropY: 0, width: sourceWidth, height: sourceHeight, scaleX: scale, scaleY: scale, offsetX: (width - sourceWidth * scale) / 2, offsetY: (height - sourceHeight * scale) / 2 }
|
|
}
|