forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDynamicBitmapTextCanvasRenderer.js
More file actions
166 lines (125 loc) · 4.04 KB
/
Copy pathDynamicBitmapTextCanvasRenderer.js
File metadata and controls
166 lines (125 loc) · 4.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
var GameObject = require('../../GameObject');
var DynamicBitmapTextCanvasRenderer = function (renderer, src, interpolationPercentage, camera)
{
var text = src.text;
var textLength = text.length;
if (GameObject.RENDER_MASK !== src.renderFlags || textLength === 0 || (src.cameraFilter > 0 && (src.cameraFilter & camera._id)))
{
return;
}
var textureFrame = src.frame;
var displayCallback = src.displayCallback;
var cameraScrollX = camera.scrollX * src.scrollFactorX;
var cameraScrollY = camera.scrollY * src.scrollFactorY;
var chars = src.fontData.chars;
var lineHeight = src.fontData.lineHeight;
var xAdvance = 0;
var yAdvance = 0;
var indexCount = 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 ctx = renderer.currentContext;
var image = src.frame.source.image;
var textureX = textureFrame.cutX;
var textureY = textureFrame.cutY;
var rotation = 0;
var scale = (src.fontSize / src.fontData.size);
// Blend Mode
if (renderer.currentBlendMode !== src.blendMode)
{
renderer.currentBlendMode = src.blendMode;
ctx.globalCompositeOperation = renderer.blendModes[src.blendMode];
}
// Alpha
if (renderer.currentAlpha !== src.alpha)
{
renderer.currentAlpha = src.alpha;
ctx.globalAlpha = src.alpha;
}
// Smoothing
if (renderer.currentScaleMode !== src.scaleMode)
{
renderer.currentScaleMode = src.scaleMode;
}
ctx.save();
ctx.translate(src.x, src.y);
ctx.rotate(src.rotation);
ctx.scale(src.scaleX, src.scaleY);
if (src.cropWidth > 0 && src.cropHeight > 0)
{
ctx.save();
ctx.beginPath();
ctx.rect(0, 0, src.cropWidth, src.cropHeight);
ctx.clip();
}
for (var index = 0; index < textLength; ++index)
{
// Reset the scale (in case the callback changed it)
scale = (src.fontSize / src.fontData.size);
rotation = 0;
charCode = text.charCodeAt(index);
if (charCode === 10)
{
xAdvance = 0;
indexCount = 0;
yAdvance += lineHeight;
lastGlyph = null;
continue;
}
glyph = chars[charCode];
if (!glyph)
{
continue;
}
glyphX = textureX + glyph.x;
glyphY = textureY + glyph.y;
glyphW = glyph.width;
glyphH = glyph.height;
x = (indexCount + glyph.xOffset + xAdvance) - src.scrollX;
y = (glyph.yOffset + yAdvance) - src.scrollY;
// This could be optimized so that it doesn't even bother drawing it if the x/y is out of range
if (lastGlyph !== null)
{
var kerningOffset = glyph.kerning[lastCharCode];
x += (kerningOffset !== undefined) ? kerningOffset : 0;
}
if (displayCallback)
{
var output = displayCallback({ tint: { topLeft: 0, topRight: 0, bottomLeft: 0, bottomRight: 0 }, index: index, charCode: charCode, x: x, y: y, scale: scale, rotation: 0, data: glyph.data });
x = output.x;
y = output.y;
scale = output.scale;
rotation = output.rotation;
}
x *= scale;
y *= scale;
x -= cameraScrollX;
y -= cameraScrollY;
ctx.save();
ctx.translate(x, y);
ctx.rotate(rotation);
ctx.scale(scale, scale);
// ctx.fillStyle = 'rgba(0,255,0,0.2)';
// ctx.fillRect(0, 0, glyphW, glyphH);
ctx.drawImage(image, glyphX, glyphY, glyphW, glyphH, 0, 0, glyphW, glyphH);
ctx.restore();
xAdvance += glyph.xAdvance;
indexCount += 1;
lastGlyph = glyph;
lastCharCode = charCode;
}
if (src.cropWidth > 0 && src.cropHeight > 0)
{
ctx.restore();
}
ctx.restore();
};
module.exports = DynamicBitmapTextCanvasRenderer;