unit UGerenciador;
interface
uses RDprint, IniFiles, Dialogs, Sysutils;

type
  TGerenciadorNF = class
  private
    FRDPrint: TRDPrint;
    FINI: string;
    procedure SetRDPrint(const Value: TRDPrint);
    procedure SetINI(const Value: string);
    function GetIntegerIni(lcSessao, lcSubSessao: String): Integer;
    function  SeSenao(llCondicao: Boolean; lvSe, lvSenao: Variant) : Variant;
  public
    function BeginDoc: boolean;
    procedure EndDoc;
    procedure Print(lcSessao, lcTexto: String);
  published
    property RDPrint: TRDPrint read FRDPrint write SetRDPrint;
    property INI: string read FINI write SetINI;
  end;

implementation

{ TGerenciadorNF }

function TGerenciadorNF.BeginDoc: boolean;
var
  lnItens: Integer;
  lnLPP  : Integer;
  lnCPP  : Integer;

begin
  // inicializa como False
  result := false;

  // valida o nmero de itens
  lnItens := GetIntegerIni('PAGINA', 'ITENS');
  if lnItens <= 0 then
    begin
    ShowMessage('O campo ITENS na sesso PAGINA no foi informado ou est zerado');
    exit;
    end;

  // linhas por polegada
  lnLPP := GetIntegerIni('PAGINA', 'LINHAS_POR_POLEGADA');
  if (lnLPP <> 6) and (lnLPP <> 8) then
    begin
    ShowMessage('O campo LINHAS_POR_POLEGADA s pode conter 6 ou 8');
    exit;
    end;

  // caracteres por polegada
  lnCPP := GetIntegerIni('PAGINA', 'CARACTERES_POR_POLEGADA');
  if (lnCPP <> 10) and (lnCPP <> 12) and (lnCPP <> 17) and (lnCPP <> 20) then
    begin
    ShowMessage('O campo CARACTERES_POR_POLEGADA s pode conter 10,12,17 ou 20');
    exit;
    end;

  // configura o componente RDPrint
  FRDPrint.TamanhoQteLinhas       := GetIntegerIni('PAGINA', 'LINHAS');
  FRDPrint.TamanhoQteColunas      := GetIntegerIni('PAGINA', 'COLUNAS');
  FRDPrint.TamanhoQteLPP          := SeSenao(lnLPP=6, Seis, Oito);
  FRDPrint.UsaGerenciadorImpr     := true;
  FRDPrint.OpcoesPreview.Preview  := true;
  FRDPrint.OpcoesPreview.Remalina := true;

  case lnCPP of
    10: FRDPrint.FonteTamanhoPadrao := S10cpp;
    12: FRDPrint.FonteTamanhoPadrao := S12cpp;
    17: FRDPrint.FonteTamanhoPadrao := S17cpp;
    20: FRDPrint.FonteTamanhoPadrao := S20cpp;
  end;

  // chama a tela de setup
  FRDPrint.Abrir;
  if not FRDPrint.Setup then
    begin
    FRDPrint.Fechar;
    exit;
    end;

  result := true;
end;

procedure TGerenciadorNF.EndDoc;
begin
  if assigned(FRDPrint) then
    FRDPrint.Fechar;
end;

function TGerenciadorNF.GetIntegerIni(lcSessao, lcSubSessao: String): Integer;
var
  loIni: TInifile;
begin
  loIni := TIniFile.Create(FINI);
  try
    Result := loIni.ReadInteger(lcSessao, lcSubSessao, 0);
  finally
    loIni.Free;
    end;
end;

procedure TGerenciadorNF.Print(lcSessao, lcTexto: String);
var
  lnColuna: Integer;
  lnLinha: Integer;
  lnLargura: Integer;
  lcTemp: String;
  ltFonte: TFonte;
begin
  try
    lnColuna := GetIntegerIni(lcSessao, 'EIXO_X');
    lnLinha  := GetIntegerIni(lcSessao, 'EIXO_Y');
    lnLargura:= GetIntegerIni(lcSessao, 'TAMANHO'); // tam. do texto que ser copiado

    // o RDPrint no aceita linha 0
    if lnLinha <= 0 then
      begin
      ShowMessage('A linha para o campo ' + lcSessao + ' dever ser maior do que zero');
      exit;
      end;

    // o RDPrint no aceita coluna 0
    if lnColuna <= 0 then
      begin
      ShowMessage('A coluna para o campo ' + lcSessao + ' dever ser maior do que zero');
      exit;
      end;

    // configurao da fonte
    ltFonte := [normal];

    // se a largura no foi informada, o que acontece na maioria dos casos
    // s usa uma nica linha
    if lnLargura = 0 then
      begin
      // usa todo o texto
      FRDPrint.impF(lnLinha, lnColuna, lcTexto, ltFonte);
      end
    else
      begin
      // posio inicial
      lcTemp := Copy(lcTexto, 1, lnLargura);
      // envia o texto
      FRDPrint.impF(lnLinha, lnColuna, lcTemp, ltFonte);
      end;
  except
    on E: Exception do
      ShowMessage(E.Message + ' na funo Imprime');
  end;
end;

function TGerenciadorNF.SeSenao(llCondicao: Boolean; lvSe,
  lvSenao: Variant): Variant;
begin
  Result := lvSe;
  if not llCondicao then
    Result := lvSenao;
end;

procedure TGerenciadorNF.SetINI(const Value: string);
begin
  FINI := Value;
end;

procedure TGerenciadorNF.SetRDPrint(const Value: TRDPrint);
begin
  FRDPrint := Value;
end;

end.
