components/u8g2/csrc/u8x8_string.c

changeset 0
88d965579617
equal deleted inserted replaced
-1:000000000000 0:88d965579617
1 /*
2
3 u8x8_string.c
4
5 string line procedures
6
7 Universal 8bit Graphics Library (https://github.com/olikraus/u8g2/)
8
9 Copyright (c) 2016, olikraus@gmail.com
10 All rights reserved.
11
12 Redistribution and use in source and binary forms, with or without modification,
13 are permitted provided that the following conditions are met:
14
15 * Redistributions of source code must retain the above copyright notice, this list
16 of conditions and the following disclaimer.
17
18 * Redistributions in binary form must reproduce the above copyright notice, this
19 list of conditions and the following disclaimer in the documentation and/or other
20 materials provided with the distribution.
21
22 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
23 CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
24 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
27 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
32 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
34 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35
36 */
37
38 #include "u8x8.h"
39
40 uint8_t u8x8_GetStringLineCnt(const char *str)
41 {
42 char e;
43 uint8_t line_cnt = 1;
44 if ( str == NULL )
45 return 0;
46 for(;;)
47 {
48 e = *str;
49 if ( e == '\0' )
50 break;
51 str++;
52 if ( e == '\n' )
53 line_cnt++;
54 }
55 return line_cnt;
56 }
57
58
59 /*
60 Assumes strings, separated by '\n' in "str".
61 Returns the string at index "line_idx". First strng has line_idx = 0
62 Example:
63 Returns "xyz" for line_idx = 1 with str = "abc\nxyz"
64 Support both UTF8 and normal strings.
65 */
66 const char *u8x8_GetStringLineStart(uint8_t line_idx, const char *str )
67 {
68 char e;
69 uint8_t line_cnt = 1;
70
71 if ( line_idx == 0 )
72 return str;
73
74 for(;;)
75 {
76 e = *str;
77 if ( e == '\0' )
78 break;
79 str++;
80 if ( e == '\n' )
81 {
82 if ( line_cnt == line_idx )
83 return str;
84 line_cnt++;
85 }
86 }
87 return NULL; /* line not found */
88 }
89
90 /* copy until first '\n' or '\0' in str */
91 /* Important: There is no string overflow check, ensure */
92 /* that the destination buffer is large enough */
93 void u8x8_CopyStringLine(char *dest, uint8_t line_idx, const char *str)
94 {
95 if ( dest == NULL )
96 return;
97 str = u8x8_GetStringLineStart( line_idx, str );
98 if ( str != NULL )
99 {
100 for(;;)
101 {
102 if ( *str == '\n' || *str == '\0' )
103 break;
104 *dest = *str;
105 dest++;
106 str++;
107 }
108 }
109 *dest = '\0';
110 }
111
112 /*
113 Draw a string
114 Extend the string to size "w"
115 Center the string within "w"
116 return the size of the string
117
118 */
119 uint8_t u8x8_DrawUTF8Line(u8x8_t *u8x8, uint8_t x, uint8_t y, uint8_t w, const char *s)
120 {
121 uint8_t d, lw;
122 uint8_t cx, dx;
123
124 d = 0;
125
126 lw = u8x8_GetUTF8Len(u8x8, s);
127 if ( lw < w )
128 {
129 d = w;
130 d -=lw;
131 d /= 2;
132 }
133
134 cx = x;
135 dx = cx + d;
136 while( cx < dx )
137 {
138 u8x8_DrawUTF8(u8x8, cx, y, " ");
139 cx++;
140 }
141 cx += u8x8_DrawUTF8(u8x8, cx, y, s);
142 dx = x + w;
143 while( cx < dx )
144 {
145 u8x8_DrawUTF8(u8x8, cx, y, " ");
146 cx++;
147 }
148 cx -= x;
149 return cx;
150 }
151
152 /*
153 draw several lines at position x,y.
154 lines are stored in s and must be separated with '\n'.
155 lines can be centered with respect to "w"
156 if s == NULL nothing is drawn and 0 is returned
157 returns the number of lines in s
158 */
159 uint8_t u8x8_DrawUTF8Lines(u8x8_t *u8x8, uint8_t x, uint8_t y, uint8_t w, const char *s)
160 {
161 uint8_t i;
162 uint8_t cnt;
163 cnt = u8x8_GetStringLineCnt(s);
164 for( i = 0; i < cnt; i++ )
165 {
166 u8x8_DrawUTF8Line(u8x8, x, y, w, u8x8_GetStringLineStart(i, s));
167 y++;
168 }
169 return cnt;
170 }

mercurial