text_canvas.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /* 8x16 text-cell renderer for window content canvases (see text_canvas.h).
  2. *
  3. * The cell grid is the terminal's internal state (char + VGA attribute); this
  4. * file is the only place that knows the terminal cell geometry. The window
  5. * server calls it with the damaged cell block and composites the resulting
  6. * RGB565 canvas. No kernel dependencies, so the host test exercises the same
  7. * code. */
  8. #include "text_canvas.h"
  9. #include "font_8x16.h"
  10. #define TEXT_CURSOR_W 2
  11. /* Standard VGA palette expanded to RGB565; the same values as fbtext.c's
  12. * attr_to_rgb() and the userland WM palette. */
  13. static const unsigned short text_pal565[16] = {
  14. 0x0000, /* 0 black */
  15. 0x0015, /* 1 blue */
  16. 0x0540, /* 2 green */
  17. 0x0555, /* 3 cyan */
  18. 0xA800, /* 4 red */
  19. 0xA815, /* 5 magenta */
  20. 0xAAA0, /* 6 brown */
  21. 0xAD55, /* 7 light gray */
  22. 0x52AA, /* 8 dark gray */
  23. 0x52BF, /* 9 light blue */
  24. 0x57EA, /* 10 light green */
  25. 0x57FF, /* 11 light cyan */
  26. 0xFAAA, /* 12 light red */
  27. 0xFABF, /* 13 light magenta */
  28. 0xFFEA, /* 14 yellow */
  29. 0xFFFF, /* 15 white */
  30. };
  31. static void put_cell(struct text_canvas *c, unsigned int col, unsigned int row,
  32. unsigned char ch, unsigned char attr, int cursor)
  33. {
  34. unsigned int x0 = col * TEXT_CELL_W;
  35. unsigned int y0 = row * TEXT_CELL_H;
  36. unsigned short fg = text_pal565[attr & 0x0F];
  37. unsigned short bg = text_pal565[(attr >> 4) & 0x0F];
  38. const unsigned char *glyph;
  39. unsigned int gy, gx;
  40. if (!c || !c->px || x0 >= c->w || y0 >= c->h)
  41. return;
  42. glyph = &font_8x16[(unsigned int)ch * FONT_8X16_ROWS];
  43. for (gy = 0; gy < TEXT_CELL_H; gy++) {
  44. unsigned int y = y0 + gy;
  45. unsigned short *dst;
  46. unsigned char bits;
  47. if (y >= c->h)
  48. break;
  49. dst = c->px + (unsigned long)y * c->w;
  50. bits = (gy < FONT_8X16_ROWS) ? glyph[gy] : 0;
  51. for (gx = 0; gx < TEXT_CELL_W; gx++) {
  52. unsigned int x = x0 + gx;
  53. if (x >= c->w)
  54. break;
  55. dst[x] = (bits & (0x80u >> gx)) ? fg : bg;
  56. }
  57. }
  58. if (!cursor)
  59. return;
  60. /* Block cursor: a full-cell-height bar in the first two columns. */
  61. for (gy = 0; gy < TEXT_CELL_H; gy++) {
  62. unsigned int y = y0 + gy;
  63. unsigned short *dst;
  64. unsigned int x;
  65. if (y >= c->h)
  66. break;
  67. dst = c->px + (unsigned long)y * c->w;
  68. for (x = 0; x < TEXT_CURSOR_W; x++) {
  69. if (x0 + x >= c->w)
  70. break;
  71. dst[x0 + x] = text_pal565[15];
  72. }
  73. }
  74. }
  75. void text_canvas_render(struct text_canvas *c,
  76. const unsigned short *cells,
  77. unsigned int cols, unsigned int rows,
  78. unsigned int col0, unsigned int row0,
  79. unsigned int ncols, unsigned int nrows,
  80. unsigned int cursor, int cursor_visible, int focused)
  81. {
  82. unsigned int col, row;
  83. if (!c || !c->px || !cells || !cols || !rows)
  84. return;
  85. if (col0 >= cols || row0 >= rows)
  86. return;
  87. if (ncols > cols - col0)
  88. ncols = cols - col0;
  89. if (nrows > rows - row0)
  90. nrows = rows - row0;
  91. if (!ncols || !nrows)
  92. return;
  93. for (row = row0; row < row0 + nrows; row++) {
  94. for (col = col0; col < col0 + ncols; col++) {
  95. unsigned int idx = row * cols + col;
  96. unsigned short cell = cells[idx];
  97. unsigned char ch = (unsigned char)(cell & 0xFF);
  98. unsigned char attr = (unsigned char)(cell >> 8);
  99. int cur = (idx == cursor && cursor_visible && focused);
  100. put_cell(c, col, row, ch, attr, cur);
  101. }
  102. }
  103. }