-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUTFONT.PAS
183 lines (161 loc) · 4.9 KB
/
UTFONT.PAS
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
{
uffont Unit
A font implementation that uses 32 bit ints to store glyph data,
using only 1KB to store the entire charset
2022 LRT
}
unit
utfont;
interface
uses
consts, utils, uexc, uclasses, types, locale,
uobject, ufont, ucanvas, ustream;
type
PSymbolArray = ^TSymbolArray;
TSymbolArray = array[0..255] of longint;
type
PTinyFont = ^TTinyFont;
TTinyFont = object (TFont)
public
constructor init;
constructor initWithData(fromChar, toChar: char; symbolArray: PSymbolArray);
destructor done; virtual;
procedure getCharSize(ch: char; scale: byte; var width, height: word); virtual;
procedure drawChar(ch: char; scale: byte; x, y: word; canvas: PCanvas); virtual;
function getSymbol(ch: char): longint;
procedure setSymbol(ch: char; symbol: longint);
procedure save(stream: PStream); virtual;
procedure load(stream: PStream); virtual;
function getClassName: string; virtual;
function getClassId: word; virtual;
private
_name: string32;
_fromChar, _toChar: char;
_mustFreeMem: boolean;
_symbolArray: PSymbolArray;
end;
implementation
type
TFontFileSignature = array[0..1] of char;
TFontFileType = word;
const
C_CHAR_WIDTH = 5;
C_CHAR_HEIGHT = 6;
C_FONT_FILE_SIGNATURE: TFontFileSignature = 'FN';
C_FONT_FILE_TYPE: TFontFileType = 1;
{ TTinyFont public }
constructor TTinyFont.init;
begin
inherited init;
_name := '';
_fromChar := #0;
_toChar := #255;
_mustFreeMem := true;
getMem(_symbolArray, sizeof(TSymbolArray));
fillChar(_symbolArray^, sizeof(TSymbolArray), 0);
end;
constructor TTinyFont.initWithData(fromChar, toChar: char; symbolArray: PSymbolArray);
begin
inherited init;
_name := '';
_fromChar := fromChar;
_toChar := toChar;
_mustFreeMem := false;
_symbolArray := symbolArray;
end;
destructor TTinyFont.done;
begin
if _mustFreeMem then freeMem(_symbolArray, sizeOf(TSymbolArray));
inherited done;
end;
procedure TTinyFont.getCharSize(ch: char; scale: byte; var width, height: word);
begin
if (ch < _fromChar) or (ch > _toChar) { out of bounds }
or ( (ch <> ' ') and (_symbolArray^[ord(ch)] = 0)) then { it's an empty char and it's not space }
begin
width := 0;
height := 0;
end else begin
width := C_CHAR_WIDTH * scale;
height := C_CHAR_HEIGHT * scale;
end;
end;
procedure TTinyFont.drawChar(ch: char; scale: byte; x, y: word; canvas: PCanvas);
var
cx, cy: word;
glyph: longint;
brush: PBrush;
previousFillColor: TColor;
begin
if (ch < _fromChar) or (ch > _toChar) then exit;
glyph := _symbolArray^[ord(ch) - ord(_fromChar)] shr 2;
if glyph <> 0 then
begin
previousFillColor := canvas^.getFillBrush^.color;
brush := canvas^.getStrokeBrush;
canvas^.getFillBrush^.color := brush^.color;
for cy := C_CHAR_HEIGHT - 1 downto 0 do
for cx := C_CHAR_WIDTH - 1 downto 0 do
begin
if (glyph and 1) <> 0 then
if scale = 1 then
canvas^.setPixel(cx + x, cy + y, brush)
else
canvas^.frect(x+(cx*scale), y+(cy*scale), scale, scale);
glyph := glyph shr 1;
end;
canvas^.getFillBrush^.color := previousFillColor;
end;
end;
procedure TTinyFont.setSymbol(ch: char; symbol: longint);
begin
if (ch >= _fromChar) or (ch <= _toChar) then
_symbolArray^[ord(ch)] := symbol;
end;
function TTinyFont.getSymbol(ch: char): longint;
begin
if (ch < _fromChar) or (ch > _toChar) then
getSymbol := 0
else
getSymbol := _symbolArray^[ord(ch)];
end;
procedure TTinyFont.save(stream: PStream);
begin
with stream^ do
begin
write(@C_FONT_FILE_SIGNATURE, sizeof(TFontFileSignature));
write(@C_FONT_FILE_TYPE, 2);
write(@_fromChar, sizeof(_fromChar));
write(@_toChar, sizeof(_toChar));
writepchar(_name);
write(_symbolArray, sizeof(TSymbolArray));
end;
end;
procedure TTinyFont.load(stream: PStream);
var
signature: TFontFileSignature;
fileType: TFontFileType;
begin
with stream^ do
begin
read(@signature, sizeof(TFontFileSignature));
if signature <> C_FONT_FILE_SIGNATURE then exit;
read(@fileType, sizeof(TFontFileType));
if fileType <> C_FONT_FILE_TYPE then exit;
read(@_fromChar, 1);
read(@_toChar, 1);
_name := readpchar;
read(_symbolArray, sizeof(TSymbolArray));
end;
end;
function TTinyFont.getClassName: string;
begin
getClassName := 'TTinyFont';
end;
function TTinyFont.getClassId: word;
begin
getClassId := C_CLASS_ID_TinyFont;
end;
{ TTinyFont private }
{ Other }
end.