File: tadrawutils.pas

package info (click to toggle)
lazarus 2.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 214,460 kB
  • sloc: pascal: 1,862,622; xml: 265,709; cpp: 56,595; sh: 3,008; java: 609; makefile: 535; perl: 297; sql: 222; ansic: 137
file content (799 lines) | stat: -rw-r--r-- 22,364 bytes parent folder | download | duplicates (2)
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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
{
 *****************************************************************************
  See the file COPYING.modifiedLGPL.txt, included in this distribution,
  for details about the license.
 *****************************************************************************

  Authors: Alexander Klenin

}

unit TADrawUtils;

{$H+}

interface

uses
  Classes, FPCanvas, FPImage, Types, TAChartUtils;

type
  // Same types as in Graphics unit, but without dependency.
  TChartAntialiasingMode = (amDontCare, amOn, amOff);

type
  ISimpleTextOut = interface
    function HtmlTextExtent(const AText: String): TPoint;
    procedure HtmlTextOut(AX, AY: Integer; const AText: String);
    procedure SimpleTextOut(AX, AY: Integer; const AText: String);
    function SimpleTextExtent(const AText: String): TPoint;
    function GetFontAngle: Double;
  end;

  { TChartTextOut }

  TChartTextOut = class
  strict private
    FAlignment: TAlignment;
    FPos: TPoint;
    FSimpleTextOut: ISimpleTextOut;
    FText1: String;
    FText2: TStrings;
    FTextFormat: TChartTextFormat;
    FWidth: Integer;

    procedure DoTextOutList;
    procedure DoTextOutString;
  public
    constructor Create(ASimpleTextOut: ISimpleTextOut);
  public
    function Alignment(AAlignment: TAlignment): TChartTextOut;
    procedure Done;
    function Pos(AX, AY: Integer): TChartTextOut;
    function Pos(const APos: TPoint): TChartTextOut;
    function Text(const AText: String): TChartTextOut;
    function Text(AText: TStrings): TChartTextOut;
    function TextFormat(AFormat: TChartTextFormat): TChartTextOut;
    function Width(AWidth: Integer): TChartTextOut;
  end;

  TChartColorToFPColorFunc = function (AColor: TChartColor): TFPColor;
  TGetFontOrientationFunc = function (AFont: TFPCustomFont): Integer;

  TChartTransparency = 0..255;

  TScaleItem = (scaleFont, scalePen);
  TScaleItems = set of TScaleItem;

  IChartDrawer = interface
    ['{6D8E5591-6788-4D2D-9FE6-596D5157C3C3}']
    procedure AddToFontOrientation(ADelta: Integer);
    procedure ClippingStart(const AClipRect: TRect);
    procedure ClippingStart;
    procedure ClippingStop;
    procedure DrawingBegin(const ABoundingBox: TRect);
    procedure DrawingEnd;
    procedure DrawLineDepth(AX1, AY1, AX2, AY2, ADepth: Integer);
    procedure DrawLineDepth(const AP1, AP2: TPoint; ADepth: Integer);
    procedure Ellipse(AX1, AY1, AX2, AY2: Integer);
    procedure FillRect(AX1, AY1, AX2, AY2: Integer);
    function GetBrushColor: TChartColor;
    function GetFontAngle: Double;       // in radians
    function GetFontColor: TFPColor;
    function GetFontName: String;
    function GetFontSize: Integer;
    function GetFontStyle: TChartFontStyles;
    procedure SetDoChartColorToFPColorFunc(AValue: TChartColorToFPColorFunc);
    procedure Line(AX1, AY1, AX2, AY2: Integer);
    procedure Line(const AP1, AP2: TPoint);
    procedure LineTo(AX, AY: Integer);
    procedure LineTo(const AP: TPoint);
    procedure MoveTo(AX, AY: Integer);
    procedure MoveTo(const AP: TPoint);
    procedure Polygon(
      const APoints: array of TPoint; AStartIndex, ANumPts: Integer);
    procedure Polyline(
      const APoints: array of TPoint; AStartIndex, ANumPts: Integer);
    procedure PrepareSimplePen(AColor: TChartColor);
    procedure PutImage(AX, AY: Integer; AImage: TFPCustomImage);
    procedure PutPixel(AX, AY: Integer; AColor: TChartColor);
    procedure RadialPie(
      AX1, AY1, AX2, AY2: Integer;
      AStartAngle16Deg, AAngleLength16Deg: Integer);
    procedure Rectangle(const ARect: TRect);
    procedure Rectangle(AX1, AY1, AX2, AY2: Integer);
    procedure ResetFont;
    function Scale(ADistance: Integer): Integer;
    procedure SetAntialiasingMode(AValue: TChartAntialiasingMode);
    procedure SetBrushColor(AColor: TChartColor);
    procedure SetBrush(ABrush: TFPCustomBrush);
    procedure SetBrushParams(AStyle: TFPBrushStyle; AColor: TChartColor);
    procedure SetFont(AValue: TFPCustomFont);
    procedure SetGetFontOrientationFunc(AValue: TGetFontOrientationFunc);
    procedure SetMonochromeColor(AColor: TChartColor);
    procedure SetPen(APen: TFPCustomPen);
    procedure SetPenParams(AStyle: TFPPenStyle; AColor: TChartColor);
    function GetRightToLeft: Boolean;
    procedure SetRightToLeft(AValue: Boolean);
    procedure SetTransparency(ATransparency: TChartTransparency);
    procedure SetXor(AXor: Boolean);
    function TextExtent(const AText: String;
      ATextFormat: TChartTextFormat = tfNormal): TPoint;
    function TextExtent(AText: TStrings;
      ATextFormat: TChartTextFormat = tfNormal): TPoint;
    function TextOut: TChartTextOut;

    property Brush: TFPCustomBrush write SetBrush;
    property BrushColor: TChartColor read GetBrushColor write SetBrushColor;
    property Font: TFPCustomFont write SetFont;
    property Pen: TFPCustomPen write SetPen;
    property DoChartColorToFPColor: TChartColorToFPColorFunc
      write SetDoChartColorToFPColorFunc;
    property DoGetFontOrientation: TGetFontOrientationFunc
      write SetGetFontOrientationFunc;
  end;

  { TBasicDrawer }

  TBasicDrawer = class(TInterfacedObject, ISimpleTextOut)
  strict protected
    FChartColorToFPColorFunc: TChartColorToFPColorFunc;
    FGetFontOrientationFunc: TGetFontOrientationFunc;
    FMonochromeColor: TChartColor;
    FRightToLeft: Boolean;
    FTransparency: TChartTransparency;
    FXor: Boolean;
    FScaleItems: TScaleItems;
    function ColorOrMono(AColor: TChartColor): TChartColor; inline;
    function FPColorOrMono(const AColor: TFPColor): TFPColor; inline;
