summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJaakko Keränen <jaakko.keranen@iki.fi>2020-10-12 08:54:47 +0300
committerJaakko Keränen <jaakko.keranen@iki.fi>2020-10-12 08:54:47 +0300
commit4a3cda29bbf2494061fb1090698cf1230e43a280 (patch)
treed0359861c951763ac253f2815cac50a343b83d02
parent8dbd6148fd761bf62bcb93b5d86c9e3fd37507e6 (diff)
Normalizing long spaces to a tab stop
Experimenting with terminal friendly space normalization where sometimes multiple spaces are used to align content horizontally (with a fixed-width font). When encountering 8 or more consecutive spaces, they are all replaced with a single tab character. Gemtext allows clients to normalize whitespace as they see fit.
-rw-r--r--src/gmdocument.c8
-rw-r--r--src/ui/text.c6
2 files changed, 14 insertions, 0 deletions
diff --git a/src/gmdocument.c b/src/gmdocument.c
index f800f6ed..87b8a22c 100644
--- a/src/gmdocument.c
+++ b/src/gmdocument.c
@@ -967,11 +967,18 @@ static void normalize_GmDocument(iGmDocument *d) {
967 continue; 967 continue;
968 } 968 }
969 iBool isPrevSpace = iFalse; 969 iBool isPrevSpace = iFalse;
970 int spaceCount = 0;
970 for (const char *ch = line.start; ch != line.end; ch++) { 971 for (const char *ch = line.start; ch != line.end; ch++) {
971 char c = *ch; 972 char c = *ch;
972 if (c == '\r') continue; 973 if (c == '\r') continue;
973 if (isNormalizableSpace_(c)) { 974 if (isNormalizableSpace_(c)) {
974 if (isPrevSpace) { 975 if (isPrevSpace) {
976 if (++spaceCount == 8) {
977 /* There are several consecutive space characters. The author likely
978 really wants to have some space here, so normalize to a tab stop. */
979 popBack_Block(&normalized->chars);
980 pushBack_Block(&normalized->chars, '\t');
981 }
975 continue; /* skip repeated spaces */ 982 continue; /* skip repeated spaces */
976 } 983 }
977 c = ' '; 984 c = ' ';
@@ -979,6 +986,7 @@ static void normalize_GmDocument(iGmDocument *d) {
979 } 986 }
980 else { 987 else {
981 isPrevSpace = iFalse; 988 isPrevSpace = iFalse;
989 spaceCount = 0;
982 } 990 }
983 appendCStrN_String(normalized, &c, 1); 991 appendCStrN_String(normalized, &c, 1);
984 } 992 }
diff --git a/src/ui/text.c b/src/ui/text.c
index 57ecce37..08c89e47 100644
--- a/src/ui/text.c
+++ b/src/ui/text.c
@@ -608,6 +608,12 @@ static iRect run_Font_(iFont *d, enum iRunMode mode, iRangecc text, size_t maxLe
608 prevCh = ch; 608 prevCh = ch;
609 continue; 609 continue;
610 } 610 }
611 if (ch == '\t') {
612 const int tabStopWidth = d->height * 8;
613 xpos = pos.x + ((int) ((xpos - pos.x) / tabStopWidth) + 1) * tabStopWidth;
614 prevCh = 0;
615 continue;
616 }
611 if (ch == '\r') { 617 if (ch == '\r') {
612 const iChar esc = nextChar_(&chPos, text.end); 618 const iChar esc = nextChar_(&chPos, text.end);
613 if (mode == draw_RunMode) { 619 if (mode == draw_RunMode) {