| 1 |
unit Img32.SVG.Path; |
| 2 |
|
| 3 |
(******************************************************************************* |
| 4 |
* Author : Angus Johnson * |
| 5 |
* Version : 4.7 * |
| 6 |
* Date : 6 January 2025 * |
| 7 |
* Website : http://www.angusj.com * |
| 8 |
* Copyright : Angus Johnson 2019-2025 * |
| 9 |
* * |
| 10 |
* Purpose : Essential structures and functions to read SVG Path elements * |
| 11 |
* * |
| 12 |
* License : Use, modification & distribution is subject to * |
| 13 |
* Boost Software License Ver 1 * |
| 14 |
* http://www.boost.org/LICENSE_1_0.txt * |
| 15 |
*******************************************************************************) |
| 16 |
|
| 17 |
interface |
| 18 |
|
| 19 |
{$I Img32.inc} |
| 20 |
|
| 21 |
uses |
| 22 |
SysUtils, Classes, Types, Math, |
| 23 |
{$IFDEF XPLAT_GENERICS} Generics.Collections, Generics.Defaults,{$ENDIF} |
| 24 |
Img32, Img32.SVG.Core, Img32.Vector, Img32.Text; |
| 25 |
|
| 26 |
{$IFDEF ZEROBASEDSTR} |
| 27 |
{$ZEROBASEDSTRINGS OFF} |
| 28 |
{$ENDIF} |
| 29 |
|
| 30 |
type |
| 31 |
|
| 32 |
TSvgPathSegType = |
| 33 |
(stUnknown, stMove, stLine, stHorz, stVert, stArc, |
| 34 |
stQBezier, stCBezier, stQSpline, stCSpline, stClose); |
| 35 |
|
| 36 |
TArcInfo = record |
| 37 |
rec : TRectD; |
| 38 |
startPos : TPointD; |
| 39 |
endPos : TPointD; |
| 40 |
rectAngle : double; |
| 41 |
sweepClockW : Boolean; |
| 42 |
end; |
| 43 |
TArcInfos = array of TArcInfo; |
| 44 |
|
| 45 |
TSvgPath = class; |
| 46 |
TSvgSubPath = class; |
| 47 |
|
| 48 |
TSvgPathSeg = class |
| 49 |
private |
| 50 |
fParent : TSvgSubPath; |
| 51 |
fOwner : TSvgPath; |
| 52 |
fIdx : integer; |
| 53 |
fFirstPt : TPointD; |
| 54 |
fFlatPath : TPathD; |
| 55 |
fSegType : TSvgPathSegType; |
| 56 |
fCtrlPts : TPathD; |
| 57 |
fExtend : integer; |
| 58 |
protected |
| 59 |
procedure Changed; {$IFDEF INLINE} inline; {$ENDIF} |
| 60 |
procedure RequireFlattened; virtual; |
| 61 |
function GetFlattened: TPathD; overload; |
| 62 |
procedure GetFlattened2(var Result: TPathD); overload; |
| 63 |
procedure GetFlattenedInternal; virtual; abstract; |
| 64 |
procedure Scale(value: double); virtual; |
| 65 |
function DescaleAndOffset(const pt: TPointD): TPointD; overload; |
| 66 |
function DescaleAndOffset(const p: TPathD): TPathD; overload; |
| 67 |
procedure SetCtrlPts(const pts: TPathD); virtual; |
| 68 |
public |
| 69 |
constructor Create(parent: TSvgSubPath; |
| 70 |
idx: integer; const firstPt : TPointD); virtual; |
| 71 |
function GetCtrlBounds: TRectD; virtual; |
| 72 |
function GetOnPathCtrlPts: TPathD; virtual; |
| 73 |
procedure Offset(dx, dy: double); virtual; |
| 74 |
function GetStringDef(relative: Boolean; decimalPrec: integer): string; virtual; |
| 75 |
function ExtendSeg(const pts: TPathD): Boolean; virtual; |
| 76 |
|
| 77 |
property Parent : TSvgSubPath read fParent; |
| 78 |
property Owner : TSvgPath read fOwner; |
| 79 |
property CtrlPts : TPathD read fCtrlPts write SetCtrlPts; |
| 80 |
property FirstPt : TPointD read fFirstPt; |
| 81 |
property FlatPath : TPathD read GetFlattened; |
| 82 |
property Index : integer read fIdx; |
| 83 |
property SegType : TSvgPathSegType read fSegType; |
| 84 |
end; |
| 85 |
|
| 86 |
TSvgStraightSeg = class(TSvgPathSeg) |
| 87 |
protected |
| 88 |
procedure GetFlattenedInternal; override; |
| 89 |
end; |
| 90 |
|
| 91 |
TSvgCurvedSeg = class(TSvgPathSeg) |
| 92 |
protected |
| 93 |
pendingScale: double; |
| 94 |
procedure RequireFlattened; override; |
| 95 |
function GetPreviousCtrlPt: TPointD; |
| 96 |
public |
| 97 |
function GetLastCtrlPt: TPointD; virtual; |
| 98 |
constructor Create(parent: TSvgSubPath; idx: integer; |
| 99 |
const firstPt : TPointD); override; |
| 100 |
end; |
| 101 |
|
| 102 |
TSvgASegment = class(TSvgCurvedSeg) |
| 103 |
private |
| 104 |
fRectTop : Boolean; |
| 105 |
fRectLeft : Boolean; |
| 106 |
fArcInfo : TArcInfo; |
| 107 |
procedure SetArcInfo(ai: TArcInfo); |
| 108 |
procedure GetRectBtnPoints(out pt1, pt2, pt3: TPointD); |
| 109 |
procedure SetCtrlPtsFromArcInfo; |
| 110 |
protected |
| 111 |
procedure SetCtrlPts(const ctrlPts: TPathD); override; |
| 112 |
procedure GetFlattenedInternal; override; |
| 113 |
procedure Scale(value: double); override; |
| 114 |
public |
| 115 |
public |
| 116 |
constructor Create(parent: TSvgSubPath; idx: integer; |
| 117 |
const firstPt : TPointD); override; |
| 118 |
procedure Offset(dx, dy: double); override; |
| 119 |
procedure ReverseArc; |
| 120 |
function GetStartAngle: double; |
| 121 |
function GetEndAngle: double; |
| 122 |
function GetStringDef(relative: Boolean; decimalPrec: integer): string; override; |
| 123 |
property ArcInfo: TArcInfo read fArcInfo write SetArcInfo; |
| 124 |
property IsLeftCtrl: Boolean read fRectLeft; |
| 125 |
property IsTopCtrl: Boolean read fRectTop; |
| 126 |
end; |
| 127 |
|
| 128 |
TSvgCSegment = class(TSvgCurvedSeg) |
| 129 |
protected |
| 130 |
procedure GetFlattenedInternal; override; |
| 131 |
public |
| 132 |
constructor Create(parent: TSvgSubPath; idx: integer; |
| 133 |
const firstPt : TPointD); override; |
| 134 |
function GetOnPathCtrlPts: TPathD; override; |
| 135 |
function GetStringDef(relative: Boolean; decimalPrec: integer): string; override; |
| 136 |
end; |
| 137 |
|
| 138 |
TSvgHSegment = class(TSvgStraightSeg) |
| 139 |
public |
| 140 |
constructor Create(parent: TSvgSubPath; idx: integer; |
| 141 |
const firstPt : TPointD); override; |
| 142 |
function GetStringDef(relative: Boolean; decimalPrec: integer): string; override; |
| 143 |
end; |
| 144 |
|
| 145 |
TSvgLSegment = class(TSvgStraightSeg) |
| 146 |
public |
| 147 |
constructor Create(parent: TSvgSubPath; idx: integer; |
| 148 |
const firstPt : TPointD); override; |
| 149 |
function GetStringDef(relative: Boolean; decimalPrec: integer): string; override; |
| 150 |
end; |
| 151 |
|
| 152 |
TSvgQSegment = class(TSvgCurvedSeg) |
| 153 |
protected |
| 154 |
procedure GetFlattenedInternal; override; |
| 155 |
public |
| 156 |
constructor Create(parent: TSvgSubPath; idx: integer; |
| 157 |
const firstPt : TPointD); override; |
| 158 |
function GetOnPathCtrlPts: TPathD; override; |
| 159 |
function GetStringDef(relative: Boolean; decimalPrec: integer): string; override; |
| 160 |
end; |
| 161 |
|
| 162 |
TSvgSSegment = class(TSvgCurvedSeg) |
| 163 |
protected |
| 164 |
procedure GetFlattenedInternal; override; |
| 165 |
public |
| 166 |
constructor Create(parent: TSvgSubPath; idx: integer; |
| 167 |
const firstPt : TPointD); override; |
| 168 |
function GetOnPathCtrlPts: TPathD; override; |
| 169 |
function GetStringDef(relative: Boolean; decimalPrec: integer): string; override; |
| 170 |
end; |
| 171 |
|
| 172 |
TSvgTSegment = class(TSvgCurvedSeg) |
| 173 |
protected |
| 174 |
procedure GetFlattenedInternal; override; |
| 175 |
public |
| 176 |
constructor Create(parent: TSvgSubPath; idx: integer; |
| 177 |
const firstPt : TPointD); override; |
| 178 |
function GetLastCtrlPt: TPointD; override; |
| 179 |
function GetStringDef(relative: Boolean; decimalPrec: integer): string; override; |
| 180 |
end; |
| 181 |
|
| 182 |
TSvgVSegment = class(TSvgStraightSeg) |
| 183 |
public |
| 184 |
constructor Create(parent: TSvgSubPath; idx: integer; |
| 185 |
const firstPt : TPointD); override; |
| 186 |
function GetStringDef(relative: Boolean; decimalPrec: integer): string; override; |
| 187 |
end; |
| 188 |
|
| 189 |
TSvgZSegment = class(TSvgStraightSeg) |
| 190 |
public |
| 191 |
constructor Create(parent: TSvgSubPath; idx: integer; |
| 192 |
const firstPt : TPointD); override; |
| 193 |
function GetStringDef(relative: Boolean; decimalPrec: integer): string; override; |
| 194 |
end; |
| 195 |
|
| 196 |
TSvgSegmentClass = class of TSvgPathSeg; |
| 197 |
|
| 198 |
TSvgSubPath = class |
| 199 |
private |
| 200 |
fParent : TSvgPath; |
| 201 |
fSegs : array of TSvgPathSeg; |
| 202 |
fPendingScale : double; |
| 203 |
fPathOffset : TPointD; |
| 204 |
fSegsCount : integer; |
| 205 |
function GetCount: integer; |
| 206 |
function GetSeg(index: integer): TSvgPathSeg; |
| 207 |
function AddSeg(segType: TSvgPathSegType; |
| 208 |
const startPt: TPointD; const pts: TPathD): TSvgPathSeg; |
| 209 |
protected |
| 210 |
procedure GrowSegs; |
| 211 |
procedure SegsLoaded; |
| 212 |
procedure InitSegs(Capacity: Integer); |
| 213 |
public |
| 214 |
isClosed : Boolean; |
| 215 |
constructor Create(parent: TSvgPath); |
| 216 |
destructor Destroy; override; |
| 217 |
procedure Clear; |
| 218 |
procedure Offset(dx, dy: double); |
| 219 |
function GetFirstPt: TPointD; |
| 220 |
function GetLastPt: TPointD; |
| 221 |
function GetBounds: TRectD; |
| 222 |
|
| 223 |
function AddASeg(const startPt, endPt: TPointD; const rect: TRectD; |
| 224 |
angle: double; isClockwise: Boolean): TSvgASegment; |
| 225 |
function AddCSeg(const startPt: TPointD; const pts: TPathD): TSvgCSegment; |
| 226 |
function AddHSeg(const startPt: TPointD; const pts: TPathD): TSvgHSegment; |
| 227 |
function AddLSeg(const startPt: TPointD; const pts: TPathD): TSvgLSegment; |
| 228 |
function AddQSeg(const startPt: TPointD; const pts: TPathD): TSvgQSegment; |
| 229 |
function AddSSeg(const startPt: TPointD; const pts: TPathD): TSvgSSegment; |
| 230 |
function AddTSeg(const startPt: TPointD; const pts: TPathD): TSvgTSegment; |
| 231 |
function AddVSeg(const startPt: TPointD; const pts: TPathD): TSvgVSegment; |
| 232 |
function AddZSeg(const endPt, firstPt: TPointD): TSvgZSegment; |
| 233 |
|
| 234 |
function GetLastSeg: TSvgPathSeg; |
| 235 |
function DeleteLastSeg: Boolean; |
| 236 |
//pendingScale: allows 'flattening' to occur with curve precision |
| 237 |
//that will accommodate future (anticipated) scaling. |
| 238 |
//Eg: a native image is 32x32 px but will be displayed at 512x512, |
| 239 |
//so pendingScale should be 16 to ensure a smooth curve |
| 240 |
function GetFlattenedPath(pendingScale: double = 1.0): TPathD; |
| 241 |
//GetSimplePath - only used for markers |
| 242 |
function GetSimplePath: TPathD; |
| 243 |
function GetMoveStrDef(relative: Boolean; decimalPrec: integer): string; |
| 244 |
function GetStringDef(relative: Boolean; decimalPrec: integer): string; |
| 245 |
property Count : integer read GetCount; |
| 246 |
property Parent : TSvgPath read fParent; |
| 247 |
property PathOffset : TPointD read fPathOffset; |
| 248 |
property Seg[index: integer]: TSvgPathSeg read GetSeg; default; |
| 249 |
end; |
| 250 |
|
| 251 |
TSvgPath = class |
| 252 |
private |
| 253 |
fPathScale : double; |
| 254 |
fPathOffs : TPointD; |
| 255 |
fSubPaths: array of TSvgSubPath; |
| 256 |
function GetPath(index: integer): TSvgSubPath; |
| 257 |
function GetBounds: TRectD; |
| 258 |
function GetControlBounds: TRectD; |
| 259 |
function GetCount: integer; |
| 260 |
public |
| 261 |
destructor Destroy; override; |
| 262 |
procedure Clear; |
| 263 |
procedure Parse(const value: UTF8String); |
| 264 |
procedure ScaleAndOffset(scale: double; dx, dy: integer); |
| 265 |
function GetStringDef(relative: Boolean; decimalPrec: integer): string; |
| 266 |
|
| 267 |
function AddPath(SegsCapacity: Integer = 0): TSvgSubPath; |
| 268 |
procedure DeleteSubPath(subPath: TSvgSubPath); |
| 269 |
property Bounds: TRectD read GetBounds; |
| 270 |
property CtrlBounds: TRectD read GetControlBounds; |
| 271 |
property Count: integer read GetCount; |
| 272 |
property Path[index: integer]: TSvgSubPath read GetPath; default; |
| 273 |
property Scale: double read fPathScale; |
| 274 |
property Offset : TPointD read fPathOffs; |
| 275 |
end; |
| 276 |
|
| 277 |
UTF8Strings = array of UTF8String; |
| 278 |
|
| 279 |
function GetSvgArcInfoRect(const p1, p2: TPointD; |
| 280 |
radii: TPointD; phi_rads: double; fA, fS: boolean): TRectD; |
| 281 |
|
| 282 |
implementation |
| 283 |
|
| 284 |
resourcestring |
| 285 |
rsSvgPathRangeError = 'TSvgPath.GetPath range error'; |
| 286 |
rsSvgSubPathRangeError = 'TSvgSubPath.GetSeg range error'; |
| 287 |
|
| 288 |
//------------------------------------------------------------------------------ |
| 289 |
// Miscellaneous functions ... |
| 290 |
//------------------------------------------------------------------------------ |
| 291 |
|
| 292 |
function CheckPathLen(const p: TPathD; modLength: integer): TPathD; |
| 293 |
var |
| 294 |
i, len: integer; |
| 295 |
begin |
| 296 |
Result := nil; |
| 297 |
len := Length(p); |
| 298 |
if (len < modLength) then Exit; |
| 299 |
Result := p; |
| 300 |
i := len mod modLength; |
| 301 |
SetLength(Result, len -i); |
| 302 |
end; |
| 303 |
//------------------------------------------------------------------------------ |
| 304 |
|
| 305 |
function TrimTrailingZeros(const floatValStr: string): string; |
| 306 |
var |
| 307 |
i: integer; |
| 308 |
begin |
| 309 |
Result := floatValStr; |
| 310 |
if Pos('.', floatValStr) = 0 then Exit; |
| 311 |
i := Length(Result); |
| 312 |
while Result[i] = '0' do dec(i); |
| 313 |
if Result[i] = '.' then dec(i); |
| 314 |
SetLength(Result, i); |
| 315 |
end; |
| 316 |
//------------------------------------------------------------------------------ |
| 317 |
|
| 318 |
function AsIntStr(val: double): string; |
| 319 |
begin |
| 320 |
Result := Format('%1.0n ', [val]); |
| 321 |
end; |
| 322 |
//------------------------------------------------------------------------------ |
| 323 |
|
| 324 |
function AsFloatStr(val: double; precision: integer): string; |
| 325 |
begin |
| 326 |
Result := TrimTrailingZeros(Format('%1.*f', [precision, val])); |
| 327 |
end; |
| 328 |
//------------------------------------------------------------------------------ |
| 329 |
|
| 330 |
function AsCoordStr(pt: TPointD; |
| 331 |
const relPt: TPointD; relative: Boolean; precision: integer): string; |
| 332 |
var |
| 333 |
s1, s2: string; |
| 334 |
begin |
| 335 |
if relative then |
| 336 |
begin |
| 337 |
pt.X := pt.X - relPt.X; |
| 338 |
pt.Y := pt.Y - relPt.Y; |
| 339 |
end; |
| 340 |
s1 := TrimTrailingZeros(Format('%1.*f', [precision, pt.x])); |
| 341 |
s2 := TrimTrailingZeros(Format('%1.*f', [precision, pt.y])); |
| 342 |
Result := s1 + ',' + s2 + ' '; |
| 343 |
end; |
| 344 |
//------------------------------------------------------------------------------ |
| 345 |
|
| 346 |
function GetSingleDigit(var c, endC: PUTF8Char; |
| 347 |
out digit: integer): Boolean; |
| 348 |
var |
| 349 |
cc: PUTF8Char; |
| 350 |
ch: UTF8Char; |
| 351 |
begin |
| 352 |
cc := SkipBlanksAndComma(c, endC); |
| 353 |
Result := cc < endC; |
| 354 |
if not Result then |
| 355 |
begin |
| 356 |
c := cc; |
| 357 |
Exit; |
| 358 |
end; |
| 359 |
ch := cc^; |
| 360 |
Result := (ch >= '0') and (ch <= '9'); |
| 361 |
if not Result then Exit; |
| 362 |
digit := Ord(ch) - Ord('0'); |
| 363 |
c := cc + 1; |
| 364 |
end; |
| 365 |
//------------------------------------------------------------------------------ |
| 366 |
|
| 367 |
const |
| 368 |
SegTypeMap: array['A'..'Z'] of TSvgPathSegType = ( |
| 369 |
stArc, // A |
| 370 |
stUnknown, // B |
| 371 |
stCBezier, // C |
| 372 |
stUnknown, // D |
| 373 |
stUnknown, // E |
| 374 |
stUnknown, // F |
| 375 |
stUnknown, // G |
| 376 |
stHorz, // H |
| 377 |
stUnknown, // I |
| 378 |
stUnknown, // J |
| 379 |
stUnknown, // K |
| 380 |
stLine, // L |
| 381 |
stMove, // M |
| 382 |
stUnknown, // N |
| 383 |
stUnknown, // O |
| 384 |
stUnknown, // P |
| 385 |
stQBezier, // Q |
| 386 |
stUnknown, // R |
| 387 |
stCSpline, // S |
| 388 |
stQSpline, // T |
| 389 |
stUnknown, // U |
| 390 |
stVert, // V |
| 391 |
stUnknown, // W |
| 392 |
stUnknown, // X |
| 393 |
stUnknown, // Y |
| 394 |
stClose // Z |
| 395 |
); |
| 396 |
|
| 397 |
function GetSegType(var c, endC: PUTF8Char; out isRelative: Boolean): TSvgPathSegType; |
| 398 |
var |
| 399 |
ch: UTF8Char; |
| 400 |
begin |
| 401 |
Result := stUnknown; |
| 402 |
if not SkipBlanks(c, endC) then Exit; |
| 403 |
ch := c^; |
| 404 |
case ch of |
| 405 |
'a'..'z': Result := SegTypeMap[UTF8Char(Byte(ch) and not $20)]; |
| 406 |
'A'..'Z': Result := SegTypeMap[ch]; |
| 407 |
end; |
| 408 |
if Result = stUnknown then Exit; |
| 409 |
isRelative := ch >= 'a'; |
| 410 |
inc(c); |
| 411 |
end; |
| 412 |
//------------------------------------------------------------------------------ |
| 413 |
|
| 414 |
function Parse2Num(var c, endC: PUTF8Char; |
| 415 |
out pt: TPointD; const relPt: TPointD): Boolean; |
| 416 |
begin |
| 417 |
Result := ParseNextNum(c, endC, true, pt.X) and |
| 418 |
ParseNextNum(c, endC, true, pt.Y); |
| 419 |
if not Result or (relPt.X = InvalidD) then Exit; |
| 420 |
pt.X := pt.X + relPt.X; |
| 421 |
pt.Y := pt.Y + relPt.Y; |
| 422 |
end; |
| 423 |
//------------------------------------------------------------------------------ |
| 424 |
|
| 425 |
function Parse1Num(var c: PUTF8Char; endC: PUTF8Char; |
| 426 |
out val: double; relVal: double): Boolean; |
| 427 |
begin |
| 428 |
Result := ParseNextNum(c, endC, true, val); |
| 429 |
if Result and (relVal <> InvalidD) then |
| 430 |
val := val + relVal; |
| 431 |
end; |
| 432 |
|
| 433 |
//------------------------------------------------------------------------------ |
| 434 |
// TSvgPathSeg |
| 435 |
//------------------------------------------------------------------------------ |
| 436 |
|
| 437 |
constructor TSvgPathSeg.Create(parent: TSvgSubPath; |
| 438 |
idx: integer; const firstPt : TPointD); |
| 439 |
begin |
| 440 |
Self.fParent := parent; |
| 441 |
Self.fOwner := parent.fParent; |
| 442 |
Self.fIdx := idx; |
| 443 |
Self.fFirstPt := firstPt; |
| 444 |
end; |
| 445 |
//------------------------------------------------------------------------------ |
| 446 |
|
| 447 |
procedure TSvgPathSeg.Scale(value: double); |
| 448 |
begin |
| 449 |
if (value <> 0) and (value <> 1) then |
| 450 |
begin |
| 451 |
fCtrlPts := ScalePath(fCtrlPts, value); |
| 452 |
fFirstPt := ScalePoint(fFirstPt, value); |
| 453 |
Changed; |
| 454 |
end; |
| 455 |
end; |
| 456 |
//------------------------------------------------------------------------------ |
| 457 |
|
| 458 |
function TSvgPathSeg.DescaleAndOffset(const pt: TPointD): TPointD; |
| 459 |
begin |
| 460 |
Result := TranslatePoint(pt, -parent.PathOffset.X, -parent.PathOffset.Y); |
| 461 |
Result := ScalePoint(Result, 1/Owner.Scale); |
| 462 |
end; |
| 463 |
//------------------------------------------------------------------------------ |
| 464 |
|
| 465 |
function TSvgPathSeg.DescaleAndOffset(const p: TPathD): TPathD; |
| 466 |
begin |
| 467 |
Result := TranslatePath(p, -parent.PathOffset.X, -parent.PathOffset.Y); |
| 468 |
Result := ScalePath(Result, 1/Owner.Scale); |
| 469 |
end; |
| 470 |
//------------------------------------------------------------------------------ |
| 471 |
|
| 472 |
procedure TSvgPathSeg.Offset(dx, dy: double); |
| 473 |
begin |
| 474 |
fFirstPt := TranslatePoint(fFirstPt, dx, dy); |
| 475 |
fCtrlPts := TranslatePath(fCtrlPts, dx, dy); |
| 476 |
Changed; |
| 477 |
end; |
| 478 |
//------------------------------------------------------------------------------ |
| 479 |
|
| 480 |
procedure TSvgPathSeg.SetCtrlPts(const pts: TPathD); |
| 481 |
begin |
| 482 |
fCtrlPts := pts; |
| 483 |
Changed; |
| 484 |
end; |
| 485 |
//------------------------------------------------------------------------------ |
| 486 |
|
| 487 |
function TSvgPathSeg.ExtendSeg(const pts: TPathD): Boolean; |
| 488 |
var |
| 489 |
len: integer; |
| 490 |
begin |
| 491 |
len := Length(pts); |
| 492 |
Result := (len <> 0) and (fExtend <> 0) and (len mod fExtend = 0); |
| 493 |
if Result then ConcatPaths(fCtrlPts, pts); |
| 494 |
end; |
| 495 |
//------------------------------------------------------------------------------ |
| 496 |
|
| 497 |
function TSvgPathSeg.GetCtrlBounds: TRectD; |
| 498 |
begin |
| 499 |
Result := GetBoundsD(PrePendPoint(fFirstPt, CtrlPts)); |
| 500 |
end; |
| 501 |
//------------------------------------------------------------------------------ |
| 502 |
|
| 503 |
procedure TSvgPathSeg.Changed; |
| 504 |
begin |
| 505 |
if fFlatPath <> nil then |
| 506 |
fFlatPath := nil; // DynArrayClear |
| 507 |
end; |
| 508 |
//------------------------------------------------------------------------------ |
| 509 |
procedure TSvgPathSeg.RequireFlattened; |
| 510 |
begin |
| 511 |
if fFlatPath = nil then |
| 512 |
GetFlattenedInternal; |
| 513 |
end; |
| 514 |
|
| 515 |
//------------------------------------------------------------------------------ |
| 516 |
function TSvgPathSeg.GetFlattened: TPathD; |
| 517 |
begin |
| 518 |
RequireFlattened; |
| 519 |
Result := fFlatPath; |
| 520 |
end; |
| 521 |
//------------------------------------------------------------------------------ |
| 522 |
|
| 523 |
procedure TSvgPathSeg.GetFlattened2(var Result: TPathD); |
| 524 |
begin // uses less DynArrayAsg and DynArrayClear calls |
| 525 |
RequireFlattened; |
| 526 |
Result := fFlatPath; |
| 527 |
end; |
| 528 |
//------------------------------------------------------------------------------ |
| 529 |
|
| 530 |
function TSvgPathSeg.GetOnPathCtrlPts: TPathD; |
| 531 |
begin |
| 532 |
Result := fCtrlPts; |
| 533 |
end; |
| 534 |
//------------------------------------------------------------------------------ |
| 535 |
|
| 536 |
function TSvgPathSeg.GetStringDef(relative: Boolean; decimalPrec: integer): string; |
| 537 |
begin |
| 538 |
Result := ''; |
| 539 |
end; |
| 540 |
|
| 541 |
//------------------------------------------------------------------------------ |
| 542 |
// TSvgStraightSeg |
| 543 |
//------------------------------------------------------------------------------ |
| 544 |
|
| 545 |
procedure TSvgStraightSeg.GetFlattenedInternal; |
| 546 |
begin |
| 547 |
PrePendPoint(fFirstPt, fCtrlPts, fFlatPath); |
| 548 |
end; |
| 549 |
|
| 550 |
//------------------------------------------------------------------------------ |
| 551 |
// TSvgCurvedSeg |
| 552 |
//------------------------------------------------------------------------------ |
| 553 |
|
| 554 |
constructor TSvgCurvedSeg.Create(parent: TSvgSubPath; idx: integer; |
| 555 |
const firstPt : TPointD); |
| 556 |
begin |
| 557 |
inherited; |
| 558 |
pendingScale := 1.0; |
| 559 |
end; |
| 560 |
//------------------------------------------------------------------------------ |
| 561 |
|
| 562 |
procedure TSvgCurvedSeg.RequireFlattened; |
| 563 |
begin |
| 564 |
//if the image has been rendered previously at a lower resolution, then |
| 565 |
//redo the flattening otherwise curves my look very rough. |
| 566 |
if (pendingScale < Parent.fPendingScale) then |
| 567 |
begin |
| 568 |
pendingScale := Parent.fPendingScale; |
| 569 |
Changed; |
| 570 |
end; |
| 571 |
inherited RequireFlattened; |
| 572 |
end; |
| 573 |
//------------------------------------------------------------------------------ |
| 574 |
|
| 575 |
function TSvgCurvedSeg.GetLastCtrlPt: TPointD; |
| 576 |
begin |
| 577 |
Result := CtrlPts[High(CtrlPts) -1]; |
| 578 |
end; |
| 579 |
//------------------------------------------------------------------------------ |
| 580 |
|
| 581 |
function TSvgCurvedSeg.GetPreviousCtrlPt: TPointD; |
| 582 |
var |
| 583 |
UseParentLastCtrlPt: Boolean; |
| 584 |
begin |
| 585 |
UseParentLastCtrlPt := False; |
| 586 |
if fIdx > 0 then |
| 587 |
begin |
| 588 |
case fSegType of |
| 589 |
stQSpline: |
| 590 |
case fParent[fIdx -1].fSegType of |
| 591 |
stQBezier, stQSpline: UseParentLastCtrlPt := True; |
| 592 |
end; |
| 593 |
stCSpline: |
| 594 |
case fParent[fIdx -1].fSegType of |
| 595 |
stCBezier, stCSpline: UseParentLastCtrlPt := True; |
| 596 |
end; |
| 597 |
end; |
| 598 |
end; |
| 599 |
|
| 600 |
if UseParentLastCtrlPt then |
| 601 |
Result := TSvgCurvedSeg(fParent[fIdx -1]).GetLastCtrlPt |
| 602 |
else |
| 603 |
Result := fFirstPt; |
| 604 |
end; |
| 605 |
|
| 606 |
//------------------------------------------------------------------------------ |
| 607 |
// TSvgASegment |
| 608 |
//------------------------------------------------------------------------------ |
| 609 |
|
| 610 |
constructor TSvgASegment.Create(parent: TSvgSubPath; idx: integer; |
| 611 |
const firstPt : TPointD); |
| 612 |
begin |
| 613 |
inherited; |
| 614 |
fSegType := stArc; |
| 615 |
fExtend := 0; |
| 616 |
end; |
| 617 |
//------------------------------------------------------------------------------ |
| 618 |
|
| 619 |
procedure TSvgASegment.SetArcInfo(ai: TArcInfo); |
| 620 |
var |
| 621 |
dx, dy: double; |
| 622 |
begin |
| 623 |
//make sure that all the ai fields are valid, |
| 624 |
//otherwise adjust them and align with ai.startpos |
| 625 |
with fArcInfo do |
| 626 |
begin |
| 627 |
rec := ai.rec; |
| 628 |
rectAngle := ai.rectAngle; |
| 629 |
startPos := GetClosestPtOnRotatedEllipse(rec, rectAngle, ai.startPos); |
| 630 |
endPos := GetClosestPtOnRotatedEllipse(rec, rectAngle, ai.endPos); |
| 631 |
sweepClockW := ai.sweepClockW; |
| 632 |
if not PointsNearEqual(ai.startPos, startPos, 0.01) then |
| 633 |
begin |
| 634 |
dx := ai.startPos.X - startPos.X; |
| 635 |
dy := ai.startPos.Y - startPos.Y; |
| 636 |
TranslateRect(rec, dx, dy); |
| 637 |
startPos := ai.startPos; |
| 638 |
endPos := TranslatePoint(endPos, dx, dy); |
| 639 |
end; |
| 640 |
end; |
| 641 |
SetCtrlPtsFromArcInfo; |
| 642 |
Changed; |
| 643 |
end; |
| 644 |
//------------------------------------------------------------------------------ |
| 645 |
|
| 646 |
procedure TSvgASegment.GetRectBtnPoints(out pt1, pt2, pt3: TPointD); |
| 647 |
var |
| 648 |
d : double; |
| 649 |
pt, sp : TPointD; |
| 650 |
begin |
| 651 |
with fArcInfo do |
| 652 |
begin |
| 653 |
//keep rec oriented to the XY axis and rotate startpos |
| 654 |
sp := startPos; |
| 655 |
pt2 := rec.MidPoint; |
| 656 |
|
| 657 |
if rectAngle <> 0 then |
| 658 |
RotatePoint(sp, pt2, -rectAngle); |
| 659 |
|
| 660 |
pt := PointD(rec.Left, pt2.Y); |
| 661 |
pt3 := PointD(rec.Right, pt2.Y); |
| 662 |
|
| 663 |
d := DistanceSqrd(pt, sp) - DistanceSqrd(pt3, sp); |
| 664 |
if not ValueAlmostZero(d, 0.01) then |
| 665 |
fRectLeft := d > 0; |
| 666 |
if fRectLeft then |
| 667 |
pt1 := PointD(rec.Left, pt2.Y) else |
| 668 |
pt1 := PointD(rec.Right, pt2.Y); |
| 669 |
|
| 670 |
pt := PointD(pt2.X, rec.Top); |
| 671 |
pt3 := PointD(pt2.X, rec.Bottom); |
| 672 |
d := DistanceSqrd(pt, sp) - DistanceSqrd(pt3, sp); |
| 673 |
if not ValueAlmostZero(d, 0.01) then fRectTop := d > 0; |
| 674 |
if fRectTop then |
| 675 |
pt3 := PointD(pt2.X, rec.Top) else |
| 676 |
pt3 := PointD(pt2.X, rec.Bottom); |
| 677 |
|
| 678 |
RotatePoint(pt1, pt2, rectAngle); |
| 679 |
RotatePoint(pt3, pt2, rectAngle); |
| 680 |
end; |
| 681 |
end; |
| 682 |
//------------------------------------------------------------------------------ |
| 683 |
|
| 684 |
procedure TSvgASegment.SetCtrlPtsFromArcInfo; |
| 685 |
begin |
| 686 |
NewPointDArray(fCtrlPts, 5, True); |
| 687 |
with fArcInfo do |
| 688 |
begin |
| 689 |
fCtrlPts[0] := startPos; |
| 690 |
GetRectBtnPoints(fCtrlPts[1], fCtrlPts[2], fCtrlPts[3]); |
| 691 |
fCtrlPts[4] := endPos; |
| 692 |
end; |
| 693 |
Changed; |
| 694 |
end; |
| 695 |
//------------------------------------------------------------------------------ |
| 696 |
|
| 697 |
procedure TSvgASegment.GetFlattenedInternal; |
| 698 |
var |
| 699 |
a1,a2: double; |
| 700 |
p: TPathD; |
| 701 |
begin |
| 702 |
fFlatPath := nil; |
| 703 |
with fArcInfo do |
| 704 |
begin |
| 705 |
a1 := GetStartAngle; |
| 706 |
a2 := GetEndAngle; |
| 707 |
if not sweepClockW then |
| 708 |
begin |
| 709 |
p := Arc(rec, a2, a1, pendingScale); |
| 710 |
p := ReversePath(p); |
| 711 |
end else |
| 712 |
p := Arc(rec, a1, a2, pendingScale); |
| 713 |
if rectAngle <> 0 then |
| 714 |
p := RotatePath(p, rec.MidPoint, rectAngle); |
| 715 |
ConcatPaths(fFlatPath, p); |
| 716 |
end; |
| 717 |
end; |
| 718 |
//------------------------------------------------------------------------------ |
| 719 |
|
| 720 |
function TSvgASegment.GetStartAngle: double; |
| 721 |
begin |
| 722 |
with fArcInfo do |
| 723 |
Result := GetRotatedEllipticalAngleFromPoint(rec, rectAngle, startPos); |
| 724 |
end; |
| 725 |
//------------------------------------------------------------------------------ |
| 726 |
|
| 727 |
function TSvgASegment.GetEndAngle: double; |
| 728 |
begin |
| 729 |
with fArcInfo do |
| 730 |
Result := GetRotatedEllipticalAngleFromPoint(rec, rectAngle, endPos); |
| 731 |
end; |
| 732 |
//------------------------------------------------------------------------------ |
| 733 |
|
| 734 |
procedure TSvgASegment.ReverseArc; |
| 735 |
begin |
| 736 |
fArcInfo.sweepClockW := not fArcInfo.sweepClockW; |
| 737 |
Changed; |
| 738 |
end; |
| 739 |
//------------------------------------------------------------------------------ |
| 740 |
|
| 741 |
procedure TSvgASegment.Offset(dx, dy: double); |
| 742 |
begin |
| 743 |
inherited; // calls Changed |
| 744 |
with fArcInfo do |
| 745 |
begin |
| 746 |
TranslateRect(rec, dx, dy); |
| 747 |
startPos := TranslatePoint(startPos, dx, dy); |
| 748 |
endPos := TranslatePoint(endPos, dx, dy); |
| 749 |
end; |
| 750 |
end; |
| 751 |
//------------------------------------------------------------------------------ |
| 752 |
|
| 753 |
procedure TSvgASegment.Scale(value: Double); |
| 754 |
begin |
| 755 |
if (value = 0) or (value = 1) then Exit; |
| 756 |
inherited; // calls Changed |
| 757 |
with fArcInfo do |
| 758 |
begin |
| 759 |
rec := ScaleRect(rec, value); |
| 760 |
startPos := ScalePoint(startPos, value); |
| 761 |
endPos := ScalePoint(endPos, value); |
| 762 |
end; |
| 763 |
end; |
| 764 |
//------------------------------------------------------------------------------ |
| 765 |
|
| 766 |
procedure TSvgASegment.SetCtrlPts(const ctrlPts: TPathD); |
| 767 |
begin |
| 768 |
//SetCtrlPtsFromArcInfo; // calls Changed |
| 769 |
end; |
| 770 |
//------------------------------------------------------------------------------ |
| 771 |
|
| 772 |
function TSvgASegment.GetStringDef(relative: Boolean; decimalPrec: integer): string; |
| 773 |
var |
| 774 |
a, a1,a2: double; |
| 775 |
sp, ep: TPointD; |
| 776 |
begin |
| 777 |
with fArcInfo do |
| 778 |
begin |
| 779 |
if relative then Result := 'a ' else Result := 'A '; |
| 780 |
|
| 781 |
Result := Result + |
| 782 |
AsFloatStr(rec.Width *0.5 /Owner.Scale, decimalPrec) + ','; |
| 783 |
Result := Result + |
| 784 |
AsFloatStr(rec.Height *0.5 /Owner.Scale, decimalPrec) + ' '; |
| 785 |
//angle as degrees |
| 786 |
Result := Result + AsIntStr(RadToDeg(rectAngle)); |
| 787 |
|
| 788 |
a1 := GetStartAngle; |
| 789 |
a2 := GetEndAngle; |
| 790 |
|
| 791 |
//large arce and direction flags |
| 792 |
a := a2 - a1; |
| 793 |
if a < 0 then a := a + angle360; |
| 794 |
|
| 795 |
if sweepClockW then |
| 796 |
begin |
| 797 |
if a >= angle180 then |
| 798 |
Result := Result + '1 1 ' else |
| 799 |
Result := Result + '0 1 '; |
| 800 |
end else |
| 801 |
begin |
| 802 |
if a >= angle180 then |
| 803 |
Result := Result + '0 0 ' else |
| 804 |
Result := Result + '1 0 '; |
| 805 |
end; |
| 806 |
//descaled and de-offset end position |
| 807 |
ep := DescaleAndOffset(endPos); |
| 808 |
sp := DescaleAndOffset(startPos); |
| 809 |
Result := Result + AsCoordStr(ep, sp, relative, decimalPrec); |
| 810 |
end; |
| 811 |
end; |
| 812 |
|
| 813 |
//------------------------------------------------------------------------------ |
| 814 |
// TSvgCSegment |
| 815 |
//------------------------------------------------------------------------------ |
| 816 |
|
| 817 |
constructor TSvgCSegment.Create(parent: TSvgSubPath; idx: integer; |
| 818 |
const firstPt : TPointD); |
| 819 |
begin |
| 820 |
inherited; |
| 821 |
fSegType := stCBezier; |
| 822 |
fExtend := 3; |
| 823 |
end; |
| 824 |
//------------------------------------------------------------------------------ |
| 825 |
|
| 826 |
function TSvgCSegment.GetOnPathCtrlPts: TPathD; |
| 827 |
var |
| 828 |
i, len: integer; |
| 829 |
begin |
| 830 |
len := Length(fCtrlPts) div 3; |
| 831 |
NewPointDArray(Result, len, True); |
| 832 |
for i := 0 to High(Result) do |
| 833 |
Result[i] := fCtrlPts[i*3 +2]; |
| 834 |
end; |
| 835 |
//------------------------------------------------------------------------------ |
| 836 |
|
| 837 |
procedure TSvgCSegment.GetFlattenedInternal; |
| 838 |
var |
| 839 |
bt : double; |
| 840 |
p: TPathD; |
| 841 |
begin |
| 842 |
bt := BezierTolerance / pendingScale; |
| 843 |
p := CheckPathLen(fCtrlPts, 3); |
| 844 |
if p = nil then |
| 845 |
fFlatPath := nil else |
| 846 |
fFlatPath := FlattenCBezier(fFirstPt, p, bt); |
| 847 |
end; |
| 848 |
//------------------------------------------------------------------------------ |
| 849 |
|
| 850 |
function TSvgCSegment.GetStringDef(relative: Boolean; decimalPrec: integer): string; |
| 851 |
var |
| 852 |
i: integer; |
| 853 |
pt, relPt: TPointD; |
| 854 |
begin |
| 855 |
if relative then Result := 'c ' else Result := 'C '; |
| 856 |
relPt := DescaleAndOffset(fFirstPt); |
| 857 |
for i := 0 to High(fCtrlPts) do |
| 858 |
begin |
| 859 |
pt:= DescaleAndOffset(fCtrlPts[i]); |
| 860 |
Result := Result + AsCoordStr(pt, relPt, relative, decimalPrec); |
| 861 |
if relative and (i mod 3 = 2) then relPt := pt; |
| 862 |
end; |
| 863 |
end; |
| 864 |
|
| 865 |
//------------------------------------------------------------------------------ |
| 866 |
// TSvgHSegment |
| 867 |
//------------------------------------------------------------------------------ |
| 868 |
|
| 869 |
constructor TSvgHSegment.Create(parent: TSvgSubPath; idx: integer; |
| 870 |
const firstPt : TPointD); |
| 871 |
begin |
| 872 |
inherited; |
| 873 |
fSegType := stHorz; |
| 874 |
fExtend := 1; |
| 875 |
end; |
| 876 |
//------------------------------------------------------------------------------ |
| 877 |
|
| 878 |
function TSvgHSegment.GetStringDef(relative: Boolean; decimalPrec: integer): string; |
| 879 |
var |
| 880 |
i: integer; |
| 881 |
pt, relPt: TPointD; |
| 882 |
begin |
| 883 |
if relative then |
| 884 |
begin |
| 885 |
Result := 'h '; |
| 886 |
relPt := DescaleAndOffset(fFirstPt); |
| 887 |
for i := 0 to High(fCtrlPts) do |
| 888 |
begin |
| 889 |
pt := DescaleAndOffset(fCtrlPts[i]); |
| 890 |
Result := Result + AsFloatStr(pt.X - relPt.X, decimalPrec) + ' '; |
| 891 |
relPt := pt; |
| 892 |
end; |
| 893 |
end else |
| 894 |
begin |
| 895 |
Result := 'H '; |
| 896 |
for i := 0 to High(fCtrlPts) do |
| 897 |
begin |
| 898 |
pt := DescaleAndOffset(fCtrlPts[i]); |
| 899 |
Result := Result + AsFloatStr(pt.X, decimalPrec) + ' '; |
| 900 |
end; |
| 901 |
end; |
| 902 |
end; |
| 903 |
|
| 904 |
//------------------------------------------------------------------------------ |
| 905 |
// TSvgLSegment |
| 906 |
//------------------------------------------------------------------------------ |
| 907 |
|
| 908 |
constructor TSvgLSegment.Create(parent: TSvgSubPath; idx: integer; |
| 909 |
const firstPt : TPointD); |
| 910 |
begin |
| 911 |
inherited; |
| 912 |
fSegType := stLine; |
| 913 |
fExtend := 1; |
| 914 |
end; |
| 915 |
//------------------------------------------------------------------------------ |
| 916 |
|
| 917 |
function TSvgLSegment.GetStringDef(relative: Boolean; decimalPrec: integer): string; |
| 918 |
var |
| 919 |
i: integer; |
| 920 |
pt, relPt: TPointD; |
| 921 |
begin |
| 922 |
if relative then Result := 'l ' else Result := 'L '; |
| 923 |
relPt := DescaleAndOffset(fFirstPt); |
| 924 |
for i := 0 to High(fCtrlPts) do |
| 925 |
begin |
| 926 |
pt:= DescaleAndOffset(fCtrlPts[i]); |
| 927 |
Result := Result + AsCoordStr(pt, relPt, relative, decimalPrec); |
| 928 |
relPt := pt; |
| 929 |
end; |
| 930 |
end; |
| 931 |
|
| 932 |
//------------------------------------------------------------------------------ |
| 933 |
// TSvgQSegment |
| 934 |
//------------------------------------------------------------------------------ |
| 935 |
|
| 936 |
constructor TSvgQSegment.Create(parent: TSvgSubPath; idx: integer; |
| 937 |
const firstPt : TPointD); |
| 938 |
begin |
| 939 |
inherited; |
| 940 |
fSegType := stQBezier; |
| 941 |
fExtend := 2; |
| 942 |
end; |
| 943 |
//------------------------------------------------------------------------------ |
| 944 |
|
| 945 |
function TSvgQSegment.GetOnPathCtrlPts: TPathD; |
| 946 |
var |
| 947 |
i, len: integer; |
| 948 |
begin |
| 949 |
len := Length(fCtrlPts) div 2; |
| 950 |
NewPointDArray(Result, len, True); |
| 951 |
for i := 0 to High(Result) do |
| 952 |
Result[i] := fCtrlPts[i*2+1]; |
| 953 |
end; |
| 954 |
//------------------------------------------------------------------------------ |
| 955 |
|
| 956 |
procedure TSvgQSegment.GetFlattenedInternal; |
| 957 |
var |
| 958 |
bt : double; |
| 959 |
p: TPathD; |
| 960 |
begin |
| 961 |
bt := BezierTolerance / pendingScale; |
| 962 |
p := CheckPathLen(fCtrlPts, 2); |
| 963 |
if p = nil then |
| 964 |
fFlatPath := nil else |
| 965 |
fFlatPath := FlattenQBezier(fFirstPt, fCtrlPts, bt); |
| 966 |
end; |
| 967 |
//------------------------------------------------------------------------------ |
| 968 |
|
| 969 |
function TSvgQSegment.GetStringDef(relative: Boolean; decimalPrec: integer): string; |
| 970 |
var |
| 971 |
i: integer; |
| 972 |
pt, relPt: TPointD; |
| 973 |
begin |
| 974 |
if relative then Result := 'q ' else Result := 'Q '; |
| 975 |
relPt := DescaleAndOffset(fFirstPt); |
| 976 |
for i := 0 to High(fCtrlPts) do |
| 977 |
begin |
| 978 |
pt := DescaleAndOffset(fCtrlPts[i]); |
| 979 |
Result := Result + AsCoordStr(pt, relPt, relative, decimalPrec); |
| 980 |
if (i mod 2) = 1 then relPt := pt; |
| 981 |
end; |
| 982 |
end; |
| 983 |
|
| 984 |
//------------------------------------------------------------------------------ |
| 985 |
// TSvgSSegment |
| 986 |
//------------------------------------------------------------------------------ |
| 987 |
|
| 988 |
constructor TSvgSSegment.Create(parent: TSvgSubPath; idx: integer; |
| 989 |
const firstPt : TPointD); |
| 990 |
begin |
| 991 |
inherited; |
| 992 |
fSegType := stCSpline; |
| 993 |
fExtend := 2; |
| 994 |
end; |
| 995 |
//------------------------------------------------------------------------------ |
| 996 |
|
| 997 |
procedure TSvgSSegment.GetFlattenedInternal; |
| 998 |
var |
| 999 |
bt : double; |
| 1000 |
p: TPathD; |
| 1001 |
begin |
| 1002 |
bt := BezierTolerance / pendingScale; |
| 1003 |
p := CheckPathLen(fCtrlPts, 2); |
| 1004 |
if p = nil then |
| 1005 |
fFlatPath := nil else |
| 1006 |
fFlatPath := FlattenCSpline(GetPreviousCtrlPt, fFirstPt, fCtrlPts, bt); |
| 1007 |
end; |
| 1008 |
//------------------------------------------------------------------------------ |
| 1009 |
|
| 1010 |
function TSvgSSegment.GetOnPathCtrlPts: TPathD; |
| 1011 |
var |
| 1012 |
i, len: integer; |
| 1013 |
begin |
| 1014 |
len := Length(fCtrlPts) div 2; |
| 1015 |
NewPointDArray(Result, len, True); |
| 1016 |
for i := 0 to High(Result) do |
| 1017 |
Result[i] := fCtrlPts[i*2+1]; |
| 1018 |
end; |
| 1019 |
//------------------------------------------------------------------------------ |
| 1020 |
|
| 1021 |
function TSvgSSegment.GetStringDef(relative: Boolean; decimalPrec: integer): string; |
| 1022 |
var |
| 1023 |
i: integer; |
| 1024 |
pt, relPt: TPointD; |
| 1025 |
begin |
| 1026 |
if relative then Result := 's ' else Result := 'S '; |
| 1027 |
relPt := DescaleAndOffset(fFirstPt); |
| 1028 |
for i := 0 to High(fCtrlPts) do |
| 1029 |
begin |
| 1030 |
pt := DescaleAndOffset(fCtrlPts[i]); |
| 1031 |
Result := Result + AsCoordStr(pt, relPt, relative, decimalPrec); |
| 1032 |
if relative and (i mod 2 = 1) then relPt := pt; |
| 1033 |
end; |
| 1034 |
end; |
| 1035 |
|
| 1036 |
//------------------------------------------------------------------------------ |
| 1037 |
// TSvgTSegment |
| 1038 |
//------------------------------------------------------------------------------ |
| 1039 |
|
| 1040 |
constructor TSvgTSegment.Create(parent: TSvgSubPath; idx: integer; |
| 1041 |
const firstPt : TPointD); |
| 1042 |
begin |
| 1043 |
inherited; |
| 1044 |
fSegType := stQSpline; |
| 1045 |
fExtend := 1; |
| 1046 |
end; |
| 1047 |
//------------------------------------------------------------------------------ |
| 1048 |
|
| 1049 |
procedure TSvgTSegment.GetFlattenedInternal; |
| 1050 |
var |
| 1051 |
bt: double; |
| 1052 |
begin |
| 1053 |
bt := BezierTolerance / pendingScale; |
| 1054 |
if fCtrlPts = nil then |
| 1055 |
fFlatPath := nil else |
| 1056 |
fFlatPath := FlattenQSpline(GetPreviousCtrlPt, fFirstPt, fCtrlPts, bt); |
| 1057 |
end; |
| 1058 |
//------------------------------------------------------------------------------ |
| 1059 |
|
| 1060 |
function TSvgTSegment.GetLastCtrlPt: TPointD; |
| 1061 |
var |
| 1062 |
i: integer; |
| 1063 |
begin |
| 1064 |
Result := ReflectPoint(GetPreviousCtrlPt, fFirstPt); |
| 1065 |
for i := 0 to High(CtrlPts) -1 do |
| 1066 |
Result := ReflectPoint(Result, CtrlPts[i]); |
| 1067 |
end; |
| 1068 |
//------------------------------------------------------------------------------ |
| 1069 |
|
| 1070 |
|
| 1071 |
function TSvgTSegment.GetStringDef(relative: Boolean; decimalPrec: integer): string; |
| 1072 |
var |
| 1073 |
i: integer; |
| 1074 |
pt, relPt: TPointD; |
| 1075 |
begin |
| 1076 |
if relative then Result := 't ' else Result := 'T '; |
| 1077 |
relPt := DescaleAndOffset(fFirstPt); |
| 1078 |
for i := 0 to High(fCtrlPts) do |
| 1079 |
begin |
| 1080 |
pt := DescaleAndOffset(fCtrlPts[i]); |
| 1081 |
Result := Result + AsCoordStr(pt, relPt, relative, decimalPrec); |
| 1082 |
if relative then relPt := pt; |
| 1083 |
end; |
| 1084 |
end; |
| 1085 |
|
| 1086 |
//------------------------------------------------------------------------------ |
| 1087 |
// TSvgVSegment |
| 1088 |
//------------------------------------------------------------------------------ |
| 1089 |
|
| 1090 |
constructor TSvgVSegment.Create(parent: TSvgSubPath; idx: integer; |
| 1091 |
const firstPt : TPointD); |
| 1092 |
begin |
| 1093 |
inherited; |
| 1094 |
fSegType := stVert; |
| 1095 |
fExtend := 1; |
| 1096 |
end; |
| 1097 |
//------------------------------------------------------------------------------ |
| 1098 |
|
| 1099 |
function TSvgVSegment.GetStringDef(relative: Boolean; decimalPrec: integer): string; |
| 1100 |
var |
| 1101 |
i: integer; |
| 1102 |
pt, relPt: TPointD; |
| 1103 |
begin |
| 1104 |
if relative then |
| 1105 |
begin |
| 1106 |
Result := 'v '; |
| 1107 |
relPt := DescaleAndOffset(fFirstPt); |
| 1108 |
for i := 0 to High(fCtrlPts) do |
| 1109 |
begin |
| 1110 |
pt := DescaleAndOffset(fCtrlPts[i]); |
| 1111 |
Result := Result + AsFloatStr(pt.Y - relPt.Y, decimalPrec) + ' '; |
| 1112 |
relPt := pt; |
| 1113 |
end; |
| 1114 |
end else |
| 1115 |
begin |
| 1116 |
Result := 'V '; |
| 1117 |
for i := 0 to High(fCtrlPts) do |
| 1118 |
begin |
| 1119 |
pt := DescaleAndOffset(fCtrlPts[i]); |
| 1120 |
Result := Result + AsFloatStr(pt.Y, decimalPrec) + ' '; |
| 1121 |
end; |
| 1122 |
end; |
| 1123 |
end; |
| 1124 |
|
| 1125 |
//------------------------------------------------------------------------------ |
| 1126 |
// TSvgZSegment |
| 1127 |
//------------------------------------------------------------------------------ |
| 1128 |
|
| 1129 |
constructor TSvgZSegment.Create(parent: TSvgSubPath; |
| 1130 |
idx: integer; const firstPt : TPointD); |
| 1131 |
begin |
| 1132 |
inherited; |
| 1133 |
fSegType := stClose; |
| 1134 |
fExtend := 0; |
| 1135 |
end; |
| 1136 |
//------------------------------------------------------------------------------ |
| 1137 |
|
| 1138 |
function TSvgZSegment.GetStringDef(relative: Boolean; |
| 1139 |
decimalPrec: integer): string; |
| 1140 |
begin |
| 1141 |
Result := 'Z '; |
| 1142 |
end; |
| 1143 |
|
| 1144 |
//------------------------------------------------------------------------------ |
| 1145 |
// TSvgSubPath |
| 1146 |
//------------------------------------------------------------------------------ |
| 1147 |
|
| 1148 |
function TSvgSubPath.GetFlattenedPath(pendingScale: double): TPathD; |
| 1149 |
var |
| 1150 |
i: integer; |
| 1151 |
flattenedPaths: TPathsD; |
| 1152 |
begin |
| 1153 |
if pendingScale <= 0 then pendingScale := 1.0; |
| 1154 |
if (pendingScale > fPendingScale) then |
| 1155 |
fPendingScale := pendingScale; |
| 1156 |
|
| 1157 |
Result := nil; |
| 1158 |
SetLength(flattenedPaths, fSegsCount); |
| 1159 |
for i := 0 to fSegsCount - 1 do |
| 1160 |
fSegs[i].GetFlattened2(flattenedPaths[i]); |
| 1161 |
ConcatPaths(Result, flattenedPaths); |
| 1162 |
end; |
| 1163 |
//------------------------------------------------------------------------------ |
| 1164 |
|
| 1165 |
function TSvgSubPath.AddSeg(segType: TSvgPathSegType; |
| 1166 |
const startPt: TPointD; const pts: TPathD): TSvgPathSeg; |
| 1167 |
var |
| 1168 |
i: integer; |
| 1169 |
begin |
| 1170 |
i := fSegsCount; |
| 1171 |
if i = Length(fSegs) then |
| 1172 |
GrowSegs; |
| 1173 |
inc(fSegsCount); |
| 1174 |
|
| 1175 |
case segType of |
| 1176 |
stCBezier : Result := TSvgCSegment.Create(self, i, startPt); |
| 1177 |
stHorz : Result := TSvgHSegment.Create(self, i, startPt); |
| 1178 |
stLine : Result := TSvgLSegment.Create(self, i, startPt); |
| 1179 |
stQBezier : Result := TSvgQSegment.Create(self, i, startPt); |
| 1180 |
stCSpline : Result := TSvgSSegment.Create(self, i, startPt); |
| 1181 |
stQSpline : Result := TSvgTSegment.Create(self, i, startPt); |
| 1182 |
stVert : Result := TSvgVSegment.Create(self, i, startPt); |
| 1183 |
else raise Exception.Create('TSvgSubPath.AddSeg error'); |
| 1184 |
end; |
| 1185 |
fSegs[i] := Result; |
| 1186 |
Result.fCtrlPts := pts; |
| 1187 |
Result.fFlatPath := nil; |
| 1188 |
if Result is TSvgCurvedSeg then |
| 1189 |
TSvgCurvedSeg(Result).pendingScale := fPendingScale; |
| 1190 |
end; |
| 1191 |
//------------------------------------------------------------------------------ |
| 1192 |
|
| 1193 |
function TSvgSubPath.AddASeg(const startPt, endPt: TPointD; const rect: TRectD; |
| 1194 |
angle: double; isClockwise: Boolean): TSvgASegment; |
| 1195 |
var |
| 1196 |
i: integer; |
| 1197 |
begin |
| 1198 |
i := fSegsCount; |
| 1199 |
if i = Length(fSegs) then |
| 1200 |
GrowSegs; |
| 1201 |
inc(fSegsCount); |
| 1202 |
|
| 1203 |
Result := TSvgASegment.Create(self, i, startPt); |
| 1204 |
fSegs[i] := Result; |
| 1205 |
Result.pendingScale := self.fPendingScale; |
| 1206 |
with Result.fArcInfo do |
| 1207 |
begin |
| 1208 |
rec := rect; |
| 1209 |
startPos := startPt; |
| 1210 |
endPos := endPt; |
| 1211 |
rectAngle := angle; |
| 1212 |
sweepClockW := isClockwise; |
| 1213 |
end; |
| 1214 |
Result.SetCtrlPtsFromArcInfo; // calls Changed |
| 1215 |
end; |
| 1216 |
//------------------------------------------------------------------------------ |
| 1217 |
|
| 1218 |
function TSvgSubPath.AddHSeg(const startPt: TPointD; const pts: TPathD): TSvgHSegment; |
| 1219 |
begin |
| 1220 |
Result := AddSeg(stHorz, startPt, pts) as TSvgHSegment; |
| 1221 |
end; |
| 1222 |
//------------------------------------------------------------------------------ |
| 1223 |
|
| 1224 |
function TSvgSubPath.AddCSeg(const startPt: TPointD; const pts: TPathD): TSvgCSegment; |
| 1225 |
begin |
| 1226 |
Result := AddSeg(stCBezier, startPt, pts) as TSvgCSegment; |
| 1227 |
end; |
| 1228 |
//------------------------------------------------------------------------------ |
| 1229 |
|
| 1230 |
function TSvgSubPath.AddLSeg(const startPt: TPointD; const pts: TPathD): TSvgLSegment; |
| 1231 |
begin |
| 1232 |
Result := AddSeg(stLine, startPt, pts) as TSvgLSegment; |
| 1233 |
end; |
| 1234 |
//------------------------------------------------------------------------------ |
| 1235 |
|
| 1236 |
function TSvgSubPath.AddQSeg(const startPt: TPointD; const pts: TPathD): TSvgQSegment; |
| 1237 |
begin |
| 1238 |
Result := AddSeg(stQBezier, startPt, pts) as TSvgQSegment; |
| 1239 |
end; |
| 1240 |
//------------------------------------------------------------------------------ |
| 1241 |
|
| 1242 |
function TSvgSubPath.AddSSeg(const startPt: TPointD; const pts: TPathD): TSvgSSegment; |
| 1243 |
begin |
| 1244 |
Result := AddSeg(stCSpline, startPt, pts) as TSvgSSegment; |
| 1245 |
end; |
| 1246 |
//------------------------------------------------------------------------------ |
| 1247 |
|
| 1248 |
function TSvgSubPath.AddTSeg(const startPt: TPointD; const pts: TPathD): TSvgTSegment; |
| 1249 |
begin |
| 1250 |
Result := AddSeg(stQSpline, startPt, pts) as TSvgTSegment; |
| 1251 |
end; |
| 1252 |
//------------------------------------------------------------------------------ |
| 1253 |
|
| 1254 |
function TSvgSubPath.AddVSeg(const startPt: TPointD; const pts: TPathD): TSvgVSegment; |
| 1255 |
begin |
| 1256 |
Result := AddSeg(stVert, startPt, pts) as TSvgVSegment; |
| 1257 |
end; |
| 1258 |
//------------------------------------------------------------------------------ |
| 1259 |
|
| 1260 |
function TSvgSubPath.AddZSeg(const endPt, firstPt: TPointD): TSvgZSegment; |
| 1261 |
var |
| 1262 |
i: integer; |
| 1263 |
begin |
| 1264 |
i := fSegsCount; |
| 1265 |
if i = Length(fSegs) then |
| 1266 |
GrowSegs; |
| 1267 |
inc(fSegsCount); |
| 1268 |
|
| 1269 |
Result := TSvgZSegment.Create(self, i, endPt); |
| 1270 |
fSegs[i] := Result; |
| 1271 |
NewPointDArray(Result.fCtrlPts, 1, True); |
| 1272 |
Result.fCtrlPts[0] := firstPt; |
| 1273 |
isClosed := true; |
| 1274 |
end; |
| 1275 |
//------------------------------------------------------------------------------ |
| 1276 |
|
| 1277 |
function TSvgSubPath.GetLastSeg: TSvgPathSeg; |
| 1278 |
var |
| 1279 |
cnt: integer; |
| 1280 |
begin |
| 1281 |
cnt := Count; |
| 1282 |
if cnt = 0 then |
| 1283 |
Result := nil else |
| 1284 |
Result := seg[cnt -1]; |
| 1285 |
end; |
| 1286 |
//------------------------------------------------------------------------------ |
| 1287 |
|
| 1288 |
function TSvgSubPath.DeleteLastSeg: Boolean; |
| 1289 |
var |
| 1290 |
cnt: integer; |
| 1291 |
begin |
| 1292 |
cnt := Count; |
| 1293 |
Result := cnt > 0; |
| 1294 |
if not Result then Exit; |
| 1295 |
seg[cnt -1].Free; |
| 1296 |
SetLength(fSegs, cnt -1); |
| 1297 |
fSegsCount := cnt - 1; |
| 1298 |
if isClosed then isClosed := false; |
| 1299 |
end; |
| 1300 |
//------------------------------------------------------------------------------ |
| 1301 |
|
| 1302 |
function TSvgSubPath.GetSimplePath: TPathD; |
| 1303 |
var |
| 1304 |
i: integer; |
| 1305 |
paths: TPathsD; |
| 1306 |
begin |
| 1307 |
if fSegsCount <= 1 then |
| 1308 |
begin |
| 1309 |
Result := Img32.Vector.MakePath(GetFirstPt); |
| 1310 |
for i := 0 to fSegsCount - 1 do |
| 1311 |
ConcatPaths(Result, fSegs[i].GetOnPathCtrlPts); |
| 1312 |
end |
| 1313 |
else |
| 1314 |
begin |
| 1315 |
SetLength(paths, 1 + fSegsCount); |
| 1316 |
paths[0] := Img32.Vector.MakePath(GetFirstPt); |
| 1317 |
for i := 0 to fSegsCount - 1 do |
| 1318 |
paths[1 + i] := fSegs[i].GetOnPathCtrlPts; |
| 1319 |
ConcatPaths(Result, paths); |
| 1320 |
end; |
| 1321 |
end; |
| 1322 |
//------------------------------------------------------------------------------ |
| 1323 |
|
| 1324 |
function TSvgSubPath.GetMoveStrDef(relative: Boolean; decimalPrec: integer): string; |
| 1325 |
var |
| 1326 |
pt: TPointD; |
| 1327 |
begin |
| 1328 |
Result := ''; |
| 1329 |
if fSegsCount = 0 then Exit; |
| 1330 |
|
| 1331 |
if decimalPrec < -3 then decimalPrec := -3 |
| 1332 |
else if decimalPrec > 4 then decimalPrec := 4; |
| 1333 |
|
| 1334 |
with fParent do |
| 1335 |
begin |
| 1336 |
pt.X := (fSegs[0].fFirstPt.X - self.PathOffset.X - Offset.X)/fPathScale; |
| 1337 |
pt.Y := (fSegs[0].fFirstPt.Y - self.PathOffset.Y - Offset.Y)/fPathScale; |
| 1338 |
end; |
| 1339 |
Result := 'M ' + AsCoordStr(pt, NullPointD, false, decimalPrec); |
| 1340 |
end; |
| 1341 |
//------------------------------------------------------------------------------ |
| 1342 |
|
| 1343 |
function TSvgSubPath.GetStringDef(relative: Boolean; decimalPrec: integer): string; |
| 1344 |
var |
| 1345 |
i: integer; |
| 1346 |
begin |
| 1347 |
if decimalPrec < -3 then decimalPrec := -3 |
| 1348 |
else if decimalPrec > 4 then decimalPrec := 4; |
| 1349 |
if Count = 0 then Exit; |
| 1350 |
|
| 1351 |
Result := GetMoveStrDef(relative, decimalPrec); |
| 1352 |
for i := 0 to Count -1 do |
| 1353 |
Result := Result + fSegs[i].GetStringDef(relative, decimalPrec); |
| 1354 |
end; |
| 1355 |
//------------------------------------------------------------------------------ |
| 1356 |
|
| 1357 |
constructor TSvgSubPath.Create(parent: TSvgPath); |
| 1358 |
begin |
| 1359 |
fParent := parent; |
| 1360 |
end; |
| 1361 |
//------------------------------------------------------------------------------ |
| 1362 |
|
| 1363 |
destructor TSvgSubPath.Destroy; |
| 1364 |
begin |
| 1365 |
Clear; |
| 1366 |
inherited; |
| 1367 |
end; |
| 1368 |
//------------------------------------------------------------------------------ |
| 1369 |
|
| 1370 |
procedure TSvgSubPath.Clear; |
| 1371 |
var |
| 1372 |
i: integer; |
| 1373 |
begin |
| 1374 |
for i := 0 to Count -1 do |
| 1375 |
fSegs[i].Free; |
| 1376 |
fSegs := nil; |
| 1377 |
fSegsCount := 0; |
| 1378 |
fPathOffset := NullPointD; |
| 1379 |
end; |
| 1380 |
//------------------------------------------------------------------------------ |
| 1381 |
|
| 1382 |
procedure TSvgSubPath.GrowSegs; |
| 1383 |
begin |
| 1384 |
SetLength(fSegs, (fSegsCount * 2) + 1); |
| 1385 |
end; |
| 1386 |
//------------------------------------------------------------------------------ |
| 1387 |
|
| 1388 |
procedure TSvgSubPath.SegsLoaded; |
| 1389 |
begin |
| 1390 |
// Trim the array to the actual used size |
| 1391 |
if Length(fSegs) <> fSegsCount then |
| 1392 |
SetLength(fSegs, fSegsCount); |
| 1393 |
end; |
| 1394 |
//------------------------------------------------------------------------------ |
| 1395 |
|
| 1396 |
procedure TSvgSubPath.InitSegs(Capacity: Integer); |
| 1397 |
begin |
| 1398 |
if Capacity > fSegsCount then |
| 1399 |
SetLength(fSegs, Capacity); |
| 1400 |
end; |
| 1401 |
//------------------------------------------------------------------------------ |
| 1402 |
|
| 1403 |
function TSvgSubPath.GetCount: integer; |
| 1404 |
begin |
| 1405 |
Result := fSegsCount; |
| 1406 |
end; |
| 1407 |
//------------------------------------------------------------------------------ |
| 1408 |
|
| 1409 |
procedure TSvgSubPath.Offset(dx, dy: double); |
| 1410 |
var |
| 1411 |
i: integer; |
| 1412 |
begin |
| 1413 |
for i := 0 to fSegsCount - 1 do fSegs[i].Offset(dx, dy); |
| 1414 |
end; |
| 1415 |
//------------------------------------------------------------------------------ |
| 1416 |
|
| 1417 |
function TSvgSubPath.GetSeg(index: integer): TSvgPathSeg; |
| 1418 |
begin |
| 1419 |
if (index < 0) or (index >= Count) then |
| 1420 |
raise Exception.Create(rsSvgSubPathRangeError); |
| 1421 |
Result := fSegs[index]; |
| 1422 |
end; |
| 1423 |
//------------------------------------------------------------------------------ |
| 1424 |
|
| 1425 |
function TSvgSubPath.GetFirstPt: TPointD; |
| 1426 |
begin |
| 1427 |
if Count = 0 then Result := NullPointD |
| 1428 |
else Result := fSegs[0].FirstPt; |
| 1429 |
end; |
| 1430 |
//------------------------------------------------------------------------------ |
| 1431 |
|
| 1432 |
function TSvgSubPath.GetLastPt: TPointD; |
| 1433 |
begin |
| 1434 |
if Count = 0 then |
| 1435 |
Result := NullPointD |
| 1436 |
else with fSegs[Count -1] do |
| 1437 |
Result := CtrlPts[High(CtrlPts)]; |
| 1438 |
end; |
| 1439 |
//------------------------------------------------------------------------------ |
| 1440 |
|
| 1441 |
function TSvgSubPath.GetBounds: TRectD; |
| 1442 |
var |
| 1443 |
i: integer; |
| 1444 |
p: TPathD; |
| 1445 |
begin |
| 1446 |
p := nil; |
| 1447 |
for i := 0 to Count -1 do |
| 1448 |
ConcatPaths(p, fSegs[i].fFlatPath); |
| 1449 |
Result := Img32.Vector.GetBoundsD(p); |
| 1450 |
end; |
| 1451 |
|
| 1452 |
//------------------------------------------------------------------------------ |
| 1453 |
// TSvgPath |
| 1454 |
//------------------------------------------------------------------------------ |
| 1455 |
|
| 1456 |
destructor TSvgPath.Destroy; |
| 1457 |
begin |
| 1458 |
Clear; |
| 1459 |
inherited; |
| 1460 |
end; |
| 1461 |
//------------------------------------------------------------------------------ |
| 1462 |
|
| 1463 |
procedure TSvgPath.ScaleAndOffset(scale: double; dx, dy: integer); |
| 1464 |
var |
| 1465 |
i,j: integer; |
| 1466 |
begin |
| 1467 |
if fPathScale = 0 then fPathScale := 1; |
| 1468 |
if scale = 0 then scale := 1; |
| 1469 |
fPathScale := fPathScale * scale; |
| 1470 |
|
| 1471 |
fPathOffs := PointD(dx, dy); |
| 1472 |
for i := 0 to Count -1 do |
| 1473 |
with fSubPaths[i] do |
| 1474 |
begin |
| 1475 |
if scale <> 1 then |
| 1476 |
for j := 0 to fSegsCount - 1 do |
| 1477 |
fSegs[j].Scale(scale); |
| 1478 |
Offset(dx,dy); |
| 1479 |
end; |
| 1480 |
end; |
| 1481 |
//------------------------------------------------------------------------------ |
| 1482 |
|
| 1483 |
function TSvgPath.GetStringDef(relative: Boolean; decimalPrec: integer): string; |
| 1484 |
var |
| 1485 |
i : integer; |
| 1486 |
begin |
| 1487 |
result := ''; |
| 1488 |
if fPathScale = 0 then fPathScale := 1; |
| 1489 |
for i := 0 to High(fSubPaths) do |
| 1490 |
Result := Result + fSubPaths[i].GetStringDef(relative, decimalPrec); |
| 1491 |
end; |
| 1492 |
//------------------------------------------------------------------------------ |
| 1493 |
|
| 1494 |
procedure TSvgPath.Parse(const value: UTF8String); |
| 1495 |
var |
| 1496 |
c, endC : PUTF8Char; |
| 1497 |
firstPt : TPointD; |
| 1498 |
lastPt : TPointD; |
| 1499 |
currPt : TPointD; |
| 1500 |
pt2, pt3 : TPointD; |
| 1501 |
angle : double; |
| 1502 |
sweepCW : integer; |
| 1503 |
largeArc : integer; |
| 1504 |
arcRec : TRectD; |
| 1505 |
isRelative : Boolean; |
| 1506 |
currSegType : TSvgPathSegType; |
| 1507 |
currSubPath : TSvgSubPath; |
| 1508 |
pts : TPathD; |
| 1509 |
ptCap : integer; |
| 1510 |
ptCnt : integer; |
| 1511 |
|
| 1512 |
procedure AddPt(const pt: TPointD); |
| 1513 |
begin |
| 1514 |
if ptCnt = ptCap then |
| 1515 |
begin |
| 1516 |
inc(ptCap, 8); |
| 1517 |
SetLengthUninit(pts, ptCap); |
| 1518 |
end; |
| 1519 |
pts[ptCnt] := pt; |
| 1520 |
inc(ptCnt); |
| 1521 |
end; |
| 1522 |
|
| 1523 |
procedure AllocEstimatedPtsCount(c, endC: PUTF8Char); |
| 1524 |
begin |
| 1525 |
// Count the numbers before the next segment type char |
| 1526 |
ptCap := 0; |
| 1527 |
while c < endC do |
| 1528 |
begin |
| 1529 |
// skip whitespaces |
| 1530 |
while (c < endC) and (c^ <= space) do |
| 1531 |
inc(c); |
| 1532 |
|
| 1533 |
if c >= endC then |
| 1534 |
break; |
| 1535 |
|
| 1536 |
case c^ of |
| 1537 |
'0'..'9', '-', '.', 'E', 'e': |
| 1538 |
begin |
| 1539 |
while (c < endC) and (c^ > space) do |
| 1540 |
inc(c); |
| 1541 |
Inc(ptCap); |
| 1542 |
end; |
| 1543 |
else |
| 1544 |
Break; |
| 1545 |
end; |
| 1546 |
end; |
| 1547 |
ptCap := ptCap div 2; // two numbers are one point |
| 1548 |
SetLength(pts, ptCap); |
| 1549 |
end; |
| 1550 |
|
| 1551 |
function EstimateSegs(c, endC: PUTF8Char): Integer; |
| 1552 |
var |
| 1553 |
ch: UTF8Char; |
| 1554 |
begin |
| 1555 |
Result := 0; |
| 1556 |
while True do |
| 1557 |
begin |
| 1558 |
if c >= endC then |
| 1559 |
Break; |
| 1560 |
ch := c^; |
| 1561 |
inc(c); |
| 1562 |
|
| 1563 |
case ch of |
| 1564 |
'A'..'Z', 'a'..'z': |
| 1565 |
begin |
| 1566 |
case ch of |
| 1567 |
'M', 'm': // move / close |
| 1568 |
Break; |
| 1569 |
'Z', 'z': |
| 1570 |
begin |
| 1571 |
Inc(Result); |
| 1572 |
Break; |
| 1573 |
end; |
| 1574 |
'E', 'e': ; // Exponent of a number |
| 1575 |
else |
| 1576 |
Inc(Result); |
| 1577 |
end; |
| 1578 |
end; |
| 1579 |
end; |
| 1580 |
end; |
| 1581 |
end; |
| 1582 |
|
| 1583 |
var |
| 1584 |
ExpectedSegCount: Integer; |
| 1585 |
begin |
| 1586 |
Clear; |
| 1587 |
currSubPath := nil; |
| 1588 |
ExpectedSegCount := 1; |
| 1589 |
|
| 1590 |
c := PUTF8Char(value); |
| 1591 |
endC := c + Length(value); |
| 1592 |
isRelative := false; |
| 1593 |
currPt := NullPointD; |
| 1594 |
|
| 1595 |
while true do |
| 1596 |
begin |
| 1597 |
currSegType := GetSegType(c, endC, isRelative); |
| 1598 |
if currSegType = stUnknown then Break; |
| 1599 |
|
| 1600 |
if currSegType = stMove then |
| 1601 |
begin |
| 1602 |
if currSubPath <> nil then |
| 1603 |
currSubPath.SegsLoaded; // Trim the segs array to the actual count |
| 1604 |
currSubPath := nil; |
| 1605 |
|
| 1606 |
ExpectedSegCount := EstimateSegs(c, endc); |
| 1607 |
|
| 1608 |
if isRelative then |
| 1609 |
lastPt := currPt else |
| 1610 |
lastPt := InvalidPointD; |
| 1611 |
|
| 1612 |
if not Parse2Num(c, endC, currPt, lastPt) then break; |
| 1613 |
lastPt := currPt; |
| 1614 |
//values immediately following a Move are implicitly Line statements |
| 1615 |
if IsNumPending(c, endC, true) then |
| 1616 |
currSegType := stLine else |
| 1617 |
Continue; |
| 1618 |
Inc(ExpectedSegCount); |
| 1619 |
end |
| 1620 |
else if (currSegType = stClose) then |
| 1621 |
begin |
| 1622 |
if currPt.X = InvalidD then Continue; |
| 1623 |
|
| 1624 |
if Assigned(currSubPath) and (currSubPath.Count > 0) then |
| 1625 |
begin |
| 1626 |
lastPt := currPt; |
| 1627 |
currPt := currSubPath.GetFirstPt; |
| 1628 |
currSubPath.AddZSeg(lastPt, currPt); |
| 1629 |
end else |
| 1630 |
begin |
| 1631 |
if not Assigned(currSubPath) then |
| 1632 |
currSubPath := AddPath(1); |
| 1633 |
currSubPath.AddZSeg(currPt, currPt); |
| 1634 |
end; |
| 1635 |
currSubPath.SegsLoaded; // Trim the segs array to the actual count |
| 1636 |
currSubPath := nil; |
| 1637 |
ExpectedSegCount := 1; |
| 1638 |
Continue; |
| 1639 |
end; |
| 1640 |
|
| 1641 |
if not Assigned(currSubPath) then |
| 1642 |
currSubPath := AddPath(ExpectedSegCount); |
| 1643 |
|
| 1644 |
pts := nil; |
| 1645 |
ptCnt := 0; ptCap := 0; |
| 1646 |
firstPt := currPt; |
| 1647 |
if isRelative then |
| 1648 |
lastPt := firstPt else |
| 1649 |
lastPt := InvalidPointD; |
| 1650 |
|
| 1651 |
case currSegType of |
| 1652 |
stArc: |
| 1653 |
begin |
| 1654 |
//nb: unlike other segment types, |
| 1655 |
//consecutive arc segs are separated. |
| 1656 |
while IsNumPending(c, endC, true) and |
| 1657 |
Parse2Num(c, endC, pt2, InvalidPointD) and |
| 1658 |
ParseNextNum(c, endC, true, angle) and |
| 1659 |
GetSingleDigit(c, endC, largeArc) and |
| 1660 |
GetSingleDigit(c, endC, sweepCW) and |
| 1661 |
Parse2Num(c, endC, currPt, lastPt) do |
| 1662 |
begin |
| 1663 |
angle := DegToRad(angle); |
| 1664 |
arcRec := GetSvgArcInfoRect(firstPt, currPt, pt2, |
| 1665 |
angle, largeArc <> 0, sweepCW <> 0); |
| 1666 |
if arcRec.IsEmpty then break; |
| 1667 |
|
| 1668 |
currSubPath.AddASeg(firstPt, currPt, |
| 1669 |
arcRec, angle, sweepCW <> 0); |
| 1670 |
|
| 1671 |
if isRelative then lastPt := currPt; |
| 1672 |
firstPt := currPt; |
| 1673 |
end; |
| 1674 |
end; |
| 1675 |
stCBezier: |
| 1676 |
begin |
| 1677 |
AllocEstimatedPtsCount(c, endC); |
| 1678 |
while IsNumPending(c, endC, true) and |
| 1679 |
Parse2Num(c, endC, pt2, lastPt) and |
| 1680 |
Parse2Num(c, endC, pt3, lastPt) and |
| 1681 |
Parse2Num(c, endC, currPt, lastPt) do |
| 1682 |
begin |
| 1683 |
AddPt(pt2); |
| 1684 |
AddPt(pt3); |
| 1685 |
AddPt(currPt); |
| 1686 |
if isRelative then lastPt := currPt; |
| 1687 |
end; |
| 1688 |
if Length(pts) <> ptCnt then |
| 1689 |
SetLength(pts, ptCnt); |
| 1690 |
currSubPath.AddSeg(stCBezier, firstPt, pts); |
| 1691 |
end; |
| 1692 |
stHorz: |
| 1693 |
begin |
| 1694 |
AllocEstimatedPtsCount(c, endC); |
| 1695 |
while IsNumPending(c, endC, true) and |
| 1696 |
Parse1Num(c, endC, currPt.X, lastPt.X) do |
| 1697 |
begin |
| 1698 |
AddPt(currPt); |
| 1699 |
if isRelative then lastPt.X := currPt.X; |
| 1700 |
end; |
| 1701 |
if Length(pts) <> ptCnt then |
| 1702 |
SetLength(pts, ptCnt); |
| 1703 |
currSubPath.AddHSeg(firstPt, pts); |
| 1704 |
end; |
| 1705 |
stQBezier, stCSpline: |
| 1706 |
begin |
| 1707 |
AllocEstimatedPtsCount(c, endC); |
| 1708 |
while IsNumPending(c, endC, true) and |
| 1709 |
Parse2Num(c, endC, pt2, lastPt) and |
| 1710 |
Parse2Num(c, endC, currPt, lastPt) do |
| 1711 |
begin |
| 1712 |
AddPt(pt2); |
| 1713 |
AddPt(currPt); |
| 1714 |
if isRelative then lastPt := currPt; |
| 1715 |
end; |
| 1716 |
if Length(pts) <> ptCnt then |
| 1717 |
SetLength(pts, ptCnt); |
| 1718 |
currSubPath.AddSeg(currSegType, firstPt, pts); |
| 1719 |
end; |
| 1720 |
stLine, stQSpline: |
| 1721 |
begin |
| 1722 |
AllocEstimatedPtsCount(c, endC); |
| 1723 |
while IsNumPending(c, endC, true) and |
| 1724 |
Parse2Num(c, endC, currPt, lastPt) do |
| 1725 |
begin |
| 1726 |
AddPt(currPt); |
| 1727 |
if isRelative then lastPt := currPt; |
| 1728 |
end; |
| 1729 |
if Length(pts) <> ptCnt then |
| 1730 |
SetLength(pts, ptCnt); |
| 1731 |
currSubPath.AddSeg(currSegType, firstPt, pts); |
| 1732 |
end; |
| 1733 |
stVert: |
| 1734 |
begin |
| 1735 |
AllocEstimatedPtsCount(c, endC); |
| 1736 |
while IsNumPending(c, endC, true) and |
| 1737 |
Parse1Num(c, endC, currPt.Y, lastPt.Y) do |
| 1738 |
begin |
| 1739 |
AddPt(currPt); |
| 1740 |
if isRelative then lastPt.Y := currPt.Y; |
| 1741 |
end; |
| 1742 |
if Length(pts) <> ptCnt then |
| 1743 |
SetLength(pts, ptCnt); |
| 1744 |
currSubPath.AddVSeg(firstPt, pts); |
| 1745 |
end; |
| 1746 |
end; |
| 1747 |
end; |
| 1748 |
if currSubPath <> nil then |
| 1749 |
currSubPath.SegsLoaded; // Trim the segs array to the actual count |
| 1750 |
end; |
| 1751 |
//------------------------------------------------------------------------------ |
| 1752 |
|
| 1753 |
function TSvgPath.GetCount: integer; |
| 1754 |
begin |
| 1755 |
Result := Length(fSubPaths); |
| 1756 |
end; |
| 1757 |
//------------------------------------------------------------------------------ |
| 1758 |
|
| 1759 |
function TSvgPath.GetPath(index: integer): TSvgSubPath; |
| 1760 |
begin |
| 1761 |
if (index < 0) or (index >= Count) then |
| 1762 |
raise Exception.Create(rsSvgPathRangeError); |
| 1763 |
Result := fSubPaths[index]; |
| 1764 |
end; |
| 1765 |
//------------------------------------------------------------------------------ |
| 1766 |
|
| 1767 |
procedure TSvgPath.Clear; |
| 1768 |
var |
| 1769 |
i: integer; |
| 1770 |
begin |
| 1771 |
for i := 0 to Count -1 do |
| 1772 |
fSubPaths[i].Free; |
| 1773 |
fSubPaths := nil; |
| 1774 |
fPathScale := 1; |
| 1775 |
end; |
| 1776 |
//------------------------------------------------------------------------------ |
| 1777 |
|
| 1778 |
function TSvgPath.GetBounds: TRectD; |
| 1779 |
var |
| 1780 |
i: integer; |
| 1781 |
p: TPathD; |
| 1782 |
begin |
| 1783 |
p := nil; |
| 1784 |
for i := 0 to Count -1 do |
| 1785 |
ConcatPaths(p, fSubPaths[i].GetFlattenedPath); |
| 1786 |
Result := Img32.Vector.GetBoundsD(p); |
| 1787 |
end; |
| 1788 |
//------------------------------------------------------------------------------ |
| 1789 |
|
| 1790 |
function TSvgPath.GetControlBounds: TRectD; |
| 1791 |
var |
| 1792 |
i,j: integer; |
| 1793 |
p: TPathD; |
| 1794 |
begin |
| 1795 |
p := nil; |
| 1796 |
for i := 0 to Count -1 do |
| 1797 |
with fSubPaths[i] do |
| 1798 |
begin |
| 1799 |
AppendPoint(p, GetFirstPt); |
| 1800 |
for j := 0 to fSegsCount - 1 do |
| 1801 |
ConcatPaths(p, fSegs[j].fCtrlPts); |
| 1802 |
end; |
| 1803 |
Result := GetBoundsD(p); |
| 1804 |
|
| 1805 |
//watch out for straight horizontal or vertical lines |
| 1806 |
if IsEmptyRect(Result) then |
| 1807 |
begin |
| 1808 |
if Result.Width = 0 then |
| 1809 |
begin |
| 1810 |
Result.Left := Result.Left - 0.5; |
| 1811 |
Result.Right := Result.Left + 1.0; |
| 1812 |
end |
| 1813 |
else if Result.Height = 0 then |
| 1814 |
begin |
| 1815 |
Result.Top := Result.Top - 0.5; |
| 1816 |
Result.Bottom := Result.Top + 1.0; |
| 1817 |
end; |
| 1818 |
end; |
| 1819 |
end; |
| 1820 |
//------------------------------------------------------------------------------ |
| 1821 |
|
| 1822 |
function TSvgPath.AddPath(SegsCapacity: Integer): TSvgSubPath; |
| 1823 |
var |
| 1824 |
i: integer; |
| 1825 |
begin |
| 1826 |
i := Count; |
| 1827 |
Result := TSvgSubPath.Create(self); |
| 1828 |
Result.InitSegs(SegsCapacity); |
| 1829 |
SetLength(fSubPaths, i + 1); |
| 1830 |
fSubPaths[i] := Result; |
| 1831 |
end; |
| 1832 |
//------------------------------------------------------------------------------ |
| 1833 |
|
| 1834 |
procedure TSvgPath.DeleteSubPath(subPath: TSvgSubPath); |
| 1835 |
var |
| 1836 |
i, len: integer; |
| 1837 |
begin |
| 1838 |
len := Length(fSubPaths); |
| 1839 |
for i := 0 to len -1 do |
| 1840 |
if subPath = fSubPaths[i] then |
| 1841 |
begin |
| 1842 |
fSubPaths[i].Free; |
| 1843 |
if i < len -1 then |
| 1844 |
Move(fSubPaths[i+1], fSubPaths[i], |
| 1845 |
(len - i -1) * SizeOf(Pointer)); |
| 1846 |
SetLength(fSubPaths, len -1); |
| 1847 |
break; |
| 1848 |
end; |
| 1849 |
end; |
| 1850 |
|
| 1851 |
//------------------------------------------------------------------------------ |
| 1852 |
// GetSvgArcInfoRect |
| 1853 |
//------------------------------------------------------------------------------ |
| 1854 |
|
| 1855 |
//https://stackoverflow.com/a/12329083 |
| 1856 |
function GetSvgArcInfoRect(const p1, p2: TPointD; radii: TPointD; |
| 1857 |
phi_rads: double; fA, fS: boolean): TRectD; |
| 1858 |
var |
| 1859 |
x1_, y1_, rxry, rxy1_, ryx1_, s_phi, c_phi: double; |
| 1860 |
hd_x, hd_y, hs_x, hs_y, sum_of_sq, lambda, coe: double; |
| 1861 |
cx, cy, cx_, cy_: double; |
| 1862 |
begin |
| 1863 |
Result := NullRectD; |
| 1864 |
if (radii.X < 0) then radii.X := -radii.X; |
| 1865 |
if (radii.Y < 0) then radii.Y := -radii.Y; |
| 1866 |
if (radii.X = 0) or (radii.Y = 0) then Exit; |
| 1867 |
|
| 1868 |
GetSinCos(phi_rads, s_phi, c_phi);; |
| 1869 |
hd_x := (p1.X - p2.X) / 2.0; // half diff of x |
| 1870 |
hd_y := (p1.Y - p2.Y) / 2.0; // half diff of y |
| 1871 |
hs_x := (p1.X + p2.X) / 2.0; // half sum of x |
| 1872 |
hs_y := (p1.Y + p2.Y) / 2.0; // half sum of y |
| 1873 |
|
| 1874 |
// F6.5.1 |
| 1875 |
x1_ := c_phi * hd_x + s_phi * hd_y; |
| 1876 |
y1_ := c_phi * hd_y - s_phi * hd_x; |
| 1877 |
|
| 1878 |
// F.6.6 Correction of out-of-range radii |
| 1879 |
// Step 3: Ensure radii are large enough |
| 1880 |
lambda := (x1_ * x1_) / (radii.X * radii.X) + |
| 1881 |
(y1_ * y1_) / (radii.Y * radii.Y); |
| 1882 |
if (lambda > 1) then |
| 1883 |
begin |
| 1884 |
radii.X := radii.X * Sqrt(lambda); |
| 1885 |
radii.Y := radii.Y * Sqrt(lambda); |
| 1886 |
end; |
| 1887 |
|
| 1888 |
rxry := radii.X * radii.Y; |
| 1889 |
rxy1_ := radii.X * y1_; |
| 1890 |
ryx1_ := radii.Y * x1_; |
| 1891 |
sum_of_sq := rxy1_ * rxy1_ + ryx1_ * ryx1_; // sum of square |
| 1892 |
if (sum_of_sq = 0) then Exit; |
| 1893 |
|
| 1894 |
coe := Sqrt(Abs((rxry * rxry - sum_of_sq) / sum_of_sq)); |
| 1895 |
if (fA = fS) then coe := -coe; |
| 1896 |
|
| 1897 |
// F6.5.2 |
| 1898 |
cx_ := coe * rxy1_ / radii.Y; |
| 1899 |
cy_ := -coe * ryx1_ / radii.X; |
| 1900 |
|
| 1901 |
// F6.5.3 |
| 1902 |
cx := c_phi * cx_ - s_phi * cy_ + hs_x; |
| 1903 |
cy := s_phi * cx_ + c_phi * cy_ + hs_y; |
| 1904 |
|
| 1905 |
Result.Left := cx - radii.X; |
| 1906 |
Result.Right := cx + radii.X; |
| 1907 |
Result.Top := cy - radii.Y; |
| 1908 |
Result.Bottom := cy + radii.Y; |
| 1909 |
end; |
| 1910 |
//------------------------------------------------------------------------------ |
| 1911 |
|
| 1912 |
end. |