CCLabelTTF.js 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225
  1. /****************************************************************************
  2. Copyright (c) 2008-2010 Ricardo Quesada
  3. Copyright (c) 2011-2012 cocos2d-x.org
  4. Copyright (c) 2013-2014 Chukong Technologies Inc.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. /**
  23. * <p>cc.LabelTTF is a subclass of cc.TextureNode that knows how to render text labels with system font or a ttf font file<br/>
  24. * All features from cc.Sprite are valid in cc.LabelTTF<br/>
  25. * cc.LabelTTF objects are slow for js-binding on mobile devices.<br/>
  26. * Consider using cc.LabelAtlas or cc.LabelBMFont instead.<br/>
  27. * You can create a cc.LabelTTF from a font name, alignment, dimension and font size or a cc.FontDefinition object.</p>
  28. * @class
  29. * @extends cc.Sprite
  30. *
  31. * @param {String} text
  32. * @param {String|cc.FontDefinition} [fontName="Arial"]
  33. * @param {Number} [fontSize=16]
  34. * @param {cc.Size} [dimensions=cc.size(0,0)]
  35. * @param {Number} [hAlignment=cc.TEXT_ALIGNMENT_LEFT]
  36. * @param {Number} [vAlignment=cc.VERTICAL_TEXT_ALIGNMENT_TOP]
  37. * @example
  38. * var myLabel = new cc.LabelTTF('label text', 'Times New Roman', 32, cc.size(320,32), cc.TEXT_ALIGNMENT_LEFT);
  39. *
  40. * var fontDef = new cc.FontDefinition();
  41. * fontDef.fontName = "Arial";
  42. * fontDef.fontSize = "32";
  43. * var myLabel = new cc.LabelTTF('label text', fontDef);
  44. *
  45. * @property {String} string - Content string of label
  46. * @property {Number} textAlign - Horizontal Alignment of label: cc.TEXT_ALIGNMENT_LEFT|cc.TEXT_ALIGNMENT_CENTER|cc.TEXT_ALIGNMENT_RIGHT
  47. * @property {Number} verticalAlign - Vertical Alignment of label: cc.VERTICAL_TEXT_ALIGNMENT_TOP|cc.VERTICAL_TEXT_ALIGNMENT_CENTER|cc.VERTICAL_TEXT_ALIGNMENT_BOTTOM
  48. * @property {Number} fontSize - Font size of label
  49. * @property {String} fontName - Font name of label
  50. * @property {String} font - The label font with a style string: e.g. "18px Verdana"
  51. * @property {Number} boundingWidth - Width of the bounding box of label, the real content width is limited by boundingWidth
  52. * @property {Number} boundingHeight - Height of the bounding box of label, the real content height is limited by boundingHeight
  53. * @property {cc.Color} fillStyle - The fill color
  54. * @property {cc.Color} strokeStyle - The stroke color
  55. * @property {Number} lineWidth - The line width for stroke
  56. * @property {Number} shadowOffsetX - The x axis offset of shadow
  57. * @property {Number} shadowOffsetY - The y axis offset of shadow
  58. * @property {Number} shadowOpacity - The opacity of shadow
  59. * @property {Number} shadowBlur - The blur size of shadow
  60. */
  61. cc.LabelTTF = cc.Sprite.extend(/** @lends cc.LabelTTF# */{
  62. _dimensions: null,
  63. _hAlignment: cc.TEXT_ALIGNMENT_CENTER,
  64. _vAlignment: cc.VERTICAL_TEXT_ALIGNMENT_TOP,
  65. _fontName: null,
  66. _fontSize: 0.0,
  67. _string: "",
  68. _originalText: null,
  69. _isMultiLine: false,
  70. _fontStyleStr: null,
  71. // font shadow
  72. _shadowEnabled: false,
  73. _shadowOffset: null,
  74. _shadowOpacity: 0,
  75. _shadowBlur: 0,
  76. _shadowColorStr: null,
  77. // font stroke
  78. _strokeEnabled: false,
  79. _strokeColor: null,
  80. _strokeSize: 0,
  81. _strokeColorStr: null,
  82. // font tint
  83. _textFillColor: null,
  84. _fillColorStr: null,
  85. _strokeShadowOffsetX: 0,
  86. _strokeShadowOffsetY: 0,
  87. _needUpdateTexture: false,
  88. _labelCanvas: null,
  89. _labelContext: null,
  90. _lineWidths: null,
  91. _className: "LabelTTF",
  92. /**
  93. * Initializes the cc.LabelTTF with a font name, alignment, dimension and font size, do not call it by yourself, you should pass the correct arguments in constructor to initialize the label.
  94. * @param {String} label string
  95. * @param {String} fontName
  96. * @param {Number} fontSize
  97. * @param {cc.Size} [dimensions=]
  98. * @param {Number} [hAlignment=]
  99. * @param {Number} [vAlignment=]
  100. * @return {Boolean} return false on error
  101. */
  102. initWithString: function (label, fontName, fontSize, dimensions, hAlignment, vAlignment) {
  103. var strInfo;
  104. if (label)
  105. strInfo = label + "";
  106. else
  107. strInfo = "";
  108. fontSize = fontSize || 16;
  109. dimensions = dimensions || cc.size(0, 0/*fontSize*/);
  110. hAlignment = hAlignment || cc.TEXT_ALIGNMENT_LEFT;
  111. vAlignment = vAlignment || cc.VERTICAL_TEXT_ALIGNMENT_TOP;
  112. this._opacityModifyRGB = false;
  113. this._dimensions = cc.size(dimensions.width, dimensions.height);
  114. this._fontName = fontName || "Arial";
  115. this._hAlignment = hAlignment;
  116. this._vAlignment = vAlignment;
  117. //this._fontSize = (cc._renderType === cc._RENDER_TYPE_CANVAS) ? fontSize : fontSize * cc.contentScaleFactor();
  118. this._fontSize = fontSize;
  119. this._fontStyleStr = this._fontSize + "px '" + fontName + "'";
  120. this._fontClientHeight = cc.LabelTTF.__getFontHeightByDiv(fontName, this._fontSize);
  121. this.string = strInfo;
  122. this._setColorsString();
  123. this._updateTexture();
  124. this._needUpdateTexture = false;
  125. return true;
  126. },
  127. ctor: function (text, fontName, fontSize, dimensions, hAlignment, vAlignment) {
  128. cc.Sprite.prototype.ctor.call(this);
  129. this._dimensions = cc.size(0, 0);
  130. this._hAlignment = cc.TEXT_ALIGNMENT_LEFT;
  131. this._vAlignment = cc.VERTICAL_TEXT_ALIGNMENT_TOP;
  132. this._opacityModifyRGB = false;
  133. this._fontStyleStr = "";
  134. this._fontName = "Arial";
  135. this._isMultiLine = false;
  136. this._shadowEnabled = false;
  137. this._shadowOffset = cc.p(0, 0);
  138. this._shadowOpacity = 0;
  139. this._shadowBlur = 0;
  140. this._shadowColorStr = "rgba(128, 128, 128, 0.5)";
  141. this._strokeEnabled = false;
  142. this._strokeColor = cc.color(255, 255, 255, 255);
  143. this._strokeSize = 0;
  144. this._strokeColorStr = "";
  145. this._textFillColor = cc.color(255, 255, 255, 255);
  146. this._fillColorStr = "rgba(255,255,255,1)";
  147. this._strokeShadowOffsetX = 0;
  148. this._strokeShadowOffsetY = 0;
  149. this._needUpdateTexture = false;
  150. this._lineWidths = [];
  151. this._setColorsString();
  152. if (fontName && fontName instanceof cc.FontDefinition) {
  153. this.initWithStringAndTextDefinition(text, fontName);
  154. }
  155. else {
  156. cc.LabelTTF.prototype.initWithString.call(this, text, fontName, fontSize, dimensions, hAlignment, vAlignment);
  157. }
  158. },
  159. init: function () {
  160. return this.initWithString(" ", this._fontName, this._fontSize);
  161. },
  162. _measureConfig: function () {
  163. this._getLabelContext().font = this._fontStyleStr;
  164. },
  165. _measure: function (text) {
  166. return this._getLabelContext().measureText(text).width;
  167. },
  168. description: function () {
  169. return "<cc.LabelTTF | FontName =" + this._fontName + " FontSize = " + this._fontSize.toFixed(1) + ">";
  170. },
  171. setColor: null,
  172. _setColorsString: null,
  173. updateDisplayedColor: null,
  174. setOpacity: null,
  175. updateDisplayedOpacity: null,
  176. updateDisplayedOpacityForCanvas: function (parentOpacity) {
  177. cc.Node.prototype.updateDisplayedOpacity.call(this, parentOpacity);
  178. this._setColorsString();
  179. },
  180. /**
  181. * Returns the text of the label
  182. * @return {String}
  183. */
  184. getString: function () {
  185. return this._string;
  186. },
  187. /**
  188. * Returns Horizontal Alignment of cc.LabelTTF
  189. * @return {cc.TEXT_ALIGNMENT_LEFT|cc.TEXT_ALIGNMENT_CENTER|cc.TEXT_ALIGNMENT_RIGHT}
  190. */
  191. getHorizontalAlignment: function () {
  192. return this._hAlignment;
  193. },
  194. /**
  195. * Returns Vertical Alignment of cc.LabelTTF
  196. * @return {cc.VERTICAL_TEXT_ALIGNMENT_TOP|cc.VERTICAL_TEXT_ALIGNMENT_CENTER|cc.VERTICAL_TEXT_ALIGNMENT_BOTTOM}
  197. */
  198. getVerticalAlignment: function () {
  199. return this._vAlignment;
  200. },
  201. /**
  202. * Returns the dimensions of cc.LabelTTF, the dimension is the maximum size of the label, set it so that label will automatically change lines when necessary.
  203. * @see cc.LabelTTF#setDimensions, cc.LabelTTF#boundingWidth and cc.LabelTTF#boundingHeight
  204. * @return {cc.Size}
  205. */
  206. getDimensions: function () {
  207. return cc.size(this._dimensions);
  208. },
  209. /**
  210. * Returns font size of cc.LabelTTF
  211. * @return {Number}
  212. */
  213. getFontSize: function () {
  214. return this._fontSize;
  215. },
  216. /**
  217. * Returns font name of cc.LabelTTF
  218. * @return {String}
  219. */
  220. getFontName: function () {
  221. return this._fontName;
  222. },
  223. /**
  224. * Initializes the CCLabelTTF with a font name, alignment, dimension and font size, do not call it by yourself, you should pass the correct arguments in constructor to initialize the label.
  225. * @param {String} text
  226. * @param {cc.FontDefinition} textDefinition
  227. * @return {Boolean}
  228. */
  229. initWithStringAndTextDefinition: null,
  230. /**
  231. * Sets the text definition used by this label
  232. * @param {cc.FontDefinition} theDefinition
  233. */
  234. setTextDefinition: function (theDefinition) {
  235. if (theDefinition)
  236. this._updateWithTextDefinition(theDefinition, true);
  237. },
  238. /**
  239. * Extract the text definition used by this label
  240. * @return {cc.FontDefinition}
  241. */
  242. getTextDefinition: function () {
  243. return this._prepareTextDefinition(false);
  244. },
  245. /**
  246. * Enable or disable shadow for the label
  247. * @param {Number} shadowOffsetX The x axis offset of the shadow
  248. * @param {Number} shadowOffsetY The y axis offset of the shadow
  249. * @param {Number} shadowOpacity The opacity of the shadow (0 to 1)
  250. * @param {Number} shadowBlur The blur size of the shadow
  251. */
  252. enableShadow: function (shadowOffsetX, shadowOffsetY, shadowOpacity, shadowBlur) {
  253. shadowOpacity = shadowOpacity || 0.5;
  254. if (false === this._shadowEnabled)
  255. this._shadowEnabled = true;
  256. var locShadowOffset = this._shadowOffset;
  257. if (locShadowOffset && (locShadowOffset.x != shadowOffsetX) || (locShadowOffset._y != shadowOffsetY)) {
  258. locShadowOffset.x = shadowOffsetX;
  259. locShadowOffset.y = shadowOffsetY;
  260. }
  261. if (this._shadowOpacity != shadowOpacity) {
  262. this._shadowOpacity = shadowOpacity;
  263. }
  264. this._setColorsString();
  265. if (this._shadowBlur != shadowBlur)
  266. this._shadowBlur = shadowBlur;
  267. this._needUpdateTexture = true;
  268. },
  269. _getShadowOffsetX: function () {
  270. return this._shadowOffset.x;
  271. },
  272. _setShadowOffsetX: function (x) {
  273. if (false === this._shadowEnabled)
  274. this._shadowEnabled = true;
  275. if (this._shadowOffset.x != x) {
  276. this._shadowOffset.x = x;
  277. this._needUpdateTexture = true;
  278. }
  279. },
  280. _getShadowOffsetY: function () {
  281. return this._shadowOffset._y;
  282. },
  283. _setShadowOffsetY: function (y) {
  284. if (false === this._shadowEnabled)
  285. this._shadowEnabled = true;
  286. if (this._shadowOffset._y != y) {
  287. this._shadowOffset._y = y;
  288. this._needUpdateTexture = true;
  289. }
  290. },
  291. _getShadowOffset: function () {
  292. return cc.p(this._shadowOffset.x, this._shadowOffset.y);
  293. },
  294. _setShadowOffset: function (offset) {
  295. if (false === this._shadowEnabled)
  296. this._shadowEnabled = true;
  297. if (this._shadowOffset.x != offset.x || this._shadowOffset.y != offset.y) {
  298. this._shadowOffset.x = offset.x;
  299. this._shadowOffset.y = offset.y;
  300. this._needUpdateTexture = true;
  301. }
  302. },
  303. _getShadowOpacity: function () {
  304. return this._shadowOpacity;
  305. },
  306. _setShadowOpacity: function (shadowOpacity) {
  307. if (false === this._shadowEnabled)
  308. this._shadowEnabled = true;
  309. if (this._shadowOpacity != shadowOpacity) {
  310. this._shadowOpacity = shadowOpacity;
  311. this._setColorsString();
  312. this._needUpdateTexture = true;
  313. }
  314. },
  315. _getShadowBlur: function () {
  316. return this._shadowBlur;
  317. },
  318. _setShadowBlur: function (shadowBlur) {
  319. if (false === this._shadowEnabled)
  320. this._shadowEnabled = true;
  321. if (this._shadowBlur != shadowBlur) {
  322. this._shadowBlur = shadowBlur;
  323. this._needUpdateTexture = true;
  324. }
  325. },
  326. /**
  327. * Disable shadow rendering
  328. */
  329. disableShadow: function () {
  330. if (this._shadowEnabled) {
  331. this._shadowEnabled = false;
  332. this._needUpdateTexture = true;
  333. }
  334. },
  335. /**
  336. * Enable label stroke with stroke parameters
  337. * @param {cc.Color} strokeColor The color of stroke
  338. * @param {Number} strokeSize The size of stroke
  339. */
  340. enableStroke: function (strokeColor, strokeSize) {
  341. if (this._strokeEnabled === false)
  342. this._strokeEnabled = true;
  343. var locStrokeColor = this._strokeColor;
  344. if ((locStrokeColor.r !== strokeColor.r) || (locStrokeColor.g !== strokeColor.g) || (locStrokeColor.b !== strokeColor.b)) {
  345. locStrokeColor.r = strokeColor.r;
  346. locStrokeColor.g = strokeColor.g;
  347. locStrokeColor.b = strokeColor.b;
  348. this._setColorsString();
  349. }
  350. if (this._strokeSize !== strokeSize)
  351. this._strokeSize = strokeSize || 0;
  352. this._needUpdateTexture = true;
  353. },
  354. _getStrokeStyle: function () {
  355. return this._strokeColor;
  356. },
  357. _setStrokeStyle: function (strokeStyle) {
  358. if (this._strokeEnabled === false)
  359. this._strokeEnabled = true;
  360. var locStrokeColor = this._strokeColor;
  361. if ((locStrokeColor.r !== strokeStyle.r) || (locStrokeColor.g !== strokeStyle.g) || (locStrokeColor.b !== strokeStyle.b)) {
  362. locStrokeColor.r = strokeStyle.r;
  363. locStrokeColor.g = strokeStyle.g;
  364. locStrokeColor.b = strokeStyle.b;
  365. this._setColorsString();
  366. this._needUpdateTexture = true;
  367. }
  368. },
  369. _getLineWidth: function () {
  370. return this._strokeSize;
  371. },
  372. _setLineWidth: function (lineWidth) {
  373. if (this._strokeEnabled === false)
  374. this._strokeEnabled = true;
  375. if (this._strokeSize !== lineWidth) {
  376. this._strokeSize = lineWidth || 0;
  377. this._needUpdateTexture = true;
  378. }
  379. },
  380. /**
  381. * Disable label stroke
  382. */
  383. disableStroke: function () {
  384. if (this._strokeEnabled) {
  385. this._strokeEnabled = false;
  386. this._needUpdateTexture = true;
  387. }
  388. },
  389. /**
  390. * Sets the text fill color
  391. * @function
  392. * @param {cc.Color} fillColor The fill color of the label
  393. */
  394. setFontFillColor: null,
  395. _getFillStyle: function () {
  396. return this._textFillColor;
  397. },
  398. //set the text definition for this label
  399. _updateWithTextDefinition: function (textDefinition, mustUpdateTexture) {
  400. if (textDefinition.fontDimensions) {
  401. this._dimensions.width = textDefinition.boundingWidth;
  402. this._dimensions.height = textDefinition.boundingHeight;
  403. } else {
  404. this._dimensions.width = 0;
  405. this._dimensions.height = 0;
  406. }
  407. this._hAlignment = textDefinition.textAlign;
  408. this._vAlignment = textDefinition.verticalAlign;
  409. this._fontName = textDefinition.fontName;
  410. this._fontSize = textDefinition.fontSize || 12;
  411. this._fontStyleStr = this._fontSize + "px '" + this._fontName + "'";
  412. this._fontClientHeight = cc.LabelTTF.__getFontHeightByDiv(this._fontName, this._fontSize);
  413. // shadow
  414. if (textDefinition.shadowEnabled)
  415. this.enableShadow(textDefinition.shadowOffsetX,
  416. textDefinition.shadowOffsetY,
  417. textDefinition.shadowOpacity,
  418. textDefinition.shadowBlur);
  419. // stroke
  420. if (textDefinition.strokeEnabled)
  421. this.enableStroke(textDefinition.strokeStyle, textDefinition.lineWidth);
  422. // fill color
  423. this.setFontFillColor(textDefinition.fillStyle);
  424. if (mustUpdateTexture)
  425. this._updateTexture();
  426. },
  427. _prepareTextDefinition: function (adjustForResolution) {
  428. var texDef = new cc.FontDefinition();
  429. if (adjustForResolution) {
  430. //texDef.fontSize = (cc._renderType === cc._RENDER_TYPE_CANVAS) ? this._fontSize : this._fontSize * cc.contentScaleFactor();
  431. texDef.fontSize = this._fontSize;
  432. texDef.boundingWidth = cc.contentScaleFactor() * this._dimensions.width;
  433. texDef.boundingHeight = cc.contentScaleFactor() * this._dimensions.height;
  434. } else {
  435. texDef.fontSize = this._fontSize;
  436. texDef.boundingWidth = this._dimensions.width;
  437. texDef.boundingHeight = this._dimensions.height;
  438. }
  439. texDef.fontName = this._fontName;
  440. texDef.textAlign = this._hAlignment;
  441. texDef.verticalAlign = this._vAlignment;
  442. // stroke
  443. if (this._strokeEnabled) {
  444. texDef.strokeEnabled = true;
  445. var locStrokeColor = this._strokeColor;
  446. texDef.strokeStyle = cc.color(locStrokeColor.r, locStrokeColor.g, locStrokeColor.b);
  447. texDef.lineWidth = this._strokeSize;
  448. } else
  449. texDef.strokeEnabled = false;
  450. // shadow
  451. if (this._shadowEnabled) {
  452. texDef.shadowEnabled = true;
  453. texDef.shadowBlur = this._shadowBlur;
  454. texDef.shadowOpacity = this._shadowOpacity;
  455. texDef.shadowOffsetX = (adjustForResolution ? cc.contentScaleFactor() : 1) * this._shadowOffset.x;
  456. texDef.shadowOffsetY = (adjustForResolution ? cc.contentScaleFactor() : 1) * this._shadowOffset.y;
  457. } else
  458. texDef._shadowEnabled = false;
  459. // text tint
  460. var locTextFillColor = this._textFillColor;
  461. texDef.fillStyle = cc.color(locTextFillColor.r, locTextFillColor.g, locTextFillColor.b);
  462. return texDef;
  463. },
  464. _fontClientHeight: 18,
  465. /**
  466. * Changes the text content of the label
  467. * @warning Changing the string is as expensive as creating a new cc.LabelTTF. To obtain better performance use cc.LabelAtlas
  468. * @param {String} text Text content for the label
  469. */
  470. setString: function (text) {
  471. text = String(text);
  472. if (this._originalText != text) {
  473. this._originalText = text + "";
  474. this._updateString();
  475. // Force update
  476. this._needUpdateTexture = true;
  477. }
  478. },
  479. _updateString: function () {
  480. this._string = this._originalText;
  481. },
  482. /**
  483. * Sets Horizontal Alignment of cc.LabelTTF
  484. * @param {cc.TEXT_ALIGNMENT_LEFT|cc.TEXT_ALIGNMENT_CENTER|cc.TEXT_ALIGNMENT_RIGHT} alignment Horizontal Alignment
  485. */
  486. setHorizontalAlignment: function (alignment) {
  487. if (alignment !== this._hAlignment) {
  488. this._hAlignment = alignment;
  489. // Force update
  490. this._needUpdateTexture = true;
  491. }
  492. },
  493. /**
  494. * Sets Vertical Alignment of cc.LabelTTF
  495. * @param {cc.VERTICAL_TEXT_ALIGNMENT_TOP|cc.VERTICAL_TEXT_ALIGNMENT_CENTER|cc.VERTICAL_TEXT_ALIGNMENT_BOTTOM} verticalAlignment
  496. */
  497. setVerticalAlignment: function (verticalAlignment) {
  498. if (verticalAlignment != this._vAlignment) {
  499. this._vAlignment = verticalAlignment;
  500. // Force update
  501. this._needUpdateTexture = true;
  502. }
  503. },
  504. /**
  505. * Set Dimensions of cc.LabelTTF, the dimension is the maximum size of the label, set it so that label will automatically change lines when necessary.
  506. * @param {cc.Size|Number} dim dimensions or width of dimensions
  507. * @param {Number} [height] height of dimensions
  508. */
  509. setDimensions: function (dim, height) {
  510. var width;
  511. if(height === undefined){
  512. width = dim.width;
  513. height = dim.height;
  514. }else
  515. width = dim;
  516. if (width != this._dimensions.width || height != this._dimensions.height) {
  517. this._dimensions.width = width;
  518. this._dimensions.height = height;
  519. this._updateString();
  520. // Force udpate
  521. this._needUpdateTexture = true;
  522. }
  523. },
  524. _getBoundingWidth: function () {
  525. return this._dimensions.width;
  526. },
  527. _setBoundingWidth: function (width) {
  528. if (width != this._dimensions.width) {
  529. this._dimensions.width = width;
  530. this._updateString();
  531. // Force udpate
  532. this._needUpdateTexture = true;
  533. }
  534. },
  535. _getBoundingHeight: function () {
  536. return this._dimensions.height;
  537. },
  538. _setBoundingHeight: function (height) {
  539. if (height != this._dimensions.height) {
  540. this._dimensions.height = height;
  541. this._updateString();
  542. // Force udpate
  543. this._needUpdateTexture = true;
  544. }
  545. },
  546. /**
  547. * Sets font size of cc.LabelTTF
  548. * @param {Number} fontSize
  549. */
  550. setFontSize: function (fontSize) {
  551. if (this._fontSize !== fontSize) {
  552. this._fontSize = fontSize;
  553. this._fontStyleStr = fontSize + "px '" + this._fontName + "'";
  554. this._fontClientHeight = cc.LabelTTF.__getFontHeightByDiv(this._fontName, fontSize);
  555. // Force update
  556. this._needUpdateTexture = true;
  557. }
  558. },
  559. /**
  560. * Sets font name of cc.LabelTTF
  561. * @param {String} fontName
  562. */
  563. setFontName: function (fontName) {
  564. if (this._fontName && this._fontName != fontName) {
  565. this._fontName = fontName;
  566. this._fontStyleStr = this._fontSize + "px '" + fontName + "'";
  567. this._fontClientHeight = cc.LabelTTF.__getFontHeightByDiv(fontName, this._fontSize);
  568. // Force update
  569. this._needUpdateTexture = true;
  570. }
  571. },
  572. _getFont: function () {
  573. return this._fontStyleStr;
  574. },
  575. _setFont: function (fontStyle) {
  576. var res = cc.LabelTTF._fontStyleRE.exec(fontStyle);
  577. if (res) {
  578. this._fontSize = parseInt(res[1]);
  579. this._fontName = res[2];
  580. this._fontStyleStr = fontStyle;
  581. this._fontClientHeight = cc.LabelTTF.__getFontHeightByDiv(this._fontName, this._fontSize);
  582. // Force update
  583. this._needUpdateTexture = true;
  584. }
  585. },
  586. _drawTTFInCanvas: function (context) {
  587. if (!context)
  588. return;
  589. var locStrokeShadowOffsetX = this._strokeShadowOffsetX, locStrokeShadowOffsetY = this._strokeShadowOffsetY;
  590. var locContentSizeHeight = this._contentSize.height - locStrokeShadowOffsetY, locVAlignment = this._vAlignment, locHAlignment = this._hAlignment,
  591. locFontHeight = this._fontClientHeight, locStrokeSize = this._strokeSize;
  592. context.setTransform(1, 0, 0, 1, 0 + locStrokeShadowOffsetX * 0.5, locContentSizeHeight + locStrokeShadowOffsetY * 0.5);
  593. //this is fillText for canvas
  594. if (context.font != this._fontStyleStr)
  595. context.font = this._fontStyleStr;
  596. context.fillStyle = this._fillColorStr;
  597. var xOffset = 0, yOffset = 0;
  598. //stroke style setup
  599. var locStrokeEnabled = this._strokeEnabled;
  600. if (locStrokeEnabled) {
  601. context.lineWidth = locStrokeSize * 2;
  602. context.strokeStyle = this._strokeColorStr;
  603. }
  604. //shadow style setup
  605. if (this._shadowEnabled) {
  606. var locShadowOffset = this._shadowOffset;
  607. context.shadowColor = this._shadowColorStr;
  608. context.shadowOffsetX = locShadowOffset.x;
  609. context.shadowOffsetY = -locShadowOffset.y;
  610. context.shadowBlur = this._shadowBlur;
  611. }
  612. context.textBaseline = cc.LabelTTF._textBaseline[locVAlignment];
  613. context.textAlign = cc.LabelTTF._textAlign[locHAlignment];
  614. var locContentWidth = this._contentSize.width - locStrokeShadowOffsetX;
  615. if (locHAlignment === cc.TEXT_ALIGNMENT_RIGHT)
  616. xOffset += locContentWidth;
  617. else if (locHAlignment === cc.TEXT_ALIGNMENT_CENTER)
  618. xOffset += locContentWidth / 2;
  619. else
  620. xOffset += 0;
  621. if (this._isMultiLine) {
  622. var locStrLen = this._strings.length;
  623. if (locVAlignment === cc.VERTICAL_TEXT_ALIGNMENT_BOTTOM)
  624. yOffset = locFontHeight + locContentSizeHeight - locFontHeight * locStrLen;
  625. else if (locVAlignment === cc.VERTICAL_TEXT_ALIGNMENT_CENTER)
  626. yOffset = locFontHeight / 2 + (locContentSizeHeight - locFontHeight * locStrLen) / 2;
  627. for (var i = 0; i < locStrLen; i++) {
  628. var line = this._strings[i];
  629. var tmpOffsetY = -locContentSizeHeight + (locFontHeight * i) + yOffset;
  630. if (locStrokeEnabled)
  631. context.strokeText(line, xOffset, tmpOffsetY);
  632. context.fillText(line, xOffset, tmpOffsetY);
  633. }
  634. } else {
  635. if (locVAlignment === cc.VERTICAL_TEXT_ALIGNMENT_BOTTOM) {
  636. if (locStrokeEnabled)
  637. context.strokeText(this._string, xOffset, yOffset);
  638. context.fillText(this._string, xOffset, yOffset);
  639. } else if (locVAlignment === cc.VERTICAL_TEXT_ALIGNMENT_TOP) {
  640. yOffset -= locContentSizeHeight;
  641. if (locStrokeEnabled)
  642. context.strokeText(this._string, xOffset, yOffset);
  643. context.fillText(this._string, xOffset, yOffset);
  644. } else {
  645. yOffset -= locContentSizeHeight * 0.5;
  646. if (locStrokeEnabled)
  647. context.strokeText(this._string, xOffset, yOffset);
  648. context.fillText(this._string, xOffset, yOffset);
  649. }
  650. }
  651. },
  652. _getLabelContext: function () {
  653. if (this._labelContext)
  654. return this._labelContext;
  655. if (!this._labelCanvas) {
  656. var locCanvas = cc.newElement("canvas");
  657. var labelTexture = new cc.Texture2D();
  658. labelTexture.initWithElement(locCanvas);
  659. this.texture = labelTexture;
  660. this._labelCanvas = locCanvas;
  661. }
  662. this._labelContext = this._labelCanvas.getContext("2d");
  663. return this._labelContext;
  664. },
  665. _checkWarp: function(strArr, i, maxWidth){
  666. var text = strArr[i];
  667. var allWidth = this._measure(text);
  668. if(allWidth > maxWidth && text.length > 1){
  669. var fuzzyLen = text.length * ( maxWidth / allWidth ) | 0;
  670. var tmpText = text.substr(fuzzyLen);
  671. var width = allWidth - this._measure(tmpText);
  672. var sLine;
  673. var pushNum = 0;
  674. //Increased while cycle maximum ceiling. default 100 time
  675. var checkWhile = 0;
  676. //Exceeded the size
  677. while(width > maxWidth && checkWhile++ < 100){
  678. fuzzyLen *= maxWidth / width;
  679. fuzzyLen = fuzzyLen | 0;
  680. tmpText = text.substr(fuzzyLen);
  681. width = allWidth - this._measure(tmpText);
  682. }
  683. checkWhile = 0;
  684. //Find the truncation point
  685. while(width < maxWidth && checkWhile++ < 100){
  686. if(tmpText){
  687. var exec = cc.LabelTTF._wordRex.exec(tmpText);
  688. pushNum = exec ? exec[0].length : 1;
  689. sLine = tmpText;
  690. }
  691. fuzzyLen = fuzzyLen + pushNum;
  692. tmpText = text.substr(fuzzyLen);
  693. width = allWidth - this._measure(tmpText);
  694. }
  695. fuzzyLen -= pushNum;
  696. var sText = text.substr(0, fuzzyLen);
  697. //symbol in the first
  698. if(cc.LabelTTF.wrapInspection){
  699. if(cc.LabelTTF._symbolRex.test(sLine || tmpText)){
  700. var result = cc.LabelTTF._lastWordRex.exec(sText);
  701. fuzzyLen -= result ? result[0].length : 0;
  702. sLine = text.substr(fuzzyLen);
  703. sText = text.substr(0, fuzzyLen);
  704. }
  705. }
  706. //To judge whether a English words are truncated
  707. if(cc.LabelTTF._firsrEnglish.test(sLine)){
  708. var result = cc.LabelTTF._lastEnglish.exec(sText);
  709. if(result && sText !== result[0]){
  710. fuzzyLen -= result[0].length;
  711. sLine = text.substr(fuzzyLen);
  712. sText = text.substr(0, fuzzyLen);
  713. }
  714. }
  715. strArr[i] = sLine || tmpText;
  716. strArr.splice(i, 0, sText);
  717. }
  718. },
  719. _updateTTF: function () {
  720. var locDimensionsWidth = this._dimensions.width, i, strLength;
  721. var locLineWidth = this._lineWidths;
  722. locLineWidth.length = 0;
  723. this._isMultiLine = false;
  724. this._measureConfig();
  725. if (locDimensionsWidth !== 0) {
  726. // Content processing
  727. this._strings = this._string.split('\n');
  728. for(i = 0; i < this._strings.length; i++){
  729. this._checkWarp(this._strings, i, locDimensionsWidth);
  730. }
  731. } else {
  732. this._strings = this._string.split('\n');
  733. for (i = 0, strLength = this._strings.length; i < strLength; i++) {
  734. locLineWidth.push(this._measure(this._strings[i]));
  735. }
  736. }
  737. if (this._strings.length > 0)
  738. this._isMultiLine = true;
  739. var locSize, locStrokeShadowOffsetX = 0, locStrokeShadowOffsetY = 0;
  740. if (this._strokeEnabled)
  741. locStrokeShadowOffsetX = locStrokeShadowOffsetY = this._strokeSize * 2;
  742. if (this._shadowEnabled) {
  743. var locOffsetSize = this._shadowOffset;
  744. locStrokeShadowOffsetX += Math.abs(locOffsetSize.x) * 2;
  745. locStrokeShadowOffsetY += Math.abs(locOffsetSize.y) * 2;
  746. }
  747. //get offset for stroke and shadow
  748. if (locDimensionsWidth === 0) {
  749. if (this._isMultiLine)
  750. locSize = cc.size(0 | (Math.max.apply(Math, locLineWidth) + locStrokeShadowOffsetX),
  751. 0 | ((this._fontClientHeight * this._strings.length) + locStrokeShadowOffsetY));
  752. else
  753. locSize = cc.size(0 | (this._measure(this._string) + locStrokeShadowOffsetX), 0 | (this._fontClientHeight + locStrokeShadowOffsetY));
  754. } else {
  755. if (this._dimensions.height === 0) {
  756. if (this._isMultiLine)
  757. locSize = cc.size(0 | (locDimensionsWidth + locStrokeShadowOffsetX), 0 | ((this._fontClientHeight * this._strings.length) + locStrokeShadowOffsetY));
  758. else
  759. locSize = cc.size(0 | (locDimensionsWidth + locStrokeShadowOffsetX), 0 | (this._fontClientHeight + locStrokeShadowOffsetY));
  760. } else {
  761. //dimension is already set, contentSize must be same as dimension
  762. locSize = cc.size(0 | (locDimensionsWidth + locStrokeShadowOffsetX), 0 | (this._dimensions.height + locStrokeShadowOffsetY));
  763. }
  764. }
  765. this.setContentSize(locSize);
  766. this._strokeShadowOffsetX = locStrokeShadowOffsetX;
  767. this._strokeShadowOffsetY = locStrokeShadowOffsetY;
  768. // need computing _anchorPointInPoints
  769. var locAP = this._anchorPoint;
  770. this._anchorPointInPoints.x = (locStrokeShadowOffsetX * 0.5) + ((locSize.width - locStrokeShadowOffsetX) * locAP.x);
  771. this._anchorPointInPoints.y = (locStrokeShadowOffsetY * 0.5) + ((locSize.height - locStrokeShadowOffsetY) * locAP.y);
  772. },
  773. /**
  774. * Returns the actual content size of the label, the content size is the real size that the label occupied while dimension is the outer bounding box of the label.
  775. * @returns {cc.Size} The content size
  776. */
  777. getContentSize: function () {
  778. if (this._needUpdateTexture)
  779. this._updateTTF();
  780. return cc.Sprite.prototype.getContentSize.call(this);
  781. },
  782. _getWidth: function () {
  783. if (this._needUpdateTexture)
  784. this._updateTTF();
  785. return cc.Sprite.prototype._getWidth.call(this);
  786. },
  787. _getHeight: function () {
  788. if (this._needUpdateTexture)
  789. this._updateTTF();
  790. return cc.Sprite.prototype._getHeight.call(this);
  791. },
  792. _updateTexture: function () {
  793. var locContext = this._getLabelContext(), locLabelCanvas = this._labelCanvas;
  794. var locContentSize = this._contentSize;
  795. if (this._string.length === 0) {
  796. locLabelCanvas.width = 1;
  797. locLabelCanvas.height = locContentSize.height || 1;
  798. this._texture && this._texture.handleLoadedTexture();
  799. this.setTextureRect(cc.rect(0, 0, 1, locContentSize.height));
  800. return true;
  801. }
  802. //set size for labelCanvas
  803. locContext.font = this._fontStyleStr;
  804. this._updateTTF();
  805. var width = locContentSize.width, height = locContentSize.height;
  806. var flag = locLabelCanvas.width == width && locLabelCanvas.height == height;
  807. locLabelCanvas.width = width;
  808. locLabelCanvas.height = height;
  809. if (flag) locContext.clearRect(0, 0, width, height);
  810. //draw text to labelCanvas
  811. this._drawTTFInCanvas(locContext);
  812. this._texture && this._texture.handleLoadedTexture();
  813. this.setTextureRect(cc.rect(0, 0, width, height));
  814. return true;
  815. },
  816. visit: function (ctx) {
  817. if (!this._string || this._string == "")
  818. return;
  819. if (this._needUpdateTexture) {
  820. this._needUpdateTexture = false;
  821. this._updateTexture();
  822. }
  823. var context = ctx || cc._renderContext;
  824. cc.Sprite.prototype.visit.call(this, context);
  825. },
  826. draw: null,
  827. _setTextureCoords: function (rect) {
  828. var tex = this._batchNode ? this.textureAtlas.texture : this._texture;
  829. if (!tex)
  830. return;
  831. var atlasWidth = tex.pixelsWidth;
  832. var atlasHeight = tex.pixelsHeight;
  833. var left, right, top, bottom, tempSwap, locQuad = this._quad;
  834. if (this._rectRotated) {
  835. if (cc.FIX_ARTIFACTS_BY_STRECHING_TEXEL) {
  836. left = (2 * rect.x + 1) / (2 * atlasWidth);
  837. right = left + (rect.height * 2 - 2) / (2 * atlasWidth);
  838. top = (2 * rect.y + 1) / (2 * atlasHeight);
  839. bottom = top + (rect.width * 2 - 2) / (2 * atlasHeight);
  840. } else {
  841. left = rect.x / atlasWidth;
  842. right = (rect.x + rect.height) / atlasWidth;
  843. top = rect.y / atlasHeight;
  844. bottom = (rect.y + rect.width) / atlasHeight;
  845. }// CC_FIX_ARTIFACTS_BY_STRECHING_TEXEL
  846. if (this._flippedX) {
  847. tempSwap = top;
  848. top = bottom;
  849. bottom = tempSwap;
  850. }
  851. if (this._flippedY) {
  852. tempSwap = left;
  853. left = right;
  854. right = tempSwap;
  855. }
  856. locQuad.bl.texCoords.u = left;
  857. locQuad.bl.texCoords.v = top;
  858. locQuad.br.texCoords.u = left;
  859. locQuad.br.texCoords.v = bottom;
  860. locQuad.tl.texCoords.u = right;
  861. locQuad.tl.texCoords.v = top;
  862. locQuad.tr.texCoords.u = right;
  863. locQuad.tr.texCoords.v = bottom;
  864. } else {
  865. if (cc.FIX_ARTIFACTS_BY_STRECHING_TEXEL) {
  866. left = (2 * rect.x + 1) / (2 * atlasWidth);
  867. right = left + (rect.width * 2 - 2) / (2 * atlasWidth);
  868. top = (2 * rect.y + 1) / (2 * atlasHeight);
  869. bottom = top + (rect.height * 2 - 2) / (2 * atlasHeight);
  870. } else {
  871. left = rect.x / atlasWidth;
  872. right = (rect.x + rect.width) / atlasWidth;
  873. top = rect.y / atlasHeight;
  874. bottom = (rect.y + rect.height) / atlasHeight;
  875. } // ! CC_FIX_ARTIFACTS_BY_STRECHING_TEXEL
  876. if (this._flippedX) {
  877. tempSwap = left;
  878. left = right;
  879. right = tempSwap;
  880. }
  881. if (this._flippedY) {
  882. tempSwap = top;
  883. top = bottom;
  884. bottom = tempSwap;
  885. }
  886. locQuad.bl.texCoords.u = left;
  887. locQuad.bl.texCoords.v = bottom;
  888. locQuad.br.texCoords.u = right;
  889. locQuad.br.texCoords.v = bottom;
  890. locQuad.tl.texCoords.u = left;
  891. locQuad.tl.texCoords.v = top;
  892. locQuad.tr.texCoords.u = right;
  893. locQuad.tr.texCoords.v = top;
  894. }
  895. this._quadDirty = true;
  896. }
  897. });
  898. if (cc._renderType === cc._RENDER_TYPE_CANVAS) {
  899. var _p = cc.LabelTTF.prototype;
  900. _p.setColor = function (color3) {
  901. cc.Node.prototype.setColor.call(this, color3);
  902. this._setColorsString();
  903. };
  904. _p._setColorsString = function () {
  905. this._needUpdateTexture = true;
  906. var locDisplayColor = this._displayedColor, locDisplayedOpacity = this._displayedOpacity;
  907. var locStrokeColor = this._strokeColor, locFontFillColor = this._textFillColor;
  908. this._shadowColorStr = "rgba(" + (0 | (locDisplayColor.r * 0.5)) + "," + (0 | (locDisplayColor.g * 0.5)) + "," + (0 | (locDisplayColor.b * 0.5)) + "," + this._shadowOpacity + ")";
  909. this._fillColorStr = "rgba(" + (0 | (locDisplayColor.r / 255 * locFontFillColor.r)) + "," + (0 | (locDisplayColor.g / 255 * locFontFillColor.g)) + ","
  910. + (0 | (locDisplayColor.b / 255 * locFontFillColor.b)) + ", " + locDisplayedOpacity / 255 + ")";
  911. this._strokeColorStr = "rgba(" + (0 | (locDisplayColor.r / 255 * locStrokeColor.r)) + "," + (0 | (locDisplayColor.g / 255 * locStrokeColor.g)) + ","
  912. + (0 | (locDisplayColor.b / 255 * locStrokeColor.b)) + ", " + locDisplayedOpacity / 255 + ")";
  913. };
  914. _p.updateDisplayedColor = function (parentColor) {
  915. cc.Node.prototype.updateDisplayedColor.call(this, parentColor);
  916. this._setColorsString();
  917. };
  918. _p.setOpacity = function (opacity) {
  919. if (this._opacity === opacity)
  920. return;
  921. cc.Sprite.prototype.setOpacity.call(this, opacity);
  922. this._setColorsString();
  923. this._needUpdateTexture = true;
  924. };
  925. //TODO: _p._updateDisplayedOpacityForCanvas
  926. _p.updateDisplayedOpacity = cc.Sprite.prototype.updateDisplayedOpacity;
  927. _p.initWithStringAndTextDefinition = function (text, textDefinition) {
  928. // prepare everything needed to render the label
  929. this._updateWithTextDefinition(textDefinition, false);
  930. // set the string
  931. this.string = text;
  932. return true;
  933. };
  934. _p.setFontFillColor = function (tintColor) {
  935. var locTextFillColor = this._textFillColor;
  936. if (locTextFillColor.r != tintColor.r || locTextFillColor.g != tintColor.g || locTextFillColor.b != tintColor.b) {
  937. locTextFillColor.r = tintColor.r;
  938. locTextFillColor.g = tintColor.g;
  939. locTextFillColor.b = tintColor.b;
  940. this._setColorsString();
  941. this._needUpdateTexture = true;
  942. }
  943. };
  944. _p.draw = cc.Sprite.prototype.draw;
  945. _p.setTextureRect = function (rect, rotated, untrimmedSize) {
  946. this._rectRotated = rotated || false;
  947. untrimmedSize = untrimmedSize || rect;
  948. this.setContentSize(untrimmedSize);
  949. this.setVertexRect(rect);
  950. var locTextureCoordRect = this._textureRect_Canvas;
  951. locTextureCoordRect.x = rect.x;
  952. locTextureCoordRect.y = rect.y;
  953. locTextureCoordRect.width = rect.width;
  954. locTextureCoordRect.height = rect.height;
  955. locTextureCoordRect.validRect = !(locTextureCoordRect.width === 0 || locTextureCoordRect.height === 0
  956. || locTextureCoordRect.x < 0 || locTextureCoordRect.y < 0);
  957. var relativeOffset = this._unflippedOffsetPositionFromCenter;
  958. if (this._flippedX)
  959. relativeOffset.x = -relativeOffset.x;
  960. if (this._flippedY)
  961. relativeOffset.y = -relativeOffset.y;
  962. this._offsetPosition.x = relativeOffset.x + (this._contentSize.width - this._rect.width) / 2;
  963. this._offsetPosition.y = relativeOffset.y + (this._contentSize.height - this._rect.height) / 2;
  964. // rendering using batch node
  965. if (this._batchNode) {
  966. this.dirty = true;
  967. }
  968. };
  969. _p = null;
  970. } else {
  971. cc.assert(typeof cc._tmp.WebGLLabelTTF === "function", cc._LogInfos.MissingFile, "LabelTTFWebGL.js");
  972. cc._tmp.WebGLLabelTTF();
  973. delete cc._tmp.WebGLLabelTTF;
  974. }
  975. cc.assert(typeof cc._tmp.PrototypeLabelTTF === "function", cc._LogInfos.MissingFile, "LabelTTFPropertyDefine.js");
  976. cc._tmp.PrototypeLabelTTF();
  977. delete cc._tmp.PrototypeLabelTTF;
  978. cc.LabelTTF._textAlign = ["left", "center", "right"];
  979. cc.LabelTTF._textBaseline = ["top", "middle", "bottom"];
  980. //check the first character
  981. cc.LabelTTF.wrapInspection = true;
  982. //Support: English French German
  983. //Other as Oriental Language
  984. cc.LabelTTF._wordRex = /([a-zA-Z0-9"A"O"U"a"oüsséècàùê^a^i^o^u]+|\S)/;
  985. cc.LabelTTF._symbolRex = /^[!,.:;}\]%\?>、‘“》?。,!]/;
  986. cc.LabelTTF._lastWordRex = /([a-zA-Z0-9"A"O"U"a"oüsséècàùê^a^i^o^u]+|\S)$/;
  987. cc.LabelTTF._lastEnglish = /[a-zA-Z0-9"A"O"U"a"oüsséècàùê^a^i^o^u]+$/;
  988. cc.LabelTTF._firsrEnglish = /^[a-zA-Z0-9"A"O"U"a"oüsséècàùê^a^i^o^u]/;
  989. // Only support style in this format: "18px Verdana" or "18px 'Helvetica Neue'"
  990. cc.LabelTTF._fontStyleRE = /^(\d+)px\s+['"]?([\w\s\d]+)['"]?$/;
  991. /**
  992. * creates a cc.LabelTTF from a font name, alignment, dimension and font size
  993. * @deprecated since v3.0, please use the new construction instead
  994. * @see cc.LabelTTF
  995. * @static
  996. * @param {String} text
  997. * @param {String|cc.FontDefinition} [fontName="Arial"]
  998. * @param {Number} [fontSize=16]
  999. * @param {cc.Size} [dimensions=cc.size(0,0)]
  1000. * @param {Number} [hAlignment=cc.TEXT_ALIGNMENT_LEFT]
  1001. * @param {Number} [vAlignment=cc.VERTICAL_TEXT_ALIGNMENT_TOP]
  1002. * @return {cc.LabelTTF|Null}
  1003. */
  1004. cc.LabelTTF.create = function (text, fontName, fontSize, dimensions, hAlignment, vAlignment) {
  1005. return new cc.LabelTTF(text, fontName, fontSize, dimensions, hAlignment, vAlignment);
  1006. };
  1007. /**
  1008. * @deprecated since v3.0, please use the new construction instead
  1009. * @function
  1010. * @static
  1011. */
  1012. cc.LabelTTF.createWithFontDefinition = cc.LabelTTF.create;
  1013. if (cc.USE_LA88_LABELS)
  1014. cc.LabelTTF._SHADER_PROGRAM = cc.SHADER_POSITION_TEXTURECOLOR;
  1015. else
  1016. cc.LabelTTF._SHADER_PROGRAM = cc.SHADER_POSITION_TEXTUREA8COLOR;
  1017. cc.LabelTTF.__labelHeightDiv = cc.newElement("div");
  1018. cc.LabelTTF.__labelHeightDiv.style.fontFamily = "Arial";
  1019. cc.LabelTTF.__labelHeightDiv.style.position = "absolute";
  1020. cc.LabelTTF.__labelHeightDiv.style.left = "-100px";
  1021. cc.LabelTTF.__labelHeightDiv.style.top = "-100px";
  1022. cc.LabelTTF.__labelHeightDiv.style.lineHeight = "normal";
  1023. document.body ?
  1024. document.body.appendChild(cc.LabelTTF.__labelHeightDiv) :
  1025. cc._addEventListener(window, 'load', function () {
  1026. this.removeEventListener('load', arguments.callee, false);
  1027. document.body.appendChild(cc.LabelTTF.__labelHeightDiv);
  1028. }, false);
  1029. cc.LabelTTF.__getFontHeightByDiv = function (fontName, fontSize) {
  1030. var clientHeight = cc.LabelTTF.__fontHeightCache[fontName + "." + fontSize];
  1031. if (clientHeight > 0) return clientHeight;
  1032. var labelDiv = cc.LabelTTF.__labelHeightDiv;
  1033. labelDiv.innerHTML = "ajghl~!";
  1034. labelDiv.style.fontFamily = fontName;
  1035. labelDiv.style.fontSize = fontSize + "px";
  1036. clientHeight = labelDiv.clientHeight;
  1037. cc.LabelTTF.__fontHeightCache[fontName + "." + fontSize] = clientHeight;
  1038. labelDiv.innerHTML = "";
  1039. return clientHeight;
  1040. };
  1041. cc.LabelTTF.__fontHeightCache = {};