gbranden pushed a commit to branch master
in repository groff.
commit c2b493c531d2a39e7234c8c54843fd7537cd5cfe
Author: G. Branden Robinson <[email protected]>
AuthorDate: Thu Apr 3 22:21:51 2025 -0500
[troff]: Trivially refactor.
* src/roff/troff/env.cpp (environment::possibly_break_line): Lift
computation that is performed in 3 of 6 cases into a variable,
`space_deficit`, whose value is computed uncoditionally. (It's an
integer subtraction--not too spendy.) Rename `extra_space_width` to
`extra_space`. Simplify expressions and annotate logic.
---
ChangeLog | 9 +++++++++
src/roff/troff/env.cpp | 24 +++++++++++++-----------
2 files changed, 22 insertions(+), 11 deletions(-)
diff --git a/ChangeLog b/ChangeLog
index 1e141979f..49824cae8 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2025-04-03 G. Branden Robinson <[email protected]>
+
+ * src/roff/troff/env.cpp (environment::possibly_break_line):
+ Trivially refactor. Lift computation that is performed in 3 of
+ 6 cases into a variable, `space_deficit`, whose value is
+ computed uncoditionally. (It's an integer subtraction--not too
+ spendy.) Rename `extra_space_width` to `extra_space`. Simplify
+ expressions and annotate logic.
+
2025-04-03 G. Branden Robinson <[email protected]>
* src/roff/groff/tests/\
diff --git a/src/roff/troff/env.cpp b/src/roff/troff/env.cpp
index 760c0dda4..9fefd2337 100644
--- a/src/roff/troff/env.cpp
+++ b/src/roff/troff/env.cpp
@@ -2259,26 +2259,28 @@ void environment::possibly_break_line(bool
must_break_here,
ndp = &(*ndp)->next;
bp->nd->split(bp->index, &pre, &post);
*ndp = post;
- hunits extra_space_width = H0;
- // TODO: We compute (target_text_length - bp->width) in several
- // cases below. Consider computing it once, unconditionally, and
- // giving it a name (`extra_space_width`? `desired_space`?).
+ // The space deficit tells us how much the line is overset (or, if
+ // negative, underset) relative to the configured line length.
+ hunits space_deficit = target_text_length - bp->width;
+ // The extra space is the amount of space to distribute among the
+ // adjustable space nodes in an output line; this process occurs
+ // only if adjustment is enabled.
+ hunits extra_space = H0;
switch (adjust_mode) {
case ADJUST_BOTH:
if (bp->nspaces != 0)
- extra_space_width = target_text_length - bp->width;
+ extra_space = space_deficit;
else if (bp->width > 0 && target_text_length > 0
&& target_text_length > bp->width)
output_warning(WARN_BREAK, "cannot adjust line; underset by %1"
- " units",
- hunits(target_text_length - bp->width).to_units());
+ " units", space_deficit.to_units());
break;
case ADJUST_CENTER:
- saved_indent += (target_text_length - bp->width)/2;
+ saved_indent += space_deficit / 2;
was_centered = true;
break;
case ADJUST_RIGHT:
- saved_indent += target_text_length - bp->width;
+ saved_indent += space_deficit;
break;
case ADJUST_LEFT:
case ADJUST_CENTER - 1:
@@ -2287,8 +2289,8 @@ void environment::possibly_break_line(bool
must_break_here,
default:
assert(0 == "unhandled case of `adjust_mode`");
}
- distribute_space(pre, bp->nspaces, extra_space_width);
- hunits output_width = bp->width + extra_space_width;
+ distribute_space(pre, bp->nspaces, extra_space);
+ hunits output_width = bp->width + extra_space;
// This should become an assert() when we can get reliable width
// data from CJK glyphs. See Savannah #44018.
if (output_width <= 0) {
_______________________________________________
groff-commit mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/groff-commit