1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-26 10:08:06 +02:00

Fix encoding issues when printing/saving a form with non-ascii characters

This commit is contained in:
Calixte Denizet 2020-08-27 16:04:17 +02:00
parent 187542da8d
commit 56424967f2
6 changed files with 508 additions and 57 deletions

View file

@ -590,6 +590,7 @@ var Font = (function FontClosure() {
this.defaultWidth = properties.defaultWidth;
this.composite = properties.composite;
this.cMap = properties.cMap;
this.capHeight = properties.capHeight / PDF_GLYPH_SPACE_UNITS;
this.ascent = properties.ascent / PDF_GLYPH_SPACE_UNITS;
this.descent = properties.descent / PDF_GLYPH_SPACE_UNITS;
this.fontMatrix = properties.fontMatrix;
@ -3351,9 +3352,93 @@ var Font = (function FontClosure() {
return (charsCache[charsCacheKey] = glyphs);
},
/**
* Chars can have different sizes (depends on the encoding).
* @param {String} a string encoded with font encoding.
* @returns {Array<Array<number>>} the positions of each char in the string.
*/
getCharPositions(chars) {
// This function doesn't use a cache because
// it's called only when saving or printing.
const positions = [];
if (this.cMap) {
const c = Object.create(null);
let i = 0;
while (i < chars.length) {
this.cMap.readCharCode(chars, i, c);
const length = c.length;
positions.push([i, i + length]);
i += length;
}
} else {
for (let i = 0, ii = chars.length; i < ii; ++i) {
positions.push([i, i + 1]);
}
}
return positions;
},
get glyphCacheValues() {
return Object.values(this.glyphCache);
},
/**
* Encode a js string using font encoding.
* The resulting array contains an encoded string at even positions
* (can be empty) and a non-encoded one at odd positions.
* @param {String} a js string.
* @returns {Array<String>} an array of encoded strings or non-encoded ones.
*/
encodeString(str) {
const buffers = [];
const currentBuf = [];
// buffers will contain: encoded, non-encoded, encoded, ...
// currentBuf is pushed in buffers each time there is a change.
// So when buffers.length is odd then the last string is an encoded one
// and currentBuf contains non-encoded chars.
const hasCurrentBufErrors = () => buffers.length % 2 === 1;
for (let i = 0, ii = str.length; i < ii; i++) {
const unicode = str.codePointAt(i);
if (unicode > 0xd7ff && (unicode < 0xe000 || unicode > 0xfffd)) {
// unicode is represented by two uint16
i++;
}
if (this.toUnicode) {
const char = String.fromCodePoint(unicode);
const charCode = this.toUnicode.charCodeOf(char);
if (charCode !== -1) {
if (hasCurrentBufErrors()) {
buffers.push(currentBuf.join(""));
currentBuf.length = 0;
}
const charCodeLength = this.cMap
? this.cMap.getCharCodeLength(charCode)
: 1;
for (let j = charCodeLength - 1; j >= 0; j--) {
currentBuf.push(
String.fromCharCode((charCode >> (8 * j)) & 0xff)
);
}
continue;
}
}
// unicode can't be encoded
if (!hasCurrentBufErrors()) {
buffers.push(currentBuf.join(""));
currentBuf.length = 0;
}
currentBuf.push(String.fromCodePoint(unicode));
}
buffers.push(currentBuf.join(""));
return buffers;
},
};
return Font;
@ -3371,6 +3456,9 @@ var ErrorFont = (function ErrorFontClosure() {
charsToGlyphs: function ErrorFont_charsToGlyphs() {
return [];
},
encodeString: function ErrorFont_encodeString(chars) {
return [chars];
},
exportData(extraProperties = false) {
return { error: this.error };
},