//    function GetFontAngle: Double; virtual; abstract;
    function SimpleTextExtent(const AText: String): TPoint; virtual; abstract;
    procedure SimpleTextOut(AX, AY: Integer; const AText: String); virtual; abstract;
    function HtmlTextExtent(const AText: String): TPoint;
    procedure HtmlTextOut(AX, AY: Integer; const AText: String);
  public
    constructor Create;
    procedure DrawingBegin(const ABoundingBox: TRect); virtual;
    procedure DrawingEnd; virtual;
    procedure DrawLineDepth(AX1, AY1, AX2, AY2, ADepth: Integer);
    procedure DrawLineDepth(const AP1, AP2: TPoint; ADepth: Integer);
    function GetFontAngle: Double; virtual; abstract;
    function GetFontColor: TFPColor; virtual; abstract;
    function GetFontName: String; virtual; abstract;
    function GetFontSize: Integer; virtual; abstract;
    function GetFontStyle: TChartFontStyles; virtual; abstract;
    function GetRightToLeft: Boolean;
    procedure LineTo(AX, AY: Integer); virtual; abstract; overload;
    procedure LineTo(const AP: TPoint); overload;
    procedure MoveTo(AX, AY: Integer); virtual; abstract; overload;
    procedure MoveTo(const AP: TPoint); overload;
    procedure Polygon(
      const APoints: array of TPoint; AStartIndex, ANumPts: Integer); virtual; abstract;
    procedure PutImage(AX, AY: Integer; AImage: TFPCustomImage); virtual;
    procedure PutPixel(AX, AY: Integer; AColor: TChartColor); virtual;
    function Scale(ADistance: Integer): Integer; virtual;
    procedure SetAntialiasingMode(AValue: TChartAntialiasingMode);
    procedure SetDoChartColorToFPColorFunc(AValue: TChartColorToFPColorFunc);
    procedure SetGetFontOrientationFunc(AValue: TGetFontOrientationFunc);
    procedure SetMonochromeColor(AColor: TChartColor);
    procedure SetRightToLeft(AValue: Boolean);
    procedure SetTransparency(ATransparency: TChartTransparency);
    procedure SetXor(AXor: Boolean);
    function TextExtent(const AText: String; ATextFormat: TChartTextFormat = tfNormal): TPoint; overload;
    function TextExtent(AText: TStrings; ATextFormat: TChartTextFormat = tfNormal): TPoint; overload;
    function TextOut: TChartTextOut;
  end;

  function ChartColorToFPColor(AChartColor: TChartColor): TFPColor;
  function FPColorToChartColor(AFPColor: TFPColor): TChartColor;
  function ColorDef(AColor, ADefaultColor: TChartColor): TChartColor; inline;

