Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2017
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.39 KB | None | 0 0
  1. fabric.SVGText = fabric.util.createClass(fabric.Object, {
  2.  
  3. type :'text-block',
  4. text :'',
  5. fontSize :24,
  6. color :void 0,
  7. lineColors :void 0,
  8. selectedLine:0,
  9. realWidth :0,
  10. realHeight :0,
  11. minWidth:0,
  12. minHeight:0,
  13. maxWidth:9999999,
  14. maxHeight:9999999,
  15. price:0,
  16. toObjectProperties:[],
  17. widths: [],
  18. font:null,
  19. fontFamily:null,
  20. lines:null,
  21. textAlign:'left',
  22.  
  23. initialize: function (objects, options) {
  24. this.callSuper('initialize');
  25.  
  26. if (options) {
  27. fabric.util.object.extend(this, options);
  28. //this.color = Backbone.toModel(this.color,Color);
  29. }
  30. this.lines = [];
  31. this.lineColors = [];
  32. options && this.set('minWidth', options.minWidth) && this.set('minHeight', options.minHeight) && this.set('maxWidth', options.maxWidth) && this.set('maxHeight', options.maxHeight) && this.set('toObjectProperties', options.toObjectProperties) && this.set('color', options.color);
  33.  
  34. this.selectedLine = 0;
  35. },
  36.  
  37. _render: function(ctx) {
  38.  
  39. var numLines = this.lines.length;
  40. ctx.save();
  41. if (numLines > 0) {
  42. var em = parseFloat(this.font.fontface.unitsPerEm);
  43. var lineHeight = parseFloat(this.font.fontface.lineHeight);
  44. var fontScale = this.fontSize / em;
  45. ctx.scale(1, -1);
  46. ctx.scale(fontScale, fontScale);
  47. var firstLetter = this.lines[0][0];
  48. var xStart = em / 2;
  49. var yStart = /*em*/lineHeight / 2;
  50. ctx.translate(this.width/2/fontScale*-1+xStart,-this.font.fontface.descent);
  51.  
  52. if (numLines > 1) {
  53. ctx.translate(0, this.height / 2 / fontScale - yStart);
  54. }
  55.  
  56. var lastLineWidth = 0;
  57. for(var i=0; i<numLines; i++){
  58. var line = this.lines[i];
  59. var numLetters = line.length;
  60. var lineWidth = 0;
  61. if (this.textAlign == "center") {
  62. if (lastLineWidth > 0)
  63. ctx.translate(-(this.width / fontScale - lastLineWidth) / 2, 0);
  64. for(var l=0; l<numLetters; l++){
  65. var letter = line[l];
  66. var xOff = letter.glyph.horizAdvX - letter.kerning;
  67.  
  68. lineWidth += xOff;
  69. }
  70.  
  71. ctx.translate((this.width / fontScale - lineWidth) / 2, 0);
  72. }
  73. else if (this.textAlign == "right") {
  74. if (lastLineWidth > 0){
  75. ctx.translate(-(this.width / fontScale - lastLineWidth), 0);
  76. }
  77. for (var l = 0; l < numLetters; l++) {
  78. var letter = line[l];
  79. var xOff = letter.glyph.horizAdvX - letter.kerning;
  80. lineWidth += xOff;
  81. }
  82. ctx.translate(this.width / fontScale-lineWidth,0);
  83. }
  84. lineWidth = 0;
  85. for (var l = 0; l < numLetters; l++) {
  86. var letter = line[l];
  87. letter.render(ctx,true);
  88. var xOff = letter.glyph.horizAdvX - letter.kerning;
  89. ctx.translate(xOff,0);
  90. lineWidth += xOff;
  91. }
  92. ctx.translate(-lineWidth,-/*em*/lineHeight);
  93. lastLineWidth = lineWidth;
  94. }
  95. }
  96. ctx.restore();
  97. },
  98.  
  99. previewColor:function (value, callback) {
  100. this.originalColor = this.color;
  101. this.color = this.lineColors[this.selectedLine] = value;
  102. //this.applyColor(this.color, callback);
  103. this.updateGlyphColors();
  104. callback && callback();
  105. },
  106.  
  107. stopPreviewColor:function (callback) {
  108. this.color = this.lineColors[this.selectedLine] = this.originalColor;
  109. this.updateGlyphColors();
  110. callback && callback();
  111. },
  112.  
  113. setColor:function (value, callback) {
  114. this.originalColor = this.color = this.lineColors[this.selectedLine] = value;
  115. //this.applyColor(value,callback);
  116. this.updateGlyphColors();
  117. callback && callback();
  118. },
  119. setColorAt: function(value,index,callback){
  120. this.lineColors[index] = value;
  121. this.updateGlyphColors();
  122. callback && callback();
  123. },
  124. setColorToAllLines:function(value,callback){
  125.  
  126. this.originalColor = this.color = value;
  127.  
  128. var len = this.lineColors.length;
  129. if(len == 0){
  130. this.lineColors[0] = value;
  131. }else{
  132. for(var i = 0; i < len; i++){
  133. this.lineColors[i] = value;
  134. }
  135. }
  136. this.updateGlyphColors();
  137. callback && callback();
  138. },
  139.  
  140. setLineColors:function(value,callback){
  141. this.lineColors = value;
  142. this.updateGlyphColors();
  143. callback && callback();
  144. },
  145.  
  146. getColors:function(){
  147. return this.lineColors;
  148. },
  149.  
  150. getActiveColor:function(){
  151. return this.lineColors[this.selectedLine];
  152. },
  153.  
  154. applyColor:function (value, callback) {
  155. if (value.get('pattern')) {
  156. this.callSuper('set', 'fill', value.get('pattern'));
  157. } else {
  158. this.callSuper('setColor', fabric.util.color.toHex(value.get('hex')));
  159. }
  160. this.updateGlyphColors();
  161. },
  162.  
  163. getLineColor:function (line) {
  164. if (this.lineColors[line] != undefined) {
  165. var color = this.lineColors[line];
  166. if (color.get('pattern')) {
  167. return color.get('pattern');
  168. } else {
  169. return fabric.util.color.toHex(color.get('hex'));
  170. }
  171. } else {
  172. return this.fill;
  173. }
  174. },
  175.  
  176. setFont: function(value){
  177. this.font = value;
  178. this.createGlyphs();
  179. return this;
  180. },
  181.  
  182. setFontFamily: function(value){
  183. this.fontFamily = value;
  184. this.createGlyphs();
  185. return this;
  186. },
  187.  
  188. setText: function(value){
  189. this.text = value;
  190. this.createGlyphs();
  191. return this;
  192. },
  193.  
  194. getText: function(){
  195. return this.text;
  196. },
  197.  
  198. removeAllObjects: function(){
  199.  
  200. },
  201.  
  202. getTextLines: function(){
  203. return this.text ? this.text.split(/\r?\n/) : [];
  204. },
  205.  
  206. createGlyphs: function(){
  207. this.lines = [];
  208.  
  209. if(this.font && this.text){
  210. var textLines = this.getTextLines();
  211. var numLines = textLines.length;
  212. this.widths = [];
  213. var w = 0;
  214. for(var i=0; i<numLines; i++){
  215. var line = textLines[i];
  216. var addLine = [];
  217. var numChars = line.length;
  218. var fill = this.getLineColor(i);
  219. var lineWidth = 0;
  220.  
  221. for(var f=0; f<numChars; f++){
  222. var chr = line.charAt(f);
  223. var nextChar = line.charAt(f+1);
  224. var letter = this.drawGlyph(chr,nextChar);
  225. letter.set('fill',fill);
  226. addLine.push(letter);
  227. lineWidth += letter.glyph.horizAdvX - letter.kerning;
  228. }
  229. this.widths.push( lineWidth * this.fontSize / parseFloat(this.font.fontface.unitsPerEm));
  230. w = w < lineWidth ? lineWidth : w;
  231. this.lines.push(addLine);
  232. }
  233.  
  234. var em = parseFloat(this.font.fontface.unitsPerEm);
  235. var fontScale = this.fontSize / em;
  236.  
  237. this.width = w * fontScale;
  238. this.height = /*em*/ this.font.fontface.lineHeight * numLines * fontScale;
  239.  
  240. }
  241.  
  242. },
  243.  
  244. updateGlyphColors: function(){
  245. var len = this.lines.length;
  246. for(var i=0; i<len; i++){
  247. var o = this.lines[i];
  248. var numLetters = o.length;
  249. for(var l=0; l<numLetters; l++){
  250. var letter = o[l];
  251. letter.set('fill', this.getLineColor(i));
  252. }
  253.  
  254. };
  255. },
  256.  
  257. getMaxGlyphHeight: function (){
  258. return Math.abs(this.font.fontface.descent) + Math.abs(this.font.fontface.ascent);
  259. },
  260.  
  261. getGlyph: function (unicode){
  262. var code = unicode.charCodeAt(0).toString(16);
  263. var hexCode = '&#x' + code.toUpperCase();
  264. return this.font.glyphs[unicode] ? this.font.glyphs[unicode] : this.font.glyphs[hexCode];
  265. },
  266.  
  267. getKerning: function(g,ng){
  268. var hkern = 0;
  269. if(ng){
  270. var g1 = g.glyphName;
  271. var g2 = ng.glyphName;
  272.  
  273. //console.log(g1,g2);
  274. if(g1 && g2 && this.font.hkerns[g1] && this.font.hkerns[g1][g2]){
  275. return parseFloat(this.font.hkerns[g1][g2]);
  276. }
  277.  
  278. var u1 = g.unicode;
  279. var u2 = ng.unicode;
  280. if(this.font.hkerns[u1] && this.font.hkerns[u1][u2]){
  281. return parseFloat(this.font.hkerns[u1][u2]);
  282. }
  283.  
  284. if(u1 && u1.substr(0,3) != '&#x'){
  285. var ux1 = u1.charCodeAt(0).toString(16);
  286. var c1 = '&#X' + ux1.toUpperCase()+';';
  287. }else{
  288. c1 = u1;
  289. }
  290.  
  291. if(u2 && u2.substr(0,3) != '&#x'){
  292. var ux2 = u2.charCodeAt(0).toString(16);
  293. var c2 = '&#X' + ux2.toUpperCase()+';';
  294. }else{
  295. c2 = u2;
  296. }
  297.  
  298. if(this.font.hkerns[c1] && this.font.hkerns[c1][c2]){
  299. return parseFloat(this.font.hkerns[c1][c2]);
  300. }
  301.  
  302. if(this.font.hkerns[u1] && this.font.hkerns[u1][c2]){
  303. return parseFloat(this.font.hkerns[u1][c2]);
  304. }
  305.  
  306. }
  307.  
  308. return hkern;
  309. },
  310.  
  311. drawGlyph: function(unicode, nextChar){
  312.  
  313. var maxGlyphHeight = this.getMaxGlyphHeight();
  314. var g = this.getGlyph(unicode);
  315. var ng = this.getGlyph(nextChar);
  316.  
  317. if(!g) g = this.getMissingGlyph(unicode);
  318.  
  319. var hkern = this.getKerning(g,ng);
  320.  
  321. var em = parseFloat(this.font.fontface.unitsPerEm);
  322. var fontScale = this.fontSize / em;
  323. var p = g.data ? new fabric.Path(g.data,{top:0,left:0,width:this.font.fontface.unitsPerEm,height:this.font.fontface.lineHeight}) : new fabric.Path('Z');
  324. p.glyph = g; p.kerning = hkern;
  325. p.pathHeight = p.height = this.font.fontface.lineHeight;
  326. p.pathWidth = p.width = this.font.fontface.unitsPerEm;
  327. p.minY = 0;
  328. p.pathOffset.y = this.font.fontface.ascent / 2;
  329.  
  330. return p;
  331. },
  332.  
  333. getMissingGlyph: function (unicode){
  334. return this.font.missingGlyph;
  335. },
  336.  
  337. toObject:function () {
  338. var o = { objectParams: {} };
  339. var len = this.toObjectProperties.length;
  340. for(var i = 0; i < len; i++){
  341. o.objectParams[this.toObjectProperties[i]] = this[this.toObjectProperties[i]];
  342. }
  343.  
  344. var lcolors = [];
  345. _(this.lineColors).each(function(col,key){
  346. lcolors[key] = col.attributes;
  347. });
  348.  
  349. o = fabric.util.object.extend(o, {text:this.text, fontFamily: this.fontFamily, fontSize: this.fontSize, textAlign: this.textAlign, widths:this.widths});
  350. o = fabric.util.object.extend(o, {lineColors:lcolors, color: this.color ? this.color.attributes : null, realWidth:this.realWidth, realHeight:this.realHeight, minWidth: this.minWidth, minHeight: this.minHeight, maxWidth: this.maxWidth, maxHeight: this.maxHeight, transformMatrix:this.getMatrix(),oCoords: this.oCoords, price: this.price, width:this.width, height:this.height});
  351. o = fabric.util.object.extend(this.callSuper('toObject'), o);
  352. return o;
  353. }
  354.  
  355. });
  356.  
  357. fabric.SVGText.fromObject = function (object) {
  358. return new fabric.SVGText(object.text, fabric.util.object.clone(object));
  359. };
  360.  
  361. fabric.TextBlock = fabric.SVGText; //alias
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement