mirror of
https://github.com/skishore/makemeahanzi.git
synced 2025-11-01 20:27:44 +08:00
Make split_and_orient_path robust to edge cases
This commit is contained in:
@ -17,13 +17,24 @@ Glyphs.get_svg_path = function(glyph) {
|
|||||||
|
|
||||||
// Error out if the condition does not hold.
|
// Error out if the condition does not hold.
|
||||||
function assert(condition, message) {
|
function assert(condition, message) {
|
||||||
if (!condition) throw new Error(message);
|
if (!condition) {
|
||||||
|
console.error(message);
|
||||||
|
throw new Error;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function clone(point) {
|
function clone(point) {
|
||||||
return [point[0], point[1]];
|
return [point[0], point[1]];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function equal(point1, point2) {
|
||||||
|
return point1[0] === point2[0] && point1[1] === point2[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
function valid(point) {
|
||||||
|
return point[0] !== undefined && point[1] !== undefined;
|
||||||
|
}
|
||||||
|
|
||||||
// Takes a non-empty list of SVG commands that may contain multiple contours.
|
// Takes a non-empty list of SVG commands that may contain multiple contours.
|
||||||
// Returns a list of lists of path segment objects that each form one contour.
|
// Returns a list of lists of path segment objects that each form one contour.
|
||||||
// Each path segment has three keys: start, end, and control.
|
// Each path segment has three keys: start, end, and control.
|
||||||
@ -35,6 +46,7 @@ function split_path(path) {
|
|||||||
var result = [[]];
|
var result = [[]];
|
||||||
var start = [path[0].x, path[0].y];
|
var start = [path[0].x, path[0].y];
|
||||||
var current = clone(start);
|
var current = clone(start);
|
||||||
|
assert(valid(current));
|
||||||
|
|
||||||
for (var i = 1; i < path.length; i++) {
|
for (var i = 1; i < path.length; i++) {
|
||||||
var command = path[i];
|
var command = path[i];
|
||||||
@ -45,16 +57,27 @@ function split_path(path) {
|
|||||||
assert(i === path.length - 1, 'Path ended early!');
|
assert(i === path.length - 1, 'Path ended early!');
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
var start = {x: command.x, y: command.y};
|
var start = [command.x, command.y];
|
||||||
var current = {x: start_point.x, y: start_point.y};
|
var current = clone(start);
|
||||||
|
assert(valid(current));
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
assert(command.type === 'Q', 'Got unexpected TTF command: ' + command.type);
|
assert(command.type === 'Q' || command.type === 'L',
|
||||||
|
'Got unexpected TTF command: ' + command.type);
|
||||||
var segment = {
|
var segment = {
|
||||||
'start': clone(current),
|
'start': clone(current),
|
||||||
'end': [command.x, command.y],
|
'end': [command.x, command.y],
|
||||||
'control': [command.x1, command.y1],
|
'control': [command.x1, command.y1],
|
||||||
};
|
};
|
||||||
|
assert(valid(segment.end));
|
||||||
|
if (equal(segment.start, segment.end)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (!valid(segment.control) ||
|
||||||
|
equal(segment.start, segment.control) ||
|
||||||
|
equal(segment.end, segment.control)) {
|
||||||
|
delete segment.control;
|
||||||
|
}
|
||||||
result[result.length - 1].push(segment);
|
result[result.length - 1].push(segment);
|
||||||
current = clone(segment.end);
|
current = clone(segment.end);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user