Embedded SDK
Embedded SDK
Loading...
Searching...
No Matches
ux_layout_paging_compute.c
Go to the documentation of this file.
1#include "os_helpers.h"
2#include "os_pic.h"
3#include "ux.h"
4#include "ux_layouts.h"
5#include "bagl.h"
6#include "string.h"
7#include "os.h"
8
9#ifdef HAVE_UX_FLOW
10#ifdef HAVE_BAGL
11
12static bool is_word_delim(unsigned char c)
13{
14 // return !((c >= 'a' && c <= 'z')
15 // || (c >= 'A' && c <= 'Z')
16 // || (c >= '0' && c <= '9'));
17 return c == ' ' || c == '\n' || c == '-' || c == '_';
18}
19
20// return the number of pages to be displayed when current page to show is -1
21unsigned int ux_layout_paging_compute(const char *text_to_split,
22 unsigned int page_to_display,
23 ux_layout_paging_state_t *paging_state,
24 bagl_font_id_e font)
25{
26#ifndef HAVE_FONTS
27 UNUSED(font);
28#endif
29
30 // reset length and offset of lines
31 memset(paging_state->offsets, 0, sizeof(paging_state->offsets));
32 memset(paging_state->lengths, 0, sizeof(paging_state->lengths));
33
34 // a page has been asked, but no page exists
35 if (page_to_display >= paging_state->count && page_to_display != (unsigned int) -1) {
36 return 0;
37 }
38
39 // compute offset/length of text of each line for the current page
40 unsigned int page = 0;
41 unsigned int line = 0;
42 const char *start = (text_to_split ? STRPIC(text_to_split) : G_ux.externalText);
43 const char *start2 = start;
44 const char *end = start + strlen(start);
45 while (start < end) {
46 unsigned int len = 0;
47 unsigned int last_len_ok = 0;
48 unsigned int linew = 0;
49 unsigned int prev_linew = 0;
50 const char *last_word_delim = start;
51 // not reached end of content
52 while (start + len < end) {
53 // compute new line length
54#ifdef HAVE_FONTS
55 linew = bagl_compute_line_width(font, 0, start, len + 1, BAGL_ENCODING_DEFAULT);
56#else // HAVE_FONTS
57 linew = se_compute_line_width_light(start, len + 1, G_ux.layout_paging.format);
58#endif // HAVE_FONTS
59 if (linew != prev_linew) {
60 prev_linew = linew;
61 last_len_ok = len;
62 }
63
64 if (linew > PIXEL_PER_LINE) {
65 // we got a full line
66 break;
67 }
68 if (len >= (sizeof(G_ux.string_buffer) - 1)) {
69 // display buffer overflow
70 if (last_len_ok) {
71 len = last_len_ok;
72 }
73 // don't split an UTF-8 sequence
74 while (len > 0 && ((unsigned char) start[len] & 0xC0) == 0x80) {
75 len--;
76 }
77 if (len == 0) {
78 // keep the first character whole to guarantee forward progress
79 len = 1;
80 while (start + len < end && len < (sizeof(G_ux.string_buffer) - 1)
81 && ((unsigned char) start[len] & 0xC0) == 0x80) {
82 len++;
83 }
84 }
85 break;
86 }
87
88 unsigned char c = start[len];
89 if (is_word_delim(c)) {
90 last_word_delim = &start[len];
91 }
92 len++;
93 // new line, don't go further
94 if (c == '\n') {
95 break;
96 }
97 }
98
99 // if not splitting line onto a word delimiter, then cut at the previous word_delim, adjust
100 // len accordingly (and a wor delim has been found already)
101 if (start + len < end && last_word_delim != start && len) {
102 // if line split within a word
103 if ((!is_word_delim(start[len - 1]) && !is_word_delim(start[len]))) {
104 len = last_word_delim - start;
105 }
106 }
107
108 // fill up the paging structure
109 if (page_to_display != (unsigned int) -1 && page_to_display == page
110 && page_to_display < paging_state->count) {
111 paging_state->offsets[line] = start - start2;
112 paging_state->lengths[line] = len;
113
114 // won't compute all pages, we reached the one to display
115#if UX_LAYOUT_PAGING_LINE_COUNT > 1
116 if (line >= UX_LAYOUT_PAGING_LINE_COUNT - 1)
117#endif // UX_LAYOUT_PAGING_LINE_COUNT
118 {
119 // a page has been computed
120 return 1;
121 }
122 }
123
124 // prepare for next line
125 start += len;
126
127 // skip to next line/page
128 line++;
129 if (
130#if UX_LAYOUT_PAGING_LINE_COUNT > 1
131 line >= UX_LAYOUT_PAGING_LINE_COUNT &&
132#endif // UX_LAYOUT_PAGING_LINE_COUNT
133 start < end) {
134 page++;
135 line = 0;
136 }
137 }
138
139 // return total number of page detected
140 return page + 1;
141}
142
143#endif // HAVE_BAGL
144#endif // HAVE_UX_FLOW
ux_state_t G_ux
Global UX state instance — see ux_state_s.
Definition main.c:34
#define BAGL_ENCODING_DEFAULT
Definition nbgl_fonts.h:108
#define STRPIC(x)