Skip to content

Commit

Permalink
*Fixed empty editor when writing incorrect svg code.
Browse files Browse the repository at this point in the history
  • Loading branch information
carloBarazzetta committed Sep 13, 2020
1 parent 81f7be2 commit a23fac8
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 15 deletions.
13 changes: 13 additions & 0 deletions Packages/SVGIconImageListEditorUnit.dfm
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,16 @@ object SVGIconImageListEditor: TSVGIconImageListEditor
TabOrder = 3
OnClick = HelpButtonClick
end
object SVGErrorStaticText: TStaticText
Left = 6
Top = 3
Width = 406
Height = 30
Anchors = [akLeft, akTop, akRight]
AutoSize = False
TabOrder = 4
Transparent = False
end
end
object paIcon: TPanel
Left = 0
Expand Down Expand Up @@ -501,6 +511,9 @@ object SVGIconImageListEditor: TSVGIconImageListEditor
ScrollBars = ssBoth
TabOrder = 6
OnChange = SVGTextChange
OnEnter = SVGTextEnter
OnExit = SVGTextExit
OnKeyDown = SVGTextKeyDown
end
object FixedColorItemComboBox: TColorBox
Left = 415
Expand Down
63 changes: 60 additions & 3 deletions Packages/SVGIconImageListEditorUnit.pas
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ TSVGIconImageListEditor = class(TForm)
DeleteButton: TButton;
CategoryEdit: TEdit;
CategoryLabel: TLabel;
SVGErrorStaticText: TStaticText;
procedure FormCreate(Sender: TObject);
procedure ApplyButtonClick(Sender: TObject);
procedure ClearAllButtonClick(Sender: TObject);
Expand Down Expand Up @@ -156,7 +157,12 @@ TSVGIconImageListEditor = class(TForm)
procedure SetCategoriesButtonClick(Sender: TObject);
procedure DeleteButtonClick(Sender: TObject);
procedure CategoryEditExit(Sender: TObject);
procedure SVGTextExit(Sender: TObject);
procedure SVGTextEnter(Sender: TObject);
procedure SVGTextKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
private
FOldSVGText: string;
FOpenDialog: TOpenPictureDialogSvg;
FSelectedCategory: string;
FSourceList, FEditingList: TSVGIconImageList;
Expand All @@ -173,6 +179,7 @@ TSVGIconImageListEditor = class(TForm)
procedure UpdateSizeGUI;
procedure AddNewItem;
procedure DeleteSelectedItems;
procedure ResetError;
public
property Modified: Boolean read FModified;
property SVGIconImageList: TSVGIconImageList read FEditingList;
Expand Down Expand Up @@ -538,7 +545,7 @@ procedure TSVGIconImageListEditor.ReplaceButtonClick(Sender: TObject);
finally
end;
end;
BuildList(ImageView.ItemIndex);
BuildList(ImageView.Items[ImageView.ItemIndex].ImageIndex);
finally
FEditingList.EndUpdate;
Screen.Cursor := crDefault;
Expand Down Expand Up @@ -606,13 +613,61 @@ procedure TSVGIconImageListEditor.SizeSpinEditChange(Sender: TObject);
end;

procedure TSVGIconImageListEditor.SVGTextChange(Sender: TObject);
var
LOldText: string;
begin
if FUpdating then Exit;
SelectedIcon.SVGText := SVGText.Lines.Text;
IconImage.Invalidate;
LOldText := SelectedIcon.SVGText;
try
SelectedIcon.SVGText := SVGText.Lines.Text;
ResetError;
IconImage.Repaint;
UpdateGUI;
except
on E: Exception do
begin
SVGErrorStaticText.Caption := E.Message;
SVGErrorStaticText.Hint := E.Message;
SelectedIcon.SVGText := '';
FEditingList.RecreateBitmaps;
IconImage.Repaint;
ImageView.Invalidate;
end;
end;
end;

procedure TSVGIconImageListEditor.ResetError;
begin
SVGErrorStaticText.Caption := '';
SVGErrorStaticText.Hint := '';
end;

