|
| 1 | + |
| 2 | +var GetBitmapTextSize = function (src) |
| 3 | +{ |
| 4 | + var text = src.text; |
| 5 | + var textLength = text.length; |
| 6 | + |
| 7 | + var bounds = { |
| 8 | + x: 0, |
| 9 | + y: 0, |
| 10 | + width: 0, |
| 11 | + height: 0 |
| 12 | + }; |
| 13 | + |
| 14 | + if (textLength === 0) |
| 15 | + { |
| 16 | + console.log('bailed'); |
| 17 | + return bounds; |
| 18 | + } |
| 19 | + |
| 20 | + bounds.x = Number.MAX_VALUE; |
| 21 | + bounds.y = Number.MAX_VALUE; |
| 22 | + bounds.width = -1; |
| 23 | + bounds.height = -1; |
| 24 | + |
| 25 | + var textureFrame = src.frame; |
| 26 | + |
| 27 | + var chars = src.fontData.chars; |
| 28 | + var lineHeight = src.fontData.lineHeight; |
| 29 | + |
| 30 | + var xAdvance = 0; |
| 31 | + var yAdvance = 0; |
| 32 | + |
| 33 | + var indexCount = 0; |
| 34 | + var charCode = 0; |
| 35 | + |
| 36 | + var glyph = null; |
| 37 | + var glyphX = 0; |
| 38 | + var glyphY = 0; |
| 39 | + var glyphW = 0; |
| 40 | + var glyphH = 0; |
| 41 | + |
| 42 | + var x = 0; |
| 43 | + var y = 0; |
| 44 | + |
| 45 | + var lastGlyph = null; |
| 46 | + var lastCharCode = 0; |
| 47 | + |
| 48 | + var textureX = textureFrame.cutX; |
| 49 | + var textureY = textureFrame.cutY; |
| 50 | + |
| 51 | + var scale = (src.fontSize / src.fontData.size); |
| 52 | + |
| 53 | + for (var index = 0; index < textLength; ++index) |
| 54 | + { |
| 55 | + charCode = text.charCodeAt(index); |
| 56 | + |
| 57 | + if (charCode === 10) |
| 58 | + { |
| 59 | + xAdvance = 0; |
| 60 | + indexCount = 0; |
| 61 | + yAdvance += lineHeight; |
| 62 | + lastGlyph = null; |
| 63 | + continue; |
| 64 | + } |
| 65 | + |
| 66 | + glyph = chars[charCode]; |
| 67 | + |
| 68 | + if (!glyph) |
| 69 | + { |
| 70 | + continue; |
| 71 | + } |
| 72 | + |
| 73 | + glyphX = textureX + glyph.x; |
| 74 | + glyphY = textureY + glyph.y; |
| 75 | + |
| 76 | + glyphW = glyph.width; |
| 77 | + glyphH = glyph.height; |
| 78 | + |
| 79 | + x = indexCount + glyph.xOffset + xAdvance; |
| 80 | + y = glyph.yOffset + yAdvance; |
| 81 | + |
| 82 | + if (lastGlyph !== null) |
| 83 | + { |
| 84 | + var kerningOffset = glyph.kerning[lastCharCode]; |
| 85 | + x += (kerningOffset !== undefined) ? kerningOffset : 0; |
| 86 | + } |
| 87 | + |
| 88 | + x *= scale; |
| 89 | + y *= scale; |
| 90 | + |
| 91 | + // ctx.save(); |
| 92 | + // ctx.translate(x, y); |
| 93 | + // ctx.rotate(rotation); |
| 94 | + // ctx.scale(scale, scale); |
| 95 | + |
| 96 | + // ctx.fillStyle = 'rgb(255,0,255)'; |
| 97 | + // ctx.fillRect(0, 0, glyphW, glyphH); |
| 98 | + |
| 99 | + // ctx.drawImage(image, glyphX, glyphY, glyphW, glyphH, 0, 0, glyphW, glyphH); |
| 100 | + |
| 101 | + // var xs = x * scale; |
| 102 | + // var ys = y * scale; |
| 103 | + |
| 104 | + if (bounds.x > x) |
| 105 | + { |
| 106 | + bounds.x = x; |
| 107 | + } |
| 108 | + |
| 109 | + if (bounds.y > y) |
| 110 | + { |
| 111 | + bounds.y = y; |
| 112 | + } |
| 113 | + |
| 114 | + var gw = x + (glyphW * scale); |
| 115 | + var gh = y + (glyphH * scale); |
| 116 | + |
| 117 | + if (bounds.width < gw) |
| 118 | + { |
| 119 | + bounds.width = gw - bounds.x; |
| 120 | + } |
| 121 | + |
| 122 | + if (bounds.height < gh) |
| 123 | + { |
| 124 | + bounds.height = gh - bounds.y; |
| 125 | + } |
| 126 | + |
| 127 | + // console.log('Letter', text[index]); |
| 128 | + // console.log('pos', x, y); |
| 129 | + // console.log('wh', glyphW, glyphH); |
| 130 | + // console.log('scaled', gw, gh); |
| 131 | + // console.log('bounds', bounds.width, bounds.height); |
| 132 | + |
| 133 | + xAdvance += glyph.xAdvance; |
| 134 | + indexCount += 1; |
| 135 | + lastGlyph = glyph; |
| 136 | + lastCharCode = charCode; |
| 137 | + } |
| 138 | + |
| 139 | + return bounds; |
| 140 | +}; |
| 141 | + |
| 142 | +module.exports = GetBitmapTextSize; |
0 commit comments