|
2 | 2 | 'use strict'; |
3 | 3 |
|
4 | 4 | (function () { |
5 | | - function basicShapePolygonParse (input, element) { |
| 5 | + var isNumeric = internalScope.isNumeric; |
| 6 | + |
| 7 | + function roundToHundredth (number) { |
| 8 | + return Math.round(number * 100) / 100; |
| 9 | + } |
| 10 | + |
| 11 | + function basicShapePolygonParse (input) { |
6 | 12 | // TODO: Support the fill-rule option and % |
7 | 13 | var argumentList = input.split(','); |
8 | 14 | var coordinate = null; |
|
34 | 40 | return {type: 'path', path: path}; |
35 | 41 | } |
36 | 42 |
|
37 | | - function basicShapeCircleParse (input, element) { |
38 | | - // TODO: Need element as an argument to this function |
39 | | - var radius; |
40 | | - var position = /at (.*?)$/.exec(input); |
| 43 | + function getCirclePathPosition (parentProperties, position) { |
| 44 | + var analysedPosition = []; |
| 45 | + |
| 46 | + if (position !== null) { |
| 47 | + var positionList = position[1].split(/\s+/); |
| 48 | + |
| 49 | + // If only one value is specified, the second value is assumed to be 'center' |
| 50 | + // https://drafts.csswg.org/css-backgrounds-3/#position |
| 51 | + for (var index in positionList) { |
| 52 | + var aPosition = positionList[index]; |
| 53 | + |
| 54 | + var aPositionUnit = /(%|px)$/.exec(aPosition)[1]; |
| 55 | + if (aPositionUnit === null) { |
| 56 | + return null; |
| 57 | + } |
| 58 | + |
| 59 | + var aPositionValueString = aPosition.substring(0, aPosition.length - aPositionUnit.length); |
| 60 | + if (!isNumeric(aPositionValueString) || aPositionValueString === '') { |
| 61 | + return null; |
| 62 | + } |
| 63 | + |
| 64 | + var aPositionValue = Number(aPositionValueString); |
| 65 | + if (aPositionUnit === '%') { |
| 66 | + if (Number(index) === 0) { |
| 67 | + if (!parentProperties || !parentProperties.width) { |
| 68 | + return null; |
| 69 | + } |
| 70 | + aPositionValue *= (parentProperties.width / 100); |
| 71 | + } |
| 72 | + if (Number(index) === 1) { |
| 73 | + if (!parentProperties || !parentProperties.height) { |
| 74 | + return null; |
| 75 | + } |
| 76 | + aPositionValue *= (parentProperties.height / 100); |
| 77 | + } |
| 78 | + } |
| 79 | + analysedPosition[index] = aPositionValue; |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + if (analysedPosition.length < 2) { |
| 84 | + for (var i = analysedPosition.length; i < 2; i++) { |
| 85 | + if (Number(i) === 0) { |
| 86 | + if (!parentProperties || !parentProperties.width) { |
| 87 | + return null; |
| 88 | + } |
| 89 | + analysedPosition[i] = parentProperties.width / 2; |
| 90 | + } else if (Number(i) === 1) { |
| 91 | + if (!parentProperties || !parentProperties.height) { |
| 92 | + return null; |
| 93 | + } |
| 94 | + analysedPosition[i] = parentProperties.height / 2; |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + return analysedPosition; |
| 100 | + } |
41 | 101 |
|
42 | | - // TODO: Need to support other positions as currently this only supports positions in which both x and y are specified and are in px |
| 102 | + function getCirclePathRadius (parentProperties, position, input) { |
| 103 | + var radiusString; |
43 | 104 | if (position === null) { |
44 | | - // TODO: Set default position to the center of the reference box |
45 | | - position = '0px 0px'; |
46 | 105 | if (input !== '') { |
47 | | - radius = input; |
| 106 | + radiusString = input; |
48 | 107 | } |
49 | 108 | } else { |
50 | | - position = position[1].split(/\s+/); |
51 | | - radius = (/^(.*?) at/.exec(input)); |
52 | | - if (radius === null) { |
53 | | - radius = 'closest-side'; |
| 109 | + radiusString = (/^(.*?) at/.exec(input)); |
| 110 | + // TODO: Add support for when a radius had not been specified |
| 111 | + if (radiusString === null) { |
| 112 | + radiusString = 'closest-side'; |
54 | 113 | } else { |
55 | | - radius = radius[1]; |
| 114 | + radiusString = radiusString[1]; |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + var radiusUnit = /(%|px)$/.exec(radiusString); |
| 119 | + if (radiusUnit === null) { |
| 120 | + return null; |
| 121 | + } |
| 122 | + |
| 123 | + var radiusValueString = radiusString.substring(0, radiusString.length - radiusUnit[1].length); |
| 124 | + if (!isNumeric(radiusValueString)) { |
| 125 | + return null; |
| 126 | + } |
| 127 | + var radiusValue = Number(radiusValueString); |
| 128 | + |
| 129 | + if (radiusUnit[1] === '%') { |
| 130 | + var height = parentProperties.height; |
| 131 | + var width = parentProperties.width; |
| 132 | + if (!height || !width) { |
| 133 | + return null; |
56 | 134 | } |
| 135 | + |
| 136 | + return roundToHundredth((Math.sqrt(Math.pow(width, 2) + Math.pow(height, 2)) / Math.sqrt(2)) * radiusValue / 100); |
| 137 | + } |
| 138 | + |
| 139 | + return Number(radiusValueString); |
| 140 | + } |
| 141 | + |
| 142 | + function basicShapeCircleParse (input, element) { |
| 143 | + var parentProperties = null; |
| 144 | + if (element) { |
| 145 | + parentProperties = element.offsetParent ? element.offsetParent.getBoundingClientRect() : null; |
57 | 146 | } |
58 | 147 |
|
59 | | - radius = Number(radius.substring(0, radius.length - 2)); |
| 148 | + var position = /at (.*?)$/.exec(input); |
| 149 | + |
| 150 | + var radiusValue = getCirclePathRadius(parentProperties, position, input); |
| 151 | + if (!radiusValue) { |
| 152 | + return undefined; |
| 153 | + } |
60 | 154 |
|
61 | | - var positionX = Number(position[0].substring(0, position[0].length - 2)); |
62 | | - var positionY = Number(position[1].substring(0, position[1].length - 2)); |
| 155 | + var analysedPosition = getCirclePathPosition(parentProperties, position); |
| 156 | + if (!analysedPosition) { |
| 157 | + return undefined; |
| 158 | + } |
63 | 159 |
|
64 | | - var pathString = 'M ' + positionX + ' ' + positionY + |
65 | | - ' m 0,' + (-radius) + |
66 | | - ' a ' + radius + ',' + radius + ' 0 0,1 ' + radius + ',' + radius + |
67 | | - ' a ' + radius + ',' + radius + ' 0 1,1 ' + (-radius) + ',' + (-radius) + ' z'; |
| 160 | + var pathString = 'M ' + analysedPosition[0] + ' ' + analysedPosition[1] + |
| 161 | + ' m 0,' + (-radiusValue) + |
| 162 | + ' a ' + radiusValue + ',' + radiusValue + ' 0 0,1 ' + radiusValue + ',' + radiusValue + |
| 163 | + ' a ' + radiusValue + ',' + radiusValue + ' 0 1,1 ' + (-radiusValue) + ',' + (-radiusValue) + ' z'; |
68 | 164 |
|
69 | 165 | return {type: 'path', path: pathString}; |
70 | 166 | } |
|
150 | 246 | return undefined; |
151 | 247 | } |
152 | 248 | var toParse = [basicShapePolygonParse, basicShapeCircleParse, basicShapeInsetParse, basicShapeEllipseParse]; |
| 249 | + // var toParse = [basicShapeCircleParse]; |
153 | 250 | for (var i = 0; i < toParse.length; i++) { |
154 | 251 | var result = toParse[i](shapeArguments[1], element); |
155 | 252 | if (result) { |
|
212 | 309 |
|
213 | 310 | internalScope.offsetPathParse = offsetPathParse; |
214 | 311 | internalScope.offsetPathMerge = offsetPathMerge; |
| 312 | + internalScope.roundToHundredth = roundToHundredth; |
215 | 313 | })(); |
0 commit comments