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 <stdlib.h>
18 #include <string.h>
20 #include "telescope.h"
21 #include "utf8.h"
23 /*
24 * Text wrapping
25 * =============
26 *
27 * There's a simple text wrapping algorithm.
28 *
29 * 1. if it's a line in a pre-formatted block:
30 * a. hard wrap.
31 * b. repeat
32 * 2. otherwise advance the line char by char.
33 * 3. when ending the space, split the line at the last occurrence of
34 * a "word separator" (i.e. " \t-") or at point if none.
35 * 4. repeat
36 *
37 */
39 void
40 erase_buffer(struct buffer *buffer)
41 {
42 empty_vlist(buffer);
43 empty_linelist(buffer);
44 }
46 void
47 empty_linelist(struct buffer *buffer)
48 {
49 struct line *l, *lt;
51 TAILQ_FOREACH_SAFE(l, &buffer->page.head, lines, lt) {
52 TAILQ_REMOVE(&buffer->page.head, l, lines);
53 free(l->line);
54 free(l->alt);
55 free(l);
56 }
57 }
59 void
60 empty_vlist(struct buffer *buffer)
61 {
62 struct vline *vl, *t;
64 buffer->top_line = NULL;
65 buffer->line_off = 0;
66 buffer->current_line = NULL;
67 buffer->line_max = 0;
69 TAILQ_FOREACH_SAFE(vl, &buffer->head, vlines, t) {
70 TAILQ_REMOVE(&buffer->head, vl, vlines);
71 free(vl->line);
72 free(vl);
73 }
74 }
76 static int
77 push_line(struct buffer *buffer, const struct line *l, const char *buf, size_t len, int flags)
78 {
79 struct vline *vl;
81 buffer->line_max++;
83 if ((vl = calloc(1, sizeof(*vl))) == NULL)
84 return 0;
86 if (len != 0 && (vl->line = calloc(1, len+1)) == NULL) {
87 free(vl);
88 return 0;
89 }
91 vl->parent = l;
92 if (len != 0)
93 memcpy(vl->line, buf, len);
94 vl->flags = flags;
96 if (TAILQ_EMPTY(&buffer->head))
97 TAILQ_INSERT_HEAD(&buffer->head, vl, vlines);
98 else
99 TAILQ_INSERT_TAIL(&buffer->head, vl, vlines);
100 return 1;
103 /*
104 * Similar to wrap_text, but emit only o vline.
105 */
106 int
107 wrap_one(struct buffer *buffer, const char *prfx, struct line *l, size_t width)
109 struct vline *vl, *t;
111 /*
112 * be lazy: call wrap_text and then discard the continuations.
113 */
115 if (!wrap_text(buffer, prfx, l, width))
116 return 0;
118 TAILQ_FOREACH_SAFE(vl, &buffer->head, vlines, t) {
119 if (vl->flags & L_CONTINUATION) {
120 TAILQ_REMOVE(&buffer->head, vl, vlines);
121 free(vl->line);
122 free(vl);
126 return 1;
129 /*
130 * Build a list of visual line by wrapping the given line, assuming
131 * that when printed will have a leading prefix prfx.
132 */
133 int
134 wrap_text(struct buffer *buffer, const char *prfx, struct line *l, size_t width)
136 const char *separators = " \t-";
137 const char *start, *end, *line, *lastsep, *lastchar;
138 uint32_t cp = 0, state = 0;
139 size_t cur, prfxwidth, w;
140 int flags;
142 if ((line = l->line) == NULL)
143 return push_line(buffer, l, NULL, 0, 0);
145 prfxwidth = utf8_swidth(prfx);
146 cur = prfxwidth;
147 start = line;
148 lastsep = NULL;
149 lastchar = line;
150 flags = 0;
151 for (; *line; line++) {
152 if (utf8_decode(&state, &cp, *line))
153 continue;
154 w = utf8_chwidth(cp);
155 if (cur + w >= width -1) {
156 end = lastsep == NULL
157 ? utf8_next_cp((char*)lastchar)
158 : utf8_next_cp((char*)lastsep);
159 if (!push_line(buffer, l, start, end - start, flags))
160 return 0;
161 flags = L_CONTINUATION;
162 start = end;
163 cur = prfxwidth + utf8_swidth_between(start, lastchar);
164 } else if (strchr(separators, *line) != NULL) {
165 lastsep = line;
168 lastchar = utf8_prev_cp(line, l->line);
169 cur += w;
172 return push_line(buffer, l, start, line - start, flags);
175 int
176 hardwrap_text(struct buffer *buffer, struct line *l, size_t width)
178 const char *line, *start, *lastchar;
179 int cont;
180 uint32_t state = 0, cp = 0;
181 size_t cur, w;
183 if ((line = l->line) == NULL)
184 return push_line(buffer, l, NULL, 0, 0);
186 start = line;
187 lastchar = line;
188 cont = 0;
189 cur = 0;
190 for (; *line; line++) {
191 if (utf8_decode(&state, &cp, *line))
192 continue;
193 w = utf8_chwidth(cp);
194 if (cur + w >= width) {
195 if (!push_line(buffer, l, start, lastchar - start, cont))
196 return 0;
197 cont = 1;
198 cur = 0;
199 start = lastchar;
202 lastchar = utf8_prev_cp(line, l->line);
203 cur += w;
206 return push_line(buffer, l, start, line - start, cont);