implementation

uses
  Math, fasthtmlparser, htmlutil, TAGeometry, TAHtml;

const
  LINE_INTERVAL = 2;

  SUBSUP_DIVISOR = 100;
  SUBSUP_SIZE_MULTIPLIER = 70; //75;
  SUB_OFFSET_MULTIPLIER = 70; //80;
  SUP_OFFSET_MULTIPLIER = -5;

type
  THTMLAnalyzer = class
  private
    FSubscript: Integer;
    FSuperscript: Integer;
    FFontStack: TFPList;
    FDrawer: IChartDrawer;
    FSize: TPoint;
    FPos: TPoint;
    FRotPos: TPoint;
    FCurrentFont: TFPCustomFont;
    FSavedFont: TFPCustomFont;
    FFontAngle: Double;
  protected
    procedure ClearFontStack;
    procedure HTMLTagFound(NoCaseTag, ActualTag: String);
    procedure HTMLTextFound_Size(AText: String);
    procedure HTMLTextFound_Out(AText: String);
    procedure Init;
    procedure PopFont;
    procedure PushFont;
  public
    constructor Create(ADrawer: IChartDrawer);
    destructor Destroy; override;
    function TextExtent(const AText: String): TPoint;
    procedure TextOut(AX, AY: Integer; const AText: String);
  end;

{ THTMLAnalyzer }

constructor THTMLAnalyzer.Create(ADrawer: IChartDrawer);
begin
  FDrawer := ADrawer;
  FSavedFont := TFPCustomFont.Create;
  FFontStack := TFPList.Create;
end;

destructor THTMLAnalyzer.Destroy;
var
  j: Integer;
begin
  for j:=0 to FFontStack.Count-1 do TFPCustomFont(FFontStack[j]).Free;
  FFontStack.Free;
  FCurrentFont.Free;
  FSavedFont.Free;
  inherited;
end;

procedure THTMLAnalyzer.ClearFontStack;
var
  j: Integer;
begin
  for j:=0 to FFontStack.Count-1 do TFPCustomFont(FFontStack[j]).Free;
  FFontStack.Clear;
end;

procedure THTMLAnalyzer.HTMLTagFound(NoCaseTag, ActualTag: String);
var
  val: String;
