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.

119 lines 4.63 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 = {})); export class CornerRadiusConverter { constructor(rectangle, dpi) { this._rectangle = rectangle; this._dpi = dpi; } convert(radiusString) { if (radiusString.indexOf("/") === -1) return this._normalize(this._combineRadiuses(this._getRadiuses(radiusString, true), this._getRadiuses(radiusString, false))); const parts = radiusString.split("/"); return this._normalize(this._combineRadiuses(this._getRadiuses(parts[0].trim(), true), this._getRadiuses(parts[1].trim(), false))); } _normalize(radiuses) { if (radiuses == null) return radiuses; const r = radiuses; const scaleTop = (r[0].width + r[1].width) / this._rectangle.width; const scaleBottom = (r[2].width + r[3].width) / this._rectangle.width; const scaleLeft = (r[0].height + r[3].height) / this._rectangle.height; const 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; } _combineRadiuses(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]) ]; } _getRadiuses(numbers, isWidth) { if (numbers == null) return null; const numberParts = numbers.split(" ").filter(v => v != null && v.length > 0); if (numberParts.length < 1) return null; if (numberParts.length > 4) return null; const results = []; for (let i = 0; i < numberParts.length; i++) { const 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; } } _tryParseNumberPart(numberString) { if (numberString.endsWith("%")) { const floatValue = parseFloat(numberString); if (!isNaN(floatValue)) { return { value: floatValue, measureType: MeasureKind.Percent }; } return null; } if (numberString.toLowerCase().endsWith("px")) { const floatValue = parseFloat(numberString); if (!isNaN(floatValue)) { return { value: floatValue, measureType: MeasureKind.Pixel }; } return null; } const floatValue = parseFloat(numberString); if (!isNaN(floatValue)) { return { value: floatValue, measureType: MeasureKind.Point }; } return null; } _toPoints(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; } } } //# sourceMappingURL=CornerRadiusConverter.js.map