aboutsummaryrefslogtreecommitdiff
path: root/wrap.c
blob: ac846f1c32fe2821e46e2d77d8881622fedb2732 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include "telescope.h"

#include <stdlib.h>
#include <string.h>

/*
 * Text wrapping
 * =============
 *
 * There's a simple text wrapping algorithm.
 *
 * 1. if it's a line in a pre-formatted block:
 *    a. hard wrap.
 *    b. repeat
 * 2. otherwise advance the line char by char.
 * 3. when ending the space, split the line at the last occurrence of
 *    a "word separator" (i.e. " \t-") or at point if none.
 * 4. repeat
 *
 */

void
empty_linelist(struct buffer *buffer)
{
	struct line *l, *lt;

	TAILQ_FOREACH_SAFE(l, &buffer->page.head, lines, lt) {
		TAILQ_REMOVE(&buffer->page.head, l, lines);
		free(l->line);
		free(l->alt);
		free(l);
	}
}

void
empty_vlist(struct buffer *buffer)
{
	struct vline *vl, *t;

	buffer->current_line = NULL;
	buffer->line_max = 0;

	TAILQ_FOREACH_SAFE(vl, &buffer->head, vlines, t) {
		TAILQ_REMOVE(&buffer->head, vl, vlines);
		free(vl->line);
		free(vl);
	}
}

static int
push_line(struct buffer *buffer, const struct line *l, const char *buf, size_t len, int cont)
{
	struct vline *vl;

	buffer->line_max++;

	if ((vl = calloc(1, sizeof(*vl))) == NULL)
		return 0;

	if (len != 0 && (vl->line = calloc(1, len+1)) == NULL) {
		free(vl);
		return 0;
	}

	vl->parent = l;
	if (len != 0)
		memcpy(vl->line, buf, len);
	vl->flags = cont;

	if (TAILQ_EMPTY(&buffer->head))
		TAILQ_INSERT_HEAD(&buffer->head, vl, vlines);
	else
		TAILQ_INSERT_TAIL(&buffer->head, vl, vlines);
	return 1;
}

/*
 * Build a list of visual line by wrapping the given line, assuming
 * that when printed will have a leading prefix prfx.
 */
int
wrap_text(struct buffer *buffer, const char *prfx, struct line *l, size_t width)
{
	const char	*separators = " \t-";
	const char	*start, *end, *line, *lastsep, *lastchar;
	uint32_t	 cp = 0, state = 0;
	size_t		 cur, prfxwidth, w;
	int		 cont;

	if ((line = l->line) == NULL)
		return push_line(buffer, l, NULL, 0, 0);

	prfxwidth = utf8_swidth(prfx);
	cur = prfxwidth;
	start = line;
	lastsep = NULL;
	lastchar = line;
	cont = 0;
	for (; *line; line++) {
		if (utf8_decode(&state, &cp, *line))
			continue;
		w = utf8_chwidth(cp);
		if (cur + w >= width -1) {
			end = lastsep == NULL
				? utf8_next_cp((char*)lastchar)
				: utf8_next_cp((char*)lastsep);
			if (!push_line(buffer, l, start, end - start, cont))
				return 0;
			cont = 1;
			start = end;
			cur = prfxwidth + utf8_swidth_between(start, lastchar);
		} else {
			if (strchr(separators, *line) != NULL)
				lastsep = line;
		}

		lastchar = utf8_prev_cp(line, l->line);
		cur += w;
	}

	return push_line(buffer, l, start, line - start, cont);
}

int
hardwrap_text(struct buffer *buffer, struct line *l, size_t width)
{
	const char	*line, *start, *lastchar;
	int		 cont;
	uint32_t	 state = 0, cp = 0;
	size_t		 cur, w;

	if ((line = l->line) == NULL)
		return push_line(buffer, l, NULL, 0, 0);

	start = line;
	lastchar = line;
	cont = 0;
	cur = 0;
	for (; *line; line++) {
		if (utf8_decode(&state, &cp, *line))
			continue;
		w = utf8_chwidth(cp);
		if (cur + w >= width) {
			if (!push_line(buffer, l, start, lastchar - start, cont))
				return 0;
			cont = 1;
			cur = 0;
			start = lastchar;
		}

		lastchar = utf8_prev_cp(line, l->line);
		cur += w;
	}

	return push_line(buffer, l, start, line - start, cont);
}