FONT.C 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278
  1. /*
  2. THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
  3. SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
  4. END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
  5. ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
  6. IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
  7. SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
  8. FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
  9. CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
  10. AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
  11. COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
  12. */
  13. /* $Source: f:/miner/source/2d/rcs/font.c $
  14. * $Revision: 1.30 $
  15. * $Author: john $
  16. * $Date: 1995/01/25 20:02:03 $
  17. *
  18. * Graphical routines for drawing fonts.
  19. *
  20. * $Log: font.c $
  21. * Revision 1.30 1995/01/25 20:02:03 john
  22. * Fixed bug with mono clipped fonts only drawing 1 row.
  23. *
  24. * Revision 1.29 1994/12/08 19:03:54 john
  25. * Made functions use cfile.
  26. *
  27. * Revision 1.28 1994/11/27 23:08:22 matt
  28. * Changes for new mprintf calling convention
  29. *
  30. * Revision 1.27 1994/11/18 23:54:50 john
  31. * Made centering work on x = 0x8000 not -0x8000.
  32. *
  33. * Revision 1.26 1994/11/18 22:50:23 john
  34. * Changed shorts to ints in parameters.
  35. *
  36. * Revision 1.25 1994/11/04 11:10:31 john
  37. * Took the & out of string sizing.
  38. *
  39. * Revision 1.24 1994/11/04 10:06:36 john
  40. * Added fade table for fading fonts. Made font that partially clips
  41. * not print a warning message.
  42. *
  43. * Revision 1.23 1994/09/29 10:09:06 john
  44. * Added real clipping to text.
  45. *
  46. * Revision 1.22 1994/09/12 19:27:51 john
  47. * Fixed bug with unclipped fonts clipping.
  48. *
  49. * Revision 1.21 1994/09/12 17:16:59 john
  50. * Added clipping.
  51. *
  52. * Revision 1.20 1994/08/28 16:43:01 matt
  53. * Added centering if x==0x8000
  54. *
  55. * Revision 1.19 1994/08/15 15:01:11 matt
  56. * Close font file after reading
  57. *
  58. * Revision 1.18 1994/08/12 18:17:42 matt
  59. * Fixed stupid bug that cause first char in font not to print
  60. *
  61. * Revision 1.17 1994/08/11 18:00:17 matt
  62. * Cleaned up code, fixed string width bug.
  63. *
  64. * Revision 1.16 1994/08/10 18:46:11 matt
  65. * Fixed a couple of little things
  66. *
  67. * Revision 1.15 1994/08/10 18:36:20 matt
  68. * Changed font file format. Made chars not in font plot as spaces. Font file
  69. * now stores palette for color font & remaps on load.
  70. *
  71. * Revision 1.14 1994/08/10 12:27:27 matt
  72. * Took out unneeded int3
  73. *
  74. * Revision 1.13 1994/08/10 12:25:26 matt
  75. * Added support for colors fonts & kerned fonts
  76. * Made our own font file format
  77. *
  78. * Revision 1.12 1994/07/22 17:19:00 john
  79. * made proportional font width be the max width.
  80. *
  81. * Revision 1.11 1994/05/06 12:50:26 john
  82. * Added supertransparency; neatend things up; took out warnings.
  83. *
  84. * Revision 1.10 1994/01/31 11:08:12 john
  85. * fixed bug with order of frees.
  86. *
  87. * Revision 1.9 1994/01/31 10:36:40 john
  88. * Free'd variable size font data.
  89. *
  90. * Revision 1.8 1994/01/27 17:17:40 john
  91. * Made error if font file doesn't exist.
  92. *
  93. * Revision 1.7 1993/10/26 13:17:48 john
  94. * *** empty log message ***
  95. *
  96. * Revision 1.6 1993/10/15 16:23:25 john
  97. * y
  98. *
  99. * Revision 1.5 1993/09/28 19:05:50 john
  100. * added support for \n in gr_string and gr_get_string_size
  101. *
  102. * Revision 1.4 1993/09/28 12:46:49 matt
  103. * On error, now call Error() instead of exit().
  104. *
  105. * Revision 1.3 1993/09/20 11:35:31 john
  106. * *** empty log message ***
  107. *
  108. * Revision 1.2 1993/09/08 15:54:39 john
  109. * renamed ReadFile to readfontfile to prevent conflicts with others.
  110. *
  111. * Revision 1.1 1993/09/08 11:43:34 john
  112. * Initial revision
  113. *
  114. *
  115. */
  116. #include <stdarg.h>
  117. #include <stdio.h>
  118. #include <stdlib.h>
  119. #include <conio.h>
  120. #include <fcntl.h>
  121. #include <io.h>
  122. #include <sys\types.h>
  123. #include <sys\stat.h>
  124. #include "mem.h"
  125. #include "gr.h"
  126. #include "grdef.h"
  127. #include "error.h"
  128. #include "cfile.h"
  129. #include "mono.h"
  130. #define FONT grd_curcanv->cv_font
  131. #define FG_COLOR grd_curcanv->cv_font_fg_color
  132. #define BG_COLOR grd_curcanv->cv_font_bg_color
  133. #define FWIDTH FONT->ft_w
  134. #define FHEIGHT FONT->ft_h
  135. #define FBASELINE FONT->ft_baseline
  136. #define FFLAGS FONT->ft_flags
  137. #define FMINCHAR FONT->ft_minchar
  138. #define FMAXCHAR FONT->ft_maxchar
  139. #define FDATA FONT->ft_data
  140. #define FCHARS FONT->ft_chars
  141. #define FWIDTHS FONT->ft_widths
  142. #define BITS_TO_BYTES(x) (((x)+7)>>3)
  143. int gr_internal_string_clipped(int x, int y, char *s );
  144. int gr_internal_string_clipped_m(int x, int y, char *s );
  145. char *find_kern_entry(grs_font *font,char first,char second)
  146. {
  147. ubyte *p=font->ft_kerndata;
  148. while (*p!=255)
  149. if (p[0]==first && p[1]==second)
  150. return p;
  151. else p+=3;
  152. return NULL;
  153. }
  154. //takes the character AFTER being offset into font
  155. #define INFONT(_c) ((_c >= 0) && (_c <= FMAXCHAR-FMINCHAR))
  156. //takes the character BEFORE being offset into current font
  157. get_char_width(int c,int c2,int *width,int *spacing)
  158. {
  159. int letter;
  160. letter = c-FMINCHAR;
  161. if (!INFONT(letter)) { //not in font, draw as space
  162. *width=0;
  163. if (FFLAGS & FT_PROPORTIONAL)
  164. *spacing = FWIDTH/2;
  165. else
  166. *spacing = FWIDTH;
  167. return;
  168. }
  169. if (FFLAGS & FT_PROPORTIONAL)
  170. *width = FWIDTHS[letter];
  171. else
  172. *width = FWIDTH;
  173. *spacing = *width;
  174. if (FFLAGS & FT_KERNED) {
  175. ubyte *p;
  176. if (!(c2==0 || c2=='\n')) {
  177. int letter2;
  178. letter2 = c2-FMINCHAR;
  179. if (INFONT(letter2)) {
  180. p = find_kern_entry(FONT,letter,letter2);
  181. if (p)
  182. *spacing = p[2];
  183. }
  184. }
  185. }
  186. }
  187. int get_centered_x(char *s)
  188. {
  189. int w,w2,s2;
  190. for (w=0;*s!=0 && *s!='\n';s++) {
  191. get_char_width(s[0],s[1],&w2,&s2);
  192. w += s2;
  193. }
  194. return ((grd_curcanv->cv_bitmap.bm_w - w) / 2);
  195. }
  196. int gr_internal_string0(int x, int y, char *s )
  197. {
  198. unsigned char * fp;
  199. char * text_ptr, * next_row, * text_ptr1;
  200. int r, BitMask, i, bits, width, spacing, letter, underline;
  201. unsigned int VideoOffset, VideoOffset1;
  202. VideoOffset1 = y * ROWSIZE + x;
  203. next_row = s;
  204. while (next_row != NULL )
  205. {
  206. text_ptr1 = next_row;
  207. next_row = NULL;
  208. if (x==0x8000) { //centered
  209. int xx = get_centered_x(text_ptr1);
  210. VideoOffset1 = y * ROWSIZE + xx;
  211. }
  212. for (r=0; r<FHEIGHT; r++)
  213. {
  214. text_ptr = text_ptr1;
  215. VideoOffset = VideoOffset1;
  216. while (*text_ptr)
  217. {
  218. if (*text_ptr == '\n' )
  219. {
  220. next_row = &text_ptr[1];
  221. break;
  222. }
  223. underline = 0;
  224. if (*text_ptr == '&' )
  225. {
  226. if ((r==FBASELINE+2) || (r==FBASELINE+3))
  227. underline = 1;
  228. text_ptr++;
  229. }
  230. get_char_width(text_ptr[0],text_ptr[1],&width,&spacing);
  231. letter = *text_ptr-FMINCHAR;
  232. if (!INFONT(letter)) { //not in font, draw as space
  233. VideoOffset += spacing;
  234. text_ptr++;
  235. continue;
  236. }
  237. if (FFLAGS & FT_PROPORTIONAL)
  238. fp = FCHARS[letter];
  239. else
  240. fp = FDATA + letter * BITS_TO_BYTES(width)*FHEIGHT;
  241. if (underline)
  242. for (i=0; i< width; i++ )
  243. DATA[VideoOffset++] = FG_COLOR;
  244. else
  245. {
  246. fp += BITS_TO_BYTES(width)*r;
  247. BitMask = 0;
  248. for (i=0; i< width; i++ )
  249. {
  250. if (BitMask==0) {
  251. bits = *fp++;
  252. BitMask = 0x80;
  253. }
  254. if (bits & BitMask)
  255. DATA[VideoOffset++] = FG_COLOR;
  256. else
  257. DATA[VideoOffset++] = BG_COLOR;
  258. BitMask >>= 1;
  259. }
  260. }
  261. VideoOffset += spacing-width; //for kerning
  262. text_ptr++;
  263. }
  264. VideoOffset1 += ROWSIZE; y++;
  265. }
  266. }
  267. return 0;
  268. }
  269. int gr_internal_string0m(int x, int y, char *s )
  270. {
  271. unsigned char * fp;
  272. char * text_ptr, * next_row, * text_ptr1;
  273. int r, BitMask, i, bits, width, spacing, letter, underline;
  274. unsigned int VideoOffset, VideoOffset1;
  275. VideoOffset1 = y * ROWSIZE + x;
  276. next_row = s;
  277. while (next_row != NULL )
  278. {
  279. text_ptr1 = next_row;
  280. next_row = NULL;
  281. if (x==0x8000) { //centered
  282. int xx = get_centered_x(text_ptr1);
  283. VideoOffset1 = y * ROWSIZE + xx;
  284. }
  285. for (r=0; r<FHEIGHT; r++)
  286. {
  287. text_ptr = text_ptr1;
  288. VideoOffset = VideoOffset1;
  289. while (*text_ptr)
  290. {
  291. if (*text_ptr == '\n' )
  292. {
  293. next_row = &text_ptr[1];
  294. break;
  295. }
  296. underline = 0;
  297. if (*text_ptr == '&' )
  298. {
  299. if ((r==FBASELINE+2) || (r==FBASELINE+3))
  300. underline = 1;
  301. text_ptr++;
  302. }
  303. get_char_width(text_ptr[0],text_ptr[1],&width,&spacing);
  304. letter = *text_ptr-FMINCHAR;
  305. if (!INFONT(letter)) { //not in font, draw as space
  306. VideoOffset += spacing;
  307. text_ptr++;
  308. continue;
  309. }
  310. if (FFLAGS & FT_PROPORTIONAL)
  311. fp = FCHARS[letter];
  312. else
  313. fp = FDATA + letter * BITS_TO_BYTES(width)*FHEIGHT;
  314. if (underline)
  315. for (i=0; i< width; i++ )
  316. DATA[VideoOffset++] = FG_COLOR;
  317. else
  318. {
  319. fp += BITS_TO_BYTES(width)*r;
  320. BitMask = 0;
  321. for (i=0; i< width; i++ )
  322. {
  323. if (BitMask==0) {
  324. bits = *fp++;
  325. BitMask = 0x80;
  326. }
  327. if (bits & BitMask)
  328. DATA[VideoOffset++] = FG_COLOR;
  329. else
  330. VideoOffset++;
  331. BitMask >>= 1;
  332. }
  333. }
  334. text_ptr++;
  335. VideoOffset += spacing-width;
  336. }
  337. VideoOffset1 += ROWSIZE; y++;
  338. }
  339. }
  340. return 0;
  341. }
  342. int gr_internal_string2(int x, int y, char *s )
  343. {
  344. unsigned char * fp;
  345. char * text_ptr, * next_row, * text_ptr1;
  346. int r, BitMask, i, bits, width, spacing, letter, underline;
  347. int page_switched;
  348. unsigned int VideoOffset, VideoOffset1;
  349. VideoOffset1 = (unsigned int)DATA + y * ROWSIZE + x;
  350. gr_vesa_setpage(VideoOffset1 >> 16);
  351. VideoOffset1 &= 0xFFFF;
  352. next_row = s;
  353. while (next_row != NULL )
  354. {
  355. text_ptr1 = next_row;
  356. next_row = NULL;
  357. if (x==0x8000) { //centered
  358. int xx = get_centered_x(text_ptr1);
  359. VideoOffset1 = y * ROWSIZE + xx;
  360. gr_vesa_setpage(VideoOffset1 >> 16);
  361. VideoOffset1 &= 0xFFFF;
  362. }
  363. for (r=0; r<FHEIGHT; r++)
  364. {
  365. text_ptr = text_ptr1;
  366. VideoOffset = VideoOffset1;
  367. page_switched = 0;
  368. while (*text_ptr)
  369. {
  370. if (*text_ptr == '\n' )
  371. {
  372. next_row = &text_ptr[1];
  373. break;
  374. }
  375. underline = 0;
  376. if (*text_ptr == '&' )
  377. {
  378. if ((r==FBASELINE+2) || (r==FBASELINE+3))
  379. underline = 1;
  380. text_ptr++;
  381. }
  382. get_char_width(text_ptr[0],text_ptr[1],&width,&spacing);
  383. Assert(width==spacing); //no kerning support here
  384. letter = *text_ptr-FMINCHAR;
  385. if (!INFONT(letter)) { //not in font, draw as space
  386. VideoOffset += spacing;
  387. text_ptr++;
  388. continue;
  389. }
  390. if (FFLAGS & FT_PROPORTIONAL)
  391. fp = FCHARS[letter];
  392. else
  393. fp = FDATA + letter * BITS_TO_BYTES(width)*FHEIGHT;
  394. if (underline)
  395. {
  396. if ( VideoOffset+width > 0xFFFF )
  397. {
  398. for (i=0; i< width; i++ )
  399. {
  400. gr_video_memory[VideoOffset++] = FG_COLOR;
  401. if (VideoOffset > 0xFFFF )
  402. {
  403. VideoOffset -= 0xFFFF + 1;
  404. page_switched = 1;
  405. gr_vesa_incpage();
  406. }
  407. }
  408. }
  409. else
  410. {
  411. for (i=0; i< width; i++ )
  412. gr_video_memory[VideoOffset++] = FG_COLOR;
  413. }
  414. }
  415. else
  416. {
  417. // fp -- dword
  418. // VideoOffset
  419. // width
  420. fp += BITS_TO_BYTES(width)*r;
  421. BitMask = 0;
  422. if ( VideoOffset+width > 0xFFFF )
  423. {
  424. for (i=0; i< width; i++ )
  425. {
  426. if (BitMask==0) {
  427. bits = *fp++;
  428. BitMask = 0x80;
  429. }
  430. if (bits & BitMask)
  431. gr_video_memory[VideoOffset++] = FG_COLOR;
  432. else
  433. gr_video_memory[VideoOffset++] = BG_COLOR;
  434. BitMask >>= 1;
  435. if (VideoOffset > 0xFFFF )
  436. {
  437. VideoOffset -= 0xFFFF + 1;
  438. page_switched = 1;
  439. gr_vesa_incpage();
  440. }
  441. }
  442. } else {
  443. if (width == 8 )
  444. {
  445. bits = *fp++;
  446. if (bits & 0x80) gr_video_memory[VideoOffset+0] = FG_COLOR;
  447. else gr_video_memory[VideoOffset+0] = BG_COLOR;
  448. if (bits & 0x40) gr_video_memory[VideoOffset+1] = FG_COLOR;
  449. else gr_video_memory[VideoOffset+1] = BG_COLOR;
  450. if (bits & 0x20) gr_video_memory[VideoOffset+2] = FG_COLOR;
  451. else gr_video_memory[VideoOffset+2] = BG_COLOR;
  452. if (bits & 0x10) gr_video_memory[VideoOffset+3] = FG_COLOR;
  453. else gr_video_memory[VideoOffset+3] = BG_COLOR;
  454. if (bits & 0x08) gr_video_memory[VideoOffset+4] = FG_COLOR;
  455. else gr_video_memory[VideoOffset+4] = BG_COLOR;
  456. if (bits & 0x04) gr_video_memory[VideoOffset+5] = FG_COLOR;
  457. else gr_video_memory[VideoOffset+5] = BG_COLOR;
  458. if (bits & 0x02) gr_video_memory[VideoOffset+6] = FG_COLOR;
  459. else gr_video_memory[VideoOffset+6] = BG_COLOR;
  460. if (bits & 0x01) gr_video_memory[VideoOffset+7] = FG_COLOR;
  461. else gr_video_memory[VideoOffset+7] = BG_COLOR;
  462. VideoOffset += 8;
  463. } else {
  464. for (i=0; i< width/2 ; i++ )
  465. {
  466. if (BitMask==0) {
  467. bits = *fp++;
  468. BitMask = 0x80;
  469. }
  470. if (bits & BitMask)
  471. gr_video_memory[VideoOffset++] = FG_COLOR;
  472. else
  473. gr_video_memory[VideoOffset++] = BG_COLOR;
  474. BitMask >>= 1;
  475. // Unroll twice
  476. if (BitMask==0) {
  477. bits = *fp++;
  478. BitMask = 0x80;
  479. }
  480. if (bits & BitMask)
  481. gr_video_memory[VideoOffset++] = FG_COLOR;
  482. else
  483. gr_video_memory[VideoOffset++] = BG_COLOR;
  484. BitMask >>= 1;
  485. }
  486. }
  487. }
  488. }
  489. text_ptr++;
  490. }
  491. VideoOffset1 += ROWSIZE; y++;
  492. if (VideoOffset1 > 0xFFFF ) {
  493. VideoOffset1 -= 0xFFFF + 1;
  494. if (!page_switched)
  495. gr_vesa_incpage();
  496. }
  497. }
  498. }
  499. return 0;
  500. }
  501. int gr_internal_string2m(int x, int y, char *s )
  502. {
  503. unsigned char * fp;
  504. char * text_ptr, * next_row, * text_ptr1;
  505. int r, BitMask, i, bits, width, spacing, letter, underline;
  506. int page_switched;
  507. unsigned int VideoOffset, VideoOffset1;
  508. VideoOffset1 = (unsigned int)DATA + y * ROWSIZE + x;
  509. gr_vesa_setpage(VideoOffset1 >> 16);
  510. VideoOffset1 &= 0xFFFF;
  511. next_row = s;
  512. while (next_row != NULL )
  513. {
  514. text_ptr1 = next_row;
  515. next_row = NULL;
  516. if (x==0x8000) { //centered
  517. int xx = get_centered_x(text_ptr1);
  518. VideoOffset1 = y * ROWSIZE + xx;
  519. gr_vesa_setpage(VideoOffset1 >> 16);
  520. VideoOffset1 &= 0xFFFF;
  521. }
  522. for (r=0; r<FHEIGHT; r++)
  523. {
  524. text_ptr = text_ptr1;
  525. VideoOffset = VideoOffset1;
  526. page_switched = 0;
  527. while (*text_ptr)
  528. {
  529. if (*text_ptr == '\n' )
  530. {
  531. next_row = &text_ptr[1];
  532. break;
  533. }
  534. underline = 0;
  535. if (*text_ptr == '&' )
  536. {
  537. if ((r==FBASELINE+2) || (r==FBASELINE+3))
  538. underline = 1;
  539. text_ptr++;
  540. }
  541. get_char_width(text_ptr[0],text_ptr[1],&width,&spacing);
  542. letter = *text_ptr-FMINCHAR;
  543. if (!INFONT(letter)) { //not in font, draw as space
  544. VideoOffset += width;
  545. text_ptr++;
  546. continue;
  547. }
  548. if (FFLAGS & FT_PROPORTIONAL)
  549. fp = FCHARS[letter];
  550. else
  551. fp = FDATA + letter * BITS_TO_BYTES(width)*FHEIGHT;
  552. if (underline)
  553. {
  554. if ( VideoOffset+width > 0xFFFF )
  555. {
  556. for (i=0; i< width; i++ )
  557. {
  558. gr_video_memory[VideoOffset++] = FG_COLOR;
  559. if (VideoOffset > 0xFFFF )
  560. {
  561. VideoOffset -= 0xFFFF + 1;
  562. page_switched = 1;
  563. gr_vesa_incpage();
  564. }
  565. }
  566. }
  567. else
  568. {
  569. for (i=0; i< width; i++ )
  570. gr_video_memory[VideoOffset++] = FG_COLOR;
  571. }
  572. }
  573. else
  574. {
  575. fp += BITS_TO_BYTES(width)*r;
  576. BitMask = 0;
  577. if ( VideoOffset+width > 0xFFFF )
  578. {
  579. for (i=0; i< width; i++ )
  580. {
  581. if (BitMask==0) {
  582. bits = *fp++;
  583. BitMask = 0x80;
  584. }
  585. if (bits & BitMask)
  586. gr_video_memory[VideoOffset++] = FG_COLOR;
  587. else
  588. VideoOffset++;
  589. BitMask >>= 1;
  590. if (VideoOffset > 0xFFFF )
  591. {
  592. VideoOffset -= 0xFFFF + 1;
  593. page_switched = 1;
  594. gr_vesa_incpage();
  595. }
  596. }
  597. } else {
  598. for (i=0; i< width; i++ )
  599. {
  600. if (BitMask==0) {
  601. bits = *fp++;
  602. BitMask = 0x80;
  603. }
  604. if (bits & BitMask)
  605. gr_video_memory[VideoOffset++] = FG_COLOR;
  606. else
  607. VideoOffset++;;
  608. BitMask >>= 1;
  609. }
  610. }
  611. }
  612. text_ptr++;
  613. VideoOffset += spacing-width;
  614. }
  615. VideoOffset1 += ROWSIZE; y++;
  616. if (VideoOffset1 > 0xFFFF ) {
  617. VideoOffset1 -= 0xFFFF + 1;
  618. if (!page_switched)
  619. gr_vesa_incpage();
  620. }
  621. }
  622. }
  623. return 0;
  624. }
  625. //a bitmap for the character
  626. grs_bitmap char_bm = {
  627. 0,0,0,0, //x,y,w,h
  628. BM_LINEAR, //type
  629. BM_FLAG_TRANSPARENT, //flags
  630. 0, //rowsize
  631. NULL, //data
  632. 0 //selector
  633. };
  634. int gr_internal_color_string(int x, int y, char *s )
  635. {
  636. unsigned char * fp;
  637. char * text_ptr, * next_row, * text_ptr1;
  638. int width, spacing,letter;
  639. int xx,yy;
  640. char_bm.bm_h = FHEIGHT; //set height for chars of this font
  641. next_row = s;
  642. yy = y;
  643. while (next_row != NULL)
  644. {
  645. text_ptr1 = next_row;
  646. next_row = NULL;
  647. text_ptr = text_ptr1;
  648. xx = x;
  649. if (xx==0x8000) //centered
  650. xx = get_centered_x(text_ptr);
  651. while (*text_ptr)
  652. {
  653. if (*text_ptr == '\n' )
  654. {
  655. next_row = &text_ptr[1];
  656. yy += FHEIGHT;
  657. break;
  658. }
  659. letter = *text_ptr-FMINCHAR;
  660. get_char_width(text_ptr[0],text_ptr[1],&width,&spacing);
  661. if (!INFONT(letter)) { //not in font, draw as space
  662. xx += spacing;
  663. text_ptr++;
  664. continue;
  665. }
  666. if (FFLAGS & FT_PROPORTIONAL)
  667. fp = FCHARS[letter];
  668. else
  669. fp = FDATA + letter * BITS_TO_BYTES(width)*FHEIGHT;
  670. char_bm.bm_w = char_bm.bm_rowsize = width;
  671. char_bm.bm_data = fp;
  672. gr_bitmapm(xx,yy,&char_bm);
  673. xx += spacing;
  674. text_ptr++;
  675. }
  676. }
  677. return 0;
  678. }
  679. int gr_string(int x, int y, char *s )
  680. {
  681. int w, h, aw;
  682. int clipped=0;
  683. if ( x == 0x8000 ) {
  684. if ( y<0 ) clipped |= 1;
  685. gr_get_string_size(s, &w, &h, &aw );
  686. // for x, since this will be centered, only look at
  687. // width.
  688. if ( w > grd_curcanv->cv_bitmap.bm_w ) clipped |= 1;
  689. if ( (y+h) > grd_curcanv->cv_bitmap.bm_h ) clipped |= 1;
  690. if ( (y+h) < 0 ) clipped |= 2;
  691. if ( y > grd_curcanv->cv_bitmap.bm_h ) clipped |= 2;
  692. } else {
  693. if ( (x<0) || (y<0) ) clipped |= 1;
  694. gr_get_string_size(s, &w, &h, &aw );
  695. if ( (x+w) > grd_curcanv->cv_bitmap.bm_w ) clipped |= 1;
  696. if ( (y+h) > grd_curcanv->cv_bitmap.bm_h ) clipped |= 1;
  697. if ( (x+w) < 0 ) clipped |= 2;
  698. if ( (y+h) < 0 ) clipped |= 2;
  699. if ( x > grd_curcanv->cv_bitmap.bm_w ) clipped |= 2;
  700. if ( y > grd_curcanv->cv_bitmap.bm_h ) clipped |= 2;
  701. }
  702. if ( !clipped )
  703. return gr_ustring(x, y, s );
  704. if ( clipped & 2 ) {
  705. // Completely clipped...
  706. mprintf( (1, "Text '%s' at (%d,%d) is off screen!\n", s, x, y ));
  707. return 0;
  708. }
  709. if ( clipped & 1 ) {
  710. // Partially clipped...
  711. //mprintf( (0, "Text '%s' at (%d,%d) is getting clipped!\n", s, x, y ));
  712. }
  713. // Partially clipped...
  714. if (FFLAGS & FT_COLOR)
  715. return gr_internal_color_string( x, y, s);
  716. if ( BG_COLOR == -1)
  717. return gr_internal_string_clipped_m( x, y, s );
  718. return gr_internal_string_clipped( x, y, s );
  719. }
  720. int gr_ustring(int x, int y, char *s )
  721. {
  722. if (FFLAGS & FT_COLOR) {
  723. return gr_internal_color_string(x,y,s);
  724. }
  725. else
  726. switch( TYPE )
  727. {
  728. case BM_LINEAR:
  729. if ( BG_COLOR == -1)
  730. return gr_internal_string0m(x,y,s);
  731. else
  732. return gr_internal_string0(x,y,s);
  733. case BM_SVGA:
  734. if ( BG_COLOR == -1)
  735. return gr_internal_string2m(x,y,s);
  736. else
  737. return gr_internal_string2(x,y,s);
  738. }
  739. return 0;
  740. }
  741. void gr_get_string_size(char *s, int *string_width, int *string_height, int *average_width )
  742. {
  743. int i = 0, longest_width = 0;
  744. int width,spacing;
  745. *string_height = FHEIGHT;
  746. *string_width = 0;
  747. *average_width = FWIDTH;
  748. if (s != NULL )
  749. {
  750. *string_width = 0;
  751. while (*s)
  752. {
  753. // if (*s == '&')
  754. // s++;
  755. while (*s == '\n')
  756. {
  757. s++;
  758. *string_height += FHEIGHT;
  759. *string_width = 0;
  760. }
  761. if (*s == 0) break;
  762. get_char_width(s[0],s[1],&width,&spacing);
  763. *string_width += spacing;
  764. if (*string_width > longest_width)
  765. longest_width = *string_width;
  766. i++;
  767. s++;
  768. }
  769. }
  770. *string_width = longest_width;
  771. }
  772. int gr_uprintf( int x, int y, char * format, ... )
  773. {
  774. char buffer[1000];
  775. va_list args;
  776. va_start(args, format );
  777. vsprintf(buffer,format,args);
  778. return gr_ustring( x, y, buffer );
  779. }
  780. int gr_printf( int x, int y, char * format, ... )
  781. {
  782. char buffer[1000];
  783. va_list args;
  784. va_start(args, format );
  785. vsprintf(buffer,format,args);
  786. return gr_string( x, y, buffer );
  787. }
  788. void gr_close_font( grs_font * font )
  789. {
  790. if (font)
  791. {
  792. if ( font->ft_chars )
  793. free( font->ft_chars );
  794. free( font );
  795. }
  796. }
  797. void build_colormap_good( ubyte * palette, ubyte * colormap, int * freq );
  798. void decode_data_asm(ubyte *data, int num_pixels, ubyte * colormap, int * count );
  799. #pragma aux decode_data_asm parm [esi] [ecx] [edi] [ebx] modify exact [esi edi eax ebx ecx] = \
  800. "again_ddn:" \
  801. "xor eax,eax" \
  802. "mov al,[esi]" \
  803. "inc dword ptr [ebx+eax*4]" \
  804. "mov al,[edi+eax]" \
  805. "mov [esi],al" \
  806. "inc esi" \
  807. "dec ecx" \
  808. "jne again_ddn"
  809. grs_font * gr_init_font( char * fontname )
  810. {
  811. grs_font *font;
  812. int i;
  813. unsigned char * ptr;
  814. int nchars;
  815. CFILE *fontfile;
  816. int file_id;
  817. int datasize; //size up to (but not including) palette
  818. fontfile = cfopen(fontname, "rb");
  819. if (!fontfile)
  820. Error( "Can't open font file %s", fontname );
  821. cfread(&file_id,sizeof(file_id),1,fontfile);
  822. cfread(&datasize,sizeof(datasize),1,fontfile);
  823. if (file_id != 'NFSP')
  824. Error( "File %s is not a font file", fontname );
  825. font = (grs_font *) malloc(datasize);
  826. cfread(font,1,datasize,fontfile);
  827. nchars = font->ft_maxchar-font->ft_minchar+1;
  828. if (font->ft_flags & FT_PROPORTIONAL) {
  829. font->ft_widths = (short *) (((int) font->ft_widths) + ((ubyte *) font));
  830. font->ft_data = ((int) font->ft_data) + ((ubyte *) font);
  831. font->ft_chars = (unsigned char **)malloc( nchars * sizeof(unsigned char *));
  832. ptr = font->ft_data;
  833. for (i=0; i< nchars; i++ ) {
  834. font->ft_chars[i] = ptr;
  835. if (font->ft_flags & FT_COLOR)
  836. ptr += font->ft_widths[i] * font->ft_h;
  837. else
  838. ptr += BITS_TO_BYTES(font->ft_widths[i]) * font->ft_h;
  839. }
  840. } else {
  841. font->ft_data = ((unsigned char *) font) + sizeof(*font);
  842. font->ft_chars = NULL;
  843. font->ft_widths = NULL;
  844. ptr = font->ft_data + (nchars * font->ft_w * font->ft_h);
  845. }
  846. if (font->ft_flags & FT_KERNED)
  847. font->ft_kerndata = ((int) font->ft_kerndata) + ((ubyte *) font);
  848. if (font->ft_flags & FT_COLOR) { //remap palette
  849. ubyte palette[256*3];
  850. ubyte colormap[256];
  851. int freq[256];
  852. cfread(palette,3,256,fontfile); //read the palette
  853. build_colormap_good( &palette, colormap, freq );
  854. colormap[255] = 255;
  855. decode_data_asm(font->ft_data, ptr-font->ft_data, colormap, freq );
  856. }
  857. cfclose(fontfile);
  858. //set curcanv vars
  859. FONT = font;
  860. FG_COLOR = 0;
  861. BG_COLOR = 0;
  862. return font;
  863. }
  864. void gr_set_fontcolor( int fg, int bg )
  865. {
  866. FG_COLOR = fg;
  867. BG_COLOR = bg;
  868. }
  869. void gr_set_curfont( grs_font * new )
  870. {
  871. FONT = new;
  872. }
  873. int gr_internal_string_clipped(int x, int y, char *s )
  874. {
  875. unsigned char * fp;
  876. char * text_ptr, * next_row, * text_ptr1;
  877. int r, BitMask, i, bits, width, spacing, letter, underline;
  878. int x1 = x, last_x;
  879. next_row = s;
  880. while (next_row != NULL )
  881. {
  882. text_ptr1 = next_row;
  883. next_row = NULL;
  884. x = x1;
  885. if (x==0x8000) //centered
  886. x = get_centered_x(text_ptr1);
  887. last_x = x;
  888. for (r=0; r<FHEIGHT; r++) {
  889. text_ptr = text_ptr1;
  890. x = last_x;
  891. while (*text_ptr) {
  892. if (*text_ptr == '\n' ) {
  893. next_row = &text_ptr[1];
  894. break;
  895. }
  896. underline = 0;
  897. if (*text_ptr == '&' ) {
  898. if ((r==FBASELINE+2) || (r==FBASELINE+3))
  899. underline = 1;
  900. text_ptr++;
  901. }
  902. get_char_width(text_ptr[0],text_ptr[1],&width,&spacing);
  903. letter = *text_ptr-FMINCHAR;
  904. if (!INFONT(letter)) { //not in font, draw as space
  905. x += spacing;
  906. text_ptr++;
  907. continue;
  908. }
  909. if (FFLAGS & FT_PROPORTIONAL)
  910. fp = FCHARS[letter];
  911. else
  912. fp = FDATA + letter * BITS_TO_BYTES(width)*FHEIGHT;
  913. if (underline) {
  914. for (i=0; i< width; i++ ) {
  915. gr_setcolor(FG_COLOR);
  916. gr_pixel( x++, y );
  917. }
  918. } else {
  919. fp += BITS_TO_BYTES(width)*r;
  920. BitMask = 0;
  921. for (i=0; i< width; i++ ) {
  922. if (BitMask==0) {
  923. bits = *fp++;
  924. BitMask = 0x80;
  925. }
  926. if (bits & BitMask)
  927. gr_setcolor(FG_COLOR);
  928. else
  929. gr_setcolor(BG_COLOR);
  930. gr_pixel( x++, y );
  931. BitMask >>= 1;
  932. }
  933. }
  934. x += spacing-width; //for kerning
  935. text_ptr++;
  936. }
  937. y++;
  938. }
  939. }
  940. return 0;
  941. }
  942. int gr_internal_string_clipped_m(int x, int y, char *s )
  943. {
  944. unsigned char * fp;
  945. char * text_ptr, * next_row, * text_ptr1;
  946. int r, BitMask, i, bits, width, spacing, letter, underline;
  947. int x1 = x, last_x;
  948. next_row = s;
  949. while (next_row != NULL )
  950. {
  951. text_ptr1 = next_row;
  952. next_row = NULL;
  953. x = x1;
  954. if (x==0x8000) //centered
  955. x = get_centered_x(text_ptr1);
  956. last_x = x;
  957. for (r=0; r<FHEIGHT; r++) {
  958. x = last_x;
  959. text_ptr = text_ptr1;
  960. while (*text_ptr) {
  961. if (*text_ptr == '\n' ) {
  962. next_row = &text_ptr[1];
  963. break;
  964. }
  965. underline = 0;
  966. if (*text_ptr == '&' ) {
  967. if ((r==FBASELINE+2) || (r==FBASELINE+3))
  968. underline = 1;
  969. text_ptr++;
  970. }
  971. get_char_width(text_ptr[0],text_ptr[1],&width,&spacing);
  972. letter = *text_ptr-FMINCHAR;
  973. if (!INFONT(letter)) { //not in font, draw as space
  974. x += spacing;
  975. text_ptr++;
  976. continue;
  977. }
  978. if (FFLAGS & FT_PROPORTIONAL)
  979. fp = FCHARS[letter];
  980. else
  981. fp = FDATA + letter * BITS_TO_BYTES(width)*FHEIGHT;
  982. if (underline) {
  983. for (i=0; i< width; i++ ) {
  984. gr_setcolor(FG_COLOR);
  985. gr_pixel( x++, y );
  986. }
  987. } else {
  988. fp += BITS_TO_BYTES(width)*r;
  989. BitMask = 0;
  990. for (i=0; i< width; i++ ) {
  991. if (BitMask==0) {
  992. bits = *fp++;
  993. BitMask = 0x80;
  994. }
  995. if (bits & BitMask) {
  996. gr_setcolor(FG_COLOR);
  997. gr_pixel( x++, y );
  998. } else {
  999. x++;
  1000. }
  1001. BitMask >>= 1;
  1002. }
  1003. }
  1004. x += spacing-width; //for kerning
  1005. text_ptr++;
  1006. }
  1007. y++;
  1008. }
  1009. }
  1010. return 0;
  1011. }
  1012.