browser.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. var xhr = require('xhr')
  2. var noop = function(){}
  3. var parseASCII = require('parse-bmfont-ascii')
  4. var parseXML = require('parse-bmfont-xml')
  5. var readBinary = require('parse-bmfont-binary')
  6. var isBinaryFormat = require('./lib/is-binary')
  7. var xtend = require('xtend')
  8. var xml2 = (function hasXML2() {
  9. return self.XMLHttpRequest && "withCredentials" in new XMLHttpRequest
  10. })()
  11. module.exports = function(opt, cb) {
  12. cb = typeof cb === 'function' ? cb : noop
  13. if (typeof opt === 'string')
  14. opt = { uri: opt }
  15. else if (!opt)
  16. opt = {}
  17. var expectBinary = opt.binary
  18. if (expectBinary)
  19. opt = getBinaryOpts(opt)
  20. xhr(opt, function(err, res, body) {
  21. if (err)
  22. return cb(err)
  23. if (!/^2/.test(res.statusCode))
  24. return cb(new Error('http status code: '+res.statusCode))
  25. if (!body)
  26. return cb(new Error('no body result'))
  27. var binary = false
  28. //if the response type is an array buffer,
  29. //we need to convert it into a regular Buffer object
  30. if (isArrayBuffer(body)) {
  31. var array = new Uint8Array(body)
  32. body = Buffer.from(array, 'binary')
  33. }
  34. //now check the string/Buffer response
  35. //and see if it has a binary BMF header
  36. if (isBinaryFormat(body)) {
  37. binary = true
  38. //if we have a string, turn it into a Buffer
  39. if (typeof body === 'string')
  40. body = Buffer.from(body, 'binary')
  41. }
  42. //we are not parsing a binary format, just ASCII/XML/etc
  43. if (!binary) {
  44. //might still be a buffer if responseType is 'arraybuffer'
  45. if (Buffer.isBuffer(body))
  46. body = body.toString(opt.encoding)
  47. body = body.trim()
  48. }
  49. var result
  50. try {
  51. var type = res.headers['content-type']
  52. if (binary)
  53. result = readBinary(body)
  54. else if (/json/.test(type) || body.charAt(0) === '{')
  55. result = JSON.parse(body)
  56. else if (/xml/.test(type) || body.charAt(0) === '<')
  57. result = parseXML(body)
  58. else
  59. result = parseASCII(body)
  60. } catch (e) {
  61. cb(new Error('error parsing font '+e.message))
  62. cb = noop
  63. }
  64. cb(null, result)
  65. })
  66. }
  67. function isArrayBuffer(arr) {
  68. var str = Object.prototype.toString
  69. return str.call(arr) === '[object ArrayBuffer]'
  70. }
  71. function getBinaryOpts(opt) {
  72. //IE10+ and other modern browsers support array buffers
  73. if (xml2)
  74. return xtend(opt, { responseType: 'arraybuffer' })
  75. if (typeof self.XMLHttpRequest === 'undefined')
  76. throw new Error('your browser does not support XHR loading')
  77. //IE9 and XML1 browsers could still use an override
  78. var req = new self.XMLHttpRequest()
  79. req.overrideMimeType('text/plain; charset=x-user-defined')
  80. return xtend({
  81. xhr: req
  82. }, opt)
  83. }