gds_font.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /**
  2. * Copyright (c) 2017-2018 Tara Keeling
  3. *
  4. * This software is released under the MIT License.
  5. * https://opensource.org/licenses/MIT
  6. */
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <stdint.h>
  10. #include <math.h>
  11. #include "gds_private.h"
  12. #include "gds.h"
  13. #include "gds_font.h"
  14. #include "gds_draw.h"
  15. #include "gds_err.h"
  16. static int RoundUpFontHeight( const struct GDS_FontDef* Font ) {
  17. int Height = Font->Height;
  18. if ( ( Height % 8 ) != 0 ) {
  19. return ( ( Height + 7 ) / 8 ) * 8;
  20. }
  21. return Height;
  22. }
  23. static const uint8_t* GetCharPtr( const struct GDS_FontDef* Font, char Character ) {
  24. return &Font->FontData[ ( Character - Font->StartChar ) * ( ( Font->Width * ( RoundUpFontHeight( Font ) / 8 ) ) + 1 ) ];
  25. }
  26. void GDS_FontDrawChar( struct GDS_Device* Device, char Character, int x, int y, int Color ) {
  27. const uint8_t* GlyphData = NULL;
  28. int GlyphColumnLen = 0;
  29. int CharStartX = 0;
  30. int CharStartY = 0;
  31. int CharWidth = 0;
  32. int CharHeight = 0;
  33. int CharEndX = 0;
  34. int CharEndY = 0;
  35. int OffsetX = 0;
  36. int OffsetY = 0;
  37. int YByte = 0;
  38. int YBit = 0;
  39. int i = 0;
  40. NullCheck( ( GlyphData = GetCharPtr( Device->Font, Character ) ), return );
  41. if ( Character >= Device->Font->StartChar && Character <= Device->Font->EndChar ) {
  42. /* The first byte in the glyph data is the width of the character in pixels, skip over */
  43. GlyphData++;
  44. GlyphColumnLen = RoundUpFontHeight( Device->Font ) / 8;
  45. CharWidth = GDS_FontGetCharWidth( Device, Character );
  46. CharHeight = GDS_FontGetHeight( Device );
  47. CharStartX = x;
  48. CharStartY = y;
  49. CharEndX = CharStartX + CharWidth;
  50. CharEndY = CharStartY + CharHeight;
  51. /* If the character is partially offscreen offset the end by
  52. * distance between (coord) and 0.
  53. */
  54. OffsetX = ( CharStartX < 0 ) ? abs( CharStartX ) : 0;
  55. OffsetY = ( CharStartY < 0 ) ? abs( CharStartY ) : 0;
  56. /* This skips into the proper column within the glyph data */
  57. GlyphData+= ( OffsetX * GlyphColumnLen );
  58. CharStartX+= OffsetX;
  59. CharStartY+= OffsetY;
  60. /* Do not attempt to draw if this character is entirely offscreen */
  61. if ( CharEndX < 0 || CharStartX >= Device->Width || CharEndY < 0 || CharStartY >= Device->Height ) {
  62. ClipDebug( x, y );
  63. return;
  64. }
  65. /* Do not attempt to draw past the end of the screen */
  66. CharEndX = ( CharEndX >= Device->Width ) ? Device->Width - 1 : CharEndX;
  67. CharEndY = ( CharEndY >= Device->Height ) ? Device->Height - 1 : CharEndY;
  68. Device->Dirty = true;
  69. for ( x = CharStartX; x < CharEndX; x++ ) {
  70. for ( y = CharStartY, i = 0; y < CharEndY && i < CharHeight; y++, i++ ) {
  71. YByte = ( i + OffsetY ) / 8;
  72. YBit = ( i + OffsetY ) & 0x07;
  73. if ( GlyphData[ YByte ] & BIT( YBit ) ) {
  74. DrawPixel( Device, x, y, Color );
  75. }
  76. }
  77. GlyphData+= GlyphColumnLen;
  78. }
  79. }
  80. }
  81. bool GDS_SetFont( struct GDS_Device* Display, const struct GDS_FontDef* Font ) {
  82. Display->FontForceProportional = false;
  83. Display->FontForceMonospace = false;
  84. Display->Font = Font;
  85. return true;
  86. }
  87. void GDS_FontForceProportional( struct GDS_Device* Display, bool Force ) {
  88. Display->FontForceProportional = Force;
  89. }
  90. void GDS_FontForceMonospace( struct GDS_Device* Display, bool Force ) {
  91. Display->FontForceMonospace = Force;
  92. }
  93. int GDS_FontGetWidth( struct GDS_Device* Display ) {
  94. return Display->Font->Width;
  95. }
  96. int GDS_FontGetHeight( struct GDS_Device* Display ) {
  97. return Display->Font->Height;
  98. }
  99. int GDS_FontGetCharWidth( struct GDS_Device* Display, char Character ) {
  100. const uint8_t* CharPtr = NULL;
  101. int Width = 0;
  102. if ( Character >= Display->Font->StartChar && Character <= Display->Font->EndChar ) {
  103. CharPtr = GetCharPtr( Display->Font, Character );
  104. Width = ( Display->Font->Monospace == true ) ? Display->Font->Width : *CharPtr;
  105. if ( Display->FontForceMonospace == true ) {
  106. Width = Display->Font->Width;
  107. }
  108. if ( Display->FontForceProportional == true ) {
  109. Width = *CharPtr;
  110. }
  111. }
  112. return Width;
  113. }
  114. int GDS_FontGetMaxCharsPerRow( struct GDS_Device* Display ) {
  115. return Display->Width / Display->Font->Width;
  116. }
  117. int GDS_FontGetMaxCharsPerColumn( struct GDS_Device* Display ) {
  118. return Display->Height / Display->Font->Height;
  119. }
  120. int GDS_FontGetCharHeight( struct GDS_Device* Display ) {
  121. return Display->Font->Height;
  122. }
  123. int GDS_FontMeasureString( struct GDS_Device* Display, const char* Text ) {
  124. int Width = 0;
  125. int Len = 0;
  126. NullCheck( Text, return 0 );
  127. for ( Len = strlen( Text ); Len >= 0; Len--, Text++ ) {
  128. if ( *Text >= Display->Font->StartChar && *Text <= Display->Font->EndChar ) {
  129. Width+= GDS_FontGetCharWidth( Display, *Text );
  130. }
  131. }
  132. return Width;
  133. }
  134. void GDS_FontDrawString( struct GDS_Device* Display, int x, int y, const char* Text, int Color ) {
  135. int Len = 0;
  136. int i = 0;
  137. NullCheck( Text, return );
  138. for ( Len = strlen( Text ), i = 0; i < Len; i++ ) {
  139. GDS_FontDrawChar( Display, *Text, x, y, Color );
  140. x+= GDS_FontGetCharWidth( Display, *Text );
  141. Text++;
  142. }
  143. }
  144. void GDS_FontDrawAnchoredString( struct GDS_Device* Display, TextAnchor Anchor, const char* Text, int Color ) {
  145. int x = 0;
  146. int y = 0;
  147. NullCheck( Text, return );
  148. GDS_FontGetAnchoredStringCoords( Display, &x, &y, Anchor, Text );
  149. GDS_FontDrawString( Display, x, y, Text, Color );
  150. }
  151. void GDS_FontGetAnchoredStringCoords( struct GDS_Device* Display, int* OutX, int* OutY, TextAnchor Anchor, const char* Text ) {
  152. int StringWidth = 0;
  153. int StringHeight = 0;
  154. NullCheck( OutX, return );
  155. NullCheck( OutY, return );
  156. NullCheck( Text, return );
  157. StringWidth = GDS_FontMeasureString( Display, Text );
  158. StringHeight = GDS_FontGetCharHeight( Display );
  159. switch ( Anchor ) {
  160. case TextAnchor_East: {
  161. *OutY = ( Display->Height / 2 ) - ( StringHeight / 2 );
  162. *OutX = ( Display->Width - StringWidth );
  163. break;
  164. }
  165. case TextAnchor_West: {
  166. *OutY = ( Display->Height / 2 ) - ( StringHeight / 2 );
  167. *OutX = 0;
  168. break;
  169. }
  170. case TextAnchor_North: {
  171. *OutX = ( Display->Width / 2 ) - ( StringWidth / 2 );
  172. *OutY = 0;
  173. break;
  174. }
  175. case TextAnchor_South: {
  176. *OutX = ( Display->Width / 2 ) - ( StringWidth / 2 );
  177. *OutY = ( Display->Height - StringHeight );
  178. break;
  179. }
  180. case TextAnchor_NorthEast: {
  181. *OutX = ( Display->Width - StringWidth );
  182. *OutY = 0;
  183. break;
  184. }
  185. case TextAnchor_NorthWest: {
  186. *OutY = 0;
  187. *OutX = 0;
  188. break;
  189. }
  190. case TextAnchor_SouthEast: {
  191. *OutY = ( Display->Height - StringHeight );
  192. *OutX = ( Display->Width - StringWidth );
  193. break;
  194. }
  195. case TextAnchor_SouthWest: {
  196. *OutY = ( Display->Height - StringHeight );
  197. *OutX = 0;
  198. break;
  199. }
  200. case TextAnchor_Center: {
  201. *OutY = ( Display->Height / 2 ) - ( StringHeight / 2 );
  202. *OutX = ( Display->Width / 2 ) - ( StringWidth / 2 );
  203. break;
  204. }
  205. default: {
  206. *OutX = 128;
  207. *OutY = 64;
  208. break;
  209. }
  210. };
  211. }