Path: blob/master/node_modules/@jimp/plugin-print/es/index.js
1126 views
"use strict";12var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");34Object.defineProperty(exports, "__esModule", {5value: true6});7exports["default"] = void 0;89var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));1011var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));1213var _path = _interopRequireDefault(require("path"));1415var _loadBmfont = _interopRequireDefault(require("load-bmfont"));1617var _utils = require("@jimp/utils");1819var _measureText = require("./measure-text");2021function xOffsetBasedOnAlignment(constants, font, line, maxWidth, alignment) {22if (alignment === constants.HORIZONTAL_ALIGN_LEFT) {23return 0;24}2526if (alignment === constants.HORIZONTAL_ALIGN_CENTER) {27return (maxWidth - (0, _measureText.measureText)(font, line)) / 2;28}2930return maxWidth - (0, _measureText.measureText)(font, line);31}3233function drawCharacter(image, font, x, y, _char) {34if (_char.width > 0 && _char.height > 0) {35var characterPage = font.pages[_char.page];36image.blit(characterPage, x + _char.xoffset, y + _char.yoffset, _char.x, _char.y, _char.width, _char.height);37}3839return image;40}4142function printText(font, x, y, text, defaultCharWidth) {43for (var i = 0; i < text.length; i++) {44var _char2 = void 0;4546if (font.chars[text[i]]) {47_char2 = text[i];48} else if (/\s/.test(text[i])) {49_char2 = '';50} else {51_char2 = '?';52}5354var fontChar = font.chars[_char2] || {};55var fontKerning = font.kernings[_char2];56drawCharacter(this, font, x, y, fontChar || {});57var kerning = fontKerning && fontKerning[text[i + 1]] ? fontKerning[text[i + 1]] : 0;58x += kerning + (fontChar.xadvance || defaultCharWidth);59}60}6162function splitLines(font, text, maxWidth) {63var words = text.split(' ');64var lines = [];65var currentLine = [];66var longestLine = 0;67words.forEach(function (word) {68var line = [].concat((0, _toConsumableArray2["default"])(currentLine), [word]).join(' ');69var length = (0, _measureText.measureText)(font, line);7071if (length <= maxWidth) {72if (length > longestLine) {73longestLine = length;74}7576currentLine.push(word);77} else {78lines.push(currentLine);79currentLine = [word];80}81});82lines.push(currentLine);83return {84lines: lines,85longestLine: longestLine86};87}8889function loadPages(Jimp, dir, pages) {90var newPages = pages.map(function (page) {91return Jimp.read(dir + '/' + page);92});93return Promise.all(newPages);94}9596var dir = process.env.DIRNAME || "".concat(__dirname, "/../");9798var _default = function _default() {99return {100constants: {101measureText: _measureText.measureText,102measureTextHeight: _measureText.measureTextHeight,103FONT_SANS_8_BLACK: _path["default"].join(dir, 'fonts/open-sans/open-sans-8-black/open-sans-8-black.fnt'),104FONT_SANS_10_BLACK: _path["default"].join(dir, 'fonts/open-sans/open-sans-10-black/open-sans-10-black.fnt'),105FONT_SANS_12_BLACK: _path["default"].join(dir, 'fonts/open-sans/open-sans-12-black/open-sans-12-black.fnt'),106FONT_SANS_14_BLACK: _path["default"].join(dir, 'fonts/open-sans/open-sans-14-black/open-sans-14-black.fnt'),107FONT_SANS_16_BLACK: _path["default"].join(dir, 'fonts/open-sans/open-sans-16-black/open-sans-16-black.fnt'),108FONT_SANS_32_BLACK: _path["default"].join(dir, 'fonts/open-sans/open-sans-32-black/open-sans-32-black.fnt'),109FONT_SANS_64_BLACK: _path["default"].join(dir, 'fonts/open-sans/open-sans-64-black/open-sans-64-black.fnt'),110FONT_SANS_128_BLACK: _path["default"].join(dir, 'fonts/open-sans/open-sans-128-black/open-sans-128-black.fnt'),111FONT_SANS_8_WHITE: _path["default"].join(dir, 'fonts/open-sans/open-sans-8-white/open-sans-8-white.fnt'),112FONT_SANS_16_WHITE: _path["default"].join(dir, 'fonts/open-sans/open-sans-16-white/open-sans-16-white.fnt'),113FONT_SANS_32_WHITE: _path["default"].join(dir, 'fonts/open-sans/open-sans-32-white/open-sans-32-white.fnt'),114FONT_SANS_64_WHITE: _path["default"].join(dir, 'fonts/open-sans/open-sans-64-white/open-sans-64-white.fnt'),115FONT_SANS_128_WHITE: _path["default"].join(dir, 'fonts/open-sans/open-sans-128-white/open-sans-128-white.fnt'),116117/**118* Loads a bitmap font from a file119* @param {string} file the file path of a .fnt file120* @param {function(Error, Jimp)} cb (optional) a function to call when the font is loaded121* @returns {Promise} a promise122*/123loadFont: function loadFont(file, cb) {124var _this = this;125126if (typeof file !== 'string') return _utils.throwError.call(this, 'file must be a string', cb);127return new Promise(function (resolve, reject) {128cb = cb || function (err, font) {129if (err) reject(err);else resolve(font);130};131132(0, _loadBmfont["default"])(file, function (err, font) {133var chars = {};134var kernings = {};135136if (err) {137return _utils.throwError.call(_this, err, cb);138}139140for (var i = 0; i < font.chars.length; i++) {141chars[String.fromCharCode(font.chars[i].id)] = font.chars[i];142}143144for (var _i = 0; _i < font.kernings.length; _i++) {145var firstString = String.fromCharCode(font.kernings[_i].first);146kernings[firstString] = kernings[firstString] || {};147kernings[firstString][String.fromCharCode(font.kernings[_i].second)] = font.kernings[_i].amount;148}149150loadPages(_this, _path["default"].dirname(file), font.pages).then(function (pages) {151cb(null, {152chars: chars,153kernings: kernings,154pages: pages,155common: font.common,156info: font.info157});158});159});160});161}162},163"class": {164/**165* Draws a text on a image on a given boundary166* @param {Jimp} font a bitmap font loaded from `Jimp.loadFont` command167* @param {number} x the x position to start drawing the text168* @param {number} y the y position to start drawing the text169* @param {any} text the text to draw (string or object with `text`, `alignmentX`, and/or `alignmentY`)170* @param {number} maxWidth (optional) the boundary width to draw in171* @param {number} maxHeight (optional) the boundary height to draw in172* @param {function(Error, Jimp)} cb (optional) a function to call when the text is written173* @returns {Jimp} this for chaining of methods174*/175print: function print(font, x, y, text, maxWidth, maxHeight, cb) {176var _this2 = this;177178if (typeof maxWidth === 'function' && typeof cb === 'undefined') {179cb = maxWidth;180maxWidth = Infinity;181}182183if (typeof maxWidth === 'undefined') {184maxWidth = Infinity;185}186187if (typeof maxHeight === 'function' && typeof cb === 'undefined') {188cb = maxHeight;189maxHeight = Infinity;190}191192if (typeof maxHeight === 'undefined') {193maxHeight = Infinity;194}195196if ((0, _typeof2["default"])(font) !== 'object') {197return _utils.throwError.call(this, 'font must be a Jimp loadFont', cb);198}199200if (typeof x !== 'number' || typeof y !== 'number' || typeof maxWidth !== 'number') {201return _utils.throwError.call(this, 'x, y and maxWidth must be numbers', cb);202}203204if (typeof maxWidth !== 'number') {205return _utils.throwError.call(this, 'maxWidth must be a number', cb);206}207208if (typeof maxHeight !== 'number') {209return _utils.throwError.call(this, 'maxHeight must be a number', cb);210}211212var alignmentX;213var alignmentY;214215if ((0, _typeof2["default"])(text) === 'object' && text.text !== null && text.text !== undefined) {216alignmentX = text.alignmentX || this.constructor.HORIZONTAL_ALIGN_LEFT;217alignmentY = text.alignmentY || this.constructor.VERTICAL_ALIGN_TOP;218var _text = text;219text = _text.text;220} else {221alignmentX = this.constructor.HORIZONTAL_ALIGN_LEFT;222alignmentY = this.constructor.VERTICAL_ALIGN_TOP;223text = text.toString();224}225226if (maxHeight !== Infinity && alignmentY === this.constructor.VERTICAL_ALIGN_BOTTOM) {227y += maxHeight - (0, _measureText.measureTextHeight)(font, text, maxWidth);228} else if (maxHeight !== Infinity && alignmentY === this.constructor.VERTICAL_ALIGN_MIDDLE) {229y += maxHeight / 2 - (0, _measureText.measureTextHeight)(font, text, maxWidth) / 2;230}231232var defaultCharWidth = Object.entries(font.chars)[0][1].xadvance;233234var _splitLines = splitLines(font, text, maxWidth),235lines = _splitLines.lines,236longestLine = _splitLines.longestLine;237238lines.forEach(function (line) {239var lineString = line.join(' ');240var alignmentWidth = xOffsetBasedOnAlignment(_this2.constructor, font, lineString, maxWidth, alignmentX);241printText.call(_this2, font, x + alignmentWidth, y, lineString, defaultCharWidth);242y += font.common.lineHeight;243});244245if ((0, _utils.isNodePattern)(cb)) {246cb.call(this, null, this, {247x: x + longestLine,248y: y249});250}251252return this;253}254}255};256};257258exports["default"] = _default;259//# sourceMappingURL=index.js.map260261