-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathff_engine.pas
560 lines (484 loc) · 13.3 KB
/
ff_engine.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
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
//------------------------------------------------------------------------------
//
// DD_FONT: Doom Font Creator
// Copyright (C) 2021-2022 by Jim Valavanis
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, inc., 59 Temple Place - Suite 330, Boston, MA
// 02111-1307, USA.
//
// DESCRIPTION:
// Font Engine
//
//------------------------------------------------------------------------------
// E-Mail: jimmyvalavanis@yahoo.gr
// Site : https://sourceforge.net/projects/dd-font/
//------------------------------------------------------------------------------
unit ff_engine;
interface
uses
Windows, SysUtils, Classes, Graphics;
const
MINFONTSIZE = 4;
MAXFONTSIZE = 20;
type
TFontEngine = class(TObject)
private
fHeight: Integer;
fPitch: TFontPitch;
fStyle: TFontStylesBase;
fCharset: TFontCharset;
fFontName: TFontDataName;
fDrawWidth: Integer;
fDrawHeight: Integer;
fFontSize: Integer;
fFrontColor: LongWord;
fBackColor: LongWord;
fGridWidth: Integer;
fGridHeight: Integer;
function RightCropLetterBitmap(const bm: TBitmap): boolean;
function GetIsBold: Boolean;
procedure SetIsBold(const v: Boolean);
function GetIsItalic: Boolean;
procedure SetIsItalic(const v: Boolean);
function GetIsUnderline: Boolean;
procedure SetIsUnderline(const v: Boolean);
function GetIsStrikeOut: Boolean;
procedure SetIsStrikeOut(const v: Boolean);
public
constructor Create; virtual;
destructor Destroy; override;
procedure Reset;
procedure DrawToCanvas(const C: TCanvas);
procedure DrawToBitmap(const bm: TBitmap);
procedure DrawCharToCanvas(const C: TCanvas; const ch: Char);
procedure DrawCharToBitmap(const bm: TBitmap; const ch: Char);
function DrawStringToBitmap(const bm: TBitmap; const s: string; const fixed_pitch: boolean): Integer;
procedure FromFont(const fnt: TFont);
procedure ToFont(const fnt: TFont);
procedure SaveToStream(const strm: TStream);
procedure LoadFromStream(const strm: TStream);
procedure SaveToFile(const fn: string);
procedure LoadFromFile(const fn: string);
procedure AttachTo(const ff: TFontEngine);
property Height: Integer read fHeight write fHeight;
property Pitch: TFontPitch read fPitch write fPitch;
property Style: TFontStylesBase read fStyle write fStyle;
property Charset: TFontCharset read fCharset write fCharset;
property FontName: TFontDataName read fFontName write fFontName;
property DrawWidth: integer read fDrawWidth write fDrawWidth;
property DrawHeight: integer read fDrawHeight write fDrawHeight;
property FontSize: integer read fFontSize write fFontSize;
property FrontColor: LongWord read fFrontColor write fFrontColor;
property BackColor: LongWord read fBackColor write fBackColor;
property GridWidth: Integer read fGridWidth write fGridWidth;
property GridHeight: Integer read fGridHeight write fGridHeight;
property Bold: Boolean read GetIsBold write SetIsBold;
property Italic: Boolean read GetIsItalic write SetIsItalic;
property Underline: Boolean read GetIsUnderline write SetIsUnderline;
property StrikeOut: Boolean read GetIsStrikeOut write SetIsStrikeOut;
end;
implementation
constructor TFontEngine.Create;
begin
inherited Create;
Reset;
end;
destructor TFontEngine.Destroy;
begin
inherited Destroy;
end;
procedure TFontEngine.Reset;
begin
fHeight := -11;
fPitch := fpDefault;
fStyle := [];
fCharset := DEFAULT_CHARSET;
fFontName := 'Doom';
fDrawWidth := 16;
fDrawHeight := 16;
fFontSize := 8;
fFrontColor := RGB(255, 255, 255);
fBackColor := RGB(0, 0, 0);
fGridWidth := 16;
fGridHeight := 16;
end;
procedure TFontEngine.DrawToCanvas(const C: TCanvas);
var
buf, letter: TBitmap;
bC, lC: TCanvas;
gx, gy: integer;
ch: Char;
begin
buf := TBitmap.Create;
buf.Width := fDrawWidth * fGridWidth;
buf.Height := fDrawHeight * fGridHeight;
buf.PixelFormat := pf32bit;
bC := buf.Canvas;
bC.Pen.Style := psClear;
bC.Pen.Color := fBackColor;
bC.Brush.Style := bsSolid;
bC.Brush.Color := fBackColor;
bC.FillRect(Rect(0, 0, fDrawWidth * fGridWidth, fDrawHeight * fGridHeight));
letter := TBitmap.Create;
letter.Width := fDrawWidth;
letter.Height := fDrawHeight;
letter.PixelFormat := pf32bit;
lC := letter.Canvas;
lC.Pen.Style := psClear;
lC.Pen.Color := fBackColor;
lC.Brush.Style := bsSolid;
lC.Brush.Color := fBackColor;
lC.Font.Height := fHeight;
lC.Font.Pitch := fPitch;
lC.Font.Style := fStyle;
lC.Font.Charset := fCharset;
lC.Font.Color := fFrontColor;
lC.Font.Name := fFontName;
lC.Font.Size := fFontSize;
ch := #0;
for gy := 0 to fGridHeight - 1 do
for gx := 0 to fGridWidth - 1 do
begin
lC.FillRect(Rect(0, 0, fDrawWidth, fDrawHeight));
lC.TextOut(0, 0, ch);
bC.Draw(gx * fDrawWidth, gy * fDrawHeight, letter);
Inc(ch);
end;
C.Draw(0, 0, buf);
buf.Free;
letter.Free;
end;
procedure TFontEngine.DrawToBitmap(const bm: TBitmap);
begin
bm.Width := fDrawWidth * fGridWidth;
bm.Height := fDrawHeight * fGridHeight;
bm.PixelFormat := pf32bit;
DrawToCanvas(bm.Canvas);
end;
procedure TFontEngine.DrawCharToCanvas(const C: TCanvas; const ch: Char);
var
letter: TBitmap;
lC: TCanvas;
begin
letter := TBitmap.Create;
letter.Width := fDrawWidth;
letter.Height := fDrawHeight;
letter.PixelFormat := pf32bit;
lC := letter.Canvas;
lC.Pen.Style := psClear;
lC.Pen.Color := fBackColor;
lC.Brush.Style := bsSolid;
lC.Brush.Color := fBackColor;
lC.Font.Height := fHeight;
lC.Font.Pitch := fPitch;
lC.Font.Style := fStyle;
lC.Font.Charset := fCharset;
lC.Font.Color := fFrontColor;
lC.Font.Name := fFontName;
lC.Font.Size := fFontSize;
lC.FillRect(Rect(0, 0, fDrawWidth, fDrawHeight));
lC.TextOut(0, 0, ch);
C.Draw(0, 0, letter);
letter.Free;
end;
procedure TFontEngine.DrawCharToBitmap(const bm: TBitmap; const ch: Char);
begin
bm.Width := fDrawWidth;
bm.Height := fDrawHeight;
bm.PixelFormat := pf32bit;
DrawCharToCanvas(bm.Canvas, ch);
end;
function TFontEngine.RightCropLetterBitmap(const bm: TBitmap): boolean;
const
BLANC_UNKNOWN = 0;
BLANC_NO = 1;
BLANC_YES = 2;
var
inds: PByteArray;
i: integer;
minwidth: integer;
function _Check_Column(const cl: integer): Byte;
var
y: integer;
begin
if inds[cl] <> BLANC_UNKNOWN then
begin
Result := inds[cl];
Exit;
end;
Result := BLANC_YES;
for y := 0 to bm.Height - 1 do
if bm.Canvas.Pixels[cl, y] <> fBackColor then
begin
Result := BLANC_NO;
Break;
end;
inds[cl] := Result;
end;
begin
Result := False;
minwidth := fFontSize div 2 - 1;
if minwidth < 3 then
minwidth := 3;
if bm.Width < minwidth then
Exit;
GetMem(inds, bm.Width);
for i := 0 to bm.Width - 1 do
inds[i] := BLANC_UNKNOWN;
for i := bm.Width - 1 downto minwidth do
begin
// Leave one blanc column on the right
if (_Check_Column(i) = BLANC_YES) and (_Check_Column(i - 1) = BLANC_YES) then
begin
bm.Width := bm.Width - 1;
Result := True;
end
else
Break;
end;
FreeMem(inds);
end;
function TFontEngine.DrawStringToBitmap(const bm: TBitmap; const s: string; const fixed_pitch: boolean): Integer;
var
letter: TBitmap;
i, w: integer;
bC: TCanvas;
begin
letter := TBitmap.Create;
w := 0;
for i := 1 to Length(s) do
begin
DrawCharToBitmap(letter, s[i]);
if not fixed_pitch then
RightCropLetterBitmap(letter);
bm.Canvas.Draw(w, 0, letter);
w := w + letter.Width;
end;
Result := w;
if w < bm.Width then
begin
bC := bm.Canvas;
bC.Pen.Style := psClear;
bC.Pen.Color := fBackColor;
bC.Brush.Style := bsSolid;
bC.Brush.Color := fBackColor;
bC.FillRect(Rect(w, 0, bm.Width, fDrawHeight));
end;
letter.Free;
end;
procedure TFontEngine.FromFont(const fnt: TFont);
begin
fHeight := fnt.Height;
fHeight := fnt.Height;
fPitch := fnt.Pitch;
fStyle := fnt.Style;
fCharset := fnt.Charset;
fFrontColor := fnt.Color;
fFontName := fnt.Name;
fFontSize := fnt.Size;
end;
procedure TFontEngine.ToFont(const fnt: TFont);
begin
fnt.Height := fHeight;
fnt.Pitch := fPitch;
fnt.Style := fStyle;
fnt.Charset := fCharset;
fnt.Color := fFrontColor;
fnt.Name := fFontName;
fnt.Size := fFontSize;
end;
const
sHeight = 'Height';
sPitch = 'Pitch';
sStyle = 'Style';
sCharset = 'Charset';
sFontName = 'FontName';
sDrawWidth = 'DrawWidth';
sDrawHeight = 'DrawHeight';
sFontSize = 'FontSize';
sFrontColor = 'FrontColor';
sBackColor = 'BackColor';
sGridWidth = 'GridWidth';
sGridHeight = 'GridHeight';
procedure TFontEngine.SaveToStream(const strm: TStream);
var
sl: TStringList;
stl: integer;
begin
sl := TStringList.Create;
try
sl.Add(sHeight + '=' + IntToStr(fHeight));
sl.Add(sPitch + '=' + IntToStr(Ord(fPitch)));
stl := 0;
if fsBold in fStyle then
stl := stl or 1;
if fsItalic in fStyle then
stl := stl or 2;
if fsUnderline in fStyle then
stl := stl or 4;
if fsStrikeOut in fStyle then
stl := stl or 8;
sl.Add(sStyle + '=' + IntToStr(stl));
sl.Add(sCharset + '=' + IntToStr(fCharset));
sl.Add(sFontName + '=' + fFontName);
sl.Add(sDrawWidth + '=' + IntToStr(fDrawWidth));
sl.Add(sDrawHeight + '=' + IntToStr(fDrawHeight));
sl.Add(sFontSize + '=' + IntToStr(fFontSize));
sl.Add(sFrontColor + '=' + IntToStr(fFrontColor));
sl.Add(sBackColor + '=' + IntToStr(fBackColor));
sl.Add(sGridWidth + '=' + IntToStr(fGridWidth));
sl.Add(sGridHeight + '=' + IntToStr(fGridHeight));
sl.SaveToStream(strm);
finally
sl.Free;
end;
end;
procedure TFontEngine.LoadFromStream(const strm: TStream);
var
sl: TStringList;
svalue: string;
tmp: integer;
procedure _load_int(const n: string; const pi: PInteger);
begin
svalue := sl.Values[n];
if svalue <> '' then
pi^ := StrToIntDef(svalue, pi^);
end;
begin
sl := TStringList.Create;
try
sl.LoadFromStream(strm);
_load_int(sHeight, @fHeight);
tmp := Ord(fPitch);
_load_int(sPitch, @tmp);
fPitch := TFontPitch(tmp);
tmp := 0;
if fsBold in fStyle then
tmp := tmp or 1;
if fsItalic in fStyle then
tmp := tmp or 2;
if fsUnderline in fStyle then
tmp := tmp or 4;
if fsStrikeOut in fStyle then
tmp := tmp or 8;
_load_int(sStyle, @tmp);
fStyle := [];
if tmp and 1 <> 0 then
Include(fStyle, fsBold);
if tmp and 2 <> 0 then
Include(fStyle, fsItalic);
if tmp and 4 <> 0 then
Include(fStyle, fsUnderline);
if tmp and 8 <> 0 then
Include(fStyle, fsStrikeOut);
tmp := fCharset;
_load_int(sCharset, @tmp);
fCharset := tmp;
svalue := sl.Values[sFontName];
if svalue <> '' then
fFontName := svalue;
_load_int(sDrawWidth, @fDrawWidth);
_load_int(sDrawHeight, @fDrawHeight);
_load_int(sFontSize, @fFontSize);
_load_int(sFrontColor, @fFrontColor);
_load_int(sBackColor, @fBackColor);
_load_int(sGridWidth, @fGridWidth);
_load_int(sGridHeight, @fGridHeight);
finally
sl.Free;
end;
end;
procedure TFontEngine.SaveToFile(const fn: string);
var
fs: TFileStream;
begin
fs := TFileStream.Create(fn, fmCreate);
try
SaveToStream(fs);
finally
fs.Free;
end;
end;
procedure TFontEngine.LoadFromFile(const fn: string);
var
fs: TFileStream;
begin
fs := TFileStream.Create(fn, fmOpenRead);
try
LoadFromStream(fs);
finally
fs.Free;
end;
end;
procedure TFontEngine.AttachTo(const ff: TFontEngine);
begin
fHeight := ff.Height;
fPitch := ff.Pitch;
fStyle := ff.Style;
fCharset := ff.Charset;
fFontName := ff.FontName;
fDrawWidth := ff.DrawWidth;
fDrawHeight := ff.DrawHeight;
fFontSize := ff.FontSize;
fFrontColor := ff.FrontColor;
fBackColor := ff.BackColor;
fGridWidth := ff.GridWidth;
fGridHeight := ff.GridHeight;
end;
function TFontEngine.GetIsBold: Boolean;
begin
Result := fsBold in fStyle;
end;
procedure TFontEngine.SetIsBold(const v: Boolean);
begin
if v then
Include(fStyle, fsBold)
else
Exclude(fStyle, fsBold);
end;
function TFontEngine.GetIsItalic: Boolean;
begin
Result := fsItalic in fStyle;
end;
procedure TFontEngine.SetIsItalic(const v: Boolean);
begin
if v then
Include(fStyle, fsItalic)
else
Exclude(fStyle, fsItalic);
end;
function TFontEngine.GetIsUnderline: Boolean;
begin
Result := fsUnderline in fStyle;
end;
procedure TFontEngine.SetIsUnderline(const v: Boolean);
begin
if v then
Include(fStyle, fsUnderline)
else
Exclude(fStyle, fsUnderline);
end;
function TFontEngine.GetIsStrikeOut: Boolean;
begin
Result := fsStrikeOut in fStyle;
end;
procedure TFontEngine.SetIsStrikeOut(const v: Boolean);
begin
if v then
Include(fStyle, fsStrikeOut)
else
Exclude(fStyle, fsStrikeOut);
end;
end.