-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFontEditor.pas
More file actions
435 lines (382 loc) · 10.7 KB
/
FontEditor.pas
File metadata and controls
435 lines (382 loc) · 10.7 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
unit FontEditor;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes,
Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls,
System.JSON,System.UITypes, System.Generics.Collections, ConfigEditors,
ConfigEditorsBase, JSONHelpers;
type
TFontEditor = class(TForm)
pnlMain: TPanel;
pnlPreview: TPanel;
lblPreview: TLabel;
pnlSettings: TPanel;
lblFontName: TLabel;
lblFontSize: TLabel;
lblFontStyle: TLabel;
lblFontColor: TLabel;
edtFontName: TEdit;
edtFontSize: TEdit;
pnlColor: TPanel;
chkBold: TCheckBox;
chkItalic: TCheckBox;
chkUnderline: TCheckBox;
chkStrikeout: TCheckBox;
btnSelectFont: TButton;
btnDefault: TButton;
btnApply: TButton;
dlgFont: TFontDialog;
dlgColor: TColorDialog;
procedure FormCreate(Sender: TObject);
procedure btnSelectFontClick(Sender: TObject);
procedure pnlColorClick(Sender: TObject);
procedure chkBoldClick(Sender: TObject);
procedure chkItalicClick(Sender: TObject);
procedure chkUnderlineClick(Sender: TObject);
procedure chkStrikeoutClick(Sender: TObject);
procedure btnDefaultClick(Sender: TObject);
procedure btnApplyClick(Sender: TObject);
private
{ Private declarations }
FFontName: string;
FFontSize: Integer;
FFontColor: TColor;
FFontStyles: TFontStyles;
FJSONObject: TJSONObject;
FModified: Boolean;
procedure SetFontName(const Value: string);
procedure SetFontSize(const Value: Integer);
procedure SetFontColor(const Value: TColor);
procedure SetFontStyles(const Value: TFontStyles);
procedure UpdatePreview;
procedure LoadFontFromJSON(AJSONObject: TJSONObject);
procedure SaveFontToJSON(AJSONObject: TJSONObject);
public
{ Public declarations }
property FontName: string read FFontName write SetFontName;
property FontSize: Integer read FFontSize write SetFontSize;
property FontColor: TColor read FFontColor write SetFontColor;
property FontStyles: TFontStyles read FFontStyles write SetFontStyles;
property JSONObject: TJSONObject read FJSONObject write FJSONObject;
property Modified: Boolean read FModified write FModified;
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
end;
TFontConfigEditor = class(TConfigEditorBase)
private
FFontEditor: TFontEditor;
public
constructor Create; override;
destructor Destroy; override;
// IConfigEditor接口实现
function GetEditorType: TConfigType; override;
procedure Load(const Config: TJSONObject); override;
function Save: TJSONObject; override;
end;
// 创建字体编辑器的工厂函数
function CreateFontEditor: IConfigEditor;
implementation
{$R *.dfm}
// 工厂函数实现
function CreateFontEditor: IConfigEditor;
begin
Result := TFontConfigEditor.Create;
end;
{ TFontEditor }
constructor TFontEditor.Create(AOwner: TComponent);
begin
inherited;
FModified := False;
FFontName := 'Tahoma';
FFontSize := 10;
FFontColor := clBlack;
FFontStyles := [];
end;
destructor TFontEditor.Destroy;
begin
inherited;
end;
procedure TFontEditor.FormCreate(Sender: TObject);
begin
// 初始化界面
edtFontName.Text := FFontName;
edtFontSize.Text := IntToStr(FFontSize);
pnlColor.Color := FFontColor;
chkBold.Checked := fsBold in FFontStyles;
chkItalic.Checked := fsItalic in FFontStyles;
chkUnderline.Checked := fsUnderline in FFontStyles;
chkStrikeout.Checked := fsStrikeOut in FFontStyles;
UpdatePreview;
end;
procedure TFontEditor.SetFontColor(const Value: TColor);
begin
if FFontColor <> Value then
begin
FFontColor := Value;
pnlColor.Color := Value;
UpdatePreview;
FModified := True;
end;
end;
procedure TFontEditor.SetFontName(const Value: string);
begin
if FFontName <> Value then
begin
FFontName := Value;
edtFontName.Text := Value;
UpdatePreview;
FModified := True;
end;
end;
procedure TFontEditor.SetFontSize(const Value: Integer);
begin
if FFontSize <> Value then
begin
FFontSize := Value;
edtFontSize.Text := IntToStr(Value);
UpdatePreview;
FModified := True;
end;
end;
procedure TFontEditor.SetFontStyles(const Value: TFontStyles);
begin
if FFontStyles <> Value then
begin
FFontStyles := Value;
chkBold.Checked := fsBold in Value;
chkItalic.Checked := fsItalic in Value;
chkUnderline.Checked := fsUnderline in Value;
chkStrikeout.Checked := fsStrikeOut in Value;
UpdatePreview;
FModified := True;
end;
end;
procedure TFontEditor.UpdatePreview;
begin
// 更新预览标签的字体设置
lblPreview.Font.Name := FFontName;
lblPreview.Font.Size := FFontSize;
lblPreview.Font.Color := FFontColor;
lblPreview.Font.Style := FFontStyles;
end;
procedure TFontEditor.btnSelectFontClick(Sender: TObject);
begin
// 设置字体对话框的初始值
dlgFont.Font.Name := FFontName;
dlgFont.Font.Size := FFontSize;
dlgFont.Font.Color := FFontColor;
dlgFont.Font.Style := FFontStyles;
if dlgFont.Execute then
begin
// 更新字体属性
SetFontName(dlgFont.Font.Name);
SetFontSize(dlgFont.Font.Size);
SetFontColor(dlgFont.Font.Color);
SetFontStyles(dlgFont.Font.Style);
end;
end;
procedure TFontEditor.btnDefaultClick(Sender: TObject);
begin
// 恢复默认设置
SetFontName('Tahoma');
SetFontSize(10);
SetFontColor(clBlack);
SetFontStyles([]);
end;
procedure TFontEditor.btnApplyClick(Sender: TObject);
begin
// 应用更改,保存到JSON对象
if Assigned(FJSONObject) then
begin
SaveFontToJSON(FJSONObject);
Self.ModalResult := mrOk;
end;
end;
procedure TFontEditor.chkBoldClick(Sender: TObject);
var
NewStyles: TFontStyles;
begin
NewStyles := FFontStyles;
if chkBold.Checked then
Include(NewStyles, fsBold)
else
Exclude(NewStyles, fsBold);
SetFontStyles(NewStyles);
end;
procedure TFontEditor.chkItalicClick(Sender: TObject);
var
NewStyles: TFontStyles;
begin
NewStyles := FFontStyles;
if chkItalic.Checked then
Include(NewStyles, fsItalic)
else
Exclude(NewStyles, fsItalic);
SetFontStyles(NewStyles);
end;
procedure TFontEditor.chkStrikeoutClick(Sender: TObject);
var
NewStyles: TFontStyles;
begin
NewStyles := FFontStyles;
if chkStrikeout.Checked then
Include(NewStyles, fsStrikeOut)
else
Exclude(NewStyles, fsStrikeOut);
SetFontStyles(NewStyles);
end;
procedure TFontEditor.chkUnderlineClick(Sender: TObject);
var
NewStyles: TFontStyles;
begin
NewStyles := FFontStyles;
if chkUnderline.Checked then
Include(NewStyles, fsUnderline)
else
Exclude(NewStyles, fsUnderline);
SetFontStyles(NewStyles);
end;
procedure TFontEditor.pnlColorClick(Sender: TObject);
begin
dlgColor.Color := FFontColor;
if dlgColor.Execute then
begin
SetFontColor(dlgColor.Color);
end;
end;
procedure TFontEditor.LoadFontFromJSON(AJSONObject: TJSONObject);
var
FontObj: TJSONObject;
StylesArray: TJSONArray;
Styles: TFontStyles;
I: Integer;
StyleStr: string;
begin
if not Assigned(AJSONObject) then
Exit;
try
// 如果JSON对象中包含字体对象
if AJSONObject.TryGetValue<TJSONObject>('Font', FontObj) then
begin
// 设置字体名称
if FontObj.TryGetValue<string>('Name', FFontName) then
edtFontName.Text := FFontName;
// 设置字体大小
if FontObj.TryGetValue<Integer>('Size', FFontSize) then
edtFontSize.Text := IntToStr(FFontSize);
// 设置字体颜色
if FontObj.TryGetValue<Integer>('Color', Integer(FFontColor)) then
pnlColor.Color := FFontColor;
// 设置字体样式
Styles := [];
if FontObj.TryGetValue<TJSONArray>('Styles', StylesArray) then
begin
for I := 0 to StylesArray.Count - 1 do
begin
StyleStr := StylesArray.Items[I].Value;
if StyleStr = 'Bold' then
Include(Styles, fsBold)
else if StyleStr = 'Italic' then
Include(Styles, fsItalic)
else if StyleStr = 'Underline' then
Include(Styles, fsUnderline)
else if StyleStr = 'StrikeOut' then
Include(Styles, fsStrikeOut);
end;
FFontStyles := Styles;
end;
// 更新界面和预览
chkBold.Checked := fsBold in FFontStyles;
chkItalic.Checked := fsItalic in FFontStyles;
chkUnderline.Checked := fsUnderline in FFontStyles;
chkStrikeout.Checked := fsStrikeOut in FFontStyles;
UpdatePreview;
end;
except
on E: Exception do
begin
ShowMessage('加载字体设置时出错: ' + E.Message);
end;
end;
end;
procedure TFontEditor.SaveFontToJSON(AJSONObject: TJSONObject);
var
FontObj: TJSONObject;
StylesArray: TJSONArray;
begin
if not Assigned(AJSONObject) then
Exit;
try
// 创建或清空字体对象
if AJSONObject.TryGetValue<TJSONObject>('Font', FontObj) then
FontObj.Free;
FontObj := TJSONObject.Create;
AJSONObject.AddPair('Font', FontObj);
// 添加字体属性
FontObj.AddPair('Name', FFontName);
FontObj.AddPair('Size', TJSONNumber.Create(FFontSize));
FontObj.AddPair('Color', TJSONNumber.Create(Integer(FFontColor)));
// 添加字体样式
StylesArray := TJSONArray.Create;
FontObj.AddPair('Styles', StylesArray);
if fsBold in FFontStyles then
StylesArray.Add('Bold');
if fsItalic in FFontStyles then
StylesArray.Add('Italic');
if fsUnderline in FFontStyles then
StylesArray.Add('Underline');
if fsStrikeOut in FFontStyles then
StylesArray.Add('StrikeOut');
FModified := False;
except
on E: Exception do
begin
ShowMessage('保存字体设置时出错: ' + E.Message);
end;
end;
end;
{ TFontConfigEditor }
constructor TFontConfigEditor.Create;
begin
inherited;
FFontEditor := TFontEditor.Create(nil);
ConfigType := etFont;
end;
destructor TFontConfigEditor.Destroy;
begin
FFontEditor.Free;
inherited;
end;
function TFontConfigEditor.GetEditorType: TConfigType;
begin
Result := etFont;
end;
procedure TFontConfigEditor.Load(const Config: TJSONObject);
begin
if Assigned(Config) then
begin
FFontEditor.JSONObject := Config.Clone as TJSONObject;
FFontEditor.LoadFontFromJSON(FFontEditor.JSONObject);
end;
end;
function TFontConfigEditor.Save: TJSONObject;
begin
Result := TJSONObject.Create;
if Assigned(FFontEditor.JSONObject) then
begin
FFontEditor.SaveFontToJSON(FFontEditor.JSONObject);
// 克隆JSON对象以返回
Result := FFontEditor.JSONObject.Clone as TJSONObject;
end
else
begin
// 创建新的JSON对象
FFontEditor.SaveFontToJSON(Result);
end;
end;
// 注册编辑器
initialization
RegisterConfigEditor(etFont, CreateFontEditor);
finalization
// 清理资源
end.