1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
|
{
*****************************************************************************
See the file COPYING.modifiedLGPL.txt, included in this distribution,
for details about the license.
*****************************************************************************
Authors: Alexander Klenin
}
unit TADrawerFPVectorial;
{$H+}
interface
uses
SysUtils, Graphics, Classes, FPCanvas, FPImage, EasyLazFreeType, FPVectorial,
TAFonts, TAChartUtils, TADrawUtils;
type
{ TFPVectorialDrawer }
TFPVectorialDrawer = class(TBasicDrawer, IChartDrawer)
strict private
FBoundingBox: TRect;
FBrushColor: TFPColor;
FBrushStyle: TFPBrushStyle;
FCanvas: TvVectorialPage;
FFont: TvFont;
FFTFont: TFreeTypefont;
FFontHeight: Integer;
FPenColor: TFPColor;
FPenStyle: TFPPenStyle;
FPenWidth: Integer;
procedure AddLine(AX, AY: Integer);
procedure ApplyBrush;
procedure ApplyPen;
function InvertY(AY: Integer): Integer; inline;
strict protected
procedure SetBrush(ABrush: TFPCustomBrush);
procedure SetFont(AFont: TFPCustomFont);
procedure SetPen(APen: TFPCustomPen);
function SimpleTextExtent(const AText: String): TPoint; override;
procedure SimpleTextOut(AX, AY: Integer; const AText: String); override;
public
constructor Create(ACanvas: TvVectorialPage);
public
procedure AddToFontOrientation(ADelta: Integer);
procedure ClippingStart;
procedure ClippingStart(const AClipRect: TRect);
procedure ClippingStop;
procedure DrawingBegin(const ABoundingBox: TRect); override;
procedure Ellipse(AX1, AY1, AX2, AY2: Integer);
procedure FillRect(AX1, AY1, AX2, AY2: Integer);
function GetBrushColor: TChartColor;
function GetFontAngle: Double; override;
function GetFontColor: TFPColor; override;
function GetFontName: String; override;
function GetFontSize: Integer; override;
function GetFontStyle: TChartFontStyles; override;
procedure Line(AX1, AY1, AX2, AY2: Integer);
procedure Line(const AP1, AP2: TPoint); overload;
procedure LineTo(AX, AY: Integer); override;
procedure MoveTo(AX, AY: Integer); override; overload;
procedure Polygon(
const APoints: array of TPoint; AStartIndex, ANumPts: Integer); override;
procedure Polyline(
const APoints: array of TPoint; AStartIndex, ANumPts: Integer);
procedure PrepareSimplePen(AColor: TChartColor);
procedure PutPixel(AX, AY: Integer; AColor: TChartColor); override;
procedure RadialPie(
AX1, AY1, AX2, AY2: Integer;
AStartAngle16Deg, AAngleLength16Deg: Integer);
procedure Rectangle(const ARect: TRect);
procedure Rectangle(AX1, AY1, AX2, AY2: Integer);
procedure ResetFont;
procedure SetBrushColor(AColor: TChartColor);
procedure SetBrushParams(AStyle: TFPBrushStyle; AColor: TChartColor);
procedure SetPenParams(AStyle: TFPPenStyle; AColor: TChartColor);
end experimental;
implementation
uses
Math, TAGeometry;
function SVGGetFontOrientationFunc(AFont: TFPCustomFont): Integer;
begin
if AFont is TFont then
Result := round(TFont(AFont).Orientation * 0.1)
else
Result := round(AFont.Orientation * 0.1);
end;
function SVGChartColorToFPColor(AChartColor: TChartColor): TFPColor;
begin
Result := ChartColorToFPColor(ColorToRGB(AChartColor));
end;
{ TFPVectorialDrawer }
procedure TFPVectorialDrawer.AddLine(AX, AY: Integer);
begin
FCanvas.AddLineToPath(AX, InvertY(AY));
end;
procedure TFPVectorialDrawer.AddToFontOrientation(ADelta: Integer);
begin
// Not implemented.
Unused(ADelta);
end;
procedure TFPVectorialDrawer.ApplyBrush;
begin
FCanvas.SetBrushColor(FBrushColor);
FCanvas.SetBrushStyle(FBrushStyle);
end;
procedure TFPVectorialDrawer.ApplyPen;
begin
FCanvas.SetPenColor(FPenColor);
FCanvas.SetPenStyle(FPenStyle);
FCanvas.SetPenWidth(FPenWidth);
end;
procedure TFPVectorialDrawer.ClippingStart(const AClipRect: TRect);
begin
Unused(AClipRect); // Not implemented.
end;
procedure TFPVectorialDrawer.ClippingStart;
begin
// Not implemented.
end;
procedure TFPVectorialDrawer.ClippingStop;
begin
// Not implemented.
end;
constructor TFPVectorialDrawer.Create(ACanvas: TvVectorialPage);
begin
inherited Create;
InitFonts;
FCanvas := ACanvas;
FGetFontOrientationFunc := @SVGGetFontOrientationFunc;
FChartColorToFPColorFunc := @SVGChartColorToFPColor;
end;
procedure TFPVectorialDrawer.DrawingBegin(const ABoundingBox: TRect);
begin
FBoundingBox := ABoundingBox;
end;
procedure TFPVectorialDrawer.Ellipse(AX1, AY1, AX2, AY2: Integer);
var
e: TEllipse;
begin
e.InitBoundingBox(AX1, AY1, AX2, AY2);
FCanvas.AddEllipse(e.FC.X, InvertY(Round(e.FC.Y)), e.FR.x, e.FR.Y, 0.0);
end;
procedure TFPVectorialDrawer.FillRect(AX1, AY1, AX2, AY2: Integer);
begin
MoveTo(AX1, AY1);
AddLine(AX2, AY1);
AddLine(AX2, AY2);
AddLine(AX1, AY2);
AddLine(AX1, AY1);
FCanvas.SetBrushStyle(bsClear);
FCanvas.SetPenColor(FPenColor);
FCanvas.EndPath();
end;
function TFPVectorialDrawer.GetBrushColor: TChartColor;
begin
Result := FPColorToChartColor(FBrushColor);
end;
function TFPVectorialDrawer.GetFontAngle: Double;
begin
Result := DegToRad(FFont.Orientation);
end;
function TFPVectorialDrawer.GetFontcolor: TFPColor;
begin
Result := FFont.Color;
end;
function TFPVectorialDrawer.GetFontName: String;
begin
Result := FFont.Name;
end;
function TFPVectorialDrawer.GetFontSize: Integer;
begin
Result := IfThen(FFont.Size = 0, DEFAULT_FONT_SIZE, FFont.Size);
end;
function TFPVectorialDrawer.GetFontStyle: TChartFontStyles;
begin
Result := [];
if FFont.Bold then Include(Result, cfsBold);
if FFont.Italic then Include(Result, cfsItalic);
if FFont.Underline then Include(Result, cfsUnderline);
if FFont.StrikeThrough then Include(Result, cfsStrikeout);
end;
function TFPVectorialDrawer.InvertY(AY: Integer): Integer;
begin
with FBoundingBox do
Result := Bottom - Top - AY;
end;
procedure TFPVectorialDrawer.Line(AX1, AY1, AX2, AY2: Integer);
begin
FCanvas.StartPath(AX1, InvertY(AY1));
AddLine(AX2, AY2);
FCanvas.SetPenColor(FPenColor);
FCanvas.EndPath();
end;
procedure TFPVectorialDrawer.Line(const AP1, AP2: TPoint);
begin
Line(AP1.X, AP1.Y, AP2.X, AP2.Y);
end;
procedure TFPVectorialDrawer.LineTo(AX, AY: Integer);
begin
FCanvas.AddLineToPath(AX, InvertY(AY), FPenColor);
end;
procedure TFPVectorialDrawer.MoveTo(AX, AY: Integer);
begin
FCanvas.EndPath();
FCanvas.StartPath(AX, InvertY(AY));
end;
procedure TFPVectorialDrawer.Polygon(
const APoints: array of TPoint; AStartIndex, ANumPts: Integer);
var
i: Integer;
begin
if ANumPts <= 0 then exit;
MoveTo(APoints[AStartIndex]);
for i := 1 to ANumPts - 1 do
with APoints[i + AStartIndex] do
AddLine(X, Y);
ApplyBrush;
ApplyPen;
FCanvas.EndPath();
end;
procedure TFPVectorialDrawer.Polyline(
const APoints: array of TPoint; AStartIndex, ANumPts: Integer);
var
i: Integer;
begin
if ANumPts <= 0 then exit;
MoveTo(APoints[AStartIndex]);
for i := 1 to ANumPts - 1 do
with APoints[i + AStartIndex] do
AddLine(X, Y);
ApplyPen;
FCanvas.EndPath();
end;
procedure TFPVectorialDrawer.PrepareSimplePen(AColor: TChartColor);
begin
FPenColor := FChartColorToFPColorFunc(AColor);
FPenStyle := psSolid;
FPenWidth := 1;
end;
procedure TFPVectorialDrawer.PutPixel(AX, AY: Integer; AColor: TChartColor);
const
d = 0.2;
var
pencol: TFPColor;
penSty: TFPPenStyle;
brushCol: TFPColor;
brushSty: TFPBrushStyle;
begin
penCol := FPenColor;
penSty := FPenStyle;
brushCol := FBrushColor;
brushSty := FBrushStyle;
SetPenParams(psSolid, AColor);
SetBrushParams(bsSolid, AColor);
AY := InvertY(AY);
FCanvas.StartPath;
FCanvas.AddMoveToPath(AX-d, AY-d);
FCanvas.AddLineToPath(AX-d, AY+d);
FCanvas.AddLineTopath(AX+d, AY+d);
FCanvas.AddLineToPath(AX+d, AY-d);
FCanvas.AddLineToPath(AX-d, AY-d);
ApplyBrush;
ApplyPen;
FCanvas.EndPath;
FPenColor := penCol;
FPenStyle := penSty;
FBrushColor := brushCol;
FBrushStyle := brushSty;
end;
procedure TFPVectorialDrawer.RadialPie(
AX1, AY1, AX2, AY2: Integer; AStartAngle16Deg, AAngleLength16Deg: Integer);
var
e: TEllipse;
p: TPointArray;
begin
e.InitBoundingBox(AX1, AY1, AX2, AY2);
p := e.TesselateRadialPie(
Deg16ToRad(AStartAngle16Deg), Deg16ToRad(AAngleLength16Deg), 4);
Polygon(p, 0, Length(p));
end;
procedure TFPVectorialDrawer.Rectangle(AX1, AY1, AX2, AY2: Integer);
begin
MoveTo(AX1, AY1);
AddLine(AX2, AY1);
AddLine(AX2, AY2);
AddLine(AX1, AY2);
AddLine(AX1, AY1);
ApplyBrush;
ApplyPen;
FCanvas.EndPath();
end;
procedure TFPVectorialDrawer.Rectangle(const ARect: TRect);
begin
with ARect do
Rectangle(Left, Top, Right, Bottom);
end;
procedure TFPVectorialDrawer.ResetFont;
begin
FFont.Orientation := 0;
end;
procedure TFPVectorialDrawer.SetBrush(ABrush: TFPCustomBrush);
begin
if ABrush is TBrush then
SetBrushColor(TBrush(ABrush).Color)
else
FBrushColor := ABrush.FPColor;
FBrushStyle := ABrush.Style;
end;
procedure TFPVectorialDrawer.SetBrushColor(AColor: TChartColor);
begin
FBrushColor := FChartColorToFPColorFunc(AColor);
end;
procedure TFPVectorialDrawer.SetBrushParams(
AStyle: TFPBrushStyle; AColor: TChartColor);
begin
SetBrushColor(AColor);
FBrushStyle := AStyle;
end;
procedure TFPVectorialDrawer.SetFont(AFont: TFPCustomFont);
var
style: TFreeTypeStyles;
begin
// *** FPVectorial font ***
if SameText(AFont.Name, 'default') then
FFont.Name := 'Arial' // FIXME: Find font in FreeType FontCollection
else
FFont.Name := AFont.Name;
FFont.Size := IfThen(AFont.Size = 0, DEFAULT_FONT_SIZE, AFont.Size);
FFont.Color := AFont.FPColor;
FFont.Orientation := FGetFontOrientationFunc(AFont);
FFont.Bold := AFont.Bold;
FFont.Italic := AFont.Italic;
FFont.Underline := AFont.Underline;
FFont.Strikethrough := AFont.Strikethrough;
// *** FreeType Font (for metrics only) ***
style := [];
if AFont.Bold then Include(style, ftsBold);
if AFont.Italic then Include(style, ftsItalic);
// create a new freetype font if not yet loaded.
if (FFTFont = nil) or (FFTFont.Family <> AFont.Name) or (FFTFont.Style <> style) then
begin
FreeAndNil(FFTFont);
FFTFont := LoadFont(FFont.Name, style);
end;
if FFTFont <> nil then begin
// Set the requested font attributes
FFTFont.SizeInPixels := IfThen(AFont.Size = 0, DEFAULT_FONT_SIZE, AFont.Size);
// This should be "SizeInPoints" - but then the font is too small... Strange...
FFTFont.UnderlineDecoration := AFont.Underline;
FFTFont.StrikeoutDecoration := AFont.StrikeThrough;
FFTFont.Hinted := true;
FFTFont.Quality := grqHighQuality;
FFontHeight := round(FFTFont.TextHeight('Tg'));
end else
FFontHeight := FFont.Size;
end;
procedure TFPVectorialDrawer.SetPen(APen: TFPCustomPen);
begin
if APen is TPen then
FPenColor := FChartColorToFPColorFunc(TPen(APen).Color)
else
FPenColor := APen.FPColor;
FPenStyle := APen.Style;
FPenWidth := APen.Width;
end;
procedure TFPVectorialDrawer.SetPenParams(
AStyle: TFPPenStyle; AColor: TChartColor);
begin
FPenStyle := AStyle;
FPenColor := FChartColorToFPColorFunc(AColor);
end;
function TFPVectorialDrawer.SimpleTextExtent(const AText: String): TPoint;
begin
if FFTFont <> nil then
begin
Result.X := Round(FFTFont.TextWidth(AText));
Result.Y := FFontHeight;
end else
begin
Result.X := FFont.Size * Length(AText) * 2 div 3;
Result.Y := FFont.Size;
end;
end;
type
TFreeTypeFontOpener = class(TFreeTypeFont);
procedure TFPVectorialDrawer.SimpleTextOut(
AX, AY: Integer; const AText: String);
var
txt: TvText;
p: TPoint;
dy: Integer;
begin
// FPVectorial uses lower-left instead of upper-left corner as text start.
if FFTFont <> nil then
dy := round(TFreeTypeFontOpener(FFTFont).GetAscent)
else
dy := FFont.Size;
p := RotatePoint(Point(0, -dy), DegToRad(FFont.Orientation)) + Point(AX, InvertY(AY));
txt := FCanvas.AddText(p.X, p.Y, 0, AText);
txt.Font := FFont;
end;
end.
|