-
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
263 additions
and
147 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { fromObject } from './fromObject' | ||
import { translate } from './translate' | ||
import { scale } from './scale' | ||
import { rotateDEG } from './rotate' | ||
import { skewDEG } from './skew' | ||
import { shear } from './shear' | ||
|
||
export function fromDefinition (definitionOrArrayOfDefinition) { | ||
return Array.isArray(definitionOrArrayOfDefinition) | ||
? definitionOrArrayOfDefinition.map(mapper) | ||
: mapper(definitionOrArrayOfDefinition) | ||
|
||
function mapper (descriptor) { | ||
switch (descriptor.type) { | ||
case 'matrix': | ||
if ('a' in descriptor && | ||
'b' in descriptor && | ||
'c' in descriptor && | ||
'd' in descriptor && | ||
'e' in descriptor && | ||
'f' in descriptor | ||
) { | ||
return fromObject(descriptor) | ||
} else { | ||
throw new Error('MISSING_MANDATORY_PARAM') | ||
} | ||
|
||
case 'translate': | ||
if (!('tx' in descriptor)) throw new Error('MISSING_MANDATORY_PARAM') | ||
|
||
if ('ty' in descriptor) return translate(descriptor.tx, descriptor.ty) | ||
|
||
return translate(descriptor.tx) | ||
|
||
case 'scale': | ||
if (!('sx' in descriptor)) throw new Error('MISSING_MANDATORY_PARAM') | ||
|
||
if ('sy' in descriptor) return scale(descriptor.sx, descriptor.sy) | ||
|
||
return scale(descriptor.sx) | ||
|
||
case 'rotate': | ||
if (!('angle' in descriptor)) throw new Error('MISSING_MANDATORY_PARAM') | ||
|
||
if ('cx' in descriptor && 'cy' in descriptor) { | ||
return rotateDEG(descriptor.angle, descriptor.cx, descriptor.cy) | ||
} | ||
return rotateDEG(descriptor.angle) | ||
|
||
case 'skewX': | ||
if (!('angle' in descriptor)) throw new Error('MISSING_MANDATORY_PARAM') | ||
return skewDEG(descriptor.angle, 0) | ||
|
||
case 'skewY': | ||
if (!('angle' in descriptor)) throw new Error('MISSING_MANDATORY_PARAM') | ||
return skewDEG(0, descriptor.angle) | ||
|
||
case 'shear': | ||
if (!('shx' in descriptor && 'shy' in descriptor)) throw new Error('MISSING_MANDATORY_PARAM') | ||
return shear(descriptor.shx, descriptor.shy) | ||
|
||
default: | ||
throw new Error('UNSUPPORTED_DESCRIPTOR') | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,11 @@ | ||
import { parse } from './fromTransformAttribute.autogenerated' | ||
import { translate } from './translate' | ||
import { rotateDEG } from './rotate' | ||
import { fromObject } from './fromObject' | ||
import { scale } from './scale' | ||
import { skewDEG } from './skew' | ||
|
||
/** | ||
* Parser for SVG Trasform Attribute http://www.w3.org/TR/SVG/coords.html#TransformAttribute <br/> | ||
* Warning: This should be considered BETA until it is released a stable version of pegjs. | ||
* @param transformString string | ||
* @returns {{descriptors: *, matrices: *}} Parsed matrices | ||
* @returns Array Parsed matrix descriptor | ||
*/ | ||
export function fromTransformAttribute (transformString) { | ||
let descriptors = parse(transformString) | ||
let matrices = descriptors.map(convertMatrixDescriptorToMatrix) | ||
return { descriptors, matrices } | ||
} | ||
|
||
function convertMatrixDescriptorToMatrix (matrixDescriptor) { | ||
let hasParam = key => matrixDescriptor.hasOwnProperty(key) | ||
|
||
let { type, ...params } = matrixDescriptor | ||
switch (type) { | ||
case 'matrix': | ||
return fromObject(params) | ||
|
||
case 'translate': | ||
if (hasParam('ty')) { return translate(params.tx, params.ty) } | ||
|
||
return translate(params.tx) | ||
|
||
case 'scale': | ||
if (hasParam('sy')) { return scale(params.sx, params.sy) } | ||
|
||
return scale(params.sx) | ||
|
||
case 'rotate': | ||
if (hasParam('cx') && hasParam('cy')) { return rotateDEG(params.angle, params.cx, params.cy) } | ||
|
||
return rotateDEG(params.angle) | ||
|
||
case 'skewX': | ||
return skewDEG(params.angle, 0) | ||
|
||
case 'skewY': | ||
return skewDEG(0, params.angle) | ||
|
||
default: | ||
/* istanbul ignore next */ | ||
throw new Error('Unsupported descriptor') | ||
} | ||
return parse(transformString) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* global describe, test, expect */ | ||
import { translate } from '../src/translate' | ||
import { rotateDEG } from '../src/rotate' | ||
import { scale } from '../src/scale' | ||
import { skewDEG } from '../src/skew' | ||
import { fromDefinition, shear } from '../src' | ||
|
||
describe.only('fromDefinition', () => { | ||
test('atomic transformations', () => { | ||
expect(fromDefinition({ type: 'matrix', a: 1.1, b: 2.2, c: 3.3, d: 4.4, e: 5.5, f: 6.6 })).toEqual( | ||
{ a: 1.1, b: 2.2, c: 3.3, d: 4.4, e: 5.5, f: 6.6 } | ||
) | ||
expect(() => fromDefinition({ type: 'matrix' })).toThrow() | ||
|
||
// translate | ||
expect(fromDefinition({ type: 'translate', tx: 1.1 })).toEqual( | ||
translate(1.1, 0) | ||
) | ||
expect(fromDefinition({ type: 'translate', tx: 1.1, ty: 2.2 })).toEqual( | ||
translate(1.1, 2.2) | ||
) | ||
expect(() => fromDefinition({ type: 'translate' })).toThrow() | ||
|
||
// scale | ||
expect(fromDefinition({ type: 'scale', sx: 1.1 })).toEqual( | ||
scale(1.1) | ||
) | ||
expect(fromDefinition({ type: 'scale', sx: 1.1, sy: 2.2 })).toEqual( | ||
scale(1.1, 2.2) | ||
) | ||
expect(() => fromDefinition({ type: 'scale' })).toThrow() | ||
|
||
// rotate | ||
expect(fromDefinition({ type: 'rotate', angle: 45 })).toEqual( | ||
rotateDEG(45) | ||
) | ||
expect(fromDefinition({ type: 'rotate', angle: 45, cx: 100, cy: 200 })).toEqual( | ||
rotateDEG(45, 100, 200) | ||
) | ||
expect(() => fromDefinition({ type: 'rotate' })).toThrow() | ||
|
||
// skew | ||
expect(fromDefinition({ type: 'skewX', angle: 45 })).toEqual( | ||
skewDEG(45, 0) | ||
) | ||
expect(fromDefinition({ type: 'skewY', angle: 45 })).toEqual( | ||
skewDEG(0, 45) | ||
) | ||
expect(() => fromDefinition({ type: 'skewX' })).toThrow() | ||
expect(() => fromDefinition({ type: 'skewY' })).toThrow() | ||
|
||
// shear | ||
expect(fromDefinition({ type: 'shear', shx: 5, shy: 10 })).toEqual( | ||
shear(5, 10) | ||
) | ||
expect(() => fromDefinition({ type: 'shear' })).toThrow() | ||
|
||
// common | ||
expect(() => fromDefinition('M_A_T_R_I_X(1,2,3,4,5,6)')).toThrow() | ||
}) | ||
|
||
test('multiple matrices', () => { | ||
expect( | ||
fromDefinition([ | ||
{ type: 'translate', tx: 1, ty: -1 }, | ||
{ type: 'matrix', a: 6, b: 5, c: 4, d: 3, e: 2, f: 1 }, | ||
{ type: 'translate', tx: 1, ty: -1 }, | ||
{ type: 'translate', tx: 1, ty: -1 }, | ||
{ type: 'translate', tx: 1, ty: -1 } | ||
]) | ||
).toEqual([ | ||
translate(1, -1), | ||
{ a: 6, b: 5, c: 4, d: 3, e: 2, f: 1 }, | ||
translate(1, -1), | ||
translate(1, -1), | ||
translate(1, -1) | ||
] | ||
) | ||
}) | ||
}) |
Oops, something went wrong.