Separating fontChar and unicode

This commit is contained in:
notmasteryet 2012-02-14 23:00:09 -06:00
parent c8e88def83
commit 16bd59edf0

View File

@ -833,12 +833,7 @@ var Font = (function FontClosure() {
else
this.rebuildToUnicode(properties);
this.toUnicodeOriginal = this.toUnicode.slice();
for (var i = 0, j = 0xE000; i < this.toUnicode.length; i++) {
if (typeof this.toUnicode[i] == 'number')
break;
this.toUnicode[i] = j++;
}
this.toFontChar = this.buildToFontChar(this.toUnicode);
if (!file) {
// The file data is not specified. Trying to fix the font name
@ -1794,7 +1789,7 @@ var Font = (function FontClosure() {
var unassignedUnicodeItems = [];
for (var i = 1; i < numGlyphs; i++) {
var cid = gidToCidMap[i] || i;
var unicode = this.toUnicode[cid];
var unicode = this.toFontChar[cid];
if (!unicode || typeof unicode !== 'number' || isSpecialUnicode(unicode) ||
unicode in usedUnicodes) {
unassignedUnicodeItems.push(i);
@ -1815,7 +1810,7 @@ var Font = (function FontClosure() {
if (unusedUnicode >= kCmapGlyphOffset + kSizeOfGlyphArea)
break;
var unicode = unusedUnicode++;
this.toUnicode[cid] = unicode;
this.toFontChar[cid] = unicode;
usedUnicodes[unicode] = true;
glyphs.push({ unicode: unicode, code: cid });
ids.push(i);
@ -1826,9 +1821,9 @@ var Font = (function FontClosure() {
var glyphs = cmapTable.glyphs;
var ids = cmapTable.ids;
var hasShortCmap = !!cmapTable.hasShortCmap;
var toUnicode = this.toUnicode;
var toFontChar = this.toFontChar;
if (toUnicode && toUnicode.length > 0) {
if (toFontChar && toFontChar.length > 0) {
// checking if cmap is just identity map
var isIdentity = true;
for (var i = 0, ii = glyphs.length; i < ii; i++) {
@ -1837,11 +1832,11 @@ var Font = (function FontClosure() {
break;
}
}
// if it is, replacing with meaningful toUnicode values
// if it is, replacing with meaningful toFontChar values
if (isIdentity) {
var usedUnicodes = [], unassignedUnicodeItems = [];
for (var i = 0, ii = glyphs.length; i < ii; i++) {
var unicode = toUnicode[i + 1];
var unicode = toFontChar[i + 1];
if (!unicode || typeof unicode !== 'number' || unicode in usedUnicodes) {
unassignedUnicodeItems.push(i);
continue;
@ -1856,11 +1851,11 @@ var Font = (function FontClosure() {
unusedUnicode++;
var cid = i + 1;
// override only if unicode mapping is not specified
if (!(cid in toUnicode))
toUnicode[cid] = unusedUnicode;
if (!(cid in toFontChar))
toFontChar[cid] = unusedUnicode;
glyphs[i].unicode = unusedUnicode++;
}
this.useToUnicode = true;
this.useToFontChar = true;
}
}
@ -1990,12 +1985,12 @@ var Font = (function FontClosure() {
properties.baseEncoding = encoding;
}
if (false && properties.subtype == 'CIDFontType0C') {
var toUnicode = [];
var toFontChar = [];
for (var i = 0; i < charstrings.length; ++i) {
var charstring = charstrings[i];
toUnicode[charstring.code] = charstring.unicode;
toFontChar[charstring.code] = charstring.unicode;
}
this.toUnicode = toUnicode;
this.toFontChar = toFontChar;
}
var fields = {
@ -2090,6 +2085,18 @@ var Font = (function FontClosure() {
return stringToArray(otf.file);
},
buildToFontChar: function font_buildToFontChar(toUnicode) {
var result = [];
var unusedUnicode = kCmapGlyphOffset;
for (var i = 0, ii = toUnicode.length; i < ii; i++) {
var unicode = toUnicode[i];
var fontCharCode = typeof unicode === 'object' ? unusedUnicode++ :
unicode;
result.push(fontCharCode);
}
return result;
},
rebuildToUnicode: function font_rebuildToUnicode(properties) {
var firstChar = properties.firstChar, lastChar = properties.lastChar;
var map = [];
@ -2222,7 +2229,7 @@ var Font = (function FontClosure() {
},
charToGlyph: function fonts_charToGlyph(charcode) {
var unicode, width, codeIRQueue;
var fontCharCode, width, codeIRQueue;
var width = this.widths[charcode];
@ -2230,38 +2237,39 @@ var Font = (function FontClosure() {
case 'CIDFontType0':
if (this.noUnicodeAdaptation) {
width = this.widths[this.unicodeToCID[charcode] || charcode];
unicode = mapPrivateUseChars(charcode);
fontCharCode = mapPrivateUseChars(charcode);
break;
}
unicode = this.toUnicode[charcode] || charcode;
fontCharCode = this.toFontChar[charcode] || charcode;
break;
case 'CIDFontType2':
if (this.noUnicodeAdaptation) {
width = this.widths[this.unicodeToCID[charcode] || charcode];
unicode = mapPrivateUseChars(charcode);
fontCharCode = mapPrivateUseChars(charcode);
break;
}
unicode = this.toUnicode[charcode] || charcode;
fontCharCode = this.toFontChar[charcode] || charcode;
break;
case 'Type1':
var glyphName = this.differences[charcode] || this.encoding[charcode];
if (!isNum(width))
width = this.widths[glyphName];
if (this.noUnicodeAdaptation) {
unicode = mapPrivateUseChars(GlyphsUnicode[glyphName] || charcode);
fontCharCode = mapPrivateUseChars(GlyphsUnicode[glyphName] ||
charcode);
break;
}
unicode = this.glyphNameMap[glyphName] ||
fontCharCode = this.glyphNameMap[glyphName] ||
GlyphsUnicode[glyphName] || charcode;
break;
case 'Type3':
var glyphName = this.differences[charcode] || this.encoding[charcode];
codeIRQueue = this.charProcIRQueues[glyphName];
unicode = charcode;
fontCharCode = charcode;
break;
case 'TrueType':
if (this.useToUnicode) {
unicode = this.toUnicode[charcode] || charcode;
if (this.useToFontChar) {
fontCharCode = this.toFontChar[charcode] || charcode;
break;
}
var glyphName = this.differences[charcode] || this.encoding[charcode];
@ -2270,16 +2278,17 @@ var Font = (function FontClosure() {
if (!isNum(width))
width = this.widths[glyphName];
if (this.noUnicodeAdaptation) {
unicode = GlyphsUnicode[glyphName] || charcode;
fontCharCode = GlyphsUnicode[glyphName] || charcode;
break;
}
if (!this.hasEncoding || this.isSymbolicFont) {
unicode = this.useToUnicode ? this.toUnicode[charcode] : charcode;
fontCharCode = this.useToFontChar ? this.toFontChar[charcode] :
charcode;
break;
}
// MacRoman encoding address by re-encoding the cmap table
unicode = glyphName in this.glyphNameMap ?
fontCharCode = glyphName in this.glyphNameMap ?
this.glyphNameMap[glyphName] : GlyphsUnicode[glyphName];
break;
default:
@ -2287,15 +2296,15 @@ var Font = (function FontClosure() {
break;
}
var unicodeChars = !('toUnicodeOriginal' in this) ? charcode :
this.toUnicodeOriginal[charcode] || charcode;
var unicodeChars = !('toUnicode' in this) ? charcode :
this.toUnicode[charcode] || charcode;
if (typeof unicodeChars === 'number')
unicodeChars = String.fromCharCode(unicodeChars);
width = (isNum(width) ? width : this.defaultWidth) * this.widthMultiplier;
return {
fontChar: String.fromCharCode(unicode),
fontChar: String.fromCharCode(fontCharCode),
unicode: unicodeChars,
width: width,
codeIRQueue: codeIRQueue