* Returns the width of the given segment of inline text in CSS pixels. * @param {string} text - the text to be measured * @param {CanvasRenderingContext2D} [context] - reference to an active 2d context for canvas rendering * @returns {number} the width of the given segment of inline text in CS
(text, context)
| 47 | * @returns {number} the width of the given segment of inline text in CSS pixels. |
| 48 | */ |
| 49 | lineWidth(text, context) { |
| 50 | if (this.ancestor instanceof Text) { |
| 51 | return context.measureText(text).width; |
| 52 | } else { |
| 53 | // it's a BitmapText |
| 54 | const characters = text.split(""); |
| 55 | const charactersLength = characters.length; |
| 56 | let width = 0; |
| 57 | let lastGlyph = null; |
| 58 | for (let i = 0; i < charactersLength; i++) { |
| 59 | const ch = characters[i].charCodeAt(0); |
| 60 | const glyph = this.ancestor.fontData.glyphs[ch]; |
| 61 | if (typeof glyph !== "undefined") { |
| 62 | const kerning = |
| 63 | lastGlyph && lastGlyph.kerning ? lastGlyph.getKerning(ch) : 0; |
| 64 | // for the last glyph, use the visual extent if it exceeds xadvance |
| 65 | const advance = |
| 66 | i < charactersLength - 1 |
| 67 | ? glyph.xadvance |
| 68 | : Math.max(glyph.xadvance, glyph.xoffset + glyph.width); |
| 69 | width += (advance + kerning) * this.ancestor.fontScale.x; |
| 70 | lastGlyph = glyph; |
| 71 | } |
| 72 | } |
| 73 | return width; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * measure the given text size in CSS pixels |
no test coverage detected