-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTFONT.INC
353 lines (325 loc) · 8.58 KB
/
TFONT.INC
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
{ Copyright 2015 Jerome Shidel }
(*
This project and related files are subject to either the terms
specified in the included LICENSE.TXT file or the GNU GPLv2.0.
*)
{ --- TFont --- }
{$IFDEF INTERFACE}
const
class_TFont = 'TFont';
type
PFont = ^TFont;
TFont = object(TPersistent)
private
public { protected }
function ObjectClass ( AName : String ) : String; virtual;
public { protected }
FFontPtr : Pointer;
FFontSize : integer;
FFileName : String;
FEnabled : boolean;
FWidth : byte;
FHeight : byte;
FHeader : TListItem;
procedure FreeFont;
function LoadFromFile : boolean; virtual;
function SaveToFile(AFileName : String) : boolean; virtual;
public
constructor Create(AName : String);
destructor Destroy; virtual;
function GetFileName : String; virtual;
procedure SetFileName ( AValue : String ) ; virtual;
function GetEnabled : boolean; virtual;
procedure SetEnabled(AValue : boolean); virtual;
function GetHeight : integer; virtual;
function GetWidth : integer; virtual;
function GetHeader : PListItem; virtual;
function GetSize : integer; virtual;
procedure Clear; virtual;
procedure CopyROMFont( AFont : TROMFont ); virtual;
procedure EmptyFont(AHeight : integer); virtual;
end;
{$ENDIF}
{$IFDEF IMPLEMENTATION}
function TFont.ObjectClass(AName : String) : String;
begin
if (AName = '') or (AName = class_TFont) then
ObjectClass := class_TFont
else
ObjectClass := inherited ObjectClass(AName);
end;
constructor TFont.Create(AName : String);
begin
inherited Create(AName);
FFontPtr := nil;
FFontSize := 0;
FFileName := '';
FEnabled := false;
FWidth := 0;
FHeight := 0;
FHeader.Create('TFontHeader');
FHeader.SetFreeOnDestroy(False);
end;
destructor TFont.Destroy;
begin
FreeFont;
FHeader.Destroy;
inherited Destroy;
end;
function TFont.LoadFromFile : boolean;
const
FileID : String = 'QCRT-FONT'#0;
var
F : TFile;
Success : boolean;
S : String;
begin
Success := false;
LoadFromFile := Success;
S := FFileName;
FreeFont;
FFileName := S;
if FFileName = '' then exit;
{$IFDEF DEVLOG_FONTLOAD}
WriteLog('++ Loading font ' + FFileName);
LogInc;
{$ENDIF}
F.Create;
F.Assign(FFileName);
F.Reset;
Success := F.IOResult = 0;
if Success then begin
S := ChrStr(#0, Length(FileID));
F.ReadRecord(S[1], Length(FileID));
Success := (S = FileID) and (IOResult = 0);
end;
while Success and (S <> '') do begin
if Success then begin
S := F.GetString;
Success := IOResult = 0;
if Success then FHeader.Add(New(PListItem, Create(S)));
end;
end;
if Success then begin
F.ReadByte(FWidth);
Success := (IOResult = 0) and (FWidth = 8); { Must be 8x?? font}
end;
if Success then begin
F.ReadByte(FHeight);
Success := IOResult = 0;
end;
if Success then begin
FFontSize := F.FileSize - F.FilePos;
Success := (IOResult = 0) and (MaxAvail > FFontSize);
end;
if Success then begin
GetMem(FFontPtr, FFontSize);
F.ReadRecord(FFontPtr^, FFontSize);
Success := IOResult = 0;
end;
if Not Success then FreeFont;
F.Destroy;
{$IFDEF DEVLOG_FONTLOAD}
LogDec;
WriteLog('++ Loading font completed ' + BoolStr(Success));
{$ENDIF}
LoadFromFile := Success;
end;
function TFont.SaveToFile(AFileName : String) : boolean;
const
FileID : String = 'QCRT-FONT'#0;
var
F : TFile;
Success : boolean;
S : String;
P : PlistItem;
begin
Success := false;
SaveToFile := Success;
{$IFDEF DEVLOG_FONTLOAD}
WriteLog('++ Saving font ' + AFileName);
LogInc;
{$ENDIF}
F.Create;
F.Assign(AFileName);
F.Rewrite;
Success := F.IOResult = 0;
if Success then begin
F.WriteRecord(FileID[1], Length(FileID));
Success := (IOResult = 0);
end;
P := FHeader.First;
while Success and (Assigned(P)) do begin
if Success then begin
S := P^.GetNameID;
if trim(S) <> '' then F.PutString(S);
Success := IOResult = 0;
P := P^.Next;
end;
end;
if Success and Assigned(FHeader.First) then begin
S := '';
F.PutString(S);
Success := (IOResult = 0); { Header text must be ended with only one blank line }
end;
if Success then begin
F.WriteByte(FWidth);
Success := (IOResult = 0); { Must be 8x?? font}
end;
if Success then begin
F.WriteByte(FHeight);
Success := IOResult = 0;
end;
if Success then begin
F.WriteRecord(FFontPtr^, FFontSize);
Success := IOResult = 0;
end;
F.Destroy;
{$IFDEF DEVLOG_FONTLOAD}
LogDec;
WriteLog('++ Saving font completed ' + BoolStr(Success));
{$ENDIF}
SaveToFile := Success;
end;
function TFont.GetFileName : String;
begin
GetFileName := FFileName;
end;
procedure TFont.SetFileName ( AValue : String );
begin
if AValue = FFileName then exit;
FFileName := AValue;
if not LoadFromFile then FFileName := '';
end;
procedure TFont.FreeFont;
begin
if FEnabled then SetEnabled(False);
if Assigned(FFontPtr) then
FreeMem(FFontPtr, FFontSize);
FFontSize := 0;
FFontPtr := nil;
FHeader.Clear;
FWidth := 0;
FHeight := 0;
FFileName := '';
end;
function TFont.GetEnabled : boolean;
begin
GetEnabled := FEnabled;
end;
procedure TFont.SetEnabled(AValue :Boolean);
begin
if FEnabled = AValue then exit;
if AValue then begin
if not Assigned(FFontPtr) then exit;
{ SetUserFont(FHeight, FFontPtr);
InitQCrt; }
UserFontSize := FHeight;
UserFontPtr := FFontPtr;
TextMode(CO80 or FontUser);
end else begin
TextMode(CO80);
end;
FEnabled := AValue;
end;
function TFont.GetHeight : integer;
begin
GetHeight := FHeight;
end;
function TFont.GetWidth : integer;
begin
GetWidth := FWidth;
end;
function TFont.GetHeader : PListItem;
begin
GetHeader := @FHeader;
end;
function TFont.GetSize : integer;
begin
if Assigned(FFontPtr) then
GetSize := FFontSize
else
GetSize := -1;
end;
procedure TFont.Clear;
begin
FreeFont;
end;
procedure TFont.CopyROMFont( AFont : TROMFont );
const
SytemFontPrefix = 'SYS_';
var
P : Pointer;
begin
case AFont of
rf8x14 : begin
EmptyFont(14);
FHeader.Add(New(PListItem, Create('TITLE=Default System 8x14 Font')));
FFileName := SytemFontPrefix + '814.FNT';
end;
rfAlt9x14 : begin
EmptyFont(14);
FHeader.Add(New(PListItem, Create('TITLE=Alternate System 9x14 Font')));
FFileName := SytemFontPrefix + '914.FNT';
end;
rf8x8 : begin
EmptyFont(8);
FHeader.Add(New(PListItem, Create('TITLE=Default System 8x8 Font')));
FFileName := SytemFontPrefix + '808.FNT';
end;
rfHigh8x8 : begin
EmptyFont(14);
FHeader.Add(New(PListItem, Create('TITLE=Default System 8x8 Font')));
FFileName := SytemFontPrefix + '808.FNT';
AFont := rf8x8;
end;
rf8x16 : begin
EmptyFont(16);
FHeader.Add(New(PListItem, Create('TITLE=Default System 8x16 Font')));
FFileName := SytemFontPrefix + '816.FNT';
end;
rfAlt8x16 : begin
EmptyFont(16);
FHeader.Add(New(PListItem, Create('TITLE=Alternate System 8x16 Font')));
FFileName := SytemFontPrefix + '816A.FNT';
end;
end;
FWidth := 8;
P := GetROMFontPtr(AFont);
Move(P^, FFontPtr^, FFontSize);
if AFont = rf8x8 then begin
P := GetROMFontPtr(rfHigh8x8);
Move(Bytes(P^)[8 * $80], Bytes(FFontPtr^)[8 * $80], 8 * $1F );
end;
end;
procedure TFont.EmptyFont(AHeight : integer);
begin
FreeFont;
FWidth := 8;
FHeight := AHeight;
FHeader.Add(New(PListItem, Create('LANGUAGE=en_US')));
if Pos('DOSBOX', UCase(PascalStr(Ptr(65024, 14)^))) > 0 then begin
FHeader.Add(New(PListItem, Create('COPYRIGHT=' + PascalStr(Ptr(65024, 14)^) )));
FHeader.Add(New(PListItem, Create('AUTHOR=' + PascalStr(Ptr(65024, 14)^) )));
FHeader.Add(New(PListItem, Create('VERSION=' + PascalStr(Ptr(65030, 1)^) )));
FHeader.Add(New(PListItem, Create('URL=http://www.dosbox.com')));
FHeader.Add(New(PListItem, Create('LICENSE=unknown')));
FHeader.Add(New(PListItem, Create('LURL=http://www.dosbox.com')));
end else begin
FHeader.Add(New(PListItem, Create('COPYRIGHT=unknown' )));
FHeader.Add(New(PListItem, Create('AUTHOR=unknown' )));
FHeader.Add(New(PListItem, Create('VERSION=1.0.0')));
FHeader.Add(New(PListItem, Create('URL=')));
FHeader.Add(New(PListItem, Create('LICENSE=GNU GENERAL PUBLIC LICENSE, Version 2')));
FHeader.Add(New(PListItem, Create('LURL=http://www.gnu.org/licenses/gpl-2.0.txt')));
end;
FHeader.Add(New(PListItem, Create('')));
FFontSize := FHeight * 256;
if not MemCheck(FFontSize) then begin
FreeFont;
Exit;
end;
GetMem(FFontPtr, FFontSize);
FillChar(FFontPtr^, FFontSize, 0);
end;
{$ENDIF}