groff-commit
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[groff] 14/20: [tbl]: Refactor.


From: G. Branden Robinson
Subject: [groff] 14/20: [tbl]: Refactor.
Date: Fri, 3 Feb 2023 16:47:55 -0500 (EST)

gbranden pushed a commit to branch master
in repository groff.

commit 0acf94675211eae539290aad78c57ccde1be7bfa
Author: G. Branden Robinson <g.branden.robinson@gmail.com>
AuthorDate: Fri Feb 3 06:08:01 2023 -0600

    [tbl]: Refactor.
    
    * src/preproc/tbl/table.cpp:
    * src/preproc/tbl/table.h: Rename `compute_expand_width` to
      `compute_overall_width`, since this member function is used on _all_
      tables, not just those undergoing column or gap expansion.  For
      instance, in a post-groff 1.22.4 development, it throws a diagnostic
      if an unexpanded table overruns the line length.
    
    * src/preproc/tbl/table.cpp (table::compute_widths): Update call site of
      `compute_overall_width`.
    
    * src/preproc/tbl/table.cpp: Split the roff register behind the
      `EXPAND_REG` C++ preprocessor macro into two, adding
      `AVAILABLE_WIDTH_REG`.  Annotate the distinction.
      (compute_overall_width): Annotate.  Move and conditionalize logic so
      as not to produce as much unnecessary roff output.
    
    This commit omits an indentation update so as to clarify the change.
---
 ChangeLog                 | 18 ++++++++++++++++++
 src/preproc/tbl/table.cpp | 38 ++++++++++++++++++++++++++------------
 src/preproc/tbl/table.h   |  2 +-
 3 files changed, 45 insertions(+), 13 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index b0841faf2..b3b8325bd 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,21 @@
+2023-02-03  G. Branden Robinson <g.branden.robinson@gmail.com>
+
+       [tbl]: Refactor.
+
+       * src/preproc/tbl/table.cpp:
+       * src/preproc/tbl/table.h: Rename `compute_expand_width` to
+       `compute_overall_width`, since this member function is used on
+       _all_ tables, not just those undergoing column or gap expansion.
+       For instance, in a post-groff 1.22.4 development, it throws a
+       diagnostic if an unexpanded table overruns the line length.
+       * src/preproc/tbl/table.cpp (table::compute_widths): Update call
+       site of `compute_overall_width`.
+       * src/preproc/tbl/table.cpp: Split the roff register behind the
+       `EXPAND_REG` C++ preprocessor macro into two, adding
+       `AVAILABLE_WIDTH_REG`.  Annotate the distinction.
+       (compute_overall_width): Annotate.  Move and conditionalize
+       logic so as not to produce as much unnecessary roff output.
+
 2023-02-03  G. Branden Robinson <g.branden.robinson@gmail.com>
 
        * src/preproc/tbl/main.cpp (main): Avoid reading from invalid
diff --git a/src/preproc/tbl/table.cpp b/src/preproc/tbl/table.cpp
index 0d1b13186..1ffdf6062 100644
--- a/src/preproc/tbl/table.cpp
+++ b/src/preproc/tbl/table.cpp
@@ -77,7 +77,8 @@ const int DEFAULT_COLUMN_SEPARATION = 3;
 // for use with `ig` requests embedded inside macro definitions
 #define NOP_NAME PREFIX "nop"
 
-#define EXPAND_REG PREFIX "expand"
+#define AVAILABLE_WIDTH_REG PREFIX "available-width"
+#define EXPAND_REG PREFIX "expansion-amount"
 
 #define LEADER_REG PREFIX LEADER
 
@@ -2175,20 +2176,23 @@ void table::build_span_list()
   }
 }
 
-void table::compute_expand_width()
+void table::compute_overall_width()
 {
-  // First, compute the unexpanded table width, measuring every column
-  // (including those eligible for expansion).
-  prints(".nr " EXPAND_REG " \\n[.l]-\\n[.i]");
+  prints(".\\\" compute overall width\n");
+  // Compute the amount of horizontal space available for expansion,
+  // measuring every column _including_ those eligible for expansion.
+  // This is the minimum required to set the table without compression.
+  prints(".nr " EXPAND_REG " 0\n");
+  prints(".nr " AVAILABLE_WIDTH_REG " \\n[.l]-\\n[.i]");
   for (int i = 0; i < ncolumns; i++)
     printfs("-\\n[%1]", span_width_reg(i, i));
   if (total_separation)
     printfs("-%1n", as_string(total_separation));
   prints("\n");
-  prints(".if \\n[" EXPAND_REG "]<0 \\{\\\n");
   // If the "expand" region option was given, a different warning will
   // be issued later (if "nowarn" was not also specified).
   if ((!(flags & NOWARN)) && (!(flags & EXPAND))) {
+    prints(".if \\n[" AVAILABLE_WIDTH_REG "]<0 \\{\\\n");
     // Protect characters in diagnostic message (especially :, [, ])
     // from being interpreted by eqn.
     prints(".  ig\n"
@@ -2205,12 +2209,21 @@ void table::compute_expand_width()
           "delim on\n"
           ".EN\n"
           ".  .\n");
-    prints(".  nr " EXPAND_REG " 0\n");
+    prints(".  nr " AVAILABLE_WIDTH_REG " 0\n");
+    prints(".\\}\n");
   }
-  prints(".\\}\n");
-  // Now, iterate through the columns again, spreading any excess line
-  // width among the expanded columns.
-  prints(".nr " EXPAND_REG " \\n[.l]-\\n[.i]");
+  // Now do a similar computation, this time omitting columns that
+  // _aren't_ undergoing expansion.  The difference is the amount of
+  // space we have to distribute among the expanded columns.
+  bool do_expansion = false;
+  for (int i = 0; i < ncolumns; i++)
+    if (expand[i]) {
+      do_expansion = true;
+      break;
+    }
+  if (do_expansion) {
+  prints(".if \\n[" AVAILABLE_WIDTH_REG "] \\\n");
+  prints(".  nr " EXPAND_REG " \\n[.l]-\\n[.i]");
   for (int i = 0; i < ncolumns; i++)
     if (!expand[i])
       printfs("-\\n[%1]", span_width_reg(i, i));
@@ -2225,6 +2238,7 @@ void table::compute_expand_width()
     if (expand[i])
       printfs(".nr %1 \\n[%1]>?\\n[" EXPAND_REG "]\n",
              span_width_reg(i, i));
+  }
 }
 
 void table::compute_total_separation()
@@ -2393,7 +2407,7 @@ void table::compute_widths()
   if (had_spanning_block)
     for (p = span_list; p; p = p->next)
       divide_span(p->start_col, p->end_col);
-  compute_expand_width();
+  compute_overall_width();
   if ((flags & EXPAND) && total_separation != 0) {
     compute_separation_factor();
     for (p = span_list; p; p = p->next)
diff --git a/src/preproc/tbl/table.h b/src/preproc/tbl/table.h
index 283c51863..bac0979c9 100644
--- a/src/preproc/tbl/table.h
+++ b/src/preproc/tbl/table.h
@@ -102,7 +102,7 @@ class table {
   int total_separation;
   int allocated_rows;
   void build_span_list();
-  void compute_expand_width();
+  void compute_overall_width();
   void do_hspan(int r, int c);
   void do_vspan(int r, int c);
   void allocate(int r);



reply via email to

[Prev in Thread] Current Thread [Next in Thread]