Skip to content

Commit

Permalink
[balance-text] Fix divide-by-zero when very narrow
Browse files Browse the repository at this point in the history
This patch fixes divide-by-zero error in `EstimateNumLines`
when the available width is smaller than `1ch`.

Bug: 1251079
Change-Id: I15888e6e8bbd0a0efb9dad5229dbef1050d668f9
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4295483
Reviewed-by: Ian Kilpatrick <ikilpatrick@chromium.org>
Commit-Queue: Ian Kilpatrick <ikilpatrick@chromium.org>
Auto-Submit: Koji Ishii <kojii@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1111030}
  • Loading branch information
kojiishi authored and Chromium LUCI CQ committed Feb 28, 2023
1 parent a498d36 commit 3bd809f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,15 @@ wtf_size_t EstimateNumLines(const String& text_content,
const SimpleFontData* font,
LayoutUnit available_width) {
const float space_width = font->SpaceWidth();
if (space_width <= 0) {
// Can't estimate without space glyph, go on to measure the actual value.
return 0;
}
const wtf_size_t num_line_chars = available_width / space_width;
if (num_line_chars <= 0) {
// The width is too narrow, don't balance.
return std::numeric_limits<wtf_size_t>::max();
}
return (text_content.length() + num_line_chars - 1) / num_line_chars;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<!DOCTYPE html>
<div style="width: 1px; text-wrap: balance">A</div>

0 comments on commit 3bd809f

Please sign in to comment.