Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Text utils (draw and measure) performance improvements
Better handle the case of extemely long strings (which previously draw and measures by removing one character at a time) to instead use a binary search pattern of finding the biggest string that fits.
  • Loading branch information
Brian Vaughn committed Aug 17, 2021
commit 95a1f38292be26a2dff9aa7303f6434fe2f41614
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,34 @@ export function trimText(
text: string,
width: number,
): string | null {
for (let i = text.length - 1; i >= 0; i--) {
const trimmedText = i === text.length - 1 ? text : text.substr(0, i) + '…';
const maxIndex = text.length - 1;

let startIndex = 0;
let stopIndex = maxIndex;

let longestValidIndex = 0;
let longestValidText = null;

// Trimming long text could be really slow if we decrease only 1 character at a time.
// Trimming with more of a binary search approach is faster in the worst cases.
while (startIndex <= stopIndex) {
const currentIndex = Math.floor((startIndex + stopIndex) / 2);
const trimmedText =
currentIndex === maxIndex ? text : text.substr(0, currentIndex) + '…';

if (getTextWidth(context, trimmedText) <= width) {
return trimmedText;
if (longestValidIndex < currentIndex) {
longestValidIndex = currentIndex;
longestValidText = trimmedText;
}

startIndex = currentIndex + 1;
} else {
stopIndex = currentIndex - 1;
}
}

return null;
return longestValidText;
}

type TextConfig = {|
Expand Down