Path: blob/master/node_modules/@jimp/plugin-print/src/measure-text.js
1126 views
export function measureText(font, text) {1let x = 0;23for (let i = 0; i < text.length; i++) {4if (font.chars[text[i]]) {5const kerning =6font.kernings[text[i]] && font.kernings[text[i]][text[i + 1]]7? font.kernings[text[i]][text[i + 1]]8: 0;910x += (font.chars[text[i]].xadvance || 0) + kerning;11}12}1314return x;15}1617export function measureTextHeight(font, text, maxWidth) {18const words = text.split(' ');19let line = '';20let textTotalHeight = font.common.lineHeight;2122for (let n = 0; n < words.length; n++) {23const testLine = line + words[n] + ' ';24const testWidth = measureText(font, testLine);2526if (testWidth > maxWidth && n > 0) {27textTotalHeight += font.common.lineHeight;28line = words[n] + ' ';29} else {30line = testLine;31}32}3334return textTotalHeight;35}363738