Skip to content

Commit 1f4e54e

Browse files
committed
Removed duplicate methods from PIXI.Text such as wordWrap and updateText as Phaser overrides them, so it was wasting bytes.
1 parent e5f1f6f commit 1f4e54e

1 file changed

Lines changed: 0 additions & 227 deletions

File tree

src/pixi/text/Text.js

Lines changed: 0 additions & 227 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ PIXI.Text = function(text, style)
5252

5353
this.setText(text);
5454
this.setStyle(style);
55-
5655
};
5756

5857
// constructor
@@ -107,43 +106,6 @@ Object.defineProperty(PIXI.Text.prototype, 'height', {
107106
}
108107
});
109108

110-
/**
111-
* Set the style of the text
112-
*
113-
* @method setStyle
114-
* @param [style] {Object} The style parameters
115-
* @param [style.font='bold 20pt Arial'] {String} The style and size of the font
116-
* @param [style.fill='black'] {Object} A canvas fillstyle that will be used on the text eg 'red', '#00FF00'
117-
* @param [style.align='left'] {String} Alignment for multiline text ('left', 'center' or 'right'), does not affect single line text
118-
* @param [style.stroke='black'] {String} A canvas fillstyle that will be used on the text stroke eg 'blue', '#FCFF00'
119-
* @param [style.strokeThickness=0] {Number} A number that represents the thickness of the stroke. Default is 0 (no stroke)
120-
* @param [style.wordWrap=false] {Boolean} Indicates if word wrap should be used
121-
* @param [style.wordWrapWidth=100] {Number} The width at which text will wrap
122-
* @param [style.dropShadow=false] {Boolean} Set a drop shadow for the text
123-
* @param [style.dropShadowColor='#000000'] {String} A fill style to be used on the dropshadow e.g 'red', '#00FF00'
124-
* @param [style.dropShadowAngle=Math.PI/4] {Number} Set a angle of the drop shadow
125-
* @param [style.dropShadowDistance=5] {Number} Set a distance of the drop shadow
126-
*/
127-
PIXI.Text.prototype.setStyle = function(style)
128-
{
129-
style = style || {};
130-
style.font = style.font || 'bold 20pt Arial';
131-
style.fill = style.fill || 'black';
132-
style.align = style.align || 'left';
133-
style.stroke = style.stroke || 'black'; //provide a default, see: https://github.com/GoodBoyDigital/pixi.js/issues/136
134-
style.strokeThickness = style.strokeThickness || 0;
135-
style.wordWrap = style.wordWrap || false;
136-
style.wordWrapWidth = style.wordWrapWidth || 100;
137-
138-
style.dropShadow = style.dropShadow || false;
139-
style.dropShadowAngle = style.dropShadowAngle || Math.PI / 6;
140-
style.dropShadowDistance = style.dropShadowDistance || 4;
141-
style.dropShadowColor = style.dropShadowColor || 'black';
142-
143-
this.style = style;
144-
this.dirty = true;
145-
};
146-
147109
/**
148110
* Set the copy for the text object. To split a line you can use '\n'.
149111
*
@@ -156,132 +118,6 @@ PIXI.Text.prototype.setText = function(text)
156118
this.dirty = true;
157119
};
158120

159-
/**
160-
* Renders text and updates it when needed
161-
*
162-
* @method updateText
163-
* @private
164-
*/
165-
PIXI.Text.prototype.updateText = function()
166-
{
167-
this.texture.baseTexture.resolution = this.resolution;
168-
169-
this.context.font = this.style.font;
170-
171-
var outputText = this.text;
172-
173-
// word wrap
174-
// preserve original text
175-
if(this.style.wordWrap)outputText = this.wordWrap(this.text);
176-
177-
//split text into lines
178-
var lines = outputText.split(/(?:\r\n|\r|\n)/);
179-
180-
//calculate text width
181-
var lineWidths = [];
182-
var maxLineWidth = 0;
183-
var fontProperties = this.determineFontProperties(this.style.font);
184-
for (var i = 0; i < lines.length; i++)
185-
{
186-
var lineWidth = this.context.measureText(lines[i]).width;
187-
lineWidths[i] = lineWidth;
188-
maxLineWidth = Math.max(maxLineWidth, lineWidth);
189-
}
190-
191-
var width = maxLineWidth + this.style.strokeThickness;
192-
if(this.style.dropShadow)width += this.style.dropShadowDistance;
193-
194-
this.canvas.width = ( width + this.context.lineWidth ) * this.resolution;
195-
196-
//calculate text height
197-
var lineHeight = fontProperties.fontSize + this.style.strokeThickness;
198-
199-
var height = lineHeight * lines.length;
200-
if(this.style.dropShadow)height += this.style.dropShadowDistance;
201-
202-
this.canvas.height = height * this.resolution;
203-
204-
this.context.scale( this.resolution, this.resolution);
205-
206-
if(navigator.isCocoonJS) this.context.clearRect(0,0,this.canvas.width,this.canvas.height);
207-
208-
// used for debugging..
209-
//this.context.fillStyle ="#FF0000"
210-
//this.context.fillRect(0, 0, this.canvas.width,this.canvas.height);
211-
212-
this.context.font = this.style.font;
213-
this.context.strokeStyle = this.style.stroke;
214-
this.context.lineWidth = this.style.strokeThickness;
215-
this.context.textBaseline = 'alphabetic';
216-
//this.context.lineJoin = 'round';
217-
218-
var linePositionX;
219-
var linePositionY;
220-
221-
if(this.style.dropShadow)
222-
{
223-
this.context.fillStyle = this.style.dropShadowColor;
224-
225-
var xShadowOffset = Math.sin(this.style.dropShadowAngle) * this.style.dropShadowDistance;
226-
var yShadowOffset = Math.cos(this.style.dropShadowAngle) * this.style.dropShadowDistance;
227-
228-
for (i = 0; i < lines.length; i++)
229-
{
230-
linePositionX = this.style.strokeThickness / 2;
231-
linePositionY = (this.style.strokeThickness / 2 + i * lineHeight) + fontProperties.ascent;
232-
233-
if(this.style.align === 'right')
234-
{
235-
linePositionX += maxLineWidth - lineWidths[i];
236-
}
237-
else if(this.style.align === 'center')
238-
{
239-
linePositionX += (maxLineWidth - lineWidths[i]) / 2;
240-
}
241-
242-
if(this.style.fill)
243-
{
244-
this.context.fillText(lines[i], linePositionX + xShadowOffset, linePositionY + yShadowOffset);
245-
}
246-
247-
// if(dropShadow)
248-
}
249-
}
250-
251-
//set canvas text styles
252-
this.context.fillStyle = this.style.fill;
253-
254-
//draw lines line by line
255-
for (i = 0; i < lines.length; i++)
256-
{
257-
linePositionX = this.style.strokeThickness / 2;
258-
linePositionY = (this.style.strokeThickness / 2 + i * lineHeight) + fontProperties.ascent;
259-
260-
if(this.style.align === 'right')
261-
{
262-
linePositionX += maxLineWidth - lineWidths[i];
263-
}
264-
else if(this.style.align === 'center')
265-
{
266-
linePositionX += (maxLineWidth - lineWidths[i]) / 2;
267-
}
268-
269-
if(this.style.stroke && this.style.strokeThickness)
270-
{
271-
this.context.strokeText(lines[i], linePositionX, linePositionY);
272-
}
273-
274-
if(this.style.fill)
275-
{
276-
this.context.fillText(lines[i], linePositionX, linePositionY);
277-
}
278-
279-
// if(dropShadow)
280-
}
281-
282-
this.updateTexture();
283-
};
284-
285121
/**
286122
* Updates texture size based on canvas size
287123
*
@@ -460,54 +296,6 @@ PIXI.Text.prototype.determineFontProperties = function(fontStyle)
460296
return properties;
461297
};
462298

463-
/**
464-
* Applies newlines to a string to have it optimally fit into the horizontal
465-
* bounds set by the Text object's wordWrapWidth property.
466-
*
467-
* @method wordWrap
468-
* @param text {String}
469-
* @private
470-
*/
471-
PIXI.Text.prototype.wordWrap = function(text)
472-
{
473-
// Greedy wrapping algorithm that will wrap words as the line grows longer
474-
// than its horizontal bounds.
475-
var result = '';
476-
var lines = text.split('\n');
477-
for (var i = 0; i < lines.length; i++)
478-
{
479-
var spaceLeft = this.style.wordWrapWidth;
480-
var words = lines[i].split(' ');
481-
for (var j = 0; j < words.length; j++)
482-
{
483-
var wordWidth = this.context.measureText(words[j]).width;
484-
var wordWidthWithSpace = wordWidth + this.context.measureText(' ').width;
485-
if(j === 0 || wordWidthWithSpace > spaceLeft)
486-
{
487-
// Skip printing the newline if it's the first word of the line that is
488-
// greater than the word wrap width.
489-
if(j > 0)
490-
{
491-
result += '\n';
492-
}
493-
result += words[j];
494-
spaceLeft = this.style.wordWrapWidth - wordWidth;
495-
}
496-
else
497-
{
498-
spaceLeft -= wordWidthWithSpace;
499-
result += ' ' + words[j];
500-
}
501-
}
502-
503-
if (i < lines.length-1)
504-
{
505-
result += '\n';
506-
}
507-
}
508-
return result;
509-
};
510-
511299
/**
512300
* Returns the bounds of the Text as a rectangle. The bounds calculation takes the worldTransform into account.
513301
*
@@ -526,21 +314,6 @@ PIXI.Text.prototype.getBounds = function(matrix)
526314
return PIXI.Sprite.prototype.getBounds.call(this, matrix);
527315
};
528316

529-
/**
530-
* Destroys this text object.
531-
*
532-
* @method destroy
533-
* @param destroyBaseTexture {Boolean} whether to destroy the base texture as well
534-
*/
535-
PIXI.Text.prototype.destroy = function(destroyBaseTexture)
536-
{
537-
// make sure to reset the the context and canvas.. dont want this hanging around in memory!
538-
this.context = null;
539-
this.canvas = null;
540-
541-
this.texture.destroy(destroyBaseTexture === undefined ? true : destroyBaseTexture);
542-
};
543-
544317
PIXI.Text.fontPropertiesCache = {};
545318
PIXI.Text.fontPropertiesCanvas = document.createElement('canvas');
546319
PIXI.Text.fontPropertiesContext = PIXI.Text.fontPropertiesCanvas.getContext('2d');

0 commit comments

Comments
 (0)