File: fontdev.C

package info (click to toggle)
sabre 0.2.4b-6
  • links: PTS
  • area: main
  • in suites: potato
  • size: 5,536 kB
  • ctags: 7,207
  • sloc: cpp: 36,926; ansic: 8,272; sh: 2,547; makefile: 208
file content (292 lines) | stat: -rw-r--r-- 6,163 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// -*- mode: C; -*-
// jed does not understand .C

//
// Minefields:
// 
// This code is mine. You may use, modify or redistribute this code 
// in any APPROPRIATE manner you may choose. 
//  
// If you use, modify or redistribute this code, I am NOT responsible
// for ANY issue raised. 
// 
// ( If you modify or remove this note, this code is no longer mine. ) 
// 
// proff@iki.fi 
// 
//

// Mon Dec 22 16:05:04 EET 1997 (proff@alf.melmac)
// created file fontdev.C

// Current version: gdev-0.3.0 beta #14
// Last modified:

#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <unistd.h>
#include "fontdev.h"

static char *_locate( char *fmt, ... )
{
   static char fn[4096]; // ?!

   va_list ap;
   va_start(ap,fmt);
   vsprintf(fn,fmt,ap);
   va_end(ap);

   if( access( fn, R_OK ) ) {
      perror( fn );
      return NULL;
   }
   return fn;
}

#define SYSFONTPATH "/usr/lib/kbd/consolefonts"

static char *_fontpaths[] = {
   SYSFONTPATH,
   NULL
};

char **fontpaths=_fontpaths;

static char *locate( char *fn )
{
   char *_fn;     
   if( (_fn=_locate( fn )) == NULL ) {
      if( strchr( fn, '/' ) == NULL ) {
	 char **v=fontpaths;
	 while( *v ) {
	    if( (_fn=_locate( "%s/%s", *v, fn )) != NULL )
	      break;
	    v++;
	 }
      }
   }
   return _fn;
}

fontdev::fontdev()
{
}

fontdev::~fontdev()
{
   if( fbp )
     delete fbp;
}

int fontdev::load( char *fn )
{
   if( (fn=locate(fn) ) == NULL )
     return 1;
   FILE *fp=fopen( fn, "rb" );
   if( !fp ) {
      perror( fn );
      return 1;
   }
   unsigned char buf[4096];
   // (try to) read 256 8x16 cells
   int scans=fread( buf, 1, 4096, fp );
   fclose( fp );
   // analyze fontset
   if( scans > 2048 )
     dimy=16;
   else
     dimy=8;
   unsigned char *p=buf, mask=0;
   int i, j;
   for( i=0; i<scans; i++, p++ ) {
      mask|=*p;
   }
   for( dimx=8; dimx>0; dimx--, mask>>=1 )
     if( mask & 1 )
       break;
   // locate font space
   dimy-=7;
   if( dimy<4 )
     dimy=4; // this is ridiculous
   for( ; dimy<16; dimy++ ) {
      p=buf+(dimy*32);
      for( j=0; j<dimy; j++, p++ ) {
	 if( *p )
	   break;
      }
      if( j == dimy )
	break;
   }
   fprintf( stderr, "found %dx%d font in %s\n", dimx, dimy, fn );
   mindimx=dimx; mindimy=dimy;
   // expand font to row-major bitmap
   fbp=new unsigned char[8*16*256];
   p=(unsigned char *)fbp;
   memset( p, 0, 8*16*256 );
   unsigned char endmask=0x80>>dimx;
   for( i=0; i<scans; i++ ) {
      for( mask=0x80; mask>endmask; mask>>=1, p++ ) {
	 if( buf[i]&mask ) {
	    *p=0xff;  
	 }
      }
   }
   return 0;
}

int fontdev::save( char *fn )
{
   FILE *fp=fopen( fn, "wb" );
   if( !fp ) {
      perror( fn );
      return 1;
   }
   calcmin();
   if( dimx!=mindimx||dimy!=mindimy )
     setcell( mindimx, mindimy );
   // there must be better way to do this loop!
   unsigned char *p=(unsigned char *)fbp;
   int i, j, k, count=0;
   for( j=0; j<256; j++ ) { // for each character
      for( i=0; i<dimy; i++ ) { // for each scan
	 char c=0;
	 for( k=0; k<dimx; k++ ) { // for each bit
	    if( p[j*dimy*dimx+i*dimx+k] )
	      c|=(0x80>>k);
	 }
	 fputc( c, fp );
	 count++;
      }
   }

   int fill=4096-count;
   if( fill > 2047 )
     fill-=2048;
   if( fill )
     while( fill-- )
       fputc( 0, fp );
   
   fclose( fp );
   return 0;
}

