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

View File

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

View File

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

View File

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