Blob


1 /*
2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include "telescope.h"
19 #include <stdlib.h>
20 #include <string.h>
22 /*
23 * Text wrapping
24 * =============
25 *
26 * There's a simple text wrapping algorithm.
27 *
28 * 1. if it's a line in a pre-formatted block:
29 * a. hard wrap.
30 * b. repeat
31 * 2. otherwise advance the line char by char.
32 * 3. when ending the space, split the line at the last occurrence of
33 * a "word separator" (i.e. " \t-") or at point if none.
34 * 4. repeat
35 *
36 */
38 void
39 erase_buffer(struct buffer *buffer)
40 {
41 empty_vlist(buffer);
42 empty_linelist(buffer);
43 }
45 void
46 empty_linelist(struct buffer *buffer)
47 {
48 struct line *l, *lt;
50 TAILQ_FOREACH_SAFE(l, &buffer->page.head, lines, lt) {
51 TAILQ_REMOVE(&buffer->page.head, l, lines);
52 free(l->line);
53 free(l->alt);
54 free(l);
55 }
56 }
58 void
59 empty_vlist(struct buffer *buffer)
60 {
61 struct vline *vl, *t;
63 buffer->top_line = NULL;
64 buffer->line_off = 0;
65 buffer->current_line = NULL;
66 buffer->line_max = 0;
68 TAILQ_FOREACH_SAFE(vl, &buffer->head, vlines, t) {
69 TAILQ_REMOVE(&buffer->head, vl, vlines);
70 free(vl->line);
71 free(vl);
72 }
73 }
75 static int
76 push_line(struct buffer *buffer, const struct line *l, const char *buf, size_t len, int flags)
77 {
78 struct vline *vl;
80 buffer->line_max++;
82 if ((vl = calloc(1, sizeof(*vl))) == NULL)
83 return 0;
85 if (len != 0 && (vl->line = calloc(1, len+1)) == NULL) {
86 free(vl);
87 return 0;
88 }
90 vl->parent = l;
91 if (len != 0)
92 memcpy(vl->line, buf, len);
93 vl->flags = flags;
95 if (TAILQ_EMPTY(&buffer->head))
96 TAILQ_INSERT_HEAD(&buffer->head, vl, vlines);
97 else
98 TAILQ_INSERT_TAIL(&buffer->head, vl, vlines);
99 return 1;
102 /*
103 * Build a list of visual line by wrapping the given line, assuming
104 * that when printed will have a leading prefix prfx.
105 */
106 int
107 wrap_text(struct buffer *buffer, const char *prfx, struct line *l, size_t width)
109 const char *separators = " \t-";
110 const char *start, *end, *line, *lastsep, *lastchar;
111 uint32_t cp = 0, state = 0;
112 size_t cur, prfxwidth, w;
113 int flags;
115 if ((line = l->line) == NULL)
116 return push_line(buffer, l, NULL, 0, 0);
118 prfxwidth = utf8_swidth(prfx);
119 cur = prfxwidth;
120 start = line;
121 lastsep = NULL;
122 lastchar = line;
123 flags = 0;
124 for (; *line; line++) {
125 if (utf8_decode(&state, &cp, *line))
126 continue;
127 w = utf8_chwidth(cp);
128 if (cur + w >= width -1) {
129 end = lastsep == NULL
130 ? utf8_next_cp((char*)lastchar)
131 : utf8_next_cp((char*)lastsep);
132 if (!push_line(buffer, l, start, end - start, flags))
133 return 0;
134 flags = L_CONTINUATION;
135 start = end;
136 cur = prfxwidth + utf8_swidth_between(start, lastchar);
137 } else if (strchr(separators, *line) != NULL) {
138 lastsep = line;
141 lastchar = utf8_prev_cp(line, l->line);
142 cur += w;
145 return push_line(buffer, l, start, line - start, flags);
148 int
149 hardwrap_text(struct buffer *buffer, struct line *l, size_t width)
151 const char *line, *start, *lastchar;
152 int cont;
153 uint32_t state = 0, cp = 0;
154 size_t cur, w;
156 if ((line = l->line) == NULL)
157 return push_line(buffer, l, NULL, 0, 0);
159 start = line;
160 lastchar = line;
161 cont = 0;
162 cur = 0;
163 for (; *line; line++) {
164 if (utf8_decode(&state, &cp, *line))
165 continue;
166 w = utf8_chwidth(cp);
167 if (cur + w >= width) {
168 if (!push_line(buffer, l, start, lastchar - start, cont))
169 return 0;
170 cont = 1;
171 cur = 0;
172 start = lastchar;
175 lastchar = utf8_prev_cp(line, l->line);
176 cur += w;
179 return push_line(buffer, l, start, line - start, cont);