in modules/layers/src/text-layer/utils.js [120:179]
function breakWord(text, maxWidth, iconMapping) {
let rows = [];
let rowStartCharIndex = 0;
let groupStartCharIndex = 0;
let rowOffsetLeft = 0;
let group = null;
for (let i = 0; i < text.length; i++) {
// 1. break text into word groups
// - if current char is white space
// - else if next char is white space
// - else if reach last char
if (text[i] === ' ') {
group = text[i];
groupStartCharIndex = i + 1;
} else if ((i + 1 < text.length && text[i + 1] === ' ') || i + 1 === text.length) {
group = text.substring(groupStartCharIndex, i + 1);
groupStartCharIndex = i + 1;
} else {
group = null;
}
if (group) {
// 2. break text into next row at maxWidth
let groupWidth = getTextWidth(group, iconMapping);
if (rowOffsetLeft + groupWidth > maxWidth) {
const lastGroupStartIndex = groupStartCharIndex - group.length;
if (rowStartCharIndex < lastGroupStartIndex) {
rows.push(text.substring(rowStartCharIndex, lastGroupStartIndex));
rowStartCharIndex = lastGroupStartIndex;
rowOffsetLeft = 0;
}
// if a single text group is bigger than maxWidth, then `break-all`
if (groupWidth > maxWidth) {
const subGroups = breakAll(group, maxWidth, iconMapping);
if (subGroups.rows.length > 1) {
// add all the sub rows to results except last row
rows = rows.concat(subGroups.rows.slice(0, subGroups.rows.length - 1));
}
// move reference to last row
rowStartCharIndex = rowStartCharIndex + subGroups.lastRowStartCharIndex;
groupWidth = subGroups.lastRowOffsetLeft;
}
}
rowOffsetLeft += groupWidth;
}
}
// last row
if (rowStartCharIndex < text.length) {
rows.push(text.substring(rowStartCharIndex));
}
return {
rows,
lastRowStartCharIndex: rowStartCharIndex,
lastRowOffsetLeft: rowOffsetLeft
};
}