@@ -33,53 +33,12 @@ import _name from './tables/name.js';
3333import os2 from './tables/os2.js' ;
3434import post from './tables/post.js' ;
3535import meta from './tables/meta.js' ;
36- import { readFile , readFileSync } from 'fs' ;
36+
3737/**
3838 * The opentype library.
3939 * @namespace opentype
4040 */
4141
42- // File loaders /////////////////////////////////////////////////////////
43- /**
44- * Loads a font from a file. The callback throws an error message as the first parameter if it fails
45- * and the font as an ArrayBuffer in the second parameter if it succeeds.
46- * @param {string } path - The path of the file
47- * @param {Function } callback - The function to call when the font load completes
48- */
49- function loadFromFile ( path , callback ) {
50- readFile ( path , function ( err , buffer ) {
51- if ( err ) {
52- return callback ( err . message ) ;
53- }
54-
55- callback ( null , buffer ) ;
56- } ) ;
57- }
58-
59- /**
60- * Loads a font from a URL. The callback throws an error message as the first parameter if it fails
61- * and the font as an ArrayBuffer in the second parameter if it succeeds.
62- * @param {string } url - The URL of the font file.
63- * @param {Function } callback - The function to call when the font load completes
64- */
65- function loadFromUrl ( url , callback ) {
66- const request = new XMLHttpRequest ( ) ;
67- request . open ( 'get' , url , true ) ;
68- request . responseType = 'arraybuffer' ;
69- request . onload = function ( ) {
70- if ( request . response ) {
71- return callback ( null , request . response ) ;
72- } else {
73- return callback ( 'Font could not be loaded: ' + request . statusText ) ;
74- }
75- } ;
76-
77- request . onerror = function ( ) {
78- callback ( 'Font could not be loaded' ) ;
79- } ;
80-
81- request . send ( ) ;
82- }
8342
8443// Table Directory Entries //////////////////////////////////////////////
8544/**
@@ -420,66 +379,11 @@ function parseBuffer(buffer, opt={}) {
420379 return font ;
421380}
422381
423- /**
424- * Asynchronously load the font from a URL or a filesystem. When done, call the callback
425- * with two arguments `(err, font)`. The `err` will be null on success,
426- * the `font` is a Font object.
427- * We use the node.js callback convention so that
428- * opentype.js can integrate with frameworks like async.js.
429- * @alias opentype.load
430- * @param {string } url - The URL of the font to load.
431- * @param {Function } callback - The callback.
432- */
433- function load ( url , callback , opt = { } ) {
434- const isNode = typeof window === 'undefined' ;
435- const loadFn = isNode && ! opt . isUrl ? loadFromFile : loadFromUrl ;
436- return new Promise ( ( resolve , reject ) => {
437- loadFn ( url , function ( err , buffer ) {
438- if ( err ) {
439- if ( callback ) {
440- return callback ( err ) ;
441- } else {
442- reject ( err ) ;
443- }
444- }
445- let font ;
446- try {
447- font = parseBuffer ( buffer , opt ) ;
448- } catch ( e ) {
449- if ( callback ) {
450- return callback ( e , null ) ;
451- } else {
452- reject ( e ) ;
453- }
454- }
455- if ( callback ) {
456- return callback ( null , font ) ;
457- } else {
458- resolve ( font ) ;
459- }
460- } ) ;
461- } ) ;
462- }
463-
464- /**
465- * Synchronously load the font from a URL or file.
466- * When done, returns the font object or throws an error.
467- * @alias opentype.loadSync
468- * @param {string } url - The URL of the font to load.
469- * @param {Object } opt - opt.lowMemory
470- * @return {opentype.Font }
471- */
472- function loadSync ( url , opt ) {
473- return parseBuffer ( readFileSync ( url ) , opt ) ;
474- }
475-
476382export {
477383 Font ,
478384 Glyph ,
479385 Path ,
480386 BoundingBox ,
481387 parse as _parse ,
482388 parseBuffer as parse ,
483- load ,
484- loadSync
485389} ;
0 commit comments