Commit e6aa7fe4 authored by Koji Ishii's avatar Koji Ishii Committed by Chromium LUCI CQ

Hyphenate the last word if only one word in the line

Blink (legacy and NG) and WebKit does not hyphenate the last
word in a paragraph, except when it is a single-word paragraph.
However, when the containing block is narrow, sometimes
authors want a long last word to be hyphenated.

This patch changtes the heursitic to allow hyphenation if the
line is a single-word line.

CSS Text 4 has a property to control this behavior, but no
browsers have implemented it yet.

Bug: 1022415
Change-Id: Ibd8d8b132e540a142f18efc09a37fed2b207bfdf
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2599567
Commit-Queue: Kent Tamura <tkent@chromium.org>
Auto-Submit: Koji Ishii <kojii@chromium.org>
Reviewed-by: default avatarKent Tamura <tkent@chromium.org>
Cr-Commit-Position: refs/heads/master@{#842345}
parent 734de1a6
...@@ -50,11 +50,18 @@ bool IsAllSpaces(const String& text, unsigned start, unsigned end) { ...@@ -50,11 +50,18 @@ bool IsAllSpaces(const String& text, unsigned start, unsigned end) {
.IsAllSpecialCharacters<IsBreakableSpace>(); .IsAllSpecialCharacters<IsBreakableSpace>();
} }
bool ShouldHyphenate(const String& text, unsigned start, unsigned end) { bool ShouldHyphenate(const String& text,
unsigned word_start,
unsigned word_end,
unsigned line_start) {
// If this is the first word in this line, allow to hyphenate. Otherwise the
// word will overflow.
if (word_start <= line_start)
return true;
// Do not hyphenate the last word in a paragraph, except when it's a single // Do not hyphenate the last word in a paragraph, except when it's a single
// word paragraph. // word paragraph.
if (IsAllSpaces(text, end, text.length())) if (IsAllSpaces(text, word_end, text.length()))
return IsAllSpaces(text, 0, start); return IsAllSpaces(text, 0, word_start);
return true; return true;
} }
...@@ -141,7 +148,7 @@ ShapingLineBreaker::BreakOpportunity ShapingLineBreaker::Hyphenate( ...@@ -141,7 +148,7 @@ ShapingLineBreaker::BreakOpportunity ShapingLineBreaker::Hyphenate(
LazyLineBreakIterator::IsBreakableSpace(text[word_start])) LazyLineBreakIterator::IsBreakableSpace(text[word_start]))
word_start++; word_start++;
if (offset >= word_start && if (offset >= word_start &&
ShouldHyphenate(text, previous_break_opportunity, word_end)) { ShouldHyphenate(text, previous_break_opportunity, word_end, start)) {
unsigned prefix_length = Hyphenate(offset, word_start, word_end, backwards); unsigned prefix_length = Hyphenate(offset, word_start, word_end, backwards);
if (prefix_length) if (prefix_length)
return {word_start + prefix_length, true}; return {word_start + prefix_length, true};
......
<!DOCTYPE html>
<title>CSS Text: `hyphens: auto` for last word</title>
<link rel="author" title="Koji Ishii" href="mailto:kojii@chromium.org">
<link rel="help" href="https://www.w3.org/TR/css-text-3/#hyphenation">
<link rel="match" href="reference/hyphens-auto-last-word-001-ref.html">
<link rel="match" href="reference/hyphens-auto-last-word-001-ref2.html">
<style>
div {
hyphens: auto;
width: 5ch;
border: 1px solid blue;
}
</style>
<body lang="en-us">
<div style="width: 10ch">Test example</div>
<div>example</div>
<div>1 example</div>
<div>1234 example</div>
<div>example 5678</div>
<div>1234 example 5678</div>
</body>
<!DOCTYPE html>
<style>
div {
width: 5ch;
border: 1px solid blue;
}
</style>
<body lang="en-us">
<div style="width: 10ch">Test ex&shy;am&shy;ple</div>
<div>ex&shy;am&shy;ple</div>
<div>1 ex&shy;am&shy;ple</div>
<div>1234 ex&shy;am&shy;ple</div>
<div>ex&shy;am&shy;ple 5678</div>
<div>1234 ex&shy;am&shy;ple 5678</div>
</body>
<!DOCTYPE html>
<style>
div {
width: 5ch;
border: 1px solid blue;
}
</style>
<body lang="en-us">
<div style="width: 10ch">Test example</div>
<div>ex&shy;am&shy;ple</div>
<div>1<br>ex&shy;am&shy;ple</div>
<div>1234 ex&shy;am&shy;ple</div>
<div>ex&shy;am&shy;ple 5678</div>
<div>1234 ex&shy;am&shy;ple 5678</div>
</body>
...@@ -8,6 +8,6 @@ div > div { ...@@ -8,6 +8,6 @@ div > div {
} }
</style> </style>
<div lang="en-us"> <div lang="en-us">
<div>hy-<br>phenation</div> <div>hy-<br>phen-<br>ation</div>
<div>hyphenation</div> <div>hyphenation</div>
</div> </div>
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment