gds_text.c 6.0 KB

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