Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@jimp/plugin-print

Package Overview
Dependencies
Maintainers
2
Versions
242
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@jimp/plugin-print - npm Package Compare versions

Comparing version 0.20.2 to 0.21.0--canary.1149.3239903.0

332

dist/index.js
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
exports.default = void 0;
var _path = _interopRequireDefault(require("path"));
var _loadBmfont = _interopRequireDefault(require("load-bmfont"));
var _utils = require("@jimp/utils");
var _measureText = require("./measure-text");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function xOffsetBasedOnAlignment(constants, font, line, maxWidth, alignment) {

@@ -24,41 +16,33 @@ if (alignment === constants.HORIZONTAL_ALIGN_LEFT) {

}
if (alignment === constants.HORIZONTAL_ALIGN_CENTER) {
return (maxWidth - (0, _measureText.measureText)(font, line)) / 2;
}
return maxWidth - (0, _measureText.measureText)(font, line);
}
function drawCharacter(image, font, x, y, _char) {
if (_char.width > 0 && _char.height > 0) {
var characterPage = font.pages[_char.page];
image.blit(characterPage, x + _char.xoffset, y + _char.yoffset, _char.x, _char.y, _char.width, _char.height);
function drawCharacter(image, font, x, y, char) {
if (char.width > 0 && char.height > 0) {
const characterPage = font.pages[char.page];
image.blit(characterPage, x + char.xoffset, y + char.yoffset, char.x, char.y, char.width, char.height);
}
return image;
}
function printText(font, x, y, text, defaultCharWidth) {
for (var i = 0; i < text.length; i++) {
var _char2 = void 0;
for (let i = 0; i < text.length; i++) {
let char;
if (font.chars[text[i]]) {
_char2 = text[i];
char = text[i];
} else if (/\s/.test(text[i])) {
_char2 = "";
char = "";
} else {
_char2 = "?";
char = "?";
}
var fontChar = font.chars[_char2] || {};
var fontKerning = font.kernings[_char2];
const fontChar = font.chars[char] || {};
const fontKerning = font.kernings[char];
drawCharacter(this, font, x, y, fontChar || {});
var kerning = fontKerning && fontKerning[text[i + 1]] ? fontKerning[text[i + 1]] : 0;
const kerning = fontKerning && fontKerning[text[i + 1]] ? fontKerning[text[i + 1]] : 0;
x += kerning + (fontChar.xadvance || defaultCharWidth);
}
}
function loadPages(Jimp, dir, pages) {
var newPages = pages.map(function (page) {
const newPages = pages.map(page => {
return Jimp.read(dir + "/" + page);

@@ -68,167 +52,139 @@ });

}
var dir = process.env.DIRNAME || "".concat(__dirname, "/../");
var _default = function _default() {
return {
constants: {
measureText: _measureText.measureText,
measureTextHeight: _measureText.measureTextHeight,
FONT_SANS_8_BLACK: _path["default"].join(dir, "fonts/open-sans/open-sans-8-black/open-sans-8-black.fnt"),
FONT_SANS_10_BLACK: _path["default"].join(dir, "fonts/open-sans/open-sans-10-black/open-sans-10-black.fnt"),
FONT_SANS_12_BLACK: _path["default"].join(dir, "fonts/open-sans/open-sans-12-black/open-sans-12-black.fnt"),
FONT_SANS_14_BLACK: _path["default"].join(dir, "fonts/open-sans/open-sans-14-black/open-sans-14-black.fnt"),
FONT_SANS_16_BLACK: _path["default"].join(dir, "fonts/open-sans/open-sans-16-black/open-sans-16-black.fnt"),
FONT_SANS_32_BLACK: _path["default"].join(dir, "fonts/open-sans/open-sans-32-black/open-sans-32-black.fnt"),
FONT_SANS_64_BLACK: _path["default"].join(dir, "fonts/open-sans/open-sans-64-black/open-sans-64-black.fnt"),
FONT_SANS_128_BLACK: _path["default"].join(dir, "fonts/open-sans/open-sans-128-black/open-sans-128-black.fnt"),
FONT_SANS_8_WHITE: _path["default"].join(dir, "fonts/open-sans/open-sans-8-white/open-sans-8-white.fnt"),
FONT_SANS_16_WHITE: _path["default"].join(dir, "fonts/open-sans/open-sans-16-white/open-sans-16-white.fnt"),
FONT_SANS_32_WHITE: _path["default"].join(dir, "fonts/open-sans/open-sans-32-white/open-sans-32-white.fnt"),
FONT_SANS_64_WHITE: _path["default"].join(dir, "fonts/open-sans/open-sans-64-white/open-sans-64-white.fnt"),
FONT_SANS_128_WHITE: _path["default"].join(dir, "fonts/open-sans/open-sans-128-white/open-sans-128-white.fnt"),
/**
* Loads a bitmap font from a file
* @param {string} file the file path of a .fnt file
* @param {function(Error, Jimp)} cb (optional) a function to call when the font is loaded
* @returns {Promise} a promise
*/
loadFont: function loadFont(file, cb) {
var _this = this;
if (typeof file !== "string") return _utils.throwError.call(this, "file must be a string", cb);
return new Promise(function (resolve, reject) {
cb = cb || function (err, font) {
if (err) reject(err);else resolve(font);
};
(0, _loadBmfont["default"])(file, function (err, font) {
var chars = {};
var kernings = {};
if (err) {
return _utils.throwError.call(_this, err, cb);
}
for (var i = 0; i < font.chars.length; i++) {
chars[String.fromCharCode(font.chars[i].id)] = font.chars[i];
}
for (var _i = 0; _i < font.kernings.length; _i++) {
var firstString = String.fromCharCode(font.kernings[_i].first);
kernings[firstString] = kernings[firstString] || {};
kernings[firstString][String.fromCharCode(font.kernings[_i].second)] = font.kernings[_i].amount;
}
loadPages(_this, _path["default"].dirname(file), font.pages).then(function (pages) {
cb(null, {
chars: chars,
kernings: kernings,
pages: pages,
common: font.common,
info: font.info
});
const dir = process.env.DIRNAME || `${__dirname}/../`;
var _default = () => ({
constants: {
measureText: _measureText.measureText,
measureTextHeight: _measureText.measureTextHeight,
FONT_SANS_8_BLACK: _path.default.join(dir, "fonts/open-sans/open-sans-8-black/open-sans-8-black.fnt"),
FONT_SANS_10_BLACK: _path.default.join(dir, "fonts/open-sans/open-sans-10-black/open-sans-10-black.fnt"),
FONT_SANS_12_BLACK: _path.default.join(dir, "fonts/open-sans/open-sans-12-black/open-sans-12-black.fnt"),
FONT_SANS_14_BLACK: _path.default.join(dir, "fonts/open-sans/open-sans-14-black/open-sans-14-black.fnt"),
FONT_SANS_16_BLACK: _path.default.join(dir, "fonts/open-sans/open-sans-16-black/open-sans-16-black.fnt"),
FONT_SANS_32_BLACK: _path.default.join(dir, "fonts/open-sans/open-sans-32-black/open-sans-32-black.fnt"),
FONT_SANS_64_BLACK: _path.default.join(dir, "fonts/open-sans/open-sans-64-black/open-sans-64-black.fnt"),
FONT_SANS_128_BLACK: _path.default.join(dir, "fonts/open-sans/open-sans-128-black/open-sans-128-black.fnt"),
FONT_SANS_8_WHITE: _path.default.join(dir, "fonts/open-sans/open-sans-8-white/open-sans-8-white.fnt"),
FONT_SANS_16_WHITE: _path.default.join(dir, "fonts/open-sans/open-sans-16-white/open-sans-16-white.fnt"),
FONT_SANS_32_WHITE: _path.default.join(dir, "fonts/open-sans/open-sans-32-white/open-sans-32-white.fnt"),
FONT_SANS_64_WHITE: _path.default.join(dir, "fonts/open-sans/open-sans-64-white/open-sans-64-white.fnt"),
FONT_SANS_128_WHITE: _path.default.join(dir, "fonts/open-sans/open-sans-128-white/open-sans-128-white.fnt"),
/**
* Loads a bitmap font from a file
* @param {string} file the file path of a .fnt file
* @param {function(Error, Jimp)} cb (optional) a function to call when the font is loaded
* @returns {Promise} a promise
*/
loadFont(file, cb) {
if (typeof file !== "string") return _utils.throwError.call(this, "file must be a string", cb);
return new Promise((resolve, reject) => {
cb = cb || function (err, font) {
if (err) reject(err);else resolve(font);
};
(0, _loadBmfont.default)(file, (err, font) => {
const chars = {};
const kernings = {};
if (err) {
return _utils.throwError.call(this, err, cb);
}
for (let i = 0; i < font.chars.length; i++) {
chars[String.fromCharCode(font.chars[i].id)] = font.chars[i];
}
for (let i = 0; i < font.kernings.length; i++) {
const firstString = String.fromCharCode(font.kernings[i].first);
kernings[firstString] = kernings[firstString] || {};
kernings[firstString][String.fromCharCode(font.kernings[i].second)] = font.kernings[i].amount;
}
loadPages(this, _path.default.dirname(file), font.pages).then(pages => {
cb(null, {
chars,
kernings,
pages,
common: font.common,
info: font.info
});
});
});
});
}
},
class: {
/**
* Draws a text on a image on a given boundary
* @param {Jimp} font a bitmap font loaded from `Jimp.loadFont` command
* @param {number} x the x position to start drawing the text
* @param {number} y the y position to start drawing the text
* @param {any} text the text to draw (string or object with `text`, `alignmentX`, and/or `alignmentY`)
* @param {number} maxWidth (optional) the boundary width to draw in
* @param {number} maxHeight (optional) the boundary height to draw in
* @param {function(Error, Jimp)} cb (optional) a function to call when the text is written
* @returns {Jimp} this for chaining of methods
*/
print(font, x, y, text, maxWidth, maxHeight, cb) {
if (typeof maxWidth === "function" && typeof cb === "undefined") {
cb = maxWidth;
maxWidth = Infinity;
}
},
"class": {
/**
* Draws a text on a image on a given boundary
* @param {Jimp} font a bitmap font loaded from `Jimp.loadFont` command
* @param {number} x the x position to start drawing the text
* @param {number} y the y position to start drawing the text
* @param {any} text the text to draw (string or object with `text`, `alignmentX`, and/or `alignmentY`)
* @param {number} maxWidth (optional) the boundary width to draw in
* @param {number} maxHeight (optional) the boundary height to draw in
* @param {function(Error, Jimp)} cb (optional) a function to call when the text is written
* @returns {Jimp} this for chaining of methods
*/
print: function print(font, x, y, text, maxWidth, maxHeight, cb) {
var _this2 = this;
if (typeof maxWidth === "function" && typeof cb === "undefined") {
cb = maxWidth;
maxWidth = Infinity;
}
if (typeof maxWidth === "undefined") {
maxWidth = Infinity;
}
if (typeof maxHeight === "function" && typeof cb === "undefined") {
cb = maxHeight;
maxHeight = Infinity;
}
if (typeof maxHeight === "undefined") {
maxHeight = Infinity;
}
if ((0, _typeof2["default"])(font) !== "object") {
return _utils.throwError.call(this, "font must be a Jimp loadFont", cb);
}
if (typeof x !== "number" || typeof y !== "number" || typeof maxWidth !== "number") {
return _utils.throwError.call(this, "x, y and maxWidth must be numbers", cb);
}
if (typeof maxWidth !== "number") {
return _utils.throwError.call(this, "maxWidth must be a number", cb);
}
if (typeof maxHeight !== "number") {
return _utils.throwError.call(this, "maxHeight must be a number", cb);
}
var alignmentX;
var alignmentY;
if ((0, _typeof2["default"])(text) === "object" && text.text !== null && text.text !== undefined) {
alignmentX = text.alignmentX || this.constructor.HORIZONTAL_ALIGN_LEFT;
alignmentY = text.alignmentY || this.constructor.VERTICAL_ALIGN_TOP;
var _text = text;
text = _text.text;
} else {
alignmentX = this.constructor.HORIZONTAL_ALIGN_LEFT;
alignmentY = this.constructor.VERTICAL_ALIGN_TOP;
text = text.toString();
}
if (maxHeight !== Infinity && alignmentY === this.constructor.VERTICAL_ALIGN_BOTTOM) {
y += maxHeight - (0, _measureText.measureTextHeight)(font, text, maxWidth);
} else if (maxHeight !== Infinity && alignmentY === this.constructor.VERTICAL_ALIGN_MIDDLE) {
y += maxHeight / 2 - (0, _measureText.measureTextHeight)(font, text, maxWidth) / 2;
}
var defaultCharWidth = Object.entries(font.chars)[0][1].xadvance;
var _splitLines = (0, _measureText.splitLines)(font, text, maxWidth),
lines = _splitLines.lines,
longestLine = _splitLines.longestLine;
lines.forEach(function (line) {
var lineString = line.join(" ");
var alignmentWidth = xOffsetBasedOnAlignment(_this2.constructor, font, lineString, maxWidth, alignmentX);
printText.call(_this2, font, x + alignmentWidth, y, lineString, defaultCharWidth);
y += font.common.lineHeight;
if (typeof maxWidth === "undefined") {
maxWidth = Infinity;
}
if (typeof maxHeight === "function" && typeof cb === "undefined") {
cb = maxHeight;
maxHeight = Infinity;
}
if (typeof maxHeight === "undefined") {
maxHeight = Infinity;
}
if (typeof font !== "object") {
return _utils.throwError.call(this, "font must be a Jimp loadFont", cb);
}
if (typeof x !== "number" || typeof y !== "number" || typeof maxWidth !== "number") {
return _utils.throwError.call(this, "x, y and maxWidth must be numbers", cb);
}
if (typeof maxWidth !== "number") {
return _utils.throwError.call(this, "maxWidth must be a number", cb);
}
if (typeof maxHeight !== "number") {
return _utils.throwError.call(this, "maxHeight must be a number", cb);
}
let alignmentX;
let alignmentY;
if (typeof text === "object" && text.text !== null && text.text !== undefined) {
alignmentX = text.alignmentX || this.constructor.HORIZONTAL_ALIGN_LEFT;
alignmentY = text.alignmentY || this.constructor.VERTICAL_ALIGN_TOP;
({
text
} = text);
} else {
alignmentX = this.constructor.HORIZONTAL_ALIGN_LEFT;
alignmentY = this.constructor.VERTICAL_ALIGN_TOP;
text = text.toString();
}
if (maxHeight !== Infinity && alignmentY === this.constructor.VERTICAL_ALIGN_BOTTOM) {
y += maxHeight - (0, _measureText.measureTextHeight)(font, text, maxWidth);
} else if (maxHeight !== Infinity && alignmentY === this.constructor.VERTICAL_ALIGN_MIDDLE) {
y += maxHeight / 2 - (0, _measureText.measureTextHeight)(font, text, maxWidth) / 2;
}
const defaultCharWidth = Object.entries(font.chars)[0][1].xadvance;
const {
lines,
longestLine
} = (0, _measureText.splitLines)(font, text, maxWidth);
lines.forEach(line => {
const lineString = line.join(" ");
const alignmentWidth = xOffsetBasedOnAlignment(this.constructor, font, lineString, maxWidth, alignmentX);
printText.call(this, font, x + alignmentWidth, y, lineString, defaultCharWidth);
y += font.common.lineHeight;
});
if ((0, _utils.isNodePattern)(cb)) {
cb.call(this, null, this, {
x: x + longestLine,
y
});
if ((0, _utils.isNodePattern)(cb)) {
cb.call(this, null, this, {
x: x + longestLine,
y: y
});
}
return this;
}
return this;
}
};
};
exports["default"] = _default;
}
});
exports.default = _default;
module.exports = exports.default;
//# sourceMappingURL=index.js.map
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {

@@ -9,29 +7,22 @@ value: true

exports.measureText = measureText;
exports.measureTextHeight = measureTextHeight;
exports.splitLines = splitLines;
exports.measureTextHeight = measureTextHeight;
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
function measureText(font, text) {
var x = 0;
for (var i = 0; i < text.length; i++) {
let x = 0;
for (let i = 0; i < text.length; i++) {
if (font.chars[text[i]]) {
var kerning = font.kernings[text[i]] && font.kernings[text[i]][text[i + 1]] ? font.kernings[text[i]][text[i + 1]] : 0;
const kerning = font.kernings[text[i]] && font.kernings[text[i]][text[i + 1]] ? font.kernings[text[i]][text[i + 1]] : 0;
x += (font.chars[text[i]].xadvance || 0) + kerning;
}
}
return x;
}
function splitLines(font, text, maxWidth) {
var words = text.split(" ");
var lines = [];
var currentLine = [];
var longestLine = 0;
words.forEach(function (word) {
var line = [].concat((0, _toConsumableArray2["default"])(currentLine), [word]).join(" ");
var length = measureText(font, line);
const words = text.split(" ");
const lines = [];
let currentLine = [];
let longestLine = 0;
words.forEach(word => {
const line = [...currentLine, word].join(" ");
const length = measureText(font, line);
if (length <= maxWidth) {

@@ -41,3 +32,2 @@ if (length > longestLine) {

}
currentLine.push(word);

@@ -51,13 +41,12 @@ } else {

return {
lines: lines,
longestLine: longestLine
lines,
longestLine
};
}
function measureTextHeight(font, text, maxWidth) {
var _splitLines = splitLines(font, text, maxWidth),
lines = _splitLines.lines;
const {
lines
} = splitLines(font, text, maxWidth);
return lines.length * font.common.lineHeight;
}
//# sourceMappingURL=measure-text.js.map

@@ -1,20 +0,5 @@

"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
var _path = _interopRequireDefault(require("path"));
var _loadBmfont = _interopRequireDefault(require("load-bmfont"));
var _utils = require("@jimp/utils");
var _measureText = require("./measure-text");
import Path from "path";
import bMFont from "load-bmfont";
import { isNodePattern, throwError } from "@jimp/utils";
import { measureText, measureTextHeight, splitLines } from "./measure-text";
function xOffsetBasedOnAlignment(constants, font, line, maxWidth, alignment) {

@@ -24,41 +9,33 @@ if (alignment === constants.HORIZONTAL_ALIGN_LEFT) {

}
if (alignment === constants.HORIZONTAL_ALIGN_CENTER) {
return (maxWidth - (0, _measureText.measureText)(font, line)) / 2;
return (maxWidth - measureText(font, line)) / 2;
}
return maxWidth - (0, _measureText.measureText)(font, line);
return maxWidth - measureText(font, line);
}
function drawCharacter(image, font, x, y, _char) {
if (_char.width > 0 && _char.height > 0) {
var characterPage = font.pages[_char.page];
image.blit(characterPage, x + _char.xoffset, y + _char.yoffset, _char.x, _char.y, _char.width, _char.height);
function drawCharacter(image, font, x, y, char) {
if (char.width > 0 && char.height > 0) {
const characterPage = font.pages[char.page];
image.blit(characterPage, x + char.xoffset, y + char.yoffset, char.x, char.y, char.width, char.height);
}
return image;
}
function printText(font, x, y, text, defaultCharWidth) {
for (var i = 0; i < text.length; i++) {
var _char2 = void 0;
for (let i = 0; i < text.length; i++) {
let char;
if (font.chars[text[i]]) {
_char2 = text[i];
char = text[i];
} else if (/\s/.test(text[i])) {
_char2 = "";
char = "";
} else {
_char2 = "?";
char = "?";
}
var fontChar = font.chars[_char2] || {};
var fontKerning = font.kernings[_char2];
const fontChar = font.chars[char] || {};
const fontKerning = font.kernings[char];
drawCharacter(this, font, x, y, fontChar || {});
var kerning = fontKerning && fontKerning[text[i + 1]] ? fontKerning[text[i + 1]] : 0;
const kerning = fontKerning && fontKerning[text[i + 1]] ? fontKerning[text[i + 1]] : 0;
x += kerning + (fontChar.xadvance || defaultCharWidth);
}
}
function loadPages(Jimp, dir, pages) {
var newPages = pages.map(function (page) {
const newPages = pages.map(page => {
return Jimp.read(dir + "/" + page);

@@ -68,166 +45,137 @@ });

}
var dir = process.env.DIRNAME || "".concat(__dirname, "/../");
var _default = function _default() {
return {
constants: {
measureText: _measureText.measureText,
measureTextHeight: _measureText.measureTextHeight,
FONT_SANS_8_BLACK: _path["default"].join(dir, "fonts/open-sans/open-sans-8-black/open-sans-8-black.fnt"),
FONT_SANS_10_BLACK: _path["default"].join(dir, "fonts/open-sans/open-sans-10-black/open-sans-10-black.fnt"),
FONT_SANS_12_BLACK: _path["default"].join(dir, "fonts/open-sans/open-sans-12-black/open-sans-12-black.fnt"),
FONT_SANS_14_BLACK: _path["default"].join(dir, "fonts/open-sans/open-sans-14-black/open-sans-14-black.fnt"),
FONT_SANS_16_BLACK: _path["default"].join(dir, "fonts/open-sans/open-sans-16-black/open-sans-16-black.fnt"),
FONT_SANS_32_BLACK: _path["default"].join(dir, "fonts/open-sans/open-sans-32-black/open-sans-32-black.fnt"),
FONT_SANS_64_BLACK: _path["default"].join(dir, "fonts/open-sans/open-sans-64-black/open-sans-64-black.fnt"),
FONT_SANS_128_BLACK: _path["default"].join(dir, "fonts/open-sans/open-sans-128-black/open-sans-128-black.fnt"),
FONT_SANS_8_WHITE: _path["default"].join(dir, "fonts/open-sans/open-sans-8-white/open-sans-8-white.fnt"),
FONT_SANS_16_WHITE: _path["default"].join(dir, "fonts/open-sans/open-sans-16-white/open-sans-16-white.fnt"),
FONT_SANS_32_WHITE: _path["default"].join(dir, "fonts/open-sans/open-sans-32-white/open-sans-32-white.fnt"),
FONT_SANS_64_WHITE: _path["default"].join(dir, "fonts/open-sans/open-sans-64-white/open-sans-64-white.fnt"),
FONT_SANS_128_WHITE: _path["default"].join(dir, "fonts/open-sans/open-sans-128-white/open-sans-128-white.fnt"),
/**
* Loads a bitmap font from a file
* @param {string} file the file path of a .fnt file
* @param {function(Error, Jimp)} cb (optional) a function to call when the font is loaded
* @returns {Promise} a promise
*/
loadFont: function loadFont(file, cb) {
var _this = this;
if (typeof file !== "string") return _utils.throwError.call(this, "file must be a string", cb);
return new Promise(function (resolve, reject) {
cb = cb || function (err, font) {
if (err) reject(err);else resolve(font);
};
(0, _loadBmfont["default"])(file, function (err, font) {
var chars = {};
var kernings = {};
if (err) {
return _utils.throwError.call(_this, err, cb);
}
for (var i = 0; i < font.chars.length; i++) {
chars[String.fromCharCode(font.chars[i].id)] = font.chars[i];
}
for (var _i = 0; _i < font.kernings.length; _i++) {
var firstString = String.fromCharCode(font.kernings[_i].first);
kernings[firstString] = kernings[firstString] || {};
kernings[firstString][String.fromCharCode(font.kernings[_i].second)] = font.kernings[_i].amount;
}
loadPages(_this, _path["default"].dirname(file), font.pages).then(function (pages) {
cb(null, {
chars: chars,
kernings: kernings,
pages: pages,
common: font.common,
info: font.info
});
const dir = process.env.DIRNAME || `${__dirname}/../`;
export default (() => ({
constants: {
measureText,
measureTextHeight,
FONT_SANS_8_BLACK: Path.join(dir, "fonts/open-sans/open-sans-8-black/open-sans-8-black.fnt"),
FONT_SANS_10_BLACK: Path.join(dir, "fonts/open-sans/open-sans-10-black/open-sans-10-black.fnt"),
FONT_SANS_12_BLACK: Path.join(dir, "fonts/open-sans/open-sans-12-black/open-sans-12-black.fnt"),
FONT_SANS_14_BLACK: Path.join(dir, "fonts/open-sans/open-sans-14-black/open-sans-14-black.fnt"),
FONT_SANS_16_BLACK: Path.join(dir, "fonts/open-sans/open-sans-16-black/open-sans-16-black.fnt"),
FONT_SANS_32_BLACK: Path.join(dir, "fonts/open-sans/open-sans-32-black/open-sans-32-black.fnt"),
FONT_SANS_64_BLACK: Path.join(dir, "fonts/open-sans/open-sans-64-black/open-sans-64-black.fnt"),
FONT_SANS_128_BLACK: Path.join(dir, "fonts/open-sans/open-sans-128-black/open-sans-128-black.fnt"),
FONT_SANS_8_WHITE: Path.join(dir, "fonts/open-sans/open-sans-8-white/open-sans-8-white.fnt"),
FONT_SANS_16_WHITE: Path.join(dir, "fonts/open-sans/open-sans-16-white/open-sans-16-white.fnt"),
FONT_SANS_32_WHITE: Path.join(dir, "fonts/open-sans/open-sans-32-white/open-sans-32-white.fnt"),
FONT_SANS_64_WHITE: Path.join(dir, "fonts/open-sans/open-sans-64-white/open-sans-64-white.fnt"),
FONT_SANS_128_WHITE: Path.join(dir, "fonts/open-sans/open-sans-128-white/open-sans-128-white.fnt"),
/**
* Loads a bitmap font from a file
* @param {string} file the file path of a .fnt file
* @param {function(Error, Jimp)} cb (optional) a function to call when the font is loaded
* @returns {Promise} a promise
*/
loadFont(file, cb) {
if (typeof file !== "string") return throwError.call(this, "file must be a string", cb);
return new Promise((resolve, reject) => {
cb = cb || function (err, font) {
if (err) reject(err);else resolve(font);
};
bMFont(file, (err, font) => {
const chars = {};
const kernings = {};
if (err) {
return throwError.call(this, err, cb);
}
for (let i = 0; i < font.chars.length; i++) {
chars[String.fromCharCode(font.chars[i].id)] = font.chars[i];
}
for (let i = 0; i < font.kernings.length; i++) {
const firstString = String.fromCharCode(font.kernings[i].first);
kernings[firstString] = kernings[firstString] || {};
kernings[firstString][String.fromCharCode(font.kernings[i].second)] = font.kernings[i].amount;
}
loadPages(this, Path.dirname(file), font.pages).then(pages => {
cb(null, {
chars,
kernings,
pages,
common: font.common,
info: font.info
});
});
});
});
}
},
class: {
/**
* Draws a text on a image on a given boundary
* @param {Jimp} font a bitmap font loaded from `Jimp.loadFont` command
* @param {number} x the x position to start drawing the text
* @param {number} y the y position to start drawing the text
* @param {any} text the text to draw (string or object with `text`, `alignmentX`, and/or `alignmentY`)
* @param {number} maxWidth (optional) the boundary width to draw in
* @param {number} maxHeight (optional) the boundary height to draw in
* @param {function(Error, Jimp)} cb (optional) a function to call when the text is written
* @returns {Jimp} this for chaining of methods
*/
print(font, x, y, text, maxWidth, maxHeight, cb) {
if (typeof maxWidth === "function" && typeof cb === "undefined") {
cb = maxWidth;
maxWidth = Infinity;
}
},
"class": {
/**
* Draws a text on a image on a given boundary
* @param {Jimp} font a bitmap font loaded from `Jimp.loadFont` command
* @param {number} x the x position to start drawing the text
* @param {number} y the y position to start drawing the text
* @param {any} text the text to draw (string or object with `text`, `alignmentX`, and/or `alignmentY`)
* @param {number} maxWidth (optional) the boundary width to draw in
* @param {number} maxHeight (optional) the boundary height to draw in
* @param {function(Error, Jimp)} cb (optional) a function to call when the text is written
* @returns {Jimp} this for chaining of methods
*/
print: function print(font, x, y, text, maxWidth, maxHeight, cb) {
var _this2 = this;
if (typeof maxWidth === "function" && typeof cb === "undefined") {
cb = maxWidth;
maxWidth = Infinity;
}
if (typeof maxWidth === "undefined") {
maxWidth = Infinity;
}
if (typeof maxHeight === "function" && typeof cb === "undefined") {
cb = maxHeight;
maxHeight = Infinity;
}
if (typeof maxHeight === "undefined") {
maxHeight = Infinity;
}
if ((0, _typeof2["default"])(font) !== "object") {
return _utils.throwError.call(this, "font must be a Jimp loadFont", cb);
}
if (typeof x !== "number" || typeof y !== "number" || typeof maxWidth !== "number") {
return _utils.throwError.call(this, "x, y and maxWidth must be numbers", cb);
}
if (typeof maxWidth !== "number") {
return _utils.throwError.call(this, "maxWidth must be a number", cb);
}
if (typeof maxHeight !== "number") {
return _utils.throwError.call(this, "maxHeight must be a number", cb);
}
var alignmentX;
var alignmentY;
if ((0, _typeof2["default"])(text) === "object" && text.text !== null && text.text !== undefined) {
alignmentX = text.alignmentX || this.constructor.HORIZONTAL_ALIGN_LEFT;
alignmentY = text.alignmentY || this.constructor.VERTICAL_ALIGN_TOP;
var _text = text;
text = _text.text;
} else {
alignmentX = this.constructor.HORIZONTAL_ALIGN_LEFT;
alignmentY = this.constructor.VERTICAL_ALIGN_TOP;
text = text.toString();
}
if (maxHeight !== Infinity && alignmentY === this.constructor.VERTICAL_ALIGN_BOTTOM) {
y += maxHeight - (0, _measureText.measureTextHeight)(font, text, maxWidth);
} else if (maxHeight !== Infinity && alignmentY === this.constructor.VERTICAL_ALIGN_MIDDLE) {
y += maxHeight / 2 - (0, _measureText.measureTextHeight)(font, text, maxWidth) / 2;
}
var defaultCharWidth = Object.entries(font.chars)[0][1].xadvance;
var _splitLines = (0, _measureText.splitLines)(font, text, maxWidth),
lines = _splitLines.lines,
longestLine = _splitLines.longestLine;
lines.forEach(function (line) {
var lineString = line.join(" ");
var alignmentWidth = xOffsetBasedOnAlignment(_this2.constructor, font, lineString, maxWidth, alignmentX);
printText.call(_this2, font, x + alignmentWidth, y, lineString, defaultCharWidth);
y += font.common.lineHeight;
if (typeof maxWidth === "undefined") {
maxWidth = Infinity;
}
if (typeof maxHeight === "function" && typeof cb === "undefined") {
cb = maxHeight;
maxHeight = Infinity;
}
if (typeof maxHeight === "undefined") {
maxHeight = Infinity;
}
if (typeof font !== "object") {
return throwError.call(this, "font must be a Jimp loadFont", cb);
}
if (typeof x !== "number" || typeof y !== "number" || typeof maxWidth !== "number") {
return throwError.call(this, "x, y and maxWidth must be numbers", cb);
}
if (typeof maxWidth !== "number") {
return throwError.call(this, "maxWidth must be a number", cb);
}
if (typeof maxHeight !== "number") {
return throwError.call(this, "maxHeight must be a number", cb);
}
let alignmentX;
let alignmentY;
if (typeof text === "object" && text.text !== null && text.text !== undefined) {
alignmentX = text.alignmentX || this.constructor.HORIZONTAL_ALIGN_LEFT;
alignmentY = text.alignmentY || this.constructor.VERTICAL_ALIGN_TOP;
({
text
} = text);
} else {
alignmentX = this.constructor.HORIZONTAL_ALIGN_LEFT;
alignmentY = this.constructor.VERTICAL_ALIGN_TOP;
text = text.toString();
}
if (maxHeight !== Infinity && alignmentY === this.constructor.VERTICAL_ALIGN_BOTTOM) {
y += maxHeight - measureTextHeight(font, text, maxWidth);
} else if (maxHeight !== Infinity && alignmentY === this.constructor.VERTICAL_ALIGN_MIDDLE) {
y += maxHeight / 2 - measureTextHeight(font, text, maxWidth) / 2;
}
const defaultCharWidth = Object.entries(font.chars)[0][1].xadvance;
const {
lines,
longestLine
} = splitLines(font, text, maxWidth);
lines.forEach(line => {
const lineString = line.join(" ");
const alignmentWidth = xOffsetBasedOnAlignment(this.constructor, font, lineString, maxWidth, alignmentX);
printText.call(this, font, x + alignmentWidth, y, lineString, defaultCharWidth);
y += font.common.lineHeight;
});
if (isNodePattern(cb)) {
cb.call(this, null, this, {
x: x + longestLine,
y
});
if ((0, _utils.isNodePattern)(cb)) {
cb.call(this, null, this, {
x: x + longestLine,
y: y
});
}
return this;
}
return this;
}
};
};
exports["default"] = _default;
}
}));
//# sourceMappingURL=index.js.map

@@ -1,36 +0,19 @@

"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.measureText = measureText;
exports.splitLines = splitLines;
exports.measureTextHeight = measureTextHeight;
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
function measureText(font, text) {
var x = 0;
for (var i = 0; i < text.length; i++) {
export function measureText(font, text) {
let x = 0;
for (let i = 0; i < text.length; i++) {
if (font.chars[text[i]]) {
var kerning = font.kernings[text[i]] && font.kernings[text[i]][text[i + 1]] ? font.kernings[text[i]][text[i + 1]] : 0;
const kerning = font.kernings[text[i]] && font.kernings[text[i]][text[i + 1]] ? font.kernings[text[i]][text[i + 1]] : 0;
x += (font.chars[text[i]].xadvance || 0) + kerning;
}
}
return x;
}
function splitLines(font, text, maxWidth) {
var words = text.split(" ");
var lines = [];
var currentLine = [];
var longestLine = 0;
words.forEach(function (word) {
var line = [].concat((0, _toConsumableArray2["default"])(currentLine), [word]).join(" ");
var length = measureText(font, line);
export function splitLines(font, text, maxWidth) {
const words = text.split(" ");
const lines = [];
let currentLine = [];
let longestLine = 0;
words.forEach(word => {
const line = [...currentLine, word].join(" ");
const length = measureText(font, line);
if (length <= maxWidth) {

@@ -40,3 +23,2 @@ if (length > longestLine) {

}
currentLine.push(word);

@@ -50,13 +32,12 @@ } else {

return {
lines: lines,
longestLine: longestLine
lines,
longestLine
};
}
function measureTextHeight(font, text, maxWidth) {
var _splitLines = splitLines(font, text, maxWidth),
lines = _splitLines.lines;
export function measureTextHeight(font, text, maxWidth) {
const {
lines
} = splitLines(font, text, maxWidth);
return lines.length * font.common.lineHeight;
}
//# sourceMappingURL=measure-text.js.map
{
"name": "@jimp/plugin-print",
"version": "0.20.2",
"version": "0.21.0--canary.1149.3239903.0",
"description": "print an image.",

@@ -24,4 +24,3 @@ "main": "dist/index.js",

"dependencies": {
"@babel/runtime": "^7.7.2",
"@jimp/utils": "^0.20.2",
"@jimp/utils": "0.21.0--canary.1149.3239903.0",
"load-bmfont": "^1.4.1"

@@ -34,5 +33,5 @@ },

"devDependencies": {
"@jimp/custom": "^0.20.2",
"@jimp/plugin-blit": "^0.20.2",
"@jimp/test-utils": "^0.20.2"
"@jimp/custom": "0.21.0--canary.1149.3239903.0",
"@jimp/plugin-blit": "0.21.0--canary.1149.3239903.0",
"@jimp/test-utils": "0.21.0--canary.1149.3239903.0"
},

@@ -42,3 +41,3 @@ "publishConfig": {

},
"gitHead": "cebbdb72f889102a8e3c42b25ad3243b16e3a485"
"gitHead": "323990352ce279d67297aed097b37bd8ec66ef51"
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc