| 1 |
/* $Id: sixel.c,v 1.31 2024/12/08 18:24:37 tom Exp $ */ |
| 2 |
|
| 3 |
#include <vttest.h> |
| 4 |
#include <ttymodes.h> |
| 5 |
#include <esc.h> |
| 6 |
|
| 7 |
#define is_inter(c) ((c) >= 0x20 && ((c) <= 0x2f)) |
| 8 |
#define is_final(c) ((c) >= 0x30 && ((c) <= 0x7e)) |
| 9 |
|
| 10 |
#define L_CURL '{' |
| 11 |
#define MAX_WIDTH 10 |
| 12 |
|
| 13 |
static char empty[1]; |
| 14 |
|
| 15 |
static const char *EraseCtl = ""; |
| 16 |
static const char *FontName = ""; |
| 17 |
static char *StartingCharPtr = empty; |
| 18 |
static const char *TextCell = ""; |
| 19 |
static const char *WidthAttr = ""; |
| 20 |
static char *font_string = empty; |
| 21 |
|
| 22 |
static int FontNumber; |
| 23 |
static int MatrixHigh; |
| 24 |
static int MatrixWide; |
| 25 |
static int StartingCharNum; |
| 26 |
|
| 27 |
/* |
| 28 |
* Lookup the given character 'chr' in the font-string and write a readable |
| 29 |
* display of the glyph |
| 30 |
*/ |
| 31 |
static void |
| 32 |
decode_header(void) |
| 33 |
{ |
| 34 |
int Pe, Pcms, Pw, Pt; |
| 35 |
char *s; |
| 36 |
|
| 37 |
switch (sscanf(font_string + 2, |
| 38 |
"%d;%d;%d;%d;%d;%d", |
| 39 |
&FontNumber, &StartingCharNum, &Pe, &Pcms, &Pw, &Pt)) { |
| 40 |
case 0: |
| 41 |
FontNumber = 0; |
| 42 |
/* FALLTHRU */ |
| 43 |
case 1: |
| 44 |
StartingCharNum = 0; |
| 45 |
/* FALLTHRU */ |
| 46 |
case 2: |
| 47 |
Pe = 0; |
| 48 |
/* FALLTHRU */ |
| 49 |
case 3: |
| 50 |
Pcms = 0; |
| 51 |
/* FALLTHRU */ |
| 52 |
case 4: |
| 53 |
Pw = 0; |
| 54 |
/* FALLTHRU */ |
| 55 |
case 5: |
| 56 |
Pt = 0; |
| 57 |
/* FALLTHRU */ |
| 58 |
case 6: |
| 59 |
break; |
| 60 |
} |
| 61 |
|
| 62 |
switch (Pcms) { |
| 63 |
case 1: |
| 64 |
MatrixWide = 0; |
| 65 |
MatrixHigh = 0; |
| 66 |
break; /* illegal */ |
| 67 |
case 2: |
| 68 |
MatrixWide = 5; |
| 69 |
MatrixHigh = 10; |
| 70 |
break; |
| 71 |
case 3: |
| 72 |
MatrixWide = 6; |
| 73 |
MatrixHigh = 10; |
| 74 |
break; |
| 75 |
case 0: |
| 76 |
case 4: |
| 77 |
MatrixWide = 7; |
| 78 |
MatrixHigh = 10; |
| 79 |
break; |
| 80 |
default: |
| 81 |
MatrixWide = Pcms; |
| 82 |
MatrixHigh = 10; |
| 83 |
break; /* 5 through 10 */ |
| 84 |
} |
| 85 |
|
| 86 |
switch (Pe) { |
| 87 |
case 0: |
| 88 |
EraseCtl = "this DRCS set"; |
| 89 |
break; |
| 90 |
case 1: |
| 91 |
EraseCtl = "only reloaded chars"; |
| 92 |
break; |
| 93 |
case 2: |
| 94 |
EraseCtl = "all chars in all DRCS sets"; |
| 95 |
break; |
| 96 |
default: |
| 97 |
EraseCtl = "?"; |
| 98 |
break; |
| 99 |
} |
| 100 |
|
| 101 |
switch (Pw) { |
| 102 |
case 0: |
| 103 |
/* FALLTHRU */ |
| 104 |
case 1: |
| 105 |
WidthAttr = "80 cols, 24 lines"; |
| 106 |
break; |
| 107 |
case 2: |
| 108 |
WidthAttr = "132 cols, 24 lines"; |
| 109 |
break; |
| 110 |
case 11: |
| 111 |
WidthAttr = "80 cols, 36 lines"; |
| 112 |
break; |
| 113 |
case 12: |
| 114 |
WidthAttr = "132 cols, 36 lines"; |
| 115 |
break; |
| 116 |
case 21: |
| 117 |
WidthAttr = "80 cols, 24 lines"; |
| 118 |
break; |
| 119 |
case 22: |
| 120 |
WidthAttr = "132 cols, 48 lines"; |
| 121 |
break; |
| 122 |
default: |
| 123 |
WidthAttr = "?"; |
| 124 |
break; |
| 125 |
} |
| 126 |
|
| 127 |
if (Pt == 2) |
| 128 |
TextCell = "Full Cell"; |
| 129 |
else |
| 130 |
TextCell = "Text"; |
| 131 |
for (s = font_string; *s; s++) { |
| 132 |
if (*s == L_CURL) { |
| 133 |
char *t; |
| 134 |
char tmp[BUF_SIZE]; |
| 135 |
size_t use = 0; |
| 136 |
for (t = s + 1; *t; t++) { |
| 137 |
if (is_inter(*t)) { |
| 138 |
tmp[use++] = *t; |
| 139 |
} |
| 140 |
if (is_final(*t)) { |
| 141 |
char *tmp2; |
| 142 |
tmp[use++] = *t++; |
| 143 |
tmp[use] = '\0'; |
| 144 |
if ((tmp2 = malloc(use + 1)) == NULL) { |
| 145 |
no_memory(); |
| 146 |
} else { |
| 147 |
FontName = strcpy(tmp2, tmp); |
| 148 |
StartingCharPtr = t; |
| 149 |
} |
| 150 |
break; |
| 151 |
} |
| 152 |
} |
| 153 |
break; |
| 154 |
} |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
static char * |
| 159 |
find_char(int chr) |
| 160 |
{ |
| 161 |
char *s = StartingCharPtr; |
| 162 |
|
| 163 |
chr -= (' ' + StartingCharNum); |
| 164 |
if (chr < 0) |
| 165 |
return NULL; |
| 166 |
while (chr > 0) { |
| 167 |
do { |
| 168 |
if (*s == '\0') |
| 169 |
return NULL; |
| 170 |
} while (*s++ != ';'); |
| 171 |
chr--; |
| 172 |
} |
| 173 |
return s; |
| 174 |
} |
| 175 |
|
| 176 |
static void |
| 177 |
display_head(FILE *fp) |
| 178 |
{ |
| 179 |
fprintf(fp, "Font %d \"%s\", Matrix %dx%d (%s, %s)\n", |
| 180 |
FontNumber, FontName, MatrixWide, MatrixHigh, WidthAttr, TextCell); |
| 181 |
fprintf(fp, "Start %d, Erase %s\n", |
| 182 |
StartingCharNum, EraseCtl); |
| 183 |
} |
| 184 |
|
| 185 |
static int |
| 186 |
display_char(FILE *fp, int chr) |
| 187 |
{ |
| 188 |
char *s; |
| 189 |
|
| 190 |
s = find_char(chr); |
| 191 |
if (s != NULL) { |
| 192 |
int bit = 0; |
| 193 |
int high = 0; |
| 194 |
int n; |
| 195 |
char bits[6][MAX_WIDTH]; |
| 196 |
|
| 197 |
for (n = 32; n < 127; ++n) { |
| 198 |
int row = 6 + (n % 16); |
| 199 |
int col = 8 * (n / 16); |
| 200 |
vt_move(row, col); |
| 201 |
if (n == chr) |
| 202 |
vt_hilite(1); |
| 203 |
cprintf("%d %cN%c", n, ESC, n); /* use SS2 to invoke G2 into GL */ |
| 204 |
if (n == chr) |
| 205 |
vt_hilite(0); |
| 206 |
} |
| 207 |
vt_move(6, 1); |
| 208 |
|
| 209 |
fprintf(fp, "Glyph '%c'\n", chr); |
| 210 |
|
| 211 |
do { |
| 212 |
if (*s >= '?' && *s <= '~') { |
| 213 |
for (n = 0; n < 6; n++) |
| 214 |
bits[n][bit] = (char) (((*s - '?') & 1 << n) ? 'O' : '.'); |
| 215 |
bit++; |
| 216 |
} else if ((*s == ';' || *s == '/') && bit) { |
| 217 |
for (n = 0; (n < 6) && (high++ < MatrixHigh); n++) { |
| 218 |
bits[n][bit] = '\0'; |
| 219 |
fprintf(fp, "%s\n", bits[n]); |
| 220 |
} |
| 221 |
bit = 0; |
| 222 |
} |
| 223 |
} while (*s++ != ';'); |
| 224 |
return TRUE; |
| 225 |
} |
| 226 |
return FALSE; |
| 227 |
} |
| 228 |
|
| 229 |
static int |
| 230 |
tst_DECDLD(MENU_ARGS) |
| 231 |
{ |
| 232 |
char *s; |
| 233 |
|
| 234 |
vt_move(1, 1); |
| 235 |
printxx("Working...\n"); |
| 236 |
for (s = font_string; *s; s++) { |
| 237 |
putchar(*s); |
| 238 |
if (*s == '\n') { |
| 239 |
fflush(stdout); |
| 240 |
padding(20); |
| 241 |
} |
| 242 |
} |
| 243 |
fflush(stdout); |
| 244 |
padding(20); |
| 245 |
printxx("...done "); |
| 246 |
|
| 247 |
tprintf("%c*%s", ESC, FontName); /* designate G2 as the DRCS font */ |
| 248 |
padding(4); |
| 249 |
fflush(stdout); |
| 250 |
|
| 251 |
return MENU_HOLD; |
| 252 |
} |
| 253 |
|
| 254 |
static int |
| 255 |
tst_display_one(MENU_ARGS) |
| 256 |
{ |
| 257 |
int d, c = -1; |
| 258 |
|
| 259 |
vt_move(1, 1); |
| 260 |
display_head(stdout); |
| 261 |
println(""); |
| 262 |
println("Press any key to display its soft-character. Repeat a key to quit."); |
| 263 |
|
| 264 |
set_tty_raw(TRUE); |
| 265 |
set_tty_echo(FALSE); |
| 266 |
|
| 267 |
pause_replay(); |
| 268 |
do { |
| 269 |
d = c; |
| 270 |
c = get_char(); |
| 271 |
if (c < 0) |
| 272 |
break; |
| 273 |
vt_move(6, 1); |
| 274 |
vt_clear(0); |
| 275 |
if (display_char(stdout, c)) { |
| 276 |
println(""); |
| 277 |
cprintf("Render: %cN%c", ESC, c); /* use SS2 to invoke G2 into GL */ |
| 278 |
} |
| 279 |
} while (c != d); |
| 280 |
resume_replay(); |
| 281 |
|
| 282 |
restore_ttymodes(); |
| 283 |
return MENU_NOHOLD; |
| 284 |
} |
| 285 |
|
| 286 |
static int |
| 287 |
tst_display_all(MENU_ARGS) |
| 288 |
{ |
| 289 |
int c; |
| 290 |
|
| 291 |
vt_move(1, 1); |
| 292 |
display_head(stdout); |
| 293 |
|
| 294 |
set_tty_raw(TRUE); |
| 295 |
set_tty_echo(FALSE); |
| 296 |
|
| 297 |
for (c = 32; c < 127; ++c) { |
| 298 |
vt_move(6, 1); |
| 299 |
display_char(stderr, c); |
| 300 |
zleep(100); |
| 301 |
} |
| 302 |
restore_ttymodes(); |
| 303 |
vt_move(23, 1); |
| 304 |
return MENU_HOLD; |
| 305 |
} |
| 306 |
|
| 307 |
/* |
| 308 |
* Remove all characters in all DRCS sets (the " @" is a dummy name) |
| 309 |
*/ |
| 310 |
static int |
| 311 |
tst_cleanup(MENU_ARGS) |
| 312 |
{ |
| 313 |
do_dcs("1;1;2%c @", L_CURL); |
| 314 |
padding(20); |
| 315 |
return MENU_NOHOLD; |
| 316 |
} |
| 317 |
|
| 318 |
/* |
| 319 |
* Read a soft-character definition string from a file. Strip off garbage |
| 320 |
* at the beginning (to accommodate the "font2xx" output format). |
| 321 |
*/ |
| 322 |
void |
| 323 |
setup_softchars(const char *filename) |
| 324 |
{ |
| 325 |
FILE *fp; |
| 326 |
int c; |
| 327 |
size_t len = 1024; |
| 328 |
size_t use = 0; |
| 329 |
char *buffer; |
| 330 |
char *s; |
| 331 |
const char *first = NULL; |
| 332 |
char *last = NULL; |
| 333 |
int save_8bits = input_8bits; |
| 334 |
input_8bits = FALSE; /* use the 7-bit input-parsing */ |
| 335 |
|
| 336 |
/* read the file into memory */ |
| 337 |
if ((fp = fopen(filename, "r")) == NULL) { |
| 338 |
failed(filename); |
| 339 |
} |
| 340 |
if ((buffer = malloc(len)) == NULL) |
| 341 |
no_memory(); |
| 342 |
while ((c = fgetc(fp)) != EOF) { |
| 343 |
if (use + 1 >= len) { |
| 344 |
char *check; |
| 345 |
if ((check = realloc(buffer, len *= 2)) == NULL) { |
| 346 |
no_memory(); |
| 347 |
} else { |
| 348 |
buffer = check; |
| 349 |
} |
| 350 |
} |
| 351 |
buffer[use++] = (char) c; |
| 352 |
} |
| 353 |
buffer[use] = '\0'; |
| 354 |
fclose(fp); |
| 355 |
|
| 356 |
/* find the DCS that begins the control string */ |
| 357 |
/* and the ST that ends the control string */ |
| 358 |
for (s = buffer; *s; s++) { |
| 359 |
if (first == NULL) { |
| 360 |
if (skip_dcs(s) != NULL) |
| 361 |
first = s; |
| 362 |
} else { |
| 363 |
if (!strncmp(s, st_input(), (size_t) 2)) { |
| 364 |
last = s + 2; |
| 365 |
*last = '\0'; |
| 366 |
break; |
| 367 |
} |
| 368 |
} |
| 369 |
} |
| 370 |
input_8bits = save_8bits; |
| 371 |
|
| 372 |
if (first == NULL || last == NULL) { |
| 373 |
fprintf(stderr, "Not a vtXXX font description: %s\n", filename); |
| 374 |
exit(EXIT_FAILURE); |
| 375 |
} |
| 376 |
for (s = buffer; (*s++ = *first++) != '\0';) ; |
| 377 |
if (LOG_ENABLED && first != NULL) |
| 378 |
fprintf(log_fp, NOTE_STR "font string %s\n", buffer); |
| 379 |
|
| 380 |
font_string = buffer; |
| 381 |
|
| 382 |
decode_header(); |
| 383 |
} |
| 384 |
|
| 385 |
int |
| 386 |
tst_softchars(MENU_ARGS) |
| 387 |
{ |
| 388 |
/* *INDENT-OFF* */ |
| 389 |
static MENU my_menu[] = { |
| 390 |
{ "Exit", NULL }, |
| 391 |
{ "Download the soft characters (DECDLD)", tst_DECDLD }, |
| 392 |
{ "Examine the soft characters, one-by-one", tst_display_one }, |
| 393 |
{ "Examine all the soft characters", tst_display_all }, |
| 394 |
{ "Clear the soft characters", tst_cleanup }, |
| 395 |
{ "", NULL } |
| 396 |
}; |
| 397 |
/* *INDENT-ON* */ |
| 398 |
|
| 399 |
vt_move(1, 1); |
| 400 |
if (font_string == NULL || *font_string == 0) { |
| 401 |
printxx("You did not specify a font-file with the -f option\n"); |
| 402 |
return MENU_HOLD; |
| 403 |
} |
| 404 |
do { |
| 405 |
vt_clear(2); |
| 406 |
__(title(0), printxx("Soft Character Sets")); |
| 407 |
__(title(2), println("Choose test type:")); |
| 408 |
} while (menu(my_menu)); |
| 409 |
return MENU_NOHOLD; |
| 410 |
} |