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
|
#include <stdio.h>
#include "rfont.h"
#include "strings.h"
#include "bitmap.h"
#include "defs.h"
RGlyph::RGlyph( int w, int h, unsigned char * dat, char mc ):
_width( w ),
_height( h ) {
letter = upcase( mc );
data = new unsigned char[ w * h ];
int count = 0;
for ( int y = 0; y < h; y++ )
for ( int x = 0; x < w; x++ ) {
data[count] = (unsigned char)((double)dat[count]*100.0/255.0);
count++;
}
}
bool RGlyph::rightLetter( char x ) {
return x==letter;
}
int RGlyph::height() {
return _height;
}
int RGlyph::width() {
return _width;
}
RGlyph::~RGlyph() {
delete[] data;
}
void RGlyph::Draw( const Bitmap & work, int x, int y, int c ) {
int o_r = Bitmap::getRed( c );
int o_g = Bitmap::getGreen( c );
int o_b = Bitmap::getBlue( c );
int count = 0;
for ( int cy = 0; cy < _height; cy++ )
for ( int cx = 0; cx < _width; cx++ ) {
int bash = count;
int lx = cx;
while ( data[bash] == data[count] && lx < _width ) {
bash++;
lx++;
}
if ( data[count] > 0 ) {
double rd = (double)data[count] / 100.0;
int a_r = (int)( (double)o_r*rd );
int a_g = (int)( (double)o_g*rd );
int a_b = (int)( (double)o_b*rd );
int fc = Bitmap::makeColor( a_r, a_g, a_b );
work.horizontalLine( x+cx, y+cy, x+lx-1, fc );
}
count = bash;
cx = lx-1;
}
}
RFont::RFont( char * fname ) {
FILE * fv = fopen( fname, "rb" );
glyphs = NULL;
if ( fv == NULL ) {
printf("Error reading font file %s\n", fname );
return;
}
printf("Loading font %s\n", fname );
max_glyphs = Util::readnum( fv, sizeof( int ) );
glyphs = new RGlyph*[max_glyphs];
for ( int q = 0; q < max_glyphs; q++ )
glyphs[q] = NULL;
for ( int q = 0; q < max_glyphs; q++ ) {
int w;
int h;
char letter;
w = Util::readnum( fv, sizeof( int ) );
h = Util::readnum( fv, sizeof( int ) );
letter = Util::readnum( fv, sizeof( char ) );
unsigned char * d = new unsigned char[ w*h ];
fread( d, sizeof( unsigned char ), w*h, fv );
glyphs[q] = new RGlyph( w, h, d, letter );
delete[] d;
}
fclose( fv );
}
int RFont::height() {
if ( glyphs == NULL ) return 0;
int average = 0;
int count = 0;
for ( char q = 'A'; q < 'Z'; q++ )
for ( int z = 0; z < max_glyphs; z++ )
if ( glyphs[z]->rightLetter(upcase(q)) ) {
average += glyphs[z]->height();
count++;
}
return average /= count;
}
void RFont::rtext( const Bitmap & work, int x, int y, int c, const char * wr ) {
if ( glyphs == NULL ) return;
int sx = x;
int sy = y;
for ( int q = 0; wr[q] != '\0'; q++ ) {
int z = 0;
for ( z = 0; z < max_glyphs && !glyphs[z]->rightLetter(upcase(wr[q])); z++ );
if ( z == max_glyphs )sx += 20;
else {
glyphs[z]->Draw( work, sx, sy, c );
sx += glyphs[z]->width();
}
}
}
RFont::~RFont() {
if ( glyphs ) {
for ( int q = 0; q < max_glyphs; q++ )
if ( glyphs[q] )
delete glyphs[q];
delete[] glyphs;
}
}
|