u8x8_string.c 3.9 KB

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