groff-commit
[Top][All Lists]
Advanced

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

[groff] 18/21: src/roff/troff/node.cpp: Refactor make_definite().


From: G. Branden Robinson
Subject: [groff] 18/21: src/roff/troff/node.cpp: Refactor make_definite().
Date: Tue, 23 Aug 2022 14:18:43 -0400 (EDT)

gbranden pushed a commit to branch master
in repository groff.

commit 085765dca1cacc5bd643a4582a79c1fd415af469
Author: G. Branden Robinson <g.branden.robinson@gmail.com>
AuthorDate: Tue Aug 23 08:20:47 2022 -0500

    src/roff/troff/node.cpp: Refactor make_definite().
    
    * src/roff/troff/node.cpp (font_family::make_definite): Refactor.
      Rename parameter from inscrutable `i` to `mounting_position`.  Add
      `assert()` to enforce invariant: we're passed a mounting position that
      is nonnegative.  Use `pos` as short alias of parameter once we're into
      the function body.  Relocate conditional branches to front-load early
      returns as well as those requiring little code to handle.  This
      reduces the average indentation level of the function, a readability
      win.
---
 ChangeLog               | 11 +++++++
 src/roff/troff/node.cpp | 88 ++++++++++++++++++++++++-------------------------
 2 files changed, 54 insertions(+), 45 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index a8b21d151..e704ebfd5 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+2022-08-23  G. Branden Robinson <g.branden.robinson@gmail.com>
+
+       * src/roff/troff/node.cpp (font_family::make_definite):
+       Refactor.  Rename parameter from inscrutable `i` to
+       `mounting_position`.  Add `assert()` to enforce invariant: we're
+       passed a mounting position that is nonnegative.  Use `pos` as
+       short alias of parameter once we're into the function body.
+       Relocate conditional branches to front-load early returns as
+       well as those requiring little code to handle.  This reduces the
+       average indentation level of the function, a readability win.
+
 2022-08-23  G. Branden Robinson <g.branden.robinson@gmail.com>
 
        [man]: Restore robustness to `EE` misuse.
diff --git a/src/roff/troff/node.cpp b/src/roff/troff/node.cpp
index e64361967..6a33c169e 100644
--- a/src/roff/troff/node.cpp
+++ b/src/roff/troff/node.cpp
@@ -6081,52 +6081,50 @@ font_family::~font_family()
   delete[] map;
 }
 
-int font_family::make_definite(int i)
-{
-  if (i >= 0) {
-    if (i < map_size && map[i] >= 0)
-      return map[i];
-    else {
-      if (i < font_table_size && font_table[i] != 0) {
-       if (i >= map_size) {
-         int old_map_size = map_size;
-         int *old_map = map;
-         map_size *= 3;
-         map_size /= 2;
-         if (i >= map_size)
-           map_size = i + 10;
-         map = new int[map_size];
-         memcpy(map, old_map, old_map_size*sizeof(int));
-         delete[] old_map;
-         for (int j = old_map_size; j < map_size; j++)
-           map[j] = -1;
-       }
-       if (font_table[i]->is_style()) {
-         symbol sty = font_table[i]->get_name();
-         symbol f = concat(nm, sty);
-         int n;
-         // don't use symbol_fontno, because that might return a style
-         // and because we don't want to translate the name
-         for (n = 0; n < font_table_size; n++)
-           if (font_table[n] != 0 && font_table[n]->is_named(f)
-               && !font_table[n]->is_style())
-             break;
-         if (n >= font_table_size) {
-           n = next_available_font_position();
-           if (!mount_font_no_translate(n, f, f))
-             return -1;
-         }
-         return map[i] = n;
-       }
-       else
-         return map[i] = i;
-      }
-      else
-       return -1;
-    }
-  }
-  else
+// Resolve a requested font mounting position to a mounting position
+// usable by the output driver.  (Positions 1 through 4 are typically
+// allocated to styles, and are not usable thus.)  A return value of
+// `-1` indicates failure.
+int font_family::make_definite(int mounting_position)
+{
+  assert(mounting_position >= 0);
+  int pos = mounting_position;
+  if (pos < 0)
+    return -1;
+  if (pos < map_size && map[pos] >= 0)
+    return map[pos];
+  if (!(pos < font_table_size && font_table[pos] != 0))
     return -1;
+  if (pos >= map_size) {
+    int old_map_size = map_size;
+    int *old_map = map;
+    map_size *= 3;
+    map_size /= 2;
+    if (pos >= map_size)
+      map_size = pos + 10;
+    map = new int[map_size];
+    memcpy(map, old_map, old_map_size*sizeof(int));
+    delete[] old_map;
+    for (int j = old_map_size; j < map_size; j++)
+      map[j] = -1;
+  }
+  if (!(font_table[pos]->is_style()))
+    return map[pos] = pos;
+  symbol sty = font_table[pos]->get_name();
+  symbol f = concat(nm, sty);
+  int n;
+  // Don't use symbol_fontno, because that might return a style and
+  // because we don't want to translate the name.
+  for (n = 0; n < font_table_size; n++)
+    if (font_table[n] != 0 && font_table[n]->is_named(f)
+       && !font_table[n]->is_style())
+      break;
+  if (n >= font_table_size) {
+    n = next_available_font_position();
+    if (!mount_font_no_translate(n, f, f))
+      return -1;
+  }
+  return map[pos] = n;
 }
 
 dictionary family_dictionary(5);



reply via email to

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