void *fontdev::getfbp( unsigned int c )
{
   unsigned char *p=(unsigned char *)fbp;
   p+=c*dimx*dimy;
   return p;
}

void fontdev::calcmin( void )
{
   // analyze width & height
   int i, j, k, widthbuf[8], heightbuf[16];
   memset( widthbuf, 0, sizeof(widthbuf));
   memset( heightbuf, 0, sizeof(heightbuf));
   
   unsigned char *p=(unsigned char *)fbp;
   for( j=0; j<256; j++ ) { // for each character
      for( i=0; i<dimy; i++ ) { // for each scan
	 for( k=0; k<dimx; k++ ) { // for each bit
	    if( p[j*dimy*dimx+i*dimx+k] ) {
	       widthbuf[k]++;
	       heightbuf[i]++;
	    }
	 }	 
      }      
   }

   for( i=dimy; i>0; i-- ) { // for each scan
      if( heightbuf[i-1] )
	break;
   }
   mindimy=i;
   for( i=dimx; i>0; i-- ) { // for each bit
      if( widthbuf[i-1] )
	break;
   }
   mindimx=i;
}
   
int fontdev::setcell( int _dimx, int _dimy )
{
   if( dimx==_dimx && dimy==_dimy )
     return 0;
   unsigned char *newfbp=new unsigned char[_dimx*_dimy*256], *p=(unsigned char *)fbp;
   memset( newfbp, 0, _dimx*_dimy*256 );
   // there must be better way to do this loop!
   int i, j, k;
   for( j=0; j<256; j++ ) { // for each character
      for( i=0; i<dimy; i++ ) { // for each scan
	 for( k=0; k<dimx; k++ ) { // for each bit
	    newfbp[j*_dimy*_dimx+i*_dimx+k]=p[j*dimy*dimx+i*dimx+k];
	 }
      }      
   }
   memcpy( fbp, newfbp, _dimx*_dimy*256 );
   delete newfbp;
   dimx=_dimx;
   dimy=_dimy;
   calcmin();
   return 0;
}

void fontdev::rotate( int what, int where )
{
   unsigned char *hold, *p;
   int i, j;
   switch( where ) {
    case up:
      hold=new unsigned char[dimx];
      p=(unsigned char *)getfbp(what);
      memcpy( hold, p, dimx );
      memcpy( p, p+dimx, (dimy-1)*dimx );
      memcpy( p+(dimy-1)*dimx, hold, dimx );
      delete hold;
      break;
    case right: 
      hold=new unsigned char[dimy];
      p=(unsigned char *)getfbp(what);
      for( i=0; i<dimy; i++ )
	hold[i]=p[(i+1)*dimx-1];
      for( j=0; j<dimy; j++, p+=dimx ) {
	 for( i=dimx-1; i>-1; i-- )
	   p[i]=p[i-1];
	 *p=hold[j];
      }
      break;
    case left: 
      hold=new unsigned char[dimy];
      p=(unsigned char *)getfbp(what);
      for( i=0; i<dimy; i++ )
	hold[i]=p[i*dimx];
      for( i=0; i<dimy; i++, p+=dimx ) {
	 memcpy( p, p+1, dimx-1 );
	 p[dimx-1]=hold[i];
      }
      break;
    case down: 
      hold=new unsigned char[dimx];
      p=(unsigned char *)getfbp(what);
      memcpy( hold, p+(dimy-1)*dimx, dimx );
      memcpy( p+dimx, p, (dimy-1)*dimx );
      memcpy( p, hold, dimx );
      delete hold;
      break;
    case turn_right: 
      hold=new unsigned char[dimx*dimy];
      memset( hold, 0, dimx*dimy );
      for( j=0; j<16; j++ ) {
	 if( j >= dimy )
	   break;
	 for( i=0; i<8; i++ ) {
	    if( i >= dimx )
	      break;
	 }
      }
      break;
    case turn_left:
      break;
   }
}