procedure TSVGIconImageListEditor.SVGTextEnter(Sender: TObject);
begin
FOldSVGText := SVGText.Lines.Text;
end;

procedure TSVGIconImageListEditor.SVGTextExit(Sender: TObject);
begin
ResetError;
UpdateGUI;
end;

procedure TSVGIconImageListEditor.SVGTextKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
var
LSelStart: Integer;
begin
if (Key = VK_ESCAPE) or ((upcase(Char(Key)) = 'Z') and (ssCtrl in Shift)) then
begin
LSelStart := SVGText.SelStart;
SVGText.Lines.Text := FOldSVGText;
SVGText.SelStart := LSelStart;
SVGText.SelLength := 0;
SelectedIcon.SVGText := FOldSVGText;
end;
end;

procedure TSVGIconImageListEditor.BuildList(Selected: Integer);
begin
FEditingList.BeginUpdate;
Expand Down Expand Up @@ -725,6 +780,8 @@ procedure TSVGIconImageListEditor.FormCreate(Sender: TObject);
FModified := False;
SVGText.Font.Name := 'Courier New';
Caption := Format(Caption, [SVGIconImageListVersion]);
SVGErrorStaticText.Font.Color := clRed;
SVGErrorStaticText.Font.Style := [fsBold];
end;

procedure TSVGIconImageListEditor.Apply;
Expand Down
8 changes: 6 additions & 2 deletions Source/D2DSVGFactory.pas
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ function RenderTarget: ID2D1DCRenderTarget;
// Support functions
function WinSvgSupported: Boolean;

resourcestring
D2D_ERROR_PARSING_SVG_TEXT = 'Error parsing SVG Text: %s';

implementation

Uses
Expand Down Expand Up @@ -166,8 +169,9 @@ procedure TD2DSVG.LoadFromSource;
MStream.Free;
end;
except
fSource := '';
end;
on E: Exception do
raise Exception.CreateFmt(D2D_ERROR_PARSING_SVG_TEXT, [E.Message]);
end;
end;

function TD2DSVG.GetFixedColor: TColor;
Expand Down
2 changes: 1 addition & 1 deletion Source/SVGIconItems.pas
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ procedure TSVGIconItem.Assign(Source: TPersistent);

constructor TSVGIconItem.Create(Collection: TCollection);
begin
inherited Create(Collection);
FSVG := GlobalSVGFactory.NewSvg;
inherited Create(Collection);
FFixedColor := SVG_INHERIT_COLOR;
end;

Expand Down
31 changes: 22 additions & 9 deletions Svg/SVG.pas
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ interface
SVGStyle,
SVGColor;

resourcestring
SVG_ERROR_PARSING_SVG_TEXT = 'Error parsing SVG Text: %s';
SVG_ERROR_TAG_SVG = 'Error: Tag "svg" not found.';
type
TSVG = class;
TSVGObject = class;
Expand Down Expand Up @@ -1991,10 +1994,11 @@ procedure TSVG.LoadFromText(const Text: string);
NodeType: XmlNodeType;
Name: PWideChar;
Len: LongWord;
LOldText: string;
begin
LOldText := FSource;
Clear;
if Text = '' then Exit;

try
Reader := CreateXmlLite.Data(Text).Reader;
while not Reader.IsEOF do
Expand All @@ -2003,18 +2007,27 @@ procedure TSVG.LoadFromText(const Text: string);
if NodeType = XmlNodeType.Element then
begin
Reader.GetLocalName(Name, Len);
if Name = 'svg' then
begin
FSource := Text;
ReadIn(Reader);
break;
end
else
Exit;
try
if Name = 'svg' then
begin
FSource := Text;
ReadIn(Reader);
break;
end
else
raise Exception.Create(SVG_ERROR_TAG_SVG);
except
on E: Exception do
begin
FSource := LOldText;
raise Exception.CreateFmt(SVG_ERROR_PARSING_SVG_TEXT, [E.Message]);
end;
end;
end;
end;
except
FSource := '';
raise;
end;
end;

Expand Down

0 comments on commit a23fac8

Please sign in to comment.