gds_text.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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->Width - Width - 1;
  85. else if (Pos == GDS_TEXT_CENTER) X = (Device->Width - 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->Width; 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->Width;
  99. }
  100. /****************************************************************************************
  101. * Try to align string for better scrolling visual. there is probably much better to do
  102. */
  103. int GDS_TextStretch(struct GDS_Device* Device, int N, char *String, int Max) {
  104. char Space[] = " ";
  105. int Len = strlen(String), Extra = 0, Boundary;
  106. N--;
  107. // we might already fit
  108. GDS_SetFont( Device, Device->Lines[N].Font );
  109. if (GDS_FontMeasureString( Device, String ) <= Device->Width) return 0;
  110. // add some space for better visual
  111. strncat(String, Space, Max-Len);
  112. String[Max] = '\0';
  113. Len = strlen(String);
  114. // mark the end of the extended string
  115. Boundary = GDS_FontMeasureString( Device, String );
  116. // add a full display width
  117. while (Len < Max && GDS_FontMeasureString( Device, String ) - Boundary < Device->Width) {
  118. String[Len++] = String[Extra++];
  119. String[Len] = '\0';
  120. }
  121. return Boundary;
  122. }
  123. /****************************************************************************************
  124. *
  125. */
  126. void GDS_TextPos(struct GDS_Device* Device, int FontType, int Where, int Attr, char *Text, ...) {
  127. va_list args;
  128. TextAnchor Anchor = TextAnchor_Center;
  129. if (Attr & GDS_TEXT_CLEAR) GDS_Clear( Device, GDS_COLOR_BLACK );
  130. if (!Text) return;
  131. va_start(args, Text);
  132. switch(Where) {
  133. case GDS_TEXT_TOP_LEFT:
  134. default:
  135. Anchor = TextAnchor_NorthWest;
  136. break;
  137. case GDS_TEXT_MIDDLE_LEFT:
  138. Anchor = TextAnchor_West;
  139. break;
  140. case GDS_TEXT_BOTTOM_LEFT:
  141. Anchor = TextAnchor_SouthWest;
  142. break;
  143. case GDS_TEXT_CENTERED:
  144. Anchor = TextAnchor_Center;
  145. break;
  146. }
  147. ESP_LOGD(TAG, "Displaying %s at %u with attribute %u", Text, Anchor, Attr);
  148. GDS_SetFont( Device, GuessFont( Device, FontType ) );
  149. GDS_FontDrawAnchoredString( Device, Anchor, Text, GDS_COLOR_WHITE );
  150. Device->Dirty = true;
  151. if (Attr & GDS_TEXT_UPDATE) GDS_Update( Device );
  152. va_end(args);
  153. }