FONT.CPP 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. ** Command & Conquer Red Alert(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /****************************************************************************
  19. *
  20. * C O N F I D E N T I A L -- W E S T W O O D S T U D I O S
  21. *
  22. *---------------------------------------------------------------------------
  23. *
  24. * FILE
  25. * font.c
  26. *
  27. * DESCRIPTION
  28. * Font manipulation.
  29. *
  30. * PROGRAMMER
  31. * Denzil E. Long, Jr.
  32. *
  33. * DATE
  34. * March 9, 1995
  35. *
  36. *---------------------------------------------------------------------------
  37. *
  38. * PUBLIC
  39. * Load_Font - Open a font for use.
  40. * Set_Font - Set the default system font.
  41. * String_Pixel_Width - Get the pixel width of a string.
  42. *
  43. ****************************************************************************/
  44. #include <stdlib.h>
  45. #include <fcntl.h>
  46. #include <io.h>
  47. #include <string.h>
  48. #include <malloc.h>
  49. #include "font.h"
  50. /*---------------------------------------------------------------------------
  51. * PRIVATE DECLARATIONS
  52. *-------------------------------------------------------------------------*/
  53. /* min and max macros */
  54. #ifdef __cplusplus
  55. #ifndef max
  56. #define max(a,b) (((a) > (b)) ? (a) : (b))
  57. #endif
  58. #ifndef min
  59. #define min(a,b) (((a) < (b)) ? (a) : (b))
  60. #endif
  61. #endif
  62. void const *FontPtr = NULL;
  63. char FontHeight = 8;
  64. char FontWidth = 8;
  65. int FontXSpacing = 0;
  66. int FontYSpacing = 0;
  67. char *FontWidthBlockPtr = NULL;
  68. /****************************************************************************
  69. *
  70. * NAME
  71. * Load_Font - Open a font for use.
  72. *
  73. * SYNOPSIS
  74. * Font = Load_Font(Name)
  75. *
  76. * char *Load_Font(char *);
  77. *
  78. * FUNCTION
  79. * Open a graphics font for use by Text_Print(). Use free() to dispose
  80. * of the font.
  81. *
  82. * INPUTS
  83. * Name - Name of font file to open.
  84. *
  85. * RESULT
  86. * Font - Pointer to font, NULL if error.
  87. *
  88. ****************************************************************************/
  89. void *cdecl Load_Font(char const *name)
  90. {
  91. Font *font = NULL;
  92. long fh;
  93. short size;
  94. short valid;
  95. /* Open the font. */
  96. if ((fh = open(name, (O_RDONLY|O_BINARY))) != -1) {
  97. /* Get the size of the font. */
  98. if (read(fh, &size, 2) == 2) {
  99. /* Allocate memory to contain the font. */
  100. if ((font = (Font *)malloc((unsigned long)size)) != NULL) {
  101. valid = 0;
  102. /* Read in the body of the font. */
  103. if (read(fh, &font->CompMethod, (unsigned long)(size - 2))
  104. == (unsigned long)(size - 2)) {
  105. /* Verify the validity of the font. */
  106. if ((font->CompMethod == 0) && (font->NumBlks == 5)) {
  107. font->Size = size;
  108. valid = 1;
  109. }
  110. }
  111. /* Free the font if it is not valid. */
  112. if (valid == 0) {
  113. free(font);
  114. font = NULL;
  115. }
  116. }
  117. }
  118. /* Close the font. */
  119. close(fh);
  120. }
  121. return ((char *)font);
  122. }
  123. /****************************************************************************
  124. *
  125. * NAME
  126. * Set_Font - Set the default system font.
  127. *
  128. * SYNOPSIS
  129. * OldFont = Set_Font(Font)
  130. *
  131. * char *Set_Font(char *);
  132. *
  133. * FUNCTION
  134. * Sets up the specified font as the default font used by the system.
  135. *
  136. * INPUTS
  137. * Font - Pointer to Font to set as default. (NULL returns current font)
  138. *
  139. * RESULT
  140. * OldFont - Previous font.
  141. *
  142. ****************************************************************************/
  143. void *cdecl Set_Font(void const *font)
  144. {
  145. void const *oldfont;
  146. FontInfo *fi;
  147. oldfont = FontPtr;
  148. if (font != NULL) {
  149. FontWidthBlockPtr = ((char *)font + ((Font *)font)->WidthBlk);
  150. fi = (FontInfo *)((char *)font + ((Font *)font)->InfoBlk);
  151. FontHeight = fi->MaxHeight;
  152. FontWidth = fi->MaxWidth;
  153. FontPtr = font;
  154. }
  155. return ((void *)oldfont);
  156. }
  157. /****************************************************************************
  158. *
  159. * NAME
  160. * String_Pixel_Width - Get the pixel width of a string.
  161. *
  162. * SYNOPSIS
  163. * Width = String_Pixel_Width(String)
  164. *
  165. * long String_Pixel_Width(char *);
  166. *
  167. * FUNCTION
  168. * Calculates the pixel width of a string of characters.
  169. *
  170. * INPUTS
  171. * String - Pointer to string to calculate width for.
  172. *
  173. * RESULT
  174. * Width - Width of string in pixels.
  175. *
  176. ****************************************************************************/
  177. unsigned short String_Pixel_Width(char const *string)
  178. {
  179. long width = 0;
  180. long largest = 0;
  181. while (*string != NULL) {
  182. if (*string == '\r') {
  183. string++;
  184. largest = max(largest, width);
  185. width = 0;
  186. } else {
  187. width += Char_Pixel_Width(*string++);
  188. }
  189. }
  190. largest = max(largest, width);
  191. return (largest);
  192. }