|
| 1 | + |
| 2 | +var Module = typeof Module !== 'undefined' ? Module : {}; |
| 3 | + |
| 4 | +if (!Module.expectedDataFileDownloads) { |
| 5 | + Module.expectedDataFileDownloads = 0; |
| 6 | + Module.finishedDataFileDownloads = 0; |
| 7 | +} |
| 8 | +Module.expectedDataFileDownloads++; |
| 9 | +(function() { |
| 10 | + var loadPackage = function(metadata) { |
| 11 | + |
| 12 | + var PACKAGE_PATH; |
| 13 | + if (typeof window === 'object') { |
| 14 | + PACKAGE_PATH = window['encodeURIComponent'](window.location.pathname.toString().substring(0, window.location.pathname.toString().lastIndexOf('/')) + '/'); |
| 15 | + } else if (typeof location !== 'undefined') { |
| 16 | + // worker |
| 17 | + PACKAGE_PATH = encodeURIComponent(location.pathname.toString().substring(0, location.pathname.toString().lastIndexOf('/')) + '/'); |
| 18 | + } else { |
| 19 | + throw 'using preloaded data can only be done on a web page or in a web worker'; |
| 20 | + } |
| 21 | + var PACKAGE_NAME = 'fonts.data'; |
| 22 | + var REMOTE_PACKAGE_BASE = 'fonts.data'; |
| 23 | + if (typeof Module['locateFilePackage'] === 'function' && !Module['locateFile']) { |
| 24 | + Module['locateFile'] = Module['locateFilePackage']; |
| 25 | + err('warning: you defined Module.locateFilePackage, that has been renamed to Module.locateFile (using your locateFilePackage for now)'); |
| 26 | + } |
| 27 | + var REMOTE_PACKAGE_NAME = Module['locateFile'] ? Module['locateFile'](REMOTE_PACKAGE_BASE, '') : REMOTE_PACKAGE_BASE; |
| 28 | + |
| 29 | + var REMOTE_PACKAGE_SIZE = metadata.remote_package_size; |
| 30 | + var PACKAGE_UUID = metadata.package_uuid; |
| 31 | + |
| 32 | + function fetchRemotePackage(packageName, packageSize, callback, errback) { |
| 33 | + var xhr = new XMLHttpRequest(); |
| 34 | + xhr.open('GET', packageName, true); |
| 35 | + xhr.responseType = 'arraybuffer'; |
| 36 | + xhr.onprogress = function(event) { |
| 37 | + var url = packageName; |
| 38 | + var size = packageSize; |
| 39 | + if (event.total) size = event.total; |
| 40 | + if (event.loaded) { |
| 41 | + if (!xhr.addedTotal) { |
| 42 | + xhr.addedTotal = true; |
| 43 | + if (!Module.dataFileDownloads) Module.dataFileDownloads = {}; |
| 44 | + Module.dataFileDownloads[url] = { |
| 45 | + loaded: event.loaded, |
| 46 | + total: size |
| 47 | + }; |
| 48 | + } else { |
| 49 | + Module.dataFileDownloads[url].loaded = event.loaded; |
| 50 | + } |
| 51 | + var total = 0; |
| 52 | + var loaded = 0; |
| 53 | + var num = 0; |
| 54 | + for (var download in Module.dataFileDownloads) { |
| 55 | + var data = Module.dataFileDownloads[download]; |
| 56 | + total += data.total; |
| 57 | + loaded += data.loaded; |
| 58 | + num++; |
| 59 | + } |
| 60 | + total = Math.ceil(total * Module.expectedDataFileDownloads/num); |
| 61 | + if (Module['setStatus']) Module['setStatus']('Downloading data... (' + loaded + '/' + total + ')'); |
| 62 | + } else if (!Module.dataFileDownloads) { |
| 63 | + if (Module['setStatus']) Module['setStatus']('Downloading data...'); |
| 64 | + } |
| 65 | + }; |
| 66 | + xhr.onerror = function(event) { |
| 67 | + throw new Error("NetworkError for: " + packageName); |
| 68 | + } |
| 69 | + xhr.onload = function(event) { |
| 70 | + if (xhr.status == 200 || xhr.status == 304 || xhr.status == 206 || (xhr.status == 0 && xhr.response)) { // file URLs can return 0 |
| 71 | + var packageData = xhr.response; |
| 72 | + callback(packageData); |
| 73 | + } else { |
| 74 | + throw new Error(xhr.statusText + " : " + xhr.responseURL); |
| 75 | + } |
| 76 | + }; |
| 77 | + xhr.send(null); |
| 78 | + }; |
| 79 | + |
| 80 | + function handleError(error) { |
| 81 | + console.error('package error:', error); |
| 82 | + }; |
| 83 | + |
| 84 | + var fetchedCallback = null; |
| 85 | + var fetched = Module['getPreloadedPackage'] ? Module['getPreloadedPackage'](REMOTE_PACKAGE_NAME, REMOTE_PACKAGE_SIZE) : null; |
| 86 | + |
| 87 | + if (!fetched) fetchRemotePackage(REMOTE_PACKAGE_NAME, REMOTE_PACKAGE_SIZE, function(data) { |
| 88 | + if (fetchedCallback) { |
| 89 | + fetchedCallback(data); |
| 90 | + fetchedCallback = null; |
| 91 | + } else { |
| 92 | + fetched = data; |
| 93 | + } |
| 94 | + }, handleError); |
| 95 | + |
| 96 | + function runWithFS() { |
| 97 | + |
| 98 | + function assert(check, msg) { |
| 99 | + if (!check) throw msg + new Error().stack; |
| 100 | + } |
| 101 | +Module['FS_createPath']('/', 'resources', true, true); |
| 102 | +Module['FS_createPath']('/resources', 'fonts', true, true); |
| 103 | + |
| 104 | + function DataRequest(start, end, audio) { |
| 105 | + this.start = start; |
| 106 | + this.end = end; |
| 107 | + this.audio = audio; |
| 108 | + } |
| 109 | + DataRequest.prototype = { |
| 110 | + requests: {}, |
| 111 | + open: function(mode, name) { |
| 112 | + this.name = name; |
| 113 | + this.requests[name] = this; |
| 114 | + Module['addRunDependency']('fp ' + this.name); |
| 115 | + }, |
| 116 | + send: function() {}, |
| 117 | + onload: function() { |
| 118 | + var byteArray = this.byteArray.subarray(this.start, this.end); |
| 119 | + this.finish(byteArray); |
| 120 | + }, |
| 121 | + finish: function(byteArray) { |
| 122 | + var that = this; |
| 123 | + |
| 124 | + Module['FS_createDataFile'](this.name, null, byteArray, true, true, true); // canOwn this data in the filesystem, it is a slide into the heap that will never change |
| 125 | + Module['removeRunDependency']('fp ' + that.name); |
| 126 | + |
| 127 | + this.requests[this.name] = null; |
| 128 | + } |
| 129 | + }; |
| 130 | + |
| 131 | + var files = metadata.files; |
| 132 | + for (var i = 0; i < files.length; ++i) { |
| 133 | + new DataRequest(files[i].start, files[i].end, files[i].audio).open('GET', files[i].filename); |
| 134 | + } |
| 135 | + |
| 136 | + |
| 137 | + function processPackageData(arrayBuffer) { |
| 138 | + Module.finishedDataFileDownloads++; |
| 139 | + assert(arrayBuffer, 'Loading data file failed.'); |
| 140 | + assert(arrayBuffer instanceof ArrayBuffer, 'bad input to processPackageData'); |
| 141 | + var byteArray = new Uint8Array(arrayBuffer); |
| 142 | + var curr; |
| 143 | + |
| 144 | + // copy the entire loaded file into a spot in the heap. Files will refer to slices in that. They cannot be freed though |
| 145 | + // (we may be allocating before malloc is ready, during startup). |
| 146 | + var ptr = Module['getMemory'](byteArray.length); |
| 147 | + Module['HEAPU8'].set(byteArray, ptr); |
| 148 | + DataRequest.prototype.byteArray = Module['HEAPU8'].subarray(ptr, ptr+byteArray.length); |
| 149 | + |
| 150 | + var files = metadata.files; |
| 151 | + for (var i = 0; i < files.length; ++i) { |
| 152 | + DataRequest.prototype.requests[files[i].filename].onload(); |
| 153 | + } |
| 154 | + Module['removeRunDependency']('datafile_fonts.data'); |
| 155 | + |
| 156 | + }; |
| 157 | + Module['addRunDependency']('datafile_fonts.data'); |
| 158 | + |
| 159 | + if (!Module.preloadResults) Module.preloadResults = {}; |
| 160 | + |
| 161 | + Module.preloadResults[PACKAGE_NAME] = {fromCache: false}; |
| 162 | + if (fetched) { |
| 163 | + processPackageData(fetched); |
| 164 | + fetched = null; |
| 165 | + } else { |
| 166 | + fetchedCallback = processPackageData; |
| 167 | + } |
| 168 | + |
| 169 | + } |
| 170 | + if (Module['calledRun']) { |
| 171 | + runWithFS(); |
| 172 | + } else { |
| 173 | + if (!Module['preRun']) Module['preRun'] = []; |
| 174 | + Module["preRun"].push(runWithFS); // FS is not initialized yet, wait for it |
| 175 | + } |
| 176 | + |
| 177 | + } |
| 178 | + loadPackage({"files": [{"start": 0, "audio": 0, "end": 263712, "filename": "/resources/fonts/Montserrat-LightItalic.ttf"}, {"start": 263712, "audio": 0, "end": 409060, "filename": "/resources/fonts/Roboto-Regular.ttf"}], "remote_package_size": 409060, "package_uuid": "fc658031-6cb7-4646-a461-27bdfbe64e8c"}); |
| 179 | + |
| 180 | +})(); |
0 commit comments