UNPKG

@aurigma/design-atoms-model

Version:

Design Atoms is a part of Customer's Canvas SDK which allows for manipulating individual design elements through your code.

121 lines 5.02 kB
import { SizeF } from "../../Math/index"; import * as Convert from "../../Utils/Convert"; var MeasureKind; (function (MeasureKind) { MeasureKind[MeasureKind["Point"] = 0] = "Point"; MeasureKind[MeasureKind["Percent"] = 1] = "Percent"; MeasureKind[MeasureKind["Pixel"] = 2] = "Pixel"; })(MeasureKind || (MeasureKind = {})); var CornerRadiusConverter = /** @class */ (function () { function CornerRadiusConverter(rectangle, dpi) { this._rectangle = rectangle; this._dpi = dpi; } CornerRadiusConverter.prototype.convert = function (radiusString) { if (radiusString.indexOf("/") === -1) return this._normalize(this._combineRadiuses(this._getRadiuses(radiusString, true), this._getRadiuses(radiusString, false))); var parts = radiusString.split("/"); return this._normalize(this._combineRadiuses(this._getRadiuses(parts[0].trim(), true), this._getRadiuses(parts[1].trim(), false))); }; CornerRadiusConverter.prototype._normalize = function (radiuses) { if (radiuses == null) return radiuses; var r = radiuses; var scaleTop = (r[0].width + r[1].width) / this._rectangle.width; var scaleBottom = (r[2].width + r[3].width) / this._rectangle.width; var scaleLeft = (r[0].height + r[3].height) / this._rectangle.height; var scaleRight = (r[1].height + r[2].height) / this._rectangle.height; if (scaleTop > 1) { r[0].width = r[0].width / scaleTop; r[1].width = r[1].width / scaleTop; } if (scaleBottom > 1) { r[2].width = r[2].width / scaleBottom; r[3].width = r[3].width / scaleBottom; } if (scaleRight > 1) { r[1].height = r[1].height / scaleRight; r[2].height = r[2].height / scaleRight; } if (scaleLeft > 1) { r[3].height = r[3].height / scaleLeft; r[0].height = r[0].height / scaleLeft; } return radiuses; }; CornerRadiusConverter.prototype._combineRadiuses = function (r1, r2) { if (r1 == null || r1.length !== 4) return null; if (r2 == null || r2.length !== 4) return null; return [ new SizeF(r1[0], r2[0]), new SizeF(r1[1], r2[1]), new SizeF(r1[2], r2[2]), new SizeF(r1[3], r2[3]) ]; }; CornerRadiusConverter.prototype._getRadiuses = function (numbers, isWidth) { if (numbers == null) return null; var numberParts = numbers.split(" ").filter(function (v) { return v != null && v.length > 0; }); if (numberParts.length < 1) return null; if (numberParts.length > 4) return null; var results = []; for (var i = 0; i < numberParts.length; i++) { var result = this._tryParseNumberPart(numberParts[i]); if (result != null) { results.push(this._toPoints(result, isWidth)); } else return null; } switch (results.length) { case 1: return [results[0], results[0], results[0], results[0]]; case 2: return [results[0], results[1], results[0], results[1]]; case 3: return [results[0], results[1], results[2], results[1]]; default: return results; } }; CornerRadiusConverter.prototype._tryParseNumberPart = function (numberString) { if (numberString.endsWith("%")) { var floatValue_1 = parseFloat(numberString); if (!isNaN(floatValue_1)) { return { value: floatValue_1, measureType: MeasureKind.Percent }; } return null; } if (numberString.toLowerCase().endsWith("px")) { var floatValue_2 = parseFloat(numberString); if (!isNaN(floatValue_2)) { return { value: floatValue_2, measureType: MeasureKind.Pixel }; } return null; } var floatValue = parseFloat(numberString); if (!isNaN(floatValue)) { return { value: floatValue, measureType: MeasureKind.Point }; } return null; }; CornerRadiusConverter.prototype._toPoints = function (item, isWidth) { switch (item.measureType) { case MeasureKind.Percent: var fullLength = isWidth ? this._rectangle.width : this._rectangle.height; return fullLength * item.value / 100; case MeasureKind.Pixel: return Convert.pixelsToPoints(item.value, this._dpi); default: return item.value; } }; return CornerRadiusConverter; }()); export { CornerRadiusConverter }; //# sourceMappingURL=CornerRadiusConverter.js.map