Address Yury's PR comments.

This commit is contained in:
Brendan Dahl 2011-12-30 09:24:13 -08:00
parent 20dace0513
commit 6afb49c6c6
3 changed files with 60 additions and 31 deletions

View File

@ -392,7 +392,7 @@ var PDFFunction = (function PDFFunctionClosure() {
}; };
})(); })();
var FunctionCache = (function FunctionCache() { var FunctionCache = (function FunctionCacheClosure() {
// Of 10 PDF's with type4 functions the maxium number of distinct values seen // Of 10 PDF's with type4 functions the maxium number of distinct values seen
// was 256. This still may need some tweaking in the future though. // was 256. This still may need some tweaking in the future though.
var MAX_CACHE_SIZE = 1024; var MAX_CACHE_SIZE = 1024;
@ -401,13 +401,13 @@ var FunctionCache = (function FunctionCache() {
this.total = 0; this.total = 0;
} }
FunctionCache.prototype = { FunctionCache.prototype = {
has: function(key) { has: function has(key) {
return key in this.cache; return key in this.cache;
}, },
get: function(key) { get: function get(key) {
return this.cache[key]; return this.cache[key];
}, },
set: function(key, value) { set: function set(key, value) {
if (this.total < MAX_CACHE_SIZE) { if (this.total < MAX_CACHE_SIZE) {
this.cache[key] = value; this.cache[key] = value;
this.total++; this.total++;
@ -417,7 +417,7 @@ var FunctionCache = (function FunctionCache() {
return FunctionCache; return FunctionCache;
})(); })();
var PostScriptStack = (function PostScriptStack() { var PostScriptStack = (function PostScriptStackClosure() {
var MAX_STACK_SIZE = 100; var MAX_STACK_SIZE = 100;
function PostScriptStack(initialStack) { function PostScriptStack(initialStack) {
this.stack = initialStack || []; this.stack = initialStack || [];
@ -445,24 +445,29 @@ var PostScriptStack = (function PostScriptStack() {
}, },
// rotate the last n stack elements p times // rotate the last n stack elements p times
roll: function roll(n, p) { roll: function roll(n, p) {
var a = this.stack.splice(this.stack.length - n, n); var stack = this.stack;
// algorithm from http://jsfromhell.com/array/rotate var l = stack.length - n;
var l = a.length, p = (Math.abs(p) >= l && (p %= l), var r = stack.length - 1, c = l + (p - Math.floor(p / n) * n), i, j, t;
p < 0 && (p += l), p), i, x; for (i = l, j = r; i < j; i++, j--) {
for (; p; p = (Math.ceil(l / p) - 1) * p - l + (l = p)) t = stack[i]; stack[i] = stack[j]; stack[j] = t;
for (i = l; i > p; x = a[--i], a[i] = a[i - p], a[i - p] = x); }
this.stack = this.stack.concat(a); for (i = l, j = c - 1; i < j; i++, j--) {
t = stack[i]; stack[i] = stack[j]; stack[j] = t;
}
for (i = c, j = r; i < j; i++, j--) {
t = stack[i]; stack[i] = stack[j]; stack[j] = t;
}
} }
}; };
return PostScriptStack; return PostScriptStack;
})(); })();
var PostScriptEvaluator = (function PostScriptEvaluator() { var PostScriptEvaluator = (function PostScriptEvaluatorClosure() {
function PostScriptEvaluator(operators, operands) { function PostScriptEvaluator(operators, operands) {
this.operators = operators; this.operators = operators;
this.operands = operands; this.operands = operands;
} }
PostScriptEvaluator.prototype = { PostScriptEvaluator.prototype = {
execute: function(initialStack) { execute: function execute(initialStack) {
var stack = new PostScriptStack(initialStack); var stack = new PostScriptStack(initialStack);
var counter = 0; var counter = 0;
var operators = this.operators; var operators = this.operators;
@ -531,11 +536,8 @@ var PostScriptEvaluator = (function PostScriptEvaluator() {
stack.push(Math.cos(a)); stack.push(Math.cos(a));
break; break;
case 'cvi': case 'cvi':
a = stack.pop(); a |= stack.pop();
if (a >= 0) stack.push(a);
stack.push(Math.floor(a));
else
stack.push(Math.ceil(a));
break; break;
case 'cvr': case 'cvr':
// noop // noop
@ -622,7 +624,7 @@ var PostScriptEvaluator = (function PostScriptEvaluator() {
break; break;
case 'neg': case 'neg':
a = stack.pop(); a = stack.pop();
stack.push(-1 * b); stack.push(-b);
break; break;
case 'not': case 'not':
a = stack.pop(); a = stack.pop();
@ -678,7 +680,7 @@ var PostScriptEvaluator = (function PostScriptEvaluator() {
b = stack.pop(); b = stack.pop();
a = stack.pop(); a = stack.pop();
if (isBool(a) && isBool(b)) if (isBool(a) && isBool(b))
stack.push((a ^ b) ? true : false); stack.push(a != b);
else else
stack.push(a ^ b); stack.push(a ^ b);
break; break;
@ -693,7 +695,7 @@ var PostScriptEvaluator = (function PostScriptEvaluator() {
return PostScriptEvaluator; return PostScriptEvaluator;
})(); })();
var PostScriptParser = (function PostScriptParser() { var PostScriptParser = (function PostScriptParserClosure() {
function PostScriptParser(lexer) { function PostScriptParser(lexer) {
this.lexer = lexer; this.lexer = lexer;
this.operators = []; this.operators = [];
@ -781,15 +783,33 @@ var PostScriptTokenTypes = {
IFELSE: 5 IFELSE: 5
}; };
var PostScriptToken = (function PostScriptToken() { var PostScriptToken = (function PostScriptTokenClosure() {
function PostScriptToken(type, value) { function PostScriptToken(type, value) {
this.type = type; this.type = type;
this.value = value; this.value = value;
} }
var opCache = {};
PostScriptToken.getOperator = function getOperator(op) {
var opValue = opCache[op];
if (opValue)
return opValue;
return opCache[op] = new PostScriptToken(PostScriptTokenTypes.OPERATOR, op);
};
PostScriptToken.LBRACE = new PostScriptToken(PostScriptTokenTypes.LBRACE,
'{');
PostScriptToken.RBRACE = new PostScriptToken(PostScriptTokenTypes.RBRACE,
'}');
PostScriptToken.IF = new PostScriptToken(PostScriptTokenTypes.IF, 'IF');
PostScriptToken.IFELSE = new PostScriptToken(PostScriptTokenTypes.IFELSE,
'IFELSE');
return PostScriptToken; return PostScriptToken;
})(); })();
var PostScriptLexer = (function PostScriptLexer() { var PostScriptLexer = (function PostScriptLexerClosure() {
function PostScriptLexer(stream) { function PostScriptLexer(stream) {
this.stream = stream; this.stream = stream;
} }
@ -821,9 +841,9 @@ var PostScriptLexer = (function PostScriptLexer() {
return new PostScriptToken(PostScriptTokenTypes.NUMBER, return new PostScriptToken(PostScriptTokenTypes.NUMBER,
this.getNumber(ch)); this.getNumber(ch));
case '{': case '{':
return new PostScriptToken(PostScriptTokenTypes.LBRACE, '{'); return PostScriptToken.LBRACE;
case '}': case '}':
return new PostScriptToken(PostScriptTokenTypes.RBRACE, '}'); return PostScriptToken.RBRACE;
} }
// operator // operator
var str = ch.toLowerCase(); var str = ch.toLowerCase();
@ -837,11 +857,11 @@ var PostScriptLexer = (function PostScriptLexer() {
} }
switch (str) { switch (str) {
case 'if': case 'if':
return new PostScriptToken(PostScriptTokenTypes.IF, str); return PostScriptToken.IF;
case 'ifelse': case 'ifelse':
return new PostScriptToken(PostScriptTokenTypes.IFELSE, str); return PostScriptToken.IFELSE;
default: default:
return new PostScriptToken(PostScriptTokenTypes.OPERATOR, str); return PostScriptToken.getOperator(str);
} }
}, },
getNumber: function getNumber(ch) { getNumber: function getNumber(ch) {

View File

@ -369,7 +369,7 @@
"rounds": 1, "rounds": 1,
"type": "eq" "type": "eq"
}, },
{ "id": "smaskdim", { "id": "type4psfunc",
"file": "pdfs/type4psfunc.pdf", "file": "pdfs/type4psfunc.pdf",
"md5": "7e6027a02ff78577f74dccdf84e37189", "md5": "7e6027a02ff78577f74dccdf84e37189",
"rounds": 1, "rounds": 1,

View File

@ -145,7 +145,16 @@ describe('function', function() {
// TODO ceiling // TODO ceiling
// TODO copy // TODO copy
// TODO cos // TODO cos
// TODO cvi it('converts to int', function() {
var stack = evaluate('{ 9.9 cvi }');
var expectedStack = [9];
expect(stack).toMatchArray(expectedStack);
});
it('converts negatives to int', function() {
var stack = evaluate('{ -9.9 cvi }');
var expectedStack = [-9];
expect(stack).toMatchArray(expectedStack);
});
// TODO cvr // TODO cvr
// TODO div // TODO div
it('duplicates', function() { it('duplicates', function() {