2 65d9b3ca 2021-03-14 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
4 65d9b3ca 2021-03-14 op * Permission to use, copy, modify, and distribute this software for any
5 65d9b3ca 2021-03-14 op * purpose with or without fee is hereby granted, provided that the above
6 65d9b3ca 2021-03-14 op * copyright notice and this permission notice appear in all copies.
8 65d9b3ca 2021-03-14 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 65d9b3ca 2021-03-14 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 65d9b3ca 2021-03-14 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 65d9b3ca 2021-03-14 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 65d9b3ca 2021-03-14 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 65d9b3ca 2021-03-14 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 65d9b3ca 2021-03-14 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 65d9b3ca 2021-03-14 op #include <stdlib.h>
18 65d9b3ca 2021-03-14 op #include <string.h>
20 88693f61 2021-07-15 op #include "defaults.h"
21 5caf7d67 2021-07-12 op #include "telescope.h"
22 5caf7d67 2021-07-12 op #include "utf8.h"
25 65d9b3ca 2021-03-14 op * Text wrapping
26 65d9b3ca 2021-03-14 op * =============
28 65d9b3ca 2021-03-14 op * There's a simple text wrapping algorithm.
30 65d9b3ca 2021-03-14 op * 1. if it's a line in a pre-formatted block:
31 65d9b3ca 2021-03-14 op * a. hard wrap.
33 676fba6f 2021-03-21 op * 2. otherwise advance the line char by char.
34 676fba6f 2021-03-21 op * 3. when ending the space, split the line at the last occurrence of
35 676fba6f 2021-03-21 op * a "word separator" (i.e. " \t-") or at point if none.
41 bca92a4c 2021-07-01 op erase_buffer(struct buffer *buffer)
43 bca92a4c 2021-07-01 op empty_vlist(buffer);
44 bca92a4c 2021-07-01 op empty_linelist(buffer);
48 46f6e974 2021-05-17 op empty_linelist(struct buffer *buffer)
50 ca5da938 2021-04-01 op struct line *l, *lt;
52 46f6e974 2021-05-17 op TAILQ_FOREACH_SAFE(l, &buffer->page.head, lines, lt) {
53 46f6e974 2021-05-17 op TAILQ_REMOVE(&buffer->page.head, l, lines);
54 ca5da938 2021-04-01 op free(l->line);
61 46f6e974 2021-05-17 op empty_vlist(struct buffer *buffer)
63 ca5da938 2021-04-01 op struct vline *vl, *t;
65 4ea9160a 2021-07-01 op buffer->top_line = NULL;
66 4ea9160a 2021-07-01 op buffer->line_off = 0;
67 46f6e974 2021-05-17 op buffer->current_line = NULL;
68 46f6e974 2021-05-17 op buffer->line_max = 0;
70 46f6e974 2021-05-17 op TAILQ_FOREACH_SAFE(vl, &buffer->head, vlines, t) {
71 46f6e974 2021-05-17 op TAILQ_REMOVE(&buffer->head, vl, vlines);
72 ca5da938 2021-04-01 op free(vl->line);
78 87f7918d 2021-07-14 op push_line(struct buffer *buffer, struct line *l, const char *buf, size_t len, int flags)
80 65d9b3ca 2021-03-14 op struct vline *vl;
82 6e6c6afb 2021-07-14 op if (!(l->flags & L_HIDDEN))
83 6e6c6afb 2021-07-14 op buffer->line_max++;
85 65d9b3ca 2021-03-14 op if ((vl = calloc(1, sizeof(*vl))) == NULL)
88 65d9b3ca 2021-03-14 op if (len != 0 && (vl->line = calloc(1, len+1)) == NULL) {
93 65d9b3ca 2021-03-14 op vl->parent = l;
95 65d9b3ca 2021-03-14 op memcpy(vl->line, buf, len);
96 5492b68f 2021-07-05 op vl->flags = flags;
98 46f6e974 2021-05-17 op if (TAILQ_EMPTY(&buffer->head))
99 46f6e974 2021-05-17 op TAILQ_INSERT_HEAD(&buffer->head, vl, vlines);
101 46f6e974 2021-05-17 op TAILQ_INSERT_TAIL(&buffer->head, vl, vlines);
106 e7b982f4 2021-07-14 op * Similar to wrap_text, but emit only o vline.
109 e7b982f4 2021-07-14 op wrap_one(struct buffer *buffer, const char *prfx, struct line *l, size_t width)
111 e7b982f4 2021-07-14 op struct vline *vl, *t;
114 e7b982f4 2021-07-14 op * be lazy: call wrap_text and then discard the continuations.
117 e7b982f4 2021-07-14 op if (!wrap_text(buffer, prfx, l, width))
120 e7b982f4 2021-07-14 op TAILQ_FOREACH_SAFE(vl, &buffer->head, vlines, t) {
121 e7b982f4 2021-07-14 op if (vl->flags & L_CONTINUATION) {
122 e7b982f4 2021-07-14 op TAILQ_REMOVE(&buffer->head, vl, vlines);
123 e7b982f4 2021-07-14 op free(vl->line);
125 6eddfe3c 2021-07-15 op buffer->line_max--;
133 65d9b3ca 2021-03-14 op * Build a list of visual line by wrapping the given line, assuming
134 65d9b3ca 2021-03-14 op * that when printed will have a leading prefix prfx.
137 46f6e974 2021-05-17 op wrap_text(struct buffer *buffer, const char *prfx, struct line *l, size_t width)
139 676fba6f 2021-03-21 op const char *separators = " \t-";
140 218ac7f6 2021-07-16 op const char *start, *end, *line, *lastsep, *lastchar, *space;
141 676fba6f 2021-03-21 op uint32_t cp = 0, state = 0;
142 676fba6f 2021-03-21 op size_t cur, prfxwidth, w;
145 676fba6f 2021-03-21 op if ((line = l->line) == NULL)
146 46f6e974 2021-05-17 op return push_line(buffer, l, NULL, 0, 0);
148 676fba6f 2021-03-21 op prfxwidth = utf8_swidth(prfx);
149 676fba6f 2021-03-21 op cur = prfxwidth;
150 676fba6f 2021-03-21 op start = line;
151 676fba6f 2021-03-21 op lastsep = NULL;
152 676fba6f 2021-03-21 op lastchar = line;
155 218ac7f6 2021-07-16 op if (l->type == LINE_LINK && emojify_link &&
156 218ac7f6 2021-07-16 op emojied_line(l->line, &space)) {
157 218ac7f6 2021-07-16 op prfxwidth = utf8_swidth_between(l->line, space);
158 218ac7f6 2021-07-16 op cur = prfxwidth;
159 218ac7f6 2021-07-16 op line = space + 1;
162 676fba6f 2021-03-21 op for (; *line; line++) {
163 676fba6f 2021-03-21 op if (utf8_decode(&state, &cp, *line))
165 676fba6f 2021-03-21 op w = utf8_chwidth(cp);
166 1247b8cc 2021-07-16 op if (cur + w > width) {
167 676fba6f 2021-03-21 op end = lastsep == NULL
168 676fba6f 2021-03-21 op ? utf8_next_cp((char*)lastchar)
169 676fba6f 2021-03-21 op : utf8_next_cp((char*)lastsep);
170 5492b68f 2021-07-05 op if (!push_line(buffer, l, start, end - start, flags))
172 963c680c 2021-07-05 op flags = L_CONTINUATION;
174 676fba6f 2021-03-21 op cur = prfxwidth + utf8_swidth_between(start, lastchar);
175 5b010818 2021-07-05 op } else if (strchr(separators, *line) != NULL) {
176 5b010818 2021-07-05 op lastsep = line;
179 676fba6f 2021-03-21 op lastchar = utf8_prev_cp(line, l->line);
183 5492b68f 2021-07-05 op return push_line(buffer, l, start, line - start, flags);
187 46f6e974 2021-05-17 op hardwrap_text(struct buffer *buffer, struct line *l, size_t width)
189 676fba6f 2021-03-21 op const char *line, *start, *lastchar;
191 676fba6f 2021-03-21 op uint32_t state = 0, cp = 0;
192 676fba6f 2021-03-21 op size_t cur, w;
194 676fba6f 2021-03-21 op if ((line = l->line) == NULL)
195 46f6e974 2021-05-17 op return push_line(buffer, l, NULL, 0, 0);
197 676fba6f 2021-03-21 op start = line;
198 676fba6f 2021-03-21 op lastchar = line;
201 676fba6f 2021-03-21 op for (; *line; line++) {
202 676fba6f 2021-03-21 op if (utf8_decode(&state, &cp, *line))
204 676fba6f 2021-03-21 op w = utf8_chwidth(cp);
205 676fba6f 2021-03-21 op if (cur + w >= width) {
206 46f6e974 2021-05-17 op if (!push_line(buffer, l, start, lastchar - start, cont))
208 88693f61 2021-07-15 op cont = L_CONTINUATION;
209 88693f61 2021-07-15 op if (dont_wrap_pre)
212 676fba6f 2021-03-21 op start = lastchar;
215 676fba6f 2021-03-21 op lastchar = utf8_prev_cp(line, l->line);
219 46f6e974 2021-05-17 op return push_line(buffer, l, start, line - start, cont);