begin
  Unused(ActualTag);

  if NoCaseTag[2] = '/' then
    case NoCaseTag of
      '</B>',
      '</STRONG>',
      '</I>',
      '</EM>',
      '</U>',
      '</S>',
      '</FONT>':
        PopFont;
      '</SUB>':
        dec(FSubscript);
      '</SUP>':
        dec(FSuperscript);
    end
  else begin
    case NoCaseTag of
      '<B>', '<STRONG>':
        begin
          PushFont;
          FCurrentFont.Bold := true;
        end;
      '<I>', '<EM>':
        begin
          PushFont;
          FCurrentFont.Italic := true;
        end;
      '<U>':
        begin
          PushFont;
          FCurrentFont.Underline := true;
        end;
      '<S>':
        begin
          PushFont;
          FCurrentFont.StrikeThrough := true;
        end;
      '<SUB>':
        begin    // Don't push the font to the stack
          inc(FSubscript);
        end;
      '<SUP>':
        begin // Don't push the font to the stack
          inc(FSuperscript);
        end;
      else
        if (pos('<FONT ', NoCaseTag) = 1) or (NoCaseTag = '<FONT>') then begin
          PushFont;
          val := GetVal(NoCaseTag, 'NAME');
          if val <> '' then
            FCurrentFont.Name := val;
          {$IFDEF HTML_FONT_SIZE}
          val := GetVal(NoCaseTag, 'SIZE');
          if val <> '' then
            FCurrentFont.Size := HTMLToFontSize(val);
          {$ENDIF}
          val := GetVal(NoCaseTag, 'COLOR');
          if val <> '' then
            FCurrentFont.FPColor := HTMLToFPColor(val);
        end else
          exit;
    end;
  end;
end;

procedure THTMLAnalyzer.HTMLTextFound_Out(AText: String);
var
  oldFontSize: Integer;
  offs: Integer;
  s: string;
  P: TPoint;
  w, h: Integer;
begin
  s := ReplaceHTMLEntities(AText);

  if (FSubScript > 0) or (FSuperScript > 0) then
  begin
    oldFontSize := FCurrentFont.Size;
    FCurrentFont.Size := (FCurrentFont.Size * SUBSUP_SIZE_MULTIPLIER) div SUBSUP_DIVISOR;
    FDrawer.SetFont(FCurrentFont);
    h := FDrawer.TextExtent('Tg', tfNormal).Y;  // tfNormal is correct
    w := FDrawer.TextExtent(s, tfNormal).X;
    if FSubScript > 0 then
      offs := (h * SUB_OFFSET_MULTIPLIER) div SUBSUP_DIVISOR
    else
      offs := (h * SUP_OFFSET_MULTIPLIER) div SUBSUP_DIVISOR;   // this is negative
    P := Point(FPos.X, FPos.Y+offs) - FRotPos;
    p := RotatePoint(P, -FFontAngle) + FRotPos;
    FDrawer.TextOut.TextFormat(tfNormal).Pos(P).Text(s).Done;
    FCurrentFont.Size := oldFontSize;
  end else
  begin
    FDrawer.SetFont(FCurrentFont);
    w := FDrawer.TextExtent(s, tfNormal).X;       // tfNormal is correct
    p := RotatePoint(FPos - FRotPos, -FFontAngle) + FRotPos;
    FDrawer.TextOut.TextFormat(tfNormal).Pos(P).Text(s).Done;
  end;
  inc(FPos.X, w);
end;

procedure THTMLAnalyzer.HTMLTextFound_Size(AText: String);
var
  ext: TPoint;
  oldFontSize: Integer;
  s: String;
  offs: Integer;
begin
  s := ReplaceHTMLEntities(AText);
  if (FSubScript > 0) or (FSuperscript > 0) then
  begin
    oldFontSize := FCurrentFont.Size;
    FCurrentFont.Size := FCurrentFont.Size * SUBSUP_SIZE_MULTIPLIER div SUBSUP_DIVISOR;
    FDrawer.SetFont(FCurrentFont);
    ext := FDrawer.TextExtent(s, tfNormal);  // tfNormal is correct
    FCurrentFont.Size := oldFontSize;
    if FSubScript > 0 then
    begin
      offs := (ext.y * SUB_OFFSET_MULTIPLIER) div SUBSUP_DIVISOR;
      if ext.y + offs > FSize.Y then ext.Y := ext.y + offs;
    end else
    begin
      offs := (ext.y * SUP_OFFSET_MULTIPLIER) div SUBSUP_DIVISOR;   // this is negative
      if ext.y - offs > FSize.Y then ext.Y := ext.y - offs;   // offs is negative
    end;
  end else
  begin
    FDrawer.SetFont(FCurrentFont);
    ext := FDrawer.TextExtent(s, tfNormal);  // tfNormal is correct
  end;
  FSize.X := FSize.X + ext.X;
  FSize.Y := Max(FSize.Y, ext.Y);
