Author: Mike Shkolnik
How can I save/restore the TFont-object in INI and/or text file?
Answer:
Sometimes you need to save/to load a font information in INI-file, Registry or some
text file.
Now I desribe the some different methods.
1. very easy but not result isn't compact and effective (by data storage)
1
2 procedure SaveFont(FStream: TIniFile; Section: string; smFont: TFont);
3 begin
4 FStream.WriteString(Section, Ident + 'Name', smFont.Name);
5 FStream.WriteInteger(Section, Ident + 'CharSet', smFont.CharSet);
6 FStream.WriteInteger(Section, Ident + 'Color', smFont.Color);
7 FStream.WriteInteger(Section, Ident + 'Size', smFont.Size);
8 FStream.WriteInteger(Section, Ident + 'Style', Byte(smFont.Style));
9 end;
10
11 procedure LoadFont(FStream: TIniFile; Section: string; smFont: TFont);
12 begin
13 smFont.Name := FStream.ReadString(Section, Ident + 'Name', smFont.Name);
14 smFont.CharSet := TFontCharSet(FStream.ReadInteger(Section, Ident + 'CharSet',
15 smFont.CharSet));
16 smFont.Color := TColor(FStream.ReadInteger(Section, Ident + 'Color',
17 smFont.Color));
18 smFont.Size := FStream.ReadInteger(Section, Ident + 'Size', smFont.Size);
19 smFont.Style := TFontStyles(Byte(FStream.ReadInteger(Section, Ident + 'Style',
20 Byte(smFont.Style))));
21 end;
2. more hardly than first method, but result is compact. I use this method in all
own apps.
22
23 procedure SaveFont(FStream: TIniFile; Section: string; smFont: TFont);
24 begin
25 FStream.WriteString(Section, 'Font', smFont.Name + ',' +
26 IntToStr(smFont.CharSet) + ',' +
27 IntToStr(smFont.Color) + ',' +
28 IntToStr(smFont.Size) + ',' +
29 IntToStr(Byte(smFont.Style)));
30 end;
31
32 procedure LoadFont(FStream: TIniFile; Section: string; smFont: TFont);
33 var
34 s, Data: string;
35 i: Integer;
36 begin
37 s := FStream.ReadString(Section, 'Font', ',,,,');
38 try
39 i := Pos(',', s);
40 if i > 0 then
41 begin
42 {Name}
43 Data := Trim(Copy(s, 1, i - 1));
44 if Data <> '' then
45 smFont.Name := Data;
46 Delete(s, 1, i);
47 i := Pos(',', s);
48 if i > 0 then
49 begin
50 {CharSet}
51 Data := Trim(Copy(s, 1, i - 1));
52 if Data <> '' then
53 smFont.Charset := TFontCharSet(StrToIntDef(Data, smFont.Charset));
54 Delete(s, 1, i);
55 i := Pos(',', s);
56 if i > 0 then
57 begin
58 {Color}
59 Data := Trim(Copy(s, 1, i - 1));
60 if Data <> '' then
61 smFont.Color := TColor(StrToIntDef(Data, smFont.Color));
62 Delete(s, 1, i);
63 i := Pos(',', s);
64 if i > 0 then
65 begin
66 {Size}
67 Data := Trim(Copy(s, 1, i - 1));
68 if Data <> '' then
69 smFont.Size := StrToIntDef(Data, smFont.Size);
70 Delete(s, 1, i);
71 {Style}
72 Data := Trim(s);
73 if Data <> '' then
74 smFont.Style := TFontStyles(Byte(StrToIntDef(Data,
75 Byte(smFont.Style))));
76 end
77 end
78 end
79 end;
80 except
81 end;
82 end;
3. as alternative for 1&2 methods I can offer the third - you can create a temporary stream, save the wished font component in this stream (Stream.SaveComponent) and then you can navigate the byte-by-byte in stream, to convert each byte into hex (or some other radix) and save into your text file as string. Each byte is a two symbols for hex radix. For font reading - on the contrary...
|