clean up string conversion functions

This commit is contained in:
fkaelberer 2014-03-27 13:01:43 +01:00
parent 5fc806823e
commit c978c026fa
4 changed files with 47 additions and 68 deletions

View File

@ -14,7 +14,8 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
/* globals error, Stream, GlyphsUnicode, CFFParser, Encodings, Util */ /* globals error, bytesToString, Stream, GlyphsUnicode, CFFParser, Encodings,
Util */
'use strict'; 'use strict';
@ -675,7 +676,7 @@ var FontRendererFactory = (function FontRendererFactoryClosure() {
var cmap, glyf, loca, cff, indexToLocFormat, unitsPerEm; var cmap, glyf, loca, cff, indexToLocFormat, unitsPerEm;
var numTables = getUshort(data, 4); var numTables = getUshort(data, 4);
for (var i = 0, p = 12; i < numTables; i++, p += 16) { for (var i = 0, p = 12; i < numTables; i++, p += 16) {
var tag = String.fromCharCode.apply(null, data.subarray(p, p + 4)); var tag = bytesToString(data.subarray(p, p + 4));
var offset = getLong(data, p + 8); var offset = getLong(data, p + 8);
var length = getLong(data, p + 12); var length = getLong(data, p + 12);
switch (tag) { switch (tag) {

View File

@ -15,11 +15,11 @@
* limitations under the License. * limitations under the License.
*/ */
/* globals assert, bytesToString, CIDToUnicodeMaps, error, ExpertCharset, /* globals assert, bytesToString, CIDToUnicodeMaps, error, ExpertCharset,
ExpertSubsetCharset, FileReaderSync, GlyphsUnicode, ExpertSubsetCharset, FileReaderSync, GlyphsUnicode, info, isArray,
info, isArray, isNum, ISOAdobeCharset, Stream, isNum, ISOAdobeCharset, Stream, stringToArray, stringToBytes,
stringToBytes, TextDecoder, warn, Lexer, Util, string32, TextDecoder, warn, Lexer, Util, FONT_IDENTITY_MATRIX,
FONT_IDENTITY_MATRIX, FontRendererFactory, shadow, isString, FontRendererFactory, shadow, isString, IdentityCMap, Name,
IdentityCMap, Name, CMapFactory, PDFJS */ CMapFactory, PDFJS */
'use strict'; 'use strict';
@ -372,7 +372,7 @@ var nonStdFontMap = {
'MS-PMincho': 'MS PMincho', 'MS-PMincho': 'MS PMincho',
'MS-PMincho-Bold': 'MS PMincho-Bold', 'MS-PMincho-Bold': 'MS PMincho-Bold',
'MS-PMincho-BoldItalic': 'MS PMincho-BoldItalic', 'MS-PMincho-BoldItalic': 'MS PMincho-BoldItalic',
'MS-PMincho-Italic': 'MS PMincho-Italic', 'MS-PMincho-Italic': 'MS PMincho-Italic'
}; };
var serifFonts = { var serifFonts = {
@ -2328,22 +2328,6 @@ var Font = (function FontClosure() {
this.loading = true; this.loading = true;
} }
function stringToArray(str) {
var array = [];
for (var i = 0, ii = str.length; i < ii; ++i) {
array[i] = str.charCodeAt(i);
}
return array;
}
function arrayToString(arr) {
var strBuf = [];
for (var i = 0, ii = arr.length; i < ii; ++i) {
strBuf.push(String.fromCharCode(arr[i]));
}
return strBuf.join('');
}
function int16(b0, b1) { function int16(b0, b1) {
return (b0 << 8) + b1; return (b0 << 8) + b1;
} }
@ -2368,22 +2352,13 @@ var Font = (function FontClosure() {
} }
function string16(value) { function string16(value) {
return (String.fromCharCode((value >> 8) & 0xff) + return String.fromCharCode((value >> 8) & 0xff, value & 0xff);
String.fromCharCode(value & 0xff));
} }
function safeString16(value) { function safeString16(value) {
// clamp value to the 16-bit int range // clamp value to the 16-bit int range
value = (value > 0x7FFF ? 0x7FFF : (value < -0x8000 ? -0x8000 : value)); value = (value > 0x7FFF ? 0x7FFF : (value < -0x8000 ? -0x8000 : value));
return (String.fromCharCode((value >> 8) & 0xff) + return String.fromCharCode((value >> 8) & 0xff, value & 0xff);
String.fromCharCode(value & 0xff));
}
function string32(value) {
return (String.fromCharCode((value >> 24) & 0xff) +
String.fromCharCode((value >> 16) & 0xff) +
String.fromCharCode((value >> 8) & 0xff) +
String.fromCharCode(value & 0xff));
} }
function createOpenTypeHeader(sfnt, file, numTables) { function createOpenTypeHeader(sfnt, file, numTables) {
@ -2893,11 +2868,7 @@ var Font = (function FontClosure() {
checkAndRepair: function Font_checkAndRepair(name, font, properties) { checkAndRepair: function Font_checkAndRepair(name, font, properties) {
function readTableEntry(file) { function readTableEntry(file) {
var tag = file.getBytes(4); var tag = bytesToString(file.getBytes(4));
tag = String.fromCharCode(tag[0]) +
String.fromCharCode(tag[1]) +
String.fromCharCode(tag[2]) +
String.fromCharCode(tag[3]);
var checksum = file.getUint32(); var checksum = file.getUint32();
var offset = file.getUint32(); var offset = file.getUint32();
@ -2927,7 +2898,7 @@ var Font = (function FontClosure() {
function readOpenTypeHeader(ttf) { function readOpenTypeHeader(ttf) {
return { return {
version: arrayToString(ttf.getBytes(4)), version: bytesToString(ttf.getBytes(4)),
numTables: ttf.getUint16(), numTables: ttf.getUint16(),
searchRange: ttf.getUint16(), searchRange: ttf.getUint16(),
entrySelector: ttf.getUint16(), entrySelector: ttf.getUint16(),
@ -4035,7 +4006,7 @@ var Font = (function FontClosure() {
for (var i = 0; i < numTables; i++) { for (var i = 0; i < numTables; i++) {
var table = tables[tablesNames[i]]; var table = tables[tablesNames[i]];
var tableData = table.data; var tableData = table.data;
ttf.file += arrayToString(tableData); ttf.file += bytesToString(new Uint8Array(tableData));
// 4-byte aligned data // 4-byte aligned data
while (ttf.file.length & 3) { while (ttf.file.length & 3) {
@ -4195,7 +4166,7 @@ var Font = (function FontClosure() {
} }
for (var field in fields) { for (var field in fields) {
var table = fields[field]; var table = fields[field];
otf.file += arrayToString(table); otf.file += bytesToString(new Uint8Array(table));
} }
return stringToArray(otf.file); return stringToArray(otf.file);
@ -4293,7 +4264,7 @@ var Font = (function FontClosure() {
// The viewer's choice, just use an identity map. // The viewer's choice, just use an identity map.
var toUnicode = []; var toUnicode = [];
var firstChar = properties.firstChar, lastChar = properties.lastChar; var firstChar = properties.firstChar, lastChar = properties.lastChar;
for (var i = firstChar, ii = lastChar; i <= ii; i++) { for (var i = firstChar; i <= lastChar; i++) {
toUnicode[i] = String.fromCharCode(i); toUnicode[i] = String.fromCharCode(i);
} }
map.isIdentity = true; map.isIdentity = true;
@ -5837,7 +5808,7 @@ var CFFParser = (function CFFParserClosure() {
} }
data[j] = c; data[j] = c;
} }
names.push(String.fromCharCode.apply(null, data)); names.push(bytesToString(data));
} }
return names; return names;
}, },
@ -5845,7 +5816,7 @@ var CFFParser = (function CFFParserClosure() {
var strings = new CFFStrings(); var strings = new CFFStrings();
for (var i = 0, ii = index.count; i < ii; ++i) { for (var i = 0, ii = index.count; i < ii; ++i) {
var data = index.get(i); var data = index.get(i);
strings.add(String.fromCharCode.apply(null, data)); strings.add(bytesToString(data));
} }
return strings; return strings;
}, },
@ -6513,13 +6484,6 @@ var CFFOffsetTracker = (function CFFOffsetTrackerClosure() {
// Takes a CFF and converts it to the binary representation. // Takes a CFF and converts it to the binary representation.
var CFFCompiler = (function CFFCompilerClosure() { var CFFCompiler = (function CFFCompilerClosure() {
function stringToArray(str) {
var array = [];
for (var i = 0, ii = str.length; i < ii; ++i) {
array[i] = str.charCodeAt(i);
}
return array;
}
function CFFCompiler(cff) { function CFFCompiler(cff) {
this.cff = cff; this.cff = cff;
} }

View File

@ -14,7 +14,8 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
/* globals PDFJS, shadow, isWorker, assert, warn, bytesToString, globalScope */ /* globals PDFJS, shadow, isWorker, assert, warn, bytesToString, string32,
globalScope */
'use strict'; 'use strict';
@ -163,13 +164,6 @@ var FontLoader = {
(data.charCodeAt(offset + 3) & 0xff); (data.charCodeAt(offset + 3) & 0xff);
} }
function string32(value) {
return String.fromCharCode((value >> 24) & 0xff) +
String.fromCharCode((value >> 16) & 0xff) +
String.fromCharCode((value >> 8) & 0xff) +
String.fromCharCode(value & 0xff);
}
function spliceString(s, offset, remove, insert) { function spliceString(s, offset, remove, insert) {
var chunk1 = data.substr(0, offset); var chunk1 = data.substr(0, offset);
var chunk2 = data.substr(offset + remove); var chunk2 = data.substr(offset + remove);
@ -297,7 +291,7 @@ var FontFace = (function FontFaceClosure() {
return null; return null;
} }
var data = bytesToString(this.data); var data = bytesToString(new Uint8Array(this.data));
var fontName = this.loadedName; var fontName = this.loadedName;
// Add the font-face rule to the document // Add the font-face rule to the document

View File

@ -152,7 +152,7 @@ var OPS = PDFJS.OPS = {
paintInlineImageXObjectGroup: 87, paintInlineImageXObjectGroup: 87,
paintImageXObjectRepeat: 88, paintImageXObjectRepeat: 88,
paintImageMaskXObjectRepeat: 89, paintImageMaskXObjectRepeat: 89,
paintSolidColorImageMask: 90, paintSolidColorImageMask: 90
}; };
// A notice for devs. These are good for things that are helpful to devs, such // A notice for devs. These are good for things that are helpful to devs, such
@ -393,23 +393,43 @@ var XRefParseException = (function XRefParseExceptionClosure() {
function bytesToString(bytes) { function bytesToString(bytes) {
var strBuf = [];
var length = bytes.length; var length = bytes.length;
for (var n = 0; n < length; ++n) { var MAX_ARGUMENT_COUNT = 8192;
strBuf.push(String.fromCharCode(bytes[n])); if (length < MAX_ARGUMENT_COUNT) {
return String.fromCharCode.apply(null, bytes);
}
var strBuf = [];
for (var i = 0; i < length; i += MAX_ARGUMENT_COUNT) {
var chunkEnd = Math.min(i + MAX_ARGUMENT_COUNT, length);
var chunk = bytes.subarray(i, chunkEnd);
strBuf.push(String.fromCharCode.apply(null, chunk));
} }
return strBuf.join(''); return strBuf.join('');
} }
function stringToArray(str) {
var length = str.length;
var array = [];
for (var i = 0; i < length; ++i) {
array[i] = str.charCodeAt(i);
}
return array;
}
function stringToBytes(str) { function stringToBytes(str) {
var length = str.length; var length = str.length;
var bytes = new Uint8Array(length); var bytes = new Uint8Array(length);
for (var n = 0; n < length; ++n) { for (var i = 0; i < length; ++i) {
bytes[n] = str.charCodeAt(n) & 0xFF; bytes[i] = str.charCodeAt(i) & 0xFF;
} }
return bytes; return bytes;
} }
function string32(value) {
return String.fromCharCode((value >> 24) & 0xff, (value >> 16) & 0xff,
(value >> 8) & 0xff, value & 0xff);
}
// Lazy test the endianness of the platform // Lazy test the endianness of the platform
// NOTE: This will be 'true' for simulated TypedArrays // NOTE: This will be 'true' for simulated TypedArrays
function isLittleEndian() { function isLittleEndian() {