fonteditor-core
Version:
fonts (ttf, woff, woff2, eot, svg, otf) parse, write, transform, glyph adjust.
104 lines (86 loc) • 3 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _computeBoundingBox = require("./computeBoundingBox");
var _pathAdjust = _interopRequireDefault(require("./pathAdjust"));
var _pathRotate = _interopRequireDefault(require("./pathRotate"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); }
function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } }
/**
* 翻转路径
*
* @param {Array} paths 路径数组
* @param {number} xScale x翻转
* @param {number} yScale y翻转
* @return {Array} 变换后的路径
*/
function mirrorPaths(paths, xScale, yScale) {
var _computePath = _computeBoundingBox.computePath.apply(void 0, _toConsumableArray(paths)),
x = _computePath.x,
y = _computePath.y,
width = _computePath.width,
height = _computePath.height;
if (xScale === -1) {
paths.forEach(function (p) {
(0, _pathAdjust["default"])(p, -1, 1, -x, 0);
(0, _pathAdjust["default"])(p, 1, 1, x + width, 0);
p.reverse();
});
}
if (yScale === -1) {
paths.forEach(function (p) {
(0, _pathAdjust["default"])(p, 1, -1, 0, -y);
(0, _pathAdjust["default"])(p, 1, 1, 0, y + height);
p.reverse();
});
}
return paths;
}
var _default = {
/**
* 旋转路径
*
* @param {Array} paths 路径数组
* @param {number} angle 弧度
* @return {Array} 变换后的路径
*/
rotate: function rotate(paths, angle) {
if (!angle) {
return paths;
}
var bound = _computeBoundingBox.computePath.apply(void 0, _toConsumableArray(paths));
var cx = bound.x + bound.width / 2;
var cy = bound.y + bound.height / 2;
paths.forEach(function (p) {
(0, _pathRotate["default"])(p, angle, cx, cy);
});
return paths;
},
/**
* 路径组变换
*
* @param {Array} paths 路径数组
* @param {number} x x 方向缩放
* @param {number} y y 方向缩放
* @return {Array} 变换后的路径
*/
move: function move(paths, x, y) {
var bound = _computeBoundingBox.computePath.apply(void 0, _toConsumableArray(paths));
paths.forEach(function (path) {
(0, _pathAdjust["default"])(path, 1, 1, x - bound.x, y - bound.y);
});
return paths;
},
mirror: function mirror(paths) {
return mirrorPaths(paths, -1, 1);
},
flip: function flip(paths) {
return mirrorPaths(paths, 1, -1);
}
};
exports["default"] = _default;