-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmainform.pas
More file actions
3020 lines (2714 loc) · 83.8 KB
/
mainform.pas
File metadata and controls
3020 lines (2714 loc) · 83.8 KB
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
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
unit MainForm;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, ExtCtrls,
Spin, ComCtrls, Menus, LCLType, FONCreator, WinFont, DPFont, TEGLFont, AmigaFont, APLFont,
FontScript, ScriptManager;
const
ProgramName = 'RetroNick'#39's Bitmap Font Editor v1.2';
type
{ TfrmMain }
TfrmMain = class(TForm)
btnClearAll: TButton;
btnClearChar: TButton;
btnShiftLeft: TButton;
btnShiftRight: TButton;
btnShiftUp: TButton;
btnShiftDown: TButton;
btnInvert: TButton;
btnFlipH: TButton;
btnFlipV: TButton;
btnCopyChar: TButton;
btnPasteChar: TButton;
btnApplyRange: TButton;
btnResetLines: TButton;
btnScanLines: TButton;
cboBold: TCheckBox;
cboItalic: TCheckBox;
cboUnderline: TCheckBox;
cboShowGrid: TCheckBox;
cboShowBaseline: TCheckBox;
chkShowAscender: TCheckBox;
chkShowDescender: TCheckBox;
chkShowXHeight: TCheckBox;
cmbCharSet: TComboBox;
cmbPitchFamily: TComboBox;
cmbZoom: TComboBox;
edtCopyright: TEdit;
edtFontName: TEdit;
edtPreviewText: TEdit;
gbCharEditor: TGroupBox;
gbCharList: TGroupBox;
gbFontProps: TGroupBox;
gbPreview: TGroupBox;
gbLineMarkers: TGroupBox;
lblCharSet: TLabel;
lblCopyright: TLabel;
lblCurrentChar: TLabel;
lblFontName: TLabel;
lblHeight: TLabel;
lblPitchFamily: TLabel;
lblPointSize: TLabel;
lblZoom: TLabel;
lblPreviewText: TLabel;
lblCharWidth: TLabel;
lblRangeStart: TLabel;
lblRangeTo: TLabel;
lblLineValues: TLabel;
lstChars: TListBox;
MainMenu: TMainMenu;
mnuScript: TMenuItem;
mnuFile: TMenuItem;
mnuEdit: TMenuItem;
mnuTools: TMenuItem;
mnuHelp: TMenuItem;
mnuNew: TMenuItem;
mnuOpen: TMenuItem;
mnuSave: TMenuItem;
mnuSep1: TMenuItem;
mnuExit: TMenuItem;
mnuUndo: TMenuItem;
mnuRedo: TMenuItem;
mnuSep2: TMenuItem;
mnuCopy: TMenuItem;
mnuPaste: TMenuItem;
mnuGenerateFromFont: TMenuItem;
mnuSep4: TMenuItem;
mnuFontMetrics: TMenuItem;
mnuSep5: TMenuItem;
mnuIncreaseWidth: TMenuItem;
mnuDecreaseWidth: TMenuItem;
mnuSep6: TMenuItem;
mnuShiftLeft: TMenuItem;
mnuShiftRight: TMenuItem;
mnuSep7: TMenuItem;
mnuAutoTrim: TMenuItem;
mnuAbout: TMenuItem;
pnlCharEdit: TPaintBox;
pnlPreview: TPaintBox;
dlgSave: TSaveDialog;
dlgOpen: TOpenDialog;
sbCharEdit: TScrollBox;
mnuSep8: TMenuItem;
spnAscent: TSpinEdit;
spnHeight: TSpinEdit;
spnPointSize: TSpinEdit;
spnCharWidth: TSpinEdit;
spnRangeStart: TSpinEdit;
spnRangeEnd: TSpinEdit;
spnAscenderLine: TSpinEdit;
spnDescenderLine: TSpinEdit;
spnXHeightLine: TSpinEdit;
StatusBar: TStatusBar;
tmrPreview: TTimer;
procedure btnApplyRangeClick(Sender: TObject);
procedure btnClearAllClick(Sender: TObject);
procedure btnClearCharClick(Sender: TObject);
procedure btnCopyCharClick(Sender: TObject);
procedure btnFlipHClick(Sender: TObject);
procedure btnFlipVClick(Sender: TObject);
procedure btnInvertClick(Sender: TObject);
procedure btnLoadFontClick(Sender: TObject);
procedure btnPasteCharClick(Sender: TObject);
procedure btnSaveFontClick(Sender: TObject);
procedure btnShiftDownClick(Sender: TObject);
procedure btnShiftLeftClick(Sender: TObject);
procedure btnShiftRightClick(Sender: TObject);
procedure btnShiftUpClick(Sender: TObject);
procedure btnResetLinesClick(Sender: TObject);
procedure btnScanLinesClick(Sender: TObject);
procedure cboShowBaselineChange(Sender: TObject);
procedure cboShowGridChange(Sender: TObject);
procedure chkLineMarkerChange(Sender: TObject);
procedure mnuScriptClick(Sender: TObject);
procedure spnLineMarkerChange(Sender: TObject);
procedure cmbZoomChange(Sender: TObject);
procedure edtPreviewTextChange(Sender: TObject);
procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
procedure lstCharsClick(Sender: TObject);
procedure lstCharsDrawItem(Control: TWinControl; Index: Integer;
ARect: TRect; State: TOwnerDrawState);
procedure mnuAboutClick(Sender: TObject);
procedure mnuExitClick(Sender: TObject);
procedure mnuFontMetricsClick(Sender: TObject);
procedure mnuGenerateFromFontClick(Sender: TObject);
procedure mnuIncreaseWidthClick(Sender: TObject);
procedure mnuShiftLeftClick(Sender: TObject);
procedure mnuShiftRightClick(Sender: TObject);
procedure mnuDecreaseWidthClick(Sender: TObject);
procedure mnuAutoTrimClick(Sender: TObject);
procedure mnuNewClick(Sender: TObject);
procedure mnuUndoClick(Sender: TObject);
procedure mnuRedoClick(Sender: TObject);
procedure mnuScriptManagerClick(Sender: TObject);
procedure pnlCharEditMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure pnlCharEditMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
procedure pnlCharEditMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure pnlCharEditPaint(Sender: TObject);
procedure pnlPreviewPaint(Sender: TObject);
procedure spnCharWidthChange(Sender: TObject);
procedure spnHeightChange(Sender: TObject);
procedure tmrPreviewTimer(Sender: TObject);
private
FCreator: TFONCreator;
FCurrentChar: Integer;
FCharBitmaps: array[0..255] of TBitmap;
FUndoStack: array[0..255] of TList;
FRedoStack: array[0..255] of TList;
FPixelSize: Integer;
FDrawing: Boolean;
FDrawColor: Boolean;
FModified: Boolean;
FCurrentFile: string;
FShowGrid: Boolean;
FShowBaseline: Boolean;
FClipboardBitmap: TBitmap;
FInitializing: Boolean;
// Character range
FCharRangeStart: Integer;
FCharRangeEnd: Integer;
FCharEnabled: array[0..255] of Boolean;
// Line markers
FShowAscenderLine: Boolean;
FShowDescenderLine: Boolean;
FShowXHeightLine: Boolean;
FAscenderLine: Integer; // -1 = auto
FDescenderLine: Integer; // -1 = auto
FXHeightLine: Integer; // -1 = auto
// Remember last dialog filter
FLastOpenFilterIndex: Integer;
FLastSaveFilterIndex: Integer;
// Scripting support
FFontAPI: TFontScriptAPI;
FScriptManagerForm: TfrmScriptManager;
procedure UpdateCharList;
procedure UpdatePreview;
procedure UpdateStatus;
procedure UpdateTitle;
procedure UpdateLineMarkerDisplay;
procedure SetPixel(X, Y: Integer; Value: Boolean);
function GetPixelAt(SX, SY: Integer; out PX, PY: Integer): Boolean;
procedure EnsureCharBitmap(Idx: Integer);
procedure SaveUndoState;
procedure LoadFONFile(const FN: string);
procedure LoadFNTFile(const FN: string);
procedure SaveFNTFile(const FN: string);
procedure LoadDPFontFile(const FN: string);
procedure SaveDPFontFile(const FN: string);
procedure LoadTEGLFontFile(const FN: string);
procedure SaveTEGLFontFile(const FN: string);
procedure LoadAmigaFontFile(const FN: string);
procedure SaveAmigaFontFile(const FN: string);
procedure LoadAPLFontFile(const FN: string);
procedure SaveAPLFontFile(const FN: string);
function TryLoadAPLFont(const FN: string): Boolean;
procedure AutoLoadFontFile(const FN: string);
function TryLoadTEGLFont(const FN: string): Boolean;
procedure ResizeCharBitmap(Idx, NewW, NewH: Integer);
procedure UpdateEditorSize;
function ConfirmSave: Boolean;
procedure SetModified(V: Boolean);
procedure ClearUndoRedo(Idx: Integer);
procedure ApplyCharRange;
procedure ScanFontLines(out AscenderY, DescenderY, XHeightY: Integer);
// Script API callbacks
function ScriptGetPixel(CharCode, X, Y: Integer): Boolean;
procedure ScriptSetPixel(CharCode, X, Y: Integer; Value: Boolean);
function ScriptGetCharWidth(CharCode: Integer): Integer;
procedure ScriptSetCharWidth(CharCode, AWidth: Integer);
function ScriptGetCharBitmap(CharCode: Integer): TBitmap;
procedure ScriptSetCharBitmap(CharCode: Integer; Bmp: TBitmap);
procedure ScriptRefresh;
procedure ScriptSaveUndo(CharCode: Integer);
procedure ScriptSelectChar(CharCode: Integer);
procedure ScriptMarkModified;
function ScriptGetCurrentChar: Integer;
function ScriptGetFontHeight: Integer;
function ScriptGetFontName: string;
procedure ScriptSetFontName(const AName: string);
function ScriptGetRangeStart: Integer;
function ScriptGetRangeEnd: Integer;
procedure ScriptSetRange(StartChar, EndChar: Integer);
procedure ScriptPrint(const Msg: string);
end;
var
frmMain: TfrmMain;
implementation
{$R *.lfm}
procedure TfrmMain.FormCreate(Sender: TObject);
var
I: Integer;
begin
FInitializing := True;
FCreator := TFONCreator.Create;
FCurrentChar := 65;
FPixelSize := 16;
FDrawing := False;
FModified := False;
FCurrentFile := '';
FShowGrid := True;
FShowBaseline := True;
FClipboardBitmap := nil;
// Character range
FCharRangeStart := 32;
FCharRangeEnd := 127;
// Line markers
FShowAscenderLine := True;
FShowDescenderLine := True;
FShowXHeightLine := True;
FAscenderLine := -1;
FDescenderLine := -1;
FXHeightLine := -1;
// Default filter indices
FLastOpenFilterIndex := 1;
FLastSaveFilterIndex := 1;
for I := 0 to 255 do
begin
FCharBitmaps[I] := nil;
FUndoStack[I] := TList.Create;
FRedoStack[I] := TList.Create;
FCharEnabled[I] := (I >= 32) and (I <= 127);
end;
cmbCharSet.Items.AddStrings(['ANSI (0)', 'Default (1)', 'Symbol (2)', 'OEM (255)']);
cmbCharSet.ItemIndex := 0;
cmbPitchFamily.Items.AddStrings(['Default', 'Fixed', 'Variable', 'Roman', 'Swiss', 'Modern', 'Script', 'Decorative']);
cmbPitchFamily.ItemIndex := 2;
cmbZoom.Items.AddStrings(['8x', '12x', '16x', '20x', '24x', '32x']);
cmbZoom.ItemIndex := 2;
edtFontName.Text := 'MyFont';
edtCopyright.Text := 'RetroNicks Bitmap Font Editor';
spnPointSize.Value := 10;
spnHeight.Value := 12;
spnAscent.Value := 10;
edtPreviewText.Text := 'Hello World! ABC abc 123';
cboShowGrid.Checked := True;
cboShowBaseline.Checked := True;
for I := 32 to 127 do EnsureCharBitmap(I);
lstChars.Style := lbOwnerDrawFixed;
lstChars.ItemHeight := 20;
UpdateCharList;
UpdateStatus;
UpdateTitle;
UpdateEditorSize;
UpdateLineMarkerDisplay;
pnlCharEdit.Repaint;
FInitializing := False;
// Initialize scripting API
FFontAPI := TFontScriptAPI.Create;
FFontAPI.OnGetPixel := @ScriptGetPixel;
FFontAPI.OnSetPixel := @ScriptSetPixel;
FFontAPI.OnGetCharWidth := @ScriptGetCharWidth;
FFontAPI.OnSetCharWidth := @ScriptSetCharWidth;
FFontAPI.OnGetCharBitmap := @ScriptGetCharBitmap;
FFontAPI.OnSetCharBitmap := @ScriptSetCharBitmap;
FFontAPI.OnRefresh := @ScriptRefresh;
FFontAPI.OnSaveUndo := @ScriptSaveUndo;
FFontAPI.OnSelectChar := @ScriptSelectChar;
FFontAPI.OnMarkModified := @ScriptMarkModified;
FFontAPI.OnGetCurrentChar := @ScriptGetCurrentChar;
FFontAPI.OnGetFontHeight := @ScriptGetFontHeight;
FFontAPI.OnGetFontName := @ScriptGetFontName;
FFontAPI.OnSetFontName := @ScriptSetFontName;
FFontAPI.OnGetRangeStart := @ScriptGetRangeStart;
FFontAPI.OnGetRangeEnd := @ScriptGetRangeEnd;
FFontAPI.OnSetRange := @ScriptSetRange;
FFontAPI.OnPrint := @ScriptPrint;
FScriptManagerForm := nil;
end;
procedure TfrmMain.FormDestroy(Sender: TObject);
var
I, J: Integer;
begin
FCreator.Free;
for I := 0 to 255 do
begin
if FCharBitmaps[I] <> nil then FCharBitmaps[I].Free;
for J := 0 to FUndoStack[I].Count - 1 do TBitmap(FUndoStack[I][J]).Free;
FUndoStack[I].Free;
for J := 0 to FRedoStack[I].Count - 1 do TBitmap(FRedoStack[I][J]).Free;
FRedoStack[I].Free;
end;
if FClipboardBitmap <> nil then FClipboardBitmap.Free;
FFontAPI.Free;
// FScriptManagerForm is freed automatically as owned form
end;
procedure TfrmMain.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
if not ConfirmSave then CloseAction := caNone;
end;
function TfrmMain.ConfirmSave: Boolean;
var
R: TModalResult;
begin
Result := True;
if FModified then
begin
R := MessageDlg('Save Changes?', 'Font modified. Save?', mtConfirmation, [mbYes, mbNo, mbCancel], 0);
case R of
mrYes: begin btnSaveFontClick(nil); Result := not FModified; end;
mrNo: Result := True;
mrCancel: Result := False;
end;
end;
end;
procedure TfrmMain.SetModified(V: Boolean);
begin
FModified := V;
UpdateTitle;
end;
procedure TfrmMain.UpdateTitle;
begin
if FCurrentFile <> '' then
Caption := ProgramName+' - ' + ExtractFileName(FCurrentFile)
else
Caption := ProgramName+' - [Untitled]';
if FModified then Caption := Caption + ' *';
end;
procedure TfrmMain.EnsureCharBitmap(Idx: Integer);
begin
if FCharBitmaps[Idx] = nil then
begin
FCharBitmaps[Idx] := TBitmap.Create;
FCharBitmaps[Idx].Width := 8;
FCharBitmaps[Idx].Height := spnHeight.Value;
FCharBitmaps[Idx].Canvas.Brush.Color := clWhite;
FCharBitmaps[Idx].Canvas.FillRect(0, 0, 8, spnHeight.Value);
end;
end;
procedure TfrmMain.ClearUndoRedo(Idx: Integer);
var
I: Integer;
begin
for I := 0 to FUndoStack[Idx].Count - 1 do TBitmap(FUndoStack[Idx][I]).Free;
FUndoStack[Idx].Clear;
for I := 0 to FRedoStack[Idx].Count - 1 do TBitmap(FRedoStack[Idx][I]).Free;
FRedoStack[Idx].Clear;
end;
procedure TfrmMain.SaveUndoState;
var
Bmp, Copy: TBitmap;
begin
Bmp := FCharBitmaps[FCurrentChar];
if Bmp = nil then Exit;
Copy := TBitmap.Create;
Copy.Assign(Bmp);
FUndoStack[FCurrentChar].Add(Copy);
if FUndoStack[FCurrentChar].Count > 50 then
begin
TBitmap(FUndoStack[FCurrentChar][0]).Free;
FUndoStack[FCurrentChar].Delete(0);
end;
while FRedoStack[FCurrentChar].Count > 0 do
begin
TBitmap(FRedoStack[FCurrentChar][FRedoStack[FCurrentChar].Count - 1]).Free;
FRedoStack[FCurrentChar].Delete(FRedoStack[FCurrentChar].Count - 1);
end;
end;
procedure TfrmMain.mnuUndoClick(Sender: TObject);
var
Bmp, UndoBmp, RedoCopy: TBitmap;
begin
if FUndoStack[FCurrentChar].Count = 0 then Exit;
Bmp := FCharBitmaps[FCurrentChar];
if Bmp = nil then Exit;
RedoCopy := TBitmap.Create;
RedoCopy.Assign(Bmp);
FRedoStack[FCurrentChar].Add(RedoCopy);
UndoBmp := TBitmap(FUndoStack[FCurrentChar][FUndoStack[FCurrentChar].Count - 1]);
FUndoStack[FCurrentChar].Delete(FUndoStack[FCurrentChar].Count - 1);
Bmp.Assign(UndoBmp);
UndoBmp.Free;
pnlCharEdit.Repaint;
UpdatePreview;
SetModified(True);
end;
procedure TfrmMain.mnuRedoClick(Sender: TObject);
var
Bmp, RedoBmp, UndoCopy: TBitmap;
begin
if FRedoStack[FCurrentChar].Count = 0 then Exit;
Bmp := FCharBitmaps[FCurrentChar];
if Bmp = nil then Exit;
UndoCopy := TBitmap.Create;
UndoCopy.Assign(Bmp);
FUndoStack[FCurrentChar].Add(UndoCopy);
RedoBmp := TBitmap(FRedoStack[FCurrentChar][FRedoStack[FCurrentChar].Count - 1]);
FRedoStack[FCurrentChar].Delete(FRedoStack[FCurrentChar].Count - 1);
Bmp.Assign(RedoBmp);
RedoBmp.Free;
pnlCharEdit.Repaint;
UpdatePreview;
SetModified(True);
end;
procedure TfrmMain.UpdateCharList;
var
I, SelIdx: Integer;
begin
lstChars.Items.BeginUpdate;
try
lstChars.Items.Clear;
SelIdx := -1;
for I := 0 to 255 do
begin
if FCharEnabled[I] then
begin
lstChars.Items.AddObject(IntToStr(I), TObject(PtrInt(I)));
if I = FCurrentChar then
SelIdx := lstChars.Items.Count - 1;
end;
end;
lstChars.ItemIndex := SelIdx;
finally
lstChars.Items.EndUpdate;
end;
end;
procedure TfrmMain.ApplyCharRange;
var
I: Integer;
begin
for I := 0 to 255 do
FCharEnabled[I] := (I >= FCharRangeStart) and (I <= FCharRangeEnd);
// Initialize bitmaps for enabled characters
for I := 0 to 255 do
if FCharEnabled[I] then
EnsureCharBitmap(I);
// Make sure current char is in range
if not FCharEnabled[FCurrentChar] then
begin
FCurrentChar := FCharRangeStart;
EnsureCharBitmap(FCurrentChar);
end;
UpdateCharList;
lstCharsClick(nil);
end;
procedure TfrmMain.btnApplyRangeClick(Sender: TObject);
begin
FCharRangeStart := spnRangeStart.Value;
FCharRangeEnd := spnRangeEnd.Value;
ApplyCharRange;
StatusBar.Panels[0].Text := Format('Character range: %d to %d', [FCharRangeStart, FCharRangeEnd]);
end;
procedure TfrmMain.lstCharsDrawItem(Control: TWinControl; Index: Integer;
ARect: TRect; State: TOwnerDrawState);
var
CC: Integer;
S: string;
Bmp: TBitmap;
X, Y, PS: Integer;
HasContent: Boolean;
begin
if (Index < 0) or (Index >= lstChars.Items.Count) then Exit;
CC := PtrInt(lstChars.Items.Objects[Index]);
with lstChars.Canvas do
begin
if odSelected in State then Brush.Color := clHighlight else Brush.Color := clWindow;
FillRect(ARect);
if odSelected in State then Font.Color := clHighlightText else Font.Color := clWindowText;
if (CC >= 32) and (CC < 127) then S := Format('%3d ''%s''', [CC, Chr(CC)])
else if CC = 127 then S := '127 DEL'
else S := Format('%3d #%d', [CC, CC]);
TextOut(ARect.Left + 4, ARect.Top + 2, S);
Bmp := FCharBitmaps[CC];
if Bmp <> nil then
begin
PS := ARect.Bottom - ARect.Top - 4;
HasContent := False;
for Y := 0 to Bmp.Height - 1 do
for X := 0 to Bmp.Width - 1 do
if Bmp.Canvas.Pixels[X, Y] = clBlack then begin HasContent := True; Break; end;
if HasContent then
StretchDraw(Rect(ARect.Right - PS - 4, ARect.Top + 2, ARect.Right - 4, ARect.Bottom - 2), Bmp)
else begin Font.Color := clGray; TextOut(ARect.Right - 45, ARect.Top + 2, '(empty)'); end;
end;
end;
end;
procedure TfrmMain.lstCharsClick(Sender: TObject);
begin
if (lstChars.ItemIndex >= 0) and (lstChars.ItemIndex < lstChars.Items.Count) then
begin
FCurrentChar := PtrInt(lstChars.Items.Objects[lstChars.ItemIndex]);
EnsureCharBitmap(FCurrentChar);
if (FCurrentChar >= 32) and (FCurrentChar < 127) then
lblCurrentChar.Caption := Format('Editing: %d ''%s''', [FCurrentChar, Chr(FCurrentChar)])
else
lblCurrentChar.Caption := Format('Editing: %d', [FCurrentChar]);
spnCharWidth.Value := FCharBitmaps[FCurrentChar].Width;
UpdateEditorSize;
pnlCharEdit.Repaint;
UpdatePreview;
UpdateStatus;
end;
end;
procedure TfrmMain.UpdateEditorSize;
var
Bmp: TBitmap;
begin
Bmp := FCharBitmaps[FCurrentChar];
if Bmp <> nil then
begin
pnlCharEdit.Width := Bmp.Width * FPixelSize + 1;
pnlCharEdit.Height := Bmp.Height * FPixelSize + 1;
end;
end;
procedure TfrmMain.cmbZoomChange(Sender: TObject);
begin
if FInitializing then Exit;
case cmbZoom.ItemIndex of
0: FPixelSize := 8; 1: FPixelSize := 12; 2: FPixelSize := 16;
3: FPixelSize := 20; 4: FPixelSize := 24; 5: FPixelSize := 32;
else FPixelSize := 16;
end;
UpdateEditorSize;
pnlCharEdit.Repaint;
end;
procedure TfrmMain.cboShowGridChange(Sender: TObject);
begin
FShowGrid := cboShowGrid.Checked;
pnlCharEdit.Repaint;
end;
procedure TfrmMain.cboShowBaselineChange(Sender: TObject);
begin
FShowBaseline := cboShowBaseline.Checked;
pnlCharEdit.Repaint;
end;
procedure TfrmMain.pnlCharEditPaint(Sender: TObject);
var
X, Y, CX, CY, GW, GH: Integer;
Bmp: TBitmap;
PB: TPaintBox;
BlackCount: Integer;
begin
PB := TPaintBox(Sender);
Bmp := FCharBitmaps[FCurrentChar];
// Always draw yellow first to confirm paint is firing
PB.Canvas.Brush.Color := clYellow;
PB.Canvas.FillRect(0, 0, PB.Width, PB.Height);
if Bmp = nil then
begin
PB.Canvas.Font.Color := clBlack;
PB.Canvas.TextOut(5, PB.Height - 20, 'ERROR: Bmp is nil for char ' + IntToStr(FCurrentChar));
Exit;
end;
// Count black pixels
BlackCount := 0;
for Y := 0 to Bmp.Height - 1 do
for X := 0 to Bmp.Width - 1 do
if Bmp.Canvas.Pixels[X, Y] = clBlack then Inc(BlackCount);
GW := Bmp.Width * FPixelSize;
GH := Bmp.Height * FPixelSize;
// Draw pixels
for Y := 0 to Bmp.Height - 1 do
for X := 0 to Bmp.Width - 1 do
begin
CX := X * FPixelSize;
CY := Y * FPixelSize;
if Bmp.Canvas.Pixels[X, Y] = clBlack then
PB.Canvas.Brush.Color := clBlack
else
PB.Canvas.Brush.Color := clWhite;
PB.Canvas.FillRect(CX, CY, CX + FPixelSize, CY + FPixelSize);
end;
if FShowGrid then
begin
PB.Canvas.Pen.Color := clSilver;
PB.Canvas.Pen.Style := psSolid;
for X := 0 to Bmp.Width do
begin
PB.Canvas.MoveTo(X * FPixelSize, 0);
PB.Canvas.LineTo(X * FPixelSize, GH);
end;
for Y := 0 to Bmp.Height do
begin
PB.Canvas.MoveTo(0, Y * FPixelSize);
PB.Canvas.LineTo(GW, Y * FPixelSize);
end;
end;
// Line markers
PB.Canvas.Pen.Style := psSolid;
PB.Canvas.Pen.Width := 2;
// Ascender line (Cyan)
if FShowAscenderLine then
begin
PB.Canvas.Pen.Color := clTeal;
if FAscenderLine >= 0 then
Y := FAscenderLine
else
Y := 0;
PB.Canvas.MoveTo(0, Y * FPixelSize);
PB.Canvas.LineTo(GW, Y * FPixelSize);
end;
// X-Height line (Purple)
if FShowXHeightLine then
begin
PB.Canvas.Pen.Color := clPurple;
if FXHeightLine >= 0 then
Y := FXHeightLine
else
Y := Round(spnAscent.Value * 0.6);
PB.Canvas.MoveTo(0, Y * FPixelSize);
PB.Canvas.LineTo(GW, Y * FPixelSize);
end;
// Baseline (Red)
if FShowBaseline then
begin
PB.Canvas.Pen.Color := clRed;
Y := spnAscent.Value;
PB.Canvas.MoveTo(0, Y * FPixelSize);
PB.Canvas.LineTo(GW, Y * FPixelSize);
end;
// Descender line (Orange)
if FShowDescenderLine then
begin
PB.Canvas.Pen.Color := $0080FF; // Orange
if FDescenderLine >= 0 then
Y := FDescenderLine
else
Y := Bmp.Height;
PB.Canvas.MoveTo(0, Y * FPixelSize);
PB.Canvas.LineTo(GW, Y * FPixelSize);
end;
//Draw Frame
PB.Canvas.Pen.Width := 1;
PB.Canvas.Pen.Color := clSilver;
PB.Canvas.Brush.Style := bsClear;
PB.Canvas.Rectangle(0, 0, PB.Width, PB.Height);
end;
function TfrmMain.GetPixelAt(SX, SY: Integer; out PX, PY: Integer): Boolean;
var
Bmp: TBitmap;
begin
Result := False;
Bmp := FCharBitmaps[FCurrentChar];
if Bmp = nil then Exit;
PX := SX div FPixelSize;
PY := SY div FPixelSize;
Result := (PX >= 0) and (PX < Bmp.Width) and (PY >= 0) and (PY < Bmp.Height);
end;
procedure TfrmMain.SetPixel(X, Y: Integer; Value: Boolean);
var
Bmp: TBitmap;
begin
Bmp := FCharBitmaps[FCurrentChar];
if Bmp = nil then Exit;
if (X >= 0) and (X < Bmp.Width) and (Y >= 0) and (Y < Bmp.Height) then
begin
if Value then Bmp.Canvas.Pixels[X, Y] := clBlack else Bmp.Canvas.Pixels[X, Y] := clWhite;
pnlCharEdit.Repaint;
tmrPreview.Enabled := True;
SetModified(True);
end;
end;
procedure TfrmMain.pnlCharEditMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var
PX, PY: Integer;
Bmp: TBitmap;
begin
if GetPixelAt(X, Y, PX, PY) then
begin
SaveUndoState;
Bmp := FCharBitmaps[FCurrentChar];
if Button = mbLeft then begin FDrawColor := Bmp.Canvas.Pixels[PX, PY] <> clBlack; SetPixel(PX, PY, FDrawColor); end
else if Button = mbRight then begin FDrawColor := False; SetPixel(PX, PY, False); end;
FDrawing := True;
end;
end;
procedure TfrmMain.pnlCharEditMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
var
PX, PY: Integer;
begin
if GetPixelAt(X, Y, PX, PY) then StatusBar.Panels[1].Text := Format('Pixel: %d, %d', [PX, PY])
else StatusBar.Panels[1].Text := '';
if FDrawing then
begin
if (ssLeft in Shift) and GetPixelAt(X, Y, PX, PY) then SetPixel(PX, PY, FDrawColor)
else if (ssRight in Shift) and GetPixelAt(X, Y, PX, PY) then SetPixel(PX, PY, False)
else if not ((ssLeft in Shift) or (ssRight in Shift)) then FDrawing := False;
end;
end;
procedure TfrmMain.pnlCharEditMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
FDrawing := False;
UpdatePreview;
lstChars.Invalidate;
end;
procedure TfrmMain.tmrPreviewTimer(Sender: TObject);
begin
tmrPreview.Enabled := False;
UpdatePreview;
lstChars.Invalidate;
end;
procedure TfrmMain.edtPreviewTextChange(Sender: TObject);
begin
UpdatePreview;
end;
procedure TfrmMain.pnlPreviewPaint(Sender: TObject);
var
I, X, Y, CharX, Scale: Integer;
Bmp: TBitmap;
PB: TPaintBox;
begin
PB := TPaintBox(Sender);
Scale := 2;
CharX := 8;
with PB.Canvas do
begin
Brush.Color := clWhite;
FillRect(0, 0, PB.Width, PB.Height);
for I := 1 to Length(edtPreviewText.Text) do
begin
Bmp := FCharBitmaps[Ord(edtPreviewText.Text[I])];
if Bmp <> nil then
begin
for Y := 0 to Bmp.Height - 1 do
for X := 0 to Bmp.Width - 1 do
if Bmp.Canvas.Pixels[X, Y] = clBlack then
begin
Brush.Color := clBlack;
FillRect(CharX + X * Scale, 8 + Y * Scale, CharX + X * Scale + Scale, 8 + Y * Scale + Scale);
end;
CharX := CharX + (Bmp.Width + 1) * Scale;
end
else CharX := CharX + 8 * Scale;
if CharX > PB.Width - 20 then Break;
end;
if FShowBaseline then
begin
Pen.Color := clRed; Pen.Style := psDot;
MoveTo(0, 8 + spnAscent.Value * Scale);
LineTo(PB.Width, 8 + spnAscent.Value * Scale);
end;
end;
end;
procedure TfrmMain.UpdatePreview;
begin
pnlPreview.Invalidate;
end;
procedure TfrmMain.spnHeightChange(Sender: TObject);
var
I, NH: Integer;
OldBmp, NewBmp: TBitmap;
begin
if FInitializing then Exit;
NH := spnHeight.Value;
for I := 0 to 255 do
if FCharBitmaps[I] <> nil then
begin
OldBmp := FCharBitmaps[I];
if OldBmp.Height <> NH then
begin
NewBmp := TBitmap.Create;
NewBmp.Width := OldBmp.Width;
NewBmp.Height := NH;
NewBmp.Canvas.Brush.Color := clWhite;
NewBmp.Canvas.FillRect(0, 0, NewBmp.Width, NH);
NewBmp.Canvas.Draw(0, 0, OldBmp);
OldBmp.Free;
FCharBitmaps[I] := NewBmp;
end;
end;
if spnAscent.Value > NH then spnAscent.Value := NH;
spnAscent.MaxValue := NH;
UpdateEditorSize;
pnlCharEdit.Repaint;
UpdatePreview;
SetModified(True);
end;
procedure TfrmMain.spnCharWidthChange(Sender: TObject);
begin
if FInitializing then Exit;
ResizeCharBitmap(FCurrentChar, spnCharWidth.Value, spnHeight.Value);
UpdateEditorSize;
pnlCharEdit.Repaint;
UpdatePreview;
lstChars.Invalidate;
SetModified(True);
end;
procedure TfrmMain.ResizeCharBitmap(Idx, NewW, NewH: Integer);
var
OldBmp, NewBmp: TBitmap;
begin
if FCharBitmaps[Idx] = nil then begin EnsureCharBitmap(Idx); FCharBitmaps[Idx].Width := NewW; FCharBitmaps[Idx].Height := NewH; Exit; end;
OldBmp := FCharBitmaps[Idx];
if (OldBmp.Width = NewW) and (OldBmp.Height = NewH) then Exit;
NewBmp := TBitmap.Create;
NewBmp.Width := NewW;
NewBmp.Height := NewH;
NewBmp.Canvas.Brush.Color := clWhite;
NewBmp.Canvas.FillRect(0, 0, NewW, NewH);
NewBmp.Canvas.Draw(0, 0, OldBmp);
OldBmp.Free;
FCharBitmaps[Idx] := NewBmp;
end;
procedure TfrmMain.btnClearCharClick(Sender: TObject);
var
Bmp: TBitmap;
begin
Bmp := FCharBitmaps[FCurrentChar];
if Bmp <> nil then
begin
SaveUndoState;
Bmp.Canvas.Brush.Color := clWhite;
Bmp.Canvas.FillRect(0, 0, Bmp.Width, Bmp.Height);
pnlCharEdit.Repaint;
UpdatePreview;
lstChars.Invalidate;
SetModified(True);
end;
end;
procedure TfrmMain.btnClearAllClick(Sender: TObject);
var
I: Integer;
begin
if MessageDlg('Clear All', 'Clear all characters?', mtConfirmation, [mbYes, mbNo], 0) = mrYes then
begin
for I := 0 to 255 do
begin
ClearUndoRedo(I);
if FCharBitmaps[I] <> nil then
begin
FCharBitmaps[I].Canvas.Brush.Color := clWhite;
FCharBitmaps[I].Canvas.FillRect(0, 0, FCharBitmaps[I].Width, FCharBitmaps[I].Height);
end;
end;
pnlCharEdit.Repaint;
UpdatePreview;
lstChars.Invalidate;
SetModified(True);
end;
end;
procedure TfrmMain.btnCopyCharClick(Sender: TObject);
var
Bmp: TBitmap;
begin
Bmp := FCharBitmaps[FCurrentChar];
if Bmp = nil then Exit;
if FClipboardBitmap <> nil then FClipboardBitmap.Free;
FClipboardBitmap := TBitmap.Create;
FClipboardBitmap.Assign(Bmp);
StatusBar.Panels[0].Text := Format('Copied char %d', [FCurrentChar]);
end;
procedure TfrmMain.btnPasteCharClick(Sender: TObject);
var
Bmp: TBitmap;
begin
if FClipboardBitmap = nil then begin StatusBar.Panels[0].Text := 'Nothing to paste'; Exit; end;
EnsureCharBitmap(FCurrentChar);
Bmp := FCharBitmaps[FCurrentChar];
SaveUndoState;
if (Bmp.Width <> FClipboardBitmap.Width) or (Bmp.Height <> FClipboardBitmap.Height) then
begin
Bmp.Width := FClipboardBitmap.Width;
Bmp.Height := FClipboardBitmap.Height;
spnCharWidth.Value := Bmp.Width;
end;
Bmp.Assign(FClipboardBitmap);
UpdateEditorSize;
pnlCharEdit.Repaint;