robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

robinmerge: 9a29622b3b00ddb601

master
19.1 KiB687 linesraw
1#include <stdio.h>
2
3#include "nvim/vterm/pen.h"
4#include "nvim/vterm/vterm.h"
5#include "nvim/vterm/vterm_internal_defs.h"
6
7#include "vterm/pen.c.generated.h"
8
9// Structure used to store RGB triples without the additional metadata stored in VTermColor.
10typedef struct {
11  uint8_t red, green, blue;
12} VTermRGB;
13
14static const VTermRGB ansi_colors[] = {
15  // R    G    B
16  {   0,   0,   0 },  // black
17  { 224,   0,   0 },  // red
18  {   0, 224,   0 },  // green
19  { 224, 224,   0 },  // yellow
20  {   0,   0, 224 },  // blue
21  { 224,   0, 224 },  // magenta
22  {   0, 224, 224 },  // cyan
23  { 224, 224, 224 },  // white == light grey
24
25  // high intensity
26  { 128, 128, 128 },  // black
27  { 255,  64,  64 },  // red
28  {  64, 255,  64 },  // green
29  { 255, 255,  64 },  // yellow
30  {  64,  64, 255 },  // blue
31  { 255,  64, 255 },  // magenta
32  {  64, 255, 255 },  // cyan
33  { 255, 255, 255 },  // white for real
34};
35
36static uint8_t ramp6[] = {
37  0x00, 0x33, 0x66, 0x99, 0xCC, 0xFF,
38};
39
40static uint8_t ramp24[] = {
41  0x00, 0x0B, 0x16, 0x21, 0x2C, 0x37, 0x42, 0x4D, 0x58, 0x63, 0x6E, 0x79,
42  0x85, 0x90, 0x9B, 0xA6, 0xB1, 0xBC, 0xC7, 0xD2, 0xDD, 0xE8, 0xF3, 0xFF,
43};
44
45static void lookup_default_colour_ansi(long idx, VTermColor *col)
46{
47  if (idx >= 0 && idx < 16) {
48    vterm_color_rgb(col,
49                    ansi_colors[idx].red, ansi_colors[idx].green, ansi_colors[idx].blue);
50  }
51}
52
53static bool lookup_colour_ansi(const VTermState *state, long index, VTermColor *col)
54{
55  if (index >= 0 && index < 16) {
56    *col = state->colors[index];
57    return true;
58  }
59
60  return false;
61}
62
63static bool lookup_colour_palette(const VTermState *state, long index, VTermColor *col)
64{
65  if (index >= 0 && index < 16) {
66    // Normal 8 colours or high intensity - parse as palette 0
67    return lookup_colour_ansi(state, index, col);
68  } else if (index >= 16 && index < 232) {
69    // 216-colour cube
70    index -= 16;
71
72    vterm_color_rgb(col, ramp6[index/6/6 % 6],
73                    ramp6[index/6   % 6],
74                    ramp6[index     % 6]);
75
76    return true;
77  } else if (index >= 232 && index < 256) {
78    // 24 greyscales
79    index -= 232;
80
81    vterm_color_rgb(col, ramp24[index], ramp24[index], ramp24[index]);
82
83    return true;
84  }
85
86  return false;
87}
88
89static int lookup_colour(const VTermState *state, int palette, const long args[], int argcount,
90                         VTermColor *col)
91{
92  switch (palette) {
93  case 2: {  // RGB mode - 3 args contain colour values directly
94    // ITU-T T.416 places a colour space id before R:G:B, as in "38:2::R:G:B".
95    // Skip it when this colon-separated group holds more than three arguments.
96    int grouplen = 0;
97    while (grouplen < argcount && CSI_ARG_HAS_MORE(args[grouplen])) {
98      grouplen++;
99    }
100    grouplen = grouplen < argcount ? grouplen + 1 : argcount;
101    const int skip = grouplen > 3 ? 1 : 0;
102
103    if (argcount - skip < 3) {
104      return argcount;
105    }
106
107    vterm_color_rgb(col, (uint8_t)CSI_ARG(args[skip]), (uint8_t)CSI_ARG(args[skip + 1]),
108                    (uint8_t)CSI_ARG(args[skip + 2]));
109
110    return skip + 3;
111  }
112
113  case 5:  // XTerm 256-colour mode
114    if (!argcount || CSI_ARG_IS_MISSING(args[0])) {
115      return argcount ? 1 : 0;
116    }
117
118    vterm_color_indexed(col, (uint8_t)args[0]);
119
120    return argcount ? 1 : 0;
121
122  default:
123    DEBUG_LOG("Unrecognised colour palette %d\n", palette);
124    return 0;
125  }
126}
127
128// Some conveniences
129
130static void setpenattr(VTermState *state, VTermAttr attr, VTermValueType type, VTermValue *val)
131{
132#ifdef DEBUG
133  if (type != vterm_get_attr_type(attr)) {
134    DEBUG_LOG("Cannot set attr %d as it has type %d, not type %d\n",
135              attr, vterm_get_attr_type(attr), type);
136    return;
137  }
138#endif
139  if (state->callbacks && state->callbacks->setpenattr) {
140    (*state->callbacks->setpenattr)(attr, val, state->cbdata);
141  }
142}
143
144static void setpenattr_bool(VTermState *state, VTermAttr attr, int boolean)
145{
146  VTermValue val = { .boolean = boolean };
147  setpenattr(state, attr, VTERM_VALUETYPE_BOOL, &val);
148}
149
150static void setpenattr_int(VTermState *state, VTermAttr attr, int number)
151{
152  VTermValue val = { .number = number };
153  setpenattr(state, attr, VTERM_VALUETYPE_INT, &val);
154}
155
156static void setpenattr_col(VTermState *state, VTermAttr attr, VTermColor color)
157{
158  VTermValue val = { .color = color };
159  setpenattr(state, attr, VTERM_VALUETYPE_COLOR, &val);
160}
161
162static void set_pen_col_ansi(VTermState *state, VTermAttr attr, long col)
163{
164  VTermColor *colp = (attr == VTERM_ATTR_BACKGROUND) ? &state->pen.bg : &state->pen.fg;
165
166  vterm_color_indexed(colp, (uint8_t)col);
167
168  setpenattr_col(state, attr, *colp);
169}
170
171void vterm_state_newpen(VTermState *state)
172{
173  // 90% grey so that pure white is brighter
174  vterm_color_rgb(&state->default_fg, 240, 240, 240);
175  vterm_color_rgb(&state->default_bg, 0, 0, 0);
176  vterm_state_set_default_colors(state, &state->default_fg, &state->default_bg);
177
178  for (int col = 0; col < 16; col++) {
179    lookup_default_colour_ansi(col, &state->colors[col]);
180  }
181}
182
183void vterm_state_resetpen(VTermState *state)
184{
185  state->pen.bold = 0;      setpenattr_bool(state, VTERM_ATTR_BOLD, 0);
186  state->pen.underline = 0; setpenattr_int(state, VTERM_ATTR_UNDERLINE, 0);
187  state->pen.italic = 0;    setpenattr_bool(state, VTERM_ATTR_ITALIC, 0);
188  state->pen.blink = 0;     setpenattr_bool(state, VTERM_ATTR_BLINK, 0);
189  state->pen.reverse = 0;   setpenattr_bool(state, VTERM_ATTR_REVERSE, 0);
190  state->pen.conceal = 0;   setpenattr_bool(state, VTERM_ATTR_CONCEAL, 0);
191  state->pen.strike = 0;    setpenattr_bool(state, VTERM_ATTR_STRIKE, 0);
192  state->pen.font = 0;      setpenattr_int(state, VTERM_ATTR_FONT, 0);
193  state->pen.small = 0;     setpenattr_bool(state, VTERM_ATTR_SMALL, 0);
194  state->pen.baseline = 0;  setpenattr_int(state, VTERM_ATTR_BASELINE, 0);
195  state->pen.dim = 0;       setpenattr_bool(state, VTERM_ATTR_DIM, 0);
196  state->pen.overline = 0;  setpenattr_bool(state, VTERM_ATTR_OVERLINE, 0);
197
198  state->pen.fg = state->default_fg;
199  setpenattr_col(state, VTERM_ATTR_FOREGROUND, state->default_fg);
200  state->pen.bg = state->default_bg;
201  setpenattr_col(state, VTERM_ATTR_BACKGROUND, state->default_bg);
202
203  state->pen.uri = 0; setpenattr_int(state, VTERM_ATTR_URI, 0);
204}
205
206void vterm_state_savepen(VTermState *state, int save)
207{
208  if (save) {
209    state->saved.pen = state->pen;
210  } else {
211    state->pen = state->saved.pen;
212
213    setpenattr_bool(state, VTERM_ATTR_BOLD,      state->pen.bold);
214    setpenattr_int(state, VTERM_ATTR_UNDERLINE, state->pen.underline);
215    setpenattr_bool(state, VTERM_ATTR_ITALIC,    state->pen.italic);
216    setpenattr_bool(state, VTERM_ATTR_BLINK,     state->pen.blink);
217    setpenattr_bool(state, VTERM_ATTR_REVERSE,   state->pen.reverse);
218    setpenattr_bool(state, VTERM_ATTR_CONCEAL,   state->pen.conceal);
219    setpenattr_bool(state, VTERM_ATTR_STRIKE,    state->pen.strike);
220    setpenattr_int(state, VTERM_ATTR_FONT,      state->pen.font);
221    setpenattr_bool(state, VTERM_ATTR_SMALL,     state->pen.small);
222    setpenattr_int(state, VTERM_ATTR_BASELINE,  state->pen.baseline);
223    setpenattr_bool(state, VTERM_ATTR_DIM,      state->pen.dim);
224    setpenattr_bool(state, VTERM_ATTR_OVERLINE, state->pen.overline);
225
226    setpenattr_col(state, VTERM_ATTR_FOREGROUND, state->pen.fg);
227    setpenattr_col(state, VTERM_ATTR_BACKGROUND, state->pen.bg);
228
229    setpenattr_int(state, VTERM_ATTR_URI, state->pen.uri);
230  }
231}
232
233void vterm_state_set_default_colors(VTermState *state, const VTermColor *default_fg,
234                                    const VTermColor *default_bg)
235{
236  if (default_fg) {
237    state->default_fg = *default_fg;
238    state->default_fg.type = (state->default_fg.type & ~VTERM_COLOR_DEFAULT_MASK)
239                             | VTERM_COLOR_DEFAULT_FG;
240  }
241
242  if (default_bg) {
243    state->default_bg = *default_bg;
244    state->default_bg.type = (state->default_bg.type & ~VTERM_COLOR_DEFAULT_MASK)
245                             | VTERM_COLOR_DEFAULT_BG;
246  }
247}
248
249void vterm_state_set_palette_color(VTermState *state, int index, const VTermColor *col)
250{
251  if (index >= 0 && index < 16) {
252    state->colors[index] = *col;
253  }
254}
255
256/// Makes sure that the given color `col` is indeed an RGB colour. After this
257/// function returns, VTERM_COLOR_IS_RGB(col) will return true, while all other
258/// flags stored in `col->type` will have been reset.
259///
260/// @param state is the VTermState instance from which the colour palette should
261/// be extracted.
262/// @param col is a pointer at the VTermColor instance that should be converted
263/// to an RGB colour.
264void vterm_state_convert_color_to_rgb(const VTermState *state, VTermColor *col)
265{
266  if (VTERM_COLOR_IS_INDEXED(col)) {  // Convert indexed colors to RGB
267    lookup_colour_palette(state, col->indexed.idx, col);
268  }
269  col->type &= VTERM_COLOR_TYPE_MASK;  // Reset any metadata but the type
270}
271
272void vterm_state_setpen(VTermState *state, const long args[], int argcount)
273{
274  // SGR - ECMA-48 8.3.117
275
276  int argi = 0;
277  int value;
278
279  while (argi < argcount) {
280    // This logic is easier to do 'done' backwards; set it true, and make it
281    // false again in the 'default' case
282    int done = 1;
283
284    long arg;
285    switch (arg = CSI_ARG(args[argi])) {
286    case CSI_ARG_MISSING:
287    case 0:  // Reset
288      vterm_state_resetpen(state);
289      break;
290
291    case 1: {  // Bold on
292      const VTermColor *fg = &state->pen.fg;
293      state->pen.bold = 1;
294      setpenattr_bool(state, VTERM_ATTR_BOLD, 1);
295      if (!VTERM_COLOR_IS_DEFAULT_FG(fg) && VTERM_COLOR_IS_INDEXED(fg) && fg->indexed.idx < 8
296          && state->bold_is_highbright) {
297        set_pen_col_ansi(state, VTERM_ATTR_FOREGROUND, fg->indexed.idx + (state->pen.bold ? 8 : 0));
298      }
299      break;
300    }
301
302    case 2:  // Dim/faint on
303      state->pen.dim = 1;
304      setpenattr_bool(state, VTERM_ATTR_DIM, 1);
305      break;
306
307    case 3:  // Italic on
308      state->pen.italic = 1;
309      setpenattr_bool(state, VTERM_ATTR_ITALIC, 1);
310      break;
311
312    case 4:  // Underline
313      state->pen.underline = VTERM_UNDERLINE_SINGLE;
314      if (CSI_ARG_HAS_MORE(args[argi])) {
315        argi++;
316        switch (CSI_ARG(args[argi])) {
317        case 0:
318          state->pen.underline = 0;
319          break;
320        case 1:
321          state->pen.underline = VTERM_UNDERLINE_SINGLE;
322          break;
323        case 2:
324          state->pen.underline = VTERM_UNDERLINE_DOUBLE;
325          break;
326        case 3:
327          state->pen.underline = VTERM_UNDERLINE_CURLY;
328          break;
329        }
330      }
331      setpenattr_int(state, VTERM_ATTR_UNDERLINE, state->pen.underline);
332      break;
333
334    case 5:  // Blink
335      state->pen.blink = 1;
336      setpenattr_bool(state, VTERM_ATTR_BLINK, 1);
337      break;
338
339    case 7:  // Reverse on
340      state->pen.reverse = 1;
341      setpenattr_bool(state, VTERM_ATTR_REVERSE, 1);
342      break;
343
344    case 8:  // Conceal on
345      state->pen.conceal = 1;
346      setpenattr_bool(state, VTERM_ATTR_CONCEAL, 1);
347      break;
348
349    case 9:  // Strikethrough on
350      state->pen.strike = 1;
351      setpenattr_bool(state, VTERM_ATTR_STRIKE, 1);
352      break;
353
354    case 10:
355    case 11:
356    case 12:
357    case 13:
358    case 14:
359    case 15:
360    case 16:
361    case 17:
362    case 18:
363    case 19:  // Select font
364      state->pen.font = CSI_ARG(args[argi]) - 10;
365      setpenattr_int(state, VTERM_ATTR_FONT, state->pen.font);
366      break;
367
368    case 21:  // Underline double
369      state->pen.underline = VTERM_UNDERLINE_DOUBLE;
370      setpenattr_int(state, VTERM_ATTR_UNDERLINE, state->pen.underline);
371      break;
372
373    case 22:  // Normal intensity (bold and dim off)
374      state->pen.bold = 0;
375      setpenattr_bool(state, VTERM_ATTR_BOLD, 0);
376      state->pen.dim = 0;
377      setpenattr_bool(state, VTERM_ATTR_DIM, 0);
378      break;
379
380    case 23:  // Italic and Gothic (currently unsupported) off
381      state->pen.italic = 0;
382      setpenattr_bool(state, VTERM_ATTR_ITALIC, 0);
383      break;
384
385    case 24:  // Underline off
386      state->pen.underline = 0;
387      setpenattr_int(state, VTERM_ATTR_UNDERLINE, 0);
388      break;
389
390    case 25:  // Blink off
391      state->pen.blink = 0;
392      setpenattr_bool(state, VTERM_ATTR_BLINK, 0);
393      break;
394
395    case 27:  // Reverse off
396      state->pen.reverse = 0;
397      setpenattr_bool(state, VTERM_ATTR_REVERSE, 0);
398      break;
399
400    case 28:  // Conceal off (Reveal)
401      state->pen.conceal = 0;
402      setpenattr_bool(state, VTERM_ATTR_CONCEAL, 0);
403      break;
404
405    case 29:  // Strikethrough off
406      state->pen.strike = 0;
407      setpenattr_bool(state, VTERM_ATTR_STRIKE, 0);
408      break;
409
410    case 30:
411    case 31:
412    case 32:
413    case 33:
414    case 34:
415    case 35:
416    case 36:
417    case 37:  // Foreground colour palette
418      value = CSI_ARG(args[argi]) - 30;
419      if (state->pen.bold && state->bold_is_highbright) {
420        value += 8;
421      }
422      set_pen_col_ansi(state, VTERM_ATTR_FOREGROUND, value);
423      break;
424
425    case 38:  // Foreground colour alternative palette
426      if (argcount - argi < 1) {
427        return;
428      }
429      argi += 1 + lookup_colour(state, CSI_ARG(args[argi + 1]), args + argi + 2,
430                                argcount - argi - 2, &state->pen.fg);
431      setpenattr_col(state, VTERM_ATTR_FOREGROUND, state->pen.fg);
432      break;
433
434    case 39:  // Foreground colour default
435      state->pen.fg = state->default_fg;
436      setpenattr_col(state, VTERM_ATTR_FOREGROUND, state->pen.fg);
437      break;
438
439    case 40:
440    case 41:
441    case 42:
442    case 43:
443    case 44:
444    case 45:
445    case 46:
446    case 47:  // Background colour palette
447      value = CSI_ARG(args[argi]) - 40;
448      set_pen_col_ansi(state, VTERM_ATTR_BACKGROUND, value);
449      break;
450
451    case 48:  // Background colour alternative palette
452      if (argcount - argi < 1) {
453        return;
454      }
455      argi += 1 + lookup_colour(state, CSI_ARG(args[argi + 1]), args + argi + 2,
456                                argcount - argi - 2, &state->pen.bg);
457      setpenattr_col(state, VTERM_ATTR_BACKGROUND, state->pen.bg);
458      break;
459
460    case 49:  // Default background
461      state->pen.bg = state->default_bg;
462      setpenattr_col(state, VTERM_ATTR_BACKGROUND, state->pen.bg);
463      break;
464
465    case 53:  // Overline on
466      state->pen.overline = 1;
467      setpenattr_bool(state, VTERM_ATTR_OVERLINE, 1);
468      break;
469
470    case 55:  // Overline off
471      state->pen.overline = 0;
472      setpenattr_bool(state, VTERM_ATTR_OVERLINE, 0);
473      break;
474
475    case 73:  // Superscript
476    case 74:  // Subscript
477    case 75:  // Superscript/subscript off
478      state->pen.small = (arg != 75);
479      state->pen.baseline =
480        (arg == 73) ? VTERM_BASELINE_RAISE
481                    : (arg == 74) ? VTERM_BASELINE_LOWER
482                                  : VTERM_BASELINE_NORMAL;
483      setpenattr_bool(state, VTERM_ATTR_SMALL,    state->pen.small);
484      setpenattr_int(state, VTERM_ATTR_BASELINE, state->pen.baseline);
485      break;
486
487    case 90:
488    case 91:
489    case 92:
490    case 93:
491    case 94:
492    case 95:
493    case 96:
494    case 97:  // Foreground colour high-intensity palette
495      value = CSI_ARG(args[argi]) - 90 + 8;
496      set_pen_col_ansi(state, VTERM_ATTR_FOREGROUND, value);
497      break;
498
499    case 100:
500    case 101:
501    case 102:
502    case 103:
503    case 104:
504    case 105:
505    case 106:
506    case 107:  // Background colour high-intensity palette
507      value = CSI_ARG(args[argi]) - 100 + 8;
508      set_pen_col_ansi(state, VTERM_ATTR_BACKGROUND, value);
509      break;
510
511    default:
512      done = 0;
513      break;
514    }
515
516    if (!done) {
517      DEBUG_LOG("libvterm: Unhandled CSI SGR %ld\n", arg);
518    }
519
520    while (CSI_ARG_HAS_MORE(args[argi++])) {}
521  }
522}
523
524static int vterm_state_getpen_color(const VTermColor *col, int argi, long args[], int fg)
525{
526  // Do nothing if the given color is the default color
527  if ((fg && VTERM_COLOR_IS_DEFAULT_FG(col))
528      || (!fg && VTERM_COLOR_IS_DEFAULT_BG(col))) {
529    return argi;
530  }
531
532  // Decide whether to send an indexed color or an RGB color
533  if (VTERM_COLOR_IS_INDEXED(col)) {
534    const uint8_t idx = col->indexed.idx;
535    if (idx < 8) {
536      args[argi++] = (idx + (fg ? 30 : 40));
537    } else if (idx < 16) {
538      args[argi++] = (idx - 8 + (fg ? 90 : 100));
539    } else {
540      args[argi++] = CSI_ARG_FLAG_MORE | (fg ? 38 : 48);
541      args[argi++] = CSI_ARG_FLAG_MORE | 5;
542      args[argi++] = idx;
543    }
544  } else if (VTERM_COLOR_IS_RGB(col)) {
545    args[argi++] = CSI_ARG_FLAG_MORE | (fg ? 38 : 48);
546    args[argi++] = CSI_ARG_FLAG_MORE | 2;
547    args[argi++] = CSI_ARG_FLAG_MORE | col->rgb.red;
548    args[argi++] = CSI_ARG_FLAG_MORE | col->rgb.green;
549    args[argi++] = col->rgb.blue;
550  }
551  return argi;
552}
553
554int vterm_state_getpen(VTermState *state, long args[], int argcount)
555{
556  int argi = 0;
557
558  if (state->pen.bold) {
559    args[argi++] = 1;
560  }
561
562  if (state->pen.dim) {
563    args[argi++] = 2;
564  }
565
566  if (state->pen.italic) {
567    args[argi++] = 3;
568  }
569
570  if (state->pen.underline == VTERM_UNDERLINE_SINGLE) {
571    args[argi++] = 4;
572  }
573  if (state->pen.underline == VTERM_UNDERLINE_CURLY) {
574    args[argi++] = 4 | CSI_ARG_FLAG_MORE, args[argi++] = 3;
575  }
576
577  if (state->pen.blink) {
578    args[argi++] = 5;
579  }
580
581  if (state->pen.reverse) {
582    args[argi++] = 7;
583  }
584
585  if (state->pen.conceal) {
586    args[argi++] = 8;
587  }
588
589  if (state->pen.strike) {
590    args[argi++] = 9;
591  }
592
593  if (state->pen.font) {
594    args[argi++] = 10 + state->pen.font;
595  }
596
597  if (state->pen.underline == VTERM_UNDERLINE_DOUBLE) {
598    args[argi++] = 21;
599  }
600
601  argi = vterm_state_getpen_color(&state->pen.fg, argi, args, true);
602
603  argi = vterm_state_getpen_color(&state->pen.bg, argi, args, false);
604
605  if (state->pen.overline) {
606    args[argi++] = 53;
607  }
608
609  if (state->pen.small) {
610    if (state->pen.baseline == VTERM_BASELINE_RAISE) {
611      args[argi++] = 73;
612    } else if (state->pen.baseline == VTERM_BASELINE_LOWER) {
613      args[argi++] = 74;
614    }
615  }
616
617  return argi;
618}
619
620int vterm_state_set_penattr(VTermState *state, VTermAttr attr, VTermValueType type, VTermValue *val)
621{
622  if (!val) {
623    return 0;
624  }
625
626  if (type != vterm_get_attr_type(attr)) {
627    DEBUG_LOG("Cannot set attr %d as it has type %d, not type %d\n",
628              attr, vterm_get_attr_type(attr), type);
629    return 0;
630  }
631
632  switch (attr) {
633  case VTERM_ATTR_BOLD:
634    state->pen.bold = (unsigned)val->boolean;
635    break;
636  case VTERM_ATTR_UNDERLINE:
637    state->pen.underline = (unsigned)val->number;
638    break;
639  case VTERM_ATTR_ITALIC:
640    state->pen.italic = (unsigned)val->boolean;
641    break;
642  case VTERM_ATTR_BLINK:
643    state->pen.blink = (unsigned)val->boolean;
644    break;
645  case VTERM_ATTR_REVERSE:
646    state->pen.reverse = (unsigned)val->boolean;
647    break;
648  case VTERM_ATTR_CONCEAL:
649    state->pen.conceal = (unsigned)val->boolean;
650    break;
651  case VTERM_ATTR_STRIKE:
652    state->pen.strike = (unsigned)val->boolean;
653    break;
654  case VTERM_ATTR_FONT:
655    state->pen.font = (unsigned)val->number;
656    break;
657  case VTERM_ATTR_FOREGROUND:
658    state->pen.fg = val->color;
659    break;
660  case VTERM_ATTR_BACKGROUND:
661    state->pen.bg = val->color;
662    break;
663  case VTERM_ATTR_SMALL:
664    state->pen.small = (unsigned)val->boolean;
665    break;
666  case VTERM_ATTR_BASELINE:
667    state->pen.baseline = (unsigned)val->number;
668    break;
669  case VTERM_ATTR_URI:
670    state->pen.uri = val->number;
671    break;
672  case VTERM_ATTR_DIM:
673    state->pen.dim = (unsigned)val->boolean;
674    break;
675  case VTERM_ATTR_OVERLINE:
676    state->pen.overline = (unsigned)val->boolean;
677    break;
678  default:
679    return 0;
680  }
681
682  if (state->callbacks && state->callbacks->setpenattr) {
683    (*state->callbacks->setpenattr)(attr, val, state->cbdata);
684  }
685
686  return 1;
687}