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 linew = 0;
48 const char *last_word_delim = start;
49 // not reached end of content
50 while (start + len < end
51 // line is not full
52 && linew <= PIXEL_PER_LINE
53 // avoid display buffer overflow for each line
54 // && len < sizeof(G_ux.string_buffer)-1
55 ) {
56 // compute new line length
57#ifdef HAVE_FONTS
58 linew = bagl_compute_line_width(font, 0, start, len + 1, BAGL_ENCODING_DEFAULT);
59#else // HAVE_FONTS
60 linew = se_compute_line_width_light(start, len + 1, G_ux.layout_paging.format);
61#endif // HAVE_FONTS
62 // if (start[len] )
63 if (linew > PIXEL_PER_LINE) {
64 // we got a full line
65 break;
66 }
67 unsigned char c = start[len];
68 if (is_word_delim(c)) {
69 last_word_delim = &start[len];
70 }
71 len++;
72 // new line, don't go further
73 if (c == '\n') {
74 break;
75 }
76 }
77
78 // if not splitting line onto a word delimiter, then cut at the previous word_delim, adjust
79 // len accordingly (and a wor delim has been found already)
80 if (start + len < end && last_word_delim != start && len) {
81 // if line split within a word
82 if ((!is_word_delim(start[len - 1]) && !is_word_delim(start[len]))) {
83 len = last_word_delim - start;
84 }
85 }
86
87 // fill up the paging structure
88 if (page_to_display != (unsigned int) -1 && page_to_display == page
89 && page_to_display < paging_state->count) {
90 paging_state->offsets[line] = start - start2;
91 paging_state->lengths[line] = len;
92
93 // won't compute all pages, we reached the one to display
94#if UX_LAYOUT_PAGING_LINE_COUNT > 1
95 if (line >= UX_LAYOUT_PAGING_LINE_COUNT - 1)
96#endif // UX_LAYOUT_PAGING_LINE_COUNT
97 {
98 // a page has been computed
99 return 1;
100 }
101 }
102
103 // prepare for next line
104 start += len;
105
106 // skip to next line/page
107 line++;
108 if (
109#if UX_LAYOUT_PAGING_LINE_COUNT > 1
110 line >= UX_LAYOUT_PAGING_LINE_COUNT &&
111#endif // UX_LAYOUT_PAGING_LINE_COUNT
112 start < end) {
113 page++;
114 line = 0;
115 }
116 }
117
118 // return total number of page detected
119 return page + 1;
120}
121
122#endif // HAVE_BAGL
123#endif // HAVE_UX_FLOW
#define BAGL_ENCODING_DEFAULT
Definition nbgl_fonts.h:96
#define G_ux
Definition ux_bagl.h:338
#define STRPIC(x)