unit SVG_to_PNG;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ShellAPI, ComCtrls, Clipbrd;

type X_DWORD   = cardinal;        // Unsigned 32-bit
type X_UINT    = cardinal;        // Largest Unsigned Integer
const ErrUINT  = High(X_UINT);    // Max range for X_UINT is High(X_UINT) - 1

const EMsgCreateProcess = 'CreateProcess failed';

type
  TForm1 = class(TForm)
    editPathToInkscape: TEdit;
    editPathToImages: TEdit;
    editPathToPNGs: TEdit;
    btnGo: TButton;
    progBar: TProgressBar;
    editPathToIM: TEdit;
    procedure btnGoClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private

    function ExecuteAndWait(const CommandLine: string): X_DWORD;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  if FileExists('c:\Program Files\Inkscape\inkscape.exe') then
    editPathToInkscape.Text := 'c:\Program Files\Inkscape\inkscape.exe';
  if FileExists('c:\Program Files\ImageMagick\convert.exe') then
    editPathToIM.Text := 'c:\Program Files\ImageMagick\convert.exe';
  if DirectoryExists(ExtractFilePath(Application.ExeName) + 'images') then
    editPathToImages.Text :=  ExtractFilePath(Application.ExeName) + 'images';
  if DirectoryExists(ExtractFilePath(Application.ExeName) + 'images\png') then
    editPathToPNGs.Text :=  ExtractFilePath(Application.ExeName) + 'images\png';
end;

function TForm1.ExecuteAndWait(const CommandLine : string) : X_DWORD;
var
  tSI : TStartupInfo;
  tPI : TProcessInformation;
  dw  : X_DWORD;
begin
  Result := ErrUINT;
  FillChar(tSI, sizeof(TStartupInfo), 0);
  tSI.cb := sizeof(TStartupInfo);
  if CreateProcess(nil, pchar(CommandLine), nil, nil, False, 0, nil, nil,
tSI, tPI) then begin
    if (WaitForSingleObject(tPI.hProcess, INFINITE) = WAIT_OBJECT_0) then
begin
      if GetExitCodeProcess(tPI.hProcess, dw) then begin
        Result := dw;
      end;
    end;
    CloseHandle(tPI.hProcess);
    CloseHandle(tPI.hThread);
  end
  else begin
    MessageBox(0, pchar('Unable to launch ' + CommandLine),
pchar(EMsgCreateProcess), MB_OK OR MB_ICONSTOP);
  end;
end;

procedure TForm1.btnGoClick(Sender: TObject);
var
cmd: string;
SR: TSearchRec;
IsFound: Boolean;

  function MakeInkscapeCommand(size: string): string;
  begin
    Result := '"' + editPathToInkscape.Text + '"' +
              ' -f "' + editPathToImages.Text+ '\' + SR.Name + '"' +
              ' -e "' + editPathToPNGs.Text + '\' + size + '\' + ChangeFileExt(SR.Name, '.png') + '"' +
              ' -w ' + size + ' -h ' + size;
  end;

  function MakeCopyCommand(size: string; kind: string): string;
  begin
    Result := 'xcopy "' + editPathToPNGs.Text + '\' + size + '\*.png" "' +
              editPathToPNGs.Text + '\' + size + '_' + kind + '" ';
  end;

begin
//First, make all the bitmap images by calling Inkscape.
  IsFound :=
    FindFirst(editPathToImages.Text+'\*.svg', faAnyFile-faDirectory, SR) = 0;
    while IsFound do begin
      Application.ProcessMessages;
      cmd := MakeInkscapeCommand('16');
      ExecuteAndWait(cmd);
      Application.ProcessMessages;
      cmd := MakeInkscapeCommand('24');
      ExecuteAndWait(cmd);
      Application.ProcessMessages;
      cmd := MakeInkscapeCommand('32');
      ExecuteAndWait(cmd);
      Application.ProcessMessages;
      cmd := MakeInkscapeCommand('48');
      ExecuteAndWait(cmd);
      Application.ProcessMessages;
      IsFound := FindNext(SR) = 0;
    end;
  FindClose(SR);
//Next, copy all of the image files into each of the other state folders
//TODO: This is not working!!!! Why?
  cmd := MakeCopyCommand('16', 'disabled');
  ExecuteAndWait(cmd);
  Application.ProcessMessages;
  cmd := MakeCopyCommand('16', 'hot');
  ExecuteAndWait(cmd);
  Application.ProcessMessages;

  cmd := MakeCopyCommand('24', 'disabled');
  ExecuteAndWait(cmd);
  Application.ProcessMessages;
  cmd := MakeCopyCommand('24', 'hot');
  ExecuteAndWait(cmd);
  Application.ProcessMessages;

  cmd := MakeCopyCommand('32', 'disabled');
  ExecuteAndWait(cmd);
  Application.ProcessMessages;
  cmd := MakeCopyCommand('32', 'hot');
  ExecuteAndWait(cmd);
  Application.ProcessMessages;

  cmd := MakeCopyCommand('48', 'disabled');
  ExecuteAndWait(cmd);
  Application.ProcessMessages;
  cmd := MakeCopyCommand('48', 'hot');
  ExecuteAndWait(cmd);
  Application.ProcessMessages; 
end;

end.
