TAChart: Fix font color/style/rotation in TADrawerSVG

git-svn-id: trunk@47402 -
This commit is contained in:
wp 2015-01-16 16:26:08 +00:00
parent 1a64d63b36
commit 6afb29b0fc
4 changed files with 104 additions and 29 deletions

View File

@ -1,5 +1,5 @@
object Form1: TForm1
Left = 318
Left = 350
Height = 299
Top = 151
Width = 538
@ -7,7 +7,7 @@ object Form1: TForm1
ClientHeight = 299
ClientWidth = 538
OnCreate = FormCreate
LCLVersion = '0.9.31'
LCLVersion = '1.5'
object Chart1: TChart
Left = 0
Height = 275
@ -15,10 +15,30 @@ object Form1: TForm1
Width = 538
AxisList = <
item
Minors = <>
Title.LabelFont.CharSet = ANSI_CHARSET
Title.LabelFont.Color = clRed
Title.LabelFont.Height = -19
Title.LabelFont.Name = 'Times New Roman'
Title.LabelFont.Orientation = 900
Title.LabelFont.Pitch = fpVariable
Title.LabelFont.Quality = fqDraft
Title.LabelFont.Style = [fsItalic]
Title.Visible = True
Title.Caption = 'y axis'
end
item
Alignment = calBottom
Minors = <>
Title.LabelFont.CharSet = ANSI_CHARSET
Title.LabelFont.Color = clBlue
Title.LabelFont.Height = -19
Title.LabelFont.Name = 'Times New Roman'
Title.LabelFont.Pitch = fpVariable
Title.LabelFont.Quality = fqDraft
Title.LabelFont.Style = [fsItalic]
Title.Visible = True
Title.Caption = 'x axis'
end>
Foot.Brush.Color = clBtnFace
Foot.Font.Color = clBlue
@ -83,7 +103,7 @@ object Form1: TForm1
end
end
object SaveDialog1: TSaveDialog
left = 64
left = 120
top = 56
end
end

View File

@ -98,7 +98,6 @@ begin
fs := TFileStream.Create(GetFileName('svg'), fmCreate);
try
id := TSVGDrawer.Create(fs, true);
id.DoChartColorToFPColor := @ChartColorSysToFPColor;
with Chart1 do
Draw(id, Rect(0, 0, Width, Height));
finally

View File

@ -1,4 +1,4 @@
<?xml version="1.0"?>
<?xml version="1.0" encoding="UTF-8"?>
<CONFIG>
<ProjectOptions>
<Version Value="9"/>
@ -48,19 +48,19 @@
<Unit0>
<Filename Value="savedemo.lpr"/>
<IsPartOfProject Value="True"/>
<UnitName Value="savedemo"/>
</Unit0>
<Unit1>
<Filename Value="main.pas"/>
<IsPartOfProject Value="True"/>
<ComponentName Value="Form1"/>
<HasResources Value="True"/>
<ResourceBaseClass Value="Form"/>
<UnitName Value="main"/>
</Unit1>
</Units>
</ProjectOptions>
<CompilerOptions>
<Version Value="10"/>
<Version Value="11"/>
<PathDelim Value="\"/>
<Target>
<Filename Value="savedemo"/>
@ -81,11 +81,5 @@
</Win32>
</Options>
</Linking>
<Other>
<CompilerMessages>
<UseMsgFile Value="True"/>
</CompilerMessages>
<CompilerPath Value="$(CompPath)"/>
</Other>
</CompilerOptions>
</CONFIG>

View File

