function fonts = listfonts(handle)
%LISTFONTS Get list of available system fonts in cell array.
%   C = LISTFONTS returns list of available system fonts.
%
%   C = LISTFONTS(NAME) returns system fonts with given font name
%   sorted in.
%
%   See also UISETFONT.

%   Copyright 1984-2000 The MathWorks, Inc.
%   $Revision: 1.10 $  $Date: 2000/06/02 04:41:58 $

persistent systemfonts;

if nargin == 1
    try
        currentfont = {get(handle, 'FontName')};
    catch
        currentfont = {''};
    end
else
    currentfont = {''};
end

if isempty(systemfonts)
    if (ispc)
        perlCommand = fullfile(matlabroot, 'sys', 'perl', 'win32', 'bin', 'perl');
        [dir, name] = fileparts(which(mfilename));
        oldDir = cd;
        % dos command does not work with UNC paths, check if we are in a
        % UNC dir
        if length(oldDir) > 2 & all(oldDir(1:2) == '\\')
            % move to a directory that's safe for dos cmd - tempdir should be OK
            if ~isempty(tempdir) & exist(tempdir) == 7
                cd(tempdir)
            end
        end
        
        [s, result] = dos([perlCommand ' ' fullfile(dir, 'private', 'listwin32fonts.pl')]);

        if ~strcmp(cd, oldDir)
            cd(oldDir);
        end
        
        if (s == 0 & ~isempty(result)  & isempty(findstr(result,'The name specified is not recognized')) & ...
            isempty(findstr(result,'Bad command or file name')))
            [font, rem] = strtok(result, char(10));
            fonts{1} = font;
            i = 1;
            while (~isempty(rem))
                i = i + 1;
                [font, rem] = strtok(rem, char(10));
                if (~isempty(rem))
                    fonts{i} = font;
                end
            end
        else
            fonts = {''};
        end
    else
        perlCommand = 'perl';
        [dir, name] = fileparts(which(mfilename));
        [s, result] = unix([perlCommand ' ' fullfile(dir, 'private', 'listunixfonts.pl')]);
        if (s == 0 & ~isempty(result) & isempty(findstr(result,'Command not found')) & ...
            isempty(findstr(result,'unable to open display')))
            [font, rem] = strtok(result, char(10));
            fonts{1} = font;
            i = 1;
            while (~isempty(rem))
                i = i + 1;
                [font, rem] = strtok(rem, char(10));
                if (~isempty(rem))
                    fonts{i} = font;
                end
            end
        else
            fonts = {''};
        end
    end

    fonts = fonts';
    % add postscipt fonts to the system fonts list. these font names are
    % defined in HG source code
    systemfonts = [fonts; 
                   {
                    'AvantGarde';
                    'Bookman';
                    'Courier';
                    'Helvetica';
                    'Helvetica-Narrow'; 
                    'NewCenturySchoolBook';
                    'Palatino';
                    'Symbol';
                    'Times';
                    'ZapfChancery';
                    'ZapfDingbats'; 
                   }];


end

% add the current font to the system font list if it's there
if isempty(currentfont{1})
    fonts = systemfonts;
else
    fonts = [systemfonts; currentfont];
end

% return a sorted and unique font list to the user
[f,i] = unique(lower(fonts));
fonts = fonts(i);
