From 275784293562fd0778936b54fb906ec5c8c09742 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9B=D0=B0=D0=B2=D1=80=D1=91=D0=BD=D0=BE=D0=B2=20=D0=90?= =?UTF-8?q?=D0=BD=D1=82=D0=BE=D0=BD?= Date: Thu, 27 Feb 2014 08:19:29 +0800 Subject: [PATCH] fix #670 Some lint error resolve --- src/plugins/TextPath.js | 45 ++++++++++++++++++++++------------- src/shapes/Text.js | 4 +++- test/unit/shapes/Text-test.js | 15 ++++++++++++ 3 files changed, 47 insertions(+), 17 deletions(-) diff --git a/src/plugins/TextPath.js b/src/plugins/TextPath.js index b736a677..01019549 100644 --- a/src/plugins/TextPath.js +++ b/src/plugins/TextPath.js @@ -1,6 +1,6 @@ (function() { var EMPTY_STRING = '', - CALIBRI = 'Calibri', + //CALIBRI = 'Calibri', NORMAL = 'normal'; /** @@ -176,7 +176,7 @@ return {}; }; - var findSegmentToFitCharacter = function(c, before) { + var findSegmentToFitCharacter = function(c) { var glyphWidth = that._getTextSize(c).width; @@ -196,8 +196,9 @@ } } - if(pathCmd === {} || p0 === undefined) + if(pathCmd === {} || p0 === undefined) { return undefined; + } var needNewSegment = false; @@ -206,8 +207,9 @@ if(Kinetic.Path.getLineLength(p0.x, p0.y, pathCmd.points[0], pathCmd.points[1]) > glyphWidth) { p1 = Kinetic.Path.getPointOnLine(glyphWidth, p0.x, p0.y, pathCmd.points[0], pathCmd.points[1], p0.x, p0.y); } - else + else { pathCmd = undefined; + } break; case 'A': @@ -217,13 +219,16 @@ // 5 = dTheta var end = pathCmd.points[4] + dTheta; - if(currentT === 0) + if(currentT === 0){ currentT = start + 0.00000001; + } // Just in case start is 0 - else if(glyphWidth > currLen) + else if(glyphWidth > currLen) { currentT += (Math.PI / 180.0) * dTheta / Math.abs(dTheta); - else + } + else { currentT -= Math.PI / 360.0 * dTheta / Math.abs(dTheta); + } // Credit for bug fix: @therth https://github.com/ericdrowell/KineticJS/issues/249 // Old code failed to render text along arc of this path: "M 50 50 a 150 50 0 0 1 250 50 l 50 0" @@ -235,15 +240,19 @@ break; case 'C': if(currentT === 0) { - if(glyphWidth > pathCmd.pathLength) + if(glyphWidth > pathCmd.pathLength) { currentT = 0.00000001; - else + } + else { currentT = glyphWidth / pathCmd.pathLength; + } } - else if(glyphWidth > currLen) + else if(glyphWidth > currLen) { currentT += (glyphWidth - currLen) / pathCmd.pathLength; - else + } + else { currentT -= (currLen - glyphWidth) / pathCmd.pathLength; + } if(currentT > 1.0) { currentT = 1.0; @@ -252,12 +261,15 @@ p1 = Kinetic.Path.getPointOnCubicBezier(currentT, pathCmd.start.x, pathCmd.start.y, pathCmd.points[0], pathCmd.points[1], pathCmd.points[2], pathCmd.points[3], pathCmd.points[4], pathCmd.points[5]); break; case 'Q': - if(currentT === 0) + if(currentT === 0) { currentT = glyphWidth / pathCmd.pathLength; - else if(glyphWidth > currLen) + } + else if(glyphWidth > currLen) { currentT += (glyphWidth - currLen) / pathCmd.pathLength; - else + } + else { currentT -= (currLen - glyphWidth) / pathCmd.pathLength; + } if(currentT > 1.0) { currentT = 1.0; @@ -283,8 +295,9 @@ // Find p1 such that line segment between p0 and p1 is approx. width of glyph findSegmentToFitCharacter(charArr[i]); - if(p0 === undefined || p1 === undefined) + if(p0 === undefined || p1 === undefined) { break; + } var width = Kinetic.Path.getLineLength(p0.x, p0.y, p1.x, p1.y); @@ -376,5 +389,5 @@ * @memberof Kinetic.TextPath.prototype */ - Kinetic.Collection.mapMethods(Kinetic.TextPath); + Kinetic.Collection.mapMethods(Kinetic.TextPath); })(); diff --git a/src/shapes/Text.js b/src/shapes/Text.js index 2b4ddf1d..98864a16 100644 --- a/src/shapes/Text.js +++ b/src/shapes/Text.js @@ -267,6 +267,7 @@ } } this._addTextLine(match, matchWidth); + textWidth = Math.max(textWidth, matchWidth); currentHeightPx += lineHeightPx; if (!shouldWrap || (fixedHeight && currentHeightPx + lineHeightPx > maxHeightPx)) { @@ -284,6 +285,7 @@ // if it does, add the line and break out of the loop this._addTextLine(line, lineWidth); currentHeightPx += lineHeightPx; + textWidth = Math.max(textWidth, lineWidth); break; } } @@ -451,5 +453,5 @@ * text.text('Hello world!'); */ - Kinetic.Collection.mapMethods(Kinetic.Text); + Kinetic.Collection.mapMethods(Kinetic.Text); })(); diff --git a/test/unit/shapes/Text-test.js b/test/unit/shapes/Text-test.js index ce4fe929..61888b9b 100644 --- a/test/unit/shapes/Text-test.js +++ b/test/unit/shapes/Text-test.js @@ -275,4 +275,19 @@ suite('Text', function(){ assert(text.getHeight() > height, 'height should have increased'); }); + test('get text width', function() { + var stage = addStage(); + var layer = new Kinetic.Layer(); + stage.add(layer); + var text = new Kinetic.Text({ + text : 'hello asd fasdf asdf asd fasdf asdfasd fa sds helloo', + fill : 'black', + width: 100 + }); + + layer.add(text); + layer.draw(); + assert.equal(text.getTextWidth() > 0 && text.getTextWidth() < 100, true); + + }); }); \ No newline at end of file