Add a basic embedded CMap reader

This commit is contained in:
Vivien Nicolas 2011-06-20 02:52:30 +02:00
parent 30460ce4b2
commit 6d3d9e3b24

63
pdf.js
View File

@ -1942,13 +1942,12 @@ var CanvasGraphics = (function() {
if (!encoding) if (!encoding)
error("Unknown font encoding"); error("Unknown font encoding");
var firstChar = xref.fetchIfRef(fontDict.get("FirstChar"));
var index = 0; var index = 0;
for (var j = 0; j < encoding.length; j++) { for (var j = 0; j < encoding.length; j++) {
encodingMap[index++] = GlyphsUnicode[encoding[j]]; encodingMap[index++] = GlyphsUnicode[encoding[j]];
} }
var firstChar = xref.fetchIfRef(fontDict.get("FirstChar"));
var widths = xref.fetchIfRef(fontDict.get("Widths")); var widths = xref.fetchIfRef(fontDict.get("Widths"));
assertWellFormed(IsArray(widths) && IsInt(firstChar), assertWellFormed(IsArray(widths) && IsInt(firstChar),
"invalid font Widths or FirstChar"); "invalid font Widths or FirstChar");
@ -1959,7 +1958,65 @@ var CanvasGraphics = (function() {
} }
} }
} else if (fontDict.has("ToUnicode")) { } else if (fontDict.has("ToUnicode")) {
TODO("ToUnicode stream translation not implemented"); var cmapObj = xref.fetchIfRef(fontDict.get("ToUnicode"));
if (IsName(cmapObj)) {
error("ToUnicode basic cmap translation not implemented");
encodingMap = {};
} else if (IsStream(cmapObj)) {
var tokens = [];
var token = "";
var cmap = cmapObj.getBytes(cmapObj.length);
for (var i =0; i < cmap.length; i++) {
var byte = cmap[i];
if (byte == 0x20 || byte == 0x0A || byte == 0x3C || byte == 0x3E) {
switch (token) {
case "useCMap":
error("useCMap is not implemented");
break;
case "begincodespacerange":
case "beginbfrange":
token = "";
tokens = [];
break;
case "endcodespacerange":
TODO("Support CMap ranges");
break;
case "endbfrange":
for (var j = 0; j < tokens.length; j+=3) {
var startRange = parseInt("0x" + tokens[j]);
var endRange = parseInt("0x" + tokens[j+1]);
var code = parseInt("0x" + tokens[j+2]);
for (var k = startRange; k <= endRange; k++) {
encodingMap[k] = code;
charset.push(code++);
}
}
break;
case "beginfbchar":
case "endfbchar":
error("fbchar parsing is not implemented");
break;
default:
if (token.length) {
tokens.push(token);
token = "";
}
break;
}
} else if (byte == 0x5B || byte == 0x5D) {
error("CMAP list parsing is not implemented");
} else {
token += String.fromCharCode(byte);
}
}
}
} }
var subType = fontDict.get("Subtype"); var subType = fontDict.get("Subtype");