end;

procedure THTMLAnalyzer.Init;
begin
  FFontAngle := FDrawer.GetFontAngle;

  FSavedFont.Name := FDrawer.GetFontName;
  FSavedFont.Size := FDrawer.GetFontSize;
  FSavedFont.FPColor := FDrawer.GetFontColor;
  FSavedFont.Bold := cfsBold in FDrawer.GetFontStyle;
  FSavedFont.Italic := cfsItalic in FDrawer.GetFontStyle;
  FSavedFont.Underline := cfsUnderline in FDrawer.GetFontStyle;
  FSavedFont.StrikeThrough := cfsStrikeOut in FDrawer.GetFontStyle;
  FSavedFont.Orientation := RadToOrient(FFontAngle);

  FCurrentFont := FSavedFont.CopyFont;
  FCurrentFont.Orientation := FSavedFont.Orientation;
  ClearFontStack;

  FSubscript := 0;
  FSuperscript := 0;
end;

procedure THTMLAnalyzer.PopFont;
begin
  FCurrentFont.Free;
  FCurrentFont := TFPCustomFont(FFontStack[FFontStack.Count-1]);
  FFontStack.Delete(FFontStack.Count-1);
end;

procedure THTMLAnalyzer.PushFont;
var
  fnt: TFPCustomFont;
begin
  fnt := FCurrentFont.CopyFont;
  fnt.Orientation := FCurrentFont.Orientation;
  FFontStack.Add(fnt);
end;

function THTMLAnalyzer.TextExtent(const AText: String): TPoint;
var
  parser: THTMLParser;
begin
  Init;
  FSize := Point(0, 0);
  parser := THTMLParser.Create('<p>' + AText + '</p>');
  try
    parser.OnFoundTag := @HTMLTagFound;
    parser.OnFoundText := @HTMLTextFound_Size;
    parser.Exec;
    Result := FSize;
  finally
    parser.Free;
    FDrawer.SetFont(FSavedFont);
  end;
end;

procedure THTMLAnalyzer.TextOut(AX, AY: Integer; const AText: String);
var
  parser: THTMLParser;
begin
  Init;
  FRotPos := Point(AX, AY);
  FPos := Point(AX, AY);
  parser := THTMLParser.Create('<p>' + AText + '</p>');
  try
    parser.OnFoundTag := @HTMLTagFound;
    parser.OnFoundText := @HTMLTextFound_Out;
    parser.Exec;
  finally
    parser.Free;
    FDrawer.SetFont(FSavedFont);
  end;
end;


{ Utilities }

function ChartColorToFPColor(AChartColor: TChartColor): TFPColor;
begin
  with Result do begin
    red := AChartColor and $FF;
    red += red shl 8;
    green := (AChartColor and $FF00);
    green += green shr 8;
    blue := (AChartColor and $FF0000) shr 8;
    blue += blue shr 8;
    alpha := alphaOpaque;
  end;
end;

function DummyGetFontOrientationFunc(AFont: TFPCustomFont): Integer;
begin
  Unused(AFont);
  Result := 0;
end;

function FPColorToChartColor(AFPColor: TFPColor): TChartColor;
begin
  Result :=
    ((AFPColor.red shr 8) and $FF) or
    (AFPColor.green and $FF00) or
    ((AFPColor.blue shl 8) and $FF0000);
end;

function ColorDef(AColor, ADefaultColor: TChartColor): TChartColor;
begin
  Result := IfThen(AColor = clTAColor, ADefaultColor, AColor);
end;

{ TChartTextOut }

function TChartTextOut.Alignment(AAlignment: TAlignment): TChartTextOut;
begin
  FAlignment := AAlignment;
  Result := Self;
end;

constructor TChartTextOut.Create(ASimpleTextOut: ISimpleTextOut);
begin
  FSimpleTextOut := ASimpleTextOut;
  FAlignment := taLeftJustify;
