(p5, fn)
| 1260 | } |
| 1261 | |
| 1262 | function font(p5, fn) { |
| 1263 | |
| 1264 | /** |
| 1265 | * A class to describe fonts. Create through <a href="#/p5/loadFont">`loadFont()`</a>. |
| 1266 | * |
| 1267 | * @class p5.Font |
| 1268 | */ |
| 1269 | p5.Font = Font; |
| 1270 | |
| 1271 | /** |
| 1272 | * @private |
| 1273 | */ |
| 1274 | fn.parseFontData = async function(pathOrData) { |
| 1275 | // load the raw font bytes |
| 1276 | let result = pathOrData instanceof Uint8Array |
| 1277 | ? pathOrData |
| 1278 | : await fn.loadBytes(pathOrData); |
| 1279 | //console.log('result:', result); |
| 1280 | |
| 1281 | if (!result) { |
| 1282 | throw Error('Failed to load font data'); |
| 1283 | } |
| 1284 | |
| 1285 | // parse the font data |
| 1286 | let fonts = Typr.parse(result); |
| 1287 | |
| 1288 | // TODO: generate descriptors from font in the future |
| 1289 | |
| 1290 | if (fonts.length === 0 || fonts[0].cmap === undefined) { |
| 1291 | throw Error('parsing font data'); |
| 1292 | } |
| 1293 | |
| 1294 | return fonts[0]; |
| 1295 | }; |
| 1296 | |
| 1297 | /** |
| 1298 | * Loads a font and creates a <a href="#/p5.Font">p5.Font</a> object. |
| 1299 | * `loadFont()` can load fonts in either .otf or .ttf format. Loaded fonts can |
| 1300 | * be used to style text on the canvas and in HTML elements. |
| 1301 | * |
| 1302 | * The first parameter, `path`, is the path to a font file. |
| 1303 | * Paths to local files should be relative. For example, |
| 1304 | * `'assets/inconsolata.otf'`. The Inconsolata font used in the following |
| 1305 | * examples can be downloaded for free |
| 1306 | * <a href="https://www.fontsquirrel.com/fonts/inconsolata" target="_blank">here</a>. |
| 1307 | * Paths to remote files should be URLs. For example, |
| 1308 | * `'https://example.com/inconsolata.otf'`. URLs may be blocked due to browser |
| 1309 | * security. |
| 1310 | * |
| 1311 | * In 2D mode, `path` can take on a few other forms. It could be a path to a CSS file, |
| 1312 | * such as one from <a href="https://fonts.google.com/">Google Fonts.</a> It could also |
| 1313 | * be a string with a <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/@font-face">CSS `@font-face` declaration.</a> |
| 1314 | * |
| 1315 | * The second parameter, `successCallback`, is optional. If a function is |
| 1316 | * passed, it will be called once the font has loaded. The callback function |
| 1317 | * may use the new <a href="#/p5.Font">p5.Font</a> object if needed. |
| 1318 | * |
| 1319 | * The third parameter, `failureCallback`, is also optional. If a function is |
no test coverage detected