From 63d5aae3f610397238b37654b612cf55a91c4a27 Mon Sep 17 00:00:00 2001 From: Yury Delendik Date: Wed, 30 Apr 2014 09:09:04 -0500 Subject: [PATCH] Groups path commands into single command --- src/core/evaluator.js | 24 +++++++++++++-- src/display/canvas.js | 61 +++++++++++++++++++++++++------------ src/shared/util.js | 3 +- test/unit/evaluator_spec.js | 4 +-- 4 files changed, 68 insertions(+), 24 deletions(-) diff --git a/src/core/evaluator.js b/src/core/evaluator.js index dde570e78..7777bf68a 100644 --- a/src/core/evaluator.js +++ b/src/core/evaluator.js @@ -502,6 +502,18 @@ var PartialEvaluator = (function PartialEvaluatorClosure() { return font; }, + buildPath: function PartialEvaluator_buildPath(operatorList, fn, args) { + var lastIndex = operatorList.length - 1; + if (lastIndex < 0 || + operatorList.fnArray[lastIndex] !== OPS.constructPath) { + operatorList.addOp(OPS.constructPath, [[fn], args]); + } else { + var opArgs = operatorList.argsArray[lastIndex]; + opArgs[0].push(fn); + Array.prototype.push.apply(opArgs[1], args); + } + }, + getOperatorList: function PartialEvaluator_getOperatorList(stream, resources, operatorList, @@ -525,7 +537,7 @@ var PartialEvaluator = (function PartialEvaluatorClosure() { var fn = operation.fn; var shading; - switch (fn) { + switch (fn | 0) { case OPS.setStrokeColorN: case OPS.setFillColorN: if (args[args.length - 1].code) { @@ -668,6 +680,14 @@ var PartialEvaluator = (function PartialEvaluatorClosure() { stateManager); args = []; continue; + case OPS.moveTo: + case OPS.lineTo: + case OPS.curveTo: + case OPS.curveTo2: + case OPS.curveTo3: + case OPS.closePath: + self.buildPath(operatorList, fn, args); + continue; } operatorList.addOp(fn, args); } @@ -838,7 +858,7 @@ var PartialEvaluator = (function PartialEvaluatorClosure() { textState = stateManager.state; var fn = operation.fn; var args = operation.args; - switch (fn) { + switch (fn | 0) { case OPS.setFont: handleSetFont(args[0].name); textState.fontSize = args[1]; diff --git a/src/display/canvas.js b/src/display/canvas.js index 355f7a354..048ffbb1f 100644 --- a/src/display/canvas.js +++ b/src/display/canvas.js @@ -959,26 +959,49 @@ var CanvasGraphics = (function CanvasGraphicsClosure() { }, // Path - moveTo: function CanvasGraphics_moveTo(x, y) { - this.ctx.moveTo(x, y); - this.current.setCurrentPoint(x, y); - }, - lineTo: function CanvasGraphics_lineTo(x, y) { - this.ctx.lineTo(x, y); - this.current.setCurrentPoint(x, y); - }, - curveTo: function CanvasGraphics_curveTo(x1, y1, x2, y2, x3, y3) { - this.ctx.bezierCurveTo(x1, y1, x2, y2, x3, y3); - this.current.setCurrentPoint(x3, y3); - }, - curveTo2: function CanvasGraphics_curveTo2(x2, y2, x3, y3) { + constructPath: function CanvasGraphics_constructPath(ops, args) { + var ctx = this.ctx; var current = this.current; - this.ctx.bezierCurveTo(current.x, current.y, x2, y2, x3, y3); - current.setCurrentPoint(x3, y3); - }, - curveTo3: function CanvasGraphics_curveTo3(x1, y1, x3, y3) { - this.curveTo(x1, y1, x3, y3, x3, y3); - this.current.setCurrentPoint(x3, y3); + var x = current.x, y = current.y; + for (var i = 0, j = 0, ii = ops.length; i < ii; i++) { + switch (ops[i] | 0) { + case OPS.moveTo: + x = args[j++]; + y = args[j++]; + ctx.moveTo(x, y); + break; + case OPS.lineTo: + x = args[j++]; + y = args[j++]; + ctx.lineTo(x, y); + break; + case OPS.curveTo: + ctx.bezierCurveTo(args[j], args[j + 1], args[j + 2], args[j + 3], + args[j + 4], args[j + 5]); + x = args[j + 4]; + y = args[j + 5]; + j += 6; + break; + case OPS.curveTo2: + ctx.bezierCurveTo(x, y, args[j], args[j + 1], + args[j + 2], args[j + 3]); + x = args[j + 2]; + y = args[j + 3]; + j += 4; + break; + case OPS.curveTo3: + ctx.bezierCurveTo(args[j], args[j + 1], args[j + 2], args[j + 3], + args[j + 2], args[j + 3]); + x = args[j + 2]; + y = args[j + 3]; + j += 4; + break; + case OPS.closePath: + ctx.closePath(); + break; + } + } + current.setCurrentPoint(x, y); }, closePath: function CanvasGraphics_closePath() { this.ctx.closePath(); diff --git a/src/shared/util.js b/src/shared/util.js index b2c172ae1..3b70aecbb 100644 --- a/src/shared/util.js +++ b/src/shared/util.js @@ -152,7 +152,8 @@ var OPS = PDFJS.OPS = { paintInlineImageXObjectGroup: 87, paintImageXObjectRepeat: 88, paintImageMaskXObjectRepeat: 89, - paintSolidColorImageMask: 90 + paintSolidColorImageMask: 90, + constructPath: 91 }; // A notice for devs. These are good for things that are helpful to devs, such diff --git a/test/unit/evaluator_spec.js b/test/unit/evaluator_spec.js index 007365714..23722beb7 100644 --- a/test/unit/evaluator_spec.js +++ b/test/unit/evaluator_spec.js @@ -115,13 +115,13 @@ describe('evaluator', function() { var evaluator = new PartialEvaluator(new PdfManagerMock(), new XrefMock(), new HandlerMock(), 'prefix'); - var stream = new StringStream('trueifalserinullh'); + var stream = new StringStream('trueifalserinulln'); var result = evaluator.getOperatorList(stream, new ResourcesMock()); expect(!!result.fnArray && !!result.argsArray).toEqual(true); expect(result.fnArray.length).toEqual(3); expect(result.fnArray[0]).toEqual(OPS.setFlatness); expect(result.fnArray[1]).toEqual(OPS.setRenderingIntent); - expect(result.fnArray[2]).toEqual(OPS.closePath); + expect(result.fnArray[2]).toEqual(OPS.endPath); expect(result.argsArray.length).toEqual(3); expect(result.argsArray[0].length).toEqual(1); expect(result.argsArray[0][0]).toEqual(true);