diff --git a/src/editor/Editor.js b/src/editor/Editor.js index 0a83240..af5985f 100644 --- a/src/editor/Editor.js +++ b/src/editor/Editor.js @@ -163,7 +163,7 @@ define( */ Editor.prototype.setFont = function(font) { - var paths = glyf2path(font); + var contours = font.contours; var width = this.render.painter.width; var height = this.render.painter.height; @@ -174,13 +174,11 @@ define( var offsetY = (height + (options.unitsPerEm + options.metrics.WinDecent)) / 2; // 构造形状集合 - var shapes = paths.map(function(path) { + var shapes = contours.map(function(path) { var shape = {}; var bound = computeBoundingBox.computePath(path); - path = pathAdjust(path, 1, -1); shape.points = pathAdjust(path, 1, 1, offsetX, offsetY); - return shape; }); diff --git a/src/editor/group/ShapeGroup.js b/src/editor/group/ShapeGroup.js index 9c432b0..ac81d1c 100644 --- a/src/editor/group/ShapeGroup.js +++ b/src/editor/group/ShapeGroup.js @@ -14,6 +14,7 @@ define( var boundAdjust = require('render/util/boundAdjust'); var lang = require('common/lang'); var computeBoundingBox = require('../../graphics/computeBoundingBox'); + var getTransformMatrix = require('../util/getTransformMatrix'); /** * 获取bound边界 @@ -94,70 +95,10 @@ define( */ Group.prototype.transform = function(point, camera) { - var bound = this.bound; - // x, y, xscale 相对符号, yscale 相对符号 - var matrix = [ - 0, - 0, - 1, - 1 - ]; - - // 是否需要等比例缩放 - var ctrlKey = camera.event && camera.event.ctrlKey; - - switch (point.pos) { - case 1: - matrix[0] = bound.x + bound.width; - matrix[1] = bound.y + bound.height; - matrix[2] = -(camera.x - matrix[0]) / bound.width; - matrix[3] = -(camera.y - matrix[1]) / bound.height; - break; - case 2: - matrix[0] = bound.x; - matrix[1] = bound.y + bound.height; - matrix[2] = (camera.x - matrix[0]) / bound.width; - matrix[3] = -(camera.y - matrix[1]) / bound.height; - break; - - case 3: - matrix[0] = bound.x; - matrix[1] = bound.y; - matrix[2] = (camera.x - matrix[0]) / bound.width; - matrix[3] = (camera.y - matrix[1]) / bound.height; - break; - - case 4: - matrix[0] = bound.x + bound.width; - matrix[1] = bound.y; - matrix[2] = -(camera.x - matrix[0]) / bound.width; - matrix[3] = (camera.y - matrix[1]) / bound.height; - break; - - case 5: - matrix[1] = bound.y + bound.height; - matrix[2] = 1; - matrix[3] = -(camera.y - matrix[1]) / bound.height; - break; - - case 7: - matrix[1] = bound.y; - matrix[3] = (camera.y - matrix[1]) / bound.height; - break; - - case 6: - matrix[0] = bound.x; - matrix[2] = (camera.x - matrix[0]) / bound.width; - break; - - case 8: - matrix[0] = bound.x + bound.width; - matrix[2] = -(camera.x - matrix[0]) / bound.width; - break; - }; + var matrix = getTransformMatrix(point.pos, this.bound, camera); // 等比缩放 - if (camera.event.ctrlKey && [1, 2, 3, 4].indexOf(point.pos) >= 0) { + if (camera.event.shiftKey && [1, 2, 3, 4].indexOf(point.pos) >= 0) { var scale = Math.max(Math.abs(matrix[2]), Math.abs(matrix[3])); matrix[2] = matrix[2] >= 0 ? scale : -scale; matrix[3] = matrix[3] >= 0 ? scale : -scale; diff --git a/src/editor/mode/point.js b/src/editor/mode/point.js index 8d9c2d8..ae33f23 100644 --- a/src/editor/mode/point.js +++ b/src/editor/mode/point.js @@ -10,7 +10,6 @@ define( function(require) { - var pathIterator = require('render/util/pathIterator'); var computeBoundingBox = require('graphics/computeBoundingBox'); var pathAdjust = require('render/util/pathAdjust'); var lang = require('common/lang'); @@ -85,32 +84,15 @@ define( var shapes = this.render.getLayer('font').shapes; shapes.forEach(function(shape) { - pathIterator(shape.points, function(c, i, p0, p1, p2) { - if(c == 'M' || c == 'L') { - controls.push({ - type: 'point', - x: p0.x, - y: p0.y, - _point: p0, - _shape: shape.id - }); - } - else if (c == 'Q') { - controls.push({ - type: 'cpoint', - x: p1.x, - y: p1.y, - _point: p1, - _shape: shape.id - }); - controls.push({ - type: 'point', - x: p2.x, - y: p2.y, - _point: p2, - _shape: shape.id - }); - } + + shape.points.forEach(function(p) { + controls.push({ + type: p.onCurve ? 'point' : 'cpoint', + x: p.x, + y: p.y, + _point: p, + _shape: shape.id + }); }); }); diff --git a/src/editor/util/getTransformMatrix.js b/src/editor/util/getTransformMatrix.js new file mode 100644 index 0000000..fabfffb --- /dev/null +++ b/src/editor/util/getTransformMatrix.js @@ -0,0 +1,88 @@ +/** + * @file getTransformMatrix.js + * @author mengke01 + * @date + * @description + * 获得变换的矩阵 + */ + + +define( + function(require) { + + + /** + * 获得变换矩阵 + * + * @param {number} pos 变换位置 + * @param {Object} bound 边界 + * @param {Object} camera 镜头对象 + * @return {Array} 变换矩阵,x,y,xScale,yScale + */ + function getTransformMatrix(pos, bound, camera) { + + // x, y, xscale 相对符号, yscale 相对符号 + var matrix = [ + 0, + 0, + 1, + 1 + ]; + + switch (pos) { + case 1: + matrix[0] = bound.x + bound.width; + matrix[1] = bound.y + bound.height; + matrix[2] = -(camera.x - matrix[0]) / bound.width; + matrix[3] = -(camera.y - matrix[1]) / bound.height; + break; + case 2: + matrix[0] = bound.x; + matrix[1] = bound.y + bound.height; + matrix[2] = (camera.x - matrix[0]) / bound.width; + matrix[3] = -(camera.y - matrix[1]) / bound.height; + break; + + case 3: + matrix[0] = bound.x; + matrix[1] = bound.y; + matrix[2] = (camera.x - matrix[0]) / bound.width; + matrix[3] = (camera.y - matrix[1]) / bound.height; + break; + + case 4: + matrix[0] = bound.x + bound.width; + matrix[1] = bound.y; + matrix[2] = -(camera.x - matrix[0]) / bound.width; + matrix[3] = (camera.y - matrix[1]) / bound.height; + break; + + case 5: + matrix[1] = bound.y + bound.height; + matrix[2] = 1; + matrix[3] = -(camera.y - matrix[1]) / bound.height; + break; + + case 7: + matrix[1] = bound.y; + matrix[3] = (camera.y - matrix[1]) / bound.height; + break; + + case 6: + matrix[0] = bound.x; + matrix[2] = (camera.x - matrix[0]) / bound.width; + break; + + case 8: + matrix[0] = bound.x + bound.width; + matrix[2] = -(camera.x - matrix[0]) / bound.width; + break; + }; + + return matrix; + } + + + return getTransformMatrix; + } +);