Advanced Strategic Command
newfont.cpp
Go to the documentation of this file.
1 
9 /*
10  This file is part of Advanced Strategic Command; http://www.asc-hq.de
11  Copyright (C) 1994-2010 Martin Bickel and Marc Schellenberger
12 
13  This program is free software; you can redistribute it and/or modify
14  it under the terms of the GNU General Public License as published by
15  the Free Software Foundation; either version 2 of the License, or
16  (at your option) any later version.
17 
18  This program is distributed in the hope that it will be useful,
19  but WITHOUT ANY WARRANTY; without even the implied warranty of
20  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  GNU General Public License for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with this program; see the file COPYING. If not, write to the
25  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
26  Boston, MA 02111-1307 USA
27 */
28 
29 #include <cstring>
30 #include <stdio.h>
31 #include <errno.h>
32 #include <stdarg.h>
33 #include <stdlib.h>
34 #include <math.h>
35 #include "global.h"
36 #include "newfont.h"
37 #include "stack.h"
38 #include "basestrm.h"
39 #include "misc.h"
40 #include "basegfx.h"
41 
42 #define blockread(a,b,c) fread(b,1,c,a)
43 #define seek(a,b) fseek(a,b,SEEK_SET)
44 
45 const char* fontid = "I\'m a font file on your Disk. Leave me alone !\x01A\x0A0";
46 
47 #define spacewidthkey (int('n'))
48 
50 
52 
53 void expand(void* p1, void* q1, int size)
54 {
55  char* src = (char*) p1;
56  char* dst = (char*) q1;
57 
58  dst[0] = src[0];
59  dst[1] = src[1];
60 
61  src+=2;
62  dst+=2;
63 
64  while ( size ) {
65  char al = *(src++);
66  for ( int i = 0; i < 8; i++ )
67  if ( size ) {
68  if ( al & (1 << i ))
69  dst[i] = 1;
70  else
71  dst[i] = 0;
72 
73  size--;
74  }
75  dst += 8;
76  }
77 }
78 
79 
80 void toldfont::read ( tnstream& stream )
81 {
82  stream.readdata ( &id, sizeof ( id ));
83  stream.readdata ( &name, sizeof ( name ));
84  number = stream.readUint8();
85  color = stream.readUint8();
86  for ( int i = 0; i < 256; ++i ) {
87  character[i].width = stream.readUint8();
88  character[i].size = stream.readWord();
89  character[i].diskposition = stream.readInt();
90  character[i].memposition = NULL; stream.readInt();
91  character[i].dummy = stream.readUint8();
92  }
93  height = stream.readWord();
94  for ( int i = 0; i < 256; ++i )
95  kernchartable[i] = stream.readUint8();
96  for ( int i = 0; i < 101; ++i )
97  for ( int j = 0; j < 101; ++j )
98  kerning[i][j] = stream.readUint8();
99 
100  dummy = stream.readWord();
101  useems = stream.readUint8();
102  caps = stream.readUint8();
103  palette = NULL; stream.readInt();
104  groundline = stream.readUint8();
105 }
106 
108 {
109  toldfont *font1;
110  Uint8 *p;
111  void *q;
112  int ll, ll2;
113  int i;
114 
115  font1 = new toldfont;
116  font1->read ( *stream );
117 
118  font1->id[47] = 0;
119 
120  if ( strncmp(font1->id,fontid, 46) ) {
121  delete font1;
122  return (NULL);
123  }
124 
125  font1->useems = false;
126  font1->palette = NULL;
127 
128  for (i = 0; i <= 255; i++) {
129  if ( font1->character[i].size ) {
130  ll2 = ( font1->character[i].size / 8 + 1) * 8;
131  q = new char [ ll2+2 ];
132  font1->character[i].memposition = (Uint8*) q;
133 
134  if (font1->color == false) {
135  ll = font1->character[i].size / 8 + 1;
136  p = new Uint8 [ ll + 2 ];
137 
138  *((Uint16*)p) = stream->readWord();
139 
140  stream->readdata ( p +2, ll );
141  expand( p, q, font1->character[i].size );
142  delete[] p;
143  } else {
144  *((Uint16*)q) = stream->readWord();
145  stream->readdata ( ((char*)q)+2, font1->character[i].size );
146  }
147 
148  Uint16* pg = (Uint16*) q;
149  if (*pg != font1->character[i].size) {
150  printf("invalid size of character %c\n",i);
151  } /* endif */
152  *pg = font1->character[i].width;
153  } else {
154  if (i == 32) {
155  ll = font1->height * ( font1->character[spacewidthkey].width - 2 );
156  p = new Uint8 [ ll+2 ];
157  Uint16* pg = (Uint16*) p;
158  memset(p,0,ll+2);
159  *pg = font1->character[spacewidthkey].width - 2;
160  font1->character[i].width = font1->character[spacewidthkey].width - 2;
161  font1->character[i].size = ll;
162  font1->character[i].memposition = (Uint8*) p;
163  } /* endif */
164  }
165  }
166 
167 
168  if (font1->color) {
169  if ( font1->palette ) {
170  font1->palette = (dacpalette256*) new char [ sizeof ( dacpalette256 ) ];
171  stream->readdata ( font1->palette, sizeof(dacpalette256));
172  }
173  } else
174  font1->palette = NULL;
175 
176 
177 
178  pfont font2 = new ( tfont );
179  font2->name = strdup ( font1->name );
180  font2->color = font1->color;
181  font2->caps = font1->caps ;
182  font2->height = font1->height;
183  font2->groundline = font1->groundline;
184  font2->palette = font1->palette;
185  for (i = 0; i < 256 ;i++ ) {
186  font2->character[i].width = font1->character[i].width;
187  font2->character[i].size = font1->character[i].size;
188  font2->character[i].memposition =font1->character[i].memposition;
189  for (int j = 0; j < 256 ; j++ ) {
190  font2->kerning[j][i] = font1->kerning[int(font1->kernchartable[i])] [int(font1->kernchartable[j])];
191  if ( font2->kerning[j][i] > 10 || font2->kerning[j][i] < - 10 )
192  printf("%c%c\n",i,j);
193  }
194 
195 
196  } /* endfor */
197 
198 
199  delete font1 ;
200 
201  return font2;
202 }
203 
204 
205 
206 void shrinkfont ( pfont font, int diff )
207 {
208  if ( font )
209  for (int i = 0; i < 256 ;i++ )
210  if (font->character[i].size )
211  for (int j = 0; j < 256 ; j++ )
212  if (font->character[j].size )
213  font->kerning[i][j] += diff;
214 
215 }
216 
217 int gettextwdth ( const char* txt, pfont font )
218 {
219  int i, result;
220 
221  result = 0;
222  if (font == NULL)
223  font = activefontsettings.font;
224  if (font == NULL)
225  return 0;
226 
227  i=0;
228  while (txt[i] != 0) {
229  if (font->character[int(txt[i])].size) {
230  result +=2 + font->character[int(txt[i])].width;
231  if ( txt[i+1] ) // if ((txt[i+1] != 0) && (txt[i+1] != ' '))
232  result += font->kerning[int(txt[i+1])][int(txt[i])];
233  } /* endif */
234  i++;
235  } /* endwhile */
236  if (result > 2) {
237  result -= 2;
238  } else {
239  result = 0;
240  } /* endif */
241 
242  return result;
243 }
244 
245 
246 void showtext3( const char * txt,
247  int xpos,
248  int ypos)
249 {
250  // collategraphicoperations cgo ( xpos, ypos, max ( activefontsettings.height, activefontsettings->font.height ),;
251 
252  char *s, *s2, *s3;
253  int i, j,k;
254  int newx = 0;
255  signed char cnt;
256  int orgjus;
257 
258 
259  if (txt[0] == 0)
260  return;
261  npush( activefontsettings );
262  s2 = new char [ 255 ];
263  s = new char [ 255 ];
264  s3 = new char [ 255 ];
265  s2[0] = 0;
266 
267  i = 0;
268  cnt = 0;
269  do {
270  if (txt[i] != '~') {
271  s2[cnt] = txt[i];
272  cnt++;
273  s2[cnt] = 0;
274  } /* endif */
275  i++;
276  } while ( txt[i] != 0 );
277 
278  cnt = -2;
279  showtext2(s2,xpos,ypos);
280  i = 0;
281  orgjus = activefontsettings.justify;
282 
283  while ((txt[i] != '~') && (txt[i] != 0)) {
284  i++;
285  } /* endwhile */
286 
287  while (txt[i] != 0) {
288  cnt+=2;
289 
290  j = i + 1;
291  s[0] = 0;
292  s3[0] = 0;
293  k=0;
294  activefontsettings.background = 255;
295 
296  while ((txt[j] != '~') && ( txt[j] != 0)) {
297  s[k] = txt[j];
298  k++;
299  s[k] = 0;
300  j++;
301  }
302 
303  if (s[0] != 0) {
304  strncpy( s3, s2, k+i-cnt );
305  s3[k+i-cnt]=0;
306  if (orgjus == lefttext) {
307  newx = xpos + gettextwdth(s3, NULL) - gettextwdth(s,NULL);
308  }
309  else
310  if (orgjus == centertext) {
311  newx = xpos + (activefontsettings.length - gettextwdth(s2, NULL)) / 2 + gettextwdth(s3,NULL) - gettextwdth(s,NULL) + 1;
312  }
313  else
314  if (orgjus == righttext) {
315  newx = xpos + 1 + (activefontsettings.length - gettextwdth(s2, NULL)) + gettextwdth(s3,NULL) - gettextwdth(s,NULL);
316  }
317  activefontsettings.color = activefontsettings.markcolor;
318  activefontsettings.justify = lefttext;
319  showtext2(s,newx,ypos);
320  }
321  i = j + 1;
322  while ((txt[i] != '~') && (txt[i] != 0))
323  i++;
324  }
325  delete[] s3;
326  delete[] s;
327  delete[] s2;
328 
329  npop( activefontsettings );
330 }
331 
332 
333 
334 
335 
336 
337 void showtext3c( const char * txt,
338  int xpos,
339  int ypos)
340 {
341  // collategraphicoperations cgo;
342 
343  int i, j,k;
344  int newx = 0;
345  int cnt;
346  int orgjus;
347 
348 
349  npush( activefontsettings );
350  char s2[1000];
351  char s[1000];
352  char s3[1000];
353  s2[0] = 0;
354 
355  i = 0;
356  cnt = 0;
357  while ( txt[i] ) {
358  if (txt[i] != '~') {
359  s2[cnt] = txt[i];
360  cnt++;
361  s2[cnt] = 0;
362  } /* endif */
363  i++;
364  }
365 
366  cnt = -2;
367  showtext2c(s2,xpos,ypos);
368  i = 0;
369  orgjus = activefontsettings.justify;
370 
371  while ((txt[i] != '~') && (txt[i] ))
372  i++;
373 
374  while ( txt[i] ) {
375  cnt+=2;
376 
377  j = i + 1;
378  s[0] = 0;
379  s3[0] = 0;
380  k=0;
381  activefontsettings.background = 255;
382 
383  while ((txt[j] != '~') && ( txt[j] != 0)) {
384  s[k] = txt[j];
385  k++;
386  s[k] = 0;
387  j++;
388  }
389 
390  if (s[0] != 0) {
391  strncpy( s3, s2, k+i-cnt );
392  s3[k+i-cnt]=0;
393  if (orgjus == lefttext) {
394  newx = xpos + gettextwdth(s3, NULL) - gettextwdth(s,NULL);
395  }
396  else
397  if (orgjus == centertext) {
398  newx = xpos + (activefontsettings.length - gettextwdth(s2, NULL)) / 2 + gettextwdth(s3,NULL) - gettextwdth(s,NULL) + 1;
399  }
400  else
401  if (orgjus == righttext) {
402  newx = xpos + 1 + (activefontsettings.length - gettextwdth(s2, NULL)) + gettextwdth(s3,NULL) - gettextwdth(s,NULL);
403  }
404  activefontsettings.font = activefontsettings.markfont;
405  activefontsettings.justify = lefttext;
406  showtext2c(s,newx,ypos);
407  }
408  i = j + 1;
409  while ((txt[i] != '~') && (txt[i] != 0))
410  i++;
411  }
412 
413  npop( activefontsettings );
414 }
415 
416 void showtext4 ( const char* TextToOutput, int x1, int y1, ... )
417 {
418  va_list paramlist;
419  va_start ( paramlist, y1 );
420 
421  char tempbuf[1000];
422 
423  vsprintf( tempbuf, TextToOutput, paramlist );
424  va_end ( paramlist );
425  showtext2 ( tempbuf, x1, y1 );
426 }
427 
428 void showtext4c ( const char* TextToOutput, int x1, int y1, ... )
429 {
430  va_list paramlist;
431  va_start ( paramlist, y1 );
432 
433  char tempbuf[1000];
434 
435  vsprintf( tempbuf, TextToOutput, paramlist );
436  va_end ( paramlist );
437  showtext2c ( tempbuf, x1, y1 );
438 }
439 
440 class InitNewfont {
441  public:
442  InitNewfont ( void )
443  {
444  memset( &activefontsettings, 0, sizeof ( activefontsettings ));
445  };
446  } initnewfont;
447 
448 char* int2string ( int i, char* buf )
449 {
450  if ( i >= 0 ) {
451  itoa ( i, buf, 10 );
452  } else {
453  buf[0] = '-';
454  i = -i;
455  itoa ( i, &buf[1], 10 );
456  }
457  if ( gettextwdth ( buf, NULL ) > activefontsettings.length && activefontsettings.length) {
458  char buf2[50];
459 
460  int pot = (int) log10 ( double(i) );
461  int base = (int) pow ( float(10), float(pot) );
462  int first = i / base;
463  int rest = i - first * base;
464 
465  buf2[0] = digit[ first ][0];
466  int p = 1;
467  buf2[p] = 'E';
468  buf2[p+1] = 0;
469  strcat ( buf2, strrr ( pot ));
470 
471  while ( gettextwdth ( buf2, NULL ) < activefontsettings.length) {
472  strcpy ( buf, buf2 );
473  base /= 10;
474  if ( p == 1 )
475  buf2[p++] = '.';
476  buf2[p++] = digit[ rest / base ][0];
477  rest = rest % base;
478 
479  buf2[p] = 'E';
480  buf2[p+1] = 0;
481  strcat ( buf2, strrr ( pot ));
482  } /* endwhile */
483  }
484  return buf;
485 
486 }
toldcharacter character[256]
Definition: newfont.h:130
tfontsettings activefontsettings
Definition: newfont.cpp:51
Uint8 markcolor
Definition: newfont.h:172
Uint8 background
Definition: newfont.h:171
#define centertext
Definition: newfont.h:185
void showtext4(const char *TextToOutput, int x1, int y1,...)
Definition: newfont.cpp:416
dacpalette256 dacpal
Definition: newfont.cpp:49
int diskposition
Definition: newfont.h:115
miscellaneous functions which are used by ASC and all its tools
void showtext2c(const ASCString &text, int x, int y)
Definition: basegfx.cpp:1958
#define npush(a)
Definition: stack.h:53
char name[256]
Definition: newfont.h:127
virtual int readInt(void)
Reads a 32 bit signed Integer. In the stream little-endian byte order is used and a translation is pe...
Definition: basestrm.cpp:284
void showtext4c(const char *TextToOutput, int x1, int y1,...)
Definition: newfont.cpp:428
Uint16 dummy
Definition: newfont.h:134
char * int2string(int i, char *buf)
Definition: newfont.cpp:448
Sint16 height
Definition: newfont.h:131
virtual int readWord(void)
Reads a 16 bit unsigned Integer. In the stream little-endian byte order is used and a translation is ...
Definition: basestrm.cpp:291
void shrinkfont(pfont font, int diff)
Definition: newfont.cpp:206
InitNewfont(void)
Definition: newfont.cpp:442
Uint8 number
Definition: newfont.h:128
Uint8 justify
Definition: newfont.h:176
Sint16 height
Definition: newfont.h:154
#define lefttext
Definition: newfont.h:184
Global platform dependant definitions. This file just branches to the platform specific files in thei...
Uint8 width
Definition: newfont.h:113
The interface for all kinds of IO stream.
Uint8 color
Definition: newfont.h:170
virtual Uint8 readUint8(void)
Reads a 8 bit unsigned Integer.
Definition: basestrm.cpp:305
Uint8 color
Definition: newfont.h:152
Uint8 caps
Definition: newfont.h:136
Uint8 caps
Definition: newfont.h:153
dacpalette256 * palette
Definition: newfont.h:137
Uint8 * memposition
Definition: newfont.h:147
dacpalette256 * palette
Definition: newfont.h:157
void read(tnstream &stream)
Definition: newfont.cpp:80
pfont font
Definition: newfont.h:169
const char * fontid
Definition: newfont.cpp:45
const char * digit[]
Definition: misc.cpp:37
char * strrr(int a)
converts a to a string.
Definition: misc.cpp:66
class InitNewfont initnewfont
signed char kerning[256][256]
Definition: newfont.h:156
void expand(void *p1, void *q1, int size)
Definition: newfont.cpp:53
pfont loadfont(tnstream *stream)
Definition: newfont.cpp:107
virtual int readdata(void *buf, int size, bool excpt=true)=0
Reads data from the stream.
Uint16 length
Definition: newfont.h:174
The interface for the various streams that ASC offers, like file and memory streams.
void showtext3(const char *txt, int xpos, int ypos)
Definition: newfont.cpp:246
Uint8 color
Definition: newfont.h:129
int gettextwdth(const char *txt, pfont font)
Definition: newfont.cpp:217
void showtext3c(const char *txt, int xpos, int ypos)
Definition: newfont.cpp:337
#define npop(a)
Definition: stack.h:54
Uint8 groundline
Definition: newfont.h:138
Uint16 width
Definition: newfont.h:145
Uint16 size
Definition: newfont.h:146
charr id
Definition: newfont.h:126
void showtext2(const ASCString &text, int x, int y)
Definition: basegfx.cpp:1953
char * itoa(int a, char *b, int c)
Definition: misc.cpp:179
tkerning kerning
Definition: newfont.h:133
basegfx.h is the interface for the legacy graphic routines (of which some are platform dependent)...
Uint8 * memposition
Definition: newfont.h:116
char * name
Definition: newfont.h:151
#define righttext
Definition: newfont.h:186
#define spacewidthkey
Definition: newfont.cpp:47
tcharacter character[256]
Definition: newfont.h:155
pfont markfont
Definition: newfont.h:180
Uint16 size
Definition: newfont.h:114
Uint8 dacpalette256[256][3]
Definition: palette.h:76
Uint8 useems
Definition: newfont.h:135
Interface for a small general-purpose stack (not type safe)
char groundline
Definition: newfont.h:158
Uint8 dummy
Definition: newfont.h:117
tkernchartable kernchartable
Definition: newfont.h:132
Definition: newfont.h:150