Merge pull request #11307 from Snuffleupagus/stringToPDFString-little-endian
Support UTF-16 little-endian strings in the `stringToPDFString` helper function (bug 1593902)
This commit is contained in:
commit
4e0b02025f
@ -752,6 +752,12 @@ function stringToPDFString(str) {
|
|||||||
strBuf.push(String.fromCharCode(
|
strBuf.push(String.fromCharCode(
|
||||||
(str.charCodeAt(i) << 8) | str.charCodeAt(i + 1)));
|
(str.charCodeAt(i) << 8) | str.charCodeAt(i + 1)));
|
||||||
}
|
}
|
||||||
|
} else if (str[0] === '\xFF' && str[1] === '\xFE') {
|
||||||
|
// UTF16LE BOM
|
||||||
|
for (let i = 2; i < length; i += 2) {
|
||||||
|
strBuf.push(String.fromCharCode(
|
||||||
|
(str.charCodeAt(i + 1) << 8) | str.charCodeAt(i)));
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
for (let i = 0; i < length; ++i) {
|
for (let i = 0; i < length; ++i) {
|
||||||
const code = PDFStringTranslateTable[str.charCodeAt(i)];
|
const code = PDFStringTranslateTable[str.charCodeAt(i)];
|
||||||
|
@ -179,11 +179,16 @@ describe('util', function() {
|
|||||||
expect(stringToPDFString(str)).toEqual('\u201Cstring\u201D');
|
expect(stringToPDFString(str)).toEqual('\u201Cstring\u201D');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('handles UTF-16BE strings', function() {
|
it('handles UTF-16 big-endian strings', function() {
|
||||||
let str = '\xFE\xFF\x00\x73\x00\x74\x00\x72\x00\x69\x00\x6E\x00\x67';
|
let str = '\xFE\xFF\x00\x73\x00\x74\x00\x72\x00\x69\x00\x6E\x00\x67';
|
||||||
expect(stringToPDFString(str)).toEqual('string');
|
expect(stringToPDFString(str)).toEqual('string');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('handles UTF-16 little-endian strings', function() {
|
||||||
|
let str = '\xFF\xFE\x73\x00\x74\x00\x72\x00\x69\x00\x6E\x00\x67\x00';
|
||||||
|
expect(stringToPDFString(str)).toEqual('string');
|
||||||
|
});
|
||||||
|
|
||||||
it('handles empty strings', function() {
|
it('handles empty strings', function() {
|
||||||
// ISO Latin 1
|
// ISO Latin 1
|
||||||
let str1 = '';
|
let str1 = '';
|
||||||
@ -192,6 +197,10 @@ describe('util', function() {
|
|||||||
// UTF-16BE
|
// UTF-16BE
|
||||||
let str2 = '\xFE\xFF';
|
let str2 = '\xFE\xFF';
|
||||||
expect(stringToPDFString(str2)).toEqual('');
|
expect(stringToPDFString(str2)).toEqual('');
|
||||||
|
|
||||||
|
// UTF-16LE
|
||||||
|
let str3 = '\xFF\xFE';
|
||||||
|
expect(stringToPDFString(str3)).toEqual('');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user