(function (global, factory) { if (typeof define === "function" && define.amd) { define(["exports", "three", "../libs/opentype.module.min.js"], factory); } else if (typeof exports !== "undefined") { factory(exports, require("three"), require("../libs/opentype.module.min.js")); } else { var mod = { exports: {} }; factory(mod.exports, global.three, global.opentypeModuleMin); global.TTFLoader = mod.exports; } })(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function (_exports, _three, _opentypeModuleMin) { "use strict"; Object.defineProperty(_exports, "__esModule", { value: true }); _exports.TTFLoader = void 0; function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } Object.defineProperty(subClass, "prototype", { value: Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }), writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } function _possibleConstructorReturn(self, call) { if (call && (typeof call === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } /** * Requires opentype.js to be included in the project. * Loads TTF files and converts them into typeface JSON that can be used directly * to create THREE.Font objects. */ var TTFLoader = /*#__PURE__*/function (_Loader) { _inherits(TTFLoader, _Loader); var _super = _createSuper(TTFLoader); function TTFLoader(manager) { var _this; _classCallCheck(this, TTFLoader); _this = _super.call(this, manager); _this.reversed = false; return _this; } _createClass(TTFLoader, [{ key: "load", value: function load(url, onLoad, onProgress, onError) { var scope = this; var loader = new _three.FileLoader(this.manager); loader.setPath(this.path); loader.setResponseType('arraybuffer'); loader.setRequestHeader(this.requestHeader); loader.setWithCredentials(this.withCredentials); loader.load(url, function (buffer) { try { onLoad(scope.parse(buffer)); } catch (e) { if (onError) { onError(e); } else { console.error(e); } scope.manager.itemError(url); } }, onProgress, onError); } }, { key: "parse", value: function parse(arraybuffer) { function convert(font, reversed) { var round = Math.round; var glyphs = {}; var scale = 100000 / ((font.unitsPerEm || 2048) * 72); var glyphIndexMap = font.encoding.cmap.glyphIndexMap; var unicodes = Object.keys(glyphIndexMap); for (var i = 0; i < unicodes.length; i++) { var unicode = unicodes[i]; var glyph = font.glyphs.glyphs[glyphIndexMap[unicode]]; if (unicode !== undefined) { (function () { var token = { ha: round(glyph.advanceWidth * scale), x_min: round(glyph.xMin * scale), x_max: round(glyph.xMax * scale), o: '' }; if (reversed) { glyph.path.commands = reverseCommands(glyph.path.commands); } glyph.path.commands.forEach(function (command) { if (command.type.toLowerCase() === 'c') { command.type = 'b'; } token.o += command.type.toLowerCase() + ' '; if (command.x !== undefined && command.y !== undefined) { token.o += round(command.x * scale) + ' ' + round(command.y * scale) + ' '; } if (command.x1 !== undefined && command.y1 !== undefined) { token.o += round(command.x1 * scale) + ' ' + round(command.y1 * scale) + ' '; } if (command.x2 !== undefined && command.y2 !== undefined) { token.o += round(command.x2 * scale) + ' ' + round(command.y2 * scale) + ' '; } }); glyphs[String.fromCodePoint(glyph.unicode)] = token; })(); } } return { glyphs: glyphs, familyName: font.getEnglishName('fullName'), ascender: round(font.ascender * scale), descender: round(font.descender * scale), underlinePosition: font.tables.post.underlinePosition, underlineThickness: font.tables.post.underlineThickness, boundingBox: { xMin: font.tables.head.xMin, xMax: font.tables.head.xMax, yMin: font.tables.head.yMin, yMax: font.tables.head.yMax }, resolution: 1000, original_font_information: font.tables.name }; } function reverseCommands(commands) { var paths = []; var path; commands.forEach(function (c) { if (c.type.toLowerCase() === 'm') { path = [c]; paths.push(path); } else if (c.type.toLowerCase() !== 'z') { path.push(c); } }); var reversed = []; paths.forEach(function (p) { var result = { type: 'm', x: p[p.length - 1].x, y: p[p.length - 1].y }; reversed.push(result); for (var i = p.length - 1; i > 0; i--) { var command = p[i]; var _result = { type: command.type }; if (command.x2 !== undefined && command.y2 !== undefined) { _result.x1 = command.x2; _result.y1 = command.y2; _result.x2 = command.x1; _result.y2 = command.y1; } else if (command.x1 !== undefined && command.y1 !== undefined) { _result.x1 = command.x1; _result.y1 = command.y1; } _result.x = p[i - 1].x; _result.y = p[i - 1].y; reversed.push(_result); } }); return reversed; } if (typeof _opentypeModuleMin.opentype === 'undefined') { console.warn('THREE.TTFLoader: The loader requires opentype.js. Make sure it\'s included before using the loader.'); return null; } return convert(_opentypeModuleMin.opentype.parse(arraybuffer), this.reversed); // eslint-disable-line no-undef } }]); return TTFLoader; }(_three.Loader); _exports.TTFLoader = TTFLoader; });