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:
Ben Wagner 2023-02-27 12:34:12 -05:00
parent 4e52bcee44
commit 158c836e26
7 changed files with 37 additions and 11 deletions

View File

@ -628,8 +628,13 @@ class PostScriptEvaluator {
} }
break; break;
case "atan": case "atan":
b = stack.pop();
a = 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; break;
case "bitshift": case "bitshift":
b = stack.pop(); b = stack.pop();
@ -650,7 +655,7 @@ class PostScriptEvaluator {
break; break;
case "cos": case "cos":
a = stack.pop(); a = stack.pop();
stack.push(Math.cos(a)); stack.push(Math.cos(((a % 360) / 180) * Math.PI));
break; break;
case "cvi": case "cvi":
a = stack.pop() | 0; a = stack.pop() | 0;
@ -774,7 +779,7 @@ class PostScriptEvaluator {
break; break;
case "sin": case "sin":
a = stack.pop(); a = stack.pop();
stack.push(Math.sin(a)); stack.push(Math.sin(((a % 360) / 180) * Math.PI));
break; break;
case "sqrt": case "sqrt":
a = stack.pop(); a = stack.pop();

View File

@ -273,6 +273,7 @@
!issue13316_reduced.pdf !issue13316_reduced.pdf
!issue15977_reduced.pdf !issue15977_reduced.pdf
!issue4575.pdf !issue4575.pdf
!colorspace_atan.pdf
!bug1011159.pdf !bug1011159.pdf
!issue5734.pdf !issue5734.pdf
!issue4875.pdf !issue4875.pdf
@ -294,6 +295,7 @@
!find_all.pdf !find_all.pdf
!helloworld-bad.pdf !helloworld-bad.pdf
!zerowidthline.pdf !zerowidthline.pdf
!colorspace_cos.pdf
!issue13242.pdf !issue13242.pdf
!js-colors.pdf !js-colors.pdf
!annotation-line-without-appearance-empty-Rect.pdf !annotation-line-without-appearance-empty-Rect.pdf
@ -423,6 +425,7 @@
!issue5475.pdf !issue5475.pdf
!issue10519_reduced.pdf !issue10519_reduced.pdf
!annotation-border-styles.pdf !annotation-border-styles.pdf
!colorspace_sin.pdf
!IdentityToUnicodeMap_charCodeOf.pdf !IdentityToUnicodeMap_charCodeOf.pdf
!PDFJS-9279-reduced.pdf !PDFJS-9279-reduced.pdf
!issue5481.pdf !issue5481.pdf

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -840,6 +840,12 @@
"link": false, "link": false,
"type": "eq" "type": "eq"
}, },
{ "id": "colorspace_sin",
"file": "pdfs/colorspace_sin.pdf",
"md5": "4e341c346f4f37cd94b82c532badb8fd",
"rounds": 1,
"type": "eq"
},
{ "id": "issue215", { "id": "issue215",
"file": "pdfs/issue215.pdf", "file": "pdfs/issue215.pdf",
"md5": "31f3dc60ecf008987d970edfd2b1df61", "md5": "31f3dc60ecf008987d970edfd2b1df61",
@ -2410,6 +2416,12 @@
"rounds": 1, "rounds": 1,
"type": "eq" "type": "eq"
}, },
{ "id": "colorspace_cos",
"file": "pdfs/colorspace_cos.pdf",
"md5": "d3703784c2558f33e03b91dccb745b6c",
"rounds": 1,
"type": "eq"
},
{ "id": "issue6692", { "id": "issue6692",
"file": "pdfs/issue6692.pdf", "file": "pdfs/issue6692.pdf",
"md5": "ba078e0ddd59cda4b6c51ea10599f49a", "md5": "ba078e0ddd59cda4b6c51ea10599f49a",
@ -3827,6 +3839,12 @@
"rounds": 1, "rounds": 1,
"type": "eq" "type": "eq"
}, },
{ "id": "colorspace_atan",
"file": "pdfs/colorspace_atan.pdf",
"md5": "ec310d65a1849a39ae57d0d16b17261d",
"rounds": 1,
"type": "eq"
},
{ {
"id": "bug1199237", "id": "bug1199237",
"file": "pdfs/bug1199237.pdf", "file": "pdfs/bug1199237.pdf",

View File

@ -138,9 +138,9 @@ describe("function", function () {
const expectedStack = [254 & 1]; const expectedStack = [254 & 1];
expect(stack).toEqual(expectedStack); expect(stack).toEqual(expectedStack);
}); });
it("calculates the inverse tangent of a number", function () { it("the angle in degrees (0-360) whose tangent is num/den.", function () {
const stack = evaluate("{ 90 atan }"); const stack = evaluate("{ 1 -1 atan }");
const expectedStack = [Math.atan(90)]; const expectedStack = [135];
expect(stack).toEqual(expectedStack); expect(stack).toEqual(expectedStack);
}); });
it("handles bitshifting ", function () { it("handles bitshifting ", function () {
@ -158,9 +158,9 @@ describe("function", function () {
const expectedStack = [99, 98, 99, 98]; const expectedStack = [99, 98, 99, 98];
expect(stack).toEqual(expectedStack); expect(stack).toEqual(expectedStack);
}); });
it("calculates the cosine of a number", function () { it("calculates the cosine of an angle in degrees", function () {
const stack = evaluate("{ 90 cos }"); const stack = evaluate("{ 180 cos }");
const expectedStack = [Math.cos(90)]; const expectedStack = [-1];
expect(stack).toEqual(expectedStack); expect(stack).toEqual(expectedStack);
}); });
it("converts to int", function () { it("converts to int", function () {
@ -358,9 +358,9 @@ describe("function", function () {
const expectedStack = [10]; const expectedStack = [10];
expect(stack).toEqual(expectedStack); 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 stack = evaluate("{ 90 sin }");
const expectedStack = [Math.sin(90)]; const expectedStack = [1];
expect(stack).toEqual(expectedStack); expect(stack).toEqual(expectedStack);
}); });
it("calculates a square root (integer)", function () { it("calculates a square root (integer)", function () {