Refactors encodeFloat and flex args

This commit is contained in:
Yury Delendik 2012-12-26 21:29:28 -06:00
parent e22ee548f9
commit ccfa0e1972

View File

@ -4443,6 +4443,7 @@ var Type1Parser = function type1Parser() {
// Type1 only command with command not (yet) built-in ,throw an error // Type1 only command with command not (yet) built-in ,throw an error
'7': -1, // sbw '7': -1, // sbw
'10': 'add',
'11': 'sub', '11': 'sub',
'12': 'div', '12': 'div',
@ -4507,16 +4508,6 @@ var Type1Parser = function type1Parser() {
return args; return args;
} }
// Remove the same number of args from the stack that are in the args
// parameter. Args should be built from breakUpArgs().
function popArgs(stack, args) {
for (var i = 0, ii = args.length; i < ii; i++) {
for (var j = 0, jj = args[i].arg.length; j < jj; j++) {
stack.pop();
}
}
}
function decodeCharString(array) { function decodeCharString(array) {
var charstring = []; var charstring = [];
var lsb = 0; var lsb = 0;
@ -4617,25 +4608,32 @@ var Type1Parser = function type1Parser() {
break; break;
case 0: case 0:
var flexArgs = breakUpArgs(charstring, 17); var flexArgs = breakUpArgs(charstring, 17);
popArgs(charstring, flexArgs);
charstring.push( // removing all flex arguments from the stack
flexArgs[2].value + flexArgs[0].value, // bcp1x + rpx charstring.splice(flexArgs[0].offset,
flexArgs[3].value + flexArgs[1].value, // bcp1y + rpy charstring.length - flexArgs[0].offset);
flexArgs[4].value, // bcp2x
flexArgs[5].value, // bcp2y charstring = charstring.concat(
flexArgs[6].value, // p2x flexArgs[0].arg, // bcp1x +
flexArgs[7].value, // p2y flexArgs[2].arg, // rpx
flexArgs[8].value, // bcp3x ['add'],
flexArgs[9].value, // bcp3y flexArgs[1].arg, // bcp1y +
flexArgs[10].value, // bcp4x flexArgs[3].arg, // rpy
flexArgs[11].value, // bcp4y ['add'],
flexArgs[12].value, // p3x flexArgs[4].arg, // bcp2x
flexArgs[13].value, // p3y flexArgs[5].arg, // bcp2y
flexArgs[14].value, // flexDepth flexArgs[6].arg, // p2x
flexArgs[7].arg, // p2y
flexArgs[8].arg, // bcp3x
flexArgs[9].arg, // bcp3y
flexArgs[10].arg, // bcp4x
flexArgs[11].arg, // bcp4y
flexArgs[12].arg, // p3x
flexArgs[13].arg, // p3y
flexArgs[14].arg, // flexDepth
// 15 = finalx unused by flex // 15 = finalx unused by flex
// 16 = finaly unused by flex // 16 = finaly unused by flex
'flex' ['flex']
); );
flexing = false; flexing = false;
@ -4650,14 +4648,9 @@ var Type1Parser = function type1Parser() {
charstring.push(0); charstring.push(0);
continue; // ignoring hmoveto continue; // ignoring hmoveto
} else if (value == 4 && flexing) { // vmoveto } else if (value == 4 && flexing) { // vmoveto
// Add the dx for flex and but also swap the values so they are the // Insert the dx for flex before dy.
// right order. var flexArgs = breakUpArgs(charstring, 1);
var vArgs = breakUpArgs(charstring, 1); charstring.splice(flexArgs[0].offset, 0, 0);
popArgs(charstring, vArgs);
charstring.push(0);
for (var t = 0, tt = vArgs[0].arg.length; t < tt; t++) {
charstring.push(vArgs[0].arg[t]);
}
continue; // ignoring vmoveto continue; // ignoring vmoveto
} else if (!HINTING_ENABLED && (value == 1 || value == 3)) { } else if (!HINTING_ENABLED && (value == 1 || value == 3)) {
charstring.push('drop', 'drop'); charstring.push('drop', 'drop');
@ -5124,6 +5117,7 @@ Type1Font.prototype = {
'rrcurveto': 8, 'rrcurveto': 8,
'callsubr': 10, 'callsubr': 10,
'return': 11, 'return': 11,
'add': [12, 10],
'sub': [12, 11], 'sub': [12, 11],
'div': [12, 12], 'div': [12, 12],
'exch': [12, 28], 'exch': [12, 28],
@ -6384,37 +6378,26 @@ var CFFCompiler = (function CFFCompilerClosure() {
else else
return this.encodeFloat(value); return this.encodeFloat(value);
}, },
encodeFloat: function CFFCompiler_encodeFloat(value) { encodeFloat: function CFFCompiler_encodeFloat(num) {
value = value.toString(); var value = num.toString();
// Strip off the any leading zeros. var nibbles = '';
if (value.substr(0, 2) === '0.')
value = value.substr(1);
else if (value.substr(0, 3) === '-0.')
value = '-' + value.substr(2);
var nibbles = [];
for (var i = 0, ii = value.length; i < ii; ++i) { for (var i = 0, ii = value.length; i < ii; ++i) {
var a = value.charAt(i), b = value.charAt(i + 1); var a = value[i];
var nibble; if (a === 'e') {
if (a === 'e' && b === '-') { nibbles += value[++i] === '-' ? 'c' : 'b';
nibble = 0xc;
++i;
} else if (a === '.') { } else if (a === '.') {
nibble = 0xa; nibbles += 'a';
} else if (a === 'E') {
nibble = 0xb;
} else if (a === '-') { } else if (a === '-') {
nibble = 0xe; nibbles += 'e';
} else { } else {
nibble = a; nibbles += a;
} }
nibbles.push(nibble);
} }
nibbles.push(0xf); nibbles += (nibbles.length & 1) ? 'f' : 'ff';
if (nibbles.length % 2)
nibbles.push(0xf);
var out = [30]; var out = [30];
for (var i = 0, ii = nibbles.length; i < ii; i += 2) for (var i = 0, ii = nibbles.length; i < ii; i += 2) {
out.push(nibbles[i] << 4 | nibbles[i + 1]); out.push(parseInt(nibbles.substr(i, 2), 16));
}
return out; return out;
}, },
encodeInteger: function CFFCompiler_encodeInteger(value) { encodeInteger: function CFFCompiler_encodeInteger(value) {