|
1 | 1 | var BitmapTextCanvasRenderer = function (renderer, src, interpolationPercentage, camera) |
2 | 2 | { |
| 3 | + if (this.renderMask !== this.renderFlags) |
| 4 | + { |
| 5 | + return; |
| 6 | + } |
| 7 | + var cameraScrollX = camera.scrollX; |
| 8 | + var cameraScrollY = camera.scrollY; |
| 9 | + var text = src.text; |
| 10 | + var textLength = text.length; |
| 11 | + var chars = src.fontData.chars; |
| 12 | + var lineHeight = src.fontData.lineHeight; |
| 13 | + var srcX = src.x; |
| 14 | + var srcY = src.y; |
| 15 | + var xAdvance = 0; |
| 16 | + var yAdvance = 0; |
| 17 | + var indexCount = 0; |
| 18 | + var charCode = 0; |
| 19 | + var glyph = null; |
| 20 | + var glyphX = 0; |
| 21 | + var glyphY = 0; |
| 22 | + var glyphW = 0; |
| 23 | + var glyphH = 0; |
| 24 | + var x = 0; |
| 25 | + var y = 0; |
| 26 | + var lastGlyph = null; |
| 27 | + var lastCharCode = 0; |
| 28 | + var ctx = renderer.currentContext; |
| 29 | + var image = src.frame.source.image; |
3 | 30 |
|
| 31 | + // Blend Mode |
| 32 | + if (renderer.currentBlendMode !== src.blendMode) |
| 33 | + { |
| 34 | + renderer.currentBlendMode = src.blendMode; |
| 35 | + ctx.globalCompositeOperation = renderer.blendModes[src.blendMode]; |
| 36 | + } |
| 37 | + |
| 38 | + // Alpha |
| 39 | + if (renderer.currentAlpha !== src.alpha) |
| 40 | + { |
| 41 | + renderer.currentAlpha = src.alpha; |
| 42 | + ctx.globalAlpha = src.alpha; |
| 43 | + } |
| 44 | + |
| 45 | + // Smoothing |
| 46 | + if (renderer.currentScaleMode !== src.scaleMode) |
| 47 | + { |
| 48 | + renderer.currentScaleMode = src.scaleMode; |
| 49 | + } |
| 50 | + |
| 51 | + for (var index = 0; index < textLength; ++index) |
| 52 | + { |
| 53 | + charCode = text.charCodeAt(index); |
| 54 | + if (charCode === 10) |
| 55 | + { |
| 56 | + xAdvance = 0; |
| 57 | + indexCount = 0; |
| 58 | + yAdvance += lineHeight; |
| 59 | + lastGlyph = null; |
| 60 | + continue; |
| 61 | + } |
| 62 | + |
| 63 | + glyph = chars[charCode]; |
| 64 | + if (!glyph) |
| 65 | + { |
| 66 | + continue; |
| 67 | + } |
| 68 | + |
| 69 | + glyphX = glyph.x; |
| 70 | + glyphY = glyph.y; |
| 71 | + glyphW = glyph.width; |
| 72 | + glyphH = glyph.height; |
| 73 | + x = (srcX + indexCount + glyph.xOffset + xAdvance) - cameraScrollX; |
| 74 | + y = (srcY + glyph.yOffset + yAdvance) - cameraScrollY; |
| 75 | + |
| 76 | + if (lastGlyph !== null) |
| 77 | + { |
| 78 | + var kerningOffset = glyph.kerning[lastCharCode]; |
| 79 | + x += (kerningOffset !== undefined) ? kerningOffset : 0; |
| 80 | + } |
| 81 | + |
| 82 | + ctx.save(); |
| 83 | + ctx.translate(x, y); |
| 84 | + ctx.drawImage(image, glyphX, glyphY, glyphW, glyphH, 0, 0, glyphW, glyphH); |
| 85 | + ctx.restore(); |
| 86 | + |
| 87 | + xAdvance += glyph.xAdvance; |
| 88 | + indexCount += 1; |
| 89 | + lastGlyph = glyph; |
| 90 | + lastCharCode = charCode; |
| 91 | + } |
4 | 92 | }; |
5 | 93 |
|
6 | 94 | module.exports = BitmapTextCanvasRenderer; |
0 commit comments