Commit 7210efa3 authored by Oleg.Korshul's avatar Oleg.Korshul Committed by Alexander.Trofimov

ZBase32 encode font names

git-svn-id: svn://192.168.3.15/activex/AVS/Sources/TeamlabOffice/trunk/OfficeWeb@59904 954022d7-b5bf-4e40-9824-e11837661b57
parent 4f41820e
"use strict";
function ZBase32Encoder()
{
this.EncodingTable = "ybndrfg8ejkmcpqxot1uwisza345h769";
this.DecodingTable = (undefined == typeof Uint8Array) ? new Array(128) : new Uint8Array(128);
var ii = 0;
for (ii = 0; ii < 128; ii++)
this.DecodingTable[ii] = 255;
var _len_32 = this.EncodingTable.length;
for (ii = 0; ii < _len_32; ii++)
{
this.DecodingTable[this.EncodingTable.charCodeAt(ii)] = ii;
}
this.GetUTF16_fromUnicodeChar = function(code)
{
if (code < 0x10000)
return String.fromCharCode(code);
else
{
code -= 0x10000;
return String.fromCharCode(0xD800 | ((code >> 10) & 0x03FF)) + String.fromCharCode(0xDC00 | (code & 0x03FF));
}
};
this.GetUTF16_fromUTF8 = function(pBuffer)
{
var _res = "";
var lIndex = 0;
var lCount = pBuffer.length;
var val = 0;
while (lIndex < lCount)
{
var byteMain = pBuffer[lIndex];
if (0x00 == (byteMain & 0x80))
{
// 1 byte
_res += this.GetUTF16_fromUnicodeChar(byteMain);
++lIndex;
}
else if (0x00 == (byteMain & 0x20))
{
// 2 byte
val = (((byteMain & 0x1F) << 6) |
(pBuffer[lIndex + 1] & 0x3F));
_res += this.GetUTF16_fromUnicodeChar(val);
lIndex += 2;
}
else if (0x00 == (byteMain & 0x10))
{
// 3 byte
val = (((byteMain & 0x0F) << 12) |
((pBuffer[lIndex + 1] & 0x3F) << 6) |
(pBuffer[lIndex + 2] & 0x3F));
_res += this.GetUTF16_fromUnicodeChar(val);
lIndex += 3;
}
else if (0x00 == (byteMain & 0x08))
{
// 4 byte
val = (((byteMain & 0x07) << 18) |
((pBuffer[lIndex + 1] & 0x3F) << 12) |
((pBuffer[lIndex + 2] & 0x3F) << 6) |
(pBuffer[lIndex + 3] & 0x3F));
_res += this.GetUTF16_fromUnicodeChar(val);
lIndex += 4;
}
else if (0x00 == (byteMain & 0x04))
{
// 5 byte
val = (((byteMain & 0x03) << 24) |
((pBuffer[lIndex + 1] & 0x3F) << 18) |
((pBuffer[lIndex + 2] & 0x3F) << 12) |
((pBuffer[lIndex + 3] & 0x3F) << 6) |
(pBuffer[lIndex + 4] & 0x3F));
_res += this.GetUTF16_fromUnicodeChar(val);
lIndex += 5;
}
else
{
// 6 byte
val = (((byteMain & 0x01) << 30) |
((pBuffer[lIndex + 1] & 0x3F) << 24) |
((pBuffer[lIndex + 2] & 0x3F) << 18) |
((pBuffer[lIndex + 3] & 0x3F) << 12) |
((pBuffer[lIndex + 4] & 0x3F) << 6) |
(pBuffer[lIndex + 5] & 0x3F));
_res += this.GetUTF16_fromUnicodeChar(val);
lIndex += 5;
}
}
return _res;
};
this.GetUTF8_fromUTF16 = function(sData)
{
var pCur = 0;
var pEnd = sData.length;
var result = [];
while (pCur < pEnd)
{
var code = sData.charCodeAt(pCur++);
if (code >= 0xD800 && code <= 0xDFFF && pCur < pEnd)
{
code = 0x10000 + (((code & 0x3FF) << 10) | (0x03FF & sData.charCodeAt(pCur++)));
}
if (code < 0x80)
{
result.push(code);
}
else if (code < 0x0800)
{
result.push(0xC0 | (code >> 6));
result.push(0x80 | (code & 0x3F));
}
else if (code < 0x10000)
{
result.push(0xE0 | (code >> 12));
result.push(0x80 | ((code >> 6) & 0x3F));
result.push(0x80 | (code & 0x3F));
}
else if (code < 0x1FFFFF)
{
result.push(0xF0 | (code >> 18));
result.push(0x80 | ((code >> 12) & 0x3F));
result.push(0x80 | ((code >> 6) & 0x3F));
result.push(0x80 | (code & 0x3F));
}
else if (code < 0x3FFFFFF)
{
result.push(0xF8 | (code >> 24));
result.push(0x80 | ((code >> 18) & 0x3F));
result.push(0x80 | ((code >> 12) & 0x3F));
result.push(0x80 | ((code >> 6) & 0x3F));
result.push(0x80 | (code & 0x3F));
}
else if (code < 0x7FFFFFFF)
{
result.push(0xFC | (code >> 30));
result.push(0x80 | ((code >> 24) & 0x3F));
result.push(0x80 | ((code >> 18) & 0x3F));
result.push(0x80 | ((code >> 12) & 0x3F));
result.push(0x80 | ((code >> 6) & 0x3F));
result.push(0x80 | (code & 0x3F));
}
}
return result;
};
this.Encode = function(sData)
{
var data = this.GetUTF8_fromUTF16(sData);
var encodedResult = "";
var len = data.length;
for (var i = 0; i < len; i += 5)
{
var byteCount = Math.min(5, len - i);
var buffer = 0;
for (var j = 0; j < byteCount; ++j)
{
buffer *= 256;
buffer += data[i + j];
}
var bitCount = byteCount * 8;
while (bitCount > 0)
{
var index = 0;
if (bitCount >= 5)
{
var _del = Math.pow(2, bitCount - 5);
//var _del = 1 << (bitCount - 5);
index = (buffer / _del) & 0x1f;
}
else
{
index = (buffer & (0x1f >> (5 - bitCount)));
index <<= (5 - bitCount);
}
encodedResult += this.EncodingTable.charAt(index);
bitCount -= 5;
}
}
return encodedResult;
};
this.Decode = function(data)
{
var result = [];
var _len = data.length;
var obj = { data: data, index : new Array(8) };
var cur = 0;
while (cur < _len)
{
cur = this.CreateIndexByOctetAndMovePosition(obj, cur);
var shortByteCount = 0;
var buffer = 0;
for (var j = 0; j < 8 && obj.index[j] != -1; ++j)
{
buffer *= 32;
buffer += (this.DecodingTable[obj.index[j]] & 0x1f);
shortByteCount++;
}
var bitCount = shortByteCount * 5;
while (bitCount >= 8)
{
//var _del = 1 << (bitCount - 8);
var _del = Math.pow(2, bitCount - 8);
var _res = (buffer / _del) & 0xff;
result.push(_res);
bitCount -= 8;
}
}
this.GetUTF16_fromUTF8(result);
};
this.CreateIndexByOctetAndMovePosition = function(obj, currentPosition)
{
var j = 0;
while (j < 8)
{
if (currentPosition >= obj.data.length)
{
obj.index[j++] = -1;
continue;
}
if (this.IgnoredSymbol(obj.data.charCodeAt(currentPosition)))
{
currentPosition++;
continue;
}
obj.index[j] = obj.data[currentPosition];
j++;
currentPosition++;
}
return currentPosition;
};
this.IgnoredSymbol = function(checkedSymbol)
{
return (checkedSymbol >= 128 || this.DecodingTable[checkedSymbol] == 255);
};
}
/** @define {boolean} */
var ASC_DOCS_API_USE_FONTS_ORIGINAL_FORMAT = false;
......@@ -119,7 +385,14 @@ function CFontFileLoader(id)
scriptElem.onload = scriptElem.onerror = oThis._callback_font_load;
if (this.IsNeedAddJSToFontPath)
scriptElem.setAttribute('src', basePath + "js/" + this.Id + ".js");
{
if (!window.g_fontNamesEncoder)
window.g_fontNamesEncoder = new ZBase32Encoder();
//var _name = this.Id + ".js";
var _name = window.g_fontNamesEncoder.Encode(this.Id + ".js") + ".js";
scriptElem.setAttribute('src', basePath + "js/" + _name);
}
else
scriptElem.setAttribute('src', basePath + this.Id + ".js");
scriptElem.setAttribute('type','text/javascript');
......@@ -159,7 +432,14 @@ function CFontFileLoader(id)
this.Status = 2;
var xhr = new XMLHttpRequest();
xhr.open('GET', basePath + "native/" + this.Id, true); // TODO:
if (!window.g_fontNamesEncoder)
window.g_fontNamesEncoder = new ZBase32Encoder();
//var _name = this.Id;
var _name = window.g_fontNamesEncoder.Encode(this.Id) + ".js";
xhr.open('GET', basePath + "odttf/" + _name, true); // TODO:
if (typeof ArrayBuffer !== 'undefined' && !window.opera)
xhr.responseType = 'arraybuffer';
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment