-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnitMain.cpp
219 lines (154 loc) · 4.02 KB
/
UnitMain.cpp
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
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "UnitMain.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
fontBitmap = new TBitmap();
fontBitmap->PixelFormat = pf24bit;
fontBitmap->Width = 256;
fontBitmap->Height = 256;
fontBitmap->Canvas->Brush->Color = TColor(0);
fontBitmap->Canvas->FillRect(Rect(0,0,fontBitmap->Width,fontBitmap->Height));
}
int TForm1::encodeLetter(TBitmap *bmp, unsigned char letterWidth, unsigned char letterHeight, unsigned char *fontBuffer )
{
int x,y;
int fbIdx;
unsigned int fColor;
unsigned int r,g,b;
unsigned char fd;
fbIdx = 0;
for( y = 0; y < letterHeight; y++ )
{
for( x = 0; x < letterWidth; x += 2 )
{
//left pixel
fColor = bmp->Canvas->Pixels[ x ][ y ];
b = fColor & 0xff;
g = ( fColor >> 8 ) & 0xff;
r = ( fColor >> 16 ) & 0xff;
fColor = ( r + g + b ) / 3;
fd = fColor & 0xf0;
//right pixel
if( ( x + 1 ) < bmp->Width )
{
fColor = bmp->Canvas->Pixels[ x + 1 ][ y ];
}else{
fColor = 0;
}
b = fColor & 0xff;
g = ( fColor >> 8 ) & 0xff;
r = ( fColor >> 16 ) & 0xff;
fColor = ( r + g + b ) / 3;
fColor &= 0xf0;
fd |= fColor >> 4;
fontBuffer[ fbIdx++ ] = fd;
}
}
return fbIdx;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Convertfont1Click(TObject *Sender)
{
int i;
AnsiString str;
unsigned char *fontBuf;
FILE *out;
AnsiString outFileName;
if( !FontDialog1->Execute() )
{
return;
}
if( SaveDialog1->Execute() )
{
outFileName = SaveDialog1->FileName;
}
else
{
return;
}
for( i = 0; i < sizeof( genFontWidths ); i++ )
{
genFontWidths[ i ] = 0;
}
for( i = 0; i < sizeof( genFontBuffer ); i++ )
{
genFontBuffer[ i ] = 0;
}
selectedFont = FontDialog1->Font;
fontBitmap->Canvas->Brush->Color = TColor( 0 );
fontBitmap->Canvas->FillRect( Rect( 0, 0, fontBitmap->Width, fontBitmap->Height ) );
fontBitmap->Canvas->Font = selectedFont;
fontBitmap->Canvas->Font->Color = TColor( 0xffffff );
genFontHeight = fontBitmap->Canvas->TextHeight( "#" );
fontBuf = &genFontBuffer[ 0 ];
for( i = 32; i < 256; i++ )
{
str = str.sprintf( "%c", i );
fontBitmap->Canvas->FillRect( Rect( 0, 0, fontBitmap->Width, fontBitmap->Height ) );
fontBitmap->Canvas->TextOut( 0, 0, str );
genFontWidths[ i ] = fontBitmap->Canvas->TextWidth( str );
fontBuf += encodeLetter( fontBitmap, genFontWidths[ i ], genFontHeight, fontBuf );
Canvas->Draw( (i%16)*20, (i/16)*20, fontBitmap );
}
out = fopen( outFileName.c_str(), "wb" );
if( out )
{
//type
fputc( 0x04, out ); //#define GF_FONT_TYPE_ALPHA4_BITMAP_VAR_WIDTH 0x0004
fputc( 0x00, out );
//flags
fputc( 0x00, out );
fputc( 0x00, out );
//width
fputc( 0x00, out );
fputc( 0x00, out );
//height
fputc( genFontHeight & 0xff, out );
fputc( ( genFontHeight >> 8 ) & 0xff, out );
//charColor
fputc( 0xff, out );
fputc( 0xff, out );
//backgroundColor
fputc( 0x00, out );
fputc( 0x00, out );
//firstChar
fputc( 32, out );
//lastChar
fputc( 0xff, out );
//charWidths 14 + 8 = 22
fputc( 22, out );
fputc( 0, out );
fputc( 0, out );
fputc( 0, out );
//charBuffer 22 + 224 char widths = 246
fputc( 246, out );
fputc( 0, out );
fputc( 0, out );
fputc( 0, out );
//charWidths
for( i = 32; i < 256; i++ )
{
fputc( genFontWidths[ i ], out );
}
//charBuffer
for( i = 0; i < ( fontBuf - &genFontBuffer[0] ); i++ )
{
fputc( genFontBuffer[ i ], out );
}
fclose( out );
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::exitClick(TObject *Sender)
{
Close();
}
//---------------------------------------------------------------------------