Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/layout/src/canvas/measureCanvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ const measureCtx = () => {
ctx.strokeOpacity = nil;
ctx.linearGradient = nil;
ctx.radialGradient = nil;
ctx.stop = nil;

ctx.getWidth = () => getMax(points.map((p) => p[0]));
ctx.getHeight = () => getMax(points.map((p) => p[1]));
Expand Down
20 changes: 11 additions & 9 deletions packages/render/src/primitives/renderCanvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,20 @@ const availableMethods = [
'radialGradient',
];

const gradientMethods = ['linearGradient', 'radialGradient'];

const painter = (ctx) => {
const p = availableMethods.reduce(
(acc, prop) => ({
...acc,
[prop]: (...args) => {
const p = {};
availableMethods.forEach((prop) => {
if (gradientMethods.includes(prop)) {
p[prop] = (...args) => ctx[prop](...args);
} else {
p[prop] = (...args) => {
ctx[prop](...args);
return p;
},
}),
{},
);

};
}
});
return p;
};

Expand Down
1 change: 1 addition & 0 deletions packages/render/tests/ctx.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ const createCTX = () => {
instance._root = { data: { AcroForm: {} } };
instance.textInput = vi.fn().mockReturnValue(instance);
instance.formField = vi.fn().mockReturnValue(instance);
instance.stop = vi.fn().mockReturnValue(instance);

return instance;
};
Expand Down
12 changes: 11 additions & 1 deletion packages/render/tests/primitives/renderCanvas.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, expect, test } from 'vitest';
import { describe, expect, test, vi } from 'vitest';

import * as P from '@react-pdf/primitives';

Expand Down Expand Up @@ -67,4 +67,14 @@ describe('primitive renderCanvas', () => {

renderCanvas(ctx, node);
});
test('should execute gradient.stop method correctly', () => {
const ctx = createCTX();
const stopSpy = vi.spyOn(ctx, 'stop');
const gradient = ctx.radialGradient(1, 2, 3, 4, 5, 6);
gradient.stop(0, 'red', 1);
gradient.stop(0, 'blue', 1);
expect(stopSpy).toHaveBeenCalledTimes(2);
expect(stopSpy).toHaveBeenCalledWith(0, 'red', 1);
expect(stopSpy).toHaveBeenCalledWith(0, 'blue', 1);
});
});