- Undocumented Matlab - https://undocumentedmatlab.com -

Font selection components

Posted By Yair Altman On October 7, 2015 | 3 Comments

I’ve written here in the past about how Matlab includes multiple alternatives for color selection [1], plot-type selection [2] and date selection [3] components, that can easily be integrated in Matlab figures (GUI). Today, I will show that Matlab also contains various built-in components for font selection.
These components are used by Matlab itself, integrated within the Preferences panel, print setup popup, property inspector window and so on. In most cases the components have remained unchanged for multiple releases, some existing in Matlab releases for the past decade or more. However, since internal components can change without prior notice, there is no assurance that any particular component will continue to be available in future Matlab releases.
Readers who are interested in additional details about the components mentioned in today’s post are referred to sections 3.3.3 and 5.5.2 of my book, Undocumented Secrets of MATLAB-Java Programming [4].

uisetfont

The only documented font-selection alternative in Matlab is uisetfont, which presents a popup dialog window that returns the selected font properties in a simple Matlab struct:

>> font = uisetfont
font =
      FontName: 'Arial'
    FontWeight: 'normal'
     FontAngle: 'normal'
     FontUnits: 'points'
      FontSize: 10

Matlab's uisetfont dialog
Matlab's uisetfont dialog


The main drawback of uisetfont is the fact that it displays a separate non-resizable modal dialog window. We cannot embed uisetfont within our own panel, integrated in our GUI figure.

DesktopFontPicker

DesktopFontPicker is a Swing component that presents a font selection panel that can easily be inserted into any Matlab GUI container (figure, panel or tab) using the javacomponent function [5]:

font = java.awt.Font('Tahoma',java.awt.Font.PLAIN, 11);
jFontPanel = com.mathworks.widgets.DesktopFontPicker(true, font);
[jhPanel,hContainer] = javacomponent(jFontPanel, [10,10,250,170], gcf);

DesktopFontPicker panel
DesktopFontPicker panel

Instead of the “Use desktop font” label, we can use our own label:

jFontPanel.setUseDesktopFontLabel('Use Yair''s standard font...')

Non-standard DesktopFontPicker panel
Non-standard DesktopFontPicker panel

To extract the selected font, use one of the following methods provided by DesktopFontPicker:

jFont = jFontPanel.getSelectedFont();  % returns a java.awt.Font object
flag = jFontPanel.getUseDesktopFont();  % true if top radio-button is selected; false if custom font is selected

FontPrefsPanel

The builtin com.mathworks.mlwidgets.prefs.FontPrefsPanel class is used in Matlab to display the font preferences panel in the Preferences window. We can integrate it directly in our GUI:

jFontPanel=com.mathworks.mlwidgets.prefs.FontPrefsPanel(java.awt.Dimension);
[jhPanel,hContainer] = javacomponent(jFontPanel, [1,1,500,470], gcf);

Using this class is admittedly more cumbersome than DesktopFontPicker and I would not recommend using it in practice.

FontPicker

Font selection can also be shown with drop-downs (combo-boxes), rather than with lists as in DesktopFontPicker, FontPrefsPanel, or uisetfont. Use of drop-downs significantly reduces the display “real-estate” required by the control. This is useful in forms where the font selection is only one of several user-configurable options, and where enough space must be reserved for other configuration controls. We can do this using the com.mathworks.widgets.fonts.FontPicker class.
Up until Matlab release R2010a, FontPicker‘s constructor accepted optional parameters of a pre-selected font (a java.awt.Font object), an optional boolean flag indicating whether to display sample text using the selected font, an optional layout indicator, and optional list of selectable font names. Several screenshots of different parameter combinations are shown below:

import com.mathworks.widgets.fonts.FontPicker
jFontPicker = FontPicker(font, sampleFlag, layout);
[hjFontPicker, hContainer] = javacomponent(jFontPicker, position, gcf);
FontPicker FontPicker FontPicker
font= [] java.awt.Font('Tahoma', java.awt.Font.PLAIN, 8 ) []
sampleFlag= false false true
layout= FontPicker.GRID_LAYOUT (=1) FontPicker.LONG_LAYOUT (=2) FontPicker.LONG_LAYOUT (=2)
position= [10,200,140,40] [10,200,225,20] [10,200,225,80]

As before, the selected font can be retrieved using jFontPicker.getSelectedFont().
In Matlab release R2010b, FontPicker‘s interface changed, and the above code no longer works. This highlights a common pitfall in future-compatibility of internal components: even when the components remain, their interface sometimes changes. Here is the new code format, starting with R2010b:

jLayout = javaMethod('valueOf', 'com.mathworks.widgets.fonts.FontPicker$Layout', 'WIDE_WITH_SAMPLE');  % options: COMPACT, WIDE, WIDE_WITH_SAMPLE
jFont = java.awt.Font('Tahoma', java.awt.Font.PLAIN, 10);  % initial font to display (may not be [])
jFontPicker = com.mathworks.widgets.fonts.FontPicker(jFont, jLayout);
jFontPanel = jFontPicker.getComponent;
[jhPanel,hContainer] = javacomponent(jFontPanel, [10,10,250,120], gcf);

