TextProperty.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. import {
  2. initialDefaultFrame,
  3. } from '../../main';
  4. import getFontProperties from '../getFontProperties';
  5. import FontManager from '../FontManager';
  6. function TextProperty(elem, data) {
  7. this._frameId = initialDefaultFrame;
  8. this.pv = '';
  9. this.v = '';
  10. this.kf = false;
  11. this._isFirstFrame = true;
  12. this._mdf = false;
  13. if (data.d && data.d.sid) {
  14. data.d = elem.globalData.slotManager.getProp(data.d);
  15. }
  16. this.data = data;
  17. this.elem = elem;
  18. this.comp = this.elem.comp;
  19. this.keysIndex = 0;
  20. this.canResize = false;
  21. this.minimumFontSize = 1;
  22. this.effectsSequence = [];
  23. this.currentData = {
  24. ascent: 0,
  25. boxWidth: this.defaultBoxWidth,
  26. f: '',
  27. fStyle: '',
  28. fWeight: '',
  29. fc: '',
  30. j: '',
  31. justifyOffset: '',
  32. l: [],
  33. lh: 0,
  34. lineWidths: [],
  35. ls: '',
  36. of: '',
  37. s: '',
  38. sc: '',
  39. sw: 0,
  40. t: 0,
  41. tr: 0,
  42. sz: 0,
  43. ps: null,
  44. fillColorAnim: false,
  45. strokeColorAnim: false,
  46. strokeWidthAnim: false,
  47. yOffset: 0,
  48. finalSize: 0,
  49. finalText: [],
  50. finalLineHeight: 0,
  51. __complete: false,
  52. };
  53. this.copyData(this.currentData, this.data.d.k[0].s);
  54. if (!this.searchProperty()) {
  55. this.completeTextData(this.currentData);
  56. }
  57. }
  58. TextProperty.prototype.defaultBoxWidth = [0, 0];
  59. TextProperty.prototype.copyData = function (obj, data) {
  60. for (var s in data) {
  61. if (Object.prototype.hasOwnProperty.call(data, s)) {
  62. obj[s] = data[s];
  63. }
  64. }
  65. return obj;
  66. };
  67. TextProperty.prototype.setCurrentData = function (data) {
  68. if (!data.__complete) {
  69. this.completeTextData(data);
  70. }
  71. this.currentData = data;
  72. this.currentData.boxWidth = this.currentData.boxWidth || this.defaultBoxWidth;
  73. this._mdf = true;
  74. };
  75. TextProperty.prototype.searchProperty = function () {
  76. return this.searchKeyframes();
  77. };
  78. TextProperty.prototype.searchKeyframes = function () {
  79. this.kf = this.data.d.k.length > 1;
  80. if (this.kf) {
  81. this.addEffect(this.getKeyframeValue.bind(this));
  82. }
  83. return this.kf;
  84. };
  85. TextProperty.prototype.addEffect = function (effectFunction) {
  86. this.effectsSequence.push(effectFunction);
  87. this.elem.addDynamicProperty(this);
  88. };
  89. TextProperty.prototype.getValue = function (_finalValue) {
  90. if ((this.elem.globalData.frameId === this.frameId || !this.effectsSequence.length) && !_finalValue) {
  91. return;
  92. }
  93. this.currentData.t = this.data.d.k[this.keysIndex].s.t;
  94. var currentValue = this.currentData;
  95. var currentIndex = this.keysIndex;
  96. if (this.lock) {
  97. this.setCurrentData(this.currentData);
  98. return;
  99. }
  100. this.lock = true;
  101. this._mdf = false;
  102. var i; var
  103. len = this.effectsSequence.length;
  104. var finalValue = _finalValue || this.data.d.k[this.keysIndex].s;
  105. for (i = 0; i < len; i += 1) {
  106. // Checking if index changed to prevent creating a new object every time the expression updates.
  107. if (currentIndex !== this.keysIndex) {
  108. finalValue = this.effectsSequence[i](finalValue, finalValue.t);
  109. } else {
  110. finalValue = this.effectsSequence[i](this.currentData, finalValue.t);
  111. }
  112. }
  113. if (currentValue !== finalValue) {
  114. this.setCurrentData(finalValue);
  115. }
  116. this.v = this.currentData;
  117. this.pv = this.v;
  118. this.lock = false;
  119. this.frameId = this.elem.globalData.frameId;
  120. };
  121. TextProperty.prototype.getKeyframeValue = function () {
  122. var textKeys = this.data.d.k;
  123. var frameNum = this.elem.comp.renderedFrame;
  124. var i = 0; var
  125. len = textKeys.length;
  126. while (i <= len - 1) {
  127. if (i === len - 1 || textKeys[i + 1].t > frameNum) {
  128. break;
  129. }
  130. i += 1;
  131. }
  132. if (this.keysIndex !== i) {
  133. this.keysIndex = i;
  134. }
  135. return this.data.d.k[this.keysIndex].s;
  136. };
  137. TextProperty.prototype.buildFinalText = function (text) {
  138. var charactersArray = [];
  139. var i = 0;
  140. var len = text.length;
  141. var charCode;
  142. var secondCharCode;
  143. var shouldCombine = false;
  144. var shouldCombineNext = false;
  145. var currentChars = '';
  146. while (i < len) {
  147. shouldCombine = shouldCombineNext;
  148. shouldCombineNext = false;
  149. charCode = text.charCodeAt(i);
  150. currentChars = text.charAt(i);
  151. if (FontManager.isCombinedCharacter(charCode)) {
  152. shouldCombine = true;
  153. // It's a potential surrogate pair (this is the High surrogate)
  154. } else if (charCode >= 0xD800 && charCode <= 0xDBFF) {
  155. if (FontManager.isRegionalFlag(text, i)) {
  156. currentChars = text.substr(i, 14);
  157. } else {
  158. secondCharCode = text.charCodeAt(i + 1);
  159. // It's a surrogate pair (this is the Low surrogate)
  160. if (secondCharCode >= 0xDC00 && secondCharCode <= 0xDFFF) {
  161. if (FontManager.isModifier(charCode, secondCharCode)) {
  162. currentChars = text.substr(i, 2);
  163. shouldCombine = true;
  164. } else if (FontManager.isFlagEmoji(text.substr(i, 4))) {
  165. currentChars = text.substr(i, 4);
  166. } else {
  167. currentChars = text.substr(i, 2);
  168. }
  169. }
  170. }
  171. } else if (charCode > 0xDBFF) {
  172. secondCharCode = text.charCodeAt(i + 1);
  173. if (FontManager.isVariationSelector(charCode)) {
  174. shouldCombine = true;
  175. }
  176. } else if (FontManager.isZeroWidthJoiner(charCode)) {
  177. shouldCombine = true;
  178. shouldCombineNext = true;
  179. }
  180. if (shouldCombine) {
  181. charactersArray[charactersArray.length - 1] += currentChars;
  182. shouldCombine = false;
  183. } else {
  184. charactersArray.push(currentChars);
  185. }
  186. i += currentChars.length;
  187. }
  188. return charactersArray;
  189. };
  190. TextProperty.prototype.completeTextData = function (documentData) {
  191. documentData.__complete = true;
  192. var fontManager = this.elem.globalData.fontManager;
  193. var data = this.data;
  194. var letters = [];
  195. var i; var
  196. len;
  197. var newLineFlag; var index = 0; var
  198. val;
  199. var anchorGrouping = data.m.g;
  200. var currentSize = 0; var currentPos = 0; var currentLine = 0; var
  201. lineWidths = [];
  202. var lineWidth = 0;
  203. var maxLineWidth = 0;
  204. var j; var
  205. jLen;
  206. var fontData = fontManager.getFontByName(documentData.f);
  207. var charData; var
  208. cLength = 0;
  209. var fontProps = getFontProperties(fontData);
  210. documentData.fWeight = fontProps.weight;
  211. documentData.fStyle = fontProps.style;
  212. documentData.finalSize = documentData.s;
  213. documentData.finalText = this.buildFinalText(documentData.t);
  214. len = documentData.finalText.length;
  215. documentData.finalLineHeight = documentData.lh;
  216. var trackingOffset = (documentData.tr / 1000) * documentData.finalSize;
  217. var charCode;
  218. if (documentData.sz) {
  219. var flag = true;
  220. var boxWidth = documentData.sz[0];
  221. var boxHeight = documentData.sz[1];
  222. var currentHeight; var
  223. finalText;
  224. while (flag) {
  225. finalText = this.buildFinalText(documentData.t);
  226. currentHeight = 0;
  227. lineWidth = 0;
  228. len = finalText.length;
  229. trackingOffset = (documentData.tr / 1000) * documentData.finalSize;
  230. var lastSpaceIndex = -1;
  231. for (i = 0; i < len; i += 1) {
  232. charCode = finalText[i].charCodeAt(0);
  233. newLineFlag = false;
  234. if (finalText[i] === ' ') {
  235. lastSpaceIndex = i;
  236. } else if (charCode === 13 || charCode === 3) {
  237. lineWidth = 0;
  238. newLineFlag = true;
  239. currentHeight += documentData.finalLineHeight || documentData.finalSize * 1.2;
  240. }
  241. if (fontManager.chars) {
  242. charData = fontManager.getCharData(finalText[i], fontData.fStyle, fontData.fFamily);
  243. cLength = newLineFlag ? 0 : (charData.w * documentData.finalSize) / 100;
  244. } else {
  245. // tCanvasHelper.font = documentData.s + 'px '+ fontData.fFamily;
  246. cLength = fontManager.measureText(finalText[i], documentData.f, documentData.finalSize);
  247. }
  248. if (lineWidth + cLength > boxWidth && finalText[i] !== ' ') {
  249. if (lastSpaceIndex === -1) {
  250. len += 1;
  251. } else {
  252. i = lastSpaceIndex;
  253. }
  254. currentHeight += documentData.finalLineHeight || documentData.finalSize * 1.2;
  255. finalText.splice(i, lastSpaceIndex === i ? 1 : 0, '\r');
  256. // finalText = finalText.substr(0,i) + "\r" + finalText.substr(i === lastSpaceIndex ? i + 1 : i);
  257. lastSpaceIndex = -1;
  258. lineWidth = 0;
  259. } else {
  260. lineWidth += cLength;
  261. lineWidth += trackingOffset;
  262. }
  263. }
  264. currentHeight += (fontData.ascent * documentData.finalSize) / 100;
  265. if (this.canResize && documentData.finalSize > this.minimumFontSize && boxHeight < currentHeight) {
  266. documentData.finalSize -= 1;
  267. documentData.finalLineHeight = (documentData.finalSize * documentData.lh) / documentData.s;
  268. } else {
  269. documentData.finalText = finalText;
  270. len = documentData.finalText.length;
  271. flag = false;
  272. }
  273. }
  274. }
  275. lineWidth = -trackingOffset;
  276. cLength = 0;
  277. var uncollapsedSpaces = 0;
  278. var currentChar;
  279. for (i = 0; i < len; i += 1) {
  280. newLineFlag = false;
  281. currentChar = documentData.finalText[i];
  282. charCode = currentChar.charCodeAt(0);
  283. if (charCode === 13 || charCode === 3) {
  284. uncollapsedSpaces = 0;
  285. lineWidths.push(lineWidth);
  286. maxLineWidth = lineWidth > maxLineWidth ? lineWidth : maxLineWidth;
  287. lineWidth = -2 * trackingOffset;
  288. val = '';
  289. newLineFlag = true;
  290. currentLine += 1;
  291. } else {
  292. val = currentChar;
  293. }
  294. if (fontManager.chars) {
  295. charData = fontManager.getCharData(currentChar, fontData.fStyle, fontManager.getFontByName(documentData.f).fFamily);
  296. cLength = newLineFlag ? 0 : (charData.w * documentData.finalSize) / 100;
  297. } else {
  298. // var charWidth = fontManager.measureText(val, documentData.f, documentData.finalSize);
  299. // tCanvasHelper.font = documentData.finalSize + 'px '+ fontManager.getFontByName(documentData.f).fFamily;
  300. cLength = fontManager.measureText(val, documentData.f, documentData.finalSize);
  301. }
  302. //
  303. if (currentChar === ' ') {
  304. uncollapsedSpaces += cLength + trackingOffset;
  305. } else {
  306. lineWidth += cLength + trackingOffset + uncollapsedSpaces;
  307. uncollapsedSpaces = 0;
  308. }
  309. letters.push({
  310. l: cLength, an: cLength, add: currentSize, n: newLineFlag, anIndexes: [], val: val, line: currentLine, animatorJustifyOffset: 0,
  311. });
  312. if (anchorGrouping == 2) { // eslint-disable-line eqeqeq
  313. currentSize += cLength;
  314. if (val === '' || val === ' ' || i === len - 1) {
  315. if (val === '' || val === ' ') {
  316. currentSize -= cLength;
  317. }
  318. while (currentPos <= i) {
  319. letters[currentPos].an = currentSize;
  320. letters[currentPos].ind = index;
  321. letters[currentPos].extra = cLength;
  322. currentPos += 1;
  323. }
  324. index += 1;
  325. currentSize = 0;
  326. }
  327. } else if (anchorGrouping == 3) { // eslint-disable-line eqeqeq
  328. currentSize += cLength;
  329. if (val === '' || i === len - 1) {
  330. if (val === '') {
  331. currentSize -= cLength;
  332. }
  333. while (currentPos <= i) {
  334. letters[currentPos].an = currentSize;
  335. letters[currentPos].ind = index;
  336. letters[currentPos].extra = cLength;
  337. currentPos += 1;
  338. }
  339. currentSize = 0;
  340. index += 1;
  341. }
  342. } else {
  343. letters[index].ind = index;
  344. letters[index].extra = 0;
  345. index += 1;
  346. }
  347. }
  348. documentData.l = letters;
  349. maxLineWidth = lineWidth > maxLineWidth ? lineWidth : maxLineWidth;
  350. lineWidths.push(lineWidth);
  351. if (documentData.sz) {
  352. documentData.boxWidth = documentData.sz[0];
  353. documentData.justifyOffset = 0;
  354. } else {
  355. documentData.boxWidth = maxLineWidth;
  356. switch (documentData.j) {
  357. case 1:
  358. documentData.justifyOffset = -documentData.boxWidth;
  359. break;
  360. case 2:
  361. documentData.justifyOffset = -documentData.boxWidth / 2;
  362. break;
  363. default:
  364. documentData.justifyOffset = 0;
  365. }
  366. }
  367. documentData.lineWidths = lineWidths;
  368. var animators = data.a; var animatorData; var
  369. letterData;
  370. jLen = animators.length;
  371. var based; var ind; var
  372. indexes = [];
  373. for (j = 0; j < jLen; j += 1) {
  374. animatorData = animators[j];
  375. if (animatorData.a.sc) {
  376. documentData.strokeColorAnim = true;
  377. }
  378. if (animatorData.a.sw) {
  379. documentData.strokeWidthAnim = true;
  380. }
  381. if (animatorData.a.fc || animatorData.a.fh || animatorData.a.fs || animatorData.a.fb) {
  382. documentData.fillColorAnim = true;
  383. }
  384. ind = 0;
  385. based = animatorData.s.b;
  386. for (i = 0; i < len; i += 1) {
  387. letterData = letters[i];
  388. letterData.anIndexes[j] = ind;
  389. if ((based == 1 && letterData.val !== '') || (based == 2 && letterData.val !== '' && letterData.val !== ' ') || (based == 3 && (letterData.n || letterData.val == ' ' || i == len - 1)) || (based == 4 && (letterData.n || i == len - 1))) { // eslint-disable-line eqeqeq
  390. if (animatorData.s.rn === 1) {
  391. indexes.push(ind);
  392. }
  393. ind += 1;
  394. }
  395. }
  396. data.a[j].s.totalChars = ind;
  397. var currentInd = -1; var
  398. newInd;
  399. if (animatorData.s.rn === 1) {
  400. for (i = 0; i < len; i += 1) {
  401. letterData = letters[i];
  402. if (currentInd != letterData.anIndexes[j]) { // eslint-disable-line eqeqeq
  403. currentInd = letterData.anIndexes[j];
  404. newInd = indexes.splice(Math.floor(Math.random() * indexes.length), 1)[0];
  405. }
  406. letterData.anIndexes[j] = newInd;
  407. }
  408. }
  409. }
  410. documentData.yOffset = documentData.finalLineHeight || documentData.finalSize * 1.2;
  411. documentData.ls = documentData.ls || 0;
  412. documentData.ascent = (fontData.ascent * documentData.finalSize) / 100;
  413. };
  414. TextProperty.prototype.updateDocumentData = function (newData, index) {
  415. index = index === undefined ? this.keysIndex : index;
  416. var dData = this.copyData({}, this.data.d.k[index].s);
  417. dData = this.copyData(dData, newData);
  418. this.data.d.k[index].s = dData;
  419. this.recalculate(index);
  420. this.setCurrentData(dData);
  421. this.elem.addDynamicProperty(this);
  422. };
  423. TextProperty.prototype.recalculate = function (index) {
  424. var dData = this.data.d.k[index].s;
  425. dData.__complete = false;
  426. this.keysIndex = 0;
  427. this._isFirstFrame = true;
  428. this.getValue(dData);
  429. };
  430. TextProperty.prototype.canResizeFont = function (_canResize) {
  431. this.canResize = _canResize;
  432. this.recalculate(this.keysIndex);
  433. this.elem.addDynamicProperty(this);
  434. };
  435. TextProperty.prototype.setMinimumFontSize = function (_fontValue) {
  436. this.minimumFontSize = Math.floor(_fontValue) || 1;
  437. this.recalculate(this.keysIndex);
  438. this.elem.addDynamicProperty(this);
  439. };
  440. export default TextProperty;