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