end;

procedure TChartTextOut.Done;
begin
  if FText2 = nil then
    DoTextOutString
  else
    DoTextOutList;
  Free;
end;

procedure TChartTextOut.DoTextOutList;
var
  i: Integer;
  a: Double;
  lineExtent, p: TPoint;
begin
  a := -FSimpleTextOut.GetFontAngle;
  for i := 0 to FText2.Count - 1 do begin
    case FTextFormat of
      tfNormal: lineExtent := FSimpleTextOut.SimpleTextExtent(FText2[i]);
      tfHtml  : lineExtent := FSimpleTextOut.HtmlTextExtent(FText2[i]);
    end;
    p := FPos;
    case FAlignment of
      taCenter: p += RotatePointX((FWidth - lineExtent.X) div 2, a);
      taRightJustify: p += RotatePointX(FWidth - lineExtent.X, a);
    end;
    case FTextFormat of
      tfNormal: FSimpleTextOut.SimpleTextOut(p.X, p.Y, FText2[i]);
      tfHtml  : FSimpleTextOut.HtmlTextOut(p.X, p.Y, FText2[i]);
    end;
    FPos += RotatePoint(Point(0, lineExtent.Y + LINE_INTERVAL), a);
  end;
end;

procedure TChartTextOut.DoTextOutString;
begin
  if System.Pos(LineEnding, FText1) = 0 then begin
    case FTextFormat of
      tfNormal: FSimpleTextOut.SimpleTextOut(FPos.X, FPos.Y, FText1);
      tfHtml  : FSimpleTextOut.HtmlTextOut(FPos.X, FPos.Y, FText1);
    end;
    exit;
  end;
  FText2 := TStringList.Create;
  try
    FText2.Text := FText1;
    DoTextOutList;
  finally
    FText2.Free;
  end;
end;

function TChartTextOut.Pos(AX, AY: Integer): TChartTextOut;
begin
  FPos := Point(AX, AY);
  Result := Self;
end;

function TChartTextOut.Pos(const APos: TPoint): TChartTextOut;
begin
  FPos := APos;
  Result := Self;
end;

function TChartTextOut.Text(const AText: String): TChartTextOut;
begin
  FText1 := AText;
  Result := Self;
end;

function TChartTextOut.Text(AText: TStrings): TChartTextOut;
begin
  FText2 := AText;
  Result := Self;
end;

function TChartTextOut.TextFormat(AFormat: TChartTextFormat): TChartTextOut;
begin
  FTextFormat := AFormat;
  Result := Self;
end;

function TChartTextOut.Width(AWidth: Integer): TChartTextOut;
begin
  FWidth := AWidth;
  Result := Self;
end;

{ TBasicDrawer }

function TBasicDrawer.ColorOrMono(AColor: TChartColor): TChartColor;
begin
  Result := ColorDef(FMonochromeColor, AColor);
end;

constructor TBasicDrawer.Create;
begin
  FChartColorToFPColorFunc := @ChartColorToFPColor;
  FGetFontOrientationFunc := @DummyGetFontOrientationFunc;
  FMonochromeColor := clTAColor;
end;

procedure TBasicDrawer.DrawingBegin(const ABoundingBox: TRect);
begin
  Unused(ABoundingBox);
end;

procedure TBasicDrawer.DrawingEnd;
begin
  // Empty
end;

procedure TBasicDrawer.DrawLineDepth(AX1, AY1, AX2, AY2, ADepth: Integer);
begin
  DrawLineDepth(Point(AX1, AY1), Point(AX2, AY2), ADepth);
end;

procedure TBasicDrawer.DrawLineDepth(const AP1, AP2: TPoint; ADepth: Integer);
var
  d: TPoint;
begin
  d := Point(ADepth, -ADepth);
  Polygon([AP1, AP1 + d, AP2 + d, AP2], 0, 4);
end;

function TBasicDrawer.FPColorOrMono(const AColor: TFPColor): TFPColor;
begin
  if FMonochromeColor = clTAColor then
    Result := AColor
  else
    Result := FChartColorToFPColorFunc(FMonochromeColor);
