index.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports["default"] = void 0;
  7. var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
  8. var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
  9. var _path = _interopRequireDefault(require("path"));
  10. var _loadBmfont = _interopRequireDefault(require("load-bmfont"));
  11. var _utils = require("@jimp/utils");
  12. var _measureText = require("./measure-text");
  13. function xOffsetBasedOnAlignment(constants, font, line, maxWidth, alignment) {
  14. if (alignment === constants.HORIZONTAL_ALIGN_LEFT) {
  15. return 0;
  16. }
  17. if (alignment === constants.HORIZONTAL_ALIGN_CENTER) {
  18. return (maxWidth - (0, _measureText.measureText)(font, line)) / 2;
  19. }
  20. return maxWidth - (0, _measureText.measureText)(font, line);
  21. }
  22. function drawCharacter(image, font, x, y, _char) {
  23. if (_char.width > 0 && _char.height > 0) {
  24. var characterPage = font.pages[_char.page];
  25. image.blit(characterPage, x + _char.xoffset, y + _char.yoffset, _char.x, _char.y, _char.width, _char.height);
  26. }
  27. return image;
  28. }
  29. function printText(font, x, y, text, defaultCharWidth) {
  30. for (var i = 0; i < text.length; i++) {
  31. var _char2 = void 0;
  32. if (font.chars[text[i]]) {
  33. _char2 = text[i];
  34. } else if (/\s/.test(text[i])) {
  35. _char2 = '';
  36. } else {
  37. _char2 = '?';
  38. }
  39. var fontChar = font.chars[_char2] || {};
  40. var fontKerning = font.kernings[_char2];
  41. drawCharacter(this, font, x, y, fontChar || {});
  42. var kerning = fontKerning && fontKerning[text[i + 1]] ? fontKerning[text[i + 1]] : 0;
  43. x += kerning + (fontChar.xadvance || defaultCharWidth);
  44. }
  45. }
  46. function splitLines(font, text, maxWidth) {
  47. var words = text.split(' ');
  48. var lines = [];
  49. var currentLine = [];
  50. var longestLine = 0;
  51. words.forEach(function (word) {
  52. var line = [].concat((0, _toConsumableArray2["default"])(currentLine), [word]).join(' ');
  53. var length = (0, _measureText.measureText)(font, line);
  54. if (length <= maxWidth) {
  55. if (length > longestLine) {
  56. longestLine = length;
  57. }
  58. currentLine.push(word);
  59. } else {
  60. lines.push(currentLine);
  61. currentLine = [word];
  62. }
  63. });
  64. lines.push(currentLine);
  65. return {
  66. lines: lines,
  67. longestLine: longestLine
  68. };
  69. }
  70. function loadPages(Jimp, dir, pages) {
  71. var newPages = pages.map(function (page) {
  72. return Jimp.read(dir + '/' + page);
  73. });
  74. return Promise.all(newPages);
  75. }
  76. var dir = process.env.DIRNAME || "".concat(__dirname, "/../");
  77. var _default = function _default() {
  78. return {
  79. constants: {
  80. measureText: _measureText.measureText,
  81. measureTextHeight: _measureText.measureTextHeight,
  82. FONT_SANS_8_BLACK: _path["default"].join(dir, 'fonts/open-sans/open-sans-8-black/open-sans-8-black.fnt'),
  83. FONT_SANS_10_BLACK: _path["default"].join(dir, 'fonts/open-sans/open-sans-10-black/open-sans-10-black.fnt'),
  84. FONT_SANS_12_BLACK: _path["default"].join(dir, 'fonts/open-sans/open-sans-12-black/open-sans-12-black.fnt'),
  85. FONT_SANS_14_BLACK: _path["default"].join(dir, 'fonts/open-sans/open-sans-14-black/open-sans-14-black.fnt'),
  86. FONT_SANS_16_BLACK: _path["default"].join(dir, 'fonts/open-sans/open-sans-16-black/open-sans-16-black.fnt'),
  87. FONT_SANS_32_BLACK: _path["default"].join(dir, 'fonts/open-sans/open-sans-32-black/open-sans-32-black.fnt'),
  88. FONT_SANS_64_BLACK: _path["default"].join(dir, 'fonts/open-sans/open-sans-64-black/open-sans-64-black.fnt'),
  89. FONT_SANS_128_BLACK: _path["default"].join(dir, 'fonts/open-sans/open-sans-128-black/open-sans-128-black.fnt'),
  90. FONT_SANS_8_WHITE: _path["default"].join(dir, 'fonts/open-sans/open-sans-8-white/open-sans-8-white.fnt'),
  91. FONT_SANS_16_WHITE: _path["default"].join(dir, 'fonts/open-sans/open-sans-16-white/open-sans-16-white.fnt'),
  92. FONT_SANS_32_WHITE: _path["default"].join(dir, 'fonts/open-sans/open-sans-32-white/open-sans-32-white.fnt'),
  93. FONT_SANS_64_WHITE: _path["default"].join(dir, 'fonts/open-sans/open-sans-64-white/open-sans-64-white.fnt'),
  94. FONT_SANS_128_WHITE: _path["default"].join(dir, 'fonts/open-sans/open-sans-128-white/open-sans-128-white.fnt'),
  95. /**
  96. * Loads a bitmap font from a file
  97. * @param {string} file the file path of a .fnt file
  98. * @param {function(Error, Jimp)} cb (optional) a function to call when the font is loaded
  99. * @returns {Promise} a promise
  100. */
  101. loadFont: function loadFont(file, cb) {
  102. var _this = this;
  103. if (typeof file !== 'string') return _utils.throwError.call(this, 'file must be a string', cb);
  104. return new Promise(function (resolve, reject) {
  105. cb = cb || function (err, font) {
  106. if (err) reject(err);else resolve(font);
  107. };
  108. (0, _loadBmfont["default"])(file, function (err, font) {
  109. var chars = {};
  110. var kernings = {};
  111. if (err) {
  112. return _utils.throwError.call(_this, err, cb);
  113. }
  114. for (var i = 0; i < font.chars.length; i++) {
  115. chars[String.fromCharCode(font.chars[i].id)] = font.chars[i];
  116. }
  117. for (var _i = 0; _i < font.kernings.length; _i++) {
  118. var firstString = String.fromCharCode(font.kernings[_i].first);
  119. kernings[firstString] = kernings[firstString] || {};
  120. kernings[firstString][String.fromCharCode(font.kernings[_i].second)] = font.kernings[_i].amount;
  121. }
  122. loadPages(_this, _path["default"].dirname(file), font.pages).then(function (pages) {
  123. cb(null, {
  124. chars: chars,
  125. kernings: kernings,
  126. pages: pages,
  127. common: font.common,
  128. info: font.info
  129. });
  130. });
  131. });
  132. });
  133. }
  134. },
  135. "class": {
  136. /**
  137. * Draws a text on a image on a given boundary
  138. * @param {Jimp} font a bitmap font loaded from `Jimp.loadFont` command
  139. * @param {number} x the x position to start drawing the text
  140. * @param {number} y the y position to start drawing the text
  141. * @param {any} text the text to draw (string or object with `text`, `alignmentX`, and/or `alignmentY`)
  142. * @param {number} maxWidth (optional) the boundary width to draw in
  143. * @param {number} maxHeight (optional) the boundary height to draw in
  144. * @param {function(Error, Jimp)} cb (optional) a function to call when the text is written
  145. * @returns {Jimp} this for chaining of methods
  146. */
  147. print: function print(font, x, y, text, maxWidth, maxHeight, cb) {
  148. var _this2 = this;
  149. if (typeof maxWidth === 'function' && typeof cb === 'undefined') {
  150. cb = maxWidth;
  151. maxWidth = Infinity;
  152. }
  153. if (typeof maxWidth === 'undefined') {
  154. maxWidth = Infinity;
  155. }
  156. if (typeof maxHeight === 'function' && typeof cb === 'undefined') {
  157. cb = maxHeight;
  158. maxHeight = Infinity;
  159. }
  160. if (typeof maxHeight === 'undefined') {
  161. maxHeight = Infinity;
  162. }
  163. if ((0, _typeof2["default"])(font) !== 'object') {
  164. return _utils.throwError.call(this, 'font must be a Jimp loadFont', cb);
  165. }
  166. if (typeof x !== 'number' || typeof y !== 'number' || typeof maxWidth !== 'number') {
  167. return _utils.throwError.call(this, 'x, y and maxWidth must be numbers', cb);
  168. }
  169. if (typeof maxWidth !== 'number') {
  170. return _utils.throwError.call(this, 'maxWidth must be a number', cb);
  171. }
  172. if (typeof maxHeight !== 'number') {
  173. return _utils.throwError.call(this, 'maxHeight must be a number', cb);
  174. }
  175. var alignmentX;
  176. var alignmentY;
  177. if ((0, _typeof2["default"])(text) === 'object' && text.text !== null && text.text !== undefined) {
  178. alignmentX = text.alignmentX || this.constructor.HORIZONTAL_ALIGN_LEFT;
  179. alignmentY = text.alignmentY || this.constructor.VERTICAL_ALIGN_TOP;
  180. var _text = text;
  181. text = _text.text;
  182. } else {
  183. alignmentX = this.constructor.HORIZONTAL_ALIGN_LEFT;
  184. alignmentY = this.constructor.VERTICAL_ALIGN_TOP;
  185. text = text.toString();
  186. }
  187. if (maxHeight !== Infinity && alignmentY === this.constructor.VERTICAL_ALIGN_BOTTOM) {
  188. y += maxHeight - (0, _measureText.measureTextHeight)(font, text, maxWidth);
  189. } else if (maxHeight !== Infinity && alignmentY === this.constructor.VERTICAL_ALIGN_MIDDLE) {
  190. y += maxHeight / 2 - (0, _measureText.measureTextHeight)(font, text, maxWidth) / 2;
  191. }
  192. var defaultCharWidth = Object.entries(font.chars)[0][1].xadvance;
  193. var _splitLines = splitLines(font, text, maxWidth),
  194. lines = _splitLines.lines,
  195. longestLine = _splitLines.longestLine;
  196. lines.forEach(function (line) {
  197. var lineString = line.join(' ');
  198. var alignmentWidth = xOffsetBasedOnAlignment(_this2.constructor, font, lineString, maxWidth, alignmentX);
  199. printText.call(_this2, font, x + alignmentWidth, y, lineString, defaultCharWidth);
  200. y += font.common.lineHeight;
  201. });
  202. if ((0, _utils.isNodePattern)(cb)) {
  203. cb.call(this, null, this, {
  204. x: x + longestLine,
  205. y: y
  206. });
  207. }
  208. return this;
  209. }
  210. }
  211. };
  212. };
  213. exports["default"] = _default;
  214. //# sourceMappingURL=index.js.map