add path split by segment
This commit is contained in:
@@ -6,6 +6,7 @@
|
||||
define(function (require) {
|
||||
var contours2svg = require('fonteditor-core/ttf/util/contours2svg');
|
||||
var path2contours = require('fonteditor-core/ttf/svg/path2contours');
|
||||
var reducePath = require('./reducePath');
|
||||
var paper = require('paper');
|
||||
|
||||
var RELATION = {
|
||||
@@ -65,6 +66,12 @@ define(function (require) {
|
||||
|
||||
var contours = resultPath ? getContours(resultPath) : paths;
|
||||
paper.clear();
|
||||
|
||||
// 清除重复的冗余节点
|
||||
for (var i = 0, l = contours.length; i < l; i++) {
|
||||
contours[i] = reducePath(contours[i]);
|
||||
}
|
||||
|
||||
return contours;
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,31 @@
|
||||
|
||||
define(
|
||||
function (require) {
|
||||
var path2contours = require('fonteditor-core/ttf/svg/path2contours');
|
||||
var contours2svg = require('fonteditor-core/ttf/util/contours2svg');
|
||||
|
||||
var reducePath = require('./reducePath');
|
||||
var paper = require('paper');
|
||||
|
||||
|
||||
|
||||
function initPaper() {
|
||||
if (!paper.project) {
|
||||
paper.setup(document.createElement('canvas'));
|
||||
}
|
||||
}
|
||||
|
||||
function getPath(contours) {
|
||||
// 根据contours获取paper.Path对象
|
||||
var d = contours2svg(contours);
|
||||
return new paper.CompoundPath(d);
|
||||
}
|
||||
|
||||
function getContours(path) {
|
||||
// 根据path获取contours数组
|
||||
var d = path.getPathData();
|
||||
return path2contours(d);
|
||||
}
|
||||
|
||||
/**
|
||||
* 线段切割路径
|
||||
@@ -15,7 +40,35 @@ define(
|
||||
* @return {Array|false} 切割后路径或者false
|
||||
*/
|
||||
function pathSplitBySegment(paths, p0, p1) {
|
||||
return paths;
|
||||
initPaper();
|
||||
var subject = getPath(paths);
|
||||
var clipper = new paper.Path({
|
||||
segments: [
|
||||
[p0.x, p0.y],
|
||||
[p1.x, p1.y]
|
||||
],
|
||||
closed: false
|
||||
});
|
||||
|
||||
var intersections = subject.getIntersections(clipper); // 分割
|
||||
if (!intersections || !intersections.length) {
|
||||
return paths;
|
||||
}
|
||||
// 根据交点分割路径
|
||||
intersections.forEach(function (spliter) {
|
||||
var result = spliter.split();
|
||||
});
|
||||
// 闭合路径
|
||||
subject.children.forEach(function (path) {
|
||||
path.segments[0].clearHandles();
|
||||
path.segments[path.segments.length - 1].clearHandles();
|
||||
path.closePath();
|
||||
});
|
||||
subject.resolveCrossings().reorient();
|
||||
var contours = getContours(subject) ;
|
||||
paper.clear();
|
||||
|
||||
return contours;
|
||||
}
|
||||
|
||||
return pathSplitBySegment;
|
||||
|
||||
Reference in New Issue
Block a user