@@ -29,31 +29,31 @@ namespace FTDI {
29
29
* be broken so that the display width is less than w. The line will also
30
30
* be broken after a '\n'. Returns the display width of the line.
31
31
*/
32
- static uint16_t find_line_break (const FontMetrics &utf8_fm, const CLCD::FontMetrics &clcd_fm, const uint16_t w, const char *str , const char *&end, bool use_utf8) {
33
- const char *p = str ;
34
- end = str ;
32
+ static uint16_t find_line_break (const FontMetrics &utf8_fm, const CLCD::FontMetrics &clcd_fm, const uint16_t w, const char *start , const char *&end, bool use_utf8) {
33
+ const char *p = start ;
34
+ end = start ;
35
35
uint16_t lw = 0 , result = 0 ;
36
36
for (;;) {
37
37
const char *next = p;
38
- utf8_char_t c = get_utf8_char_and_inc (next);
38
+ const utf8_char_t c = get_utf8_char_and_inc (next);
39
39
// Decide whether to break the string at this location
40
40
if (c == ' \n ' || c == ' \0 ' || c == ' ' ) {
41
41
end = p;
42
42
result = lw;
43
43
}
44
44
if (c == ' \n ' || c == ' \0 ' ) break ;
45
- // Now add the length of the current character to the tally.
46
- lw + = use_utf8 ? utf8_fm.get_char_width (c) : clcd_fm.char_widths [(uint8_t )c];
45
+ // Measure the next character
46
+ const uint16_t cw = use_utf8 ? utf8_fm.get_char_width (c) : clcd_fm.char_widths [(uint8_t )c];
47
47
// Stop processing once string exceeds the display width
48
- if (lw >= w) {
49
- if (end == str) {
50
- end = p;
51
- result = lw;
52
- }
53
- break ;
54
- }
48
+ if (lw + cw > w) break ;
49
+ // Now add the length of the current character to the tally.
50
+ lw += cw;
55
51
p = next;
56
52
}
53
+ if (end == start) {
54
+ end = p;
55
+ result = lw;
56
+ }
57
57
return result;
58
58
}
59
59
@@ -66,12 +66,13 @@ namespace FTDI {
66
66
const uint16_t wrap_width = width;
67
67
width = height = 0 ;
68
68
for (;;) {
69
- uint16_t line_width = find_line_break (utf8_fm, clcd_fm, wrap_width, line_start, line_end, use_utf8);
69
+ const uint16_t line_width = find_line_break (utf8_fm, clcd_fm, wrap_width, line_start, line_end, use_utf8);
70
+ if (line_end == line_start) break ;
70
71
width = max (width, line_width);
71
72
height += utf8_fm.get_height ();
72
73
line_start = line_end;
73
- if (line_start[ 0 ] == ' \n ' || line_start[ 0 ] == ' ' ) line_start++;
74
- if (line_start[ 0 ] == ' \0 ' ) break ;
74
+ if (* line_start == ' \n ' || * line_start == ' ' ) line_start++;
75
+ if (* line_start == ' \0 ' ) break ;
75
76
}
76
77
}
77
78
@@ -108,28 +109,25 @@ namespace FTDI {
108
109
const char *line_start = str, *line_end;
109
110
for (;;) {
110
111
find_line_break (utf8_fm, clcd_fm, w, line_start, line_end, use_utf8);
112
+ if (line_end == line_start) break ;
111
113
112
114
const size_t line_len = line_end - line_start;
113
115
if (line_len) {
114
- char line[line_len + 1 ];
115
- strncpy (line, line_start, line_len);
116
- line[line_len] = 0 ;
117
-
118
116
#if ENABLED(TOUCH_UI_USE_UTF8)
119
- if (use_utf8) {
120
- draw_utf8_text (cmd, x + dx, y + dy, line , utf8_fm.fs , options & ~(OPT_CENTERY | OPT_BOTTOMY));
121
- } else
117
+ if (use_utf8)
118
+ draw_utf8_text (cmd, x + dx, y + dy, line_start , utf8_fm.fs , options & ~(OPT_CENTERY | OPT_BOTTOMY), line_len );
119
+ else
122
120
#endif
123
121
{
124
122
cmd.CLCD ::CommandFifo::text (x + dx, y + dy, font, options & ~(OPT_CENTERY | OPT_BOTTOMY));
125
- cmd.CLCD ::CommandFifo::str (line );
123
+ cmd.CLCD ::CommandFifo::str (line_start, line_len );
126
124
}
127
125
}
128
126
y += utf8_fm.get_height ();
129
127
130
128
line_start = line_end;
131
- if (line_start[ 0 ] == ' \n ' || line_start[ 0 ] == ' ' ) line_start++;
132
- if (line_start[ 0 ] == ' \0 ' ) break ;
129
+ if (* line_start == ' \n ' || * line_start == ' ' ) line_start++;
130
+ if (* line_start == ' \0 ' ) break ;
133
131
}
134
132
}
135
133
0 commit comments