-
Notifications
You must be signed in to change notification settings - Fork 0
/
ninePatch.js
85 lines (84 loc) · 3.35 KB
/
ninePatch.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
var ninePatch = (function () {
var bin = {
readUint: function (buff, p) {
return (
buff[p] * (256 * 256 * 256) +
((buff[p + 1] << 16) | (buff[p + 2] << 8) | buff[p + 3])
);
},
readASCII: function (buff, p, l) {
var s = "";
for (var i = 0; i < l; i++) {
s += String.fromCharCode(buff[p + i]);
}
return s;
}
};
function decode(buff) {
var data = new Uint8Array(buff),
offset = 8;
var out = {};
var pngFlag = [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a];
for (var i = 0; i < 8; i++) {
if (data[i] != pngFlag[i]) throw "This is not a PNG file!";
}
while (offset < data.length) {
var len = bin.readUint(data, offset);
offset += 4;
var type = bin.readASCII(data, offset, 4);
offset += 4;
if(type=="IHDR") {
out.width = bin.readUint(data, offset);
out.height = bin.readUint(data, offset + 4);
} else if (type == "npTc") {
var npTcOffset = offset;
var keys = [
{ name: "wasDeserialized", byteLength: 1 },
{ name: "numXDivs", byteLength: 1 },
{ name: "numYDivs", byteLength: 1 },
{ name: "numColors", byteLength: 1 },
{ name: "xDivsOffset", byteLength: 4 },
{ name: "yDivsOffset", byteLength: 4 },
{ name: "paddingLeft", byteLength: 4 },
{ name: "paddingRight", byteLength: 4 },
{ name: "paddingTop", byteLength: 4 },
{ name: "paddingBottom", byteLength: 4 },
{ name: "colorsOffset", byteLength: 4 },
];
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
if (key.byteLength == 1) {
out[key.name] = data[npTcOffset];
} else if (key.byteLength == 4) {
out[key.name] = bin.readUint(data, npTcOffset);
}
npTcOffset += key.byteLength;
}
out.xDivs = [];
for (var i = 0; i < out.numXDivs; i++) {
out.xDivs.push(bin.readUint(data, npTcOffset));
npTcOffset += 4;
}
out.yDivs = [];
for (var i = 0; i < out.numYDivs; i++) {
out.yDivs.push(bin.readUint(data, npTcOffset));
npTcOffset += 4;
}
break;
} else if (type == "IEND") {
break;
}
offset += len;
// skip crc
offset += 4;
}
return out;
}
function generateCss(data) {
return `border-image-slice: ${data.yDivs[0]} ${data.width - data.xDivs[1]} ${data.height - data.yDivs[1]} ${data.xDivs[0]} fill; border-image-width: auto; padding: ${data.paddingTop}px ${data.paddingRight}px ${data.paddingBottom}px ${data.paddingLeft}px`
}
return {
decode: decode,
generateCss: generateCss
};
})();