function fonts = listfonts(handle)
%LISTFONTS Get list of available system fonts in cell array.
%   C = LISTFONTS returns list of available system fonts.
%
%   C = LISTFONTS(H) returns system fonts with object's FontName
%   sorted into the list.
%
%   Examples:
%     Example1:
%       list = listfonts
%
%     Example2:
%       h = uicontrol('Style', 'text', 'string', 'My Font');
%       list = listfonts(h)
%
%   See also UISETFONT.

%   Copyright 1984-2021 The MathWorks, Inc.

persistent systemfonts;
if nargin == 1
    try
        currentfont = {get(handle, 'FontName')};
    catch ME %#ok<NASGU>
        currentfont = {''};
    end
else
    currentfont = {''};
end

isjava = usejava('awt');

if isempty(systemfonts)
    if matlab.ui.internal.dialog.DialogUtils.checkDecaf
        fonts = getStandardFonts();
    elseif isjava
        fontlist = com.mathworks.mwswing.FontUtils.getFontNames.toArray();
        fonts = cell(fontlist);
    else
        fonts = {};
    end
    
    % always add postscipt fonts to the system fonts list.
    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));  %#ok
fonts = fonts(i);
end

function fonts = getStandardFonts()
% returns fonts as defined in
% toolbox/matlab/graphics/web/web/scene/js/MW/graphics/system/detail/FontUtils.js
% Once a QT alternative is available these hardcoded values might
% be replaced.
fonts = getCommonFonts();
if ismac
    fonts = [fonts;getMacFonts()];
elseif isunix
    fonts = [fonts;getUnixFonts()];
elseif ispc
    fonts = [fonts;getPcFonts()];
else
    fonts = [fonts;getMacFonts();getUnixFonts();getPcFonts()];
end
end

function commonFonts = getCommonFonts()
% List of fonts that are common on many platforms
commonFonts = {
    'Arial';
    'Times New Roman';
    'Arial Black';
    'Courier New';
    'Verdana';
    'Comic Sans MS';
    'Courier';
    'Trebuchet MS';
    'Georgia';
    'Impact';
    'Arial Narrow';
    'Helvetica';
    'Times';
    'Terminal';
    };
end

function macFonts = getMacFonts()
% List of fonts that are common on Mac
macFonts = {
    'Geneva';
    'Monaco';
    'Skia';
    'Apple Chancery';
    'Hoefler Text';
    'Optima';
    'Futura';
    'Lucida Grande';
    'Helvetica Neue';
    'Herculanum';
    'Papyrus';
    'Baskerville';
    'Gill Sans';
    'Didot';
    'Arial Rounded MT Bold';
    'Brush Script MT';
    'Zapfino';
    'Copperplate';
    'Marker Felt';
    'American Typewriter';
    'Big Caslon';
    'y.n.w.u.a.y';
    'Zapf Humanist 601 Demi BT';
    'Zapf Humanist 601 Ultra BT';
    'SWF!T_v01';
    'UniversS 45 Light';
    'UniversS 47 Condensed Light';
    'UniversS 55 Roman';
    'UniversS Euro 45 Light';
    'SWF!T_v02';
    'A.C.M.E. Secret Agent';
    'Geometric Slabserif 703 Extra Bold BT';
    'Humanist 777 Black BT';
    'Humanist 777 Light BT';
    'Alpha Silouettes 3';
    'Alpha-Silouettes 2';
    'Business/Industrial Regular';
    'Cochin';
    'ESRI Environmental & Icons';
    'ESRI Oil, Gas, & Water';
    'ESRI Transportation & Civic';
    'ESRI Transportation & Municipal';
    'Garamond Narrow Bold:001.022';
    'Garamond Narrow Italic:001.022';
    'Garamond Narrow Plain:001.022';
    'German Blackletters, 15th c.';
    'hooge 05_54';
    'Humanist 521 Extra Bold BT';
    'Humanist 521 Light BT';
    'Humanist 521 Ultra Bold BT';
    'Humanist 777 BT';
    'Chicago';
    'Osaka';
    'Palatino';
    'Charcoal';
    'Andale Mono';
    'New York';
    'Univers 45 Light';
    'Univers 57 Condensed';
    'Helvetica 35 Thin';
    'Helvetica 45 Light';
    'Helvetica 55 Roman';
    'Helvetica 65 Medium';
    'Helvetica 95 Black';
    'Gothic 13';
    'Helvetica 85 Heavy';
    'Gadget';
    'Warren 1';
    'week (sRB)';
    'Zartz!';
    'Trade Gothic Bold Condensed No.20';
    'Trade Gothic Condensed No.18';
    'Aldine 401 BT';
    'hooge 05_53';
    'Orator 10 Pitch BT';
    'Orator 15 Pitch BT';
    'Aldine 721 BT';
    'Aldine 721 Light BT';
    'Geometric Slabserif 703 Bold BT';
    'Hemi Head 426';
    'Misbehavin''';
    'Papyrus LET Plain:1.0';
    'Scriptina - Alternates';
    'Broken 15';
    'Day Roman - Expert';
    'Helvetica 25 Ultra Light';
    'Helvetica 75 Bold Outline';
    'L''Elf Noir du Mal Normal';
    'Techno';
    'Textile';
    'Capitals';
    'Sand'
    };
end
function unixFonts = getUnixFonts()
% List of fonts that are common on Unix
unixFonts = {
    'Utopia';
    'Lucidatypewriter';
    'New Century Schoolbook';
    'Lucida';
    'Lucidabright';
    'Charter';
    'Fixed';
    'Nimbus Mono L';
    'Nimbus Roman No9 L';
    'Nimbus Sans L';
    'Century Schoolbook L';
    'ClearlyU';
    'Helvetica-Narrow-BoldOblique'
    };
end

function pcFonts = getPcFonts()
% List of fonts that are common on Windows
pcFonts = {
    'Tahoma';
    'Century Gothic';
    'MS Sans Serif';
    'Lucida Console';
    'Garamond';
    'Lucida Sans Unicode';
    'Bookman Old Style';
    'Book Antiqua';
    'Microsoft Sans Serif';
    'Palatino Linotype';
    'Monotype Corsiva';
    'Haettenschweiler';
    'Modern';
    'Franklin Gothic Medium';
    'Script';
    'Consolas'
    };
end