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