index.js 10 KB

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