@ -14,9 +14,20 @@ unit TADrawerSVG;
interface
uses
Classes, FPImage, FPCanvas, TAChartUtils, TADrawUtils;
Graphics, Classes, FPImage, FPCanvas, TAChartUtils, TADrawUtils;
type
TSVGFont = record
Name: String;
Color: TFPColor;
Size: integer;
Orientation: Integer; // angle * 10 (90° --> 900), >0 if ccw.
Bold: boolean;
Italic: boolean;
Underline: boolean;
StrikeThrough: boolean;
end;
TSVGDrawer = class(TBasicDrawer, IChartDrawer)
strict private
@ -24,8 +35,7 @@ type
FBrushColor: TFPColor;
FBrushStyle: TFPBrushStyle;
FClippingPathId: Integer;
FFont: TFPCustomFont;
FFontAngle: Double;
FFont: TSVGFont;
FPatterns: TStrings;
FPen: TFPCustomPen;
FPrevPos: TPoint;
@ -49,6 +59,7 @@ type
function GetFontAngle: Double; override;
function SimpleTextExtent(const AText: String): TPoint; override;
procedure SimpleTextOut(AX, AY: Integer; const AText: String); override;
public
constructor Create(AStream: TStream; AWriteDocType: Boolean);
destructor Destroy; override;
@ -115,11 +126,25 @@ begin
Result := FloatToStr(AValue, fmtSettings);
end;
function SVGGetFontOrientationFunc(AFont: TFPCustomFont): Integer;
begin
if AFont is TFont then
Result := (AFont as TFont).Orientation
else
Result := AFont.Orientation;
end;
function SVGChartColorToFPColor(AChartColor: TChartColor): TFPColor;
begin
Result := ChartColorToFPColor(ColorToRGB(AChartColor));
end;
{ TSVGDrawer }
procedure TSVGDrawer.AddToFontOrientation(ADelta: Integer);
begin
FFontAngle += OrientToRad(ADelta);
FFont.Orientation += ADelta;
end;
procedure TSVGDrawer.ClippingStart(const AClipRect: TRect);
@ -148,6 +173,8 @@ begin
FStream := AStream;
FPatterns := TStringList.Create;
FPen := TFPCustomPen.Create;
FGetFontOrientationFunc := @SVGGetFontOrientationFunc;
FChartColorToFPColorFunc := @SVGChartColorToFPColor;
if AWriteDocType then begin
WriteStr('<?xml version="1.0"?>');
WriteStr('<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN"');
@ -221,7 +248,7 @@ end;
function TSVGDrawer.GetFontAngle: Double;
begin
Result := FFontAngle;
Result := FFont.Orientation;
end;
procedure TSVGDrawer.Line(AX1, AY1, AX2, AY2: Integer);
@ -374,10 +401,25 @@ begin
end;
procedure TSVGDrawer.SetFont(AFont: TFPCustomFont);
var
i: Integer;
begin
FFont := AFont;
if FMonochromeColor <> clTAColor then
FFont.FPColor := FChartColorToFPColorFunc(FMonochromeColor);
with FFont do begin
Name := AFont.Name;
Size := IfThen(AFont.Size=0, 8, AFont.Size);
// ???
if FMonochromeColor <> clTAColor then
Color := FChartColorToFPColorFunc(FMonochromeColor)
else
Color := AFont.FPColor;
Orientation := FGetFontOrientationFunc(AFont);
FFont.Bold := AFont.Bold;
FFont.Italic := AFont.Italic;
FFont.Underline := AFont.Underline;
FFont.Strikethrough := AFont.Strikethrough;
end;
end;
procedure TSVGDrawer.SetPen(APen: TFPCustomPen);
@ -404,15 +446,35 @@ end;
procedure TSVGDrawer.SimpleTextOut(AX, AY: Integer; const AText: String);
var
p: TPoint;
stext: String;
sstyle: String;
fs: TFormatSettings;
begin
p := RotatePoint(Point(0, FontSize), -FFontAngle) + Point(AX, AY);
WriteFmt(
'<text x="%d" y="%d" textLength="%d" ' +
'style="stroke:none; fill:%s;%s font-family:%s; font-size:%dpt;">' +
'%s</text>',
[p.X, p.Y, SimpleTextExtent(AText).X,
ColorToHex(FFont.FPColor), FormatIfNotEmpty(' opacity:%s;', OpacityStr),
FFont.Name, FontSize, AText]);
fs := DefaultFormatSettings;
fs.DecimalSeparator := '.';
fs.ThousandSeparator := '#';
p := RotatePoint(Point(0, FontSize), OrientToRad(-FFont.Orientation)) + Point(AX, AY);
stext := Format('x="%d" y="%d"', [p.X, p.Y]);
if FFont.Orientation <> 0 then
stext := stext + Format(' transform="rotate(%g,%d,%d)"', [-FFont.Orientation*0.1, p.X, p.Y], fs);
sstyle := Format('fill:%s; font-family:''%s''; font-size:%dpt;',
[ColorToHex(FFont.Color), FFont.Name, FontSize]);
if FFont.Bold then
sstyle := sstyle + ' font-weight:bold;';
if FFont.Italic then
sstyle := sstyle + ' font-style:oblique;';
if FFont.Underline and FFont.Strikethrough then
sstyle := sstyle + ' text-decoration:underline,line-through;'
else if FFont.Underline then
sstyle := sstyle + ' text-deocration:underline;'
else if FFont.Strikethrough then
sstyle := sstyle + ' text-decoration:line-through;';
if OpacityStr <> '' then
sstyle := sstyle + OpacityStr + ';';
WriteFmt('<text %s style="%s">%s</text>', [stext, sstyle, AText]);
end;
function TSVGDrawer.StyleFill: String;