The code below shows how to load and render a SVG file into an ISkCanvas:
uses
System.Skia, FMX.Skia; // or Vcl.Skia
var
LBitmap: TBitmap;
begin
LBitmap := TBitmap.Create(100, 100);
try
LBitmap.SkiaDraw(
procedure (const ACanvas: ISKCanvas)
var
LSvgBrush: TSkSvgBrush;
begin
LSvgBrush := TSkSvgBrush.Create;
try
LSvgBrush.Source := TFile.ReadAllText('Samples\Demo\Assets\panda.svg');
LSvgBrush.Render(ACanvas, RectF(0, 0, LBitmap.Width, LBitmap.Height), 1);
finally
LSvgBrush.Free;
end;
end);
It is possible to create any type of SVG file using Skia4Delphi in an extremely simple way. Using the TSkSVGCanvas class, a SkCanvas is generated in which everything that you drawn using it, is transformed into an SVG file, even texts, gradients and effects. See an example:
uses
System.Skia;
type
TSkDrawProc = reference to procedure(const ACanvas: ISkCanvas; const ADest: TRectF);
procedure CreateSVG(const AOutputFileName: string; const AWidth, AHeight: Integer; const ADrawProc: TSkDrawProc);
var
LStream: TStream;
LCanvas: ISkCanvas;
begin
LStream := TFileStream.Create(AOutputFileName, fmCreate);
try
LCanvas := TSkSVGCanvas.Make(RectF(0, 0, AWidth, AHeight), LStream, [TSkSVGCanvasFlag.ConvertTextToPaths]);
ADrawProc(LCanvas, RectF(0, 0, AWidth, AHeight));
LCanvas := nil;
finally
LStream.Free;
end;
end;
CreateSVG('output.svg', 256, 256,
procedure (const ACanvas: ISkCanvas; const ADest: TRectF)
var
LFont: ISkFont;
LPaint: ISkPaint;
begin
LFont := TSkFont.Create(TSkTypeface.MakeFromFile('Samples\Demo\Assets\nunito-extrabold.ttf'), 23);
LPaint := TSkPaint.Create;
LPaint.Shader := TSkShader.MakeGradientLinear(PointF(0, 0), PointF(256, 145), $FFFF5F5F, $FF5B8DFE, TSkTileMode.Clamp);
ACanvas.DrawSimpleText('"Each dream that you', 2, 25, LFont, LPaint);
ACanvas.DrawSimpleText('leave behind is a part', 2, 55, LFont, LPaint);
ACanvas.DrawSimpleText('of your future that will', 2, 85, LFont, LPaint);
ACanvas.DrawSimpleText('no longer exist."', 2, 115, LFont, LPaint);
LFont := TSkFont.Create(TSkTypeface.MakeFromFile('Samples\Demo\Assets\bonheur-royale-regular.ttf'), 28);
LPaint.Shader := nil;
LPaint.Color := $FF5B8DFE;
ACanvas.DrawSimpleText('(Steve Jobs)', 2, 150, LFont, LPaint);
end);
This code results in the SVG file below:
TSkSvg is the control (VCL or FMX) to load and display SVG easily:
var
LSvg: TSkSvg;
begin
LSvg := TSkSvg.Create(Self);
LSvg.Svg.Source := TFile.ReadAllText('Samples\Demo\Assets\panda.svg');
LSvg.Parent := Self;
end;
The example above results in the output below:
Text property of SVG file content.
When this property is Null (default value), the control will render the SVG in default color. But when this property have another value, the control will replace the SVG color to the OverrideColor
. This is usefull in icons SVGs, for example.
Specifies whether and how to resize, replicate, and position the SVG draw inside the TSkSvg control. See the available options:
TSkSvgWrapMode | Description |
---|---|
Default | Keep SVG stretch defined in SVG file |
Fit (Default) | Stretches SVG into the destination rectangle, preserving aspect ratio |
FitCrop | Stretches SVG into the destination rectangle, preserving aspect ratio, but cropping the excess |
Original | Display the SVG with its original dimensions |
OriginalCenter | Display the SVG with its original dimensions but centered |
Place | Places the SVG inside the destination rectangle. If the SVG is greater than the destination rectangle then the SVG is scaled down with aspect ratio preserved |
Stretch | Stretch the SVG to fill the entire destination rectangle |
Tile | Tile (multiply) the SVG to cover the entire destination rectangle |
Skia4Delphi does not support inner <style>
element, only presentation attributes. Some vector graphics editors when saving/exporting the SVG file allow you to configure it to be saved/exported with presentation attributes only in styling options.
You can create or convert any SVG file so that it can be compatible with the library in different ways. The recommended way is to use Adobe Illustrator:
- Open in Adobe Illustrator the SVG file (or vector like EPS file);
- Click in File > Export > Export as...;
- Choose the file name output, and the SVG to file type and press OK;
- In SVG Options window, set Presentation Attributes and click in OK:
Skia4Delphi does not support DTD <!DOCTYPE ...>
.
When exported via Adobe Illustrator the DTD is removed, but it can be removed manually as well.
Skia4Delphi does not support inner <symbol>
element.