UNPKG

@visactor/vrender-components

Version:

components library for dp visualization

93 lines (83 loc) 4.83 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: !0 }), exports.clampText = exports.defaultLabelPosition = exports.DefaultRectPositions = exports.DefaultPositions = exports.place = exports.placeToCandidates = exports.canPlaceInside = exports.canPlace = void 0; const vutils_1 = require("@visactor/vutils"), scaler_1 = require("./scaler"); function canPlace($, bitmap, bound, checkBound = !0, pad = 0) { let range = bound; pad > 0 && (range = { x1: bound.x1 - pad, x2: bound.x2 + pad, y1: bound.y1 - pad, y2: bound.y2 + pad }), range = (0, scaler_1.boundToRange)($, range); const outOfBounds = bitmap.outOfBounds(range); return (!checkBound || !outOfBounds) && (outOfBounds && (range = (0, scaler_1.clampRangeByBitmap)($, range)), !bitmap.getRange(range)); } function canPlaceInside(textBound, shapeBound) { return !(!textBound || !shapeBound) && shapeBound.encloses(textBound); } function placeToCandidates($, bitmap, text, candidates = [], clampForce = !0, pad = 0, changePosition = !1) { var _a, _b, _c, _d; const validCandidates = candidates.filter((candidate => (0, vutils_1.isValid)(candidate))), curX = null !== (_a = text.attribute.x) && void 0 !== _a ? _a : 0, curY = null !== (_b = text.attribute.y) && void 0 !== _b ? _b : 0, base = text.AABBBounds, candidateBounds = { x1: base.x1, x2: base.x2, y1: base.y1, y2: base.y2 }; let lastCandidate; for (let i = 0; i < validCandidates.length; i++) { const c = validCandidates[i]; lastCandidate = c; const dx = (null !== (_c = c.x) && void 0 !== _c ? _c : curX) - curX, dy = (null !== (_d = c.y) && void 0 !== _d ? _d : curY) - curY; if (candidateBounds.x1 = base.x1 + dx, candidateBounds.x2 = base.x2 + dx, candidateBounds.y1 = base.y1 + dy, candidateBounds.y2 = base.y2 + dy, canPlace($, bitmap, candidateBounds, clampForce, pad)) return bitmap.setRange((0, scaler_1.boundToRange)($, candidateBounds, !0)), c; } return changePosition && lastCandidate && text.setAttributes(lastCandidate), !1; } function place($, bitmap, s, attrs, text, bounds, labeling) { var _a, _b; const clampForce = null === (_a = attrs.overlap) || void 0 === _a ? void 0 : _a.clampForce, overlapPadding = null === (_b = attrs.overlap) || void 0 === _b ? void 0 : _b.overlapPadding; if ("bound" === s.type || "position" === s.type) { if ((0, vutils_1.isFunction)(labeling)) { const candidates = (((0, vutils_1.isFunction)(s.position) ? s.position(text.attribute) : s.position) || defaultLabelPosition(attrs.type)).map((p => labeling(text.AABBBounds, bounds, p, attrs.offset))), shouldClone = !1 === s.restorePosition; return placeToCandidates($, bitmap, text, candidates, clampForce, overlapPadding, shouldClone); } return !1; } if ("moveY" === s.type) { const candidates = (s.offset ? (0, vutils_1.isFunction)(s.offset) ? s.offset(text.attribute) : s.offset : []).map((dy => ({ x: text.attribute.x, y: text.attribute.y + dy }))); return placeToCandidates($, bitmap, text, candidates, clampForce, overlapPadding); } if ("moveX" === s.type) { const candidates = (s.offset ? (0, vutils_1.isFunction)(s.offset) ? s.offset(text.attribute) : s.offset : []).map((dx => ({ x: text.attribute.x + dx, y: text.attribute.y }))); return placeToCandidates($, bitmap, text, candidates, clampForce, overlapPadding); } return !1; } function defaultLabelPosition(type) { return "rect" === type ? exports.DefaultRectPositions : exports.DefaultPositions; } function clampText(text, width, height, padding = {}) { const {x1: x1, x2: x2, y1: y1, y2: y2} = text.AABBBounds, {top: top = 0, left: left = 0, right: right = 0, bottom: bottom = 0} = padding, minX = Math.min(x1, x2), maxX = Math.max(x1, x2), minY = Math.min(y1, y2), maxY = Math.max(y1, y2), maxXWithPadding = width + right, maxYWithPadding = height + bottom; let dx = 0, dy = 0; return minX < 0 - left ? dx = -minX : maxX > maxXWithPadding && (dx = maxXWithPadding - maxX), minY < 0 - top ? dy = -minY : maxY > maxYWithPadding && (dy = maxYWithPadding - maxY), { dx: dx, dy: dy }; } exports.canPlace = canPlace, exports.canPlaceInside = canPlaceInside, exports.placeToCandidates = placeToCandidates, exports.place = place, exports.DefaultPositions = [ "top", "bottom", "right", "left", "top-right", "bottom-right", "top-left", "bottom-left" ], exports.DefaultRectPositions = [ "top", "inside-top", "inside" ], exports.defaultLabelPosition = defaultLabelPosition, exports.clampText = clampText; //# sourceMappingURL=place.js.map