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