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 empty_linelist(struct buffer *buffer)
40 {
41 struct line *l, *lt;
43 TAILQ_FOREACH_SAFE(l, &buffer->page.head, lines, lt) {
44 TAILQ_REMOVE(&buffer->page.head, l, lines);
45 free(l->line);
46 free(l->alt);
47 free(l);
48 }
49 }
51 void
52 empty_vlist(struct buffer *buffer)
53 {
54 struct vline *vl, *t;
56 buffer->current_line = NULL;
57 buffer->line_max = 0;
59 TAILQ_FOREACH_SAFE(vl, &buffer->head, vlines, t) {
60 TAILQ_REMOVE(&buffer->head, vl, vlines);
61 free(vl->line);
62 free(vl);
63 }
64 }
66 static int
67 push_line(struct buffer *buffer, const struct line *l, const char *buf, size_t len, int cont)
68 {
69 struct vline *vl;
71 buffer->line_max++;
73 if ((vl = calloc(1, sizeof(*vl))) == NULL)
74 return 0;
76 if (len != 0 && (vl->line = calloc(1, len+1)) == NULL) {
77 free(vl);
78 return 0;
79 }
81 vl->parent = l;
82 if (len != 0)
83 memcpy(vl->line, buf, len);
84 vl->flags = cont;
86 if (TAILQ_EMPTY(&buffer->head))
87 TAILQ_INSERT_HEAD(&buffer->head, vl, vlines);
88 else
89 TAILQ_INSERT_TAIL(&buffer->head, vl, vlines);
90 return 1;
91 }
93 /*
94 * Build a list of visual line by wrapping the given line, assuming
95 * that when printed will have a leading prefix prfx.
96 */
97 int
98 wrap_text(struct buffer *buffer, const char *prfx, struct line *l, size_t width)
99 {
100 const char *separators = " \t-";
101 const char *start, *end, *line, *lastsep, *lastchar;
102 uint32_t cp = 0, state = 0;
103 size_t cur, prfxwidth, w;
104 int cont;
106 if ((line = l->line) == NULL)
107 return push_line(buffer, l, NULL, 0, 0);
109 prfxwidth = utf8_swidth(prfx);
110 cur = prfxwidth;
111 start = line;
112 lastsep = NULL;
113 lastchar = line;
114 cont = 0;
115 for (; *line; line++) {
116 if (utf8_decode(&state, &cp, *line))
117 continue;
118 w = utf8_chwidth(cp);
119 if (cur + w >= width -1) {
120 end = lastsep == NULL
121 ? utf8_next_cp((char*)lastchar)
122 : utf8_next_cp((char*)lastsep);
123 if (!push_line(buffer, l, start, end - start, cont))
124 return 0;
125 cont = 1;
126 start = end;
127 cur = prfxwidth + utf8_swidth_between(start, lastchar);
128 } else {
129 if (strchr(separators, *line) != NULL)
130 lastsep = line;
133 lastchar = utf8_prev_cp(line, l->line);
134 cur += w;
137 return push_line(buffer, l, start, line - start, cont);
140 int
141 hardwrap_text(struct buffer *buffer, struct line *l, size_t width)
143 const char *line, *start, *lastchar;
144 int cont;
145 uint32_t state = 0, cp = 0;
146 size_t cur, w;
148 if ((line = l->line) == NULL)
149 return push_line(buffer, l, NULL, 0, 0);
151 start = line;
152 lastchar = line;
153 cont = 0;
154 cur = 0;
155 for (; *line; line++) {
156 if (utf8_decode(&state, &cp, *line))
157 continue;
158 w = utf8_chwidth(cp);
159 if (cur + w >= width) {
160 if (!push_line(buffer, l, start, lastchar - start, cont))
161 return 0;
162 cont = 1;
163 cur = 0;
164 start = lastchar;
167 lastchar = utf8_prev_cp(line, l->line);
168 cur += w;
171 return push_line(buffer, l, start, line - start, cont);