Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 4 additions & 1 deletion src/text/paragraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,8 @@ Paragraph Typesetter::set(const std::vector<Span>& spans,
float ascent = strut.ascent;
float descent = strut.descent;
size_t end = range.end;
// A line that ends its paragraph is not justified, as in print.
const bool endsAtBreak = end > range.begin && clusters[end - 1].newline;
// Trailing spaces hang past the line's end.
while (end > range.begin &&
(clusters[end - 1].space || clusters[end - 1].newline)) {
Expand Down Expand Up @@ -630,7 +632,8 @@ Paragraph Typesetter::set(const std::vector<Span>& spans,
} else if (options.align == ParagraphOptions::Align::End) {
pen = options.maxWidth - line.width;
} else if (options.align == ParagraphOptions::Align::Justify &&
!lastLine && std::isfinite(options.maxWidth)) {
!lastLine && !endsAtBreak &&
std::isfinite(options.maxWidth)) {
const auto spaces = std::count_if(
placed.begin(), placed.end(),
[](const Cluster& cluster) { return cluster.space; });
Expand Down
26 changes: 26 additions & 0 deletions tests/paragraph_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,32 @@ TEST_CASE("a centered line offsets its glyphs once, by the line") {
CHECK(paragraph.lines[0].glyphs[0].x == 0);
}

TEST_CASE("a justified line that wraps reaches the far edge") {
Setting setting;
bureau::text::ParagraphOptions options;
options.maxWidth = 120;
const auto start = setting.set("one two three four five six", options);
options.align = bureau::text::ParagraphOptions::Align::Justify;
const auto justified = setting.set("one two three four five six", options);

REQUIRE(justified.lines.size() > 1);
CHECK(justified.lines[0].glyphs.back().x > start.lines[0].glyphs.back().x);
}

TEST_CASE("a justified line that ends its paragraph keeps its words together") {
Setting setting;
bureau::text::ParagraphOptions options;
options.maxWidth = 400;
options.preserveBreaks = true;
const auto start = setting.set("one two\nthree four", options);
options.align = bureau::text::ParagraphOptions::Align::Justify;
const auto justified = setting.set("one two\nthree four", options);

REQUIRE(justified.lines.size() == 2);
CHECK(justified.lines[0].glyphs.back().x ==
doctest::Approx(start.lines[0].glyphs.back().x));
}

TEST_CASE("text shrinks no narrower than its longest word") {
Setting setting;
const auto words = setting.set("a supercalifragilistic word");
Expand Down
Loading