gds_text.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * (c) Philippe G. 2019, philippe_44@outlook.com
  3. *
  4. * This software is released under the MIT License.
  5. * https://opensource.org/licenses/MIT
  6. *
  7. */
  8. #include <string.h>
  9. #include <ctype.h>
  10. #include <stdint.h>
  11. #include <arpa/inet.h>
  12. #include "esp_log.h"
  13. #include "gds_private.h"
  14. #include "gds.h"
  15. #include "gds_draw.h"
  16. #include "gds_text.h"
  17. #define max(a,b) (((a) > (b)) ? (a) : (b))
  18. static char TAG[] = "gds";
  19. /****************************************************************************************
  20. * Set fonts for each line in text mode
  21. */
  22. static const struct GDS_FontDef *GuessFont( struct GDS_Device *Device, int FontType) {
  23. switch(FontType) {
  24. case GDS_FONT_LINE_1:
  25. return &Font_line_1;
  26. case GDS_FONT_LINE_2:
  27. return &Font_line_2;
  28. case GDS_FONT_SMALL:
  29. return &Font_droid_sans_fallback_11x13;
  30. case GDS_FONT_MEDIUM:
  31. default:
  32. return &Font_droid_sans_fallback_15x17;
  33. #ifdef USE_LARGE_FONTS
  34. case GDS_FONT_LARGE:
  35. return &Font_droid_sans_fallback_24x28;
  36. break;
  37. case GDS_FONT_SEGMENT:
  38. if (Device->Height == 32) return &Font_Tarable7Seg_16x32;
  39. else return &Font_Tarable7Seg_32x64;
  40. #else
  41. case GDS_FONT_LARGE:
  42. case GDS_FONT_SEGMENT:
  43. ESP_LOGW(TAG, "large fonts disabled");
  44. return &Font_droid_sans_fallback_15x17;
  45. break;
  46. #endif
  47. }
  48. }
  49. /****************************************************************************************
  50. * Set fonts for each line in text mode
  51. */
  52. bool GDS_TextSetFontAuto(struct GDS_Device* Device, int N, int FontType, int Space) {
  53. const struct GDS_FontDef *Font = GuessFont( Device, FontType );
  54. return GDS_TextSetFont( Device, N, Font, Space );
  55. }
  56. /****************************************************************************************
  57. * Set fonts for each line in text mode
  58. */
  59. bool GDS_TextSetFont(struct GDS_Device* Device, int N, const struct GDS_FontDef *Font, int Space) {
  60. if (--N >= MAX_LINES) return false;
  61. Device->Lines[N].Font = Font;
  62. // re-calculate lines absolute position
  63. Device->Lines[N].Space = Space;
  64. Device->Lines[0].Y = Device->Lines[0].Space;
  65. for (int i = 1; i <= N; i++) Device->Lines[i].Y = Device->Lines[i-1].Y + Device->Lines[i-1].Font->Height + Device->Lines[i].Space;
  66. ESP_LOGI(TAG, "Adding line %u at %d (height:%u)", N + 1, Device->Lines[N].Y, Device->Lines[N].Font->Height);
  67. if (Device->Lines[N].Y + Device->Lines[N].Font->Height > Device->Height) {
  68. ESP_LOGW(TAG, "line does not fit display");
  69. return false;
  70. }
  71. return true;
  72. }
  73. /****************************************************************************************
  74. *
  75. */
  76. bool GDS_TextLine(struct GDS_Device* Device, int N, int Pos, int Attr, char *Text) {
  77. int Width, X = Pos;
  78. // counting 1..n
  79. N--;
  80. GDS_SetFont( Device, Device->Lines[N].Font );
  81. if (Attr & GDS_TEXT_MONOSPACE) GDS_FontForceMonospace( Device, true );
  82. Width = GDS_FontMeasureString( Device, Text );
  83. // adjusting position, erase only EoL for rigth-justified
  84. if (Pos == GDS_TEXT_RIGHT) X = Device->TextWidth - Width - 1;
  85. else if (Pos == GDS_TEXT_CENTER) X = (Device->TextWidth - Width) / 2;
  86. // erase if requested
  87. if (Attr & GDS_TEXT_CLEAR) {
  88. int Y_min = max(0, Device->Lines[N].Y), Y_max = max(0, Device->Lines[N].Y + Device->Lines[N].Font->Height);
  89. for (int c = (Attr & GDS_TEXT_CLEAR_EOL) ? X : 0; c < Device->TextWidth; c++)
  90. for (int y = Y_min; y < Y_max; y++)
  91. DrawPixelFast( Device, c, y, GDS_COLOR_BLACK );
  92. }
  93. GDS_FontDrawString( Device, X, Device->Lines[N].Y, Text, GDS_COLOR_WHITE );
  94. ESP_LOGD(TAG, "displaying %s line %u (x:%d, attr:%u)", Text, N+1, X, Attr);
  95. // update whole display if requested
  96. Device->Dirty = true;
  97. if (Attr & GDS_TEXT_UPDATE) GDS_Update( Device );
  98. return Width + X < Device->TextWidth;
  99. }
  100. /****************************************************************************************
  101. *
  102. */
  103. int GDS_GetTextWidth(struct GDS_Device* Device, int N, int Attr, char *Text) {
  104. const struct GDS_FontDef *Font = GDS_SetFont( Device, Device->Lines[N-1].Font );
  105. if (Attr & GDS_TEXT_MONOSPACE) GDS_FontForceMonospace( Device, true );
  106. int Width = GDS_FontMeasureString( Device, Text );
  107. GDS_SetFont( Device, Font );
  108. return Width;
  109. }
  110. /****************************************************************************************
  111. * Try to align string for better scrolling visual. there is probably much better to do
  112. */
  113. int GDS_TextStretch(struct GDS_Device* Device, int N, char *String, int Max) {
  114. char Space[] = " ";
  115. int Len = strlen(String), Extra = 0, Boundary;
  116. N--;
  117. // we might already fit
  118. GDS_SetFont( Device, Device->Lines[N].Font );
  119. if (GDS_FontMeasureString( Device, String ) <= Device->TextWidth) return 0;
  120. // add some space for better visual
  121. strncat(String, Space, Max-Len);
  122. String[Max] = '\0';
  123. Len = strlen(String);
  124. // mark the end of the extended string
  125. Boundary = GDS_FontMeasureString( Device, String );
  126. // add a full display width
  127. while (Len < Max && GDS_FontMeasureString( Device, String ) - Boundary < Device->TextWidth) {
  128. String[Len++] = String[Extra++];
  129. String[Len] = '\0';
  130. }
  131. return Boundary;
  132. }
  133. /****************************************************************************************
  134. *
  135. */
  136. void GDS_TextPos(struct GDS_Device* Device, int FontType, int Where, int Attr, char *Text, ...) {
  137. va_list args;
  138. TextAnchor Anchor = TextAnchor_Center;
  139. if (Attr & GDS_TEXT_CLEAR) GDS_Clear( Device, GDS_COLOR_BLACK );
  140. if (!Text) return;
  141. va_start(args, Text);
  142. switch(Where) {
  143. case GDS_TEXT_TOP_LEFT:
  144. default:
  145. Anchor = TextAnchor_NorthWest;
  146. break;
  147. case GDS_TEXT_MIDDLE_LEFT:
  148. Anchor = TextAnchor_West;
  149. break;
  150. case GDS_TEXT_BOTTOM_LEFT:
  151. Anchor = TextAnchor_SouthWest;
  152. break;
  153. case GDS_TEXT_CENTERED:
  154. Anchor = TextAnchor_Center;
  155. break;
  156. }
  157. ESP_LOGD(TAG, "Displaying %s at %u with attribute %u", Text, Anchor, Attr);
  158. GDS_SetFont( Device, GuessFont( Device, FontType ) );
  159. GDS_FontDrawAnchoredString( Device, Anchor, Text, GDS_COLOR_WHITE );
  160. Device->Dirty = true;
  161. if (Attr & GDS_TEXT_UPDATE) GDS_Update( Device );
  162. va_end(args);
  163. }