-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
this will soon be exposed for (1) configuring how paint fonts are registered (to support skia-canvas etc) and (2) configuring how fonts are loaded into a buffer, since that's kind of messed up right now and (3) to merge setWasmLocator into
- Loading branch information
Showing
8 changed files
with
58 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import {environment, defaultEnvironment} from './environment.js'; | ||
|
||
// TypeScript does not support different possibilities of runtime environments, | ||
// so the types loaded are for node. To add the browser environment too would | ||
// add too many globals. | ||
// | ||
// https://gist.github.com/RyanCavanaugh/702ebd1ca2fc060e58e634b4e30c1c1c | ||
declare const document: any; | ||
declare const FontFace: any; | ||
|
||
if (environment.registerFont === defaultEnvironment.registerFont) { | ||
environment.registerFont = function (match, buffer, url) { | ||
const descriptor = match.toCssDescriptor(); | ||
const face = new FontFace(descriptor.family, buffer, descriptor); | ||
document.fonts.add(face); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import {environment, defaultEnvironment} from './environment.js'; | ||
import {fileURLToPath} from 'url'; | ||
|
||
const alreadyRegistered = new Set<string>(); | ||
|
||
let canvas: typeof import('canvas') | undefined; | ||
|
||
try { | ||
canvas = await import('canvas'); | ||
} catch (e) { | ||
} | ||
|
||
if (canvas?.registerFont && environment.registerFont === defaultEnvironment.registerFont) { | ||
environment.registerFont = (match, buffer, url) => { | ||
const filename = fileURLToPath(url); | ||
if (canvas?.registerFont && !alreadyRegistered.has(filename)) { | ||
const descriptor = match.toCssDescriptor(); | ||
canvas.registerFont(filename, descriptor); | ||
alreadyRegistered.add(filename); | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import type {FaceMatch} from './text-font.js'; | ||
|
||
export interface Environment { | ||
registerFont(match: FaceMatch, buffer: Uint8Array, url: URL): void; | ||
} | ||
|
||
export const defaultEnvironment: Environment = { | ||
registerFont() { | ||
throw new Error('Invalid build! Your bundler needs to support "exports" in package.json.'); | ||
} | ||
}; | ||
|
||
export const environment = {...defaultEnvironment}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters