aboutsummaryrefslogtreecommitdiffstats
path: root/src/gdisp/mcufont/mf_wordwrap.c
blob: de684ca1d120cf2e6f3190e90ebaa9913fc96e76 (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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*
 * This file is subject to the terms of the GFX License. If a copy of
 * the license was not distributed with this file, you can obtain one at:
 *
 *              http://ugfx.org/license.html
 */

#include "mf_wordwrap.h"

#ifndef MF_NO_COMPILE

/* Returns true if the line can be broken at this character. */
static bool is_wrap_space(uint16_t c)
{
    return c == ' ' || c == '\n' || c == '\t' || c == '\r' || c == '-';
}

#if MF_USE_ADVANCED_WORDWRAP

/* Represents a single word and the whitespace after it. */
struct wordlen_s
{
    int16_t word; /* Length of the word in pixels. */
    int16_t space; /* Length of the whitespace in pixels. */
    uint16_t chars; /* Number of characters in word + space, combined. */
};

/* Take the next word from the string and compute its width.
 * Returns true if the word ends in a linebreak. */
static bool get_wordlen(const struct mf_font_s *font, mf_str *text,
                        struct wordlen_s *result)
{
    mf_char c;
    mf_str prev = *text;

    result->word = 0;
    result->space = 0;
    result->chars = 0;

    c = mf_getchar(text);
    while (c && !is_wrap_space(c))
    {
        result->chars++;
        result->word += mf_character_width(font, c);

		prev = *text;
        c = mf_getchar(text);
    }

    while (c && is_wrap_space(c))
    {
        result->chars++;

        if (c == ' ' || c == '-')
            result->space += mf_character_width(font, c);
        else if (c == '\t')
            result->space += mf_character_width(font, 'm') * MF_TABSIZE;
        else if (c == '\n') {
			/* Special case for newlines, skip the character then break. */
			prev = *text;
            break;
		}

        prev = *text;
        c = mf_getchar(text);
    }

    /* The last loop reads the first character of next word, put it back. */
    if (c)
        *text = prev;

    return (c == '\0' || c == '\n');
}

/* Represents the rendered length for a single line. */
struct linelen_s
{
    mf_str start; /* Start of the text for line. */
    uint16_t chars; /* Total number of characters on the line. */
    int16_t width; /* Total length of all words + whitespace on the line in pixels. */
    bool linebreak; /* True if line ends in a linebreak */
    struct wordlen_s last_word; /* Last word on the line. */
    struct wordlen_s last_word_2; /* Second to last word on the line. */
};

/* Append word onto the line if it fits. If it would overflow, don't add and
 * return false. */
static bool append_word(const struct mf_font_s *font, int16_t width,
                        struct linelen_s *current, mf_str *text)
{
    mf_str tmp = *text;
    struct wordlen_s wordlen;
    bool linebreak;

    linebreak = get_wordlen(font, &tmp, &wordlen);

    if (current->width + wordlen.word <= width)
    {
        *text = tmp;
        current->last_word_2 = current->last_word;
        current->last_word = wordlen;
        current->linebreak = linebreak;
        current->chars += wordlen.chars;
        current->width += wordlen.word + wordlen.space;
        return true;
    }
    else
    {
        return false;
    }
}

/* Append a character to the line if it fits. */
static bool append_char(const struct mf_font_s *font, int16_t width,
                        struct linelen_s *current, mf_str *text)
{
    mf_str tmp = *text;
    mf_char c;
    uint16_t w;

    c = mf_getchar(&tmp);
    w = mf_character_width(font, c);

    if (current->width + w <= width)
    {
        *text = tmp;
        current->chars++;
        current->width += w;
        return true;
    }
    else
    {
        return false;
    }
}

static int32_t sq16(int16_t x) { return (int32_t)x * x; }

/* Try to balance the lines by potentially moving one word from the previous
 * line to the the current one. */
static void tune_lines(struct linelen_s *current, struct linelen_s *previous,
                       int16_t max_width)
{
    int16_t curw1, prevw1;
    int16_t curw2, prevw2;
    int32_t delta1, delta2;

    /* If the lines are rendered as is */
    curw1 = current->width - current->last_word.space;
    prevw1 = previous->width - previous->last_word.space;
    delta1 = sq16(max_width - prevw1) + sq16(max_width - curw1);

    /* If the last word is moved */
    curw2 = current->width + previous->last_word.word;
    prevw2 = previous->width - previous->last_word.word
                             - previous->last_word.space
                             - previous->last_word_2.space;
    delta2 = sq16(max_width - prevw2) + sq16(max_width - curw2);

    if (delta1 > delta2 && curw2 <= max_width)
    {
        /* Do the change. */
        uint16_t chars;

        chars = previous->last_word.chars;
        previous->chars -= chars;
        current->chars += chars;
        previous->width -= previous->last_word.word + previous->last_word.space;
        current->width += previous->last_word.word + previous->last_word.space;
        previous->last_word = previous->last_word_2;

        while (chars--) mf_rewind(&current->start);
    }
}

void mf_wordwrap(const struct mf_font_s *font, int16_t width,
                 mf_str text, mf_line_callback_t callback, void *state)
{
    struct linelen_s current = { 0 };
    struct linelen_s previous = { 0 };
    bool full;
    uint32_t giveUp = 2048;     /* Do while-loop a maximum of x times */

    current.start = text;

    while (*text)
    {
        full = !append_word(font, width, &current, &text);

        if (full || current.linebreak)
        {
            if (!current.chars)
            {
                /* We have a very long word. We must just cut it off at some
                 * point. */
                while (append_char(font, width, &current, &text));
            }

            if (previous.chars)
            {
                /* Tune the length and dispatch the previous line. */
                if (!previous.linebreak && !current.linebreak)
                    tune_lines(&current, &previous, width);

                if (!callback(previous.start, previous.chars, state))
                    return;
            }

            previous = current;
            current.start = text;
            current.chars = 0;
            current.width = 0;
            current.linebreak = false;
            current.last_word.word = 0;
            current.last_word.space = 0;
            current.last_word.chars = 0;
        }
        
        giveUp--;
        if (giveUp == 0)
        {
            break;
        }
    }

    /* Dispatch the last lines. */
    if (previous.chars)
    {
        if (!callback(previous.start, previous.chars, state))
            return;
    }

    if (current.chars)
        callback(current.start, current.chars, state);
}

#else

void mf_wordwrap(const struct mf_font_s *font, int16_t width,
                 mf_str text, mf_line_callback_t callback, void *state)
{
    mf_str linestart;

    /* Current line width and character count */
    int16_t lw_cur = 0, cc_cur = 0;

    /* Previous wrap point */
    int16_t cc_prev;
    mf_str ls_prev;

    linestart = text;

    while (*text)
    {
        cc_prev = 0;
        ls_prev = text;

        while (*text)
        {
            mf_char c;
            int16_t new_width;
            mf_str tmp;

            tmp = text;
            c = mf_getchar(&text);
            new_width = lw_cur + mf_character_width(font, c);

            if (c == '\n')
            {
                cc_prev = cc_cur + 1;
                ls_prev = text;
                break;
            }

            if (new_width > width)
            {
                text = tmp;
                break;
            }

            cc_cur++;
            lw_cur = new_width;

            if (is_wrap_space(c))
            {
                cc_prev = cc_cur;
                ls_prev = text;
            }
        }

        /* Handle unbreakable words */
        if (cc_prev == 0)
        {
            cc_prev = cc_cur;
            ls_prev = text;
        }

        if (!callback(linestart, cc_prev, state))
            return;

        linestart = ls_prev;
        text = linestart;
        lw_cur = 0;
        cc_cur = 0;
    }
}

#endif

#endif //MF_NO_COMPILE