1 /* SCCS Id: @(#)jtp_txt.c 1.0 2000/9/10 */
2 /* Copyright (c) Jaakko Peltonen, 2000 */
3 /* NetHack may be freely redistributed. See license for details. */
4
5 #ifndef _jtp_txt_c_
6 #define _jtp_txt_c_
7
8 /*---------------------------------------------------------------------------
9 Text displaying
10 ---------------------------------------------------------------------------*/
11
12 #include <stdio.h>
13 #include <time.h>
14 #include "jtp_gen.h"
15 #include "jtp_gra.h"
16 #include "jtp_gfl.h"
17 #include "jtp_txt.h"
18
19 jtp_font * jtp_fonts = NULL;
20
21 int txtw_xbegin = 0;
22 int txtw_ybegin = 0;
23 int txtw_xend = 639;
24 int txtw_yend = 479;
25
26
27 /*---------------------------------------------------------------------------
28 PCX font loader
29 ---------------------------------------------------------------------------*/
jtp_load_font(char * nimi,int paikka)30 void jtp_load_font
31 (
32 char *nimi, /* Filename of the PCX image of the font */
33 int paikka /* Font index in the font array */
34 )
35 {
36 int i,j,otax,otax2,otay,otay2,verty,verty2;
37 int fpicx,fpicy;
38 unsigned char line1[] = "abcdefghijklmnopqrstuvwxyz\0";
39 unsigned char line2[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ\0";
40 unsigned char line3[] = "0123456789.,:;\'\"?!()[]/\\#%+-=���� _*$\0";
41 unsigned char * lines[3] = {line1,line2,line3};
42 unsigned char * fontbuffer;
43 int tempheight1 = 0, tempheight2 = 0;
44
45 for (i = 0; i < 256; i++)
46 {
47 jtp_fonts[paikka].fontpics[i].kuva = NULL;
48 jtp_fonts[paikka].fontpics[i].blmod = 0;
49 }
50
51 jtp_load_PCX_buf(0,0,nimi,&fontbuffer,0);
52 fpicy = fontbuffer[0]*256+fontbuffer[1];
53 fpicx = fontbuffer[2]*256+fontbuffer[3];
54
55 verty2 = -1;
56 for (j = 0; j < 3; j++)
57 {
58 verty = verty2+1;
59 while (fontbuffer[verty*fpicx+4] == 254) verty++;
60
61 verty2 = verty+1;
62 while (fontbuffer[verty2*fpicx+4] == 254) verty2++;
63
64 otax = 1; otay = verty;
65 if (verty2-verty+1 > tempheight1) tempheight1 = verty2-verty+1;
66
67 for (i = 0; i < strlen((char *)lines[j]); i++)
68 {
69 while (fontbuffer[otay*fpicx+otax+4]==254) otay++;
70 otay2=otay;
71
72 while (fontbuffer[otay2*fpicx+otax+fpicx+4]!=254) otay2++;
73 jtp_fonts[paikka].fontpics[(lines[j][i])].blmod=otay2-verty2;
74 if (otay2 - verty2 > tempheight2) tempheight2 = otay2 - verty2;
75
76 otax2=otax;
77 while (fontbuffer[otay*fpicx+otax2+1+4]!=254) otax2++;
78
79 jtp_fonts[paikka].fontpics[(lines[j][i])].kuva=jtp_get_img_src(otax,otay,otax2,otay2,fontbuffer);
80
81 otax=otax2+2;
82 otay=verty;
83 }
84 }
85 free(fontbuffer);
86
87 jtp_fonts[paikka].spacing=2;
88 /*
89 jtp_fonts[paikka].lineheight=jtp_fonts[paikka].fontpics['g'].kuva[1];
90 jtp_fonts[paikka].lineheight+=jtp_fonts[paikka].fontpics['h'].kuva[1];
91 jtp_fonts[paikka].lineheight-=jtp_fonts[paikka].fontpics['c'].kuva[1];
92 jtp_fonts[paikka].baseline=jtp_fonts[paikka].fontpics['h'].kuva[1];
93 */
94 /* printf("Lineheight %d, baseline %d\n", tempheight1+tempheight2,tempheight1-1); getch(); */
95 jtp_fonts[paikka].lineheight=tempheight1 + tempheight2;
96 jtp_fonts[paikka].baseline=tempheight1-1;
97 }
98
99
100
101
102 /*---------------------------------------------------------------------------
103 Text plotting into an image pointer
104 ---------------------------------------------------------------------------*/
jtp_put_char(int x,int y,unsigned char textcol,unsigned char * a,unsigned char * destin)105 int jtp_put_char
106 (
107 int x, int y,
108 unsigned char textcol,
109 unsigned char *a,
110 unsigned char *destin
111 )
112 {
113 int Xsize,Ysize,i,j,k,l,yalku,yloppu,xalku,xloppu;
114 int dplus;
115 unsigned char vari;
116
117 if ((a==NULL) || (x>jtp_screen.drx2)) return(0);
118 Ysize = a[1]; /* Assumes that character height is < 256 pixels */
119 Xsize = a[3]; /* Assumes that character width is < 256 pixels */
120 y-=Ysize-1;
121
122 if (x<=jtp_screen.drx1-Xsize) return(0);
123 if ((y<=jtp_screen.dry1-Ysize) || (y>jtp_screen.dry2)) return(Xsize);
124
125 if (y+Ysize-1>jtp_screen.dry2) yloppu=jtp_screen.dry2-y; else yloppu=Ysize-1;
126 if (y<jtp_screen.dry1) {yalku=jtp_screen.dry1-y;} else yalku=0;
127
128 if (x+Xsize-1>jtp_screen.drx2) xloppu=jtp_screen.drx2-x; else xloppu=Xsize-1;
129 if (x<jtp_screen.drx1) {xalku=jtp_screen.drx1-x;} else xalku=0;
130
131 a += 4+yalku*Xsize;
132 if (destin != jtp_screen.vpage)
133 {
134 dplus = destin[3]+256*destin[2];
135 destin += 4;
136 }
137 else dplus = jtp_screen.width;
138
139 destin += (y+yalku)*dplus+x;
140
141 for (j = yalku; j <= yloppu; j++)
142 {
143 for (i = xalku; i <= xloppu; i++)
144 {
145 vari = a[i];
146 if (vari == 15) destin[i] = textcol;
147 else if (vari > 0) destin[i] = vari;
148 }
149 a += Xsize;
150 destin += dplus;
151 }
152 return(Xsize);
153 }
154
155
jtp_put_text(int ax,int ay,int fontti,unsigned char textcol,char * jono,unsigned char * destin)156 void jtp_put_text
157 (
158 int ax, int ay,
159 int fontti, unsigned char textcol,
160 char *jono,
161 unsigned char *destin
162 )
163 {
164 int chx = ax,pituus = strlen(jono),i;
165 unsigned char curcol = textcol;
166 char kirjain;
167
168 for (i = 0; i < pituus; i++)
169 {
170 kirjain = jono[i];
171 switch (kirjain)
172 {
173 case '\n':
174 ay += jtp_fonts[fontti].lineheight;
175 chx=ax;
176 break;
177 default:
178 if (jtp_fonts[fontti].fontpics[(unsigned char)kirjain].kuva != NULL)
179 chx+=jtp_put_char(chx,ay+jtp_fonts[fontti].fontpics[(unsigned char)kirjain].blmod,
180 curcol,jtp_fonts[fontti].fontpics[(unsigned char)kirjain].kuva,destin) +
181 jtp_fonts[fontti].spacing;
182 if (chx>jtp_screen.drx2)
183 {
184 ay += jtp_fonts[fontti].lineheight;
185 chx = ax;
186 }
187 break;
188 }
189 }
190 }
191
192 /*---------------------------------------------------------------------------
193 Text characteristics
194 ---------------------------------------------------------------------------*/
195
jtp_text_length(char * jono,int fontti)196 int jtp_text_length
197 (
198 char *jono,
199 int fontti
200 )
201 {
202 int text_ln, old_ln;
203 int str_ln, i;
204 char kirjain;
205
206 if (!jono) return(0);
207
208 text_ln = 0;
209 old_ln = 0;
210 str_ln = strlen(jono);
211 for (i = 0; i < str_ln; i++)
212 {
213 kirjain = jono[i];
214 switch(kirjain)
215 {
216 case '\n':
217 if (text_ln > old_ln)
218 old_ln = text_ln-jtp_fonts[fontti].spacing;
219 text_ln = 0;
220 break;
221 default:
222 if (jtp_fonts[fontti].fontpics[(unsigned char)kirjain].kuva != NULL)
223 {
224 /* Assume that character width is < 256 pixels */
225 text_ln += jtp_fonts[fontti].fontpics[(unsigned char)kirjain].kuva[3];
226 text_ln += jtp_fonts[fontti].spacing;
227 }
228 break;
229 }
230 }
231 if (text_ln > jtp_fonts[fontti].spacing)
232 text_ln -= jtp_fonts[fontti].spacing;
233 if (text_ln > old_ln) return(text_ln);
234 return(old_ln);
235 }
236
237
jtp_text_height(char * jono,int fontti)238 int jtp_text_height
239 (
240 char *jono,
241 int fontti
242 )
243 {
244 int ay=0,pituus,i;
245 char kirjain;
246
247 if (!jono) return(0);
248
249 // printf(jono); printf("\n\n"); getch();
250 pituus = strlen(jono);
251 for (i = 0; i < pituus; i++)
252 {
253 kirjain=jono[i];
254 switch (kirjain)
255 {
256 case '\n': ay+=jtp_fonts[fontti].lineheight; break;//fflush(stdout); getch(); printf(" newline \n"); getch(); break;
257 default: break;//printf("%c",kirjain); break;
258 }
259 }
260 ay += jtp_fonts[fontti].lineheight;
261 // printf("Total height is %d\n",ay); getch();
262 return(ay);
263 }
264
265
jtp_set_text_window(int xalku,int yalku,int xloppu,int yloppu)266 void jtp_set_text_window(int xalku,int yalku,int xloppu,int yloppu)
267 {
268 jtp_screen.drx1 = xalku;
269 jtp_screen.dry1 = yalku;
270 jtp_screen.drx2 = xloppu;
271 jtp_screen.dry2 = yloppu;
272 }
273
274
jtp_free_fonts(int n_of_fonts)275 void jtp_free_fonts(int n_of_fonts)
276 {
277 int i, j;
278
279 if (jtp_fonts == NULL) return;
280 for (j = 0; j < n_of_fonts; j++)
281 {
282 for (i = 0; i < 256; i++)
283 if (jtp_fonts[j].fontpics[i].kuva != NULL)
284 {
285 free(jtp_fonts[j].fontpics[i].kuva);
286 jtp_fonts[j].fontpics[i].kuva = NULL;
287 }
288 }
289 free(jtp_fonts);
290 jtp_fonts = NULL;
291 }
292
293 #endif
294