end;

function TBasicDrawer.GetRightToLeft: Boolean;
begin
  Result := FRightToLeft;
end;

function TBasicDrawer.HtmlTextExtent(const AText: String): TPoint;
var
  IDrawer: IChartDrawer;
begin
  IDrawer := Self as IChartDrawer;
//  GetInterface('IChartDrawer', IDrawer);
  with THtmlAnalyzer.Create(IDrawer) do
    try
      Result := TextExtent(AText);
    finally
      Free;
    end;
end;

procedure TBasicDrawer.HtmlTextOut(AX, AY: Integer; const AText: String);
var
  IDrawer: IChartDrawer;
begin
  IDrawer := Self as IChartDrawer;
//  GetInterface('IChartDrawer', IDrawer);
  with THtmlAnalyzer.Create(IDrawer) do
    try
      TextOut(AX, AY, AText);
    finally
      Free;
    end;
end;

procedure TBasicDrawer.LineTo(const AP: TPoint);
begin
  LineTo(AP.X, AP.Y)
end;

procedure TBasicDrawer.MoveTo(const AP: TPoint);
begin
  MoveTo(AP.X, AP.Y)
end;

procedure TBasicDrawer.PutImage(AX, AY: Integer; AImage: TFPCustomImage);
begin
  Unused(AX, AY);
  Unused(AImage);
end;

procedure TBasicDrawer.PutPixel(AX, AY: Integer; AColor: TChartColor);
begin
  Unused(AX, AY);
  Unused(AColor);
end;

function TBasicDrawer.Scale(ADistance: Integer): Integer;
begin
  Result := ADistance;
end;

procedure TBasicDrawer.SetAntialiasingMode(AValue: TChartAntialiasingMode);
begin
  Unused(AValue);
end;

procedure TBasicDrawer.SetDoChartColorToFPColorFunc(
  AValue: TChartColorToFPColorFunc);
begin
  FChartColorToFPColorFunc := AValue;
end;

procedure TBasicDrawer.SetGetFontOrientationFunc(
  AValue: TGetFontOrientationFunc);
begin
  FGetFontOrientationFunc := AValue;
end;

procedure TBasicDrawer.SetMonochromeColor(AColor: TChartColor);
begin
  FMonochromeColor := AColor;
end;

procedure TBasicDrawer.SetRightToLeft(AValue: Boolean);
begin
  FRightToLeft := AValue;
end;

procedure TBasicDrawer.SetTransparency(ATransparency: TChartTransparency);
begin
  FTransparency := ATransparency;
end;

procedure TBasicDrawer.SetXor(AXor: Boolean);
begin
  FXor := AXor;
end;

function TBasicDrawer.TextExtent(const AText: String;
  ATextFormat: TChartTextFormat = tfNormal): TPoint;
var
  sl: TStrings;
begin
  if Pos(LineEnding, AText) = 0 then
    case ATextFormat of
      tfNormal: exit(SimpleTextExtent(AText));
      tfHTML  : exit(HtmlTextExtent(AText));
    end;

  sl := TStringList.Create;
  try
    sl.Text := AText;
    Result := TextExtent(sl, ATextFormat);
  finally
    sl.Free;
  end;
end;

function TBasicDrawer.TextExtent(AText: TStrings;
  ATextFormat: TChartTextFormat = tfNormal): TPoint;
var
  i: Integer;
begin
  Result := Size(0, -LINE_INTERVAL);
  case ATextFormat of
    tfNormal:
      for i := 0 to AText.Count - 1 do
        with SimpleTextExtent(AText[i]) do begin
          Result.X := Max(Result.X, X);
          Result.Y += Y + LINE_INTERVAL;
        end;
    tfHtml:
      for i := 0 to AText.Count - 1 do
        with HtmlTextExtent(AText[i]) do begin
          Result.X := Max(Result.X, X);
          Result.Y += Y + LINE_INTERVAL;
        end;
  end;
end;

function TBasicDrawer.TextOut: TChartTextOut;
begin
  Result := TChartTextOut.Create(Self);
end;

end.