FontChooserPanel

As a final alternative for font selection, we can use the JIDE font-selection component. This component has two variants: as a drop-down/combo-box (com.jidesoft.combobox.FontComboBox) and as a standard JPanel (com.jidesoft.combobox.FontChooserPanel):

jFont = java.awt.Font('arial black',java.awt.Font.PLAIN, 8);
jFontPicker = com.jidesoft.combobox.FontComboBox(jFont);
[hjFontPicker, hContainer] = javacomponent(jFontPicker, position, gcf);
set(hjFontPicker, 'ItemStateChangedCallback', @myCallbackFunction);

JIDE's FontComboBox
JIDE's FontComboBox

Within the callback function, use getSelectedFont() to retrieve the updated font (again, a java.awt.Font object). There is also a corresponding setSelectedFont(font) to programmatically update the control with the specified Font object.
The combo-box presents a FontChooserPanel, which can be accessed (via the PopupPanel property or the corresponding getPopupPanel() method) after it has been initially created. Thereafter, the panel can be customized. For example, the preview text can be modified via the panel’s PreviewText property (or the setPreviewText(text) method).
The same FontChooserPanel can also be displayed as a stand-alone font-selection panel, unrelated to any combo-box. Different GUI requirements might prefer using a compact combo-box approach, or the larger stand-alone panel.
This combo-box/panel duality is a common feature of JIDE controls. I have previously shown it in my color selection components [1] and date selection components [3] articles.

popupmenu uicontrol

As another example of using a font-selection drop-down (combo-box), we can use a standard Matlab popupmenu uicontrol, setting its String property value to a cell-array containing the supported system’s fonts (as returned by the listfonts function). A nice twist here is to use the undocumented trick that all Matlab uicontrols inherently support HTML [6] to list each of the fonts in their respective font style:

fontStr = @(font) ['' font ''];
htmlStr = cellfun(fontStr, listfonts, 'uniform',false);
uicontrol('style','popupmenu', 'string',htmlStr, 'pos',[20,350,100,20]);

HTML-rendered fonts popup menu
HTML-rendered fonts popup menu

Note that we could also use a listbox uicontrol using the same code.

Austria visit, 11-15 October, 2015

I will be travelling to clients in Austria next week, between October 11-15. If you are in Austria and wish to meet me to discuss how I could bring value to your work, then please email me (altmany at gmail).

Categories: GUI, Java, Medium risk of breaking in future versions, Undocumented feature


3 Comments (Open | Close)

3 Comments To "Font selection components"

#1 Comment By Mikhail On October 8, 2015 @ 01:02

Hey Yair,

There is a listfonts command in MATLAB. I guess you can use its output to populate font selection dialog in your application.

#2 Comment By Yair Altman On October 8, 2015 @ 01:08

@Mikhail – read my section on [13] – this is exactly what I did there.

#3 Comment By Volkmar On October 15, 2015 @ 03:07

Although uisetfont is a stock MATLAB UI for font selection, its output is not directly usable when creating new graphics/ui objects: Graphics objects do not accept the struct itself as a font specification (at least not in HG2). One needs to convert this struct into a list of key/value pairs to actually use it as font specifications. Something like this

 
fs = uisetfont;
keys    = fieldnames(fs);
values  = struct2cell(fs);
keyvals = [keys'; values'];
keyvals{:}

creates a key/value cell array, which can be inserted using colon expansion when font properties need to be specified.


Article printed from Undocumented Matlab: https://undocumentedmatlab.com

URL to article: https://undocumentedmatlab.com/articles/font-selection-components

URLs in this post:

[1] color selection: http://undocumentedmatlab.com/blog/color-selection-components

[2] plot-type selection: http://undocumentedmatlab.com/blog/plot-type-selection-components

[3] date selection: http://undocumentedmatlab.com/blog/date-selection-components

[4] Undocumented Secrets of MATLAB-Java Programming: http://undocumentedmatlab.com/books/matlab-java

[5] the javacomponent function: http://undocumentedmatlab.com/blog/javacomponent

[6] Matlab uicontrols inherently support HTML: http://undocumentedmatlab.com/blog/html-support-in-matlab-uicomponents

[7] Date selection components : https://undocumentedmatlab.com/articles/date-selection-components

[8] Color selection components : https://undocumentedmatlab.com/articles/color-selection-components

[9] Plot-type selection components : https://undocumentedmatlab.com/articles/plot-type-selection-components

[10] Matlab toolstrip – part 7 (selection controls) : https://undocumentedmatlab.com/articles/matlab-toolstrip-part-7-selection-controls

[11] Listbox selection hacks : https://undocumentedmatlab.com/articles/listbox-selection-hacks

[12] Figure toolbar components : https://undocumentedmatlab.com/articles/figure-toolbar-components

[13] : https://undocumentedmatlab.com/blog/font-selection-components#uicontrol

Copyright © Yair Altman - Undocumented Matlab. All rights reserved.