[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[groff] 04/05: [grotty]: Slightly refactor.
From: |
G. Branden Robinson |
Subject: |
[groff] 04/05: [grotty]: Slightly refactor. |
Date: |
Sat, 9 Oct 2021 06:46:21 -0400 (EDT) |
gbranden pushed a commit to branch master
in repository groff.
commit 3e9b214a881b7cb5ca339505a985ab4f3113fe4b
Author: G. Branden Robinson <g.branden.robinson@gmail.com>
AuthorDate: Fri Oct 8 19:24:51 2021 +1100
[grotty]: Slightly refactor.
* src/devices/grotty/tty.cpp: Boolify. Demote numerous variables (and
one return type) from `int` to `bool`, use Boolean insted of integer
literals with them, and give the variables names resembling logical
predicates.
- horizontal_tab_flag -> want_horizontal_tabs
- form_feed_flag -> want_form_feeds
- bold_flag_option -> want_emboldening_by_overstriking
- bold_flag -> do_bold
- underline_flag_option -> want_italics_by_underlining
- underline_flag -> do_underline
- overstrike_flag -> want_glyph_composition_by_overstriking
- draw_flag -> allow_drawing_commands
- italic_flag_option -> want_sgr_italics
- italic_flag -> do_sgr_italics
- reverse_flag_option -> want_reverse_video_for_italics
- reverse_flag -> do_reverse_video
- old_drawing_scheme -> use_overstriking_drawing_scheme
(class tty_printer:printer): Similarly.
- is_underline -> is_underlining
- is_bold -> is_boldfacing
- cu_flag -> is_continuously_underlining
(tty_printer::tty_color): Demote return type as above. Invert
its sense; rename `unknown_color` to `is_known_color`.
(tty_printer::color_to_idx): Invert sense of test at `tty_color()`
call site.
---
ChangeLog | 30 ++++++++
src/devices/grotty/tty.cpp | 181 +++++++++++++++++++++++----------------------
2 files changed, 121 insertions(+), 90 deletions(-)
diff --git a/ChangeLog b/ChangeLog
index 86e457a..c67290b 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,33 @@
+2021-10-08 G. Branden Robinson <g.branden.robinson@gmail.com>
+
+ [grotty]: Slightly refactor.
+
+ * src/devices/grotty/tty.cpp: Boolify. Demote numerous
+ variables (and one return type) from `int` to `bool`, use
+ Boolean insted of integer literals with them, and give the
+ variables names resembling logical predicates.
+ - horizontal_tab_flag -> want_horizontal_tabs
+ - form_feed_flag -> want_form_feeds
+ - bold_flag_option -> want_emboldening_by_overstriking
+ - bold_flag -> do_bold
+ - underline_flag_option -> want_italics_by_underlining
+ - underline_flag -> do_underline
+ - overstrike_flag -> want_glyph_composition_by_overstriking
+ - draw_flag -> allow_drawing_commands
+ - italic_flag_option -> want_sgr_italics
+ - italic_flag -> do_sgr_italics
+ - reverse_flag_option -> want_reverse_video_for_italics
+ - reverse_flag -> do_reverse_video
+ - old_drawing_scheme -> use_overstriking_drawing_scheme
+ (class tty_printer:printer): Similarly.
+ - is_underline -> is_underlining
+ - is_bold -> is_boldfacing
+ - cu_flag -> is_continuously_underlining
+ (tty_printer::tty_color): Demote return type as above. Invert
+ its sense; rename `unknown_color` to `is_known_color`.
+ (tty_printer::color_to_idx): Invert sense of test at
+ `tty_color()` call site.
+
2021-10-06 G. Branden Robinson <g.branden.robinson@gmail.com>
[grotty]: Slightly refactor.
diff --git a/src/devices/grotty/tty.cpp b/src/devices/grotty/tty.cpp
index a71217e..a440142 100644
--- a/src/devices/grotty/tty.cpp
+++ b/src/devices/grotty/tty.cpp
@@ -43,19 +43,19 @@ extern "C" const char *Version_string;
// A character of the output device fits in a 32-bit word.
typedef unsigned int output_character;
-static int horizontal_tab_flag = 0;
-static int form_feed_flag = 0;
-static int bold_flag_option = 1;
-static int bold_flag;
-static int underline_flag_option = 1;
-static int underline_flag;
-static int overstrike_flag = 1;
-static int draw_flag = 1;
-static int italic_flag_option = 0;
-static int italic_flag;
-static int reverse_flag_option = 0;
-static int reverse_flag;
-static int old_drawing_scheme = 0;
+static bool want_horizontal_tabs = false;
+static bool want_form_feeds = false;
+static bool want_emboldening_by_overstriking = true;
+static bool do_bold;
+static bool want_italics_by_underlining = true;
+static bool do_underline;
+static bool want_glyph_composition_by_overstriking = true;
+static bool allow_drawing_commands = true;
+static bool want_sgr_italics = false;
+static bool do_sgr_italics;
+static bool want_reverse_video_for_italics = false;
+static bool do_reverse_video;
+static bool use_overstriking_drawing_scheme = false;
static void update_options();
static void usage(FILE *stream);
@@ -127,9 +127,9 @@ tty_font *tty_font::load_tty_font(const char *s)
long n;
if (num != 0 && (n = strtol(num, 0, 0)) != 0)
f->mode = (unsigned char)(n & (BOLD_MODE|UNDERLINE_MODE));
- if (!underline_flag)
+ if (!do_underline)
f->mode &= ~UNDERLINE_MODE;
- if (!bold_flag)
+ if (!do_bold)
f->mode &= ~BOLD_MODE;
if ((f->mode & (BOLD_MODE|UNDERLINE_MODE)) == (BOLD_MODE|UNDERLINE_MODE))
f->mode = (unsigned char)((f->mode & ~(BOLD_MODE|UNDERLINE_MODE))
@@ -178,9 +178,9 @@ class tty_printer : public printer {
int cached_vpos;
schar curr_fore_idx;
schar curr_back_idx;
- int is_underline;
- int is_bold;
- int cu_flag;
+ bool is_underlining;
+ bool is_boldfacing;
+ bool is_continuously_underlining;
PTABLE(schar) tty_colors;
void make_underline(int);
void make_bold(output_character, int);
@@ -189,8 +189,8 @@ class tty_printer : public printer {
unsigned char);
void simple_add_char(const output_character, const environment *);
char *make_rgb_string(unsigned int, unsigned int, unsigned int);
- int tty_color(unsigned int, unsigned int, unsigned int, schar *,
- schar = DEFAULT_COLOR_IDX);
+ bool tty_color(unsigned int, unsigned int, unsigned int, schar *,
+ schar = DEFAULT_COLOR_IDX);
void line(int, int, int, int, color *, color *);
void draw_line(int *, int, const environment *);
void draw_polygon(int *, int, const environment *);
@@ -232,22 +232,22 @@ char *tty_printer::make_rgb_string(unsigned int r,
return s;
}
-int tty_printer::tty_color(unsigned int r,
- unsigned int g,
- unsigned int b, schar *idx, schar value)
+bool tty_printer::tty_color(unsigned int r,
+ unsigned int g,
+ unsigned int b, schar *idx, schar value)
{
- int unknown_color = 0;
+ bool is_known_color = true;
char *s = make_rgb_string(r, g, b);
schar *i = tty_colors.lookup(s);
if (!i) {
- unknown_color = 1;
+ is_known_color = false;
i = new schar[1];
*i = value;
tty_colors.define(s, i);
}
*idx = *i;
delete[] s;
- return unknown_color;
+ return is_known_color;
}
tty_printer::tty_printer() : cached_v(0)
@@ -274,7 +274,7 @@ tty_printer::tty_printer() : cached_v(0)
lines = new tty_glyph *[nlines];
for (int i = 0; i < nlines; i++)
lines[i] = 0;
- cu_flag = 0;
+ is_continuously_underlining = false;
}
tty_printer::~tty_printer()
@@ -284,7 +284,7 @@ tty_printer::~tty_printer()
void tty_printer::make_underline(int w)
{
- if (old_drawing_scheme) {
+ if (use_overstriking_drawing_scheme) {
if (!w)
warning("can't underline zero-width character");
else {
@@ -293,21 +293,21 @@ void tty_printer::make_underline(int w)
}
}
else {
- if (!is_underline) {
- if (italic_flag)
+ if (!is_underlining) {
+ if (do_sgr_italics)
putstring(SGR_ITALIC);
- else if (reverse_flag)
+ else if (do_reverse_video)
putstring(SGR_REVERSE);
else
putstring(SGR_UNDERLINE);
}
- is_underline = 1;
+ is_underlining = true;
}
}
void tty_printer::make_bold(output_character c, int w)
{
- if (old_drawing_scheme) {
+ if (use_overstriking_drawing_scheme) {
if (!w)
warning("can't print zero-width character in bold");
else {
@@ -316,9 +316,9 @@ void tty_printer::make_bold(output_character c, int w)
}
}
else {
- if (!is_bold)
+ if (!is_boldfacing)
putstring(SGR_BOLD);
- is_bold = 1;
+ is_boldfacing = true;
}
}
@@ -329,9 +329,9 @@ schar tty_printer::color_to_idx(color *col)
unsigned int r, g, b;
col->get_rgb(&r, &g, &b);
schar idx;
- if (tty_color(r, g, b, &idx)) {
+ if (!tty_color(r, g, b, &idx)) {
char *s = col->print_color();
- error("Unknown color (%1) mapped to default", s);
+ error("unrecognized color '%1' mapped to default", s);
delete[] s;
}
return idx;
@@ -450,9 +450,9 @@ void tty_printer::special(char *arg, const environment
*env, char type)
;
int n;
if (*p != '\0' && sscanf(p, "%d", &n) == 1 && n == 0)
- old_drawing_scheme = 1;
+ use_overstriking_drawing_scheme = true;
else
- old_drawing_scheme = 0;
+ use_overstriking_drawing_scheme = false;
update_options();
}
else if (strncmp(command, "link", p - command) == 0)
@@ -468,7 +468,7 @@ void tty_printer::special(char *arg, const environment
*env, char type)
void tty_printer::special_link(const char *arg, const environment *env)
{
static bool is_link_active = false;
- if (old_drawing_scheme)
+ if (use_overstriking_drawing_scheme)
return;
for (const char *s = OSC8; *s != '\0'; s++)
simple_add_char(*s, env);
@@ -541,7 +541,7 @@ void tty_printer::change_fill_color(const environment *
const env)
void tty_printer::draw(int code, int *p, int np, const environment *env)
{
- if (!draw_flag)
+ if (!allow_drawing_commands)
return;
if (code == 'l')
draw_line(p, np, env);
@@ -682,12 +682,12 @@ void tty_printer::put_color(schar color_index, int back)
if (color_index == DEFAULT_COLOR_IDX) {
putstring(SGR_DEFAULT);
// set bold and underline again
- if (is_bold)
+ if (is_boldfacing)
putstring(SGR_BOLD);
- if (is_underline) {
- if (italic_flag)
+ if (is_underlining) {
+ if (do_sgr_italics)
putstring(SGR_ITALIC);
- else if (reverse_flag)
+ else if (do_reverse_video)
putstring(SGR_REVERSE);
else
putstring(SGR_UNDERLINE);
@@ -757,12 +757,12 @@ void tty_printer::end_page(int page_length)
tty_glyph *nextp;
curr_fore_idx = DEFAULT_COLOR_IDX;
curr_back_idx = DEFAULT_COLOR_IDX;
- is_underline = 0;
- is_bold = 0;
+ is_underlining = false;
+ is_boldfacing = false;
for (p = g; p; delete p, p = nextp) {
nextp = p->next;
if (p->mode & CU_MODE) {
- cu_flag = (p->code != 0);
+ is_continuously_underlining = (p->code != 0);
continue;
}
if (nextp && p->hpos == nextp->hpos) {
@@ -780,7 +780,7 @@ void tty_printer::end_page(int page_length)
nextp->code = p->code;
continue;
}
- if (!overstrike_flag)
+ if (!want_glyph_composition_by_overstriking)
continue;
}
if (hpos > p->hpos) {
@@ -790,44 +790,45 @@ void tty_printer::end_page(int page_length)
} while (hpos > p->hpos);
}
else {
- if (horizontal_tab_flag) {
+ if (want_horizontal_tabs) {
for (;;) {
int next_tab_pos = ((hpos + TAB_WIDTH) / TAB_WIDTH) * TAB_WIDTH;
if (next_tab_pos > p->hpos)
break;
- if (cu_flag)
+ if (is_continuously_underlining)
make_underline(p->w);
- else if (!old_drawing_scheme && is_underline) {
- if (italic_flag)
+ else if (!use_overstriking_drawing_scheme
+ && is_underlining) {
+ if (do_sgr_italics)
putstring(SGR_NO_ITALIC);
- else if (reverse_flag)
+ else if (do_reverse_video)
putstring(SGR_NO_REVERSE);
else
putstring(SGR_NO_UNDERLINE);
- is_underline = 0;
+ is_underlining = false;
}
putchar('\t');
hpos = next_tab_pos;
}
}
for (; hpos < p->hpos; hpos++) {
- if (cu_flag)
+ if (is_continuously_underlining)
make_underline(p->w);
- else if (!old_drawing_scheme && is_underline) {
- if (italic_flag)
+ else if (!use_overstriking_drawing_scheme && is_underlining) {
+ if (do_sgr_italics)
putstring(SGR_NO_ITALIC);
- else if (reverse_flag)
+ else if (do_reverse_video)
putstring(SGR_NO_REVERSE);
else
putstring(SGR_NO_UNDERLINE);
- is_underline = 0;
+ is_underlining = false;
}
putchar(' ');
}
}
assert(hpos == p->hpos);
if (p->mode & COLOR_CHANGE) {
- if (!old_drawing_scheme) {
+ if (!use_overstriking_drawing_scheme) {
if (p->fore_color_idx != curr_fore_idx) {
put_color(p->fore_color_idx, 0);
curr_fore_idx = p->fore_color_idx;
@@ -841,22 +842,22 @@ void tty_printer::end_page(int page_length)
}
if (p->mode & UNDERLINE_MODE)
make_underline(p->w);
- else if (!old_drawing_scheme && is_underline) {
- if (italic_flag)
+ else if (!use_overstriking_drawing_scheme && is_underlining) {
+ if (do_sgr_italics)
putstring(SGR_NO_ITALIC);
- else if (reverse_flag)
+ else if (do_reverse_video)
putstring(SGR_NO_REVERSE);
else
putstring(SGR_NO_UNDERLINE);
- is_underline = 0;
+ is_underlining = false;
}
if (p->mode & BOLD_MODE)
make_bold(p->code, p->w);
- else if (!old_drawing_scheme && is_bold) {
+ else if (!use_overstriking_drawing_scheme && is_boldfacing) {
putstring(SGR_NO_BOLD);
- is_bold = 0;
+ is_boldfacing = false;
}
- if (!old_drawing_scheme) {
+ if (!use_overstriking_drawing_scheme) {
if (p->fore_color_idx != curr_fore_idx) {
put_color(p->fore_color_idx, 0);
curr_fore_idx = p->fore_color_idx;
@@ -869,14 +870,14 @@ void tty_printer::end_page(int page_length)
put_char(p->code);
hpos += p->w / font::hor;
}
- if (!old_drawing_scheme
- && (is_bold || is_underline
+ if (!use_overstriking_drawing_scheme
+ && (is_boldfacing || is_underlining
|| curr_fore_idx != DEFAULT_COLOR_IDX
|| curr_back_idx != DEFAULT_COLOR_IDX))
putstring(SGR_DEFAULT);
putchar('\n');
}
- if (form_feed_flag) {
+ if (want_form_feeds) {
if (last_line < lines_per_page)
putchar('\f');
}
@@ -898,19 +899,19 @@ printer *make_printer()
static void update_options()
{
- if (old_drawing_scheme) {
- italic_flag = 0;
- reverse_flag = 0;
+ if (use_overstriking_drawing_scheme) {
+ do_sgr_italics = false;
+ do_reverse_video = false;
bold_underline_mode = bold_underline_mode_option;
- bold_flag = bold_flag_option;
- underline_flag = underline_flag_option;
+ do_bold = want_emboldening_by_overstriking;
+ do_underline = want_italics_by_underlining;
}
else {
- italic_flag = italic_flag_option;
- reverse_flag = reverse_flag_option;
+ do_sgr_italics = want_sgr_italics;
+ do_reverse_video = want_reverse_video_for_italics;
bold_underline_mode = BOLD_MODE|UNDERLINE_MODE;
- bold_flag = 1;
- underline_flag = 1;
+ do_bold = true;
+ do_underline = true;
}
}
@@ -919,7 +920,7 @@ int main(int argc, char **argv)
program_name = argv[0];
static char stderr_buf[BUFSIZ];
if (getenv("GROFF_NO_SGR"))
- old_drawing_scheme = 1;
+ use_overstriking_drawing_scheme = true;
setbuf(stderr, stderr_buf);
setlocale(LC_CTYPE, "");
int c;
@@ -937,30 +938,30 @@ int main(int argc, char **argv)
break;
case 'i':
// Use italic font instead of underlining.
- italic_flag_option = 1;
+ want_sgr_italics = true;
break;
case 'I':
// ignore include search path
break;
case 'b':
// Do not embolden by overstriking.
- bold_flag_option = 0;
+ want_emboldening_by_overstriking = false;
break;
case 'c':
// Use old scheme for emboldening and underline.
- old_drawing_scheme = 1;
+ use_overstriking_drawing_scheme = true;
break;
case 'u':
// Do not underline.
- underline_flag_option = 0;
+ want_italics_by_underlining = false;
break;
case 'o':
// Do not overstrike (other than emboldening and underlining).
- overstrike_flag = 0;
+ want_glyph_composition_by_overstriking = false;
break;
case 'r':
// Use reverse mode instead of underlining.
- reverse_flag_option = 1;
+ want_reverse_video_for_italics = true;
break;
case 'B':
// Do bold-underlining as bold.
@@ -972,17 +973,17 @@ int main(int argc, char **argv)
break;
case 'h':
// Use horizontal tabs.
- horizontal_tab_flag = 1;
+ want_horizontal_tabs = true;
break;
case 'f':
- form_feed_flag = 1;
+ want_form_feeds = true;
break;
case 'F':
font::command_line_font_dir(optarg);
break;
case 'd':
// Ignore \D commands.
- draw_flag = 0;
+ allow_drawing_commands = false;
break;
case CHAR_MAX + 1: // --help
usage(stdout);
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [groff] 04/05: [grotty]: Slightly refactor.,
G. Branden Robinson <=