ISVG Interface
Unit: SVGInterfaces
Description
ISVG is the core interface for SVG document manipulation and rendering. It is implemented by factory-specific classes (Image32, Skia4Delphi, Direct2D, SVGMagic) and provides a unified API regardless of the underlying rendering engine.
Properties
Size Properties (Read-Only)
property Width: Single;
property Height: Single;Natural size of the SVG document.
Rendering Properties
property Opacity: Single; // 0.0 to 1.0
property GrayScale: Boolean;
property FixedColor: TColor;
property ApplyFixedColorToRootOnly: Boolean;- Opacity is limited to the range 0..1 (a value outside it is clamped).
- FixedColor:
SVG_INHERIT_COLOR(clDefault) andSVG_NONE_COLOR(clNone) both mean "no fixed color": the original colors of the SVG are used.fill="none"is always preserved, so outline icons stay outlines. - ApplyFixedColorToRootOnly: the color is applied only to what the root element passes down (its fill, stroke and
currentColor); elements with their own colors keep them.
Content
property Source: string;SVG XML source code. It always returns the text as it was assigned or loaded (also with engines that rewrite it internally, like Skia4Delphi with <style> blocks). Assigning an invalid SVG raises ESVGException; assigning '' empties the document.
Methods
State
function IsEmpty: Boolean;
procedure Clear;Clear empties the document: after it IsEmpty is True, Source is '', Width and Height are 0, and PaintTo paints nothing.
File/Stream Operations
procedure SaveToStream(Stream: TStream);
procedure SaveToFile(const FileName: string);
procedure LoadFromStream(Stream: TStream);
procedure LoadFromFile(const FileName: string);LoadFromStream and LoadFromFile raise ESVGException if the content is not a valid SVG, with every engine.
Rendering
procedure PaintTo(DC: HDC; R: TRectF; KeepAspectRatio: Boolean = True);Global Functions
Factory Management
function GlobalSVGFactory: ISVGFactory;
procedure SetGlobalSvgFactory(const AFactory: ISVGFactory);Factory Getters
function GetImage32SVGFactory: ISVGFactory;
function GetD2DSVGFactory: ISVGFactory;
function GetSkiaSVGFactory: ISVGFactory;
function GetSVGMagicFactory: ISVGFactory;Examples
Direct SVG Usage
var
SVG: ISVG;
begin
SVG := GlobalSVGFactory.NewSvg;
SVG.LoadFromFile('icon.svg');
SVG.FixedColor := clBlue;
SVG.Opacity := 0.8;
SVG.PaintTo(Canvas.Handle, TRectF.Create(0, 0, 100, 100));
end;Change Rendering Engine
// Switch to Skia4Delphi
SetGlobalSvgFactory(GetSkiaSVGFactory);
// All new SVG objects will use Skia
var SVG := GlobalSVGFactory.NewSvg;