Skip to content

Commit

Permalink
Separate font and font size
Browse files Browse the repository at this point in the history
If the font is not available but the OS is supported, OSFont will be empty but OSFontSize will have the value of the system font size. Also, FONTEXIST was made into a separate function.
  • Loading branch information
JorgWoehl committed Dec 20, 2016
1 parent 61d02bc commit fdf5280
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 22 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Graphical user interfaces developed with MATLAB often lack the look and feel of

## Usage

`[OSFont, OSFontSize] = getOSfont(OS, OSVersion)` returns the name and size (in points) of the system UI font of operating system `OS` in version `OSVersion`. If the system UI font is not available to MATLAB, it is replaced by a similar font and a warning is issued. If the OS is not supported, or if the selected font is not available, `OSFont` and `OSFontSize` are returned empty.
`[OSFont, OSFontSize] = getOSfont(OS, OSVersion)` returns the name and size (in points) of the system UI font of operating system `OS` in version `OSVersion`. If the system UI font is not available to MATLAB, it is replaced by a similar font and a warning is issued. If the OS is not supported, `OSFONT` and `OSFONTSIZE` are returned empty. `OSFONT` is also returned empty if the selected font is not available.

`OS` is a character vector containing the name of the operating system in lowercase letters. The following operating systems are supported:

Expand All @@ -32,9 +32,11 @@ catch
OSVersion = [];
end
[OSFont, OSFontSize] = getOSfont(OS, OSVersion);
% if returned empty, fall back on factory settings
if isempty(OSFont)
% default to factory settings
OSFont = get(groot, 'factoryUicontrolFontName');
OSFont = get(groot, 'factoryUicontrolFontName');
end
if isempty(OSFontSize)
OSFontSize = get(groot, 'factoryUicontrolFontSize');
end
```
Expand Down
19 changes: 19 additions & 0 deletions fontexist.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
function bool = fontexist(font)
%FONTEXIST Check existence of font.
% FONTEXIST(FONT) returns TRUE if FONT is an available system font.

% Created 2016-01-05 by Jorg C. Woehl
% 2016-12-16 (JCW): Converted to standalone function, comments added.

% input: empty character array, or nonempty character vector
assert(ischar(font) && (isrow(font) || isempty(font)),...
'fontexist:IncorrectInputType', 'Input must be an empty character array or a nonempty character vector.');
if isempty(font)
% reduce to simplest empty type
font = '';
end

idx = find(strcmpi(listfonts, font), 1);
bool = ~isempty(idx);

end
33 changes: 14 additions & 19 deletions getOSfont.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
% size (in points) of the user interface (UI) font of operating system OS
% in version OSVERSION. If the system UI font is not available to MATLAB,
% it is replaced by a similar font and a warning is issued. If the OS is
% not supported, or if the selected font is not available, OSFONT and
% OSFONTSIZE are returned empty.
% not supported, OSFONT and OSFONTSIZE are returned empty. OSFONT is also
% returned empty if the selected font is not available.
%
% OS is a character vector containing the name of the operating system in
% lowercase letters. The following operating systems are supported:
Expand All @@ -29,9 +29,11 @@
% OSVersion = [];
% end
% [OSFont, OSFontSize] = getOSfont(OS, OSVersion);
% % if returned empty, fall back on factory settings
% if isempty(OSFont)
% % default to factory settings
% OSFont = get(groot, 'factoryUicontrolFontName');
% OSFont = get(groot, 'factoryUicontrolFontName');
% end
% if isempty(OSFontSize)
% OSFontSize = get(groot, 'factoryUicontrolFontSize');
% end
%
Expand All @@ -40,6 +42,7 @@
% Created 2016-01-05 by Jorg C. Woehl
% 2016-12-05 (JCW): Converted to standalone function, comments added.
% 2016-12-08 (JCW): Input arguments added.
% 2016-12-19 (JCW): Font and font size treated separately.

%% Input argument validation

Expand All @@ -48,8 +51,10 @@
'Input 1 must be an empty character array or a nonempty character vector.');
% convert to all lowercase if necessary
OS = lower(OS);

if ~isempty(OS)
if isempty(OS)
% reduce to simplest empty type
OS = '';
else
% input 2: (nonempty) numeric vector containing finite real non-negative "integers"
assert(isnumeric(OSVersion) && isvector(OSVersion) && all(isfinite(OSVersion))...
&& isreal(OSVersion) && all(OSVersion == round(OSVersion)) && all(OSVersion >= 0),...
Expand All @@ -60,6 +65,7 @@
%% Determine system UI font

font = '';
fontSize = [];

% supported operating systems
switch OS
Expand Down Expand Up @@ -147,23 +153,12 @@
end
end

% check if the selected font exists, otherwise return empty variables
OSFontSize = fontSize;
% check if the selected font exists
if fontexist(font)
OSFont = font;
OSFontSize = fontSize;
else
OSFont = '';
OSFontSize = [];
end

end

%% is font available on system?

function bool = fontexist(font)

% find first occurrence of font in list of all fonts on the system
idx = find(strcmpi(listfonts, font), 1);
bool = ~isempty(idx);

end

0 comments on commit fdf5280

Please sign in to comment.