use of java.awt.font.TextAttribute in project Zong by Xenoage.
the class TextLayoutTools method createAttributesMap.
/**
* Creates a Map with the attributes representing
* the given style for an AttibutedString.
*/
private static Map<TextAttribute, Object> createAttributesMap(FormattedTextStyle style) {
Map<TextAttribute, Object> ret = new HashMap<>();
FontInfo fontInfo = notNull(style.getFont(), FontInfo.defaultValue);
// font name
Font font = toAwtFont(fontInfo);
ret.put(TextAttribute.FAMILY, font.getFamily());
// font size
ret.put(TextAttribute.SIZE, font.getSize2D());
// color
ret.put(TextAttribute.FOREGROUND, toAwtColor(style.getColor()));
// bold
FontStyle fontStyle = fontInfo.getStyle();
ret.put(TextAttribute.WEIGHT, (fontStyle.isSet(FontStyle.Bold) ? TextAttribute.WEIGHT_BOLD : TextAttribute.WEIGHT_REGULAR));
// italic
ret.put(TextAttribute.POSTURE, (fontStyle.isSet(FontStyle.Italic) ? TextAttribute.POSTURE_OBLIQUE : TextAttribute.POSTURE_REGULAR));
// underline
ret.put(TextAttribute.UNDERLINE, (fontStyle.isSet(FontStyle.Underline) ? TextAttribute.UNDERLINE_ON : null));
// superscript
switch(style.getSuperscript()) {
case Super:
ret.put(TextAttribute.SUPERSCRIPT, TextAttribute.SUPERSCRIPT_SUPER);
break;
case Sub:
ret.put(TextAttribute.SUPERSCRIPT, TextAttribute.SUPERSCRIPT_SUB);
break;
default:
ret.put(TextAttribute.SUPERSCRIPT, null);
}
return ret;
}
use of java.awt.font.TextAttribute in project jsql-injection by ron190.
the class AbstractRadioLink method setUnderlined.
/**
* Change font of radio label to underline.
*/
public final void setUnderlined() {
Font font = this.getFont();
Map<TextAttribute, Object> attributes = new HashMap<>(font.getAttributes());
attributes.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_LOW_DOTTED);
attributes.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD);
this.setFont(font.deriveFont(attributes));
}
use of java.awt.font.TextAttribute in project cuba by cuba-platform.
the class SwingXTableSettings method loadFontPreferences.
protected void loadFontPreferences(Element element) {
// load font preferences
String fontFamily = element.attributeValue("fontFamily");
String fontSize = element.attributeValue("fontSize");
String fontStyle = element.attributeValue("fontStyle");
String fontUnderline = element.attributeValue("fontUnderline");
if (!StringUtils.isBlank(fontFamily) && !StringUtils.isBlank(fontSize) && !StringUtils.isBlank(fontUnderline) && !StringUtils.isBlank(fontStyle)) {
try {
int size = Integer.parseInt(fontSize);
int style = Integer.parseInt(fontStyle);
String[] availableFonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
int fontIndex = Arrays.asList(availableFonts).indexOf(fontFamily);
if (fontIndex < 0) {
log.debug("Unsupported font family, font settings not loaded");
return;
}
Configuration configuration = AppBeans.get(Configuration.NAME);
DesktopConfig desktopConfig = configuration.getConfig(DesktopConfig.class);
int sizeIndex = desktopConfig.getAvailableFontSizes().indexOf(size);
if (sizeIndex < 0) {
log.debug("Unsupported font size, font settings not loaded");
return;
}
Boolean underline = BooleanUtils.toBooleanObject(fontUnderline);
@SuppressWarnings("MagicConstant") Font font = new Font(fontFamily, style, size);
if (underline != null && Boolean.TRUE.equals(underline)) {
Map<TextAttribute, Integer> attributes = new HashMap<>();
attributes.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
font = font.deriveFont(attributes);
}
table.setFont(font);
} catch (NumberFormatException ex) {
log.debug("Broken font definition in user setting");
}
}
}
use of java.awt.font.TextAttribute in project cuba by cuba-platform.
the class FontDialog method initUI.
private void initUI() {
Configuration configuration = AppBeans.get(Configuration.NAME);
DesktopConfig desktopConfig = configuration.getConfig(DesktopConfig.class);
setIconImage(null);
setIconImages(null);
setPreferredSize(new Dimension(400, 220));
setSize(new Dimension(400, 220));
setMinimumSize(new Dimension(380, 200));
JPanel contentPane = new JPanel();
contentPane.setLayout(new BorderLayout(0, 5));
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
// font properties panel
JPanel fontPrefsPanel = new JPanel();
fontPrefsPanel.setLayout(new BoxLayout(fontPrefsPanel, BoxLayout.X_AXIS));
fontFamilyBox = new JComboBox();
fontFamilyBox.setPreferredSize(new Dimension(160, -1));
String[] availableFonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
fontFamilyBox.setModel(new DefaultComboBoxModel<>(availableFonts));
fontSizeBox = new JComboBox();
fontSizeBox.setPreferredSize(new Dimension(60, -1));
fontSizeBox.setMaximumSize(new Dimension(60, Integer.MAX_VALUE));
fontSizeBox.setMinimumSize(new Dimension(60, 0));
fontSizeBox.setModel(new ListComboBoxModel<>(desktopConfig.getAvailableFontSizes()));
DesktopResources resources = App.getInstance().getResources();
boldToggle = new JToggleButton(resources.getIcon("font/bold.png"));
italicToggle = new JToggleButton(resources.getIcon("font/italic.png"));
underlineToggle = new JToggleButton(resources.getIcon("font/underline.png"));
fontPrefsPanel.add(fontFamilyBox);
fontPrefsPanel.add(fontSizeBox);
fontPrefsPanel.add(boldToggle);
fontPrefsPanel.add(italicToggle);
fontPrefsPanel.add(underlineToggle);
if (editFont != null) {
fontFamilyBox.setSelectedItem(editFont.getFamily());
fontSizeBox.setSelectedItem(editFont.getSize());
// toggle buttons
Map<TextAttribute, ?> attributes = editFont.getAttributes();
boldToggle.setSelected((editFont.getStyle() & Font.BOLD) == Font.BOLD);
italicToggle.setSelected((editFont.getStyle() & Font.ITALIC) == Font.ITALIC);
underlineToggle.setSelected(attributes.get(TextAttribute.UNDERLINE) == TextAttribute.UNDERLINE_ON);
} else {
fontFamilyBox.setSelectedIndex(0);
fontSizeBox.setSelectedIndex(0);
}
initListeners();
contentPane.add(fontPrefsPanel, BorderLayout.NORTH);
// preview panel
JPanel previewPanel = new JPanel();
previewPanel.setLayout(new GridBagLayout());
previewPanel.setPreferredSize(new Dimension(-1, 120));
previewPanel.setMinimumSize(new Dimension(0, 120));
previewPanel.setSize(-1, 120);
previewLabel = new JLabel("ABCDEFG abcdefg");
previewPanel.add(previewLabel);
previewLabel.setMaximumSize(new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE));
if (editFont != null)
previewLabel.setFont(editFont);
CollapsiblePanel groupBox = new CollapsiblePanel(previewPanel);
groupBox.setCollapsible(false);
groupBox.setCaption(messages.getMessage(getClass(), "FontDialog.preview"));
contentPane.add(groupBox, BorderLayout.CENTER);
// buttons panel
JPanel buttonsPanel = new JPanel();
buttonsPanel.setLayout(new BoxLayout(buttonsPanel, BoxLayout.X_AXIS));
JButton okBtn = new JButton(new AbstractAction(messages.getMessage(getClass(), "actions.Ok"), resources.getIcon("icons/ok.png")) {
@Override
public void actionPerformed(ActionEvent e) {
result = compileFont();
closeDialog();
}
});
okBtn.setPreferredSize(new Dimension(0, DesktopComponentsHelper.BUTTON_HEIGHT));
JButton cancelBtn = new JButton(new AbstractAction(messages.getMessage(getClass(), "actions.Cancel"), resources.getIcon("icons/cancel.png")) {
@Override
public void actionPerformed(ActionEvent e) {
closeDialog();
}
});
cancelBtn.setPreferredSize(new Dimension(0, DesktopComponentsHelper.BUTTON_HEIGHT));
buttonsPanel.add(okBtn);
buttonsPanel.add(cancelBtn);
contentPane.add(buttonsPanel, BorderLayout.SOUTH);
initToolTips();
setContentPane(contentPane);
pack();
applyLocation();
}
use of java.awt.font.TextAttribute in project mudmap2 by Neop.
the class MapPainterDefault method drawText.
/**
* Draw place tile text
*
* @param g graphics to draw on
* @param x x coordinate of text area (within tile borders)
* @param y y coordinate of text area (within tile borders)
* @param width text area width
* @param height text area height
* @param top text for the top part
* @param flags flags line
* @param exits exits line
*/
private void drawText(Graphics g, int x, int y, int width, int height, List<String> top, String flags, String exits) {
FontMetrics fm = g.getFontMetrics();
final int lineHeight = fm.getMaxAscent();
// maximum number of lines
final int maxLines = (int) Math.floor((height - fm.getDescent()) / lineHeight);
// max number of lines for the top part
final int topLines = maxLines - ((!flags.isEmpty() || !exits.isEmpty()) && maxLines > 1 ? 1 : 0);
// reformat lines
LinkedList<String> linesTop = new LinkedList<>();
for (String topText : top) {
LinkedList<String> fitLineLength = fitLineLength(topText, fm, width, topLines);
linesTop.addAll(fitLineLength);
if (linesTop.size() >= topLines)
break;
}
// draw top lines
for (int i = 0; i < topLines && i < linesTop.size(); ++i) {
g.drawString(linesTop.get(i), x, y + (i + 1) * lineHeight);
}
if (maxLines > 1) {
if (fm.stringWidth(flags + exits) < width) {
// draw flags
g.drawString(flags, x, y + height - fm.getDescent());
}
// change font for exits
Font orig = g.getFont();
// derive font: increase font size and decrease character spacing
Map<TextAttribute, Object> attributes = new HashMap<>();
attributes.put(TextAttribute.SIZE, 17);
attributes.put(TextAttribute.TRACKING, 0.0);
g.setFont(orig.deriveFont(attributes));
FontMetrics fm2 = g.getFontMetrics();
int exitsWidth = fm2.stringWidth(exits);
// draw exit string
g.drawString(exits, x + width - exitsWidth, y + height - fm2.getDescent());
g.setFont(orig);
}
}
Aggregations