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

@ -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 () {