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, *space;
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;
155 if (l->type == LINE_LINK && emojify_link &&
156 emojied_line(l->line, &space)) {
157 prfxwidth = utf8_swidth_between(l->line, space);
158 cur = prfxwidth;
159 line = space + 1;
162 for (; *line; line++) {
163 if (utf8_decode(&state, &cp, *line))
164 continue;
165 w = utf8_chwidth(cp);
166 if (cur + w > width) {
167 end = lastsep == NULL
168 ? utf8_next_cp((char*)lastchar)
169 : utf8_next_cp((char*)lastsep);
170 if (!push_line(buffer, l, start, end - start, flags))
171 return 0;
172 flags = L_CONTINUATION;
173 start = end;
174 cur = prfxwidth + utf8_swidth_between(start, lastchar);
175 } else if (strchr(separators, *line) != NULL) {
176 lastsep = line;
179 lastchar = utf8_prev_cp(line, l->line);
180 cur += w;
183 return push_line(buffer, l, start, line - start, flags);
186 int
187 hardwrap_text(struct buffer *buffer, struct line *l, size_t width)
189 const char *line, *start, *lastchar;
190 int cont;
191 uint32_t state = 0, cp = 0;
192 size_t cur, w;
194 if ((line = l->line) == NULL)
195 return push_line(buffer, l, NULL, 0, 0);
197 start = line;
198 lastchar = line;
199 cont = 0;
200 cur = 0;
201 for (; *line; line++) {
202 if (utf8_decode(&state, &cp, *line))
203 continue;
204 w = utf8_chwidth(cp);
205 if (cur + w >= width) {
206 if (!push_line(buffer, l, start, lastchar - start, cont))
207 return 0;
208 cont = L_CONTINUATION;
209 if (dont_wrap_pre)
210 return 1;
211 cur = 0;
212 start = lastchar;
215 lastchar = utf8_prev_cp(line, l->line);
216 cur += w;
219 return push_line(buffer, l, start, line - start, cont);