|
|
Re: Text control not wide enough in italic font [message #1857331 is a reply to message #1856817] |
Tue, 31 January 2023 11:38 |
Julien Guigné Messages: 9 Registered: April 2022 |
Junior Member |
|
|
If it can help.
Fonts are initialized in a singleton class :
public final class UIUtil
{
private Font fontNormal, fontBold, fontBoldAndItalic, fontItalic;
private Hashtable<String, Font> fontMap = new Hashtable<String, Font>();
private static UIUtil _instance;
private UIUtil()
{
}
public static UIUtil getInstance()
{
synchronized(UIUtil.class)
{
if (_instance == null)
{
_instance = new UIUtil();
_instance.init();
}
}
if (_instance.fontNormal == null)
{
_instance.init();
}
return _instance;
}
private void init()
{
if (Display.getCurrent() == null)
return;
Font systemFont = Display.getCurrent().getSystemFont();
fontNormal = systemFont;
FontData[] fd = systemFont.getFontData();
FontData fdBold = new FontData(fd[0].getName(), fd[0].getHeight(), SWT.BOLD);
fontBold = UIUtil.getFont(fdBold);
FontData fdBoldAndItalic = new FontData(fd[0].getName(), fd[0].getHeight(), SWT.BOLD | SWT.ITALIC);
fontBoldAndItalic = UIUtil.getFont(fdBoldAndItalic);
FontData fdItalic = new FontData(fd[0].getName(), fd[0].getHeight(), SWT.ITALIC);
fontItalic = UIUtil.getFont(fdItalic);
}
public static Font getFont(final FontData fd)
{
return getFont(fd, true);
}
/*
* Mandatory: called in UI thread
*/
public static Font getFont(final FontData fontData, final boolean useCache)
{
if (fontData == null)
return null;
FontData fd = cloneFontData(fontData);
Font font = null;
String sFont = getFontString(fd);
if (useCache)
font = UIUtil.getFontFromMap(sFont);
if (font != null && !font.isDisposed())
return font;
else
font = new Font(Display.getCurrent(), fd);
if (useCache)
getInstance().fontMap.put(sFont, font);
return font;
}
public static FontData cloneFontData(final FontData fontData)
{
String name = fontData.getName();
int height = fontData.getHeight();
int style = fontData.getStyle();
return new FontData(name, height, style);
}
private static String getFontString(FontData fd)
{
String sFont = fd.getName() + " " + fd.getHeight() + " " + fd.getStyle();
return sFont;
}
private static Font getFontFromMap(final String sFont)
{
return getInstance().fontMap.get(sFont);
}
// Methods called to set a font to a widget (for example a Label)
public static void setFontToItalic(final Label item)
{
if (item == null || item.isDisposed())
return;
setFont(item, getInstance().fontItalic);
}
public static void setFont(final Label item, final Font font)
{
if (item == null || item.isDisposed() || font == null || font.isDisposed())
return;
item.setFont(font);
}
}
|
|
|
|
Powered by
FUDForum. Page generated in 0.01643 seconds