-
Notifications
You must be signed in to change notification settings - Fork 0
/
scorecompression.js
170 lines (124 loc) · 4.48 KB
/
scorecompression.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
goog.provide('ScoreLibrary.Inflater');
goog.provide('ScoreLibrary.MusicXMLMIMETypes');
goog.require('ScoreLibrary');
goog.require('ScoreLibrary.Score.Node');
goog.require('ScoreLibrary.Score.XMLHelper');
/**
* @enum {string}
*/
ScoreLibrary.MusicXMLMIMETypes = {
MIME_MXL: 'application/vnd.recordare.musicxml',
MIME_XML: 'application/vnd.recordare.musicxml+xml'
};
/**
* @constructor
*/
ScoreLibrary.Inflater =
function(text, context, successCallback, errorCallback) {
this.context = context;
this.successCallback = successCallback;
this.errorCallback = errorCallback;
var inflater = this;
if (!zip.workerScriptsPath) {
throw Error('ScoreLibrary.Inflater(): ' +
'set zip.workerScriptsPath first!');
}
zip.createReader(
new zip.BlobReader(this.binaryStringToBlob(text)),
function(reader) {
inflater.callbackContents(reader);
},
function(error) {
inflater.errorCallback.call(
inflater.context, error);
}
);
};
ScoreLibrary.Inflater.prototype.binaryStringToBlob = function(text) {
var length = text.length;
var bytes = new Uint8Array(length);
for (var i = 0; i < length; ++i) {
bytes[i] = (text.charCodeAt(i) & 0xff);
}
var blob_builder = new zip.BlobBuilder();
blob_builder.append(bytes.buffer);
var mime_types = ScoreLibrary.MusicXMLMIMETypes;
return blob_builder.getBlob(mime_types.MIME_MXL);
};
ScoreLibrary.Inflater.prototype.callbackContents = function(reader) {
var inflater = this;
reader.getEntries(
function(entries) {
var container_entry = undefined;
if (entries.some(
function(entry) {
if (entry.filename ===
'META-INF/container.xml') {
container_entry = entry;
return true;
}
return false;
})) {
container_entry.getData(
new zip.TextWriter(),
function(text) {
inflater.callbackContainer(reader, entries, text);
});
}
});
};
ScoreLibrary.Inflater.prototype.callbackContainer =
function(reader, entries, text) {
var inflater = this;
var xml_helper =
new ScoreLibrary.Score.XMLHelper.getInstance();
var root_files =
xml_helper.getMXLContainer(
new ScoreLibrary.Score.Node(text)).root_files;
if (root_files &&
root_files.length > 0 &&
(root_files[0].media_type ===
ScoreLibrary.MusicXMLMIMETypes.MIME_XML ||
root_files[0].media_type === '') &&
root_files[0].full_path) {
var musicxml_path = root_files[0].full_path;
var musicxml_entry = undefined;
if (entries.some(
function(entry) {
if (entry.filename === musicxml_path) {
musicxml_entry = entry;
return true;
}
return false;
})) {
musicxml_entry.getData(
new zip.TextWriter(),
function(text) {
inflater.successCallback.call(
inflater.context, $.parseXML(text));
});
return;
}
}
this.errorCallback.call(
this.context,
'ScoreLibrary.Inflater.callbackContainer(): invalid contents!');
};
/**
* @author XiongWenjie <[email protected]>
* @license This file is part of
* score-library <http://www.musicxml-viewer.com>.
* score-library is free software:
* you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* score-library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with score-library.
* If not, see <http://www.gnu.org/licenses>.
*/