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

Refactor text extraction / font loading logic

This commit is contained in:
Yury Delendik 2012-09-13 08:09:46 -07:00
parent 6fc70bb2a7
commit e086cf36f4
4 changed files with 98 additions and 144 deletions

View file

@ -1464,54 +1464,30 @@ var NormalizedUnicodes = {
'\uFE4F': '\u005F'
};
function fontCharsToUnicode(charCodes, fontProperties) {
var toUnicode = fontProperties.toUnicode;
var composite = fontProperties.composite;
var encoding, differences, cidToUnicode;
var result = '';
if (composite) {
cidToUnicode = fontProperties.cidToUnicode;
for (var i = 0, ii = charCodes.length; i < ii; i += 2) {
var charCode = (charCodes.charCodeAt(i) << 8) |
charCodes.charCodeAt(i + 1);
if (toUnicode && charCode in toUnicode) {
var unicode = toUnicode[charCode];
result += typeof unicode !== 'number' ? unicode :
String.fromCharCode(unicode);
continue;
}
result += String.fromCharCode(!cidToUnicode ? charCode :
cidToUnicode[charCode] || charCode);
}
} else {
differences = fontProperties.differences;
encoding = fontProperties.baseEncoding;
for (var i = 0, ii = charCodes.length; i < ii; i++) {
var charCode = charCodes.charCodeAt(i);
var unicode;
if (toUnicode && charCode in toUnicode) {
var unicode = toUnicode[charCode];
result += typeof unicode !== 'number' ? unicode :
String.fromCharCode(unicode);
continue;
}
function reverseIfRtl(chars) {
var charsLength = chars.length;
//reverse an arabic ligature
if (charsLength <= 1 || !isRTLRangeFor(chars.charCodeAt(0)))
return chars;
var glyphName = charCode in differences ? differences[charCode] :
encoding[charCode];
if (glyphName in GlyphsUnicode) {
result += String.fromCharCode(GlyphsUnicode[glyphName]);
continue;
}
result += String.fromCharCode(charCode);
}
}
// normalizing the unicode characters
for (var i = 0, ii = result.length; i < ii; i++) {
if (!(result[i] in NormalizedUnicodes))
var s = '';
for (var ii = charsLength - 1; ii >= 0; ii--)
s += chars[ii];
return s;
}
function fontCharsToUnicode(charCodes, font) {
var glyphs = font.charsToGlyphs(charCodes);
var result = '';
for (var i = 0, ii = glyphs.length; i < ii; i++) {
var glyph = glyphs[i];
if (!glyph)
continue;
result = result.substring(0, i) + NormalizedUnicodes[result[i]] +
result.substring(i + 1);
ii = result.length;
var glyphUnicode = glyph.unicode;
if (glyphUnicode in NormalizedUnicodes)
glyphUnicode = NormalizedUnicodes[glyphUnicode];
result += reverseIfRtl(glyphUnicode);
}
return result;
}
@ -1536,8 +1512,9 @@ var Font = (function FontClosure() {
}
this.name = name;
this.loadedName = properties.loadedName;
this.coded = properties.coded;
this.charProcOperatorList = properties.charProcOperatorList;
this.loadCharProcs = properties.coded;
this.sizes = [];
var names = name.split('+');
@ -1641,7 +1618,6 @@ var Font = (function FontClosure() {
this.widthMultiplier = !properties.fontMatrix ? 1.0 :
1.0 / properties.fontMatrix[0];
this.encoding = properties.baseEncoding;
this.loadedName = properties.loadedName;
this.loading = true;
};