{********************************************************************}
{                                                                    }
{ written by TMS Software                                            }
{            copyright © 2013                                        }
{            Email : info@tmssoftware.com                            }
{            Web : http://www.tmssoftware.com                        }
{                                                                    }
{ The source code is given as is. The author is not responsible      }
{ for any possible damage done due to the use of this code.          }
{ The complete source code remains property of the author and may    }
{ not be distributed, published, given or sold in any form as such.  }
{ No parts of the source code can be included in any other component }
{ or application without written authorization of the author.        }
{********************************************************************}

unit FMX.TMSQuickPDFRenderLib;

interface

{$I TMSDEFS.INC}

uses
  Classes, Types, FMX.TMSPDFRenderLib, DebenuPDFLibrary;

  type
  TTMSFMXQuickPDFRenderLib = class(TTMSFMXPDFRenderLib)
  private
    FFile: String;
    FPDFLib: TDebenuPDFLibrary;
  public
    function CreateInstance: Integer; override;
    procedure DestroyInstance; override;
    procedure SetOrigin(AOrigin: Integer); override;
    procedure SetPageDimensions(AWidth, AHeight: Single); override;
    procedure NewPage; override;
    procedure BeginExport(AFileName: String); override;
    procedure EndExport; override;
    procedure SetFillColor(ARed, AGreen, ABlue: Extended); override;
    procedure SetTextColor(ARed, AGreen, ABlue: Extended); override;
    procedure SelectFont(AFontId: Integer); override;
    procedure SetTextSize(AFontSize: Single); override;
    procedure SetLineWidth(AWidth: Single); override;
    procedure SetLineColor(ARed, AGreen, ABlue: Extended); override;
    procedure DrawLine(AX, AY, AXTo, AYTo: Single); override;
    procedure AddImageFromStream(AStream: TStream); override;
    procedure DrawBox(ARect: TRectF); override;
    procedure DrawGradient(ARect: TRectF; ARed, AGreen, ABlue, ARedTo, AGreenTo, ABlueTo: Extended); override;
    procedure DrawText(AX, AY: Single; AText: String); override;
    procedure DrawImage(ARect: TRectF); override;
    function GetTextWidth(AText: String): Single; override;
    function GetTextHeight: Single; override;
    function AddTrueTypeFont(FontName: String): Integer; override;
    function UnlockKey(AUnlockKey: String): Integer; override;
    procedure SetInformation(AKey: Integer; AKeyValue: String); override;
  end;

const AUnlockKey = 'Your trial / registered license key for QuickPDF';

implementation

{ TTMSFMXQuickPDFRenderLib }

procedure TTMSFMXQuickPDFRenderLib.AddImageFromStream(AStream: TStream);
begin
  if Assigned(FPDFLib) then
    FPDFLib.AddImageFromStream(AStream, 6);
end;

function TTMSFMXQuickPDFRenderLib.AddTrueTypeFont(FontName: String): Integer;
begin
  Result := 0;
  if Assigned(FPDFLib) then
    Result := FPDFLib.AddTrueTypeFont(FontName, 3);
end;

procedure TTMSFMXQuickPDFRenderLib.BeginExport(AFileName: String);
begin
  FFile := AFileName;
end;

function TTMSFMXQuickPDFRenderLib.CreateInstance: Integer;
begin
  if Assigned(FPDFLib) then
    DestroyInstance;
  FPDFLib := TDebenuPDFLibrary.Create;
  Result := UnlockKey(AUnlockKey);
end;

procedure TTMSFMXQuickPDFRenderLib.DestroyInstance;
begin
  if Assigned(FPDFLib) then
    FPDFLib.Free;
end;

procedure TTMSFMXQuickPDFRenderLib.DrawBox(ARect: TRectF);
begin
  if Assigned(FPDFLib) then
    FPDFLib.DrawBox(ARect.Left, ARect.Top, ARect.Width, ARect.Height, 1);
end;

procedure TTMSFMXQuickPDFRenderLib.DrawGradient(ARect: TRectF; ARed, AGreen, ABlue,
  ARedTo, AGreenTo, ABlueTo: Extended);
var
  diffr,startr,endr: Extended;
  diffg,startg,endg: Extended;
  diffb,startb,endb: Extended;
  iend: Extended;
  rstepr,rstepg,rstepb,rstepw: Extended;
  i: Word;
  stepw: Extended;
  Steps: Integer;
  r: TRectf;
begin
  if Assigned(FPDFLib) then
  begin
    Steps := 32;

    startr := ARed;
    startg := AGreen;
    startb := ABlue;

    endr := ARedTo;
    endg := AGreenTo;
    endb := ABlueTo;

    diffr := endr - startr;
    diffg := endg - startg;
    diffb := endb - startb;

    rstepr := diffr / steps;
    rstepg := diffg / steps;
    rstepb := diffb / steps;

   rstepw := ARect.Height / Steps;

    for i := 0 to Steps - 1 do
    begin
      endr := startr + (rstepr * i);
      endg := startg + (rstepg * i);
      endb := startb + (rstepb * i);
      stepw := (i*rstepw);
      SetFillColor(endr, endg, endb);
      iend := ARect.Top + stepw + Trunc(rstepw) + 1;
      if iend > ARect.Bottom then
        iend := ARect.Bottom;

      r := RectF(ARect.Left,ARect.Top + stepw,ARect.Right,iend);
      DrawBox(r);
    end;
  end;
end;

procedure TTMSFMXQuickPDFRenderLib.DrawImage(ARect: TRectF);
begin
  if Assigned(FPDFLib) then
    FPDFLib.DrawImage(ARect.Left, ARect.Top, ARect.Width, ARect.Height);
end;

procedure TTMSFMXQuickPDFRenderLib.DrawLine(AX, AY, AXTo, AYTo: Single);
begin
  if Assigned(FPDFLib) then
    FPDFLib.DrawLine(AX, AY, AXTo, AYTo);
end;

procedure TTMSFMXQuickPDFRenderLib.DrawText(AX, AY: Single; AText: String);
begin
  if Assigned(FPDFLib) then
    FPDFLib.DrawText(AX, AY, AText);
end;

function TTMSFMXQuickPDFRenderLib.GetTextHeight: Single;
begin
  Result := 0;
  if Assigned(FPDFLib) then
    Result := FPDFLib.GetTextHeight;
end;

function TTMSFMXQuickPDFRenderLib.GetTextWidth(AText: String): Single;
begin
  Result := 0;
  if Assigned(FPDFLib) then
    Result := FPDFLib.GetTextWidth(AText);
end;

procedure TTMSFMXQuickPDFRenderLib.NewPage;
begin
  if Assigned(FPDFLib) then
    FPDFLib.NewPage;
end;

procedure TTMSFMXQuickPDFRenderLib.EndExport;
begin
  if Assigned(FPDFLib) then
    FPDFLib.SaveToFile(FFile);
end;

procedure TTMSFMXQuickPDFRenderLib.SelectFont(AFontId: Integer);
begin
  if Assigned(FPDFLib) then
    FPDFLib.SelectFont(AFontID);
end;

procedure TTMSFMXQuickPDFRenderLib.SetFillColor(ARed, AGreen, ABlue: Extended);
begin
  if Assigned(FPDFLib) then
    FPDFLib.SetFillColor(ARed, AGreen, ABlue);
end;

procedure TTMSFMXQuickPDFRenderLib.SetInformation(AKey: Integer;
  AKeyValue: String);
begin
  if Assigned(FPDFLib) then
    FPDFLib.SetInformation(AKey, AKeyValue);
end;

procedure TTMSFMXQuickPDFRenderLib.SetLineColor(ARed, AGreen, ABlue: Extended);
begin
  if Assigned(FPDFLib) then
    FPDFLib.SetLineColor(ARed, AGreen, ABlue);
end;

procedure TTMSFMXQuickPDFRenderLib.SetLineWidth(AWidth: Single);
begin
  if Assigned(FPDFLib) then
    FPDFLib.SetLineWidth(AWidth);
end;

procedure TTMSFMXQuickPDFRenderLib.SetOrigin(AOrigin: Integer);
begin
  if Assigned(FPDFLib) then
    FPDFLib.SetOrigin(AOrigin);
end;

procedure TTMSFMXQuickPDFRenderLib.SetPageDimensions(AWidth, AHeight: Single);
begin
  if Assigned(FPDFLib) then
    FPDFLib.SetPageDimensions(AWidth, AHeight);
end;

procedure TTMSFMXQuickPDFRenderLib.SetTextColor(ARed, AGreen, ABlue: Extended);
begin
  if Assigned(FPDFLib) then
    FPDFLib.SetTextColor(ARed, AGreen, ABlue);
end;

procedure TTMSFMXQuickPDFRenderLib.SetTextSize(AFontSize: Single);
begin
  if Assigned(FPDFLib) then
    FPDFLib.SetTextSize(AFontSize);
end;

function TTMSFMXQuickPDFRenderLib.UnlockKey(AUnlockKey: String): Integer;
begin
  Result := 0;
  if Assigned(FPDFLib) then
    Result := FPDFLib.UnlockKey(AUnLockKey);
end;

end.
