Skip to content

Commit

Permalink
feat: add ability to have 2 decimal floating point pen size
Browse files Browse the repository at this point in the history
  • Loading branch information
MasterOdin committed Aug 26, 2019
1 parent 4b30725 commit d19360d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
16 changes: 14 additions & 2 deletions src/UI/pen.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,26 @@ function savePoint(x, y) {
/**
* Set the attributes of the pen.
*
* @param {Number} penSize The size of the lines drawn by the pen
* @param {Number} penSize The size of the lines drawn by the pen, rounded to 2 decimal places
* @param {String} penColor The color of the lines drawn by the pen
*/
export function setPen(penSize = 1, penColor = '000000') {
_penSize = parseInt(penSize, 10);
_penSize = Math.round(parseFloat(penSize) * 1e2) / 1e2;
_penColor = penColor;
}

/**
* Return pen attributes of the pen
*
* @return {Object} Object with size and color
*/
export function getPen() {
return {
size: _penSize,
color: _penColor
};
}

/**
* Enable the pen behavior
*/
Expand Down
23 changes: 19 additions & 4 deletions test/UI/pen.spec.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { equal } from 'assert';
import { equal, deepStrictEqual } from 'assert';
import PDFJSAnnotate from '../../src/PDFJSAnnotate';
import { firePointerEvent } from '../fireEvent';
import mockAddAnnotation from '../mockAddAnnotation';
import mockGetAnnotations from '../mockGetAnnotations';
import mockSVGContainer from '../mockSVGContainer';
import { setPen, enablePen, disablePen } from '../../src/UI/pen';
import { setPen, getPen, enablePen, disablePen } from '../../src/UI/pen';

let svg;
let addAnnotationSpy;
let __addAnnotation = PDFJSAnnotate.__storeAdapter.addAnnotation;
let __getAnnotations = PDFJSAnnotate.__storeAdapter.getAnnotations;

function simulateCreateDrawingAnnotation(penSize, penColor) {
setPen(penSize, penColor);
function simulateCreateDrawingAnnotation() {
setPen();

firePointerEvent(svg, 'pointerdown', {
clientX: 10,
Expand Down Expand Up @@ -90,4 +90,19 @@ describe('UI::pen', function() {
done();
}, 0);
});

it('allow floating point pen size', () => {
setPen(0.1);
deepStrictEqual(getPen(), {size: 0.1, color: '000000'});
});

it('round floating point pen size to 2 decimal points', () => {
setPen(0.123456);
deepStrictEqual(getPen(), {size: 0.12, color: '000000'});
});

it('parseFloat run on penSize', () => {
setPen('0.12');
deepStrictEqual(getPen(), {size: 0.12, color: '000000'});
});
});

0 comments on commit d19360d

Please sign in to comment.