Browse Source

cleanup, added new flag to the font properties called compositeFont. If true, the encoding and ShowText strings must be multi-byte

Adil Allawi 14 years ago
parent
commit
6b6e97dff6
  1. 21
      fonts.js
  2. 53
      pdf.js

21
fonts.js

@ -389,6 +389,7 @@ var Font = (function() {
var data; var data;
switch (properties.type) { switch (properties.type) {
case 'Type1': case 'Type1':
case 'CIDFontType0':
var cff = new CFF(name, file, properties); var cff = new CFF(name, file, properties);
this.mimetype = 'font/opentype'; this.mimetype = 'font/opentype';
@ -397,15 +398,7 @@ var Font = (function() {
break; break;
case 'TrueType': case 'TrueType':
this.mimetype = 'font/opentype'; case 'CIDFontType2':
// Repair the TrueType file if it is can be damaged in the point of
// view of the sanitizer
data = this.checkAndRepair(name, file, properties);
break;
case 'Type0':
//this is a Truetype font
this.mimetype = 'font/opentype'; this.mimetype = 'font/opentype';
// Repair the TrueType file if it is can be damaged in the point of // Repair the TrueType file if it is can be damaged in the point of
@ -421,6 +414,7 @@ var Font = (function() {
this.type = properties.type; //use the type to test if the string is single or multi-byte this.type = properties.type; //use the type to test if the string is single or multi-byte
this.id = Fonts.registerFont(name, data, properties); this.id = Fonts.registerFont(name, data, properties);
this.loadedName = 'pdfFont' + this.id; this.loadedName = 'pdfFont' + this.id;
this.compositeFont = properties.compositeFont;
}; };
function stringToArray(str) { function stringToArray(str) {
@ -1150,8 +1144,11 @@ var Font = (function() {
if (!charsCache) if (!charsCache)
charsCache = this.charsCache = Object.create(null); charsCache = this.charsCache = Object.create(null);
if (this.type == "Type0") { if (this.compositeFont) {
//string needs to be converted from byte to multi-byte assume for now two-byte // composite fonts have multi-byte strings
// convert the string from single-byte to multi-byte
// XXX assuming CIDFonts are two-byte - later need to extract the correct byte encoding
// according to the PDF spec
str = ''; str = '';
var multiByteStr = ""; var multiByteStr = "";
var length = chars.length; var length = chars.length;
@ -1162,7 +1159,7 @@ var Font = (function() {
byte2 = 0; byte2 = 0;
else else
byte2 = chars.charCodeAt(i) & 0xFF; byte2 = chars.charCodeAt(i) & 0xFF;
multiByteStr += String.fromCharCode((byte1<<8) | byte2); multiByteStr += String.fromCharCode((byte1 << 8) | byte2);
} }
str = multiByteStr; str = multiByteStr;
} }

53
pdf.js

@ -64,14 +64,6 @@ function stringToBytes(str) {
return bytes; return bytes;
} }
function singleByteToMultiByteString (str) {
var multiByteStr = "";
var bytes = stringToBytes(e);
for (var j = 0; j<bytes.length; j++) {
multiByteStr += String.fromCharCode((bytes[j++]<<16) | bytes[j]);
}
return multiByteStr;
}
var Stream = (function() { var Stream = (function() {
function constructor(arrayBuffer, start, length, dict) { function constructor(arrayBuffer, start, length, dict) {
this.bytes = new Uint8Array(arrayBuffer); this.bytes = new Uint8Array(arrayBuffer);
@ -3635,16 +3627,22 @@ var PartialEvaluator = (function() {
var fd; var fd;
var descendant = []; var descendant = [];
var subType = fontDict.get('Subtype'); var subType = fontDict.get('Subtype');
var compositeFont = false;
assertWellFormed(IsName(subType), 'invalid font Subtype'); assertWellFormed(IsName(subType), 'invalid font Subtype');
//If font is a composite get the FontDescriptor from the descendant font //If font is a composite
if (subType.name == "Type0") // - get the descendant font
// - set the type according to the descendant font
// - get the FontDescriptor from the descendant font
if (subType.name == 'Type0')
{ {
var df = fontDict.get("DescendantFonts"); var df = fontDict.get('DescendantFonts');
if (!df) if (!df)
return null; return null;
compositeFont = true;
descendant = xref.fetch(df[0]); descendant = xref.fetch(df[0]);
fd = descendant.get("FontDescriptor"); subType = descendant.get('Subtype');
fd = descendant.get('FontDescriptor');
} else { } else {
fd = fontDict.get('FontDescriptor'); fd = fontDict.get('FontDescriptor');
} }
@ -3665,24 +3663,26 @@ var PartialEvaluator = (function() {
var encodingMap = {}; var encodingMap = {};
var charset = []; var charset = [];
if (subType.name == 'Type0') { if (compositeFont) {
//XXX CIDFont support - only identity CID Encoding for now //Special CIDFont support
//XXX only identity CID Encodings supported for now
var encoding = xref.fetchIfRef(fontDict.get('Encoding')); var encoding = xref.fetchIfRef(fontDict.get('Encoding'));
if (IsName(encoding)) { if (IsName(encoding)) {
//Encoding is a predefined CMap //Encoding is a predefined CMap
if (encoding.name == 'Identity-H') { if (encoding.name == 'Identity-H') {
if (descendant.get('Subtype').name == 'CIDFontType2') if (subType.name == 'CIDFontType2') {
{ var cidToGidMap = descendant.get('CIDToGIDMap');
//Extract an encoding from the CIDToGIDMap if (cidToGidMap) {
var glyphsStream = xref.fetchIfRef(descendant.get('CIDToGIDMap')); //Extract the charset from the CIDToGIDMap
var glyphsData = glyphsStream.getBytes(0); var glyphsStream = xref.fetchIfRef(cidToGidMap);
var i = 0; var glyphsData = glyphsStream.getBytes(0);
for (var j=0; j<glyphsData.length; j++) { var i = 0;
var glyphID = (glyphsData[j++]*0x100)+glyphsData[j]; //glyph ids are big-endian 2-byte values
//encodingMap[glyphID] = i++; for (var j=0; j<glyphsData.length; j++) {
charset.push(glyphID); var glyphID = (glyphsData[j++] << 8) | glyphsData[j];
charset.push(glyphID);
}
} }
encoding[0] = 0;
} }
} else { } else {
TODO ('Need to support predefined CMaps see PDF 32000-1:2008 9.7.5.2 Predefined CMaps') TODO ('Need to support predefined CMaps see PDF 32000-1:2008 9.7.5.2 Predefined CMaps')
@ -3820,7 +3820,8 @@ var PartialEvaluator = (function() {
flags: descriptor.get('Flags'), flags: descriptor.get('Flags'),
italicAngle: descriptor.get('ItalicAngle'), italicAngle: descriptor.get('ItalicAngle'),
fixedPitch: false, fixedPitch: false,
textMatrix: IDENTITY_MATRIX textMatrix: IDENTITY_MATRIX,
compositeFont: compositeFont
}; };
return { return {

Loading…
Cancel
Save