measure-text.js 1011 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.measureText = measureText;
  6. exports.measureTextHeight = measureTextHeight;
  7. function measureText(font, text) {
  8. var x = 0;
  9. for (var i = 0; i < text.length; i++) {
  10. if (font.chars[text[i]]) {
  11. var kerning = font.kernings[text[i]] && font.kernings[text[i]][text[i + 1]] ? font.kernings[text[i]][text[i + 1]] : 0;
  12. x += (font.chars[text[i]].xadvance || 0) + kerning;
  13. }
  14. }
  15. return x;
  16. }
  17. function measureTextHeight(font, text, maxWidth) {
  18. var words = text.split(' ');
  19. var line = '';
  20. var textTotalHeight = font.common.lineHeight;
  21. for (var n = 0; n < words.length; n++) {
  22. var testLine = line + words[n] + ' ';
  23. var testWidth = measureText(font, testLine);
  24. if (testWidth > maxWidth && n > 0) {
  25. textTotalHeight += font.common.lineHeight;
  26. line = words[n] + ' ';
  27. } else {
  28. line = testLine;
  29. }
  30. }
  31. return textTotalHeight;
  32. }
  33. //# sourceMappingURL=measure-text.js.map