summaryrefslogtreecommitdiff
path: root/src/user/raleigh/s/text_flower.cpp
blob: bd4ecd8691ac85b4155899b142c6020af9f18dc5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <raleigh/s/text_flower.h>
#include <knob/block.h>

namespace raleigh {
  text_flower::text_flower(const char *s, uint32_t cols, uint32_t max_rows)
    : s(s), lines(max_rows ? max_rows : 10), offsets(max_rows ? max_rows : 10),
      max_rows(max_rows), cols(cols) {
    flow_text();
  }

  __attribute__ ((pure))
  uint32_t text_flower::get_n_lines() {
    return lines.n_entries;
  }

  __attribute__ ((pure))
  char *text_flower::get_nth_line(uint32_t n) {
    return lines.buf[n];
  }

  __attribute__ ((pure))
  uint32_t text_flower::get_line_offset(uint32_t n) {
    return offsets.buf[n];
  }

  void text_flower::draw_text(_pixel_t *start, uint32_t pitch, const struct font_info *fi, _pixel_t color) {
    for (uint32_t y = 0; y < lines.n_entries; ++y) {
      const char *line = lines.buf[y];
      for (uint32_t x = 0; line[x]; ++x)
        put_char_no_bg(fi, line[x], start + y * fi->space_height * pitch + x * fi->space_width, pitch, color);
    }
  }

  void text_flower::push_line() {
    lines.add_back(strndup(line_start, row_len));
    offsets.add_back(line_start - s);
    line_start = on_char;
    row_len = 0;
    if (max_rows && (lines.n_entries == max_rows))
      on_char = "";
  }

  void text_flower::flow_text() {
    for (uint32_t i = 0; i < lines.n_entries; ++i)
      free_block(lines.buf[i]);

    lines.n_entries = 0;
    offsets.n_entries = 0;
    line_start = on_char = s;
    row_len = 0;

    while (*on_char) {
      if (*on_char == '\n') {
        ++on_char;
        push_line();
      }

      else if (*on_char == ' ') {
        ++on_char;
        if (row_len != 0) {
          if (++row_len == cols)
            push_line();
        }
        else
          ++line_start;
      }

      else {
        uint32_t word_len = str_find_any(on_char, " \n");
        if (!cols) {
          row_len += word_len;
          on_char += word_len;
        }

        else if (row_len + word_len <= cols) {
          row_len += word_len;
          on_char += word_len;
          if (row_len == cols)
            push_line();
        }

        else if (word_len > cols) {
          on_char += cols - row_len;
          row_len = cols;
          push_line();
        }

        else
          push_line();
      }
    }

    if (row_len && (!max_rows || (lines.n_entries != max_rows)))
      push_line();
  }
}