2018-03-30 17:46:44 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
|
|
|
* @copyright 2018 Photon Storm Ltd.
|
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
*/
|
|
|
|
|
2018-08-03 18:02:43 +00:00
|
|
|
var SetTransform = require('../../../renderer/canvas/utils/SetTransform');
|
|
|
|
|
2018-03-30 17:46:44 +00:00
|
|
|
/**
|
|
|
|
* Renders this Game Object with the Canvas Renderer to the given Camera.
|
|
|
|
* The object will not render if any of its renderFlags are set or it is being actively filtered out by the Camera.
|
|
|
|
* This method should not be called directly. It is a utility function of the Render module.
|
|
|
|
*
|
|
|
|
* @method Phaser.GameObjects.BitmapText#renderCanvas
|
|
|
|
* @since 3.0.0
|
|
|
|
* @private
|
|
|
|
*
|
|
|
|
* @param {Phaser.Renderer.Canvas.CanvasRenderer} renderer - A reference to the current active Canvas renderer.
|
|
|
|
* @param {Phaser.GameObjects.BitmapText} src - The Game Object being rendered in this call.
|
|
|
|
* @param {number} interpolationPercentage - Reserved for future use and custom pipelines.
|
|
|
|
* @param {Phaser.Cameras.Scene2D.Camera} camera - The Camera that is rendering the Game Object.
|
2018-04-04 16:14:55 +00:00
|
|
|
* @param {Phaser.GameObjects.Components.TransformMatrix} parentMatrix - This transform matrix is defined if the game object is nested
|
2018-03-30 17:46:44 +00:00
|
|
|
*/
|
2018-04-04 16:14:55 +00:00
|
|
|
var BitmapTextCanvasRenderer = function (renderer, src, interpolationPercentage, camera, parentMatrix)
|
2018-03-30 17:46:44 +00:00
|
|
|
{
|
2018-07-09 20:57:51 +00:00
|
|
|
var text = src._text;
|
2018-03-30 17:46:44 +00:00
|
|
|
var textLength = text.length;
|
|
|
|
|
2018-08-03 18:02:43 +00:00
|
|
|
var ctx = renderer.currentContext;
|
|
|
|
|
|
|
|
if (textLength === 0 || !SetTransform(renderer, ctx, src, camera, parentMatrix))
|
2018-03-30 17:46:44 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var textureFrame = src.frame;
|
|
|
|
|
|
|
|
var chars = src.fontData.chars;
|
|
|
|
var lineHeight = src.fontData.lineHeight;
|
2018-07-09 20:57:51 +00:00
|
|
|
var letterSpacing = src._letterSpacing;
|
2018-03-30 17:46:44 +00:00
|
|
|
|
|
|
|
var xAdvance = 0;
|
|
|
|
var yAdvance = 0;
|
|
|
|
|
|
|
|
var charCode = 0;
|
|
|
|
|
|
|
|
var glyph = null;
|
|
|
|
var glyphX = 0;
|
|
|
|
var glyphY = 0;
|
|
|
|
var glyphW = 0;
|
|
|
|
var glyphH = 0;
|
|
|
|
|
|
|
|
var x = 0;
|
|
|
|
var y = 0;
|
|
|
|
|
|
|
|
var lastGlyph = null;
|
|
|
|
var lastCharCode = 0;
|
|
|
|
|
|
|
|
var image = src.frame.source.image;
|
|
|
|
|
|
|
|
var textureX = textureFrame.cutX;
|
|
|
|
var textureY = textureFrame.cutY;
|
|
|
|
|
2018-07-09 20:57:51 +00:00
|
|
|
var scale = (src._fontSize / src.fontData.size);
|
|
|
|
|
|
|
|
var align = src._align;
|
|
|
|
var currentLine = 0;
|
|
|
|
var lineOffsetX = 0;
|
|
|
|
|
|
|
|
// Update the bounds - skipped internally if not dirty
|
|
|
|
src.getTextBounds(false);
|
|
|
|
|
|
|
|
var lineData = src._bounds.lines;
|
|
|
|
|
|
|
|
if (align === 1)
|
|
|
|
{
|
|
|
|
lineOffsetX = (lineData.longest - lineData.lengths[0]) / 2;
|
|
|
|
}
|
|
|
|
else if (align === 2)
|
|
|
|
{
|
|
|
|
lineOffsetX = (lineData.longest - lineData.lengths[0]);
|
|
|
|
}
|
2018-03-30 17:46:44 +00:00
|
|
|
|
2018-08-03 18:02:43 +00:00
|
|
|
ctx.translate(-src.displayOriginX, -src.displayOriginY);
|
|
|
|
|
|
|
|
var roundPixels = camera.roundPixels;
|
2018-03-30 17:46:44 +00:00
|
|
|
|
2018-07-09 20:57:51 +00:00
|
|
|
for (var i = 0; i < textLength; i++)
|
2018-03-30 17:46:44 +00:00
|
|
|
{
|
2018-07-09 20:57:51 +00:00
|
|
|
charCode = text.charCodeAt(i);
|
2018-03-30 17:46:44 +00:00
|
|
|
|
|
|
|
if (charCode === 10)
|
|
|
|
{
|
2018-07-09 20:57:51 +00:00
|
|
|
currentLine++;
|
|
|
|
|
|
|
|
if (align === 1)
|
|
|
|
{
|
|
|
|
lineOffsetX = (lineData.longest - lineData.lengths[currentLine]) / 2;
|
|
|
|
}
|
|
|
|
else if (align === 2)
|
|
|
|
{
|
|
|
|
lineOffsetX = (lineData.longest - lineData.lengths[currentLine]);
|
|
|
|
}
|
|
|
|
|
2018-03-30 17:46:44 +00:00
|
|
|
xAdvance = 0;
|
|
|
|
yAdvance += lineHeight;
|
|
|
|
lastGlyph = null;
|
2018-07-09 20:57:51 +00:00
|
|
|
|
2018-03-30 17:46:44 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
glyph = chars[charCode];
|
|
|
|
|
|
|
|
if (!glyph)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
glyphX = textureX + glyph.x;
|
|
|
|
glyphY = textureY + glyph.y;
|
|
|
|
|
|
|
|
glyphW = glyph.width;
|
|
|
|
glyphH = glyph.height;
|
|
|
|
|
2018-07-09 15:06:36 +00:00
|
|
|
x = glyph.xOffset + xAdvance;
|
2018-03-30 17:46:44 +00:00
|
|
|
y = glyph.yOffset + yAdvance;
|
|
|
|
|
|
|
|
if (lastGlyph !== null)
|
|
|
|
{
|
|
|
|
var kerningOffset = glyph.kerning[lastCharCode];
|
|
|
|
x += (kerningOffset !== undefined) ? kerningOffset : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
x *= scale;
|
|
|
|
y *= scale;
|
|
|
|
|
2018-07-09 20:57:51 +00:00
|
|
|
x += lineOffsetX;
|
|
|
|
|
2018-04-02 13:42:24 +00:00
|
|
|
xAdvance += glyph.xAdvance + letterSpacing;
|
2018-03-30 17:46:44 +00:00
|
|
|
lastGlyph = glyph;
|
|
|
|
lastCharCode = charCode;
|
|
|
|
|
|
|
|
// Nothing to render or a space? Then skip to the next glyph
|
|
|
|
if (glyphW === 0 || glyphH === 0 || charCode === 32)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-07-09 20:57:51 +00:00
|
|
|
if (roundPixels)
|
2018-03-30 17:46:44 +00:00
|
|
|
{
|
2018-11-30 10:27:25 +00:00
|
|
|
x = Math.round(x);
|
|
|
|
y = Math.round(y);
|
2018-03-30 17:46:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx.save();
|
|
|
|
|
|
|
|
ctx.translate(x, y);
|
|
|
|
|
|
|
|
ctx.scale(scale, scale);
|
|
|
|
|
|
|
|
ctx.drawImage(image, glyphX, glyphY, glyphW, glyphH, 0, 0, glyphW, glyphH);
|
|
|
|
|
|
|
|
ctx.restore();
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.restore();
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = BitmapTextCanvasRenderer;
|