Correct PostScript trigonometric operators
PDF 32000-1:2008 7.10.5.1 "Type 4 (PostScript Calculator) Functions" defers to the PostScript Language Reference for the description of these functions. The PostScript Language Reference, third edition chapter 8 "Operators" defines the `angle` type as a "number of degrees". Section 8.1 defines "angle `sin` real", "angle `cos` real", and "num den `atan` angle". The documentation for `atan` further states that it will return an angle in degrees between 0 and 360. Handle these operators correctly in `PostScriptEvaluator.execute`. Convert the inputs to `sin` and `cos` from degrees to radians for use with `Math.sin` and `Math.cos`. Correctly pop two values from the stack for `atan`, use `Math.atan2`, and convert from radians to (positive) degrees.
This commit is contained in:
parent
4e52bcee44
commit
158c836e26
@ -628,8 +628,13 @@ class PostScriptEvaluator {
|
||||
}
|
||||
break;
|
||||
case "atan":
|
||||
b = stack.pop();
|
||||
a = stack.pop();
|
||||
stack.push(Math.atan(a));
|
||||
a = (Math.atan2(a, b) / Math.PI) * 180;
|
||||
if (a < 0) {
|
||||
a += 360;
|
||||
}
|
||||
stack.push(a);
|
||||
break;
|
||||
case "bitshift":
|
||||
b = stack.pop();
|
||||
@ -650,7 +655,7 @@ class PostScriptEvaluator {
|
||||
break;
|
||||
case "cos":
|
||||
a = stack.pop();
|
||||
stack.push(Math.cos(a));
|
||||
stack.push(Math.cos(((a % 360) / 180) * Math.PI));
|
||||
break;
|
||||
case "cvi":
|
||||
a = stack.pop() | 0;
|
||||
@ -774,7 +779,7 @@ class PostScriptEvaluator {
|
||||
break;
|
||||
case "sin":
|
||||
a = stack.pop();
|
||||
stack.push(Math.sin(a));
|
||||
stack.push(Math.sin(((a % 360) / 180) * Math.PI));
|
||||
break;
|
||||
case "sqrt":
|
||||
a = stack.pop();
|
||||
|
3
test/pdfs/.gitignore
vendored
3
test/pdfs/.gitignore
vendored
@ -273,6 +273,7 @@
|
||||
!issue13316_reduced.pdf
|
||||
!issue15977_reduced.pdf
|
||||
!issue4575.pdf
|
||||
!colorspace_atan.pdf
|
||||
!bug1011159.pdf
|
||||
!issue5734.pdf
|
||||
!issue4875.pdf
|
||||
@ -294,6 +295,7 @@
|
||||
!find_all.pdf
|
||||
!helloworld-bad.pdf
|
||||
!zerowidthline.pdf
|
||||
!colorspace_cos.pdf
|
||||
!issue13242.pdf
|
||||
!js-colors.pdf
|
||||
!annotation-line-without-appearance-empty-Rect.pdf
|
||||
@ -423,6 +425,7 @@
|
||||
!issue5475.pdf
|
||||
!issue10519_reduced.pdf
|
||||
!annotation-border-styles.pdf
|
||||
!colorspace_sin.pdf
|
||||
!IdentityToUnicodeMap_charCodeOf.pdf
|
||||
!PDFJS-9279-reduced.pdf
|
||||
!issue5481.pdf
|
||||
|
BIN
test/pdfs/colorspace_atan.pdf
Normal file
BIN
test/pdfs/colorspace_atan.pdf
Normal file
Binary file not shown.
BIN
test/pdfs/colorspace_cos.pdf
Normal file
BIN
test/pdfs/colorspace_cos.pdf
Normal file
Binary file not shown.
BIN
test/pdfs/colorspace_sin.pdf
Normal file
BIN
test/pdfs/colorspace_sin.pdf
Normal file
Binary file not shown.
@ -840,6 +840,12 @@
|
||||
"link": false,
|
||||
"type": "eq"
|
||||
},
|
||||
{ "id": "colorspace_sin",
|
||||
"file": "pdfs/colorspace_sin.pdf",
|
||||
"md5": "4e341c346f4f37cd94b82c532badb8fd",
|
||||
"rounds": 1,
|
||||
"type": "eq"
|
||||
},
|
||||
{ "id": "issue215",
|
||||
"file": "pdfs/issue215.pdf",
|
||||
"md5": "31f3dc60ecf008987d970edfd2b1df61",
|
||||
@ -2410,6 +2416,12 @@
|
||||
"rounds": 1,
|
||||
"type": "eq"
|
||||
},
|
||||
{ "id": "colorspace_cos",
|
||||
"file": "pdfs/colorspace_cos.pdf",
|
||||
"md5": "d3703784c2558f33e03b91dccb745b6c",
|
||||
"rounds": 1,
|
||||
"type": "eq"
|
||||
},
|
||||
{ "id": "issue6692",
|
||||
"file": "pdfs/issue6692.pdf",
|
||||
"md5": "ba078e0ddd59cda4b6c51ea10599f49a",
|
||||
@ -3827,6 +3839,12 @@
|
||||
"rounds": 1,
|
||||
"type": "eq"
|
||||
},
|
||||
{ "id": "colorspace_atan",
|
||||
"file": "pdfs/colorspace_atan.pdf",
|
||||
"md5": "ec310d65a1849a39ae57d0d16b17261d",
|
||||
"rounds": 1,
|
||||
"type": "eq"
|
||||
},
|
||||
{
|
||||
"id": "bug1199237",
|
||||
"file": "pdfs/bug1199237.pdf",
|
||||
|
@ -138,9 +138,9 @@ describe("function", function () {
|
||||
const expectedStack = [254 & 1];
|
||||
expect(stack).toEqual(expectedStack);
|
||||
});
|
||||
it("calculates the inverse tangent of a number", function () {
|
||||
const stack = evaluate("{ 90 atan }");
|
||||
const expectedStack = [Math.atan(90)];
|
||||
it("the angle in degrees (0-360) whose tangent is num/den.", function () {
|
||||
const stack = evaluate("{ 1 -1 atan }");
|
||||
const expectedStack = [135];
|
||||
expect(stack).toEqual(expectedStack);
|
||||
});
|
||||
it("handles bitshifting ", function () {
|
||||
@ -158,9 +158,9 @@ describe("function", function () {
|
||||
const expectedStack = [99, 98, 99, 98];
|
||||
expect(stack).toEqual(expectedStack);
|
||||
});
|
||||
it("calculates the cosine of a number", function () {
|
||||
const stack = evaluate("{ 90 cos }");
|
||||
const expectedStack = [Math.cos(90)];
|
||||
it("calculates the cosine of an angle in degrees", function () {
|
||||
const stack = evaluate("{ 180 cos }");
|
||||
const expectedStack = [-1];
|
||||
expect(stack).toEqual(expectedStack);
|
||||
});
|
||||
it("converts to int", function () {
|
||||
@ -358,9 +358,9 @@ describe("function", function () {
|
||||
const expectedStack = [10];
|
||||
expect(stack).toEqual(expectedStack);
|
||||
});
|
||||
it("calculates the sine of a number", function () {
|
||||
it("calculates the sine of an angle in degrees", function () {
|
||||
const stack = evaluate("{ 90 sin }");
|
||||
const expectedStack = [Math.sin(90)];
|
||||
const expectedStack = [1];
|
||||
expect(stack).toEqual(expectedStack);
|
||||
});
|
||||
it("calculates a square root (integer)", function () {
|
||||
|
Loading…
Reference in New Issue
Block a user