Debian Codegraph @

components/Image32/source/Img32.Fmt.SVG.pas

raw
1 unit Img32.Fmt.SVG;
2
3 (*******************************************************************************
4 * Author : Angus Johnson *
5 * Version : 4.7 *
6 * Date : 6 January 2025 *
7 * Website : http://www.angusj.com *
8 * Copyright : Angus Johnson 2019-2025 *
9 * Purpose : SVG file format extension for TImage32 *
10 * License : http://www.boost.org/LICENSE_1_0.txt *
11 *******************************************************************************)
12
13 interface
14
15 {$I Img32.inc}
16
17 uses
18 {$IFDEF MSWINDOWS} Windows, {$ENDIF}
19 {$IF NOT DEFINED(NEWPOSFUNC) OR DEFINED(FPC)} StrUtils, {$IFEND}
20 {$IFDEF UNICODE} AnsiStrings, {$ENDIF}
21 SysUtils, Classes, Math,
22 {$IFDEF XPLAT_GENERICS} Generics.Collections, Generics.Defaults, {$ENDIF}
23 Img32, Img32.Vector, Img32.SVG.Core, Img32.SVG.Reader
24 {$IF DEFINED(USING_LCL)}, Types{$IFEND}
25 ;
26
27 type
28 TImageFormat_SVG = class(TImageFormat)
29 public
30 class function IsValidImageStream(stream: TStream): Boolean; override;
31 function LoadFromStream(stream: TStream;
32 img32: TImage32; imgIndex: integer = 0): Boolean; override;
33 // SaveToStream: not implemented for SVG streams
34 procedure SaveToStream(stream: TStream;
35 img32: TImage32; quality: integer = 0); override;
36 class function CanCopyToClipboard: Boolean; override;
37 class function CopyToClipboard(img32: TImage32): Boolean; override;
38 class function CanPasteFromClipboard: Boolean; override;
39 class function PasteFromClipboard(img32: TImage32): Boolean; override;
40 end;
41
42 TSvgListObject = class
43 xml : string;
44 name : string;
45 end;
46
47 TSvgImageList32 = class(TInterfacedObj, INotifySender)
48 private
49 fReader : TSvgReader;
50 {$IFDEF XPLAT_GENERICS}
51 fList : TList<TSvgListObject>;
52 {$ELSE}
53 fList : TList;
54 {$ENDIF}
55 fDefWidth : integer;
56 fDefHeight : integer;
57 fRecipientList : TRecipients;
58 fUpdateCnt : integer;
59 {$IFDEF MSWINDOWS}
60 fResName : string;
61 procedure SetResName(const resName: string);
62 {$ENDIF}
63 procedure SetDefWidth(value: integer);
64 procedure SetDefHeight(value: integer);
65 protected
66 procedure Changed; virtual;
67 procedure BeginUpdate;
68 procedure EndUpdate;
69 procedure NotifyRecipients(notifyFlag: TImg32Notification);
70 public
71 constructor Create;
72 destructor Destroy; override;
73 procedure Clear;
74 function Count: integer;
75 function Find(const aName: string): integer;
76 procedure AddRecipient(recipient: INotifyRecipient);
77 procedure DeleteRecipient(recipient: INotifyRecipient);
78 function CreateImage(index: integer): TImage32;
79 procedure GetImage(index: integer; image: TImage32); overload;
80 procedure GetImage(index: integer; image: TImage32; out aName: string); overload;
81 procedure Add(const aName, xml: string);
82 procedure AddFromFile(const aName, filename: string);
83 procedure AddFromResource(const aName, resName: string; resType: PChar);
84 procedure Insert(index: integer; const name, xml: string);
85 procedure Move(currentIndex, newIndex: integer);
86 procedure Delete(index: integer);
87 property DefaultWidth: integer read fDefWidth write SetDefWidth;
88 property DefaultHeight: integer read fDefHeight write SetDefHeight;
89 {$IFDEF MSWINDOWS}
90 property ResourceName: string read fResName write SetResName;
91 {$ENDIF}
92 end;
93
94 implementation
95
96 //------------------------------------------------------------------------------
97 // Three routines used to enumerate a resource type
98 //------------------------------------------------------------------------------
99
100 function Is_IntResource(lpszType: PChar): Boolean;
101 begin
102 Result := NativeUInt(lpszType) shr 16 = 0;
103 end;
104 //------------------------------------------------------------------------------
105
106 function ResourceNameToString(lpszName: PChar): string;
107 begin
108 if Is_IntResource(lpszName) then
109 Result := '#' + IntToStr(NativeUInt(lpszName)) else
110 Result := lpszName;
111 end;
112 //------------------------------------------------------------------------------
113
114 function EnumResNameProc(hModule: HMODULE; lpszType, lpszName: PChar;
115 lParam: NativeInt): Boolean; stdcall;
116 var
117 n: string;
118 begin
119 n:= ResourceNameToString(lpszName);
120 TSvgImageList32(lParam).AddFromResource(n, n, lpszType);
121 Result := true;
122 end;
123
124 //------------------------------------------------------------------------------
125 // TSvgImageList32
126 //------------------------------------------------------------------------------
127
128 constructor TSvgImageList32.Create;
129 begin
130 fReader := TSvgReader.Create;
131 {$IFDEF XPLAT_GENERICS}
132 fList := TList<TSvgListObject>.Create;
133 {$ELSE}
134 fList := TList.Create;
135 {$ENDIF}
136 end;
137 //------------------------------------------------------------------------------
138
139 destructor TSvgImageList32.Destroy;
140 begin
141 NotifyRecipients(inDestroy);
142 Clear;
143 fList.Free;
144 fReader.Free;
145 inherited;
146 end;
147 //------------------------------------------------------------------------------
148
149 {$IFDEF MSWINDOWS}
150 procedure TSvgImageList32.SetResName(const resName: string);
151 begin
152 if fResName = resName then Exit;
153 fResName := resName;
154 BeginUpdate;
155 try
156 Clear;
157 EnumResourceNames(HInstance, PChar(resName), @EnumResNameProc, lParam(self));
158 finally
159 EndUpdate;
160 end;
161 end;
162 //------------------------------------------------------------------------------
163 {$ENDIF}
164
165 function TSvgImageList32.Count: integer;
166 begin
167 result := fList.Count;
168 end;
169 //------------------------------------------------------------------------------
170
171 procedure TSvgImageList32.Clear;
172 var
173 i: integer;
174 begin
175 for i := 0 to fList.Count -1 do
176 TSvgListObject(fList[i]).Free;
177 fList.Clear;
178 Changed;
179 end;
180 //------------------------------------------------------------------------------
181
182 function TSvgImageList32.Find(const aName: string): integer;
183 var
184 i: integer;
185 begin
186 for i := 0 to fList.Count -1 do
187 with TSvgListObject(fList[i]) do
188 if SameText(name, aName) then
189 begin
190 Result := i;
191 Exit;
192 end;
193 Result := -1;
194 end;
195 //------------------------------------------------------------------------------
196
197 procedure TSvgImageList32.GetImage(index: integer; image: TImage32; out aName: string);
198 begin
199 if not Assigned(image) or (index < 0) or (index >= count) then Exit;
200 if image.IsEmpty then
201 image.SetSize(fDefWidth, fDefHeight);
202 with TSvgListObject(fList[index]) do
203 begin
204 fReader.LoadFromString(xml);
205 aName := name;
206 end;
207 fReader.DrawImage(image, true);
208 end;
209 //------------------------------------------------------------------------------
210
211 function TSvgImageList32.CreateImage(index: integer): TImage32;
212 begin
213 Result := TImage32.Create(DefaultWidth, DefaultHeight);
214 GetImage(index, Result);
215 end;
216 //------------------------------------------------------------------------------
217
218 procedure TSvgImageList32.GetImage(index: integer; image: TImage32);
219 var
220 dummy: string;
221 begin
222 GetImage(index, image, dummy);
223 end;
224 //------------------------------------------------------------------------------
225
226 procedure TSvgImageList32.Add(const aName, xml: string);
227 begin
228 Insert(count, aName, xml);
229 end;
230 //------------------------------------------------------------------------------
231
232 procedure TSvgImageList32.AddFromFile(const aName, filename: string);
233 begin
234 if not FileExists(filename) then Exit;
235 with TStringList.Create do
236 try
237 LoadFromFile(filename);
238 Self.Insert(Self.Count, aName, Text);
239 finally
240 Free;
241 end;
242 end;
243 //------------------------------------------------------------------------------
244
245 procedure TSvgImageList32.AddFromResource(const aName, resName: string; resType: PChar);
246 var
247 rs: TResourceStream;
248 ansi: AnsiString;
249 begin
250 rs := TResourceStream.Create(hInstance, resName, resType);
251 try
252 SetLength(ansi, rs.Size);
253 rs.Read(ansi[1], rs.Size);
254 Self.Insert(Self.Count, aName, string(ansi));
255 finally
256 rs.Free;
257 end;
258 end;
259 //------------------------------------------------------------------------------
260
261 procedure TSvgImageList32.Insert(index: integer; const name, xml: string);
262 var
263 lo: TSvgListObject;
264 begin
265 if index < 0 then index := 0
266 else if index > Count then index := Count;
267
268 lo := TSvgListObject.Create;
269 lo.name := name;
270 lo.xml := xml;
271 fList.Insert(index, lo);
272 Changed;
273 end;
274 //------------------------------------------------------------------------------
275
276 procedure TSvgImageList32.Move(currentIndex, newIndex: integer);
277 begin
278 fList.Move(currentIndex, newIndex);
279 end;
280 //------------------------------------------------------------------------------
281
282 procedure TSvgImageList32.Delete(index: integer);
283 begin
284 TSvgListObject(fList[index]).Free;
285 fList.Delete(index);
286 end;
287 //------------------------------------------------------------------------------
288
289 procedure TSvgImageList32.BeginUpdate;
290 begin
291 inc(fUpdateCnt);
292 end;
293 //------------------------------------------------------------------------------
294
295 procedure TSvgImageList32.EndUpdate;
296 begin
297 dec(fUpdateCnt);
298 if fUpdateCnt = 0 then Changed;
299 end;
300 //------------------------------------------------------------------------------
301
302 procedure TSvgImageList32.Changed;
303 begin
304 if (fUpdateCnt = 0) then
305 NotifyRecipients(inStateChange);
306 end;
307 //------------------------------------------------------------------------------
308
309 procedure TSvgImageList32.SetDefWidth(value: integer);
310 begin
311 if fDefWidth = value then Exit;
312 fDefWidth := value;
313 Changed;
314 end;
315 //------------------------------------------------------------------------------
316
317 procedure TSvgImageList32.SetDefHeight(value: integer);
318 begin
319 if fDefHeight = value then Exit;
320 fDefHeight := value;
321 Changed;
322 end;
323 //------------------------------------------------------------------------------
324
325 procedure TSvgImageList32.AddRecipient(recipient: INotifyRecipient);
326 var
327 len: integer;
328 begin
329 len := Length(fRecipientList);
330 SetLength(fRecipientList, len+1);
331 fRecipientList[len] := Recipient;
332 end;
333 //------------------------------------------------------------------------------
334
335 procedure TSvgImageList32.DeleteRecipient(recipient: INotifyRecipient);
336 var
337 i, highI: integer;
338 begin
339 highI := High(fRecipientList);
340 i := highI;
341 while (i >= 0) and (fRecipientList[i] <> Recipient) do dec(i);
342 if i < 0 then Exit;
343 if i < highI then
344 System.Move(fRecipientList[i+1], fRecipientList[i],
345 (highI - i) * SizeOf(INotifyRecipient));
346 SetLength(fRecipientList, highI);
347 end;
348 //------------------------------------------------------------------------------
349
350 procedure TSvgImageList32.NotifyRecipients(notifyFlag: TImg32Notification);
351 var
352 i: integer;
353 begin
354 if fUpdateCnt > 0 then Exit;
355 for i := High(fRecipientList) downto 0 do
356 try
357 //when destroying in a finalization section
358 //it's possible for recipients to have been destroyed
359 //without their destructors being called.
360 fRecipientList[i].ReceiveNotification(self, notifyFlag);
361 except
362 end;
363 end;
364
365 //------------------------------------------------------------------------------
366 // Loading (reading) SVG images from file ...
367 //------------------------------------------------------------------------------
368
369 function TImageFormat_SVG.LoadFromStream(stream: TStream;
370 img32: TImage32; imgIndex: integer = 0): Boolean;
371 var
372 r: TRectWH;
373 sx: double;
374 begin
375 with TSvgReader.Create do
376 try
377 Result := LoadFromStream(stream);
378 if not Result then Exit;
379
380 r := RootElement.viewboxWH;
381 img32.BeginUpdate;
382 try
383 if img32.IsEmpty then
384 begin
385 with RootElement do
386 if Width.IsValid and Height.IsValid then
387 img32.SetSize(
388 Round(Width.GetValue(defaultSvgWidth, 0)),
389 Round(Height.GetValue(defaultSvgHeight, 0)))
390 else if not r.IsEmpty then
391 img32.SetSize(Round(r.Width), Round(r.Height))
392 else
393 img32.SetSize(defaultSvgWidth, defaultSvgHeight);
394 end
395 else if not r.IsEmpty then
396 begin
397 // scale the SVG to best fit the image dimensions
398 sx := GetScaleForBestFit(r.Width, r.Height, img32.Width, img32.Height);
399 img32.SetSize(Round(r.Width * sx), Round(r.Height * sx));
400 end;
401
402 //draw the SVG image to fit inside the canvas
403 DrawImage(img32, True);
404 finally
405 img32.EndUpdate;
406 end;
407 finally
408 Free;
409 end;
410 end;
411
412 //------------------------------------------------------------------------------
413 // Saving (writing) SVG images to file (not currently implemented) ...
414 //------------------------------------------------------------------------------
415
416 class function TImageFormat_SVG.IsValidImageStream(stream: TStream): Boolean;
417 var
418 i, savedPos, len: integer;
419 buff: array [1..1024] of AnsiChar;
420 begin
421 Result := false;
422 savedPos := stream.Position;
423 len := Min(1024, stream.Size - savedPos);
424 stream.Read(buff[1], len);
425 stream.Position := savedPos;
426 for i := 1 to len -4 do
427 begin
428 if buff[i] < #9 then Exit
429 else if (buff[i] = '<') and
430 (buff[i +1] = 's') and
431 (buff[i +2] = 'v') and
432 (buff[i +3] = 'g') then
433 begin
434 Result := true;
435 break;
436 end;
437 end;
438 end;
439 //------------------------------------------------------------------------------
440
441 procedure TImageFormat_SVG.SaveToStream(stream: TStream;
442 img32: TImage32; quality: integer);
443 begin
444 //not enabled
445 end;
446 //------------------------------------------------------------------------------
447
448 class function TImageFormat_SVG.CanCopyToClipboard: Boolean;
449 begin
450 Result := false;
451 end;
452 //------------------------------------------------------------------------------
453
454 class function TImageFormat_SVG.CopyToClipboard(img32: TImage32): Boolean;
455 begin
456 Result := false;
457 end;
458 //------------------------------------------------------------------------------
459
460 class function TImageFormat_SVG.CanPasteFromClipboard: Boolean;
461 begin
462 Result := false;
463 end;
464 //------------------------------------------------------------------------------
465
466 class function TImageFormat_SVG.PasteFromClipboard(img32: TImage32): Boolean;
467 begin
468 Result := false;
469 end;
470
471 //------------------------------------------------------------------------------
472 //------------------------------------------------------------------------------
473
474 initialization
475 TImage32.RegisterImageFormatClass('SVG', TImageFormat_SVG, cpLow);
476
477 end.