-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetFontSizeInPixels.m
41 lines (33 loc) · 1.14 KB
/
getFontSizeInPixels.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
function [textWidth, textHeight] = getFontSizeInPixels(fontSize, s)
% getFontSizeInPixels - get font size in pixels based on fontSize input (points or normalized)
% e.g., 13 points = 0.04 normalized => 6.4 px width
s.FontSize = fontSize;
if nargin < 2
if s.FontSize <= 1
s.FontUnits = 'normalized';
else
s.FontUnits = 'points';
end
s.FontAngle = get(0,'defaultuicontrolFontAngle');
s.FontName = get(0,'defaultuicontrolFontName');
s.FontWeight = get(0,'defaultuicontrolFontWeight');
end
if ~isfield(s, 'Position')
s.Position = [0 0 560 420]; % use ml2017b default figure size
end
hFig = figure('Visible','off', 'Position',s.Position);
hAx = axes(hFig);
% Get text size in data units
hTest = text(0,0,'2','Units','pixels', 'FontUnits',s.FontUnits,...
'FontAngle',s.FontAngle, 'FontName',s.FontName, 'FontSize',s.FontSize,...
'FontWeight',s.FontWeight,'Parent',hAx);
textExt = get(hTest,'Extent');
delete(hFig)
textHeight = textExt(4);
textWidth = textExt(3);
% If using a proportional font, shrink text width by a fudge factor to
% account for kerning.
if ~strcmpi(s.FontName,'FixedWidth')
textWidth = textWidth*0.8;
end
end