Debian Codegraph @

components/synunihighlighter/source/SynUniClasses.pas

raw
1 {
2 The contents of this file are subject to the Mozilla Public License
3 Version 1.1 (the "License"); you may not use this file except in compliance
4 with the License. You may obtain a copy of the License at
5 http://www.mozilla.org/MPL/
6
7 Software distributed under the License is distributed on an "AS IS" basis,
8 WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
9 the specific language governing rights and limitations under the License.
10
11 The Original Code is: SynUniHighlighter.pas, released 2003-01
12 All Rights Reserved.
13
14 Alternatively, the contents of this file may be used under the terms of the
15 GNU General Public License Version 2 or later (the "GPL"), in which case
16 the provisions of the GPL are applicable instead of those above.
17 If you wish to allow use of your version of this file only under the terms
18 of the GPL and not to allow others to use your version of this file
19 under the MPL, indicate your decision by deleting the provisions above and
20 replace them with the notice and other provisions required by the GPL.
21 If you do not delete the provisions above, a recipient may use your version
22 of this file under either the MPL or the GPL.
23
24 }{
25 @abstract(Provides a universal highlighter for SynEdit)
26 @authors(Fantasist [[email protected]], Vit [[email protected]],
27 Vitalik [[email protected]])
28 @created(2003)
29 @lastmod(2004-05-12)
30 }
31
32 unit SynUniClasses;
33
34 interface
35
36 uses
37 SysUtils, Graphics,
38 Classes, SynEditHighlighter, Contnrs, Laz2_DOM;
39
40 type
41 TSymbSet = set of char;
42
43 TSynInfo = class;
44 TStreamWriter = class;
45 TSynSymbol = class;
46 TSymbolNode = class;
47 TSymbolList = class;
48 TSynRule = class;
49
50 TVersionType = (vtInternalTest, vtBeta, vtRelease);
51
52 TAuthorInfo = record
53 Name: string; Email: string; Web: string;
54 Copyright: string; Company: string; Remark: string;
55 end;
56
57 TVerInfo = record
58 Version: integer; Revision: integer;
59 VersionType: TVersionType; ReleaseDate: TDateTime;
60 end;
61
62 THighInfo = record
63 Name: string; Extensions: string; Other: Boolean
64 end;
65
66 TSynInfo = class
67 Author: TAuthorInfo;
68 Version: TVerInfo;
69 General: THighInfo;
70 History: TStringList;
71 Sample: TStringlist;
72 constructor Create();
73 procedure Clear();
74 procedure LoadFromXml(xml: TDOMNode);
75 procedure LoadFromStream(Stream: TStream);
76 procedure SaveToStream(Stream: TStream; Ind: integer = 0); overload;
77 procedure SaveToStream(StreamWriter: TStreamWriter; Ind: integer = 0); overload;
78 end;
79
80 TSynEditProperties = class
81
82 end;
83
84 TSymbStartType = (stUnspecified, stAny, stTerm);
85 TSymbBrakeType = (btUnspecified, btAny, btTerm);
86 TSymbStartLine = (slNotFirst, slFirst, slFirstNonSpace);
87
88 TStreamWriter = class
89 Stream: TStream;
90 constructor Create(aStream: TStream);
91 procedure WriteString(const Str: string);
92 procedure InsertTag(Ind: integer; Name: string; Value: string);
93 procedure WriteTag(Ind: integer; Name: string; EndLine: boolean = False);
94 procedure WriteParam(Key, Value: string; CloseTag: string = '');
95 procedure WriteBoolParam(Key: string; Value, Default: boolean; CloseTag: string = '');
96 end;
97
98 TSynAttributes = class (TSynHighlighterAttributes)
99 public
100 OldColorForeground: TColor;
101 OldColorBackground: TColor;
102 ParentForeground: boolean;
103 ParentBackground: boolean;
104 constructor Create(Name: string);
105 function ToString: String; override;
106 function GetHashCode: PtrInt; override;
107 procedure LoadFromString(Value: string);
108 procedure SaveToStream(StreamWriter: TStreamWriter);
109 end;
110
111 TAbstractRule = class;
112
113 TSynSymbol = class
114 public
115 Symbol: string;
116 fOpenRule: TAbstractRule;
117 StartType: TSymbStartType;
118 BrakeType: TSymbBrakeType;
119 StartLine: TSymbStartLine;
120 Attributes: TSynHighlighterAttributes;
121 constructor Create(st: string; Attribs: TSynHighlighterAttributes); virtual;
122 destructor Destroy(); override;
123 end;
124
125 TSymbolNode = class
126 ch: char;
127 BrakeType: TSymbBrakeType;
128 StartType: TSymbStartType;
129 NextSymbs: TSymbolList;
130 tkSynSymbol: TSynSymbol;
131 constructor Create(AC: char; SynSymbol: TSynSymbol; ABrakeType: TSymbBrakeType); overload; virtual;
132 constructor Create(AC: char); overload;
133 destructor Destroy(); override;
134 end;
135
136 TSymbolList = class
137 SymbList: TList;
138 procedure AddSymbol(symb: TSymbolNode);
139 procedure SetSymbolNode(Index: Integer; Value: TSymbolNode);
140 function FindSymbol(ch: char): TSymbolNode;
141 function GetSymbolNode(Index: integer): TSymbolNode;
142 function GetCount(): integer;
143 property Nodes[index: integer]: TSymbolNode read GetSymbolNode write SetSymbolNode;
144 property Count: Integer read GetCount;
145 constructor Create(); virtual;
146 destructor Destroy(); override;
147 end;
148
149 TSynUniStyles = class (TObjectList)
150 public
151 FileName: string;
152 constructor Create();
153 destructor Destroy(); override;
154 function GetStyle(const Name: string): TSynAttributes;
155 function GetStyleDef(const Name: string; const Def: TSynAttributes): TSynAttributes;
156 procedure AddStyle(Name: string; Foreground, Background: TColor; FontStyle: TFontStyles);
157 procedure ListStylesNames(const AList: TStrings);
158 function GetStylesAsXML(): string;
159 procedure Load();
160 procedure Save();
161 end;
162
163 TAbstractRule = class
164 Enabled: boolean;
165 constructor Create();
166 end;
167
168 TSynRule = class(TAbstractRule)
169 public
170 Ind: integer; //temp
171 Name: string;
172 Attribs: TSynAttributes;
173 Style: string;
174 Styles: TSynUniStyles;
175 constructor Create();
176 destructor Destroy(); override;
177 procedure LoadFromXml(xml: TDOMNode); virtual; abstract;
178 procedure LoadFromStream(aSrc: TStream);
179 procedure LoadFromFile(FileName: string);
180 function GetAsStream(): TMemoryStream;
181 procedure SaveToStream(Stream: TStream; Ind: integer = 0); overload;
182 procedure SaveToStream(StreamWriter: TStreamWriter; Ind: integer = 0); overload; virtual; abstract;
183 end;
184
185 function StrToSet(st: string): TSymbSet;
186 function SetToStr(st: TSymbSet): string;
187 function StrToFontStyle(Style: string): TFontStyles;
188 function FontStyleToStr(Style: TFontStyles): string;
189 procedure FreeList(var List: TList);
190 procedure ClearList(List: TList);
191 function Indent(i: integer): string;
192
193 const
194 AbsoluteTermSymbols: TSymbSet = [#0, #9, #10, #13, #32];
195 EOL = #13#10;
196 CloseEmptyTag = '/>';
197 CloseStartTag = '>';
198
199 implementation
200
201 uses
202 Crc, Laz2_XMLRead;
203
204 function StrToSet(st: string): TSymbSet;
205 var i: integer;
206 begin
207 result := [];
208 for i := 1 to length(st) do Result := Result + [st[i]];
209 end;
210
211 function SetToStr(st: TSymbSet): string;
212 var b: byte;
213 begin
214 Result := '';
215 for b := 1 to 255 do
216 if (chr(b) in st) and (not (chr(b) in AbsoluteTermSymbols)) then
217 Result := Result+chr(b);
218 end;
219
220 function StrToFontStyle(Style: string): TFontStyles;
221 begin
222 Result := [];
223 if Pos('B', Style) > 0 then
224 Include( Result, fsBold );
225 if Pos('I', Style) > 0 then
226 Include( Result, fsItalic );
227 if Pos('U', Style) > 0 then
228 Include( Result, fsUnderline );
229 if Pos('S', Style) > 0 then
230 Include( Result, fsStrikeOut );
231 end;
232
233 function FontStyleToStr(Style: TFontStyles): string;
234 begin
235 Result := '';
236 if fsBold in Style then Result := Result + 'B';
237 if fsItalic in Style then Result := Result + 'I';
238 if fsUnderline in Style then Result := Result + 'U';
239 if fsStrikeOut in Style then Result := Result + 'S';
240 end;
241
242 procedure FreeList(var List: TList);
243 var i: integer;
244 begin
245 if List = nil then exit;
246 for i := 0 to List.Count-1 do
247 TObject(List[i]).Free;
248 List.Free;
249 List := nil;
250 end;
251
252 procedure ClearList(List: TList);
253 var i: integer;
254 begin
255 if List = nil then exit;
256 for i := 0 to List.Count-1 do
257 TObject(List[i]).Free;
258 List.Clear;
259 end;
260
261 //==== TInfo =================================================================
262 constructor TSynInfo.Create();
263 begin
264 inherited;
265 end;
266
267 procedure TSynInfo.Clear();
268 begin
269 General.Other := False;
270 General.Name := '';
271 General.Extensions := '';
272 Author.Name := '';
273 Author.Email := '';
274 Author.Web := '';
275 Author.Copyright := '';
276 Author.Company := '';
277 Author.Remark := '';
278 Version.Version := 0;
279 Version.Revision := 0;
280 Version.ReleaseDate := 0;
281 Version.VersionType := vtInternalTest;
282 History.Clear;
283 Sample.Clear;
284 end;
285
286 function ReadValue(ANode: TDOMNode): String;
287 begin
288 if Assigned(ANode.FirstChild) then
289 Result:= ANode.FirstChild.NodeValue
290 else
291 Result:= EmptyStr;
292 end;
293
294 procedure TSynInfo.LoadFromXml(xml: TDOMNode);
295 var
296 i, J: integer;
297 Key, Value: string;
298 ChildNode1, ChildNode2: TDOMNode;
299 begin
300 for J := 0 to Int32(xml.ChildNodes.Count) - 1 do
301 begin
302 ChildNode1:= xml.ChildNodes.Item[J];
303 if SameText('General', ChildNode1.NodeName) then
304 for i := 0 to Int32(ChildNode1.Attributes.Length) - 1 do begin
305 Key := ChildNode1.Attributes[i].NodeName; Value := ChildNode1.Attributes[i].NodeValue;
306 if SameText('Name', Key) then General.Name := Value else
307 if SameText('Extensions', Key) then General.Extensions := Value else
308 if SameText('Other', Key) then General.Other := StrToBoolDef(Value, False)
309 end else
310 if SameText('Author', ChildNode1.NodeName) then
311 for i := 0 to Int32(ChildNode1.Attributes.Length) - 1 do begin
312 Key := ChildNode1.Attributes[i].NodeName; Value := ChildNode1.Attributes[i].NodeValue;
313 if SameText('Name', Key) then Author.Name := Value else
314 if SameText('Email', Key) then Author.Email := Value else
315 if SameText('Web', Key) then Author.Web := Value else
316 if SameText('Copyright', Key) then Author.Copyright := Value else
317 if SameText('Company', Key) then Author.Company := Value else
318 if SameText('Remark', Key) then Author.Remark := Value else
319 end else
320 if SameText('Version', ChildNode1.NodeName) then
321 for i := 0 to Int32(ChildNode1.Attributes.Length) - 1 do begin
322 Key := ChildNode1.Attributes[i].NodeName; Value := ChildNode1.Attributes[i].NodeValue;
323 if SameText('Version', Key) then Version.Version := StrToIntDef(Value, 0) else
324 if SameText('Revision', Key) then Version.Revision := StrToIntDef(Value, 0) else
325 if SameText('Date', Key) then
326 try
327 Value := StringReplace(Value, ',', DefaultFormatSettings.DecimalSeparator, [rfReplaceAll]); // Since no one ever call something like "GetFormatSettings", "DefaultFormatSettings" still hold the default values.
328 Value := StringReplace(Value, '.', DefaultFormatSettings.DecimalSeparator, [rfReplaceAll]); // Just in case there is something we did not think about.
329 Version.ReleaseDate := StrToFloat(Value, DefaultFormatSettings);
330 except
331 // Ignore
332 end
333 else if SameText('Type', Key) then
334 if Value = 'Beta' then Version.VersionType := vtBeta else
335 if Value = 'Release' then Version.VersionType := vtRelease else
336 Version.VersionType := vtInternalTest
337 end else
338 if SameText('History', ChildNode1.NodeName) then begin
339 History.Clear; Sample.Clear;
340 for I:= 0 to Int32(ChildNode1.ChildNodes.Count) - 1 do
341 begin
342 ChildNode2 := ChildNode1.ChildNodes.Item[I];
343 if ChildNode2.NodeName = 'H' then
344 History.Add(ReadValue(ChildNode2));
345 end;
346 end else
347 if SameText('Sample', ChildNode1.NodeName) then begin
348 Sample.Clear;
349 for I:= 0 to Int32(ChildNode1.ChildNodes.Count) - 1 do
350 begin
351 ChildNode2 := ChildNode1.ChildNodes.Item[I];
352 if ChildNode2.NodeName = 'S' then
353 Sample.Add(ReadValue(ChildNode2));
354 end;
355 end;
356 end;
357 end;
358
359 procedure TSynInfo.LoadFromStream(Stream: TStream);
360 var
361 xml: TXMLDocument = nil;
362 begin
363 try
364 ReadXMLFile(xml, Stream);
365 LoadFromXml(xml);
366 finally
367 xml.Free;
368 end;
369 end;
370
371 procedure TSynInfo.SaveToStream(Stream: TStream; Ind: integer);
372 var
373 StreamWriter: TStreamWriter;
374 begin
375 StreamWriter := TStreamWriter.Create(Stream);
376 SaveToStream(StreamWriter, Ind);
377 StreamWriter.Free;
378 end;
379
380 procedure TSynInfo.SaveToStream(StreamWriter: TStreamWriter; Ind: integer);
381 var
382 i: integer;
383 begin
384 with StreamWriter do begin
385 WriteTag(Ind, 'Info', True);
386
387 WriteTag(Ind+2, 'General');
388 WriteParam('Name', General.Name);
389 WriteParam('Extensions', General.Extensions);
390 WriteParam('Other', BoolToStr(General.Other), CloseEmptyTag);
391
392 WriteTag(Ind+2, 'Author');
393 WriteParam('Name', Author.Name);
394 WriteParam('Email', Author.Email);
395 WriteParam('Web', Author.Web);
396 WriteParam('Copyright', Author.Copyright);
397 WriteParam('Company', Author.Company);
398 WriteParam('Remark', Author.Remark, CloseEmptyTag);
399
400 WriteTag(Ind+2, 'Version');
401 WriteParam('Version', IntToStr(Version.Version));
402 WriteParam('Revision', IntToStr(Version.Revision));
403 WriteParam('Date', FloatToStr(Version.ReleaseDate), CloseEmptyTag);
404 { case Version.VersionType of
405 vtInternalTest: WriteParam('Type', 'Internal Test');
406 vtBeta: WriteParam('Type', 'Beta');
407 vtRelease: WriteParam('Type', 'Release');
408 end;}
409
410 WriteTag(Ind+2, 'History', True);
411 for i := 0 to History.Count-1 do InsertTag(Ind+4, 'H', History[i]);
412 WriteTag(Ind+2, '/History', True);
413
414 WriteTag(Ind+2, 'Sample', True);
415 for i := 0 to Sample.Count-1 do InsertTag(Ind+4, 'S', Sample[i]);
416 WriteTag(Ind+2, '/Sample', True);
417
418 WriteTag(Ind, '/Info', True);
419 end;
420 end;
421 //==== TStreamWriter =========================================================
422 function Indent(i: integer): string;
423 begin
424 SetLength(Result, i);
425 // if i > 0 then !!!!!!!!!!!!!!!!!!!!!!!!!
426 {To prevent error...}
427 {$IFDEF FPC}
428 if i > 0 then
429 {$ENDIF}
430 FillChar(Result[1], i, #32);
431 end;
432
433 function GetValidValue(Value: string): string;
434 begin
435 Value := StringReplace(Value, '&', '&', [rfReplaceAll, rfIgnoreCase]);
436 Value := StringReplace(Value, '<', '&lt;', [rfReplaceAll, rfIgnoreCase]);
437 Value := StringReplace(Value, '"', '&quot;', [rfReplaceAll, rfIgnoreCase]);
438 Result := StringReplace(Value, '>', '&gt;', [rfReplaceAll, rfIgnoreCase]);
439 end;
440
441 constructor TStreamWriter.Create(aStream: TStream);
442 begin
443 Stream := aStream;
444 end;
445
446 procedure TStreamWriter.WriteString(const Str: string);
447 begin
448 Stream.Write(Str[1], Length(Str));
449 end;
450
451 procedure TStreamWriter.InsertTag(Ind: integer; Name: string; Value: string);
452 begin
453 WriteString(Format('%s<%s>%s</%s>'+EOL, [Indent(Ind), Name, GetValidValue(Value), Name]));
454 end;
455
456 procedure TStreamWriter.WriteTag(Ind: integer; Name: string; EndLine: boolean = False);
457 begin
458 WriteString(Format('%s<%s', [Indent(Ind), Name]));
459 if EndLine then WriteString('>' + EOL);
460 end;
461
462 procedure TStreamWriter.WriteParam(Key, Value: string; CloseTag: string = '');
463 begin
464 WriteString(Format(' %s="%s"', [Key, GetValidValue(Value)]));
465 if CloseTag <> '' then WriteString(CloseTag + EOL);
466 end;
467
468 procedure TStreamWriter.WriteBoolParam(Key: string; Value, Default: boolean; CloseTag: string = '');
469 begin
470 If Value <> Default then
471 WriteParam(Key, BoolToStr(Value,True), CloseTag);
472 end;
473
474 //==== TAttributes ===========================================================
475 constructor TSynAttributes.Create(Name: String);
476 begin
477 inherited Create(Name{SYNS_AttrDefaultPackage});
478 end;
479
480 function TSynAttributes.ToString: String;
481 begin
482 Result:= '$' + HexStr(Foreground, 8) + ',' +
483 '$' + HexStr(Background, 8) + ';' +
484 BoolToStr(ParentForeground,True) + ':' +
485 BoolToStr(ParentBackground,True) + '.' +
486 FontStyleToStr(Style);
487 end;
488
489 function TSynAttributes.GetHashCode: PtrInt;
490 var
491 ACrc: Cardinal = 0;
492 AStyle: TFontStyles;
493 ABackground: TColor;
494 AForeground: TColor;
495 begin
496 AStyle:= Style;
497 ABackground:= Background;
498 AForeground:= Foreground;
499 ACrc:= CRC32(ACrc, @ABackground, SizeOf(TColor));
500 ACrc:= CRC32(ACrc, @AForeground, SizeOf(TColor));
501 ACrc:= CRC32(ACrc, @AStyle, SizeOf(TFontStyles));
502 ACrc:= CRC32(ACrc, @ParentForeground, SizeOf(Boolean));
503 Result:= PtrInt(CRC32(ACrc, @ParentBackground, SizeOf(Boolean)));
504 end;
505
506 procedure TSynAttributes.LoadFromString(Value: string);
507 begin
508 ParentForeground := False;
509 ParentBackground := False;
510 Foreground := StrToIntDef(Copy(Value, 1, pos(',',Value)-1), 0);
511 OldColorForeground := Foreground;
512 Background := StrToIntDef(Copy(Value, pos(',',Value)+1, pos(';',Value)-pos(',',Value)-1), $FFFFFF);
513 OldColorBackground := Background;
514 ParentForeground := LowerCase(Copy(Value, pos(';',Value)+1, pos(':',Value)-pos(';',Value)-1)) = 'true';
515 ParentBackground := LowerCase(Copy(Value, pos(':',Value)+1, pos('.',Value)-pos(':',Value)-1)) = 'true';
516 Style := StrToFontStyle(Copy(Value, pos('.',Value)+1, Length(Value)-pos('.',Value)));
517 end;
518
519 procedure TSynAttributes.SaveToStream(StreamWriter: TStreamWriter);
520 begin
521 StreamWriter.WriteParam('Attributes', ToString);
522 end;
523
524 //==== TSynSymbol ============================================================
525 constructor TSynSymbol.Create(st: string; Attribs: TSynHighlighterAttributes);
526 //: Constructor of TSynSymbol
527 begin
528 Attributes := Attribs;
529 Symbol := st;
530 fOpenRule := nil;
531 StartLine := slNotFirst;
532 StartType := stUnspecified;
533 BrakeType := btUnspecified;
534 end;
535
536 destructor TSynSymbol.Destroy;
537 //: Destructor of TSynSymbol
538 begin
539 inherited;
540 end;
541
542 //==== TSymbolNode ===========================================================
543 constructor TSymbolNode.Create(AC: char; SynSymbol: TSynSymbol;
544 ABrakeType: TSymbBrakeType);
545 begin
546 ch := AC;
547 NextSymbs := TSymbolList.Create;
548 BrakeType := ABrakeType;
549 StartType := SynSymbol.StartType;
550 tkSynSymbol := SynSymbol;
551 end;
552
553 constructor TSymbolNode.Create(AC: char);
554 begin
555 ch := AC;
556 NextSymbs := TSymbolList.Create;
557 tkSynSymbol := nil;
558 end;
559
560 destructor TSymbolNode.Destroy;
561 //: Destructor of TSymbolNode
562 begin
563 NextSymbs.Free;
564 inherited;
565 end;
566
567 //==== TSymbolList ===========================================================
568 procedure TSymbolList.AddSymbol(symb: TSymbolNode);
569 //: Add Node to SymbolList
570 begin
571 SymbList.Add(symb);
572 end;
573
574 constructor TSymbolList.Create;
575 //: Constructor of TSymbolList
576 begin
577 SymbList := TList.Create;
578 end;
579
580 destructor TSymbolList.Destroy;
581 //: Destructor of TSymbolList
582 begin
583 FreeList(SymbList);
584 inherited;
585 end;
586
587 function TSymbolList.FindSymbol(ch: char): TSymbolNode;
588 //: Find Node in SymbolList by char
589 var
590 i: integer;
591 begin
592 Result := nil;
593 for i := 0 to SymbList.Count-1 do
594 if TSymbolNode(SymbList[i]).ch = ch then
595 begin
596 Result := TSymbolNode(SymbList[i]);
597 break;
598 end;
599 end;
600
601 function TSymbolList.GetCount: integer;
602 //: Return Node count in SymbolList
603 begin
604 Result := SymbList.Count
605 end;
606
607 function TSymbolList.GetSymbolNode(Index: integer): TSymbolNode;
608 //: Return Node in SymbolList by index
609 begin
610 Result := TSymbolNode(SymbList[index]);
611 end;
612
613 procedure TSymbolList.SetSymbolNode(Index: Integer; Value: TSymbolNode);
614 //: Set Node in SymbolList bt index
615 begin
616 if Index < SymbList.Count then
617 TSymbolNode(SymbList[index]).Free;
618 SymbList[index] := Value;
619 end;
620
621 constructor TAbstractRule.Create();
622 begin
623 Enabled := True;
624 end;
625
626 //==== TSynRule ==============================================================
627 constructor TSynRule.Create;
628 begin
629 inherited;
630 ind := -1;
631 Attribs := TSynAttributes.Create('unknown');
632 end;
633
634 destructor TSynRule.Destroy;
635 begin
636 Attribs.Free;
637 inherited Destroy;
638 end;
639
640 function TSynRule.GetAsStream: TMemoryStream;
641 begin
642 Result := TMemoryStream.Create;
643 SaveToStream(Result);
644 end;
645
646 procedure TSynRule.SaveToStream(Stream: TStream; Ind: integer = 0);
647 var
648 StreamWriter: TStreamWriter;
649 begin
650 StreamWriter := TStreamWriter.Create(Stream);
651 SaveToStream(StreamWriter, Ind);
652 StreamWriter.Free;
653 end;
654
655 procedure TSynRule.LoadFromStream(aSrc: TStream);
656 var
657 I: Integer;
658 ChildNode: TDOMNode;
659 TagName: UnicodeString;
660 xml: TXMLDocument = nil;
661 begin
662 if ClassName = 'TSynRange' then TagName := 'Range' else
663 if ClassName = 'TSynKeyList' then TagName := 'Keywords' else
664 if ClassName = 'TSynSet' then TagName := 'Set' else
665 raise Exception.Create(ClassName + '.LoadFromStream - Unknown rule to load!');
666 try
667 ReadXMLFile(xml, aSrc);
668 for I:= 0 to Int32(xml.ChildNodes.Count) - 1 do
669 begin
670 ChildNode:= xml.ChildNodes.Item[I];
671 if SameText(ChildNode.NodeName, TagName) then
672 LoadFromXml(ChildNode);
673 end;
674 finally
675 xml.Free;
676 end;
677 end;
678
679 procedure TSynRule.LoadFromFile(FileName: string);
680 var
681 xml: TXMLDocument = nil;
682 begin
683 if not FileExists(FileName) then
684 raise Exception.Create(ClassName + '.LoadFromFile - "'+FileName+'" does not exists.');
685
686 try
687 ReadXMLFile(xml, FileName);
688 LoadFromXml(xml);
689 finally
690 xml.Free;
691 end;
692 end;
693
694 //==== TSynUniStyles =========================================================
695 constructor TSynUniStyles.Create;
696 begin
697 inherited Create(True);
698 end;
699
700 destructor TSynUniStyles.Destroy;
701 begin
702 inherited;
703 end;
704
705 function TSynUniStyles.GetStyle(const Name: string): TSynAttributes;
706 begin
707 Result := GetStyleDef(Name, nil);
708 end;
709
710 function TSynUniStyles.GetStyleDef(const Name: string;
711 const Def: TSynAttributes): TSynAttributes;
712 var
713 i: integer;
714 begin
715 Result := Def;
716 for i := 0 to Self.Count-1 do
717 if SameText(TSynAttributes(Self.Items[i]).Name, Name) then begin
718 Result := TSynAttributes(Self.Items[i]);
719 Exit;
720 end;
721 end;
722
723 procedure TSynUniStyles.AddStyle(Name: string; Foreground, Background: TColor;
724 FontStyle: TFontStyles);
725 var
726 Atr: TSynAttributes;
727 begin
728 Atr := TSynAttributes.Create(Name);
729 Atr.Foreground := Foreground;
730 Atr.Background := Background;
731 Atr.Style := FontStyle;
732 Self.Add(Atr);
733 end;
734
735 procedure TSynUniStyles.ListStylesNames(const AList: TStrings);
736 var
737 i: integer;
738 begin
739 aList.BeginUpdate;
740 try
741 aList.Clear;
742 for i := 0 to Self.Count-1 do
743 aList.Add(TSynAttributes(Self.Items[i]).Name);
744 finally
745 aList.EndUpdate;
746 end;
747 end;
748
749 function TSynUniStyles.GetStylesAsXML: string;
750 var
751 i: integer;
752 begin
753 // Result:= '<?xml version="1.0" encoding="ISO-8859-1"?>'#13#10#13#10';
754 Result := '<Schemes>'#13#10;
755 Result := Result + ' <Scheme Name="Default">'#13#10;
756 for i := 0 to Self.Count-1 do
757 with TSynAttributes(Self.Items[I]) do
758 Result := Result + ' <Style Name="' + Name +
759 '" Fg="' + IntToStr(Foreground) +
760 '" Bg="' + IntToStr(Background) +
761 '" FontStyle="' + FontStyleToStr(Style) + '/>'#13#10;
762 Result := Result + ' </Scheme>'#13#10 + '</Schemes>';
763 end;
764
765 procedure TSynUniStyles.Load;
766 var
767 I: Integer;
768 Xml: TXMLDocument;
769 Style: TSynAttributes;
770 ARoot, ChildNode: TDOMNode;
771 Foreground, Background: TColor;
772 Name, FontStyle, Key, Value: string;
773 begin
774 if not FileExists(FileName) then
775 raise EFileNotFoundException.Create(ClassName + '.Load - "' + FileName + '" does not exists.');
776
777 Clear;
778 ReadXMLFile(Xml, FileName);
779 try
780 ARoot:= Xml.FindNode('Scheme');
781 if Assigned(ARoot) then
782 begin
783 ChildNode:= ARoot.FirstChild;
784 while Assigned(ChildNode) do
785 begin
786 if SameText(ChildNode.NodeName, 'Style') then
787 begin
788 Name := '';
789 Foreground := clWindowText;
790 Background := clWindow;
791 FontStyle := '';
792 for I := 0 to Int32(ChildNode.Attributes.Length) - 1 do
793 begin
794 Key := ChildNode.Attributes[I].NodeName;
795 Value := ChildNode.Attributes[I].NodeValue;
796 if SameText('Name', Key) then Name := Value else
797 if SameText('Foreground', Key) then Foreground := StrToIntDef(Value, clWindowText) else
798 if SameText('Background', Key) then Background := StrToIntDef(Value, clWindow) else
799 if SameText('FontStyle', Key) then FontStyle := Value else
800 end;
801 Style := Self.GetStyle(Name);
802 if Style <> nil then begin
803 Style.Foreground := Foreground;
804 Style.Background := Background;
805 Style.Style := StrToFontStyle(FontStyle);
806 end else
807 Self.AddStyle(Name, Foreground, Background, StrToFontStyle(FontStyle));
808 end;
809 ChildNode:= ChildNode.NextSibling;
810 end;
811 end;
812 finally
813 FreeAndNil(Xml);
814 end;
815 end;
816
817 procedure TSynUniStyles.Save;
818 var
819 S: string;
820 begin
821 if not FileExists(FileName) then
822 raise Exception.Create(ClassName + '.Save - "' + FileName + '" does not exists.');
823
824 S := Self.GetStylesAsXML;
825 with TFileStream.Create(FileName, fmOpenWrite) do
826 try
827 Write(Pointer(S)^, length(S));
828 finally
829 Free;
830 end;
831 end;
832
833 end.