Skip to content

Commit d19360d

Browse files
committed
feat: add ability to have 2 decimal floating point pen size
1 parent 4b30725 commit d19360d

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

src/UI/pen.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,14 +125,26 @@ function savePoint(x, y) {
125125
/**
126126
* Set the attributes of the pen.
127127
*
128-
* @param {Number} penSize The size of the lines drawn by the pen
128+
* @param {Number} penSize The size of the lines drawn by the pen, rounded to 2 decimal places
129129
* @param {String} penColor The color of the lines drawn by the pen
130130
*/
131131
export function setPen(penSize = 1, penColor = '000000') {
132-
_penSize = parseInt(penSize, 10);
132+
_penSize = Math.round(parseFloat(penSize) * 1e2) / 1e2;
133133
_penColor = penColor;
134134
}
135135

136+
/**
137+
* Return pen attributes of the pen
138+
*
139+
* @return {Object} Object with size and color
140+
*/
141+
export function getPen() {
142+
return {
143+
size: _penSize,
144+
color: _penColor
145+
};
146+
}
147+
136148
/**
137149
* Enable the pen behavior
138150
*/

test/UI/pen.spec.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
import { equal } from 'assert';
1+
import { equal, deepStrictEqual } from 'assert';
22
import PDFJSAnnotate from '../../src/PDFJSAnnotate';
33
import { firePointerEvent } from '../fireEvent';
44
import mockAddAnnotation from '../mockAddAnnotation';
55
import mockGetAnnotations from '../mockGetAnnotations';
66
import mockSVGContainer from '../mockSVGContainer';
7-
import { setPen, enablePen, disablePen } from '../../src/UI/pen';
7+
import { setPen, getPen, enablePen, disablePen } from '../../src/UI/pen';
88

99
let svg;
1010
let addAnnotationSpy;
1111
let __addAnnotation = PDFJSAnnotate.__storeAdapter.addAnnotation;
1212
let __getAnnotations = PDFJSAnnotate.__storeAdapter.getAnnotations;
1313

14-
function simulateCreateDrawingAnnotation(penSize, penColor) {
15-
setPen(penSize, penColor);
14+
function simulateCreateDrawingAnnotation() {
15+
setPen();
1616

1717
firePointerEvent(svg, 'pointerdown', {
1818
clientX: 10,
@@ -90,4 +90,19 @@ describe('UI::pen', function() {
9090
done();
9191
}, 0);
9292
});
93+
94+
it('allow floating point pen size', () => {
95+
setPen(0.1);
96+
deepStrictEqual(getPen(), {size: 0.1, color: '000000'});
97+
});
98+
99+
it('round floating point pen size to 2 decimal points', () => {
100+
setPen(0.123456);
101+
deepStrictEqual(getPen(), {size: 0.12, color: '000000'});
102+
});
103+
104+
it('parseFloat run on penSize', () => {
105+
setPen('0.12');
106+
deepStrictEqual(getPen(), {size: 0.12, color: '000000'});
107+
});
93108
});

0 commit comments

Comments
 (0)