source: trunk/Packages/bgrabitmap/bgrasvgoriginal.pas

Last change on this file was 2, checked in by chronos, 5 years ago
File size: 4.6 KB
Line 
1unit BGRASVGOriginal;
2
3{$mode objfpc}{$H+}
4
5interface
6
7uses
8 Classes, SysUtils, BGRABitmapTypes, BGRABitmap, BGRASVG, BGRATransform, BGRALayerOriginal;
9
10type
11 { TBGRALayerSVGOriginal }
12
13 TBGRALayerSVGOriginal = class(TBGRALayerCustomOriginal)
14 private
15 function GetDPI: single;
16 function GetSvgHeight: single;
17 function GetSvgWidth: single;
18 procedure SetDPI(AValue: single);
19 protected
20 FSVG: TBGRASVG;
21 FContentVersion: integer;
22 procedure ContentChanged;
23 public
24 constructor Create; override;
25 destructor Destroy; override;
26 procedure Render(ADest: TBGRABitmap; AMatrix: TAffineMatrix; ADraft: boolean); override;
27 function GetRenderBounds({%H-}ADestRect: TRect; AMatrix: TAffineMatrix): TRect; override;
28 procedure LoadFromStorage(AStorage: TBGRACustomOriginalStorage); override;
29 procedure SaveToStorage(AStorage: TBGRACustomOriginalStorage); override;
30 procedure LoadFromStream(AStream: TStream); override;
31 procedure SaveSVGToStream(AStream: TStream);
32 procedure LoadSVGFromStream(AStream: TStream);
33 class function StorageClassName: RawByteString; override;
34 property Width: single read GetSvgWidth;
35 property Height: single read GetSvgHeight;
36 property DPI: single read GetDPI write SetDPI;
37 end;
38
39implementation
40
41uses BGRACanvas2D, BGRAMemDirectory;
42
43{ TBGRALayerSVGOriginal }
44
45function TBGRALayerSVGOriginal.GetDPI: single;
46begin
47 result := FSVG.DefaultDpi;
48end;
49
50function TBGRALayerSVGOriginal.GetSvgHeight: single;
51begin
52 result := FSVG.ViewSizeInUnit[cuPixel].y;
53end;
54
55function TBGRALayerSVGOriginal.GetSvgWidth: single;
56begin
57 result := FSVG.ViewSizeInUnit[cuPixel].x;
58end;
59
60procedure TBGRALayerSVGOriginal.SetDPI(AValue: single);
61begin
62 FSVG.DefaultDpi:= AValue;
63 NotifyChange;
64end;
65
66procedure TBGRALayerSVGOriginal.ContentChanged;
67begin
68 FContentVersion += 1;
69 NotifyChange;
70end;
71
72constructor TBGRALayerSVGOriginal.Create;
73begin
74 inherited Create;
75 FSVG := TBGRASVG.Create;
76 FContentVersion := 0;
77end;
78
79destructor TBGRALayerSVGOriginal.Destroy;
80begin
81 FSVG.Free;
82 inherited Destroy;
83end;
84
85procedure TBGRALayerSVGOriginal.Render(ADest: TBGRABitmap;
86 AMatrix: TAffineMatrix; ADraft: boolean);
87var
88 c2D: TBGRACanvas2D;
89begin
90 if Assigned(FSVG) then
91 begin
92 c2D := TBGRACanvas2D.Create(ADest);
93 c2D.transform(AMatrix);
94 if ADraft then c2D.antialiasing := false;
95 FSVG.Draw(c2D,0,0);
96 c2D.Free;
97 end;
98end;
99
100function TBGRALayerSVGOriginal.GetRenderBounds(ADestRect: TRect;
101 AMatrix: TAffineMatrix): TRect;
102var
103 aff: TAffineBox;
104 min, size: TPointF;
105begin
106 if Assigned(FSVG) then
107 begin
108 min := FSVG.ViewMinInUnit[cuPixel];
109 size := FSVG.ViewSizeInUnit[cuPixel];
110 aff := AMatrix*TAffineBox.AffineBox(min,PointF(min.x+size.x,min.y),PointF(min.x,min.y+size.y));
111 result := aff.RectBounds;
112 end else
113 result := EmptyRect;
114end;
115
116procedure TBGRALayerSVGOriginal.LoadFromStorage(
117 AStorage: TBGRACustomOriginalStorage);
118var svgStream: TMemoryStream;
119 valDpi: Single;
120begin
121 svgStream := TMemoryStream.Create;
122 try
123 if AStorage.ReadFile('content.svg', svgStream) then
124 begin
125 if not Assigned(FSVG) then FSVG := TBGRASVG.Create;
126 svgStream.Position:= 0;
127 FSVG.LoadFromStream(svgStream);
128 end else
129 begin
130 FreeAndNil(FSVG);
131 FSVG := TBGRASVG.Create;
132 end;
133 FContentVersion:= AStorage.Int['content-version'];
134 finally
135 svgStream.Free;
136 end;
137 valDpi := AStorage.Float['dpi'];
138 if valDpi <> EmptySingle then
139 FSVG.DefaultDpi:= valDpi;
140end;
141
142procedure TBGRALayerSVGOriginal.SaveToStorage(
143 AStorage: TBGRACustomOriginalStorage);
144var svgStream: TMemoryStream;
145begin
146 if Assigned(FSVG) then
147 begin
148 if FContentVersion > AStorage.Int['content-version'] then
149 begin
150 svgStream := TMemoryStream.Create;
151 try
152 FSVG.SaveToStream(svgStream);
153 AStorage.WriteFile('content.svg', svgStream, true);
154 svgStream := nil;
155 AStorage.Int['content-version'] := FContentVersion;
156 finally
157 svgStream.Free;
158 end;
159 end;
160 AStorage.Float['dpi'] := FSVG.DefaultDpi;
161 end;
162end;
163
164procedure TBGRALayerSVGOriginal.LoadFromStream(AStream: TStream);
165begin
166 if TMemDirectory.CheckHeader(AStream) then
167 inherited LoadFromStream(AStream)
168 else
169 LoadSVGFromStream(AStream);
170end;
171
172procedure TBGRALayerSVGOriginal.SaveSVGToStream(AStream: TStream);
173begin
174 FSVG.SaveToStream(AStream);
175end;
176
177procedure TBGRALayerSVGOriginal.LoadSVGFromStream(AStream: TStream);
178begin
179 FSVG.LoadFromStream(AStream);
180 ContentChanged;
181end;
182
183class function TBGRALayerSVGOriginal.StorageClassName: RawByteString;
184begin
185 result := 'svg';
186end;
187
188initialization
189
190 RegisterLayerOriginal(TBGRALayerSVGOriginal);
191
192end.
193
Note: See TracBrowser for help on using the repository browser.