Debian Codegraph @

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

raw
1 unit Img32.SVG.Reader;
2
3 (*******************************************************************************
4 * Author : Angus Johnson *
5 * Version : 4.8 *
6 * Date : 12 January 2025 *
7 * Website : http://www.angusj.com *
8 * Copyright : Angus Johnson 2019-2025 *
9 * *
10 * Purpose : Read SVG 2.0 files *
11 * *
12 * License : Use, modification & distribution is subject to *
13 * Boost Software License Ver 1 *
14 * http://www.boost.org/LICENSE_1_0.txt *
15 *******************************************************************************)
16
17 interface
18
19 {$I Img32.inc}
20
21 uses
22 SysUtils, Classes, Types, Math,
23 {$IFDEF XPLAT_GENERICS} Generics.Collections, Generics.Defaults,{$ENDIF}
24 Img32, Img32.SVG.Core, Img32.SVG.Path, Img32.Vector,
25 Img32.Draw, Img32.Text, Img32.Transform;
26
27 {$IFDEF ZEROBASEDSTR}
28 {$ZEROBASEDSTRINGS OFF}
29 {$ENDIF}
30
31 type
32
33 TBaseElement = class;
34 TElementClass = class of TBaseElement;
35
36 TDrawData = record
37 currentColor : TColor32;
38 fillColor : TColor32;
39 fillOpacity : double;
40 fillRule : TFillRule;
41 fillEl : UTF8String;
42 strokeColor : TColor32;
43 strokeOpacity : double;
44 strokeWidth : TValue;
45 strokeCap : TEndStyle;
46 strokeJoin : TJoinStyle;
47 strokeMitLim : double;
48 strokeEl : UTF8String;
49 dashArray : TArrayOfDouble;
50 dashOffset : double;
51 fontInfo : TSVGFontInfo;
52 markerStart : UTF8String;
53 markerMiddle : UTF8String;
54 markerEnd : UTF8String;
55 filterElRef : UTF8String;
56 maskElRef : UTF8String;
57 clipElRef : UTF8String;
58 matrix : TMatrixD;
59 visible : Boolean;
60 useEl : TBaseElement; // to check for and prevent <USE> recursion
61 bounds : TRectD;
62 end;
63
64 PSvgIdNameHashMapItem = ^TSvgIdNameHashMapItem;
65 TSvgIdNameHashMapItem = record
66 Hash: Cardinal;
67 Next: Integer;
68 Name: UTF8String;
69 Element: TBaseElement;
70 end;
71
72 TSvgIdNameHashMap = class(TObject)
73 private
74 FItems: array of TSvgIdNameHashMapItem;
75 FBuckets: TArrayOfInteger;
76 FCount: Integer;
77 FMod: Cardinal;
78 procedure Grow;
79 function FindItemIndex(const Name: UTF8String): Integer;
80 public
81 procedure AddOrIgnore(const idName: UTF8String; element: TBaseElement);
82 function FindElement(const idName: UTF8String): TBaseElement;
83 procedure Clear;
84 end;
85
86 TSvgReader = class;
87
88 TBaseElement = class
89 private
90 fParent : TBaseElement;
91 fXmlEl : TSvgXmlEl;
92 fSvgReader : TSvgReader;
93 {$IFDEF XPLAT_GENERICS}
94 fChilds : TList<TBaseElement>;
95 {$ELSE}
96 fChilds : TList;
97 {$ENDIF}
98 fId : UTF8String;
99 fDrawData : TDrawData; // currently both static and dynamic vars
100 function FindRefElement(const refname: UTF8String): TBaseElement;
101 function GetChildCount: integer;
102 function GetChild(index: integer): TBaseElement;
103 function FindChild(const idName: UTF8String): TBaseElement;
104 protected
105 elRectWH : TValueRecWH; // multifunction variable
106 function IsFirstChild: Boolean;
107 procedure LoadAttributes;
108 procedure LoadAttribute(attrib: PSvgAttrib);
109 function LoadContent: Boolean; virtual;
110 // GetRelFracLimit: ie when to assume untyped vals are relative vals
111 function GetRelFracLimit: double; virtual;
112 procedure Draw(image: TImage32; drawDat: TDrawData); virtual;
113 procedure DrawChildren(image: TImage32; const drawDat: TDrawData);
114 public
115 constructor Create(parent: TBaseElement; svgEl: TSvgXmlEl); virtual;
116 destructor Destroy; override;
117 property Child[index: integer]: TBaseElement read GetChild; default;
118 property ChildCount: integer read GetChildCount;
119 property DrawData: TDrawData read fDrawData write fDrawData;
120 property Id: UTF8String read fId;
121 end;
122
123 TShapeElement = class(TBaseElement)
124 protected
125 hasPaths : Boolean;
126 pathsLoaded : Boolean;
127 drawPathsO : TPathsD; //open only
128 drawPathsC : TPathsD; //closed only
129 function GetBounds: TRectD; virtual;
130 function HasMarkers: Boolean;
131 procedure GetPaths(const drawDat: TDrawData); virtual;
132 // GetSimplePath: is only required for markers
133 function GetSimplePath(const drawDat: TDrawData): TPathsD; virtual;
134 procedure DrawFilled(img: TImage32;
135 const paths: TPathsD; drawDat: TDrawData);
136 procedure DrawStroke(img: TImage32;
137 const paths: TPathsD; drawDat: TDrawData; isClosed: Boolean);
138 procedure DrawMarkers(img: TImage32; drawDat: TDrawData);
139 procedure Draw(image: TImage32; drawDat: TDrawData); override;
140 public
141 constructor Create(parent: TBaseElement; svgEl: TSvgXmlEl); override;
142 end;
143
144 TSvgElement = class(TShapeElement)
145 protected
146 procedure Draw(image: TImage32; drawDat: TDrawData); override;
147 public
148 viewboxWH : TRectWH;
149 function Width : TValue;
150 function Height : TValue;
151 end;
152
153 TSvgReader = class
154 private
155 fSvgParser : TSvgParser;
156 fBkgndColor : TColor32;
157 fBackgndImage : TImage32;
158 fTempImage : TImage32;
159 fBlurQuality : integer;
160 fIdList : TSvgIdNameHashMap;
161 fLinGradRenderer : TLinearGradientRenderer;
162 fRadGradRenderer : TSvgRadialGradientRenderer;
163 fCustomRendererCache: TCustomRendererCache;
164 fRootElement : TSvgElement;
165 fFontCache : TFontCache;
166 fUsePropScale : Boolean;
167 fSimpleDraw : Boolean;
168 fSimpleDrawList : TList;
169 function LoadInternal: Boolean;
170 function GetIsEmpty: Boolean;
171 function GetTempImage: TImage32;
172 procedure SetBlurQuality(quality: integer);
173 protected
174 userSpaceBounds : TRectD;
175 currentColor : TColor32;
176 procedure GetBestFont(const svgFontInfo: TSVGFontInfo);
177 property RadGradRenderer: TSvgRadialGradientRenderer read fRadGradRenderer;
178 property LinGradRenderer: TLinearGradientRenderer read fLinGradRenderer;
179 property BackgndImage : TImage32 read fBackgndImage;
180 property TempImage : TImage32 read GetTempImage;
181 public
182 constructor Create;
183 destructor Destroy; override;
184 procedure Clear;
185 procedure DrawImage(img: TImage32; scaleToImage: Boolean);
186 function LoadFromStream(stream: TStream): Boolean;
187 function LoadFromFile(const filename: string): Boolean;
188 function LoadFromString(const str: string): Boolean;
189
190 // The following two methods are deprecated and intended only for ...
191 // https://github.com/EtheaDev/SVGIconImageList
192 procedure SetOverrideFillColor(color: TColor32); //deprecated;
193 procedure SetOverrideStrokeColor(color: TColor32); //deprecated;
194
195 function FindElement(const idName: UTF8String): TBaseElement;
196 property BackgroundColor : TColor32 read fBkgndColor write fBkgndColor;
197 property BlurQuality : integer read fBlurQuality write SetBlurQuality;
198 property IsEmpty : Boolean read GetIsEmpty;
199 // KeepAspectRatio: this property has also been added for the convenience of
200 // the third-party SVGIconImageList. (IMHO it should always = true)
201 property KeepAspectRatio: Boolean
202 read fUsePropScale write fUsePropScale;
203 property RootElement : TSvgElement read fRootElement;
204 // RecordSimpleDraw: record simple drawing instructions
205 property RecordSimpleDraw: Boolean read fSimpleDraw write fSimpleDraw;
206 // SimpleDrawList: list of PSimpleDrawData records;
207 property SimpleDrawList : TList read fSimpleDrawList;
208 end;
209
210 PSimpleDrawData = ^TSimpleDrawData;
211 TSimpleDrawData = record
212 paths : TPathsD;
213 fillRule : TFillRule;
214 color : TColor32;
215 tag : integer;
216 end;
217
218 var
219 // https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/width
220 defaultSvgWidth: integer = 300;
221 // https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/height
222 defaultSvgHeight: integer = 150;
223
224 implementation
225
226 uses
227 Img32.Extra, Img32.Clipper2;
228
229 type
230 TFourDoubles = array [0..3] of double;
231
232 TDefsElement = class(TBaseElement)
233 public
234 constructor Create(parent: TBaseElement; svgEl: TSvgXmlEl); override;
235 end;
236
237 TStyleElement = class(TBaseElement)
238 public
239 constructor Create(parent: TBaseElement; svgEl: TSvgXmlEl); override;
240 end;
241
242 // TImageElement only supports *embedded* jpg & png images.
243 // And it requires Img32.Fmt.JPG & Img32.Fmt.PNG to be included
244 // in the USES clause of at least one of the application's units.
245 // (nb: If using the FMX framework, then add Img32.FMX instead of
246 // Img32.Fmt.JPG & Img32.Fmt.PNG to the USES clause.)
247 TImageElement = class(TBaseElement)
248 private
249 fRefEl: UTF8String;
250 fImage: TImage32;
251 fTransparent: Boolean;
252 protected
253 procedure Draw(image: TImage32; drawDat: TDrawData); override;
254 public
255 destructor Destroy; override;
256 end;
257
258 TGroupElement = class(TShapeElement)
259 protected
260 procedure Draw(image: TImage32; drawDat: TDrawData); override;
261 end;
262
263 TSwitchElement = class(TShapeElement)
264 protected
265 procedure Draw(image: TImage32; drawDat: TDrawData); override;
266 end;
267
268 TUseElement = class(TShapeElement)
269 private
270 callerUse: TBaseElement;
271 function ValidateNonRecursion(el: TBaseElement): Boolean;
272 protected
273 fRefEl: UTF8String;
274 procedure GetPaths(const drawDat: TDrawData); override;
275 procedure Draw(img: TImage32; drawDat: TDrawData); override;
276 end;
277
278 TMaskElement = class(TShapeElement)
279 protected
280 maskRec: TRect;
281 procedure GetPaths(const drawDat: TDrawData); override;
282 procedure ApplyMask(img: TImage32; const drawDat: TDrawData);
283 end;
284
285 TSymbolElement = class(TShapeElement)
286 protected
287 viewboxWH: TRectWH;
288 public
289 constructor Create(parent: TBaseElement; svgEl: TSvgXmlEl); override;
290 end;
291
292 //-------------------------------------
293
294 TPathElement = class(TShapeElement)
295 private
296 fSvgPaths : TSvgPath;
297 procedure Flatten(index: integer; scalePending: double;
298 out path: TPathD; out isClosed: Boolean);
299 protected
300 function GetBounds: TRectD; override;
301 procedure ParseDAttrib(const value: UTF8String);
302 procedure GetPaths(const drawDat: TDrawData); override;
303 function GetSimplePath(const drawDat: TDrawData): TPathsD; override;
304 public
305 constructor Create(parent: TBaseElement; svgEl: TSvgXmlEl); override;
306 destructor Destroy; override;
307 end;
308
309 TPolyElement = class(TShapeElement) //polyline or polygon
310 protected
311 path : TPathD;
312 function GetBounds: TRectD; override;
313 procedure ParsePoints(const value: UTF8String);
314 procedure GetPaths(const drawDat: TDrawData); override;
315 function GetSimplePath(const drawDat: TDrawData): TPathsD; override;
316 end;
317
318 TLineElement = class(TShapeElement)
319 protected
320 path : TPathD;
321 function GetBounds: TRectD; override;
322 procedure GetPaths(const drawDat: TDrawData); override;
323 function GetSimplePath(const drawDat: TDrawData): TPathsD; override;
324 public
325 constructor Create(parent: TBaseElement; svgEl: TSvgXmlEl); override;
326 end;
327
328 TCircleElement = class(TShapeElement)
329 protected
330 bounds : TRectD;
331 centerPt : TValuePt;
332 radius : TValue;
333 function GetBounds: TRectD; override;
334 procedure GetPaths(const drawDat: TDrawData); override;
335 public
336 constructor Create(parent: TBaseElement; svgEl: TSvgXmlEl); override;
337 end;
338
339 TEllipseElement = class(TShapeElement)
340 protected
341 bounds : TRectD;
342 centerPt : TValuePt;
343 radius : TValuePt;
344 function GetBounds: TRectD; override;
345 procedure GetPaths(const drawDat: TDrawData); override;
346 public
347 constructor Create(parent: TBaseElement; svgEl: TSvgXmlEl); override;
348 end;
349
350 TRectElement = class(TShapeElement)
351 protected
352 radius : TValuePt;
353 function GetBounds: TRectD; override;
354 procedure GetPaths(const drawDat: TDrawData); override;
355 function GetSimplePath(const drawDat: TDrawData): TPathsD; override;
356 public
357 constructor Create(parent: TBaseElement; svgEl: TSvgXmlEl); override;
358 end;
359
360 TTSpanElement = class;
361
362 // TTextElement: although a TShapeElement descendant, it's really just
363 // a container for other TShapeElements (<tspan>, <textpath> etc).
364 TTextElement = class(TShapeElement)
365 protected
366 offset : TValuePt;
367 textDx : double;
368 angle : TArrayOfDouble;
369 currentPt : TPointD;
370 currSpanEl : TTSpanElement; //the current 'real' <tspan>
371 lastChrSpc : Boolean;
372 procedure Draw(img: TImage32; drawDat: TDrawData); override;
373 public
374 constructor Create(parent: TBaseElement; svgEl: TSvgXmlEl); override;
375 end;
376
377 TTextSubElement = class(TShapeElement)
378 protected
379 offset : TValuePt;
380 textEl : TTextElement;
381 function GetTextEl: TTextElement;
382 end;
383
384 TTSpanElement = class(TTextSubElement)
385 protected
386 chunkDx : double;
387 angle : TArrayOfDouble;
388 procedure GetPaths(const drawDat: TDrawData); override;
389 public
390 procedure Draw(image: TImage32; drawDat: TDrawData); override;
391 end;
392
393 TTextPathElement = class(TTextSubElement)
394 private
395 pathEl: TPathElement;
396 scale: double;
397 protected
398 pathName : UTF8String; //name (id) of path element
399 procedure GetPathsInternal(el: TBaseElement; const drawDat: TDrawData);
400 procedure GetPaths(const drawDat: TDrawData); override;
401 function GetBounds: TRectD; override;
402 public
403 procedure Draw(image: TImage32; drawDat: TDrawData); override;
404 end;
405
406 TTextAreaElement = class(TShapeElement)
407 protected
408 procedure GetPaths(const drawDat: TDrawData); override;
409 end;
410
411 TMarkerElement = class(TShapeElement)
412 private
413 fPoints : TPathD;
414 protected
415 refPt : TValuePt;
416 angle : double;
417 angle2 : double;
418 markerBoxWH : TRectWH;
419 autoStartReverse : Boolean;
420 procedure SetEndPoint(const pt: TPointD; angle: double);
421 function SetMiddlePoints(const points: TPathD): Boolean;
422 procedure Draw(img: TImage32; drawDat: TDrawData); override;
423 public
424 constructor Create(parent: TBaseElement; svgEl: TSvgXmlEl); override;
425 end;
426
427 TSvgColorStop = record
428 offset : double;
429 color : TColor32;
430 end;
431 TSvgColorStops = array of TSvgColorStop;
432
433 TFillElement = class(TBaseElement)
434 protected
435 refEl : UTF8String;
436 units : Cardinal;
437 function GetRelFracLimit: double; override;
438 end;
439
440 TPatternElement = class(TFillElement)
441 protected
442 ImgRenderer : TImageRenderer;
443 pattBoxWH : TRectWH;
444 function PrepareRenderer(renderer: TImageRenderer;
445 drawDat: TDrawData): Boolean; virtual;
446 public
447 constructor Create(parent: TBaseElement; svgEl: TSvgXmlEl); override;
448 destructor Destroy; override;
449 end;
450
451 //nb: gradients with objectBoundingBox should not be applied to
452 //elements without width and height.
453 TGradientElement = class(TFillElement)
454 protected
455 stops : TSvgColorStops;
456 spreadMethod : TGradientFillStyle;
457 function LoadContent: Boolean; override;
458 procedure AddStop(color: TColor32; offset: double);
459 procedure AssignTo(other: TBaseElement); virtual;
460 function PrepareRenderer(renderer: TCustomGradientRenderer;
461 drawDat: TDrawData): Boolean; virtual;
462 procedure AddColorStopsToRenderer(renderer: TCustomGradientRenderer);
463 end;
464
465 TRadGradElement = class(TGradientElement)
466 protected
467 radius: TValuePt;
468 F, C: TValuePt;
469 procedure AssignTo(other: TBaseElement); override;
470 function PrepareRenderer(renderer: TCustomGradientRenderer;
471 drawDat: TDrawData): Boolean; override;
472 public
473 constructor Create(parent: TBaseElement; svgEl: TSvgXmlEl); override;
474 end;
475
476 TLinGradElement = class(TGradientElement)
477 protected
478 startPt, endPt: TValuePt;
479 procedure AssignTo(other: TBaseElement); override;
480 function PrepareRenderer(renderer: TCustomGradientRenderer;
481 drawDat: TDrawData): Boolean; override;
482 public
483 constructor Create(parent: TBaseElement; svgEl: TSvgXmlEl); override;
484 end;
485
486 TGradStopElement = class(TBaseElement)
487 protected
488 offset: double;
489 color: TColor32;
490 public
491 constructor Create(parent: TBaseElement; svgEl: TSvgXmlEl); override;
492 end;
493
494 TFilterElement = class(TBaseElement)
495 private
496 fSrcImg : TImage32;
497 fLastImg : TImage32;
498 fScale : double;
499 fFilterBounds : TRect;
500 fObjectBounds : TRect;
501 fImages : array of TImage32;
502 fNames : array of UTF8String;
503 protected
504 procedure Clear;
505 function GetRelFracLimit: double; override;
506 function GetAdjustedBounds(const bounds: TRectD): TRectD;
507 function FindNamedImage(const name: UTF8String): TImage32;
508 function AddNamedImage(const name: UTF8String): TImage32;
509 function GetNamedImage(const name: UTF8String; isIn: Boolean): TImage32;
510 procedure Apply(img: TImage32;
511 const filterBounds: TRect; const matrix: TMatrixD);
512 public
513 constructor Create(parent: TBaseElement; svgEl: TSvgXmlEl); override;
514 destructor Destroy; override;
515 end;
516
517 TFeBaseElement = class(TBaseElement)
518 private
519 function GetParentAsFilterEl: TFilterElement;
520 protected
521 in1: UTF8String;
522 in2: UTF8String;
523 res: UTF8String;
524 srcImg, dstImg: TImage32;
525 srcRec, dstRec: TRect;
526 function GetSrcAndDst: Boolean;
527 function GetBounds(img: TImage32): TRect;
528 procedure Apply; virtual; abstract;
529 property ParentFilterEl: TFilterElement read GetParentAsFilterEl;
530 end;
531
532 TFeBlendElement = class(TFeBaseElement)
533 protected
534 procedure Apply; override;
535 end;
536
537 TFeImageElement = class(TFeBaseElement)
538 private
539 refEl: UTF8String;
540 fImage: TImage32;
541 protected
542 procedure Apply; override;
543 public
544 destructor Destroy; override;
545 end;
546
547 TCompositeOp = (coOver, coIn, coOut, coAtop, coXOR, coArithmetic);
548
549 TFeCompositeElement = class(TFeBaseElement)
550 protected
551 fourKs: TFourDoubles; //arithmetic constants
552 compositeOp: TCompositeOp;
553 procedure Apply; override;
554 public
555 constructor Create(parent: TBaseElement; svgEl: TSvgXmlEl); override;
556 end;
557
558 TFeColorMatrixElement = class(TFeBaseElement)
559 protected
560 values: TArrayOfDouble;
561 procedure Apply; override;
562 end;
563
564 TFuncType = (ftIdentity, ftTable, ftDiscrete, ftLinear, ftGamma);
565
566 TFeComponentTransferElement = class(TFeBaseElement)
567 protected
568 procedure Apply; override;
569 end;
570
571 TFeComponentTransferChild = class(TBaseElement)
572 protected
573 bytes: TArrayOfByte;
574 protected
575 funcType: TFuncType;
576 intercept: double;
577 slope: double;
578 tableValues: TArrayOfDouble;
579 end;
580
581 TFeFuncRElement = class(TFeComponentTransferChild)
582 end;
583 TFeFuncGElement = class(TFeComponentTransferChild)
584 end;
585 TFeFuncBElement = class(TFeComponentTransferChild)
586 end;
587 TFeFuncAElement = class(TFeComponentTransferChild)
588 end;
589
590 TFeDefuseLightElement = class(TFeBaseElement)
591 protected
592 color : TColor32;
593 surfaceScale : double;
594 diffuseConst : double;
595 kernelSize : integer;
596 procedure Apply; override;
597 end;
598
599 TFeDropShadowElement = class(TFeBaseElement)
600 protected
601 stdDev : double;
602 offset : TValuePt;
603 floodColor : TColor32;
604 procedure Apply; override;
605 public
606 constructor Create(parent: TBaseElement; svgEl: TSvgXmlEl); override;
607 end;
608
609 TFeFloodElement = class(TFeBaseElement)
610 protected
611 floodColor : TColor32;
612 procedure Apply; override;
613 public
614 constructor Create(parent: TBaseElement; svgEl: TSvgXmlEl); override;
615 end;
616
617 TFeGaussElement = class(TFeBaseElement)
618 protected
619 stdDev: double;
620 procedure Apply; override;
621 public
622 constructor Create(parent: TBaseElement; svgEl: TSvgXmlEl); override;
623 end;
624
625 TFeMergeElement = class(TFeBaseElement)
626 protected
627 procedure Apply; override;
628 end;
629
630 TFeMergeNodeElement = class(TFeBaseElement)
631 protected
632 procedure Apply; override;
633 end;
634
635 TFeOffsetElement = class(TFeBaseElement)
636 protected
637 offset : TValuePt;
638 procedure Apply; override;
639 end;
640
641 TFePointLightElement = class(TFeBaseElement)
642 protected
643 z : double;
644 end;
645
646 TFeSpecLightElement = class(TFeBaseElement)
647 protected
648 exponent : double;
649 color : TColor32;
650 procedure Apply; override;
651 end;
652
653 TClipPathElement = class(TShapeElement)
654 protected
655 units: Cardinal;
656 procedure GetPaths(const drawDat: TDrawData); override;
657 public
658 constructor Create(parent: TBaseElement; svgEl: TSvgXmlEl); override;
659 end;
660 //-------------------------------------
661
662
663 const
664 buffSize = 32;
665 clAlphaSet = $00010101;
666 SourceImage : UTF8String = 'SourceGraphic';
667 //SourceAlpha : UTF8String = 'SourceAlpha';
668 tmpFilterImg : UTF8String = 'tmp';
669
670 //https://www.w3.org/TR/css-fonts-3/#font-family-prop
671 emptyDrawInfo: TDrawData =
672 (currentColor: clInvalid;
673 fillColor: clInvalid; fillOpacity: InvalidD;
674 fillRule: frNegative; fillEl: '';
675 strokeColor: clInvalid; strokeOpacity: InvalidD;
676 strokeWidth: (rawVal: InvalidD; unitType: utNumber);
677 strokeCap: esPolygon; strokeJoin: jsMiter; strokeMitLim: 0.0; strokeEl : '';
678 dashArray: nil; dashOffset: 0;
679 fontInfo: (family: tfUnknown; familyNames: nil; size: 0; spacing: 0.0;
680 spacesInText: sitUndefined; textLength: 0; italic: sfsUndefined;
681 weight: -1; align: staUndefined; decoration: fdUndefined;
682 baseShift: (rawVal: InvalidD; unitType: utNumber));
683 markerStart: ''; markerMiddle: ''; markerEnd: '';
684 filterElRef: ''; maskElRef: ''; clipElRef: '';
685 matrix: ((1, 0, 0),(0, 1, 0),(0, 0, 1)); visible: true;
686 useEl: nil; bounds: (Left:0; Top:0; Right:0; Bottom:0));
687
688 var
689 //defaultFontHeight: this size will be used to retrieve ALL glyph contours
690 //(and later scaled as necessary). This relatively large default ensures
691 //that contours will have adequate detail.
692 defaultFontHeight: double = 20.0;
693
694 //------------------------------------------------------------------------------
695 // Miscellaneous functions ...
696 //------------------------------------------------------------------------------
697
698 function HashToElementClass(hash: Cardinal): TElementClass;
699 begin
700 case hash of
701 hClippath : Result := TClipPathElement;
702 hCircle : Result := TCircleElement;
703 hDefs : Result := TDefsElement;
704 hEllipse : Result := TEllipseElement;
705 hFilter : Result := TFilterElement;
706 hfeBlend : Result := TFeBlendElement;
707 hfeColorMatrix : Result := TFeColorMatrixElement;
708 hFeComponentTransfer : Result := TFeComponentTransferElement;
709 hFeFuncR : Result := TFeFuncRElement;
710 hFeFuncG : Result := TFeFuncGElement;
711 hFeFuncB : Result := TFeFuncBElement;
712 hFeFuncA : Result := TFeFuncAElement;
713 hfeComposite : Result := TFeCompositeElement;
714 hfeDefuseLighting : Result := TFeDefuseLightElement;
715 hfeDropShadow : Result := TFeDropShadowElement;
716 hfeFlood : Result := TFeFloodElement;
717 hFeGaussianBlur : Result := TFeGaussElement;
718 hFeImage : Result := TFeImageElement;
719 hfeMerge : Result := TFeMergeElement;
720 hfeMergeNode : Result := TFeMergeNodeElement;
721 hfeOffset : Result := TFeOffsetElement;
722 hfePointLight : Result := TFePointLightElement;
723 hfeSpecularLighting : Result := TFeSpecLightElement;
724 hG : Result := TGroupElement;
725 hImage : Result := TImageElement;
726 hLine : Result := TLineElement;
727 hLineargradient : Result := TLinGradElement;
728 hMarker : Result := TMarkerElement;
729 hMask : Result := TMaskElement;
730 hPath : Result := TPathElement;
731 hPattern : Result := TPatternElement;
732 hPolyline : Result := TPolyElement;
733 hPolygon : Result := TPolyElement;
734 hRadialgradient : Result := TRadGradElement;
735 hRect : Result := TRectElement;
736 hStop : Result := TGradStopElement;
737 hStyle : Result := TStyleElement;
738 hSvg : Result := TSvgElement;
739 hSwitch : Result := TSwitchElement;
740 hSymbol : Result := TSymbolElement;
741 hText : Result := TTextElement;
742 hTextArea : Result := TTextAreaElement;
743 hTextPath : Result := TTextPathElement;
744 hTSpan : Result := TTSpanElement;
745 hUse : Result := TUseElement;
746 else Result := TBaseElement; //use generic class
747 end;
748 end;
749 //------------------------------------------------------------------------------
750
751 procedure UpdateDrawInfo(var drawDat: TDrawData; thisElement: TBaseElement);
752 begin
753 with thisElement.fDrawData do
754 begin
755 if currentColor <> clInvalid then
756 thisElement.fSvgReader.currentColor := currentColor;
757 if fillRule <> frNegative then
758 drawDat.fillRule := fillRule;
759 if (fillColor = clCurrent) then
760 drawDat.fillColor := thisElement.fSvgReader.currentColor
761 else if (fillColor <> clInvalid) then
762 drawDat.fillColor := fillColor;
763 if fillOpacity <> InvalidD then
764 drawDat.fillOpacity := fillOpacity;
765 if (fillEl <> '') then
766 drawDat.fillEl := fillEl;
767 if (strokeColor = clCurrent) then
768 drawDat.strokeColor := thisElement.fSvgReader.currentColor
769 else if strokeColor <> clInvalid then
770 drawDat.strokeColor := strokeColor;
771 if strokeOpacity <> InvalidD then
772 drawDat.strokeOpacity := strokeOpacity;
773 if strokeWidth.IsValid then
774 drawDat.strokeWidth := strokeWidth;
775 if strokeCap = esPolygon then
776 drawDat.strokeCap := strokeCap;
777 if strokeJoin <> jsMiter then
778 drawDat.strokeJoin := strokeJoin;
779 if strokeMitLim > 0 then
780 drawDat.strokeMitLim := strokeMitLim;
781 if Assigned(dashArray) then
782 drawDat.dashArray := Copy(dashArray, 0, Length(dashArray));
783 if dashOffset <> 0 then
784 drawDat.dashOffset := dashOffset;
785 if (strokeEl <> '') then
786 drawDat.strokeEl := strokeEl;
787
788 if (clipElRef <> '') then
789 drawDat.clipElRef := clipElRef;
790 if (maskElRef <> '') then
791 drawDat.maskElRef := maskElRef;
792 if (filterElRef <> '') then
793 drawDat.filterElRef := filterElRef;
794 if not IsIdentityMatrix(matrix) then
795 MatrixMultiply2(matrix, drawDat.matrix);
796 end;
797 end;
798 //------------------------------------------------------------------------------
799
800 procedure UpdateFontInfo(var drawDat: TDrawData; thisElement: TBaseElement);
801 begin
802 with thisElement.fDrawData do
803 begin
804 if fontInfo.family <> tfUnknown then
805 begin
806 drawDat.fontInfo.family := fontInfo.family;
807 drawDat.fontInfo.familyNames := nil;
808 end;
809 if Assigned(fontInfo.familyNames) then
810 drawDat.fontInfo.familyNames := fontInfo.familyNames;
811
812 if fontInfo.size > 0 then
813 drawDat.fontInfo.size := fontInfo.size;
814 if fontInfo.spacing <> 0 then
815 drawDat.fontInfo.spacing := fontInfo.spacing;
816 if fontInfo.textLength > 0 then
817 drawDat.fontInfo.textLength := fontInfo.textLength;
818
819 if (fontInfo.italic <> sfsUndefined) then
820 drawDat.fontInfo.italic := fontInfo.italic;
821 if (fontInfo.weight <> -1) then
822 drawDat.fontInfo.weight := fontInfo.weight;
823
824 if fontInfo.align <> staUndefined then
825 drawDat.fontInfo.align := fontInfo.align;
826
827 if fontInfo.spacesInText <> sitUndefined then
828 drawDat.fontInfo.spacesInText := fontInfo.spacesInText;
829
830 if (thisElement is TTextElement) or
831 (fontInfo.decoration <> fdUndefined) then
832 drawDat.fontInfo.decoration := fontInfo.decoration;
833 if fontInfo.baseShift.IsValid then
834 drawDat.fontInfo.baseShift := fontInfo.baseShift;
835 end;
836 end;
837 //------------------------------------------------------------------------------
838
839 function IsFilled(const drawDat: TDrawData): Boolean;
840 begin
841 with drawDat do
842 Result := (fillOpacity <> 0) and
843 ((fillColor <> clNone32) or (fillEl <> ''));
844 end;
845 //------------------------------------------------------------------------------
846
847 function IsStroked(const drawDat: TDrawData): Boolean;
848 begin
849 with drawDat do
850 if (strokeOpacity = 0) then
851 Result := false
852 else if (strokeEl = '') and
853 ((strokeColor = clNone32) or (strokeColor = clInvalid)) then
854 Result := false
855 else
856 Result := ((strokeWidth.rawVal = InvalidD) or (strokeWidth.rawVal > 0));
857 end;
858 //------------------------------------------------------------------------------
859
860 function MergeColorAndOpacity(color: TColor32; opacity: double): TColor32;
861 begin
862 if (opacity < 0) or (opacity >= 1.0) then Result := color or $FF000000
863 else if opacity = 0 then Result := clNone32
864 else Result := (color and $FFFFFF) + Round(opacity * 255) shl 24;
865 end;
866 //------------------------------------------------------------------------------
867
868 function UTF8StringToFloat(const ansiValue: UTF8String;
869 out value: double): Boolean;
870 var
871 c: PUTF8Char;
872 begin
873 c := PUTF8Char(ansiValue);
874 Result := ParseNextNum(c, c + Length(ansiValue), false, value);
875 end;
876 //------------------------------------------------------------------------------
877
878 function UTF8StringToFloatEx(const ansiValue: UTF8String;
879 var value: double; out measureUnit: TUnitType): Boolean;
880 var
881 c: PUTF8Char;
882 begin
883 c := PUTF8Char(ansiValue);
884 Result := ParseNextNumEx(c, c + Length(ansiValue), false, value, measureUnit);
885 end;
886 //------------------------------------------------------------------------------
887
888 procedure UTF8StringToOpacity(const ansiValue: UTF8String; var color: TColor32);
889 var
890 opacity: double;
891 begin
892 if color = clNone32 then
893 begin
894 color := clAlphaSet;
895 Exit;
896 end;
897
898 if color = clInvalid then color := clNone32;
899 if not UTF8StringToFloat(ansiValue, opacity) then Exit;
900 with TARGB(color) do
901 if (opacity <= 0) then
902 begin
903 if Color = clNone32 then Color := clAlphaSet
904 else A := 0;
905 end
906 else if (opacity >= 1) then A := 255
907 else A := Round(255 * opacity);
908 end;
909 //------------------------------------------------------------------------------
910
911 // Note: This MatrixApply() is a function, whereas in Img32.Transform it's a procedure.
912 function MatrixApply(const paths: TPathsD; const matrix: TMatrixD): TPathsD; overload;
913 var
914 i,j,len,len2: integer;
915 pp,rr: PPointD;
916 begin
917 if not Assigned(paths) then
918 Result := nil
919 else if IsIdentityMatrix(matrix) then
920 Result := CopyPaths(paths)
921 else
922 begin
923 len := Length(paths);
924 SetLength(Result, len);
925 for i := 0 to len -1 do
926 begin
927 len2 := Length(paths[i]);
928 NewPointDArray(Result[i], len2, True);
929 if len2 = 0 then Continue;
930 pp := @paths[i][0];
931 rr := @Result[i][0];
932 for j := 0 to High(paths[i]) do
933 begin
934 rr.X := pp.X * matrix[0, 0] + pp.Y * matrix[1, 0] + matrix[2, 0];
935 rr.Y := pp.X * matrix[0, 1] + pp.Y * matrix[1, 1] + matrix[2, 1];
936 inc(pp); inc(rr);
937 end;
938 end;
939 end;
940 end;
941 //------------------------------------------------------------------------------
942
943 function FixSpaces(const text: UnicodeString; trimLeadingSpace: Boolean): UnicodeString;
944 var
945 i,j, len: integer;
946 begin
947 //changes \r\n\t chars to spaces
948 //and trims consecutive spaces
949 len := Length(text);
950 SetLength(Result, len);
951 if len = 0 then Exit;
952
953 if trimLeadingSpace then
954 begin
955 i := 1;
956 while (i <= len) and (text[i] <= #32) do inc(i);
957 if i > len then
958 begin
959 Result := '';
960 Exit;
961 end;
962 Result[1] := text[i];
963 inc(i);
964 end else
965 begin
966 // allow a single leading space char
967 if text[1] <= #32 then
968 Result[1] := #32
969 else
970 Result[1] := text[1];
971 i := 2;
972 end;
973
974 j := 1;
975 for i := i to len do
976 begin
977 if (text[i] <= #32) then
978 begin
979 if (Result[j] = #32) then Continue;
980 inc(j);
981 Result[j] := #32;
982 end else
983 begin
984 inc(j);
985 Result[j] := text[i];
986 end;
987 end;
988 SetLength(Result, j);
989 end;
990 //------------------------------------------------------------------------------
991
992 function IsBlankText(const text: UnicodeString): Boolean;
993 var
994 i: integer;
995 begin
996 Result := false;
997 for i := 1 to Length(text) do
998 if (text[i] > #32) and (text[i] <> #160) then Exit;
999 Result := true;
1000 end;
1001 //------------------------------------------------------------------------------
1002
1003 function SvgTextAlignToTextAlign(svgAlign: TSvgTextAlign): TTextAlign;
1004 begin
1005 case svgAlign of
1006 staCenter: Result := taCenter;
1007 staRight: Result := taRight;
1008 staJustify: Result := taJustify;
1009 else Result := taLeft;
1010 end;
1011 end;
1012
1013 //------------------------------------------------------------------------------
1014 // TSvgIdNameHashMap
1015 //------------------------------------------------------------------------------
1016
1017 procedure TSvgIdNameHashMap.Grow;
1018 var
1019 Len, I: Integer;
1020 Index: Integer;
1021 begin
1022 Len := Length(FItems);
1023 if Len < 5 then
1024 Len := 5
1025 else
1026 Len := Len * 2;
1027
1028 SetLength(FItems, Len);
1029 FMod := Cardinal(Len);
1030 if not Odd(FMod) then
1031 Inc(FMod);
1032 SetLengthUninit(FBuckets, FMod);
1033 FillChar(FBuckets[0], FMod * SizeOf(FBuckets[0]), $FF);
1034
1035 // Rehash
1036 for I := 0 to FCount - 1 do
1037 begin
1038 Index := (FItems[I].Hash and $7FFFFFFF) mod FMod;
1039 FItems[I].Next := FBuckets[Index];
1040 FBuckets[Index] := I;
1041 end;
1042 end;
1043 //------------------------------------------------------------------------------
1044
1045 function TSvgIdNameHashMap.FindItemIndex(const Name: UTF8String): Integer;
1046 var
1047 hash: Cardinal;
1048 begin
1049 Result := -1;
1050 if FMod = 0 then Exit;
1051 Hash := GetHash(Name);
1052 Result := FBuckets[(Hash and $7FFFFFFF) mod FMod];
1053 while (Result <> -1) and
1054 ((FItems[Result].Hash <> Hash) or
1055 not IsSameUTF8String(FItems[Result].Name, Name)) do
1056 Result := FItems[Result].Next;
1057 end;
1058 //------------------------------------------------------------------------------
1059
1060 procedure TSvgIdNameHashMap.AddOrIgnore(const idName: UTF8String; element: TBaseElement);
1061 var
1062 Index: Integer;
1063 Hash: Cardinal;
1064 Item: PSvgIdNameHashMapItem;
1065 Bucket: PInteger;
1066 begin
1067 Index := FindItemIndex(idName);
1068 if Index >= 0 then
1069 Exit; // already exists so ignore;
1070
1071 // add new item
1072 if FCount = Length(FItems) then Grow;
1073 Index := FCount;
1074 Inc(FCount);
1075
1076 Hash := GetHash(idName);
1077 Bucket := @FBuckets[(Hash and $7FFFFFFF) mod FMod];
1078 Item := @FItems[Index];
1079 Item.Next := Bucket^;
1080 Item.Hash := Hash;
1081 Item.Name := idName;
1082 Item.Element := element;
1083 Bucket^ := Index;
1084 end;
1085 //------------------------------------------------------------------------------
1086
1087 function TSvgIdNameHashMap.FindElement(const idName: UTF8String): TBaseElement;
1088 var
1089 Index: Integer;
1090 begin
1091 if FCount = 0 then
1092 Result := nil
1093 else
1094 begin
1095 Index := FindItemIndex(idName);
1096 if Index < 0 then
1097 Result := nil else
1098 Result := FItems[Index].Element
1099 end;
1100 end;
1101 //------------------------------------------------------------------------------
1102
1103 procedure TSvgIdNameHashMap.Clear;
1104 begin
1105 FCount := 0;
1106 FMod := 0;
1107 FItems := nil;
1108 FBuckets := nil;
1109 end;
1110
1111 //------------------------------------------------------------------------------
1112 // TDefsElement
1113 //------------------------------------------------------------------------------
1114
1115 constructor TDefsElement.Create(parent: TBaseElement; svgEl: TSvgXmlEl);
1116 begin
1117 inherited;
1118 fDrawData.visible := false;
1119 end;
1120
1121 //------------------------------------------------------------------------------
1122 // TStyleElement
1123 //------------------------------------------------------------------------------
1124
1125 constructor TStyleElement.Create(parent: TBaseElement; svgEl: TSvgXmlEl);
1126 begin
1127 inherited;
1128 fDrawData.visible := false;
1129 // See ParseStyleElementContent in Img32.Core.
1130 end;
1131
1132 //------------------------------------------------------------------------------
1133 // TImageElement
1134 //------------------------------------------------------------------------------
1135
1136 function TrimAnySpaces(const s: UTF8String): UTF8String;
1137 var
1138 i, j, len: integer;
1139 dst: PUTF8Char;
1140 begin
1141 len := Length(s);
1142 SetLength(Result, len);
1143 dst := PUTF8Char(Pointer(Result));
1144 j := 0;
1145 for i := 1 to len do
1146 if s[i] > #32 then
1147 begin
1148 dst[j] := s[i];
1149 inc(j);
1150 end;
1151 if j <> len then
1152 SetLength(Result, j);
1153 end;
1154 //------------------------------------------------------------------------------
1155
1156 procedure ReadRefElImage(const refEl: UTF8String; out img: TImage32);
1157 var
1158 len, offset: integer;
1159 s: UTF8String;
1160 ms: TMemoryStream;
1161 c: PUTF8Char;
1162 begin
1163 img := nil;
1164
1165 // unfortunately white spaces are sometimes found inside encoded base64
1166 s := TrimAnySpaces(refEl);
1167 len := Length(s);
1168
1169 // currently only accepts **embedded** images
1170 if (len = 0) then Exit;
1171
1172 c := PUTF8Char(s);
1173 if not Match(c, 'data:image/') then Exit;
1174
1175 if Match(@c[11], 'jpg;base64,') then offset := 22
1176 else if Match(@c[11], 'jpeg;base64,') then offset := 23
1177 else if Match(@c[11], 'png;base64,') then offset := 22
1178 else Exit;
1179
1180 ms := TMemoryStream.Create;
1181 try
1182 if not Base64Decode(@c[offset], len -offset, ms) then Exit;
1183 img := TImage32.Create;
1184 if not img.LoadFromStream(ms) then
1185 begin
1186 FreeAndNil(img);
1187 Exit;
1188 end;
1189 finally
1190 ms.Free;
1191 end;
1192 end;
1193
1194 //------------------------------------------------------------------------------
1195 // TImageElement
1196 //------------------------------------------------------------------------------
1197
1198 destructor TImageElement.Destroy;
1199 begin
1200 if Assigned(fImage) then fImage.Free;
1201 inherited Destroy;
1202 end;
1203 //------------------------------------------------------------------------------
1204
1205 procedure TImageElement.Draw(image: TImage32; drawDat: TDrawData);
1206 var
1207 dstRecD: TRectD;
1208 tmp: TImage32;
1209 tmpScale: TPointD;
1210 begin
1211 dstRecD := Self.elRectWH.GetRectD(0,0);
1212 MatrixMultiply2(fDrawData.matrix, drawDat.matrix);
1213
1214 MatrixApply(drawDat.matrix, dstRecD);
1215
1216 if (fRefEl <> '') and not Assigned(fImage) then
1217 begin
1218 ReadRefElImage(fRefEl, fImage);
1219 if Assigned(fImage) then
1220 begin
1221 fRefEl := ''; // ie avoid reloading fImage
1222 fTransparent := fImage.HasTransparency;
1223 end;
1224 end;
1225
1226 if fImage <> nil then
1227 begin
1228 if elRectWH.IsValid then
1229 begin
1230 tmpScale.X := elRectWH.width.rawVal / fImage.Width;
1231 tmpScale.Y := elRectWH.Height.rawVal / fImage.Height;
1232 MatrixScale(drawDat.matrix, tmpScale.X, tmpScale.Y);
1233 end;
1234
1235 tmp := TImage32.Create();
1236 try
1237 tmp.AssignSettings(fImage);
1238 MatrixApply(drawDat.matrix, fImage, tmp);
1239 // CopyBlend is slower than Copy, so only use it if we have a
1240 // transparent image.
1241 if fTransparent then
1242 image.CopyBlend(tmp, tmp.Bounds, Rect(dstRecD), BlendToAlphaLine)
1243 else
1244 image.Copy(tmp, tmp.Bounds, Rect(dstRecD));
1245 finally
1246 tmp.Free;
1247 end;
1248 end;
1249 end;
1250
1251 //------------------------------------------------------------------------------
1252 // TGroupElement
1253 //------------------------------------------------------------------------------
1254
1255 procedure TGroupElement.Draw(image: TImage32; drawDat: TDrawData);
1256 var
1257 clipEl : TClipPathElement;
1258 maskEl : TMaskElement;
1259 tmpImg : TImage32;
1260 clipPaths : TPathsD;
1261 clipRec : TRect;
1262 dstClipRec: TRect;
1263 offsetX, offsetY: integer;
1264 fr: TFillRule;
1265 begin
1266 if fChilds.Count = 0 then Exit;
1267
1268 UpdateDrawInfo(drawDat, self);
1269 UpdateFontInfo(drawDat, self);
1270
1271 if drawDat.fillRule = frNegative then
1272 drawDat.fillRule := frNonZero;
1273
1274 maskEl := TMaskElement(FindRefElement(drawDat.maskElRef));
1275 clipEl := TClipPathElement(FindRefElement(drawDat.clipElRef));
1276 if Assigned(clipEl) then
1277 begin
1278 drawDat.clipElRef := '';
1279 with clipEl do
1280 begin
1281 GetPaths(drawDat);
1282 clipPaths := CopyPaths(drawPathsC);
1283 AppendPath(clipPaths, drawPathsO);
1284 MatrixApply(drawDat.matrix, clipPaths);
1285 clipRec := Img32.Vector.GetBounds(clipPaths);
1286 end;
1287 if IsEmptyRect(clipRec) then Exit;
1288 dstClipRec := clipRec; // save for blending tmpImg to image
1289
1290 // Translate the clipPaths, clipRec and matrix
1291 // to minimize the size of the mask image.
1292 offsetX := clipRec.Left;
1293 offsetY := clipRec.Top;
1294 if offsetX < 0 then offsetX := 0;
1295 if offsetY < 0 then offsetY := 0;
1296 if (offsetX > 0) or (offsetY > 0) then
1297 begin
1298 MatrixTranslate(drawDat.matrix, -offsetX, -offsetY); // for DrawChildren
1299 clipPaths := TranslatePath(clipPaths, -offsetX, -offsetY);
1300 TranslateRect(clipRec, -offsetX, -offsetY);
1301 end;
1302
1303 //nb: it's not safe to use fReader.TempImage when calling DrawChildren
1304 tmpImg := TImage32.Create(Min(image.Width, clipRec.Right), Min(image.Height, clipRec.Bottom));
1305 try
1306 DrawChildren(tmpImg, drawDat);
1307 if clipEl.fDrawData.fillRule = frNegative then
1308 fr := frNonZero else
1309 fr := clipEl.fDrawData.fillRule;
1310 EraseOutsidePaths(tmpImg, clipPaths, fr, clipRec, fSvgReader.fCustomRendererCache);
1311 image.CopyBlend(tmpImg, clipRec, dstClipRec, BlendToAlphaLine);
1312 finally
1313 tmpImg.Free;
1314 end;
1315 end
1316 else if Assigned(maskEl) then
1317 begin
1318 drawDat.maskElRef := '';
1319 with maskEl do
1320 begin
1321 GetPaths(drawDat);
1322 clipRec := maskRec;
1323 end;
1324
1325 // Translate the maskRec, the matix and the clipRec to minimize
1326 // the size of the mask image.
1327 dstClipRec := clipRec; // save for blending tmpImg to image
1328 offsetX := -clipRec.Left;
1329 offsetY := -clipRec.Top;
1330 if offsetX > 0 then offsetX := 0;
1331 if offsetY > 0 then offsetY := 0;
1332 if (offsetX < 0) or (offsetY < 0) then
1333 begin
1334 MatrixTranslate(drawDat.matrix, offsetX, offsetY); // for DrawChildren
1335 TranslateRect(clipRec, offsetX, offsetY);
1336 TranslateRect(maskEl.maskRec, offsetX, offsetY);
1337 end;
1338
1339 tmpImg := TImage32.Create(Min(image.Width, clipRec.Right), Min(image.Height, clipRec.Bottom));
1340 try
1341 DrawChildren(tmpImg, drawDat);
1342 TMaskElement(maskEl).ApplyMask(tmpImg, drawDat);
1343 image.CopyBlend(tmpImg, clipRec, dstClipRec, BlendToAlphaLine);
1344 finally
1345 tmpImg.Free;
1346 end;
1347 end else
1348 DrawChildren(image, drawDat);
1349 end;
1350
1351 //------------------------------------------------------------------------------
1352 // TSwitchElement
1353 //------------------------------------------------------------------------------
1354
1355 procedure TSwitchElement.Draw(image: TImage32; drawDat: TDrawData);
1356 var
1357 i: integer;
1358 begin
1359 for i := 0 to fChilds.Count -1 do
1360 if TBaseElement(fChilds[i]) is TShapeElement then
1361 with TShapeElement(fChilds[i]) do
1362 if fDrawData.visible then
1363 begin
1364 Draw(image, drawDat);
1365 break; //break after the first successful drawing
1366 end;
1367 end;
1368
1369 //------------------------------------------------------------------------------
1370 // TUseElement
1371 //------------------------------------------------------------------------------
1372
1373 procedure TUseElement.GetPaths(const drawDat: TDrawData);
1374 var
1375 el: TBaseElement;
1376 dx, dy: double;
1377 begin
1378 if pathsLoaded or (fRefEl = '') then Exit;
1379
1380 el := FindRefElement(fRefEl);
1381 if not Assigned(el) or not (el is TShapeElement) then Exit;
1382 pathsLoaded := true;
1383
1384 with TShapeElement(el) do
1385 begin
1386 GetPaths(drawDat);
1387 self.drawPathsC := CopyPaths(drawPathsC);
1388 self.drawPathsO := CopyPaths(drawPathsO);
1389 end;
1390
1391 if elRectWH.left.IsValid then
1392 dx := elRectWH.left.rawVal else
1393 dx := 0;
1394 if elRectWH.top.IsValid then
1395 dy := elRectWH.top.rawVal else
1396 dy := 0;
1397
1398 if (dx <> 0) or (dy <> 0) then
1399 begin
1400 drawPathsC := TranslatePath(drawPathsC, dx, dy);
1401 drawPathsO := TranslatePath(drawPathsO, dx, dy);
1402 end;
1403 end;
1404 //------------------------------------------------------------------------------
1405
1406 function TUseElement.ValidateNonRecursion(el: TBaseElement): Boolean;
1407 begin
1408 Result := false;
1409 while assigned(el) do
1410 begin
1411 if (el = Self) then Exit;
1412 if not (el is TUseElement) then break; //shouldn't happen
1413 el := TUseElement(el).callerUse;
1414 end;
1415 Result := true;
1416 end;
1417 //------------------------------------------------------------------------------
1418
1419 procedure TUseElement.Draw(img: TImage32; drawDat: TDrawData);
1420 var
1421 el: TBaseElement;
1422 s, dx, dy: double;
1423 scale, scale2: TPointD;
1424 mat: TMatrixD;
1425 begin
1426
1427 //make sure there's not recursion, either directly or indirectly
1428 if not ValidateNonRecursion(drawDat.useEl) then Exit;
1429
1430 callerUse := drawDat.useEl;
1431 drawDat.useEl := self;
1432
1433 el := FindRefElement(fRefEl);
1434 if not Assigned(el) then Exit;
1435
1436 UpdateDrawInfo(drawDat, self); //nb: <use> attribs override el's.
1437 MatrixExtractScale(drawDat.matrix, scale.X, scale.Y);
1438
1439 if elRectWH.left.IsValid then dx := elRectWH.left.rawVal else dx := 0;
1440 if elRectWH.top.IsValid then dy := elRectWH.top.rawVal else dy := 0;
1441
1442 if (dx <> 0) or (dy <> 0) then
1443 begin
1444 mat := IdentityMatrix;
1445 MatrixTranslate(mat, dx, dy);
1446 MatrixMultiply2(mat, drawDat.matrix);
1447 end;
1448
1449 if el is TSymbolElement then
1450 begin
1451 with TSymbolElement(el) do
1452 begin
1453 if not viewboxWH.IsEmpty then
1454 begin
1455 //scale the symbol according to its width and height attributes
1456 if elRectWH.width.IsValid and elRectWH.height.IsValid then
1457 begin
1458 scale2.X := elRectWH.width.rawVal / viewboxWH.Width;
1459 scale2.Y := elRectWH.height.rawVal / viewboxWH.Height;
1460 if scale2.Y < scale2.X then s := scale2.Y else s := scale2.X;
1461 //the following 3 lines will scale without translating
1462 mat := IdentityMatrix;
1463 MatrixScale(mat, s, s);
1464 MatrixMultiply2(mat, drawDat.matrix);
1465 drawDat.bounds := RectD(0,0,viewboxWH.Width, viewboxWH.Height);
1466 end;
1467
1468 if self.elRectWH.width.IsValid and
1469 self.elRectWH.height.IsValid then
1470 begin
1471 with viewboxWH do
1472 begin
1473 dx := -Left/Width * self.elRectWH.width.rawVal;
1474 dy := -Top/Height * self.elRectWH.height.rawVal;
1475
1476 //scale <symbol> proportionally to fill the <use> element
1477 scale2.X := self.elRectWH.width.rawVal / Width;
1478 scale2.Y := self.elRectWH.height.rawVal / Height;
1479 if scale2.Y < scale2.X then s := scale2.Y else s := scale2.X;
1480 end;
1481
1482 mat := IdentityMatrix;
1483 MatrixScale(mat, s, s);
1484 MatrixTranslate(mat, dx, dy);
1485 MatrixMultiply2(mat, drawDat.matrix);
1486
1487 //now center after scaling
1488 if scale2.X > scale2.Y then
1489 begin
1490 if scale2.X > 1 then
1491 begin
1492 s := (self.elRectWH.width.rawVal - viewboxWH.Width) * 0.5;
1493 MatrixTranslate(drawDat.matrix, s * scale.X, 0);
1494 end;
1495 end else if scale2.Y > 1 then
1496 begin
1497 s := (self.elRectWH.height.rawVal - viewboxWH.Height) * 0.5;
1498 MatrixTranslate(drawDat.matrix, 0, s * scale.Y);
1499 end;
1500
1501 end;
1502 end;
1503 DrawChildren(img, drawDat);
1504 end;
1505 end
1506 else if el is TImageElement then
1507 el.Draw(img, drawDat)
1508 else if el is TShapeElement then
1509 el.Draw(img, drawDat);
1510 end;
1511
1512 //------------------------------------------------------------------------------
1513 // TMaskElement
1514 //------------------------------------------------------------------------------
1515
1516 procedure TMaskElement.GetPaths(const drawDat: TDrawData);
1517 var
1518 i : integer;
1519 el : TShapeElement;
1520 begin
1521 maskRec := NullRect;
1522 for i := 0 to fChilds.Count -1 do
1523 if TBaseElement(fChilds[i]) is TShapeElement then
1524 begin
1525 el := TShapeElement(fChilds[i]);
1526 el.GetPaths(drawDat);
1527 maskRec := Img32.Vector.UnionRect(maskRec, Img32.Vector.GetBounds(el.drawPathsC));
1528 Img32.Vector.UnionRect(maskRec, Img32.Vector.GetBounds(el.drawPathsO));
1529 end;
1530 MatrixApply(drawDat.matrix, maskRec);
1531 end;
1532 //------------------------------------------------------------------------------
1533
1534 procedure TMaskElement.ApplyMask(img: TImage32; const drawDat: TDrawData);
1535 var
1536 tmpImg: TImage32;
1537 begin
1538 tmpImg := TImage32.Create(Min(img.Width, maskRec.Right), Min(img.Height, maskRec.Bottom));
1539 try
1540 DrawChildren(tmpImg, drawDat);
1541 img.CopyBlend(tmpImg, maskRec, maskRec, BlendBlueChannelLine);
1542 finally
1543 tmpImg.Free;
1544 end;
1545 end;
1546
1547 //------------------------------------------------------------------------------
1548 // TSymbolElement
1549 //------------------------------------------------------------------------------
1550
1551 constructor TSymbolElement.Create(parent: TBaseElement; svgEl: TSvgXmlEl);
1552 begin
1553 inherited;
1554 fDrawData.visible := false;
1555 end;
1556
1557 //------------------------------------------------------------------------------
1558 // TGradElement
1559 //------------------------------------------------------------------------------
1560
1561 function TGradientElement.LoadContent: Boolean;
1562 var
1563 i: integer;
1564 begin
1565 Result := inherited LoadContent;
1566 for i := 0 to fChilds.Count -1 do
1567 if TBaseElement(fChilds[i]) is TGradStopElement then
1568 with TGradStopElement(fChilds[i]) do
1569 AddStop(color, offset);
1570 end;
1571 //------------------------------------------------------------------------------
1572
1573 procedure TGradientElement.AddStop(color: TColor32; offset: double);
1574 var
1575 len: integer;
1576 begin
1577 //if a stop is less than previous stops, it is set equal to the largest stop.
1578 //If two stops are equal the last stop controls the color from that point.
1579 len := Length(stops);
1580 if (len > 0) and (stops[len-1].offset > offset) then
1581 offset := stops[len-1].offset;
1582 setLength(stops, len+1);
1583 stops[len].offset := Min(1,Max(0, offset));
1584 stops[len].color := color;
1585 end;
1586 //------------------------------------------------------------------------------
1587
1588 procedure TGradientElement.AssignTo(other: TBaseElement);
1589 var
1590 i, len: integer;
1591 begin
1592 if not Assigned(other) or not (other is TGradientElement) then Exit;
1593 inherited;
1594
1595 with TGradientElement(other) do
1596 begin
1597 if units = 0 then
1598 units := Self.units;
1599
1600 if Length(stops) = 0 then
1601 begin
1602 len := Length(self.stops);
1603 SetLength(stops, len);
1604 for i := 0 to len -1 do
1605 stops[i] := Self.stops[i];
1606 end;
1607
1608 if IsIdentityMatrix(fDrawData.matrix) then
1609 fDrawData.matrix := self.fDrawData.matrix;
1610 end;
1611 end;
1612 //------------------------------------------------------------------------------
1613
1614 function TGradientElement.PrepareRenderer(
1615 renderer: TCustomGradientRenderer; drawDat: TDrawData): Boolean;
1616 var
1617 el: TBaseElement;
1618 begin
1619 if (refEl <> '') then
1620 begin
1621 el := FindRefElement(refEl);
1622 if Assigned(el) and (el is TGradientElement) then
1623 TGradientElement(el).AssignTo(self);
1624 end;
1625 Result := Length(stops) > 0;
1626 end;
1627 //------------------------------------------------------------------------------
1628
1629 procedure TGradientElement.AddColorStopsToRenderer(renderer: TCustomGradientRenderer);
1630 var
1631 i, hiStops: Integer;
1632 begin
1633 hiStops := High(stops);
1634 if (hiStops = 0) or (renderer = nil) then Exit;
1635
1636 // If vector boundary-stops are implicit, then boundary
1637 // and adjacent inner stop (explicit) should have the
1638 // same color
1639 if stops[0].offset > 0 then
1640 with stops[0] do
1641 renderer.InsertColorStop(offset, color);
1642
1643 for i := 1 to hiStops -1 do
1644 with stops[i] do
1645 renderer.InsertColorStop(offset, color);
1646
1647 // If vector boundary-stops are implicit, then boundary
1648 // and adjacent inner stop (explicit) should have the
1649 // same color
1650 if stops[hiStops].offset < 1 then
1651 with stops[hiStops] do
1652 renderer.InsertColorStop(offset, color);
1653 end;
1654
1655 //------------------------------------------------------------------------------
1656 // TRadGradElement
1657 //------------------------------------------------------------------------------
1658
1659 constructor TRadGradElement.Create(parent: TBaseElement; svgEl: TSvgXmlEl);
1660 begin
1661 inherited;
1662 radius.Init;
1663 F.Init;
1664 C.Init;
1665 end;
1666 //------------------------------------------------------------------------------
1667
1668 procedure TRadGradElement.AssignTo(other: TBaseElement);
1669 begin
1670 if not Assigned(other) or not (other is TGradientElement) then Exit;
1671 inherited;
1672 if other is TRadGradElement then
1673 with TRadGradElement(other) do
1674 begin
1675 if not radius.IsValid then radius := self.radius;
1676 if not C.IsValid then C := self.C;
1677 if not F.IsValid then F := self.F;
1678 end;
1679 end;
1680 //------------------------------------------------------------------------------
1681
1682 function TRadGradElement.PrepareRenderer(renderer: TCustomGradientRenderer;
1683 drawDat: TDrawData): Boolean;
1684 var
1685 hiStops: integer;
1686 cp, fp, r: TPointD;
1687 scale, scale2: TPointD;
1688 rec2, rec3: TRectD;
1689 begin
1690 inherited PrepareRenderer(renderer, drawDat);
1691 hiStops := High(stops);
1692 Result := hiStops >= 0;
1693 if not Result then Exit;
1694
1695 if units = hUserSpaceOnUse then
1696 rec2 := fSvgReader.userSpaceBounds else
1697 rec2 := drawDat.bounds;
1698
1699 if radius.IsValid then
1700 begin
1701 if radius.X.HasFontUnits then
1702 r := radius.GetPoint(drawDat.fontInfo.size, GetRelFracLimit) else
1703 r := radius.GetPoint(rec2, GetRelFracLimit);
1704 end else
1705 begin
1706 r.X := rec2.Width * 0.5;
1707 r.Y := rec2.Height * 0.5;
1708 end;
1709 MatrixExtractScale(drawDat.matrix, scale.X, scale.Y);
1710 MatrixExtractScale(fDrawData.matrix, scale2.X, scale2.Y);
1711 r := ScalePoint(r, scale.X * scale2.X, scale.Y * scale2.Y);
1712
1713 if C.IsValid then
1714 begin
1715 if C.X.HasFontUnits then
1716 cp := C.GetPoint(drawDat.fontInfo.size, GetRelFracLimit) else
1717 cp := C.GetPoint(rec2, GetRelFracLimit);
1718 cp := TranslatePoint(cp, rec2.Left, rec2.Top);
1719 end else
1720 cp := rec2.MidPoint;
1721 MatrixApply(fDrawData.matrix, cp);
1722 MatrixApply(drawDat.matrix, cp);
1723
1724 rec3 := RectD(cp.X-r.X, cp.Y-r.Y, cp.X+r.X, cp.Y+r.Y);
1725
1726 if F.IsValid then
1727 begin
1728 if F.X.HasFontUnits then
1729 fp := F.GetPoint(drawDat.fontInfo.size, GetRelFracLimit) else
1730 fp := F.GetPoint(rec2, GetRelFracLimit);
1731 fp := TranslatePoint(fp, rec2.Left, rec2.Top);
1732 MatrixApply(fDrawData.matrix, fp);
1733 MatrixApply(drawDat.matrix, fp);
1734 end else
1735 fp := MidPoint(rec3);
1736
1737 with renderer as TSvgRadialGradientRenderer do
1738 begin
1739 SetParameters(Rect(rec3), Point(fp),
1740 stops[0].color, stops[hiStops].color, spreadMethod);
1741 AddColorStopsToRenderer(renderer);
1742 end;
1743 end;
1744
1745 //------------------------------------------------------------------------------
1746 // TLinGradElement
1747 //------------------------------------------------------------------------------
1748
1749 constructor TLinGradElement.Create(parent: TBaseElement; svgEl: TSvgXmlEl);
1750 begin
1751 inherited;
1752 startPt.Init;
1753 endPt.Init;
1754 end;
1755 //------------------------------------------------------------------------------
1756
1757 procedure TLinGradElement.AssignTo(other: TBaseElement);
1758 begin
1759 if not Assigned(other) or not (other is TGradientElement) then Exit;
1760 inherited;
1761 if other is TLinGradElement then
1762 with TLinGradElement(other) do
1763 begin
1764 if not startPt.IsValid then startPt := self.startPt;
1765 if not endPt.IsValid then endPt := self.endPt;
1766 end;
1767 end;
1768 //------------------------------------------------------------------------------
1769
1770 function TLinGradElement.PrepareRenderer(
1771 renderer: TCustomGradientRenderer; drawDat: TDrawData): Boolean;
1772 var
1773 pt1, pt2: TPointD;
1774 hiStops: integer;
1775 rec2: TRectD;
1776 begin
1777 inherited PrepareRenderer(renderer, drawDat);
1778 hiStops := High(stops);
1779 Result := (hiStops >= 0);
1780 if not Result then Exit;
1781
1782 //w3c-coords-units-01-b.svg
1783
1784 //if gradientUnits=objectBoundingBox (default) then all values must be
1785 //percentages. Also... when the object's bounding box is not square, the
1786 //gradient may render non-perpendicular relative to the gradient vector
1787 //unless the gradient vector is vertical or horizontal.
1788 //https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/gradientUnits
1789
1790 if units = hUserSpaceOnUse then
1791 rec2 := fSvgReader.userSpaceBounds else
1792 rec2 := drawDat.bounds;
1793
1794 with TLinearGradientRenderer(renderer) do
1795 begin
1796 if startPt.X.HasFontUnits then
1797 pt1 := startPt.GetPoint(drawDat.fontInfo.size, GetRelFracLimit) else
1798 pt1 := startPt.GetPoint(rec2, GetRelFracLimit);
1799
1800 if (startPt.X.unitType <> utPixel) or
1801 (units <> hUserSpaceOnUse) then
1802 pt1.X := pt1.X + rec2.Left;
1803
1804 if (startPt.Y.unitType <> utPixel) or
1805 (units <> hUserSpaceOnUse) then
1806 pt1.Y := pt1.Y + rec2.Top;
1807
1808 MatrixApply(fDrawData.matrix, pt1);
1809 MatrixApply(drawDat.matrix, pt1);
1810
1811
1812 if not endPt.X.IsValid then
1813 pt2.X := rec2.Width else
1814 pt2.X := endPt.X.GetValue(rec2.Width, GetRelFracLimit);
1815 pt2.Y := endPt.Y.GetValue(rec2.Height, GetRelFracLimit);
1816 pt2 := TranslatePoint(pt2, rec2.Left, rec2.Top);
1817
1818 MatrixApply(fDrawData.matrix, pt2);
1819 MatrixApply(drawDat.matrix, pt2);
1820
1821 if (units <> hUserSpaceOnUse) and
1822 ((pt2.X <> pt1.X) or (pt2.Y <> pt1.Y)) then
1823 begin
1824 //skew the gradient
1825 end;
1826
1827 SetParameters(pt1, pt2, stops[0].color,
1828 stops[hiStops].color, spreadMethod);
1829 AddColorStopsToRenderer(renderer);
1830 end;
1831 end;
1832
1833 //------------------------------------------------------------------------------
1834 // TGradStopElement
1835 //------------------------------------------------------------------------------
1836
1837 constructor TGradStopElement.Create(parent: TBaseElement; svgEl: TSvgXmlEl);
1838 begin
1839 inherited;
1840 color := clBlack32;
1841 end;
1842
1843 //------------------------------------------------------------------------------
1844 // TFilterElement
1845 //------------------------------------------------------------------------------
1846
1847 constructor TFilterElement.Create(parent: TBaseElement; svgEl: TSvgXmlEl);
1848 begin
1849 inherited;
1850 fDrawData.visible := false;
1851 elRectWH.Init;
1852 end;
1853 //------------------------------------------------------------------------------
1854
1855 destructor TFilterElement.Destroy;
1856 begin
1857 Clear;
1858 inherited;
1859 end;
1860 //------------------------------------------------------------------------------
1861
1862 procedure TFilterElement.Clear;
1863 var
1864 i: integer;
1865 begin
1866 for i := 0 to High(fImages) do
1867 fImages[i].Free;
1868 fImages := nil;
1869 fNames := nil;
1870 fLastImg := nil;
1871 end;
1872 //------------------------------------------------------------------------------
1873
1874 function TFilterElement.GetRelFracLimit: double;
1875 begin
1876 // assume fractional values below 2.5 are always relative
1877 Result := 2.5;
1878 end;
1879 //------------------------------------------------------------------------------
1880
1881 function TFilterElement.GetAdjustedBounds(const bounds: TRectD): TRectD;
1882 var
1883 recWH: TRectWH;
1884 delta: TSizeD;
1885 d: double;
1886 pt: TPointD;
1887 i: integer;
1888 hasOffset: Boolean;
1889 begin
1890 fObjectBounds := Rect(bounds);
1891 if elRectWH.IsValid then
1892 begin
1893 recWH := elRectWH.GetRectWH(bounds, GetRelFracLimit);
1894 Result.Left := bounds.Left + recWH.Left;
1895 Result.Top := bounds.Top + recWH.Top;
1896 Result.Right := Result.Left + recWH.Width;
1897 Result.Bottom := Result.Top + recWH.Height;
1898 end else
1899 begin
1900 Result := bounds;
1901
1902 //when the filter's width and height are undefined then limit the filter
1903 //margin to 20% of the bounds when just blurring, not also offsetting.
1904 hasOffset := false;
1905
1906 delta.cx := 0; delta.cy := 0;
1907 for i := 0 to ChildCount -1 do
1908 begin
1909 if Child[i] is TFeGaussElement then
1910 begin
1911 d := TFeGaussElement(Child[i]).stdDev * 3 * fScale;
1912 delta.cx := delta.cx + d;
1913 delta.cy := delta.cy + d;
1914 end
1915 else if Child[i] is TFeDropShadowElement then
1916 with TFeDropShadowElement(Child[i]) do
1917 begin
1918 d := stdDev * 0.75 * fScale;
1919 pt := offset.GetPoint(bounds, 1);
1920 delta.cx := delta.cx + d + Abs(pt.X) * fScale;
1921 delta.cy := delta.cy + d + Abs(pt.Y) * fScale;
1922 hasOffset := true;
1923 end
1924 else if Child[i] is TFeOffsetElement then
1925 with TFeOffsetElement(Child[i]) do
1926 begin
1927 pt := offset.GetPoint(bounds, 1);
1928 delta.cx := delta.cx + Abs(pt.X) * fScale;
1929 delta.cy := delta.cy + Abs(pt.Y) * fScale;
1930 hasOffset := true;
1931 end;
1932 end;
1933
1934 if (delta.cx = InvalidD) or (delta.cy = InvalidD) then Exit;
1935
1936 //limit the filter margin to 20% if only blurring
1937 if not hasOffset then
1938 with delta, bounds do
1939 begin
1940 if cx > Width * 0.2 then cx := Width * 0.2;
1941 if cy > Height * 0.2 then cy := Height * 0.2;
1942 end;
1943 Img32.Vector.InflateRect(Result, delta.cx, delta.cy);
1944
1945 end;
1946 end;
1947 //------------------------------------------------------------------------------
1948
1949 function TFilterElement.FindNamedImage(const name: UTF8String): TImage32;
1950 var
1951 i, len: integer;
1952 begin
1953 Result := nil;
1954 len := Length(fNames);
1955 for i := 0 to len -1 do
1956 if name = fNames[i] then
1957 begin
1958 Result := fImages[i];
1959 Break;
1960 end;
1961 end;
1962 //------------------------------------------------------------------------------
1963
1964 function TFilterElement.AddNamedImage(const name: UTF8String): TImage32;
1965 var
1966 len, w, h: integer;
1967 begin
1968 len := Length(fNames);
1969 SetLength(fNames, len+1);
1970 SetLength(fImages, len+1);
1971 RectWidthHeight(fFilterBounds, w, h);
1972 Result := TImage32.Create(w, h);
1973 fImages[len] := Result;
1974 fNames[len] := name;
1975 end;
1976 //------------------------------------------------------------------------------
1977
1978 function TFilterElement.GetNamedImage(const name: UTF8String; isIn: Boolean): TImage32;
1979 begin
1980 Result := FindNamedImage(name);
1981 if not Assigned(Result) then
1982 Result := AddNamedImage(name)
1983 else if not isIn then
1984 Exit;
1985
1986 case GetHash(name) of
1987 hBackgroundImage:
1988 Result.Copy(fSvgReader.BackgndImage, fFilterBounds, Result.Bounds);
1989 hBackgroundAlpha:
1990 begin
1991 Result.Copy(fSvgReader.BackgndImage, fFilterBounds, Result.Bounds);
1992 Result.SetRGB(clNone32, Result.Bounds);
1993 end;
1994 hSourceGraphic:
1995 Result.Copy(fSrcImg, fFilterBounds, Result.Bounds);
1996 hSourceAlpha:
1997 begin
1998 Result.Copy(fSrcImg, fFilterBounds, Result.Bounds);
1999 Result.SetRGB(clBlack32, Result.Bounds);
2000 end;
2001 end;
2002 end;
2003 //------------------------------------------------------------------------------
2004
2005 procedure TFilterElement.Apply(img: TImage32;
2006 const filterBounds: TRect; const matrix: TMatrixD);
2007 var
2008 i: integer;
2009 begin
2010 MatrixExtractScale(matrix, fScale);
2011 fFilterBounds := filterBounds;
2012 Types.IntersectRect(fObjectBounds, fObjectBounds, img.Bounds);
2013 fSrcImg := img;
2014
2015 try
2016 for i := 0 to fChilds.Count -1 do
2017 begin
2018 case TBaseElement(fChilds[i]).fXmlEl.hash of
2019 hfeBlend : TFeBlendElement(fChilds[i]).Apply;
2020 hfeColorMatrix : TFeColorMatrixElement(fChilds[i]).Apply;
2021 hFeComponentTransfer : TFeComponentTransferElement(fChilds[i]).Apply;
2022 hfeComposite : TFeCompositeElement(fChilds[i]).Apply;
2023 hfeDefuseLighting : TFeDefuseLightElement(fChilds[i]).Apply;
2024 hfeDropShadow : TFeDropShadowElement(fChilds[i]).Apply;
2025 hfeFlood : TFeFloodElement(fChilds[i]).Apply;
2026 hfeImage : TFeImageElement(fChilds[i]).Apply;
2027 hFeGaussianBlur : TFeGaussElement(fChilds[i]).Apply;
2028 hfeMerge : TFeMergeElement(fChilds[i]).Apply;
2029 hfeOffset : TFeOffsetElement(fChilds[i]).Apply;
2030 hfeSpecularLighting : TFeSpecLightElement(fChilds[i]).Apply;
2031 end;
2032 end;
2033 if Assigned(fLastImg) then
2034 fSrcImg.Copy(fLastImg, fLastImg.Bounds, fFilterBounds);
2035 finally
2036 Clear;
2037 end;
2038 end;
2039
2040 //------------------------------------------------------------------------------
2041 // TFeBaseElement
2042 //------------------------------------------------------------------------------
2043
2044 function TFeBaseElement.GetParentAsFilterEl: TFilterElement;
2045 var
2046 el: TBaseElement;
2047 begin
2048 el := fParent;
2049 while Assigned(el) and not (el is TFilterElement) do
2050 el := el.fParent;
2051 if not Assigned(el) then
2052 Result := nil else
2053 Result := TFilterElement(el);
2054 end;
2055 //------------------------------------------------------------------------------
2056
2057 function TFeBaseElement.GetBounds(img: TImage32): TRect;
2058 var
2059 pfe: TFilterElement;
2060 begin
2061 pfe := ParentFilterEl;
2062 if img = pfe.fSrcImg then
2063 Result := pfe.fFilterBounds else
2064 Result := img.Bounds;
2065 end;
2066 //------------------------------------------------------------------------------
2067
2068 function TFeBaseElement.GetSrcAndDst: Boolean;
2069 var
2070 pfe: TFilterElement;
2071 begin
2072 pfe := ParentFilterEl;
2073 if (in1 <> '') then
2074 srcImg := pfe.GetNamedImage(in1, true)
2075 else if Assigned(pfe.fLastImg) then
2076 srcImg := pfe.fLastImg
2077 else
2078 srcImg := pfe.GetNamedImage(SourceImage, false);
2079
2080 if (res <> '') then
2081 dstImg := pfe.GetNamedImage(res, false) else
2082 dstImg := pfe.GetNamedImage(SourceImage, false);
2083
2084 Result := Assigned(srcImg) and Assigned(dstImg);
2085 if not Result then Exit;
2086 pfe.fLastImg := dstImg;
2087 srcRec := GetBounds(srcImg);
2088 dstRec := GetBounds(dstImg);
2089 end;
2090
2091 //------------------------------------------------------------------------------
2092 // TFeBlendElement
2093 //------------------------------------------------------------------------------
2094
2095 procedure TFeBlendElement.Apply;
2096 var
2097 pfe: TFilterElement;
2098 srcImg2, dstImg2: TImage32;
2099 srcRec2, dstRec2: TRect;
2100 begin
2101 if not GetSrcAndDst then Exit;
2102 pfe := ParentFilterEl;
2103 if (in2 = '') then Exit;
2104 if dstImg = srcImg then
2105 dstImg2 := pfe.AddNamedImage(tmpFilterImg) else
2106 dstImg2 := dstImg;
2107 dstRec2 := GetBounds(dstImg2);
2108
2109 srcImg2 := pfe.GetNamedImage(in2, true);
2110 srcRec2 := GetBounds(srcImg2);
2111 dstImg2.CopyBlend(srcImg2, srcRec2, dstRec2, BlendToAlphaLine);
2112 dstImg2.CopyBlend(srcImg, srcRec, dstRec2, BlendToAlphaLine);
2113 if dstImg = srcImg then
2114 dstImg.Copy(dstImg2, dstRec2, dstRec);
2115 end;
2116
2117 //------------------------------------------------------------------------------
2118 // TFeImageElement
2119 //------------------------------------------------------------------------------
2120
2121 destructor TFeImageElement.Destroy;
2122 begin
2123 fImage.Free;
2124 inherited Destroy;
2125 end;
2126 //------------------------------------------------------------------------------
2127
2128 procedure TFeImageElement.Apply;
2129 begin
2130 if GetSrcAndDst then
2131 begin
2132 if refEl <> '' then
2133 ReadRefElImage(refEl, fImage); // also clears refEl
2134
2135 if fImage <> nil then
2136 dstImg.Copy(fImage, fImage.Bounds, dstRec);
2137 end;
2138 end;
2139
2140 //------------------------------------------------------------------------------
2141 // TFeCompositeElement
2142 //------------------------------------------------------------------------------
2143
2144 constructor TFeCompositeElement.Create(parent: TBaseElement; svgEl: TSvgXmlEl);
2145 begin
2146 inherited;
2147 fourKs[0] := InvalidD; fourKs[1] := InvalidD;
2148 fourKs[2] := InvalidD; fourKs[3] := InvalidD;
2149 end;
2150 //------------------------------------------------------------------------------
2151
2152 procedure Arithmetic(p1, p2, r: PColor32; const ks: array of byte);
2153 var
2154 c1 : PARGB absolute p1;
2155 c2 : PARGB absolute p2;
2156 res : PARGB absolute r;
2157 begin
2158 res.A := (((c1.A xor 255) * (c2.A xor 255)) shr 8) xor 255;
2159 res.R := ClampByte((ks[0] * c1.R * c2.R +
2160 ks[1] * c1.R * 255 + ks[2] * c2.R * 255 + ks[3] * 65025) shr 16);
2161 res.G := ClampByte((ks[0] * c1.G * c2.G +
2162 ks[1] * c1.G * 255 + ks[2] * c2.G * 255 + ks[3] * 65025) shr 16);
2163 res.B := ClampByte((ks[0] * c1.B * c2.B +
2164 ks[1] * c1.B * 255 + ks[2] * c2.B * 255 + ks[3] * 65025) shr 16);
2165 end;
2166 //------------------------------------------------------------------------------
2167
2168 procedure ArithmeticBlend(src1, src2, dst: TImage32;
2169 const recS1, recS2, recDst: TRect; const ks: TFourDoubles);
2170 var
2171 kk: array[0..3] of byte;
2172 w,h, w2,h2, w3,h3, i,j: integer;
2173 p1,p2,r: PColor32;
2174 begin
2175 RectWidthHeight(recS1, w, h);
2176 RectWidthHeight(recS2, w2, h2);
2177 RectWidthHeight(recDst, w3, h3);
2178 if (w2 <> w) or (w3 <> w) or (h2 <> h) or (h3 <> h) or
2179 (ks[0] = InvalidD) or (ks[1] = InvalidD) or
2180 (ks[2] = InvalidD) or (ks[3] = InvalidD) then Exit;
2181
2182 for i := 0 to 3 do
2183 kk[i] := ClampByte(ks[i]*255);
2184
2185 for i := 0 to h -1 do
2186 begin
2187 p1 := @src1.Pixels[(recS1.Top + i) * src1.Width + recS1.Left];
2188 p2 := @src2.Pixels[(recS2.Top + i) * src2.Width + recS2.Left];
2189 r := @dst.Pixels[(recDst.Top + i) * dst.Width + recDst.Left];
2190 for j := 0 to w -1 do
2191 begin
2192 Arithmetic(p1, p2, r, kk);
2193 inc(p1); inc(p2); inc(r);
2194 end;
2195 end;
2196 end;
2197 //------------------------------------------------------------------------------
2198
2199 procedure TFeCompositeElement.Apply;
2200 var
2201 pfe: TFilterElement;
2202 srcImg2, dstImg2: TImage32;
2203 srcRec2, dstRec2: TRect;
2204 begin
2205 if not GetSrcAndDst then Exit;
2206 pfe := ParentFilterEl;
2207 if (in2 = '') then Exit;
2208
2209 srcImg2 := pfe.GetNamedImage(in2, true);
2210 srcRec2 := GetBounds(srcImg2); //either filter bounds or image bounds
2211
2212 if (dstImg = srcImg) or (dstImg = srcImg2) then
2213 dstImg2 := pfe.AddNamedImage(tmpFilterImg) else
2214 dstImg2 := dstImg;
2215 dstRec2 := GetBounds(dstImg2); //either filter bounds or image bounds
2216
2217 case compositeOp of
2218 coIn:
2219 begin
2220 dstImg2.Copy(srcImg, srcRec, dstRec2);
2221 dstImg2.CopyBlend(srcImg2, srcRec2, dstRec2, BlendMaskLine);
2222 end;
2223 coOut:
2224 begin
2225 dstImg2.Copy(srcImg, srcRec, dstRec2);
2226 dstImg2.CopyBlend(srcImg2, srcRec2, dstRec2, BlendInvertedMaskLine);
2227 end;
2228 coAtop:
2229 begin
2230 dstImg2.Copy(srcImg2, srcRec2, dstRec2);
2231 dstImg2.CopyBlend(srcImg, srcRec, dstRec2, BlendToAlphaLine);
2232 dstImg2.CopyBlend(srcImg2, srcRec2, dstRec2, BlendMaskLine);
2233 end;
2234 coXOR:
2235 begin
2236 dstImg2.Copy(srcImg2, srcRec2, dstRec2);
2237 dstImg2.CopyBlend(srcImg, srcRec, dstRec2, BlendToAlphaLine);
2238 dstImg2.CopyBlend(srcImg2, srcRec2, dstRec2, BlendInvertedMaskLine);
2239 end;
2240 coArithmetic:
2241 begin
2242 ArithmeticBlend(srcImg, srcImg2, dstImg2,
2243 srcRec, srcRec2, dstRec2, fourKs);
2244 end;
2245 else //coOver
2246 begin
2247 dstImg2.CopyBlend(srcImg2, srcRec2, dstRec2, BlendToAlphaLine);
2248 dstImg2.CopyBlend(srcImg, srcRec, dstRec2, BlendToAlphaLine);
2249 end;
2250 end;
2251 if (dstImg <> dstImg2) then
2252 dstImg.Copy(dstImg2, dstRec2, dstRec);
2253 end;
2254
2255 //------------------------------------------------------------------------------
2256 // TFeColorMatrixElement
2257 //------------------------------------------------------------------------------
2258
2259 type
2260 TColorMatrix = array[0..19] of Byte;
2261
2262 function ApplyColorMatrix(color: TColor32; const mat: TColorMatrix): TColor32;
2263 var
2264 clrIn : TARGB absolute color;
2265 clrOut: TARGB absolute Result;
2266 begin
2267 clrOut.R := ClampByte(MulBytes(mat[0],clrIn.R) + MulBytes(mat[1],clrIn.G) +
2268 MulBytes(mat[2],clrIn.B) + MulBytes(mat[3],clrIn.A) + mat[4]);
2269 clrOut.G := ClampByte(MulBytes(mat[5],clrIn.R) + MulBytes(mat[6],clrIn.G) +
2270 MulBytes(mat[7],clrIn.B) + MulBytes(mat[8],clrIn.A) + mat[9]);
2271 clrOut.B := ClampByte(MulBytes(mat[10],clrIn.R) + MulBytes(mat[11],clrIn.G) +
2272 MulBytes(mat[12],clrIn.B) + MulBytes(mat[13],clrIn.A) + mat[14]);
2273 clrOut.A := ClampByte(MulBytes(mat[15],clrIn.R) + MulBytes(mat[16],clrIn.G) +
2274 MulBytes(mat[17],clrIn.B) + MulBytes(mat[18],clrIn.A) + mat[19]);
2275 end;
2276 //------------------------------------------------------------------------------
2277
2278 procedure TFeColorMatrixElement.Apply;
2279 var
2280 i,j, dx1,dx2: integer;
2281 colorMatrix: TColorMatrix;
2282 p1, p2: PColor32;
2283 begin
2284 if not GetSrcAndDst or not Assigned(values) then Exit;
2285 for i := 0 to 19 do
2286 colorMatrix[i] := ClampByte(Integer(Round(values[i]*255)));
2287
2288 dx1 := srcImg.Width - RectWidth(srcRec);
2289 dx2 := dstImg.Width - RectWidth(dstRec);
2290 p1 := @srcImg.Pixels[srcRec.Top * srcImg.Width + srcRec.Left];
2291 p2 := @dstImg.Pixels[dstRec.Top * dstImg.Width + dstRec.Left];
2292 for i := srcRec.Top to srcRec.Bottom -1 do
2293 begin
2294 for j := srcRec.Left to srcRec.Right -1 do
2295 begin
2296 p2^ := ApplyColorMatrix(p1^, colorMatrix);
2297 inc(p1); inc(p2);
2298 end;
2299 inc(p1, dx1); inc(p2, dx2);
2300 end;
2301 end;
2302
2303 //------------------------------------------------------------------------------
2304 // TFeComponentTransferElement
2305 //------------------------------------------------------------------------------
2306
2307 procedure TFeComponentTransferElement.Apply;
2308 var
2309 i,j,k, dx1,dx2: integer;
2310 d: double;
2311 rangeSize: integer;
2312 p1: PColor32;
2313 p2: PARGB;
2314 childFuncs: array[0..3] of TFeComponentTransferChild;
2315 begin
2316 if not GetSrcAndDst or (ChildCount = 0) then Exit;
2317 for i := 0 to 3 do childFuncs[i] := nil;
2318 for i := 0 to ChildCount -1 do
2319 begin
2320 if Child[i] is TFeFuncBElement then
2321 childFuncs[0] := TFeFuncBElement(Child[i])
2322 else if Child[i] is TFeFuncGElement then
2323 childFuncs[1] := TFeFuncGElement(Child[i])
2324 else if Child[i] is TFeFuncRElement then
2325 childFuncs[2] := TFeFuncRElement(Child[i])
2326 else if Child[i] is TFeFuncAElement then
2327 childFuncs[3] := TFeFuncAElement(Child[i]);
2328 end;
2329
2330 // build each childFuncs' bytes array
2331 for k := 0 to 3 do
2332 with childFuncs[k] do
2333 begin
2334 if not Assigned(childFuncs[k]) then Continue;
2335 case funcType of
2336 ftDiscrete:
2337 begin
2338 if Length(tableValues) = 0 then Continue;
2339 SetLength(bytes, 256);
2340 rangeSize := 256 div Length(tableValues);
2341 for i:= 0 to High(tableValues) do
2342 for j:= 0 to rangeSize -1 do
2343 bytes[i*rangeSize + j] := ClampByte(tableValues[i] * 255);
2344 end;
2345 ftTable:
2346 begin
2347 if Length(tableValues) < 2 then Continue;
2348 SetLength(bytes, 256);
2349 rangeSize := 256 div (Length(tableValues) -1);
2350 for i:= 0 to High(tableValues) -1 do
2351 begin
2352 intercept := tableValues[i];
2353 slope := (tableValues[i+1] - intercept) / rangeSize;
2354 for j:= 0 to rangeSize -1 do
2355 bytes[i*rangeSize + j] := ClampByte((j * slope + intercept) * 255);
2356 end;
2357 end;
2358 ftLinear:
2359 begin
2360 SetLength(bytes, 256);
2361 d := intercept * 255;
2362 for i:= 0 to 255 do
2363 bytes[i] := ClampByte(i * slope + d);
2364 end;
2365 end;
2366 end;
2367
2368 for k := 0 to 3 do
2369 if Assigned(childFuncs[k]) and not Assigned(childFuncs[k].bytes) then
2370 childFuncs[k] := nil;
2371
2372 dx1 := srcImg.Width - RectWidth(srcRec);
2373 dx2 := dstImg.Width - RectWidth(dstRec);
2374 p1 := @srcImg.Pixels[srcRec.Top * srcImg.Width + srcRec.Left];
2375 p2 := @dstImg.Pixels[dstRec.Top * dstImg.Width + dstRec.Left];
2376 for i := srcRec.Top to srcRec.Bottom -1 do
2377 begin
2378 for j := srcRec.Left to srcRec.Right -1 do
2379 begin
2380 p2.Color := p1^;
2381 if Assigned(childFuncs[0]) then p2.B := childFuncs[0].bytes[p2.B];
2382 if Assigned(childFuncs[1]) then p2.G := childFuncs[1].bytes[p2.G];
2383 if Assigned(childFuncs[2]) then p2.R := childFuncs[2].bytes[p2.R];
2384 if Assigned(childFuncs[3]) then p2.A := childFuncs[3].bytes[p2.A];
2385 inc(p1); inc(p2);
2386 end;
2387 inc(p1, dx1); inc(p2, dx2);
2388 end;
2389 end;
2390
2391 //------------------------------------------------------------------------------
2392 // TFeDefuseLightElement
2393 //------------------------------------------------------------------------------
2394
2395 procedure TFeDefuseLightElement.Apply;
2396 begin
2397 //not implemented
2398 if not GetSrcAndDst then Exit;
2399 if srcImg <> dstImg then
2400 dstImg.Copy(srcImg, srcRec, dstRec);
2401 end;
2402
2403 //------------------------------------------------------------------------------
2404 // TFeDropShadowElement
2405 //------------------------------------------------------------------------------
2406
2407 constructor TFeDropShadowElement.Create(parent: TBaseElement; svgEl: TSvgXmlEl);
2408 begin
2409 inherited;
2410 stdDev := InvalidD;
2411 floodColor := clInvalid;
2412 offset.X.SetValue(0);
2413 offset.Y.SetValue(0);
2414 end;
2415 //------------------------------------------------------------------------------
2416
2417 procedure TFeDropShadowElement.Apply;
2418 var
2419 alpha: Byte;
2420 off: TPointD;
2421 dstOffRec: TRect;
2422 pfe: TFilterElement;
2423 dropShadImg: TImage32;
2424 begin
2425 if not GetSrcAndDst then Exit;
2426 pfe := ParentFilterEl;
2427 dropShadImg := pfe.GetNamedImage(tmpFilterImg, false);
2428 dropShadImg.Copy(srcImg, srcRec, dropShadImg.Bounds);
2429
2430 off := offset.GetPoint(RectD(pfe.fObjectBounds), GetRelFracLimit);
2431 off := ScalePoint(off, pfe.fScale);
2432 dstOffRec := dstRec;
2433 with Point(off) do TranslateRect(dstOffRec, X, Y);
2434 dstImg.Copy(srcImg, srcRec, dstOffRec);
2435 dstImg.SetRGB(floodColor);
2436 alpha := GetAlpha(floodColor);
2437 if (alpha > 0) and (alpha < 255) then
2438 dstImg.ReduceOpacity(alpha);
2439 if stdDev > 0 then
2440 FastGaussianBlur(dstImg, dstRec,
2441 Ceil(stdDev *0.75 * ParentFilterEl.fScale) , 1);
2442 dstImg.CopyBlend(dropShadImg, dropShadImg.Bounds, dstRec, BlendToAlphaLine);
2443 end;
2444
2445 //------------------------------------------------------------------------------
2446 // TFeFloodElement
2447 //------------------------------------------------------------------------------
2448
2449 constructor TFeFloodElement.Create(parent: TBaseElement; svgEl: TSvgXmlEl);
2450 begin
2451 inherited;
2452 floodColor := clInvalid;
2453 end;
2454 //------------------------------------------------------------------------------
2455
2456 procedure TFeFloodElement.Apply;
2457 var
2458 rec: TRect;
2459 begin
2460 if not GetSrcAndDst then Exit;
2461 if elRectWH.IsValid then
2462 rec := Rect(elRectWH.GetRectD(RectD(srcRec), GetRelFracLimit)) else
2463 rec := dstRec;
2464 dstImg.FillRect(rec, floodColor);
2465 end;
2466
2467 //------------------------------------------------------------------------------
2468 // TFeGaussElement
2469 //------------------------------------------------------------------------------
2470
2471 constructor TFeGaussElement.Create(parent: TBaseElement; svgEl: TSvgXmlEl);
2472 begin
2473 inherited;
2474 stdDev := InvalidD;
2475 end;
2476 //------------------------------------------------------------------------------
2477
2478 procedure TFeGaussElement.Apply;
2479 begin
2480 if (stdDev = InvalidD) or not GetSrcAndDst then Exit;
2481
2482 if srcImg <> dstImg then
2483 dstImg.Copy(srcImg, srcRec, dstRec);
2484 //GaussianBlur(dstImg, dstRec, Round(stdDev * ParentFilterEl.fScale));
2485 // FastGaussianBlur is a very good approximation and also much faster.
2486 // However, empirically stdDev/2 more closely emulates other renderers.
2487 FastGaussianBlur(dstImg, dstRec, Ceil(stdDev/2 * ParentFilterEl.fScale));
2488 end;
2489
2490 //------------------------------------------------------------------------------
2491 // TFeMergeElement
2492 //------------------------------------------------------------------------------
2493
2494 procedure TFeMergeElement.Apply;
2495 var
2496 i: integer;
2497 tmpImg: TImage32;
2498 pfe: TFilterElement;
2499 begin
2500 tmpImg := nil;
2501 if not GetSrcAndDst then Exit;
2502 pfe := ParentFilterEl;
2503
2504 for i := 0 to fChilds.Count -1 do
2505 if TBaseElement(fChilds[i]) is TFeMergeNodeElement then
2506 with TFeMergeNodeElement(fChilds[i]) do
2507 begin
2508 if not GetSrcAndDst then Continue;
2509 if Assigned(tmpImg) then
2510 tmpImg.CopyBlend(srcImg, srcRec, tmpImg.Bounds, BlendToAlphaLine)
2511 else if srcImg = pfe.fSrcImg then
2512 tmpImg := pfe.GetNamedImage(SourceImage, false)
2513 else
2514 tmpImg := srcImg;
2515 end;
2516
2517 dstImg.Copy(tmpImg, tmpImg.Bounds, dstRec);
2518 pfe.fLastImg := dstImg;
2519 end;
2520
2521 //------------------------------------------------------------------------------
2522 // TFeMergeNodeElement
2523 //------------------------------------------------------------------------------
2524
2525 procedure TFeMergeNodeElement.Apply;
2526 begin
2527 //should never get here ;)
2528 end;
2529
2530 //------------------------------------------------------------------------------
2531 // TFeOffsetElement
2532 //------------------------------------------------------------------------------
2533
2534 procedure TFeOffsetElement.Apply;
2535 var
2536 off: TPointD;
2537 dstOffRec: TRect;
2538 tmpImg: TImage32;
2539 pfe: TFilterElement;
2540 begin
2541 if not GetSrcAndDst then Exit;
2542 pfe := ParentFilterEl;
2543 off := offset.GetPoint(RectD(pfe.fObjectBounds), GetRelFracLimit);
2544 off := ScalePoint(off, pfe.fScale);
2545 dstOffRec := dstRec;
2546 with Point(off) do TranslateRect(dstOffRec, X, Y);
2547
2548 if srcImg = dstImg then
2549 begin
2550 tmpImg := pfe.GetNamedImage(tmpFilterImg, false);
2551 tmpImg.Copy(srcImg, srcRec, tmpImg.Bounds);
2552 dstImg.Clear(dstRec);
2553 dstImg.Copy(tmpImg, tmpImg.Bounds, dstOffRec);
2554 end else
2555 begin
2556 dstImg.Clear(dstRec);
2557 dstImg.Copy(srcImg, srcRec, dstOffRec);
2558 end;
2559 end;
2560
2561 //------------------------------------------------------------------------------
2562 // TFeSpecLightElement
2563 //------------------------------------------------------------------------------
2564
2565 procedure TFeSpecLightElement.Apply;
2566 begin
2567 //not implemented
2568 if not GetSrcAndDst then Exit;
2569 if srcImg <> dstImg then
2570 dstImg.Copy(srcImg, srcRec, dstRec);
2571 end;
2572
2573 //------------------------------------------------------------------------------
2574 // TClipPathElement
2575 //------------------------------------------------------------------------------
2576
2577 constructor TClipPathElement.Create(parent: TBaseElement; svgEl: TSvgXmlEl);
2578 begin
2579 inherited;
2580 fDrawData.visible := false;
2581 end;
2582 //------------------------------------------------------------------------------
2583
2584 procedure TClipPathElement.GetPaths(const drawDat: TDrawData);
2585 var
2586 i: integer;
2587 begin
2588 if pathsLoaded then Exit;
2589 pathsLoaded := true;
2590 for i := 0 to fChilds.Count -1 do
2591 if TBaseElement(fChilds[i]) is TShapeElement then
2592 with TShapeElement(fChilds[i]) do
2593 begin
2594 GetPaths(drawDat);
2595 AppendPath(self.drawPathsO, drawPathsO);
2596 AppendPath(self.drawPathsC, drawPathsC);
2597 // apply child's matrix ...
2598 MatrixApply(DrawData.matrix, self.drawPathsC);
2599 MatrixApply(DrawData.matrix, self.drawPathsO);
2600 end;
2601 // apply <clippath>'s matrix ...
2602 MatrixApply(DrawData.matrix, drawPathsC);
2603 MatrixApply(DrawData.matrix, drawPathsO);
2604 end;
2605
2606 //------------------------------------------------------------------------------
2607 // TShapeElement
2608 //------------------------------------------------------------------------------
2609
2610 constructor TShapeElement.Create(parent: TBaseElement; svgEl: TSvgXmlEl);
2611 begin
2612 inherited;
2613 elRectWH.Init;
2614 hasPaths := true;
2615 fDrawData.visible := true;
2616 if fXmlEl.name = '' then Exit;
2617 end;
2618 //------------------------------------------------------------------------------
2619
2620 function TShapeElement.GetBounds: TRectD;
2621 var
2622 i: integer;
2623 begin
2624 Result := UnionRect(GetBoundsD(drawPathsC), GetBoundsD(drawPathsO));
2625 if Result.IsEmpty then
2626 begin
2627 Result := NullRectD;
2628 for i := 0 to fChilds.Count -1 do
2629 if TBaseElement(Child[i]) is TShapeElement then
2630 Result := UnionRect(Result, TShapeElement(Child[i]).GetBounds);
2631 end;
2632 end;
2633 //------------------------------------------------------------------------------
2634
2635 function TShapeElement.HasMarkers: Boolean;
2636 begin
2637 Result := IsStroked(fDrawData) and ((fDrawData.markerStart <> '') or
2638 (fDrawData.markerMiddle <> '') or (fDrawData.markerEnd <> ''));
2639 end;
2640 //------------------------------------------------------------------------------
2641
2642 procedure TShapeElement.Draw(image: TImage32; drawDat: TDrawData);
2643 var
2644 d : double;
2645 img : TImage32;
2646 stroked : Boolean;
2647 filled : Boolean;
2648 tmpRec : TRectD;
2649 clipRec : TRectD;
2650 clipRec2 : TRect;
2651 clipPathEl : TBaseElement;
2652 filterEl : TBaseElement;
2653 maskEl : TBaseElement;
2654 clipPaths : TPathsD;
2655 fillPaths : TPathsD;
2656 di : TDrawData;
2657 useTmpImage : Boolean;
2658 begin
2659 UpdateDrawInfo(drawDat, self);
2660
2661 filled := IsFilled(drawDat);
2662 stroked := IsStroked(drawDat);
2663 GetPaths(drawDat);
2664
2665 if not (filled or stroked) or not hasPaths then
2666 begin
2667 inherited;
2668 Exit;
2669 end;
2670 tmpRec := GetBounds;
2671 if not tmpRec.IsEmpty then
2672 drawDat.bounds := tmpRec;
2673
2674 img := image;
2675 clipRec2 := NullRect;
2676
2677 maskEl := FindRefElement(drawDat.maskElRef);
2678 clipPathEl := FindRefElement(drawDat.clipElRef);
2679 filterEl := FindRefElement(drawDat.filterElRef);
2680
2681 useTmpImage :=
2682 Assigned(clipPathEl) or Assigned(filterEl) or Assigned(maskEl);
2683
2684 if useTmpImage then
2685 begin
2686 img := fSvgReader.TempImage;
2687
2688 //get special effects bounds
2689 if Assigned(clipPathEl) then
2690 begin
2691 drawDat.clipElRef := '';
2692 di := drawDat;
2693 with TClipPathElement(clipPathEl) do
2694 begin
2695 GetPaths(di);
2696 clipPaths := drawPathsC;
2697 AppendPath(clipPaths, drawPathsO);
2698 clipPaths := MatrixApply(clipPaths, di.matrix);
2699 clipRec := GetBoundsD(clipPaths);
2700 end;
2701 end
2702 else if Assigned(maskEl) then
2703 begin
2704 drawDat.maskElRef := '';
2705 with TMaskElement(maskEl) do
2706 begin
2707 GetPaths(drawDat);
2708 clipRec := RectD(maskRec);
2709 end;
2710 end else
2711 begin
2712 clipRec := drawDat.bounds;
2713 if clipRec.IsEmpty and (drawDat.fontInfo.textLength > 0) and
2714 (self is TTextPathElement) then
2715 begin
2716 clipRec.Left := fParent.elRectWH.left.rawVal;
2717 clipRec.Bottom := fParent.elRectWH.top.rawVal;
2718 clipRec.Right := clipRec.Left + drawDat.fontInfo.textLength;
2719 clipRec.Top := clipRec.Bottom - drawDat.fontInfo.size;
2720 end;
2721 if stroked and drawDat.strokeWidth.IsValid then
2722 begin
2723 with drawDat.strokeWidth do
2724 if HasFontUnits then
2725 d := GetValue(drawDat.fontInfo.size, GetRelFracLimit) else
2726 d := GetValueXY(clipRec, GetRelFracLimit);
2727 Img32.Vector.InflateRect(clipRec, d * 0.5, d * 0.5);
2728 end;
2729 if Assigned(filterEl) then
2730 begin
2731 drawDat.filterElRef := '';
2732 with TFilterElement(filterEl) do
2733 begin
2734 MatrixExtractScale(DrawData.matrix, fScale);
2735 clipRec := GetAdjustedBounds(clipRec);
2736 end;
2737 end;
2738 MatrixApply(drawDat.matrix, clipRec);
2739 end;
2740 clipRec2 := Rect(clipRec);
2741 Types.IntersectRect(clipRec2, clipRec2, img.Bounds);
2742 if IsEmptyRect(clipRec2) then Exit;
2743 if image <> fSvgReader.TempImage then
2744 img.Clear(clipRec2);
2745 end;
2746
2747 if not IsValidMatrix(drawDat.matrix) then
2748 raise Exception.Create('Invalid matrix found when drawing element');
2749
2750 if Assigned(drawPathsC) or Assigned(drawPathsO) then
2751 begin
2752 if filled then
2753 begin
2754 // it's slightly more efficient to apply the matrix here
2755 // rather than inside DrawFilled().
2756 fillPaths := drawPathsC;
2757 if Assigned(drawPathsO) then
2758 AppendPath(fillPaths, drawPathsO);
2759 fillPaths := MatrixApply(fillPaths, drawDat.matrix);
2760 DrawFilled(img, fillPaths, drawDat);
2761 end;
2762
2763 if stroked then
2764 begin
2765 // it's slightly more efficient to apply the matrix
2766 // inside DrawStroke() rather than here.
2767 if Assigned(drawPathsC) then
2768 DrawStroke(img, drawPathsC, drawDat, true);
2769 if stroked and Assigned(drawPathsO) then
2770 DrawStroke(img, drawPathsO, drawDat, false);
2771 end;
2772 end;
2773
2774 if Assigned(filterEl) then
2775 with TFilterElement(filterEl) do
2776 Apply(img, clipRec2, drawDat.matrix);
2777
2778 if Assigned(maskEl) then
2779 TMaskElement(maskEl).ApplyMask(img, drawDat)
2780 else if Assigned(clipPathEl) then
2781 with TClipPathElement(clipPathEl) do
2782 begin
2783 if fDrawData.fillRule = frNegative then
2784 EraseOutsidePaths(img, clipPaths, frNonZero, clipRec2,
2785 fSvgReader.fCustomRendererCache) else
2786 EraseOutsidePaths(img, clipPaths, fDrawData.fillRule, clipRec2,
2787 fSvgReader.fCustomRendererCache);
2788 end;
2789
2790 if useTmpImage and (img <> image) then
2791 image.CopyBlend(img, clipRec2, clipRec2, BlendToAlphaLine);
2792
2793 //todo: enable "paint-order" to change filled/stroked/marker paint order
2794 if HasMarkers then DrawMarkers(img, drawDat);
2795
2796 inherited; // DrawChildren
2797 end;
2798 //------------------------------------------------------------------------------
2799
2800 procedure TShapeElement.DrawMarkers(img: TImage32; drawDat: TDrawData);
2801 var
2802 i,j: integer;
2803 scale, sw: double;
2804 markerEl: TBaseElement;
2805 markerPaths: TPathsD;
2806 pt1, pt2: TPointD;
2807 di: TDrawData;
2808 begin
2809 markerPaths := GetSimplePath(drawDat);
2810 markerPaths := StripNearDuplicates(markerPaths, 0.01, false);
2811
2812 if not Assigned(markerPaths) then Exit;
2813 MatrixApply(drawDat.matrix, markerPaths);
2814
2815 di := emptyDrawInfo;
2816
2817 //prepare to scale the markers by the stroke width
2818 with fDrawData.strokeWidth do
2819 if not IsValid then sw := 1
2820 else if HasFontUnits then
2821 sw := GetValue(drawDat.fontInfo.size, GetRelFracLimit)
2822 else sw := GetValueXY(drawDat.bounds, GetRelFracLimit);
2823
2824 MatrixExtractScale(drawDat.matrix, scale);
2825 MatrixScale(di.matrix, sw * scale);
2826 if (fDrawData.markerStart <> '') then
2827 begin
2828 markerEl := FindRefElement(fDrawData.markerStart);
2829 if Assigned(markerEl) and (markerEl is TMarkerElement) then
2830 with TMarkerElement(markerEl) do
2831 begin
2832 for i := 0 to High(markerPaths) do
2833 begin
2834 if Length(markerPaths[i]) < 2 then Continue;
2835 pt1 := markerPaths[i][0];
2836 pt2 := markerPaths[i][1];
2837 if autoStartReverse then
2838 SetEndPoint(pt1, GetAngle(pt2, pt1)) else
2839 SetEndPoint(pt1, GetAngle(pt1, pt2));
2840 Draw(img, di);
2841 end;
2842 end;
2843 end;
2844
2845 if (fDrawData.markerMiddle <> '') then
2846 begin
2847 markerEl := FindRefElement(fDrawData.markerMiddle);
2848 if Assigned(markerEl) and (markerEl is TMarkerElement) then
2849 with TMarkerElement(markerEl) do
2850 for i := 0 to High(markerPaths) do
2851 if SetMiddlePoints(markerPaths[i]) then
2852 Draw(img, di);
2853 end;
2854
2855 if (fDrawData.markerEnd <> '') then
2856 begin
2857 markerEl := FindRefElement(fDrawData.markerEnd);
2858 if Assigned(markerEl) and (markerEl is TMarkerElement) then
2859 with TMarkerElement(markerEl) do
2860 begin
2861 for i := 0 to High(markerPaths) do
2862 begin
2863 j := High(markerPaths[i]);
2864 if j < 1 then Continue;
2865 pt1 := markerPaths[i][j];
2866 pt2 := markerPaths[i][j-1];
2867 SetEndPoint(pt1, GetAngle(pt2, pt1));
2868 Draw(img, di);
2869 end;
2870 end;
2871 end;
2872 end;
2873 //------------------------------------------------------------------------------
2874
2875 procedure TShapeElement.GetPaths(const drawDat: TDrawData);
2876 var
2877 i: integer;
2878 begin
2879 for i := 0 to fChilds.Count -1 do
2880 if TBaseElement(fChilds[i]) is TShapeElement then
2881 TShapeElement(fChilds[i]).GetPaths(drawDat);
2882 end;
2883 //------------------------------------------------------------------------------
2884
2885 function TShapeElement.GetSimplePath(const drawDat: TDrawData): TPathsD;
2886 begin
2887 Result := nil;
2888 end;
2889 //------------------------------------------------------------------------------
2890
2891 procedure TShapeElement.DrawFilled(img: TImage32;
2892 const paths: TPathsD; drawDat: TDrawData);
2893 var
2894 refEl: TBaseElement;
2895 rec: TRect;
2896 opacity: Byte;
2897 begin
2898 if not assigned(paths) then Exit;
2899 if drawDat.fillColor = clCurrent then
2900 drawDat.fillColor := fSvgReader.currentColor;
2901 if drawDat.fillRule = frNegative then
2902 drawDat.fillRule := frNonZero;
2903
2904 if not IsValid(drawDat.fillOpacity) then
2905 opacity := 255 else
2906 opacity := ClampByte(drawDat.fillOpacity * 255);
2907
2908 if (drawDat.fillEl <> '') then
2909 begin
2910 refEl := FindRefElement(drawDat.fillEl);
2911 if Assigned(refEl) and (refEl is TFillElement) then
2912 begin
2913 if refEl is TRadGradElement then
2914 begin
2915 with TRadGradElement(refEl) do
2916 begin
2917 fSvgReader.RadGradRenderer.Opacity := opacity;
2918 if PrepareRenderer(fSvgReader.RadGradRenderer, drawDat) then
2919 DrawPolygon(img, paths, drawDat.fillRule, fSvgReader.RadGradRenderer);
2920 end;
2921 end
2922 else if refEl is TLinGradElement then
2923 begin
2924 with TLinGradElement(refEl) do
2925 begin
2926 fSvgReader.LinGradRenderer.Opacity := opacity;
2927 if PrepareRenderer(fSvgReader.LinGradRenderer, drawDat) then
2928 DrawPolygon(img, paths, drawDat.fillRule, fSvgReader.LinGradRenderer);
2929 end;
2930 end
2931 else if refEl is TPatternElement then
2932 begin
2933 with TPatternElement(refEl) do
2934 if PrepareRenderer(ImgRenderer, drawDat) then
2935 begin
2936 rec := img32.Vector.GetBounds(paths);
2937 ImgRenderer.Offset := rec.TopLeft;
2938 DrawPolygon(img, paths, drawDat.fillRule, ImgRenderer);
2939 end;
2940 end;
2941 end;
2942 end
2943 else if drawDat.fillColor = clInvalid then
2944 begin
2945 DrawPolygon(img, paths, drawDat.fillRule,
2946 MergeColorAndOpacity(clBlack32, drawDat.fillOpacity),
2947 fSvgReader.fCustomRendererCache);
2948 end
2949 else
2950 with drawDat do
2951 begin
2952 DrawPolygon(img, paths, fillRule,
2953 MergeColorAndOpacity(fillColor, fillOpacity),
2954 fSvgReader.fCustomRendererCache);
2955 end;
2956 end;
2957 //------------------------------------------------------------------------------
2958
2959 procedure TShapeElement.DrawStroke(img: TImage32;
2960 const paths: TPathsD; drawDat: TDrawData; isClosed: Boolean);
2961 var
2962 i: integer;
2963 dashOffset, sw: double;
2964 dashArray: TArrayOfDouble;
2965 miterLim, scale: Double;
2966 strokeClr: TColor32;
2967 strokePaths: TPathsD;
2968 refEl: TBaseElement;
2969 endStyle: TEndStyle;
2970 joinStyle: TJoinStyle;
2971 bounds: TRectD;
2972 paths2: TPathsD;
2973 opacity: Byte;
2974 begin
2975 if not Assigned(paths) then Exit;
2976 MatrixExtractScale(drawDat.matrix, scale);
2977 joinStyle := fDrawData.strokeJoin;
2978
2979 bounds := fSvgReader.userSpaceBounds;
2980 with drawDat.strokeWidth do
2981 begin
2982 if not IsValid then
2983 sw := 1
2984 else if HasFontUnits then
2985 sw := GetValue(drawDat.fontInfo.size, GetRelFracLimit)
2986 else
2987 sw := GetValueXY(bounds, 0);
2988 end;
2989
2990 miterLim := drawDat.strokeMitLim;
2991 if drawDat.strokeColor = clCurrent then
2992 drawDat.strokeColor := fSvgReader.currentColor;
2993
2994 if Length(drawDat.dashArray) > 0 then
2995 dashArray := ScaleDashArray(drawDat.dashArray, scale) else
2996 dashArray := nil;
2997 dashOffset := drawDat.dashOffset;
2998
2999 with drawDat do
3000 strokeClr := MergeColorAndOpacity(strokeColor, strokeOpacity);
3001
3002 if not IsValid(drawDat.strokeOpacity) then
3003 opacity := 255 else
3004 opacity := ClampByte(drawDat.strokeOpacity * 255);
3005
3006 if isClosed then
3007 begin
3008 if Assigned(dashArray) then
3009 begin
3010 if joinStyle = jsRound then
3011 endStyle := esRound else
3012 endStyle := esButt;
3013 dashArray := ScaleDashArray(drawDat.dashArray, 1); // ie. don't scale yet!
3014 strokePaths := nil;
3015 for i := 0 to High(paths) do
3016 begin
3017 paths2 := GetDashedPath(paths[i], true, dashArray, @dashOffset);
3018 AppendPath(strokePaths, paths2);
3019 end;
3020 strokePaths :=
3021 RoughOutline(strokePaths, sw, joinStyle, endStyle, miterLim, scale);
3022 end else
3023 begin
3024 endStyle := esPolygon;
3025 strokePaths :=
3026 RoughOutline(paths, sw, joinStyle, endStyle, miterLim, scale);
3027 end;
3028 end else
3029 begin
3030 if fDrawData.strokeCap = esPolygon then
3031 endStyle := esButt else
3032 endStyle := fDrawData.strokeCap;
3033 if Assigned(dashArray) then
3034 begin
3035 strokePaths := MatrixApply(paths, drawDat.matrix);
3036 DrawDashedLine(img, strokePaths, dashArray,
3037 @dashOffset, sw * scale, strokeClr, endStyle, jsAuto,
3038 fSvgReader.fCustomRendererCache);
3039 Exit;
3040 end;
3041 strokePaths :=
3042 RoughOutline(paths, sw, joinStyle, endStyle, miterLim, scale);
3043 end;
3044 strokePaths := MatrixApply(strokePaths, drawDat.matrix);
3045
3046 if (drawDat.strokeEl <> '') then
3047 begin
3048 refEl := FindRefElement(drawDat.strokeEl);
3049 if not Assigned(refEl) then Exit;
3050
3051 if refEl is TRadGradElement then
3052 begin
3053 with TRadGradElement(refEl) do
3054 begin
3055 fSvgReader.RadGradRenderer.Opacity := opacity;
3056 PrepareRenderer(fSvgReader.RadGradRenderer, drawDat);
3057 end;
3058 DrawPolygon(img, strokePaths, frNonZero, fSvgReader.RadGradRenderer);
3059 end
3060 else if refEl is TLinGradElement then
3061 begin
3062 with TLinGradElement(refEl) do
3063 begin
3064 fSvgReader.LinGradRenderer.Opacity := opacity;
3065 PrepareRenderer(fSvgReader.LinGradRenderer, drawDat);
3066 end;
3067 DrawPolygon(img, strokePaths, frNonZero, fSvgReader.LinGradRenderer);
3068 end
3069 else if refEl is TPatternElement then
3070 with TPatternElement(refEl) do
3071 begin
3072 imgRenderer.Opacity := opacity;
3073 PrepareRenderer(imgRenderer, drawDat);
3074 DrawLine(img, strokePaths, 1, imgRenderer, esPolygon, joinStyle, scale);
3075 DrawPolygon(img, strokePaths, frNonZero, imgRenderer);
3076 end;
3077 end else
3078 begin
3079 DrawPolygon(img, strokePaths,
3080 frNonZero, strokeClr, fSvgReader.fCustomRendererCache);
3081 end;
3082 end;
3083
3084 //------------------------------------------------------------------------------
3085 // TPathElement
3086 //------------------------------------------------------------------------------
3087
3088 constructor TPathElement.Create(parent: TBaseElement; svgEl: TSvgXmlEl);
3089 begin
3090 inherited;
3091 fSvgPaths := TSvgPath.Create;
3092 end;
3093 //------------------------------------------------------------------------------
3094
3095 destructor TPathElement.Destroy;
3096 begin
3097 fSvgPaths.Free;
3098 inherited;
3099 end;
3100 //------------------------------------------------------------------------------
3101
3102 function TPathElement.GetBounds: TRectD;
3103 var
3104 i: integer;
3105 begin
3106 Result := NullRectD;
3107 for i := 0 to fSvgPaths.Count -1 do
3108 Result := UnionRect(Result, fSvgPaths[i].GetBounds);
3109 end;
3110 //------------------------------------------------------------------------------
3111
3112 procedure TPathElement.ParseDAttrib(const value: UTF8String);
3113 begin
3114 fSvgPaths.Parse(value);
3115 end;
3116 //------------------------------------------------------------------------------
3117
3118 procedure TPathElement.Flatten(index: integer; scalePending: double;
3119 out path: TPathD; out isClosed: Boolean);
3120 begin
3121 isClosed := fSvgPaths[index].isClosed;
3122 path := fSvgPaths[index].GetFlattenedPath(scalePending);
3123 end;
3124 //------------------------------------------------------------------------------
3125
3126 procedure TPathElement.GetPaths(const drawDat: TDrawData);
3127 var
3128 i: integer;
3129 scalePending: double;
3130 isClosed: Boolean;
3131 path: TPathD;
3132 begin
3133 if pathsLoaded then Exit;
3134 pathsLoaded := true;
3135 MatrixExtractScale(drawDat.matrix, scalePending);
3136 for i := 0 to fSvgPaths.Count -1 do
3137 begin
3138 Flatten(i, scalePending, path, isClosed);
3139 if not Assigned(path) then Continue;
3140
3141 if isClosed then
3142 AppendPath(drawPathsC, path) else
3143 AppendPath(drawPathsO, path);
3144 end;
3145 end;
3146 //------------------------------------------------------------------------------
3147
3148 function TPathElement.GetSimplePath(const drawDat: TDrawData): TPathsD;
3149 var
3150 i: integer;
3151 begin
3152 Result := nil;
3153 SetLength(Result, fSvgPaths.Count);
3154 for i := 0 to fSvgPaths.Count -1 do
3155 Result[i] := fSvgPaths[i].GetSimplePath;
3156 end;
3157
3158 //------------------------------------------------------------------------------
3159 // TPolyElement
3160 //------------------------------------------------------------------------------
3161
3162 function TPolyElement.GetBounds: TRectD;
3163 begin
3164 Result := GetBoundsD(path);
3165 end;
3166 //------------------------------------------------------------------------------
3167
3168 procedure TPolyElement.GetPaths(const drawDat: TDrawData);
3169 begin
3170 if pathsLoaded or not Assigned(path) then Exit;
3171 pathsLoaded := true;
3172 if (fXmlEl.hash = hPolygon) then
3173 begin
3174 AppendPath(drawPathsC, path); //hPolygon
3175 end else
3176 begin
3177 AppendPath(drawPathsO, path); //hPolyline
3178 end;
3179 end;
3180 //------------------------------------------------------------------------------
3181
3182 function TPolyElement.GetSimplePath(const drawDat: TDrawData): TPathsD;
3183 begin
3184 Result := nil;
3185 AppendPath(Result, path);
3186 end;
3187 //------------------------------------------------------------------------------
3188
3189 procedure TPolyElement.ParsePoints(const value: UTF8String);
3190 var
3191 currCnt, currCap: integer;
3192
3193 procedure AddPoint(const pt: TPointD);
3194 begin
3195 if currCnt = currCap then
3196 begin
3197 currCap := currCap + buffSize;
3198 SetLength(path, currCap);
3199 end;
3200 path[currCnt] := pt;
3201 inc(currCnt);
3202 end;
3203
3204 var
3205 pt: TPointD;
3206 c, endC: PUTF8Char;
3207 begin
3208 currCnt := 0;
3209 currCap := buffSize;
3210 c := PUTF8Char(value);
3211 endC := c + Length(value);
3212 SetLength(path, currCap);
3213 while IsNumPending(c, endC, true) and
3214 ParseNextNum(c, endC, true, pt.X) and
3215 ParseNextNum(c, endC, true, pt.Y) do
3216 AddPoint(pt);
3217 SetLength(path, currCnt);
3218 end;
3219
3220 //------------------------------------------------------------------------------
3221 // TLineElement
3222 //------------------------------------------------------------------------------
3223
3224 constructor TLineElement.Create(parent: TBaseElement; svgEl: TSvgXmlEl);
3225 begin
3226 inherited;
3227 NewPointDArray(path, 2, True);
3228 path[0] := NullPointD; path[1] := NullPointD;
3229 end;
3230 //------------------------------------------------------------------------------
3231
3232 function TLineElement.GetBounds: TRectD;
3233 begin
3234 Result := GetBoundsD(path);
3235 end;
3236 //------------------------------------------------------------------------------
3237
3238 procedure TLineElement.GetPaths(const drawDat: TDrawData);
3239 begin
3240 if pathsLoaded then Exit;
3241 pathsLoaded := true;
3242 AppendPath(drawPathsO, path);
3243 end;
3244 //------------------------------------------------------------------------------
3245
3246 function TLineElement.GetSimplePath(const drawDat: TDrawData): TPathsD;
3247 begin
3248 Result := nil;
3249 AppendPath(Result, path);
3250 end;
3251
3252 //------------------------------------------------------------------------------
3253 // TCircleElement
3254 //------------------------------------------------------------------------------
3255
3256 constructor TCircleElement.Create(parent: TBaseElement; svgEl: TSvgXmlEl);
3257 begin
3258 inherited;
3259 bounds := NullRectD;
3260 centerPt.Init;
3261 radius.Init;
3262 end;
3263 //------------------------------------------------------------------------------
3264
3265 function TCircleElement.GetBounds: TRectD;
3266 begin
3267 Result := bounds;
3268 end;
3269 //------------------------------------------------------------------------------
3270
3271 procedure TCircleElement.GetPaths(const drawDat: TDrawData);
3272 var
3273 scalePending : double;
3274 pt : TPointD;
3275 path : TPathD;
3276 r: double;
3277 begin
3278 if pathsLoaded or not radius.IsValid then Exit;
3279 pathsLoaded := true;
3280 r := radius.GetValueXY(drawDat.bounds, GetRelFracLimit);
3281 pt := centerPt.GetPoint(drawDat.bounds, GetRelFracLimit);
3282 MatrixExtractScale(drawDat.matrix, scalePending);
3283 bounds := RectD(pt.X -r, pt.Y -r, pt.X +r, pt.Y +r);
3284 path := Ellipse(bounds, scalePending);
3285 AppendPath(drawPathsC, path);
3286 end;
3287
3288 //------------------------------------------------------------------------------
3289 // TEllipseElement
3290 //------------------------------------------------------------------------------
3291
3292 constructor TEllipseElement.Create(parent: TBaseElement; svgEl: TSvgXmlEl);
3293 begin
3294 inherited;
3295 centerPt.Init;
3296 radius.Init;
3297 end;
3298 //------------------------------------------------------------------------------
3299
3300 function TEllipseElement.GetBounds: TRectD;
3301 begin
3302 Result := bounds;
3303 end;
3304 //------------------------------------------------------------------------------
3305
3306 procedure TEllipseElement.GetPaths(const drawDat: TDrawData);
3307 var
3308 scalePending : double;
3309 path : TPathD;
3310 rad : TPointD;
3311 centPt : TPointD;
3312 begin
3313 if pathsLoaded then Exit;
3314 pathsLoaded := true;
3315 rad := radius.GetPoint(drawDat.bounds, GetRelFracLimit);
3316 centPt := centerPt.GetPoint(drawDat.bounds, GetRelFracLimit);
3317 with centPt do
3318 bounds := RectD(X -rad.X, Y -rad.Y, X +rad.X, Y +rad.Y);
3319 MatrixExtractScale(drawDat.matrix, scalePending);
3320 path := Ellipse(bounds, scalePending);
3321 AppendPath(drawPathsC, path);
3322 end;
3323
3324 //------------------------------------------------------------------------------
3325 // TRectElement
3326 //------------------------------------------------------------------------------
3327
3328 constructor TRectElement.Create(parent: TBaseElement; svgEl: TSvgXmlEl);
3329 begin
3330 inherited;
3331 radius.Init;
3332 elRectWH.width.SetValue(100, utPercent);
3333 elRectWH.height.SetValue(100, utPercent);
3334 end;
3335 //------------------------------------------------------------------------------
3336
3337 function TRectElement.GetBounds: TRectD;
3338 begin
3339 Result := elRectWH.GetRectD(NullRectD, GetRelFracLimit);
3340 end;
3341 //------------------------------------------------------------------------------
3342
3343 procedure TRectElement.GetPaths(const drawDat: TDrawData);
3344 var
3345 radXY : TPointD;
3346 bounds: TRectD;
3347 path : TPathD;
3348 begin
3349 if pathsLoaded then Exit;
3350 if elRectWH.width.HasFontUnits then
3351 bounds := elRectWH.GetRectD(drawDat.fontInfo.size, GetRelFracLimit) else
3352 bounds := elRectWH.GetRectD(drawDat.bounds, GetRelFracLimit);
3353 if bounds.IsEmpty then Exit;
3354 pathsLoaded := true;
3355
3356 radXY := radius.GetPoint(bounds, GetRelFracLimit);
3357 if (radXY.X > 0) or (radXY.Y > 0) then
3358 begin
3359 if (radXY.X <= 0) then radXY.X := radXY.Y
3360 else if (radXY.Y <= 0) then radXY.Y := radXY.X;
3361 path := RoundRect(bounds, radXY);
3362 end else
3363 path := Rectangle(bounds);
3364 AppendPath(drawPathsC, path);
3365 end;
3366 //------------------------------------------------------------------------------
3367
3368 function TRectElement.GetSimplePath(const drawDat: TDrawData): TPathsD;
3369 var
3370 rec: TRectD;
3371 begin
3372 Result := nil;
3373 rec := elRectWH.GetRectD(drawDat.bounds, GetRelFracLimit);
3374 if not rec.IsEmpty then
3375 AppendPath(Result, Rectangle(rec));
3376 end;
3377
3378 //------------------------------------------------------------------------------
3379 // TTextElement
3380 //------------------------------------------------------------------------------
3381
3382 constructor TTextElement.Create(parent: TBaseElement; svgEl: TSvgXmlEl);
3383 begin
3384 inherited;
3385 offset.Init;
3386 hasPaths := false;
3387 end;
3388 //------------------------------------------------------------------------------
3389
3390 procedure TTextElement.Draw(img: TImage32; drawDat: TDrawData);
3391 begin
3392 UpdateDrawInfo(drawDat, self);
3393 UpdateFontInfo(drawDat, self);
3394
3395 fSvgReader.GetBestFont(drawDat.FontInfo);
3396 if not Assigned(fSvgReader.fFontCache) then Exit;
3397 if drawDat.fontInfo.size = 0 then drawDat.fontInfo.size := 16;
3398
3399 if offset.X.IsValid then
3400 currentPt.X := offset.X.rawVal
3401 else if elRectWH.left.IsValid then
3402 currentPt.X := elRectWH.left.rawVal
3403 else
3404 currentPt.X := 0;
3405
3406 if offset.Y.IsValid then
3407 currentPt.Y := offset.Y.rawVal
3408 else if elRectWH.top.IsValid then
3409 currentPt.Y := elRectWH.top.rawVal
3410 else
3411 currentPt.Y := 0;
3412
3413 lastChrSpc := false;
3414 textDx := 0;
3415 currSpanEl := nil;
3416
3417 //get child paths (which also updates currentPt)
3418 GetPaths(drawDat);
3419
3420 DrawChildren(img, drawDat);
3421 end;
3422
3423 //------------------------------------------------------------------------------
3424 // TTextSubElement
3425 //------------------------------------------------------------------------------
3426
3427 function TTextSubElement.GetTextEl: TTextElement;
3428 var
3429 el: TBaseElement;
3430 begin
3431 if not Assigned(textEl) then
3432 begin
3433 el := fParent;
3434 while Assigned(el) and not (el is TTextElement) do
3435 el := el.fParent;
3436 if Assigned(el) then
3437 textEl := TTextElement(el);
3438 end;
3439 Result := textEl;
3440 end;
3441
3442 //------------------------------------------------------------------------------
3443 // TTSpanElement
3444 //------------------------------------------------------------------------------
3445
3446 procedure TTSpanElement.GetPaths(const drawDat: TDrawData);
3447 var
3448 tmpX, startX, fontScale, fontSize, bs: double;
3449 i,j, len : integer;
3450 di : TDrawData;
3451 s : UnicodeString;
3452 mat : TMatrixD;
3453 tmpPaths : TPathsD;
3454 codepoints: TArrayOfCardinal;
3455 angles : TArrayOfDouble;
3456 glyphInfo : PGlyphInfo;
3457 glyphRec : TRectD;
3458 begin
3459 // 1. We only want to process this method once even though it's called twice,
3460 // first indirectly by TTextElement.Draw, and then by TTSpanElement.Draw.
3461 // 2. This method isn't called when <tspan> is a sub-element of <textpath>.
3462
3463 if pathsLoaded then Exit;
3464 pathsLoaded := true;
3465
3466 di := drawDat;
3467 if ChildCount > 0 then
3468 begin
3469 UpdateDrawInfo(di, self);
3470 UpdateFontInfo(di, self);
3471 end;
3472
3473 if drawDat.FontInfo.size = 0 then
3474 fontSize := 16.0 else
3475 fontSize := drawDat.FontInfo.size;
3476 fSvgReader.GetBestFont(di.FontInfo);
3477 if not Assigned(fSvgReader.fFontCache) then Exit;
3478
3479 GetTextEl;
3480 if not Assigned(textEl) or
3481 (textEl.currentPt.X = InvalidD) or
3482 (textEl.currentPt.Y = InvalidD) then Exit;
3483
3484 //by not changing the fontCache.FontHeight, the quality of
3485 //small font render improves very significantly (though of course
3486 //this requires additional glyph scaling and offsetting).
3487 fontScale := fontSize / fSvgReader.fFontCache.FontHeight;
3488
3489 if elRectWH.left.IsValid then
3490 textEl.currentPt.X := elRectWH.left.rawVal;
3491 if elRectWH.top.IsValid then
3492 textEl.currentPt.Y := elRectWH.top.rawVal;
3493
3494 if offset.X.IsValid then
3495 textEl.currentPt.X := textEl.currentPt.X + offset.X.GetValue(0, 0);
3496 if offset.Y.IsValid then
3497 textEl.currentPt.Y := textEl.currentPt.Y + offset.Y.GetValue(0, 0);
3498
3499 // only 'virtual' (dummy) <tspan> elements are self-closing, and
3500 // mostly their parents are 'real' <tspan> elements. However,
3501 // virtual <tspan> elements can also have <text> element parents.
3502 if not fXmlEl.selfClosed then
3503 begin
3504 textEl.currSpanEl := self;
3505 angles := nil;
3506 end
3507 else if (fParent is TTSpanElement) then
3508 begin
3509 if Assigned(TTSpanElement(fParent).angle) then
3510 angles := TTSpanElement(fParent).angle else
3511 angles := textEl.angle;
3512 end else
3513 begin
3514 angles := textEl.angle;
3515 textEl.currSpanEl := nil;
3516 end;
3517
3518 chunkDx := 0;
3519 if (Length(fXmlEl.text) > 0) and (fontSize > 1) then
3520 begin
3521 // this should be a virtual (dummy) <tspan> element
3522 //assert(fXmlEl.selfClosed);
3523
3524 s := DecodeUtf8ToUnicode(HtmlDecode(fXmlEl.text));
3525 // don't allow a dup. spaces or a space at the beginning of a text
3526 s := FixSpaces(s, textEl.lastChrSpc or
3527 ((fParent = textEl) and (self = textEl.Child[0])));
3528
3529 if IsBlankText(s) then
3530 begin
3531 drawPathsC := nil;
3532 // don't allow duplicate spaces or a space at the beginning of text
3533 if textEl.lastChrSpc or (self = textEl.Child[0]) then Exit;
3534 tmpX := fSvgReader.fFontCache.GetSpaceWidth;
3535 textEl.lastChrSpc := true;
3536 end
3537 else if Assigned(angles) then
3538 begin
3539 drawPathsC := nil;
3540 tmpPaths := nil;
3541 tmpX := 0;
3542 codepoints := fSvgReader.fFontCache.GetTextCodePoints(s);
3543 // make sure 'angles' is at least the length of codepoints
3544 len := Length(codepoints);
3545 if len > Length(angles) then
3546 begin
3547 j := High(angles);
3548 SetLength(angles, len); // extend angles
3549 for i := j +1 to len -1 do angles[i] := angles[j];
3550 end;
3551
3552 textEl.lastChrSpc := (codepoints[len -1] = 32);
3553 // now get each rotated glyph and append to drawPathsC ...
3554 for i := 0 to len -1 do
3555 begin
3556 glyphInfo := fSvgReader.fFontCache.GetGlyphInfo(codepoints[i]);
3557 if Assigned(glyphInfo.paths) then
3558 begin
3559 glyphRec := GetBoundsD(glyphInfo.paths);
3560 tmpPaths := RotatePath(glyphInfo.paths, glyphRec.MidPoint, angles[i]);
3561 if i > 0 then
3562 tmpPaths := TranslatePath(tmpPaths, tmpX, 0);
3563 AppendPath(drawPathsC, tmpPaths);
3564 end;
3565 tmpX := tmpX + glyphInfo.hmtx.advanceWidth * fSvgReader.fFontCache.Scale;
3566 end;
3567 end else
3568 begin
3569 drawPathsC := fSvgReader.fFontCache.GetTextOutline(0, 0, s, tmpX);
3570 textEl.lastChrSpc := s[length(s)] = space;
3571 end;
3572
3573 chunkDx := tmpX * fontScale;
3574 if Assigned(textEl.currSpanEl) then
3575 with textEl.currSpanEl do
3576 chunkDx := chunkDx + self.chunkDx;
3577 textEl.textDx := textEl.textDx + chunkDx;
3578
3579 with textEl.currentPt do
3580 begin
3581 startX := X;
3582 X := X + chunkDx;
3583 end;
3584
3585 if Assigned(drawPathsC) then // eg. unassigned if a space char
3586 begin
3587 with drawDat.fontInfo do
3588 if not baseShift.IsValid then
3589 bs := 0 else
3590 bs := baseShift.GetValue(size, GetRelFracLimit);
3591 mat := IdentityMatrix;
3592 MatrixScale(mat, fontScale);
3593 MatrixTranslate(mat, startX, textEl.currentPt.Y - bs);
3594 MatrixApply(mat, drawPathsC);
3595 end;
3596 end;
3597
3598 // nested <tspan> elements are always possible,
3599 // except when self is a pseudo 'selfClosed' <tspan> element
3600 inherited GetPaths(di); // gets any children paths
3601 end;
3602 //------------------------------------------------------------------------------
3603
3604 procedure TTSpanElement.Draw(image: TImage32; drawDat: TDrawData);
3605 var
3606 stroked : Boolean;
3607 filled : Boolean;
3608 tmpRec : TRect;
3609 fillPaths : TPathsD;
3610 begin
3611 if ChildCount = 0 then
3612 fDrawData := fParent.fDrawData
3613 else
3614 begin
3615 UpdateDrawInfo(drawDat, self);
3616 UpdateFontInfo(drawDat, self);
3617 end;
3618
3619 if not fXmlEl.selfClosed then
3620 begin
3621 // DrawChildren and exit ...
3622 inherited;
3623 Exit;
3624 end;
3625
3626 filled := IsFilled(drawDat);
3627 stroked := IsStroked(drawDat);
3628 if Assigned(drawPathsC) and Assigned(textEl) then
3629 begin
3630 // a <tspan> element that contains text (and a path) must be virtual.
3631 // But its parent may be another <tspan>, or a <text> or a <textarea>.
3632
3633 tmpRec := Rect(GetBounds);
3634 if not IsEmptyRect(tmpRec) then
3635 drawDat.bounds := RectD(tmpRec);
3636
3637 if (fParent is TTSpanElement) and fParent.elRectWH.left.IsValid then
3638 begin
3639 case drawDat.FontInfo.align of
3640 staCenter: drawPathsC := TranslatePath(drawPathsC, -chunkDx * 0.5, 0);
3641 staRight: drawPathsC := TranslatePath(drawPathsC, -chunkDx, 0);
3642 end;
3643 end
3644 else if textEl.textDx <> 0 then
3645 begin
3646 case drawDat.FontInfo.align of
3647 staCenter: drawPathsC := TranslatePath(drawPathsC, -textEl.textDx * 0.5, 0);
3648 staRight: drawPathsC := TranslatePath(drawPathsC, -textEl.textDx, 0);
3649 end;
3650 end;
3651
3652 // todo - 1. implement paint-order - fill stroke vs stroke fill
3653 // 2. wavy and colored underlines
3654
3655 if stroked then
3656 begin
3657 // it's slightly more efficient to apply the matrix
3658 // inside DrawStroke() rather than here.
3659 DrawStroke(image, drawPathsC, drawDat, true);
3660 end;
3661
3662 if filled then
3663 begin
3664 // it's slightly more efficient to apply the matrix here
3665 // rather than inside DrawFilled().
3666 fillPaths := MatrixApply(drawPathsC, drawDat.matrix);
3667 DrawFilled(image, fillPaths, drawDat);
3668 end;
3669 end;
3670 end;
3671
3672 //------------------------------------------------------------------------------
3673 // TTextPathElement
3674 //------------------------------------------------------------------------------
3675
3676 function GetPathDistance(const path: TPathD): double;
3677 var
3678 i: integer;
3679 begin
3680 Result := 0;
3681 for i := 1 to High(path) do
3682 Result := Result + Distance(path[i-1], path[i]);
3683 end;
3684 //------------------------------------------------------------------------------
3685
3686 procedure TTextPathElement.GetPathsInternal(el: TBaseElement;
3687 const drawDat: TDrawData);
3688 var
3689 i, len : integer;
3690 charsThatFit : integer;
3691 spacing : double;
3692 textWidth : double;
3693 outX : double;
3694 spanEl : TTSpanElement;
3695 dd : TDrawData;
3696 unicodeText : UnicodeString;
3697 pathDist : double;
3698 mat : TMatrixD;
3699 tmpPath : TPathD;
3700 tmpPaths : TPathsD;
3701 isClosed : Boolean;
3702 begin
3703 if not (el is TTSpanElement) then Exit;
3704 spanEl := TTSpanElement(el);
3705 if Assigned(spanEl.drawPathsC) then Exit;
3706 spanEl.pathsLoaded := true;
3707 spanEl.GetTextEl;
3708
3709 dd := drawDat;
3710 UpdateDrawInfo(dd, el);
3711 UpdateFontInfo(dd, el);
3712
3713 if spanEl.offset.X.IsValid then
3714 textEl.currentPt.X := Max(0, textEl.currentPt.X +
3715 Round(spanEl.offset.X.rawVal / scale));
3716 if spanEl.offset.Y.IsValid then
3717 textEl.currentPt.Y := textEl.currentPt.Y +
3718 Round(spanEl.offset.Y.rawVal / scale);
3719
3720 if spanEl.fXmlEl.text = '' then
3721 begin
3722 // nb: recursive
3723 for i := 0 to spanEl.ChildCount -1 do
3724 GetPathsInternal(spanEl.Child[i], dd);
3725 Exit;
3726 end;
3727
3728 // nb: <tspan> elements that own text will always be pseudo <tspan> elements.
3729 // Pseudo <tspan> elements have been created inside real <tspan> elements to
3730 // provide a reliable way to manage text mixed with nested <tspan> elements.
3731
3732 //trim CRLFs and multiple spaces
3733 unicodeText := DecodeUtf8ToUnicode(HtmlDecode(spanEl.fXmlEl.text));
3734 if dd.fontInfo.spacesInText <> sitPreserve then
3735 unicodeText := TrimMultiSpacesUnicode(unicodeText) else
3736 unicodeText := StripNewlines(unicodeText);
3737
3738 //adjust glyph spacing when fFontInfo.textLength is assigned.
3739 spacing := dd.FontInfo.spacing /scale;
3740 len := Length(unicodeText);
3741 if (len < 2) then spacing := 0
3742 else if (dd.FontInfo.align = staJustify) and
3743 (pathEl.fsvgPaths.count = 1) then
3744 with TPathElement(pathEl) do
3745 begin
3746 Flatten(0, scale, tmpPath, isClosed);
3747 pathDist := GetPathDistance(tmpPath);
3748 textWidth := fSvgReader.fFontCache.GetTextWidth(unicodeText);
3749 spacing := (pathDist/scale) - textWidth;
3750 spacing := spacing / (len -1);
3751 end
3752 else if (dd.FontInfo.textLength > 0) then
3753 begin
3754 textWidth := fSvgReader.fFontCache.GetTextWidth(unicodeText);
3755 spacing := (dd.FontInfo.textLength/scale) - textWidth;
3756 spacing := spacing / (len -1);
3757 end;
3758
3759 with pathEl do
3760 begin
3761 mat := fDrawData.matrix;
3762 MatrixScale(mat, 1/scale);
3763 for i := 0 to fSvgPaths.Count -1 do
3764 begin
3765 Flatten(i, scale, tmpPath, isClosed);
3766 //'path' is temporarily scaled to accommodate fReader.fFontCache's
3767 //static fontheight. The returned glyphs will be de-scaled later.
3768 MatrixApply(mat, tmpPath);
3769 tmpPaths := GetTextOutlineOnPath(unicodeText, tmpPath,
3770 fSvgReader.fFontCache, taLeft, textEl.currentPt.X,
3771 textEl.currentPt.Y, spacing, charsThatFit, outX);
3772 AppendPath(spanEl.drawPathsC, tmpPaths);
3773 textEl.currentPt.X := outX;
3774 if charsThatFit = Length(unicodeText) then Break;
3775 Delete(unicodeText, 1, charsThatFit);
3776 textEl.currentPt := NullPointD;
3777 end;
3778 end;
3779 spanEl.drawPathsC := ScalePath(spanEl.drawPathsC, scale);
3780
3781 for i := 0 to spanEl.ChildCount -1 do
3782 GetPathsInternal(spanEl.Child[i], dd);
3783 end;
3784 //------------------------------------------------------------------------------
3785
3786 procedure TTextPathElement.GetPaths(const drawDat: TDrawData);
3787 var
3788 i: integer;
3789 dd: TDrawData;
3790 el: TBaseElement;
3791 begin
3792 if pathsLoaded or not Assigned(fSvgReader.fFontCache) then Exit;
3793 pathsLoaded := true;
3794 GetTextEl;
3795 if not Assigned(textEl) then Exit;
3796
3797 dd := drawDat;
3798 UpdateDrawInfo(dd, self);
3799 UpdateFontInfo(dd, self);
3800
3801 el := FindRefElement(pathName);
3802 if not (el is TPathElement) then Exit;
3803 pathEl := TPathElement(el);
3804 fSvgReader.GetBestFont(dd.FontInfo);
3805 scale := dd.FontInfo.size/fSvgReader.fFontCache.FontHeight;
3806
3807 if offset.X.IsValid then
3808 textEl.currentPt.X := Max(0,
3809 textEl.currentPt.X +
3810 Round(offset.X.GetValue(dd.bounds.Width, 1) / scale));
3811 if offset.Y.IsValid then
3812 textEl.currentPt.Y :=
3813 textEl.currentPt.Y + Round(offset.Y.rawVal / scale);
3814
3815 // nb: recursive
3816 for i := 0 to ChildCount -1 do
3817 GetPathsInternal(Child[i], drawDat);
3818 end;
3819 //------------------------------------------------------------------------------
3820
3821 function TTextPathElement.GetBounds: TRectD;
3822 var
3823 textEl: TTextElement;
3824 begin
3825 textEl := TTextElement(fParent);
3826 {$IFDEF UNICODE}
3827 if IsBlankText(UTF8ToUnicodeString(fXmlEl.text)) then
3828 {$ELSE}
3829 if IsBlankText(Utf8Decode(fXmlEl.text)) then
3830 {$ENDIF}
3831 Result := textEl.fDrawData.bounds else
3832 Result := inherited GetBounds;
3833 end;
3834 //------------------------------------------------------------------------------
3835
3836 procedure TTextPathElement.Draw(image: TImage32; drawDat: TDrawData);
3837 begin
3838 UpdateFontInfo(drawDat, self);
3839 inherited;
3840 end;
3841
3842 //------------------------------------------------------------------------------
3843 // TTextAreaElement
3844 //------------------------------------------------------------------------------
3845
3846 procedure TTextAreaElement.GetPaths(const drawDat: TDrawData);
3847 var
3848 scale : double;
3849 fontSize : double;
3850 lnHeight : double;
3851 di : TDrawData;
3852 mat : TMatrixD;
3853 text : Utf8String;
3854 s : UnicodeString;
3855 textRec : TRectD;
3856 const
3857 margin = 1;
3858 begin
3859 if pathsLoaded then Exit;
3860 text := fXmlEl.text;
3861 if not elRectWH.width.IsValid or not elRectWH.height.IsValid or
3862 (text = '') then Exit;
3863 pathsLoaded := true;
3864
3865 di := drawDat;
3866 UpdateDrawInfo(di, self);
3867
3868 if drawDat.FontInfo.size = 0 then
3869 fontSize := 16.0 else
3870 fontSize := drawDat.FontInfo.size;
3871 fSvgReader.GetBestFont(di.FontInfo);
3872 if not Assigned(fSvgReader.fFontCache) then Exit;
3873 scale := fontSize / fSvgReader.fFontCache.FontHeight;
3874
3875 s := DecodeUtf8ToUnicode(HtmlDecode(text));
3876 s := FixSpaces(s, false);
3877 s := StringReplace(s, '<tbreak/>', #10, [rfReplaceAll, rfIgnoreCase]);
3878
3879 lnHeight := fSvgReader.fFontCache.LineHeight;
3880
3881 textRec := elRectWH.GetRectD(di.bounds.Width, di.bounds.Height, 1);
3882 textRec := ScaleRect(textRec, 1/scale);
3883 InflateRect(textRec, -margin, -margin);
3884
3885 with TChunkedText.Create(s, fSvgReader.fFontCache) do
3886 try
3887 // and compress the lineheight a little
3888 drawPathsC := GetTextGlyphs(Rect(textRec), taLeft, tvaTop, 0, lnHeight * 0.8);
3889 finally
3890 Free;
3891 end;
3892
3893 mat := IdentityMatrix;
3894 MatrixScale(mat, scale);
3895 MatrixApply(mat, drawPathsC);
3896 end;
3897
3898 //------------------------------------------------------------------------------
3899 // TMarkerElement
3900 //------------------------------------------------------------------------------
3901
3902 constructor TMarkerElement.Create(parent: TBaseElement; svgEl: TSvgXmlEl);
3903 begin
3904 inherited;
3905 fDrawData.visible := false;
3906 end;
3907 //------------------------------------------------------------------------------
3908
3909 procedure TMarkerElement.Draw(img: TImage32; drawDat: TDrawData);
3910 var
3911 i, len: integer;
3912 l,t,w,h,scale, a, a2: double;
3913 mat: TMatrixD;
3914 angles: TArrayOfDouble;
3915 begin
3916 UpdateDrawInfo(drawDat, self);
3917 mat := drawDat.matrix;
3918
3919 if elRectWH.width.IsValid and elRectWH.height.IsValid and
3920 not markerBoxWH.IsEmpty then
3921 begin
3922 w := elRectWH.width.rawVal;
3923 h := elRectWH.height.rawVal;
3924 //currently assume preserve aspect ratio
3925 scale := Min(w/markerBoxWH.Width, h/markerBoxWH.Height);
3926 MatrixScale(mat, scale, scale);
3927 end;
3928
3929 if refPt.X.IsValid and refPt.Y.IsValid then
3930 begin
3931 l := refPt.X.rawVal;
3932 t := refPt.Y.rawVal;
3933 MatrixExtractScale(mat, scale);
3934 MatrixTranslate(mat, -l * scale, -t * scale);
3935 end;
3936
3937 len := Length(fPoints);
3938 if len = 0 then Exit;
3939 SetLength(angles, len);
3940 angles[0] := angle;
3941 a := angle;
3942 for i := 0 to len -2 do
3943 begin
3944 a2 := GetAngle(fPoints[i], fPoints[i+1]);
3945 angles[i] := Average(a, a2);
3946 a := a2;
3947 end;
3948 if len > 1 then
3949 angles[len -1] := Average(a, angle2);
3950
3951 //for each 'point' draw the marker
3952 for i := 0 to len -1 do
3953 begin
3954 drawDat.matrix := mat;
3955 MatrixRotate(drawDat.matrix, NullPointD, angles[i]);
3956 MatrixTranslate(drawDat.matrix, fPoints[i].X, fPoints[i].Y);
3957 DrawChildren(img, drawDat);
3958 end;
3959 end;
3960 //------------------------------------------------------------------------------
3961
3962 procedure TMarkerElement.SetEndPoint(const pt: TPointD; angle: double);
3963 begin
3964 NewPointDArray(fPoints, 1, True);
3965 fPoints[0] := pt;
3966 self.angle := angle;
3967 end;
3968 //------------------------------------------------------------------------------
3969
3970 function TMarkerElement.SetMiddlePoints(const points: TPathD): Boolean;
3971 var
3972 len: integer;
3973 begin
3974 len := Length(points);
3975 Result := len > 2;
3976 if Result then
3977 begin
3978 angle := GetAngle(Points[0],Points[1]);
3979 angle2 := GetAngle(Points[len-2],Points[len-1]);
3980 Self.fPoints := Copy(points, 1, len -2);
3981 end;
3982 end;
3983
3984 //------------------------------------------------------------------------------
3985 // TFillElement
3986 //------------------------------------------------------------------------------
3987
3988 function TFillElement.GetRelFracLimit: double;
3989 begin
3990 //always assume fractional values below 1 are relative
3991 Result := 1.0;
3992 end;
3993
3994 //------------------------------------------------------------------------------
3995 // TPatternElement
3996 //------------------------------------------------------------------------------
3997
3998 constructor TPatternElement.Create(parent: TBaseElement; svgEl: TSvgXmlEl);
3999 begin
4000 inherited;
4001 imgRenderer := TImageRenderer.Create;
4002 elRectWH.Init;
4003 pattBoxWH.Width := InvalidD;
4004 pattBoxWH.Height := InvalidD;
4005 fDrawData.visible := false;
4006 end;
4007 //------------------------------------------------------------------------------
4008
4009 destructor TPatternElement.Destroy;
4010 begin
4011 imgRenderer.Free;
4012 inherited;
4013 end;
4014 //------------------------------------------------------------------------------
4015
4016 function TPatternElement.PrepareRenderer(renderer: TImageRenderer;
4017 drawDat: TDrawData): Boolean;
4018 var
4019 i : integer;
4020 recWH : TRectWH;
4021 el : TBaseElement;
4022 rec : TRectD;
4023 mat : TMatrixD;
4024 sx,sy : double;
4025 scale : TPointD;
4026 begin
4027 Result := false;
4028
4029 MatrixExtractScale(drawDat.matrix, scale.X, scale.Y);
4030
4031 if units = hUserSpaceOnUse then
4032 rec := fSvgReader.userSpaceBounds else
4033 rec := drawDat.bounds;
4034
4035 //todo: implement patternUnits & patternContentUnits too
4036
4037 sx := 1; sy := 1;
4038 if elRectWH.Width.IsValid and elRectWH.Height.IsValid then
4039 begin
4040 recWH := elRectWH.GetRectWH(rec, GetRelFracLimit);
4041 if recWH.IsEmpty then Exit;
4042
4043 //also scale if necessary
4044 if not pattBoxWH.IsEmpty then
4045 begin
4046 sx := recWH.Width/pattBoxWH.Width;
4047 sy := recWH.Height/pattBoxWH.Height;
4048 end;
4049
4050 end
4051 else if not pattBoxWH.IsEmpty then
4052 begin
4053 recWH.Width := pattBoxWH.Width;
4054 recWH.Height := pattBoxWH.Width;
4055 end else
4056 Exit;
4057
4058 renderer.Image.SetSize(
4059 Round(recWH.Width * scale.X),
4060 Round(recWH.Height * scale.Y));
4061
4062 Result := true;
4063
4064 mat := IdentityMatrix;
4065 MatrixScale(mat, scale.X * sx, scale.Y * sy);
4066
4067 if (refEl <> '') then
4068 begin
4069 el := FindRefElement(refEl);
4070 if Assigned(el) and (el is TShapeElement) then
4071 with TShapeElement(el) do
4072 begin
4073 drawDat := fDrawData;
4074 drawDat.matrix := mat;
4075 drawDat.bounds := recWH.RectD;
4076 Draw(renderer.Image, drawDat);
4077 end;
4078 end;
4079
4080 for i := 0 to fChilds.Count -1 do
4081 if TBaseElement(fChilds[i]) is TShapeElement then
4082 with TShapeElement(fChilds[i]) do
4083 begin
4084 drawDat := fDrawData;
4085 drawDat.matrix := mat;
4086 drawDat.bounds := rec;
4087 Draw(renderer.Image, drawDat);
4088 end
4089 else if TBaseElement(fChilds[i]) is TImageElement then
4090 with TImageElement(fChilds[i]) do
4091 begin
4092 drawDat := fDrawData;
4093 drawDat.matrix := mat;
4094 drawDat.bounds := rec;
4095 Draw(renderer.Image, drawDat);
4096 end;
4097 end;
4098
4099 //------------------------------------------------------------------------------
4100 // TSvgElement
4101 //------------------------------------------------------------------------------
4102
4103 procedure TSvgElement.Draw(image: TImage32; drawDat: TDrawData);
4104 var
4105 sx, sy: double;
4106 dd: TDrawData;
4107 begin
4108 dd := drawDat;
4109 if (fSvgReader.RootElement <> self) then
4110 begin
4111 if (elRectWH.left.rawVal <> InvalidD) or
4112 (elRectWH.top.rawVal <> InvalidD) then
4113 begin
4114 MatrixExtractScale(dd.matrix, sx, sy);
4115 MatrixTranslate(dd.matrix,
4116 elRectWH.left.rawVal * sx,
4117 elRectWH.top.rawVal *sy);
4118 end;
4119 if not viewboxWH.IsEmpty then
4120 begin
4121 sx := fSvgReader.BackgndImage.Width / viewboxWH.Width;
4122 sy := fSvgReader.BackgndImage.Height / viewboxWH.Height;
4123 MatrixScale(dd.matrix, sx, sy);
4124 end;
4125 end;
4126 DrawChildren(image, dd);
4127 end;
4128 //------------------------------------------------------------------------------
4129
4130 function TSvgElement.Width: TValue;
4131 begin
4132 Result := elRectWH.Width;
4133 end;
4134 //------------------------------------------------------------------------------
4135
4136 function TSvgElement.Height: TValue;
4137 begin
4138 Result := elRectWH.Height;
4139 end;
4140
4141 //------------------------------------------------------------------------------
4142 // TBaseElement
4143 //------------------------------------------------------------------------------
4144
4145 constructor TBaseElement.Create(parent: TBaseElement; svgEl: TSvgXmlEl);
4146 begin
4147 {$IFDEF XPLAT_GENERICS}
4148 fChilds := TList<TBaseElement>.create;
4149 {$ELSE}
4150 fChilds := TList.Create;
4151 {$ENDIF}
4152 fXmlEl := svgEl;
4153 self.fParent := parent;
4154 elRectWH.Init;
4155 fDrawData := emptyDrawInfo;
4156 if Assigned(parent) then
4157 begin
4158 fDrawData.strokeCap := parent.fDrawData.strokeCap;
4159 fDrawData.strokeJoin := parent.fDrawData.strokeJoin;
4160 fSvgReader := parent.fSvgReader;
4161 end;
4162 end;
4163 //------------------------------------------------------------------------------
4164
4165 destructor TBaseElement.Destroy;
4166 var
4167 i: integer;
4168 begin
4169 for i := 0 to fChilds.Count -1 do
4170 TBaseElement(fChilds[i]).Free;
4171 fChilds.Free;
4172 inherited;
4173 end;
4174 //------------------------------------------------------------------------------
4175
4176 function TBaseElement.IsFirstChild: Boolean;
4177 begin
4178 Result := not Assigned(fParent) or (self = fParent.fChilds[0]);
4179 end;
4180 //------------------------------------------------------------------------------
4181
4182 procedure TBaseElement.Draw(image: TImage32; drawDat: TDrawData);
4183 begin
4184 DrawChildren(image, drawDat);
4185 end;
4186 //------------------------------------------------------------------------------
4187
4188 procedure TBaseElement.DrawChildren(image: TImage32; const drawDat: TDrawData);
4189 var
4190 i: integer;
4191 begin
4192 for i := 0 to fChilds.Count -1 do
4193 with TBaseElement(fChilds[i]) do
4194 if fDrawData.visible then Draw(image, drawDat);
4195 end;
4196 //------------------------------------------------------------------------------
4197
4198 function TBaseElement.GetChildCount: integer;
4199 begin
4200 Result := fChilds.Count;
4201 end;
4202 //------------------------------------------------------------------------------
4203
4204 function TBaseElement.FindChild(const idName: UTF8String): TBaseElement;
4205 var
4206 i: integer;
4207 begin
4208 if Match(self.fId, idName) then
4209 begin
4210 Result := self;
4211 Exit;
4212 end;
4213
4214 Result := nil;
4215 for i := 0 to ChildCount -1 do
4216 begin
4217 Result := Child[i].FindChild(idName);
4218 if Assigned(Result) then Break;
4219 end;
4220 end;
4221 //------------------------------------------------------------------------------
4222
4223 function TBaseElement.GetChild(index: integer): TBaseElement;
4224 begin
4225 if (index < 0) or (index >= fChilds.count) then
4226 Result := nil else
4227 Result := TBaseElement(fChilds[index]);
4228 end;
4229 //------------------------------------------------------------------------------
4230
4231 function TBaseElement.FindRefElement(const refname: UTF8String): TBaseElement;
4232 var
4233 len: integer;
4234 c, endC: PUTF8Char;
4235 ref: UTF8String;
4236 begin
4237 result := nil;
4238 len := Length(refname);
4239 if len = 0 then Exit;
4240 c := PUTF8Char(Pointer(refname));
4241 endC := c + len;
4242 if Match(c, 'url(') then
4243 begin
4244 inc(c, 4);
4245 dec(endC); //removes trailing ')'
4246 end;
4247 if c^ = '#' then inc(c);
4248 if c = PUTF8Char(Pointer(refname)) then
4249 Result := fSvgReader.fIdList.FindElement(refname)
4250 else
4251 begin
4252 ToUTF8String(c, endC, ref);
4253 Result := fSvgReader.fIdList.FindElement(ref);
4254 end;
4255 end;
4256
4257 //------------------------------------------------------------------------------
4258 // dozens of function to process various element attributes
4259 //------------------------------------------------------------------------------
4260
4261 procedure Id_Attrib(aOwnerEl: TBaseElement; const value: UTF8String);
4262 begin
4263 aOwnerEl.fId := value;
4264 aOwnerEl.fSvgReader.fIdList.AddOrIgnore(value, aOwnerEl);
4265 end;
4266 //------------------------------------------------------------------------------
4267
4268 procedure In_Attrib(aOwnerEl: TBaseElement; const value: UTF8String);
4269 begin
4270 if aOwnerEl is TFeBaseElement then
4271 TFeBaseElement(aOwnerEl).in1 := value;
4272 end;
4273 //------------------------------------------------------------------------------
4274
4275 procedure In2_Attrib(aOwnerEl: TBaseElement; const value: UTF8String);
4276 begin
4277 if aOwnerEl is TFeBaseElement then
4278 TFeBaseElement(aOwnerEl).in2 := value;
4279 end;
4280 //------------------------------------------------------------------------------
4281
4282 procedure Intercept_Attrib(aOwnerEl: TBaseElement; const value: UTF8String);
4283 var
4284 c, endC: PUTF8Char;
4285 val: double;
4286 begin
4287 if (value = '') or not (aOwnerEl is TFeComponentTransferChild) then Exit;
4288 c := PUTF8Char(value);
4289 endC := c + Length(value);
4290 with TFeComponentTransferChild(aOwnerEl) do
4291 if ParseNextNum(c, endC, false, val) then
4292 intercept := val else
4293 intercept := 1;
4294 end;
4295 //------------------------------------------------------------------------------
4296
4297 procedure Slope_Attrib(aOwnerEl: TBaseElement; const value: UTF8String);
4298 var
4299 c, endC: PUTF8Char;
4300 val: double;
4301 begin
4302 if (value = '') or not (aOwnerEl is TFeComponentTransferChild) then Exit;
4303 c := PUTF8Char(value);
4304 endC := c + Length(value);
4305 with TFeComponentTransferChild(aOwnerEl) do
4306 if ParseNextNum(c, endC, false, val) then
4307 slope := val else
4308 slope := 1;
4309 end;
4310 //------------------------------------------------------------------------------
4311
4312 procedure TableValues_Attrib(aOwnerEl: TBaseElement; const value: UTF8String);
4313 var
4314 c, endC: PUTF8Char;
4315 val: double;
4316 len: integer;
4317 begin
4318 if (value = '') or not (aOwnerEl is TFeComponentTransferChild) then
4319 Exit;
4320 with TFeComponentTransferChild(aOwnerEl) do
4321 begin
4322 len := 0;
4323 tableValues := nil;
4324 c := PUTF8Char(value);
4325 endC := c + Length(value);
4326 while ParseNextNum(c, endC, true, val) do
4327 begin
4328 SetLength(tableValues, len +1);
4329 tableValues[len] := val;
4330 inc(len);
4331 end;
4332 end;
4333 end;
4334 //------------------------------------------------------------------------------
4335
4336 procedure Type_Attrib(aOwnerEl: TBaseElement; const value: UTF8String);
4337 begin
4338 if (value = '') or not (aOwnerEl is TFeComponentTransferChild) then Exit;
4339 with TFeComponentTransferChild(aOwnerEl) do
4340 begin
4341 case value[1] of
4342 'D','d': funcType := ftDiscrete;
4343 'G','g': funcType := ftGamma;
4344 'L','l': funcType := ftLinear;
4345 'T','t': funcType := ftTable;
4346 else funcType := ftIdentity;
4347 end;
4348 end;
4349 end;
4350 //------------------------------------------------------------------------------
4351
4352 procedure LetterSpacing_Attrib(aOwnerEl: TBaseElement; const value: UTF8String);
4353 begin
4354 with aOwnerEl do
4355 UTF8StringToFloat(value, fDrawData.FontInfo.spacing);
4356 end;
4357 //------------------------------------------------------------------------------
4358
4359 procedure Href_Attrib(aOwnerEl: TBaseElement; const value: UTF8String);
4360 begin
4361 case aOwnerEl.fXmlEl.Hash of
4362 hFeImage:
4363 TFeImageElement(aOwnerEl).refEl := ExtractRef(value);
4364 hImage:
4365 TImageElement(aOwnerEl).fRefEl := ExtractRef(value);
4366 hUse:
4367 TUseElement(aOwnerEl).fRefEl := ExtractRef(value);
4368 hTextPath:
4369 TTextPathElement(aOwnerEl).pathName := ExtractRef(value);
4370 else if aOwnerEl is TFillElement then
4371 TFillElement(aOwnerEl).refEl := ExtractRef(value);
4372 end;
4373 end;
4374 //------------------------------------------------------------------------------
4375
4376 procedure Space_Attrib(aOwnerEl: TBaseElement; const value: UTF8String);
4377 begin
4378 case aOwnerEl.fXmlEl.Hash of
4379 hText: if value = 'preserve' then
4380 TTextPathElement(aOwnerEl).fDrawData.fontInfo.spacesInText := sitPreserve;
4381 end;
4382 end;
4383 //------------------------------------------------------------------------------
4384
4385
4386 procedure BaselineShift_Attrib(aOwnerEl: TBaseElement; const value: UTF8String);
4387 var
4388 mu: TUnitType;
4389 val: double;
4390 hash: cardinal;
4391 c, endC: PUTF8Char;
4392 begin
4393 c := PUTF8Char(value);
4394 endC := c + Length(value);
4395 hash := ParseNextWordHash(c, endC);
4396 with aOwnerEl.fDrawData.FontInfo do
4397 case hash of
4398 hSuper: baseShift.SetValue(50, utPercent);
4399 hSub: baseShift.SetValue(-50, utPercent);
4400 hBaseline: baseShift.SetValue(0, utPixel);
4401 else
4402 begin
4403 UTF8StringToFloatEx(value, val, mu);
4404 baseShift.SetValue(val, mu);
4405 end;
4406 end;
4407 end;
4408 //------------------------------------------------------------------------------
4409
4410 procedure Color_Attrib(aOwnerEl: TBaseElement; const value: UTF8String);
4411 var
4412 color: TColor32;
4413 begin
4414 color := clInvalid;
4415 UTF8StringToColor32(value, color);
4416 //for setting currentcolor when drawing (eg drawing shapes)
4417 aOwnerEl.fDrawData.currentColor := color;
4418 //for setting currentcolor during element creation (eg gradient colors)
4419 aOwnerEl.fSvgReader.currentColor := color;
4420 end;
4421 //------------------------------------------------------------------------------
4422
4423 procedure LightingColor_Attrib(aOwnerEl: TBaseElement; const value: UTF8String);
4424 var
4425 color: TColor32;
4426 begin
4427 color := clInvalid;
4428 UTF8StringToColor32(value, color);
4429 if (aOwnerEl is TFeSpecLightElement) then
4430 TFeSpecLightElement(aOwnerEl).color := color
4431 else if (aOwnerEl is TFeDefuseLightElement) then
4432 TFeDefuseLightElement(aOwnerEl).color := color
4433 end;
4434 //------------------------------------------------------------------------------
4435
4436 procedure ClipPath_Attrib(aOwnerEl: TBaseElement; const value: UTF8String);
4437 begin
4438 aOwnerEl.fDrawData.clipElRef := ExtractRef(value);
4439 end;
4440 //------------------------------------------------------------------------------
4441
4442 procedure D_Attrib(aOwnerEl: TBaseElement; const value: UTF8String);
4443 begin
4444 if aOwnerEl is TPathElement then
4445 TPathElement(aOwnerEl).ParseDAttrib(value);
4446 end;
4447 //------------------------------------------------------------------------------
4448
4449 procedure Fill_Attrib(aOwnerEl: TBaseElement; const value: UTF8String);
4450 begin
4451 case aOwnerEl.fXmlEl.Hash of
4452 hfeDropShadow:
4453 UTF8StringToColor32(value, TFeDropShadowElement(aOwnerEl).floodColor);
4454 hfeFlood:
4455 UTF8StringToColor32(value, TFeFloodElement(aOwnerEl).floodColor);
4456 else
4457 begin
4458 if Match(PUTF8Char(value), 'url(') then
4459 aOwnerEl.fDrawData.fillEl := ExtractRef(value)
4460 else
4461 UTF8StringToColor32(value, aOwnerEl.fDrawData.fillColor);
4462 end;
4463 end;
4464 end;
4465 //------------------------------------------------------------------------------
4466
4467 procedure FillOpacity_Attrib(aOwnerEl: TBaseElement; const value: UTF8String);
4468 var
4469 opacity: double;
4470 begin
4471 case aOwnerEl.fXmlEl.Hash of
4472 hfeDropShadow:
4473 UTF8StringToOpacity(value, TFeDropShadowElement(aOwnerEl).floodColor);
4474 hfeFlood:
4475 UTF8StringToOpacity(value, TFeFloodElement(aOwnerEl).floodColor);
4476 else
4477 begin
4478 UTF8StringToFloat(value, opacity);
4479 with aOwnerEl.fDrawData do
4480 begin
4481 if IsValid(fillOpacity) then
4482 fillOpacity := fillOpacity * opacity else
4483 fillOpacity := opacity;
4484 end;
4485 end;
4486 end;
4487 end;
4488 //------------------------------------------------------------------------------
4489
4490 procedure DashArray_Attrib(aOwnerEl: TBaseElement; const value: UTF8String);
4491 var
4492 c, endC: PUTF8Char;
4493 val: double;
4494 len: integer;
4495 begin
4496 c := PUTF8Char(value);
4497 endC := c + Length(value);
4498 with aOwnerEl.fDrawData do
4499 begin
4500 len := Length(dashArray);
4501 while ParseNextNum(c, endC, true, val) do
4502 begin
4503 SetLength(dashArray, len +1);
4504 dashArray[len] := val;
4505 inc(len);
4506 end;
4507 end;
4508 end;
4509 //------------------------------------------------------------------------------
4510
4511 procedure DashOffset_Attrib(aOwnerEl: TBaseElement; const value: UTF8String);
4512 var
4513 c, endC: PUTF8Char;
4514 begin
4515 c := PUTF8Char(value);
4516 endC := c + Length(value);
4517 with aOwnerEl.fDrawData do
4518 ParseNextNum(c, endC, true, dashOffset);
4519 end;
4520 //------------------------------------------------------------------------------
4521
4522 procedure Display_Attrib(aOwnerEl: TBaseElement; const value: UTF8String);
4523 begin
4524 if GetHash(value) = hNone then
4525 aOwnerEl.fDrawData.visible := false;
4526 end;
4527 //------------------------------------------------------------------------------
4528
4529 procedure Font_Attrib(aOwnerEl: TBaseElement; const value: UTF8String);
4530 begin
4531 GetSvgFontInfo(value, aOwnerEl.fDrawData.FontInfo);
4532 end;
4533 //------------------------------------------------------------------------------
4534
4535 procedure FontFamily_Attrib(aOwnerEl: TBaseElement; const value: UTF8String);
4536 var
4537 hash: cardinal;
4538 c, endC: PUTF8Char;
4539 begin
4540 with aOwnerEl.fDrawData.FontInfo do
4541 begin
4542 family := tfUnknown;
4543 familyNames := GetCommaSeparatedArray(value);
4544 // get comma separated family names
4545
4546 c := PUTF8Char(value);
4547 endC := c + Length(value);
4548 while ParseNextWordExHash(c, endC, hash) do
4549 begin
4550 case hash of
4551 hSans_045_Serif, hArial : family := tfSansSerif;
4552 hSerif, hTimes: family := tfSerif;
4553 hMonospace: family := tfMonospace;
4554 else Continue;
4555 end;
4556 break;
4557 end;
4558 end;
4559 end;
4560 //------------------------------------------------------------------------------
4561
4562 procedure FontSize_Attrib(aOwnerEl: TBaseElement; const value: UTF8String);
4563 var
4564 num: double;
4565 c, endC: PUTF8Char;
4566 begin
4567 c := PUTF8Char(value); endC := c + Length(value);
4568 if not ParseNextNum(c, endC, false, num) then Exit;
4569 aOwnerEl.fDrawData.FontInfo.size := num;
4570 end;
4571 //------------------------------------------------------------------------------
4572
4573 procedure FontStyle_Attrib(aOwnerEl: TBaseElement; const value: UTF8String);
4574 begin
4575 with aOwnerEl.fDrawData.FontInfo do
4576 if GetHash(value) = hItalic then
4577 italic := sfsItalic else
4578 italic := sfsNone;
4579 end;
4580 //------------------------------------------------------------------------------
4581
4582 procedure FontWeight_Attrib(aOwnerEl: TBaseElement; const value: UTF8String);
4583 var
4584 num: double;
4585 hash: cardinal;
4586 c, endC: PUTF8Char;
4587 begin
4588 c := PUTF8Char(value);
4589 endC := c + Length(value);
4590 with aOwnerEl.fDrawData.FontInfo do
4591 begin
4592 if IsNumPending(c, endC, false) and
4593 ParseNextNum(c, endC, false, num) then
4594 weight := Round(num)
4595 else if ParseNextWordHash(c, endC, hash) then
4596 case hash of
4597 hBold : weight := 600;
4598 hNormal : weight := 400;
4599 hBolder : if weight >= 0 then weight := Min(900, weight + 200)
4600 else weight := 600;
4601 hLighter: if weight >= 0 then weight := Max(0, weight - 200)
4602 else weight := 200;
4603
4604 end;
4605 end;
4606 end;
4607 //------------------------------------------------------------------------------
4608
4609 procedure Fx_Attrib(aOwnerEl: TBaseElement; const value: UTF8String);
4610 begin
4611 if (aOwnerEl is TRadGradElement) then
4612 with TRadGradElement(aOwnerEl) do
4613 begin
4614 UTF8StringToFloatEx(value, F.X.rawVal, F.X.unitType);
4615 end;
4616 end;
4617 //------------------------------------------------------------------------------
4618
4619 procedure Fy_Attrib(aOwnerEl: TBaseElement; const value: UTF8String);
4620 begin
4621 if (aOwnerEl is TRadGradElement) then
4622 with TRadGradElement(aOwnerEl) do
4623 begin
4624 UTF8StringToFloatEx(value, F.Y.rawVal, F.Y.unitType);
4625 end;
4626 end;
4627 //------------------------------------------------------------------------------
4628
4629 procedure TextAlign_Attrib(aOwnerEl: TBaseElement; const value: UTF8String);
4630 begin
4631 with aOwnerEl.fDrawData.FontInfo do
4632 case GetHash(value) of
4633 hMiddle : align := staCenter;
4634 hEnd : align := staRight;
4635 hJustify : align := staJustify;
4636 else align := staLeft;
4637 end;
4638 end;
4639 //------------------------------------------------------------------------------
4640
4641 procedure TextDecoration_Attrib(aOwnerEl: TBaseElement; const value: UTF8String);
4642 begin
4643 with aOwnerEl.fDrawData.FontInfo do
4644 if PosEx('underline', value) > 0 then
4645 decoration := fdUnderline
4646 else if PosEx('line-through', value) > 0 then
4647 decoration := fdStrikeThrough
4648 else
4649 decoration := fdNone;
4650 end;
4651 //------------------------------------------------------------------------------
4652
4653 procedure TextLength_Attrib(aOwnerEl: TBaseElement; const value: UTF8String);
4654 begin
4655 UTF8StringToFloat(value, aOwnerEl.fDrawData.FontInfo.textLength);
4656 end;
4657 //------------------------------------------------------------------------------
4658
4659
4660 procedure MarkerStart_Attrib(aOwnerEl: TBaseElement; const value: UTF8String);
4661 begin
4662 if not (aOwnerEl is TShapeElement) then Exit;
4663 aOwnerEl.fDrawData.markerStart := ExtractRef(value);
4664 end;
4665 //------------------------------------------------------------------------------
4666
4667 procedure MarkerMiddle_Attrib(aOwnerEl: TBaseElement; const value: UTF8String);
4668 begin
4669 if not (aOwnerEl is TShapeElement) then Exit;
4670 aOwnerEl.fDrawData.markerMiddle := ExtractRef(value);
4671 end;
4672 //------------------------------------------------------------------------------
4673
4674 procedure MarkerEnd_Attrib(aOwnerEl: TBaseElement; const value: UTF8String);
4675 begin
4676 if not (aOwnerEl is TShapeElement) then Exit;
4677 aOwnerEl.fDrawData.markerEnd := ExtractRef(value);
4678 end;
4679 //------------------------------------------------------------------------------
4680
4681 procedure Filter_Attrib(aOwnerEl: TBaseElement; const value: UTF8String);
4682 begin
4683 if (aOwnerEl is TShapeElement) then
4684 aOwnerEl.fDrawData.filterElRef := ExtractRef(value);
4685 end;
4686 //------------------------------------------------------------------------------
4687
4688 procedure Mask_Attrib(aOwnerEl: TBaseElement; const value: UTF8String);
4689 begin
4690 if (aOwnerEl is TShapeElement) then
4691 aOwnerEl.fDrawData.maskElRef := ExtractRef(value);
4692 end;
4693 //------------------------------------------------------------------------------
4694
4695 procedure Offset_Attrib(aOwnerEl: TBaseElement; const value: UTF8String);
4696 var
4697 val: TValue;
4698 begin
4699 if (aOwnerEl is TGradStopElement) then
4700 with TGradStopElement(aOwnerEl) do
4701 begin
4702 val.Init;
4703 UTF8StringToFloatEx(value, val.rawVal, val.unitType);
4704 offset := val.GetValue(1, GetRelFracLimit);
4705 end
4706 end;
4707 //------------------------------------------------------------------------------
4708
4709 procedure Opacity_Attrib(aOwnerEl: TBaseElement; const value: UTF8String);
4710 var
4711 opacity: double;
4712 begin
4713 if not UTF8StringToFloat(value, opacity) then Exit;
4714 opacity := ClampRange(opacity, 0,1);
4715 with aOwnerEl.fDrawData do
4716 begin
4717 if IsValid(fillOpacity) then
4718 fillOpacity := fillOpacity * opacity else
4719 fillOpacity := opacity;
4720 if IsValid(strokeOpacity) then
4721 strokeOpacity := strokeOpacity * opacity else
4722 strokeOpacity := opacity;
4723 end;
4724 end;
4725 //------------------------------------------------------------------------------
4726
4727 procedure Operator_Attrib(aOwnerEl: TBaseElement; const value: UTF8String);
4728 begin
4729 if (aOwnerEl is TFeCompositeElement) then
4730 with TFeCompositeElement(aOwnerEl) do
4731 case GetHash(value) of
4732 hAtop : compositeOp := coAtop;
4733 hIn : compositeOp := coIn;
4734 hOut : compositeOp := coOut;
4735 hOver : compositeOp := coOver;
4736 hXor : compositeOp := coXor;
4737 hArithmetic : compositeOp := coArithmetic;
4738 end;
4739 end;
4740 //------------------------------------------------------------------------------
4741
4742 procedure Orient_Attrib(aOwnerEl: TBaseElement; const value: UTF8String);
4743 begin
4744 if (aOwnerEl is TMarkerElement) and
4745 (GetHash(value) = hauto_045_start_045_reverse) then
4746 TMarkerElement(aOwnerEl).autoStartReverse := true;
4747 end;
4748 //------------------------------------------------------------------------------
4749
4750 procedure StartOffset_Attrib(aOwnerEl: TBaseElement; const value: UTF8String);
4751 var
4752 val: double;
4753 begin
4754 if (aOwnerEl is TTextPathElement) and
4755 UTF8StringToFloat(value, val) then
4756 TTextPathElement(aOwnerEl).offset.X.SetValue(val);
4757 end;
4758 //------------------------------------------------------------------------------
4759
4760 procedure StopColor_Attrib(aOwnerEl: TBaseElement; const value: UTF8String);
4761 var
4762 acolor: TColor32;
4763 begin
4764 if aOwnerEl is TGradStopElement then
4765 begin
4766 acolor := clInvalid;
4767 UTF8StringToColor32(value, acolor);
4768 with TGradStopElement(aOwnerEl) do
4769 if acolor = clCurrent then
4770 color := aOwnerEl.fSvgReader.currentColor else
4771 color := acolor;
4772 end;
4773 end;
4774 //------------------------------------------------------------------------------
4775
4776 procedure StopOpacity_Attrib(aOwnerEl: TBaseElement; const value: UTF8String);
4777 begin
4778 if aOwnerEl is TGradStopElement then
4779 UTF8StringToOpacity(value, TGradStopElement(aOwnerEl).color);
4780 end;
4781 //------------------------------------------------------------------------------
4782
4783 procedure Points_Attrib(aOwnerEl: TBaseElement; const value: UTF8String);
4784 begin
4785 if aOwnerEl is TPolyElement then
4786 TPolyElement(aOwnerEl).ParsePoints(value);
4787 end;
4788 //------------------------------------------------------------------------------
4789
4790 procedure Stroke_Attrib(aOwnerEl: TBaseElement; const value: UTF8String);
4791 begin
4792 if Match(PUTF8Char(value), 'url(') then
4793 aOwnerEl.fDrawData.strokeEl := ExtractRef(value)
4794 else if Match(PUTF8Char(value), 'currentcolor') then
4795 aOwnerEl.fDrawData.strokeColor := clCurrent
4796 else
4797 UTF8StringToColor32(value, aOwnerEl.fDrawData.strokeColor);
4798 end;
4799 //------------------------------------------------------------------------------
4800
4801 procedure StrokeLineCap_Attrib(aOwnerEl: TBaseElement; const value: UTF8String);
4802 var
4803 hash: cardinal;
4804 c, endC: PUTF8Char;
4805 begin
4806 c := PUTF8Char(value);
4807 endC := c + Length(value);
4808 hash := ParseNextWordHash(c, endC);
4809 with aOwnerEl.fDrawData do
4810 case hash of
4811 hButt : strokeCap := esButt;
4812 hRound : strokeCap := esRound;
4813 hSquare : strokeCap := esSquare;
4814 end;
4815 end;
4816 //------------------------------------------------------------------------------
4817
4818 procedure StrokeLineJoin_Attrib(aOwnerEl: TBaseElement; const value: UTF8String);
4819 var
4820 hash: cardinal;
4821 c, endC: PUTF8Char;
4822 begin
4823 c := PUTF8Char(value);
4824 endC := c + Length(value);
4825 hash := ParseNextWordHash(c, endC);
4826 with aOwnerEl.fDrawData do
4827 case hash of
4828 hMiter : strokeJoin := jsMiter;
4829 hRound : strokeJoin := jsRound;
4830 hBevel : strokeJoin := jsSquare;
4831 end;
4832 end;
4833 //------------------------------------------------------------------------------
4834
4835 procedure StrokeMiterLimit_Attrib(aOwnerEl: TBaseElement; const value: UTF8String);
4836 begin
4837 UTF8StringToFloat(value, aOwnerEl.fDrawData.strokeMitLim);
4838 end;
4839 //------------------------------------------------------------------------------
4840
4841 procedure StrokeOpacity_Attrib(aOwnerEl: TBaseElement; const value: UTF8String);
4842 var
4843 opacity: double;
4844 begin
4845 UTF8StringToFloat(value, opacity);
4846 opacity := ClampRange(opacity, 0,1);
4847 with aOwnerEl.fDrawData do
4848 begin
4849 if IsValid(strokeOpacity) then
4850 strokeOpacity := strokeOpacity * opacity else
4851 strokeOpacity := opacity;
4852 end;
4853 end;
4854 //------------------------------------------------------------------------------
4855
4856 procedure StrokeWidth_Attrib(aOwnerEl: TBaseElement; const value: UTF8String);
4857 begin
4858 with aOwnerEl do
4859 begin
4860 UTF8StringToFloatEx(value, fDrawData.strokewidth.rawVal,
4861 fDrawData.strokewidth.unitType);
4862 end;
4863 end;
4864 //------------------------------------------------------------------------------
4865
4866 procedure FillRule_Attrib(aOwnerEl: TBaseElement; const value: UTF8String);
4867 begin
4868 if LowerCaseTable[value[1]] = 'e' then
4869 aOwnerEl.fDrawData.fillRule := frEvenOdd else
4870 aOwnerEl.fDrawData.fillRule := frNonZero;
4871 end;
4872 //------------------------------------------------------------------------------
4873
4874 procedure Rotate_Attrib(aOwnerEl: TBaseElement; const value: UTF8String);
4875 var
4876 i, cnt: integer;
4877 angle: TArrayOfDouble;
4878 c, endC: PUTF8Char;
4879 begin
4880 SetLength(angle, Length(value));
4881 c := PUTF8Char(value);
4882 endC := c + Length(value);
4883 cnt := 0;
4884 while ParseNextNum(c, endC, true, angle[cnt]) do inc(cnt);
4885 SetLength(angle, cnt);
4886
4887 for i := 0 to cnt -1 do
4888 angle[i] := DegToRad(angle[i]);
4889
4890 if aOwnerEl is TTextElement then
4891 TTextElement(aOwnerEl).angle := angle
4892 else if aOwnerEl is TTSpanElement then
4893 TTSpanElement(aOwnerEl).angle := angle;
4894 end;
4895 //------------------------------------------------------------------------------
4896
4897 procedure Transform_Attrib(aOwnerEl: TBaseElement; const value: UTF8String);
4898 begin
4899 with aOwnerEl.fDrawData do
4900 MatrixMultiply2(ParseTransform(value), matrix);
4901 end;
4902 //------------------------------------------------------------------------------
4903
4904 procedure Values_Attrib(aOwnerEl: TBaseElement; const value: UTF8String);
4905 var
4906 cnt: integer;
4907 c, endC: PUTF8Char;
4908 begin
4909 if aOwnerEl is TFeColorMatrixElement then
4910 with TFeColorMatrixElement(aOwnerEl) do
4911 begin
4912 SetLength(values, 20);
4913 c := PUTF8Char(value);
4914 endC := c + Length(value);
4915 cnt := 0;
4916 while (cnt < 20) and ParseNextNum(c, endC, true, values[cnt]) do
4917 inc(cnt);
4918 if cnt < 20 then values := nil;
4919 end;
4920 end;
4921 //------------------------------------------------------------------------------
4922
4923 procedure GradientTransform_Attrib(aOwnerEl: TBaseElement; const value: UTF8String);
4924 var
4925 mat: TMatrixD;
4926 begin
4927 if not (aOwnerEl is TGradientElement) then Exit;
4928 mat := ParseTransform(value);
4929 with aOwnerEl.fDrawData do
4930 MatrixMultiply2(mat, matrix);
4931 end;
4932 //------------------------------------------------------------------------------
4933
4934 procedure GradientUnits_Attrib(aOwnerEl: TBaseElement; const value: UTF8String);
4935 begin
4936 if aOwnerEl is TFillElement then
4937 with TFillElement(aOwnerEl) do
4938 units := GetHash(value);
4939 end;
4940 //------------------------------------------------------------------------------
4941
4942 procedure Viewbox_Attrib(aOwnerEl: TBaseElement; const value: UTF8String);
4943
4944 function LoadViewbox: TRectWH;
4945 var
4946 c, endC: PUTF8Char;
4947 begin
4948 c := PUTF8Char(value);
4949 endC := c + Length(value);
4950 with Result do
4951 if not ParseNextNum(c, endC, false, Left) or
4952 not ParseNextNum(c, endC, true, Top) or
4953 not ParseNextNum(c, endC, true, Width) or
4954 not ParseNextNum(c, endC, true, Height) then
4955 Result := RectWH(0,0,0,0);
4956 end;
4957
4958 begin
4959 case aOwnerEl.fXmlEl.Hash of
4960 hSvg : TSvgElement(aOwnerEl).viewboxWH := LoadViewbox;
4961 hMarker : TMarkerElement(aOwnerEl).markerBoxWH := LoadViewbox;
4962 hSymbol : TSymbolElement(aOwnerEl).viewboxWH := LoadViewbox;
4963 else if aOwnerEl is TPatternElement then
4964 TPatternElement(aOwnerEl).pattBoxWH := LoadViewbox;
4965 end;
4966 end;
4967 //------------------------------------------------------------------------------
4968
4969 procedure Visibility_Attrib(aOwnerEl: TBaseElement; const value: UTF8String);
4970 begin
4971 case GetHash(value) of
4972 hCollapse: aOwnerEl.fDrawData.visible := false;
4973 hHidden: aOwnerEl.fDrawData.visible := false;
4974 hVisible: aOwnerEl.fDrawData.visible := true;
4975 end;
4976 end;
4977 //------------------------------------------------------------------------------
4978
4979
4980 procedure Height_Attrib(aOwnerEl: TBaseElement; const value: UTF8String);
4981 var
4982 mu: TUnitType;
4983 val: double;
4984 begin
4985 UTF8StringToFloatEx(value, val, mu);
4986 with aOwnerEl do
4987 begin
4988 elRectWH.height.SetValue(val, mu);
4989 end;
4990 end;
4991 //------------------------------------------------------------------------------
4992
4993 procedure Width_Attrib(aOwnerEl: TBaseElement; const value: UTF8String);
4994 var
4995 mu: TUnitType;
4996 val: double;
4997 begin
4998 UTF8StringToFloatEx(value, val, mu);
4999 with aOwnerEl do
5000 begin
5001 elRectWH.width.SetValue(val, mu);
5002 end;
5003 end;
5004 //------------------------------------------------------------------------------
5005
5006 procedure Cx_Attrib(aOwnerEl: TBaseElement; const value: UTF8String);
5007 var
5008 mu: TUnitType;
5009 val: double;
5010 begin
5011 UTF8StringToFloatEx(value, val, mu);
5012 case aOwnerEl.fXmlEl.Hash of
5013 hCircle:
5014 with TCircleElement(aOwnerEl) do centerPt.X.SetValue(val, mu);
5015 hEllipse:
5016 with TEllipseElement(aOwnerEl) do centerPt.X.SetValue(val, mu);
5017 hRadialGradient:
5018 with TRadGradElement(aOwnerEl) do
5019 begin
5020 C.X.SetValue(val, mu);
5021 end;
5022 end;
5023 end;
5024 //------------------------------------------------------------------------------
5025
5026 procedure Cy_Attrib(aOwnerEl: TBaseElement; const value: UTF8String);
5027 var
5028 mu: TUnitType;
5029 val: double;
5030 begin
5031 UTF8StringToFloatEx(value, val, mu);
5032 case aOwnerEl.fXmlEl.Hash of
5033 hCircle:
5034 with TCircleElement(aOwnerEl) do centerPt.Y.SetValue(val, mu);
5035 hEllipse:
5036 with TEllipseElement(aOwnerEl) do centerPt.Y.SetValue(val, mu);
5037 hRadialGradient:
5038 with TRadGradElement(aOwnerEl) do
5039 begin
5040 C.Y.SetValue(val, mu);
5041 end;
5042 end;
5043 end;
5044 //------------------------------------------------------------------------------
5045
5046 procedure Dx_Attrib(aOwnerEl: TBaseElement; const value: UTF8String);
5047 var
5048 mu: TUnitType;
5049 val: double;
5050 begin
5051 UTF8StringToFloatEx(value, val, mu);
5052 case aOwnerEl.fXmlEl.Hash of
5053 hfeDropShadow:
5054 TFeDropShadowElement(aOwnerEl).offset.X.SetValue(val, mu);
5055 hfeOffset:
5056 TFeOffsetElement(aOwnerEl).offset.X.SetValue(val, mu);
5057 hText:
5058 TTextElement(aOwnerEl).offset.X.SetValue(val, mu);
5059 hTSpan:
5060 TTSpanElement(aOwnerEl).offset.X.SetValue(val, mu);
5061 hTextPath:
5062 TTextPathElement(aOwnerEl).offset.X.SetValue(val, mu);
5063 end;
5064 end;
5065 //------------------------------------------------------------------------------
5066
5067 procedure Dy_Attrib(aOwnerEl: TBaseElement; const value: UTF8String);
5068 var
5069 mu: TUnitType;
5070 val: double;
5071 begin
5072 UTF8StringToFloatEx(value, val, mu);
5073 case aOwnerEl.fXmlEl.Hash of
5074 hfeDropShadow:
5075 TFeDropShadowElement(aOwnerEl).offset.Y.SetValue(val, mu);
5076 hfeOffset:
5077 TFeOffsetElement(aOwnerEl).offset.Y.SetValue(val, mu);
5078 hText:
5079 TTextElement(aOwnerEl).offset.Y.SetValue(val, mu);
5080 hTSpan:
5081 TTSpanElement(aOwnerEl).offset.Y.SetValue(val, mu);
5082 hTextPath:
5083 TTextPathElement(aOwnerEl).offset.Y.SetValue(val, mu);
5084 end;
5085 end;
5086 //------------------------------------------------------------------------------
5087
5088 procedure Result_Attrib(aOwnerEl: TBaseElement; const value: UTF8String);
5089 begin
5090 if (aOwnerEl is TFeBaseElement) then
5091 TFeBaseElement(aOwnerEl).res := ExtractRef(value);
5092 end;
5093 //------------------------------------------------------------------------------
5094
5095 procedure Rx_Attrib(aOwnerEl: TBaseElement; const value: UTF8String);
5096 var
5097 mu: TUnitType;
5098 val: double;
5099 begin
5100 UTF8StringToFloatEx(value, val, mu);
5101 case aOwnerEl.fXmlEl.Hash of
5102 hRect:
5103 with TRectElement(aOwnerEl) do
5104 begin
5105 radius.X.SetValue(val, mu);
5106 end;
5107 hCircle:
5108 with TCircleElement(aOwnerEl) do
5109 begin
5110 radius.SetValue(val, mu);
5111 end;
5112 hEllipse:
5113 with TEllipseElement(aOwnerEl) do
5114 begin
5115 radius.X.SetValue(val, mu);
5116 end;
5117 hRadialGradient:
5118 with TRadGradElement(aOwnerEl) do
5119 begin
5120 radius.X. SetValue(val, mu);
5121 radius.Y. SetValue(val, mu);
5122 end;
5123 hMarker:
5124 with TMarkerElement(aOwnerEl) do
5125 refPt.X.SetValue(val, mu);
5126 end;
5127 end;
5128 //------------------------------------------------------------------------------
5129
5130 procedure Ry_Attrib(aOwnerEl: TBaseElement; const value: UTF8String);
5131 var
5132 mu: TUnitType;
5133 val: double;
5134 begin
5135 UTF8StringToFloatEx(value, val, mu);
5136 case aOwnerEl.fXmlEl.Hash of
5137 hRect:
5138 with TRectElement(aOwnerEl) do
5139 begin
5140 radius.Y.SetValue(val, mu);
5141 end;
5142 hEllipse:
5143 with TEllipseElement(aOwnerEl) do
5144 begin
5145 radius.Y.SetValue(val, mu);
5146 end;
5147 hMarker:
5148 with TMarkerElement(aOwnerEl) do refPt.Y.SetValue(val, mu);
5149 end;
5150 end;
5151 //------------------------------------------------------------------------------
5152
5153 procedure SpreadMethod_Attrib(aOwnerEl: TBaseElement; const value: UTF8String);
5154 var
5155 hash: cardinal;
5156 c, endC: PUTF8Char;
5157 begin
5158 if not (aOwnerEl is TGradientElement) then Exit;
5159 c := PUTF8Char(value);
5160 endC := c + Length(value);
5161 hash := ParseNextWordHash(c, endC);
5162 with TGradientElement(aOwnerEl) do
5163 case hash of
5164 hPad : spreadMethod := gfsClamp;
5165 hReflect : spreadMethod := gfsMirror;
5166 hRepeat : spreadMethod := gfsRepeat;
5167 end;
5168 end;
5169 //------------------------------------------------------------------------------
5170
5171 procedure SpectacularExponent(aOwnerEl: TBaseElement; const value: UTF8String);
5172 var
5173 se: double;
5174 begin
5175 if not (aOwnerEl is TFeSpecLightElement) then Exit;
5176 UTF8StringToFloat(value, se);
5177 if (se > 0) and (se < 100) then
5178 TFeSpecLightElement(aOwnerEl).exponent := se;
5179 end;
5180 //------------------------------------------------------------------------------
5181
5182 procedure StdDev_Attrib(aOwnerEl: TBaseElement; const value: UTF8String);
5183 var
5184 sd: double;
5185 begin
5186 UTF8StringToFloat(value, sd);
5187 if (sd < 0) and (sd > 100) then Exit;
5188 case aOwnerEl.fXmlEl.Hash of
5189 hfeGaussianBlur:
5190 TFeGaussElement(aOwnerEl).stdDev := sd;
5191 hfeDropShadow:
5192 TFeDropShadowElement(aOwnerEl).stdDev := sd;
5193 end;
5194 end;
5195 //------------------------------------------------------------------------------
5196
5197 procedure K1_Attrib(aOwnerEl: TBaseElement; const value: UTF8String);
5198 var
5199 val: double;
5200 begin
5201 UTF8StringToFloat(value, val);
5202 if aOwnerEl is TFeCompositeElement then
5203 TFeCompositeElement(aOwnerEl).fourKs[0] := val;
5204 end;
5205 //------------------------------------------------------------------------------
5206
5207 procedure K2_Attrib(aOwnerEl: TBaseElement; const value: UTF8String);
5208 var
5209 val: double;
5210 begin
5211 UTF8StringToFloat(value, val);
5212 if aOwnerEl is TFeCompositeElement then
5213 TFeCompositeElement(aOwnerEl).fourKs[1] := val;
5214 end;
5215 //------------------------------------------------------------------------------
5216
5217 procedure K3_Attrib(aOwnerEl: TBaseElement; const value: UTF8String);
5218 var
5219 val: double;
5220 begin
5221 UTF8StringToFloat(value, val);
5222 if aOwnerEl is TFeCompositeElement then
5223 TFeCompositeElement(aOwnerEl).fourKs[2] := val;
5224 end;
5225 //------------------------------------------------------------------------------
5226
5227 procedure K4_Attrib(aOwnerEl: TBaseElement; const value: UTF8String);
5228 var
5229 val: double;
5230 begin
5231 UTF8StringToFloat(value, val);
5232 if aOwnerEl is TFeCompositeElement then
5233 TFeCompositeElement(aOwnerEl).fourKs[3] := val;
5234 end;
5235 //------------------------------------------------------------------------------
5236
5237 procedure X1_Attrib(aOwnerEl: TBaseElement; const value: UTF8String);
5238 var
5239 mu: TUnitType;
5240 val: double;
5241 begin
5242 UTF8StringToFloatEx(value, val, mu);
5243 case aOwnerEl.fXmlEl.Hash of
5244 hLine:
5245 TLineElement(aOwnerEl).path[0].X := val;
5246 hLinearGradient:
5247 with TLinGradElement(aOwnerEl) do
5248 begin
5249 startPt.X.SetValue(val, mu);
5250 end;
5251 hFilter:
5252 with aOwnerEl do
5253 begin
5254 elRectWH.left.SetValue(val, mu);
5255 end;
5256 else
5257 aOwnerEl.elRectWH.left.SetValue(val, mu);
5258 end;
5259 end;
5260 //------------------------------------------------------------------------------
5261
5262 procedure X2_Attrib(aOwnerEl: TBaseElement; const value: UTF8String);
5263 var
5264 mu: TUnitType;
5265 val: double;
5266 begin
5267 UTF8StringToFloatEx(value, val, mu);
5268 case aOwnerEl.fXmlEl.Hash of
5269 hLine:
5270 TLineElement(aOwnerEl).path[1].X := val;
5271 hLinearGradient:
5272 with TLinGradElement(aOwnerEl) do
5273 begin
5274 endPt.X.SetValue(val, mu);
5275 end;
5276 end;
5277 end;
5278 //------------------------------------------------------------------------------
5279
5280 procedure Y1_Attrib(aOwnerEl: TBaseElement; const value: UTF8String);
5281 var
5282 mu: TUnitType;
5283 val: double;
5284 begin
5285 UTF8StringToFloatEx(value, val, mu);
5286 case aOwnerEl.fXmlEl.Hash of
5287 hLine:
5288 TLineElement(aOwnerEl).path[0].Y := val;
5289 hLinearGradient:
5290 with TLinGradElement(aOwnerEl) do
5291 begin
5292 startPt.Y.SetValue(val, mu);
5293 end;
5294 hFilter:
5295 with aOwnerEl do
5296 begin
5297 elRectWH.top.SetValue(val, mu);
5298 end;
5299 else
5300 aOwnerEl.elRectWH.top.SetValue(val, mu);
5301 end;
5302 end;
5303 //------------------------------------------------------------------------------
5304
5305 procedure Y2_Attrib(aOwnerEl: TBaseElement; const value: UTF8String);
5306 var
5307 mu: TUnitType;
5308 val: double;
5309 begin
5310 UTF8StringToFloatEx(value, val, mu);
5311 case aOwnerEl.fXmlEl.Hash of
5312 hLine:
5313 TLineElement(aOwnerEl).path[1].Y := val;
5314 hLinearGradient:
5315 with TLinGradElement(aOwnerEl) do
5316 begin
5317 endPt.Y.SetValue(val, mu);
5318 end;
5319 end;
5320 end;
5321 //------------------------------------------------------------------------------
5322
5323 procedure Z_Attrib(aOwnerEl: TBaseElement; const value: UTF8String);
5324 var
5325 val: double;
5326 begin
5327 UTF8StringToFloat(value, val);
5328 if aOwnerEl is TFePointLightElement then
5329 TFePointLightElement(aOwnerEl).z := val;
5330 end;
5331
5332 //------------------------------------------------------------------------------
5333 //------------------------------------------------------------------------------
5334
5335 procedure TBaseElement.LoadAttribute(attrib: PSvgAttrib);
5336 begin
5337 with attrib^ do
5338 case hash of
5339 hbaseline_045_shift: BaselineShift_Attrib(self, value);
5340 hColor: Color_Attrib(self, value);
5341 hClip_045_path: ClipPath_Attrib(self, value);
5342 hCx: Cx_Attrib(self, value);
5343 hCy: Cy_Attrib(self, value);
5344 hD: D_Attrib(self, value);
5345 hDisplay: Display_Attrib(self, value);
5346 hDx: Dx_Attrib(self, value);
5347 hDy: Dy_Attrib(self, value);
5348 hStroke_045_DashArray: DashArray_Attrib(self, value);
5349 hStroke_045_DashOffset: DashOffset_Attrib(self, value);
5350 hFill: Fill_Attrib(self, value);
5351 hFill_045_Opacity: FillOpacity_Attrib(self, value);
5352 hFill_045_Rule: FillRule_Attrib(self, value);
5353 hFilter: Filter_Attrib(self, value);
5354 hflood_045_color: Fill_Attrib(self, value);
5355 hflood_045_opacity: FillOpacity_Attrib(self, value);
5356 hFont: Font_Attrib(self, value);
5357 hFont_045_Family: FontFamily_Attrib(self, value);
5358 hFont_045_Size: FontSize_Attrib(self, value);
5359 hFont_045_Style: FontStyle_Attrib(self, value);
5360 hFont_045_Weight: FontWeight_Attrib(self, value);
5361 hFx: Fx_Attrib(self, value);
5362 hFy: Fy_Attrib(self, value);
5363 hGradientTransform: GradientTransform_Attrib(self, value);
5364 hGradientUnits: GradientUnits_Attrib(self, value);
5365 hHeight: Height_Attrib(self, value);
5366 hHref: Href_Attrib(self, value);
5367 hId: Id_Attrib(self, value);
5368 hIn: In_Attrib(self, value);
5369 hIn2: In2_Attrib(self, value);
5370 hIntercept: Intercept_Attrib(self, value);
5371 hk1: K1_Attrib(self, value);
5372 hk2: K2_Attrib(self, value);
5373 hk3: K3_Attrib(self, value);
5374 hk4: K4_Attrib(self, value);
5375 hletter_045_spacing: LetterSpacing_Attrib(self, value);
5376 //hlighting_045_color: LightingColor_Attrib(self, value);
5377 hMarker_045_End: MarkerEnd_Attrib(self, value);
5378 hMarkerHeight: Height_Attrib(self, value);
5379 hMarker_045_Mid: MarkerMiddle_Attrib(self, value);
5380 hMarker_045_Start: MarkerStart_Attrib(self, value);
5381 hMarkerWidth: Width_Attrib(self, value);
5382 hMask: Mask_Attrib(self, value);
5383 hOffset: Offset_Attrib(self, value);
5384 hOpacity: Opacity_Attrib(self, value);
5385 hOperator: Operator_Attrib(self, value);
5386 hOrient: Orient_Attrib(self, value);
5387 hPatternUnits: GradientUnits_Attrib(self, value);
5388 hPatternTransform: Transform_Attrib(self, value);
5389 hPoints: Points_Attrib(self, value);
5390 hR: Rx_Attrib(self, value);
5391 hRefX: Rx_Attrib(self, value);
5392 hRefY: Ry_Attrib(self, value);
5393 hResult: Result_Attrib(self, value);
5394 hRotate: Rotate_Attrib(self, value);
5395 hRx: Rx_Attrib(self, value);
5396 hRy: Ry_Attrib(self, value);
5397 hspecularExponent: SpectacularExponent(self, value);
5398 hSlope: Slope_Attrib(self, value);
5399 hSpace: Space_Attrib(self, value);
5400 hSpreadMethod: SpreadMethod_Attrib(self, value);
5401 hstdDeviation: StdDev_Attrib(self, value);
5402 hStartOffset: StartOffset_Attrib(self, value);
5403 hStop_045_Color: StopColor_Attrib(self, value);
5404 hStop_045_Opacity: StopOpacity_Attrib(self, value);
5405 hStroke: Stroke_Attrib(self, value);
5406 hstroke_045_linecap: StrokeLineCap_Attrib(self, value);
5407 hstroke_045_linejoin: StrokeLineJoin_Attrib(self, value);
5408 hstroke_045_miterlimit: StrokeMiterLimit_Attrib(self, value);
5409 hStroke_045_Opacity: StrokeOpacity_Attrib(self, value);
5410 hStroke_045_Width: StrokeWidth_Attrib(self, value);
5411 hTableValues: TableValues_Attrib(self, value);
5412 hText_045_Anchor: TextAlign_Attrib(self, value);
5413 hText_045_Decoration: TextDecoration_Attrib(self, value);
5414 hTextLength: TextLength_Attrib(self, value);
5415 hTransform: Transform_Attrib(self, value);
5416 hType: Type_Attrib(self, value);
5417 hValues: Values_Attrib(self, value);
5418 hViewbox: Viewbox_Attrib(self, value);
5419 hVisibility: Visibility_Attrib(self, value);
5420 hWidth: Width_Attrib(self, value);
5421 hX: X1_Attrib(self, value);
5422 hX1: X1_Attrib(self, value);
5423 hX2: X2_Attrib(self, value);
5424 hXlink_058_Href: Href_Attrib(self, value);
5425 hY: Y1_Attrib(self, value);
5426 hY1: Y1_Attrib(self, value);
5427 hY2: Y2_Attrib(self, value);
5428 hZ: Z_Attrib(self, value);
5429 end;
5430 end;
5431 //------------------------------------------------------------------------------
5432
5433 procedure TBaseElement.LoadAttributes;
5434 var
5435 i: integer;
5436 begin
5437 for i := 0 to fXmlEl.AttribCount -1 do
5438 LoadAttribute(PSvgAttrib(fXmlEl.attrib[i]));
5439 end;
5440 //------------------------------------------------------------------------------
5441
5442 function PreferRelativeFraction(val: TValue): TTriState;
5443 {$IFDEF INLINE} inline; {$ENDIF}
5444 begin
5445 if (val.rawVal = InvalidD) or (val.unitType = utUnknown) then
5446 Result := tsUnknown
5447 else if val.unitType = utPercent then Result := tsYes
5448 else if val.unitType <> utNumber then Result := tsNo
5449 else if (Abs(val.rawVal) < 1) then Result := tsYes
5450 else Result := tsNo;
5451 end;
5452 //------------------------------------------------------------------------------
5453
5454 function TBaseElement.GetRelFracLimit: double;
5455 begin
5456 //the default behaviour here is to assume untyped fractional values
5457 //below 1.0 are values relative (to the bounding size) BUT ONLY WHEN
5458 //the parent element's width or height are relative (ie percentages).
5459 if Assigned(fParent) and (fParent.fXmlEl.hash <> hSvg) then
5460 begin
5461 case PreferRelativeFraction(fParent.elRectWH.width) of
5462 tsYes: begin Result := 1.0; Exit; end;
5463 tsNo : begin Result := 0; Exit; end;
5464 end;
5465 case PreferRelativeFraction(fParent.elRectWH.height) of
5466 tsYes: begin Result := 1.0; Exit; end;
5467 tsNo : begin Result := 0; Exit; end;
5468 end;
5469 end;
5470 Result := 0;
5471 end;
5472 //------------------------------------------------------------------------------
5473
5474 function TBaseElement.LoadContent: Boolean;
5475 var
5476 i : integer;
5477 svgEl : TSvgXmlEl;
5478 elClass : TElementClass;
5479 el : TBaseElement;
5480 begin
5481 Result := false;
5482 for i := 0 to fXmlEl.childs.Count -1 do
5483 begin
5484 svgEl := TSvgXmlEl(fXmlEl.childs[i]);
5485 if svgEl.hash = 0 then Continue;
5486 elClass := HashToElementClass(svgEl.hash);
5487 el := elClass.Create(self, svgEl);
5488 Self.fChilds.Add(el);
5489 el.LoadAttributes;
5490 if el.fXmlEl.childs.Count = 0 then Continue
5491 else if not el.LoadContent then Exit; //error
5492 end;
5493 Result := true;
5494 end;
5495
5496 //------------------------------------------------------------------------------
5497 // TSvgReader
5498 //------------------------------------------------------------------------------
5499
5500 constructor TSvgReader.Create;
5501 begin
5502 fSvgParser := TSvgParser.Create;
5503 fLinGradRenderer := TLinearGradientRenderer.Create;
5504 fRadGradRenderer := TSvgRadialGradientRenderer.Create;
5505 fCustomRendererCache := TCustomRendererCache.Create;
5506 fIdList := TSvgIdNameHashMap.Create;
5507 fSimpleDrawList := TList.Create;
5508
5509 fBlurQuality := 1; //0: draft (faster); 1: good; 2: excellent (slow)
5510 currentColor := clBlack32;
5511 fUsePropScale := true;
5512 end;
5513 //------------------------------------------------------------------------------
5514
5515 destructor TSvgReader.Destroy;
5516 begin
5517 Clear;
5518 fSvgParser.Free;
5519 fIdList.Free;
5520
5521 fLinGradRenderer.Free;
5522 fRadGradRenderer.Free;
5523 fCustomRendererCache.Free;
5524 FreeAndNil(fFontCache);
5525 fSimpleDrawList.Free;
5526
5527 inherited;
5528 end;
5529 //------------------------------------------------------------------------------
5530
5531 procedure TSvgReader.Clear;
5532 var
5533 i: integer;
5534 begin
5535 FreeAndNil(fRootElement);
5536 fSvgParser.Clear;
5537 fIdList.Clear;
5538 fLinGradRenderer.Clear;
5539 fRadGradRenderer.Clear;
5540 currentColor := clBlack32;
5541 userSpaceBounds := NullRectD;
5542 for i := 0 to fSimpleDrawList.Count -1 do
5543 Dispose(PSimpleDrawData(fSimpleDrawList[i]));
5544 fSimpleDrawList.Clear;
5545 end;
5546 //------------------------------------------------------------------------------
5547
5548 procedure TSvgReader.DrawImage(img: TImage32; scaleToImage: Boolean);
5549 var
5550 scale, scaleH: double;
5551 di: TDrawData;
5552 begin
5553 if not Assigned(fRootElement) or not assigned(img) then Exit;
5554
5555 with fRootElement do
5556 begin
5557 if viewboxWH.IsEmpty then
5558 begin
5559 viewboxWH.Width := elRectWH.width.GetValue(defaultSvgWidth, 0);
5560 viewboxWH.height := elRectWH.height.GetValue(defaultSvgHeight, 0);
5561 if viewboxWH.IsEmpty then Exit; // this should never happen
5562 end;
5563
5564 fBackgndImage := img;
5565 di := fDrawData;
5566 if di.currentColor = clInvalid then
5567 di.currentColor := currentColor;
5568
5569 MatrixTranslate(di.matrix, -viewboxWH.Left, -viewboxWH.Top);
5570
5571 //the width and height attributes generally indicate the size of the
5572 //rendered image unless they are percentage values. Nevertheless, these
5573 //values can be still overridden by the scaleToImage parameter above
5574
5575 di.bounds := viewboxWH.RectD;
5576 userSpaceBounds := fDrawData.bounds;
5577
5578 if scaleToImage and not img.IsEmpty then
5579 begin
5580 //nb: the calculated vbox.width and vbox.height are ignored here since
5581 //we're scaling the SVG image to the display image. However we still
5582 //need to call GetViewbox (above) to make sure that viewboxWH is filled.
5583 if fUsePropScale then
5584 begin
5585 scale := GetScaleForBestFit(
5586 viewboxWH.Width, viewboxWH.Height, img.Width, img.Height);
5587 scaleH := scale;
5588 end else
5589 begin
5590 scale := GetScale(viewboxWH.Width, img.Width);
5591 scaleH := GetScale(viewboxWH.Height, img.Height);
5592 end;
5593 MatrixScale(di.matrix, scale, scaleH);
5594 img.SetSize(
5595 Round(viewboxWH.Width * scale),
5596 Round(viewboxWH.Height * scaleH));
5597 end else
5598 img.SetSize(Round(viewboxWH.Width), Round(viewboxWH.Height));
5599
5600 end;
5601
5602 if fBkgndColor <> clNone32 then
5603 img.Clear(fBkgndColor);
5604
5605 // // Delay the creation of the TempImage until it is actually needed.
5606 // // Not all SVGs need it.
5607 // fTempImageWidth := img.Width;
5608 // fTempImageHeight := img.Height;
5609
5610 img.BeginUpdate;
5611 try
5612 fRootElement.Draw(img, di);
5613 finally
5614 // fTempImageWidth := 0;
5615 // fTempImageHeight := 0;
5616 FreeAndNil(fTempImage);
5617 img.EndUpdate;
5618 end;
5619 end;
5620 //------------------------------------------------------------------------------
5621
5622 function TSvgReader.GetTempImage: TImage32;
5623 var
5624 Pixels: TArrayOfColor32;
5625 begin
5626 // Use an additional method to execute the dyn-array management
5627 // only if we create the TempImage.
5628 if fTempImage = nil then
5629 begin
5630 // Create an uninitialized image. It is cleared by the caller before it is used.
5631 NewColor32Array(Pixels, BackgndImage.Width * BackgndImage.Height, True);
5632 fTempImage := TImage32.Create(Pixels, BackgndImage.Width, BackgndImage.Height);
5633 fTempImage.BlockNotify;
5634 end;
5635 Result := fTempImage;
5636 end;
5637 //------------------------------------------------------------------------------
5638
5639 function TSvgReader.LoadInternal: Boolean;
5640 begin
5641 Result := false;
5642 if not Assigned(fSvgParser.svgTree) or
5643 (fSvgParser.svgTree.hash <> hSvg) then Exit;
5644 fRootElement := TSvgElement.Create(nil, fSvgParser.svgTree);
5645 fRootElement.fSvgReader := self;
5646 fRootElement.LoadAttributes;
5647 Result := fRootElement.LoadContent;
5648 end;
5649 //------------------------------------------------------------------------------
5650
5651 function TSvgReader.LoadFromFile(const filename: string): Boolean;
5652 begin
5653 Clear;
5654 Result := fSvgParser.LoadFromFile(filename) and LoadInternal;
5655 end;
5656 //------------------------------------------------------------------------------
5657
5658 function TSvgReader.LoadFromStream(stream: TStream): Boolean;
5659 begin
5660 Clear;
5661 Result := fSvgParser.LoadFromStream(stream) and LoadInternal;
5662 end;
5663 //------------------------------------------------------------------------------
5664
5665 function TSvgReader.LoadFromString(const str: string): Boolean;
5666 begin
5667 Clear;
5668 Result := fSvgParser.LoadFromString(str) and LoadInternal;
5669 end;
5670 //------------------------------------------------------------------------------
5671
5672 procedure TSvgReader.SetOverrideFillColor(color: TColor32);
5673 var
5674 dd: TDrawData;
5675 begin
5676 if not Assigned(RootElement) or (color = clNone32) then Exit;
5677 dd := RootElement.DrawData;
5678 dd.fillColor := color;
5679 RootElement.DrawData := dd;
5680 end;
5681 //------------------------------------------------------------------------------
5682
5683 procedure TSvgReader.SetOverrideStrokeColor(color: TColor32);
5684 var
5685 dd: TDrawData;
5686 begin
5687 if not Assigned(RootElement) or (color = clNone32) then Exit;
5688 dd := RootElement.DrawData;
5689 if dd.strokeColor = clInvalid then Exit;
5690 dd.strokeColor := color;
5691 RootElement.DrawData := dd;
5692 end;
5693 //------------------------------------------------------------------------------
5694
5695 function TSvgReader.FindElement(const idName: UTF8String): TBaseElement;
5696 begin
5697 if Assigned(RootElement) then
5698 Result := RootElement.FindChild(idName) else
5699 Result := nil;
5700 end;
5701 //------------------------------------------------------------------------------
5702
5703 procedure TSvgReader.GetBestFont(const svgFontInfo: TSVGFontInfo);
5704 var
5705 i, len: integer;
5706 bestFontReader: TFontReader;
5707 fi: TFontInfo;
5708 begin
5709 if svgFontInfo.family = tfUnknown then
5710 fi.family := tfSerif else
5711 fi.family := svgFontInfo.family;
5712 fi.faceName := ''; //just match to a family here, not to a specific facename
5713 fi.macStyles := [];
5714 len := Length(svgFontInfo.familyNames);
5715 SetLength(fi.familyNames, len);
5716 for i := 0 to len -1 do
5717 fi.familyNames[i] := svgFontInfo.familyNames[i];
5718
5719 if svgFontInfo.italic = sfsItalic then
5720 Include(fi.macStyles, msItalic);
5721 if svgFontInfo.weight >= 600 then
5722 Include(fi.macStyles, msBold);
5723
5724 bestFontReader := FontManager.GetBestMatchFont(fi);
5725 if not Assigned(bestFontReader) then Exit;
5726
5727 if Assigned(fFontCache) then
5728 fFontCache.FontReader := bestFontReader else
5729 fFontCache := TFontCache.Create(bestFontReader, defaultFontHeight);
5730
5731 fFontCache.Underlined := False;
5732 fFontCache.StrikeOut := False;
5733 case svgFontInfo.decoration of
5734 fdUnderline : fFontCache.Underlined := true;
5735 fdStrikeThrough : fFontCache.StrikeOut := true;
5736 end;
5737 end;
5738 //------------------------------------------------------------------------------
5739
5740 procedure TSvgReader.SetBlurQuality(quality: integer);
5741 begin
5742 fBlurQuality := Max(0, Min(2, quality));
5743 end;
5744 //------------------------------------------------------------------------------
5745
5746 function TSvgReader.GetIsEmpty: Boolean;
5747 begin
5748 Result := not Assigned(fRootElement);
5749 end;
5750 //------------------------------------------------------------------------------
5751 //------------------------------------------------------------------------------
5752
5753 end.