FONT.CPP 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. //
  2. // Copyright 2020 Electronic Arts Inc.
  3. //
  4. // TiberianDawn.DLL and RedAlert.dll and corresponding source code is free
  5. // software: you can redistribute it and/or modify it under the terms of
  6. // the GNU General Public License as published by the Free Software Foundation,
  7. // either version 3 of the License, or (at your option) any later version.
  8. // TiberianDawn.DLL and RedAlert.dll and corresponding source code is distributed
  9. // in the hope that it will be useful, but with permitted additional restrictions
  10. // under Section 7 of the GPL. See the GNU General Public License in LICENSE.TXT
  11. // distributed with this program. You should have received a copy of the
  12. // GNU General Public License along with permitted additional restrictions
  13. // with this program. If not, see https://github.com/electronicarts/CnC_Remastered_Collection
  14. /***************************************************************************
  15. ** C O N F I D E N T I A L --- W E S T W O O D A S S O C I A T E S **
  16. ***************************************************************************
  17. * *
  18. * Project Name : LIBRARY *
  19. * *
  20. * File Name : FONT.C *
  21. * *
  22. * Programmer : David Dettmer *
  23. * *
  24. * Last Update : July 20, 1994 [SKB] *
  25. * *
  26. *-------------------------------------------------------------------------*
  27. * Functions: *
  28. * Char_Pixel_Width -- Return pixel width of a character. *
  29. * String_Pixel_Width -- Return pixel width of a string of characters. *
  30. * Get_Next_Text_Print_XY -- Calculates X and Y given ret value from Text_P*
  31. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  32. #include "font.h"
  33. #include <malloc.h>
  34. #include <dos.h>
  35. #include <fcntl.h>
  36. #include <io.h>
  37. #include <sys\stat.h>
  38. #include <string.h>
  39. #include <wwstd.h>
  40. /***************************************************************************
  41. * CHAR_PIXEL_WIDTH -- Return pixel width of a character. *
  42. * *
  43. * Retreives the pixel width of a character from the font width block. *
  44. * *
  45. * INPUT: Character. *
  46. * *
  47. * OUTPUT: Pixel width of a string of characters. *
  48. * *
  49. * WARNINGS: Set_Font must have been called first. *
  50. * *
  51. * HISTORY: *
  52. * 01/31/1992 DRD : Created. *
  53. * 06/30/1994 SKB : Converted to 32 bit library. *
  54. *=========================================================================*/
  55. int __cdecl Char_Pixel_Width(char chr)
  56. {
  57. int width;
  58. width = (unsigned char)*(FontWidthBlockPtr + (unsigned char)chr) + FontXSpacing;
  59. return(width);
  60. }
  61. /***************************************************************************
  62. * STRING_PIXEL_WIDTH -- Return pixel width of a string of characters. *
  63. * *
  64. * Calculates the pixel width of a string of characters. This uses *
  65. * the font width block for the widths. *
  66. * *
  67. * INPUT: Pointer to string of characters. *
  68. * *
  69. * OUTPUT: Pixel width of a string of characters. *
  70. * *
  71. * WARNINGS: Set_Font must have been called first. *
  72. * *
  73. * HISTORY: *
  74. * 01/30/1992 DRD : Created. *
  75. * 01/31/1992 DRD : Use Char_Pixel_Width. *
  76. * 06/30/1994 SKB : Converted to 32 bit library. *
  77. *=========================================================================*/
  78. unsigned int __cdecl String_Pixel_Width(char const *string)
  79. {
  80. WORD width; // Working accumulator of string width.
  81. WORD largest = 0; // Largest recorded width of the string.
  82. if (!string) return(0);
  83. width = 0;
  84. while (*string) {
  85. if (*string == '\r') {
  86. string++;
  87. largest = MAX(largest, width);
  88. width = 0;
  89. } else {
  90. width += Char_Pixel_Width(*string++); // add each char's width
  91. }
  92. }
  93. largest = MAX(largest, width);
  94. return(largest);
  95. }
  96. /***************************************************************************
  97. * GET_NEXT_TEXT_PRINT_XY -- Calculates X and Y given ret value from Text_P*
  98. * *
  99. * *
  100. * INPUT: VVPC& vp - viewport that was printed to. *
  101. * unsigned long offset - offset that Text_Print returned. *
  102. * INT *x - x return value. *
  103. * INT *y - y return value. *
  104. * *
  105. * OUTPUT: x and y are set. *
  106. * *
  107. * WARNINGS: *
  108. * *
  109. * HISTORY: *
  110. * 07/20/1994 SKB : Created. *
  111. *=========================================================================*/
  112. VOID __cdecl Get_Next_Text_Print_XY(GraphicViewPortClass& gp, unsigned long offset, INT *x, INT *y)
  113. {
  114. INT buffwidth;
  115. if (offset) {
  116. buffwidth = gp.Get_Width() + gp.Get_XAdd();
  117. offset -= gp.Get_Offset();
  118. *x = offset % buffwidth;
  119. *y = offset / buffwidth;
  120. } else {
  121. *x = *y = 0;
  122. }
  123. }