Author: rwhitcomb Date: Thu Jan 14 01:12:38 2021 New Revision: 1885461 URL: http://svn.apache.org/viewvc?rev=1885461&view=rev Log: PIVOT-1051: First bunch of code changes to support lists of font names. Not complete yet as we investigate how to implement this.
Modified: pivot/trunk/demos/src/org/apache/pivot/demos/text/TextPaneDemo.java pivot/trunk/wtk-terra/src/org/apache/pivot/wtk/skin/terra/TerraTheme.java pivot/trunk/wtk/src/org/apache/pivot/wtk/FontUtilities.java pivot/trunk/wtk/src/org/apache/pivot/wtk/Theme.java Modified: pivot/trunk/demos/src/org/apache/pivot/demos/text/TextPaneDemo.java URL: http://svn.apache.org/viewvc/pivot/trunk/demos/src/org/apache/pivot/demos/text/TextPaneDemo.java?rev=1885461&r1=1885460&r2=1885461&view=diff ============================================================================== --- pivot/trunk/demos/src/org/apache/pivot/demos/text/TextPaneDemo.java (original) +++ pivot/trunk/demos/src/org/apache/pivot/demos/text/TextPaneDemo.java Thu Jan 14 01:12:38 2021 @@ -106,7 +106,7 @@ public class TextPaneDemo implements App super.render(item, index, listView, selected, state, highlighted, disabled); if (item != null) { String fontFamilyName = (String) item; - label.getStyles().put(Style.font, Font.decode(fontFamilyName + "-12")); + label.getStyles().put(Style.font, FontUtilities.decode(fontFamilyName + "-12")); } } }); @@ -116,7 +116,7 @@ public class TextPaneDemo implements App super.render(data, button, highlight); if (data != null) { String fontFamilyName = (String) data; - label.getStyles().put(Style.font, Font.decode(fontFamilyName + "-12")); + label.getStyles().put(Style.font, FontUtilities.decode(fontFamilyName + "-12")); } } }); @@ -129,7 +129,7 @@ public class TextPaneDemo implements App public void selectedItemChanged(final ListButton listButton, final Object previousSelectedItem) { int selectedFontSize = ((Integer) fontSizeListButton.getSelectedItem()).intValue(); String selectedFontFamily = (String) fontFamilyListButton.getSelectedItem(); - final Font derivedFont = Font.decode(selectedFontFamily + " " + selectedFontSize); + final Font derivedFont = FontUtilities.decode(selectedFontFamily + " " + selectedFontSize); applyStyleToSelection(span -> span.setFont(derivedFont)); requestTextPaneFocus(); @@ -200,7 +200,7 @@ public class TextPaneDemo implements App } span.setFont(font); } else { - span.setFont(FontUtilities.ARIAL, Font.BOLD, 12); + span.setFont(FontUtilities.SANS_SERIF_FONTS, Font.BOLD, 12); } }); requestTextPaneFocus(); @@ -220,7 +220,7 @@ public class TextPaneDemo implements App } span.setFont(font); } else { - span.setFont(FontUtilities.ARIAL, Font.ITALIC, 12); + span.setFont(FontUtilities.SANS_SERIF_FONTS, Font.ITALIC, 12); } }); requestTextPaneFocus(); Modified: pivot/trunk/wtk-terra/src/org/apache/pivot/wtk/skin/terra/TerraTheme.java URL: http://svn.apache.org/viewvc/pivot/trunk/wtk-terra/src/org/apache/pivot/wtk/skin/terra/TerraTheme.java?rev=1885461&r1=1885460&r2=1885461&view=diff ============================================================================== --- pivot/trunk/wtk-terra/src/org/apache/pivot/wtk/skin/terra/TerraTheme.java (original) +++ pivot/trunk/wtk-terra/src/org/apache/pivot/wtk/skin/terra/TerraTheme.java Thu Jan 14 01:12:38 2021 @@ -350,7 +350,7 @@ public final class TerraTheme extends Th @SuppressWarnings("unchecked") Map<String, ?> properties = (Map<String, ?>) serializer.readObject(inputStream); - font = Font.decode((String) properties.get(FONT_PROPERTY)); + font = FontUtilities.decodeFont((String) properties.get(FONT_PROPERTY)); String defaultStylesName = (String) properties.get(DEFAULT_STYLES_PROPERTY); if (defaultStylesName != null && !defaultStylesName.isEmpty()) { Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/FontUtilities.java URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/FontUtilities.java?rev=1885461&r1=1885460&r2=1885461&view=diff ============================================================================== --- pivot/trunk/wtk/src/org/apache/pivot/wtk/FontUtilities.java (original) +++ pivot/trunk/wtk/src/org/apache/pivot/wtk/FontUtilities.java Thu Jan 14 01:12:38 2021 @@ -30,7 +30,13 @@ public final class FontUtilities { /** The standard name for the {@code Arial} font. */ public static final String ARIAL = "Arial"; - /** The obvious factor needed to convert a number to a percentage factor. */ + /** + * A list of "standard" sans-serif fonts, useful when cross-platform + * support is necessary. + */ + public static final String SANS_SERIF_FONTS = "Arial,Verdana,Helvetica,SansSerif"; + + /** The obvious factor needed to convert a number to a percentage value. */ private static final float PERCENT_SCALE = 100.0f; /** @@ -60,13 +66,89 @@ public final class FontUtilities { throw new IllegalArgumentException(exception); } } else { - font = Font.decode(value); + font = decode(value); } return font; } /** + * Decode a font specification. + * <p> This is the same as {@link Font#decode(String)} except that we will allow multiple + * font/family names separated by commas as the <code><i>fontname</i></code> part of the + * spec (much the same as CSS allows). + * <p>The list of allowed formats is: + * <ul><li><i>fontname-style-pointsize</i></li> + * <li><i>fontname-pointsize</i></li> + * <li><i>fontname-style</i></li> + * <li><i>fontname</i></li> + * <li><i>fontname style pointsize</i></li> + * <li><i>fontname pointsize</i></li> + * <li><i>fontname style</i></li> + * <li><i>fontname</i></li> + * </ul> + * where <i>fontname</i> can be <i>fontname</i>[,<i>fontname</i>]*. + * + * @param str The font specification as above. + * @return The font according to the desired specification as much as possible. + * @see Font#decode(String) + */ + public static Font decode(final String str) { + if (Utils.isNullOrEmpty(str)) { + return Font.decode(str); + } + + if (str.indexOf(',') > 0) { + // Search from the end to find the separator (either ' ' or '-') + int len = str.length(); + char sep = ' '; + while (--len >= 0) { + char ch = str.charAt(len); + if (ch == ' ' || ch == '-') { + sep = ch; + break; + } + } + int pos = str.indexOf(sep); + String name = pos < 0 ? str : str.substring(0, pos); + String spec = pos < 0 ? "" : str.substring(pos); + String[] names = name.split(","); + for (String nm : names) { + Font f = Font.decode(nm + spec); +System.out.println("getName = " + f.getName() + ", getFontName = " + f.getFontName() + ", getFamily = " + f.getFamily()); + } + } else { + return Font.decode(str); + } + } + + /** + * Get a new font with the given name, style, and size. + * <p> The {@code name} can be a comma-separated list of names, and the first one matched will be used. + * + * @param name The font name, which can be a list (such as <code>Arial,Verdana,SansSerif</code> with + * no spaces). + * @param style The integer font style (as in {@link Font#PLAIN} or {@link Font#ITALIC}). + * @param size The integer font size (in points). + * @return The newly created font with these attributes. + */ + public static Font getFont(final String name, final int style, final int size) { + if (Utils.isNullOrEmpty(name)) { + return new Font(name, style, size); + } + + if (name.indexOf(',') > 0) { + String[] names = name.split(","); + for (String nm : names) { + Font f = new Font(nm, style, size); +System.out.println("getName = " + f.getName() + ", getFontName = " + f.getFontName() + ", getFamily = " + f.getFamily()); + } + } else { + return new Font(name, style, size); + } + } + + /** * Decode a font size specification, taking into account "nnn%" form, and the existing size. * * @param sizeValue The input size value, could be a number or a numeric string, or a number Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/Theme.java URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/Theme.java?rev=1885461&r1=1885460&r2=1885461&view=diff ============================================================================== --- pivot/trunk/wtk/src/org/apache/pivot/wtk/Theme.java (original) +++ pivot/trunk/wtk/src/org/apache/pivot/wtk/Theme.java Thu Jan 14 01:12:38 2021 @@ -298,7 +298,9 @@ public abstract class Theme { * <li>{@value #BOLD_KEY} - true/false</li> * <li>{@value #ITALIC_KEY} - true/false</li> * </ul> - * Omitted values are taken from the theme's font. + * Omitted values are taken from the theme's font. The name (if given) can be + * a comma-separated list of names, which are searched in the order given to + * find the first available in the list (such as <code>Arial,Verdana,SansSerif</code>. * @return The new font derived from the current font. * @throws IllegalArgumentException if the supplied dictionary is {@code null}. */ @@ -307,17 +309,17 @@ public abstract class Theme { Font font = theme.getFont(); - String name = font.getName(); + String name = (font != null) ? font.getName() : Font.DIALOG; if (dictionary.containsKey(NAME_KEY)) { name = (String) dictionary.get(NAME_KEY); } - int size = font.getSize(); + int size = (font != null) ? font.getSize() : 12; if (dictionary.containsKey(SIZE_KEY)) { size = FontUtilities.decodeFontSize(dictionary.get(SIZE_KEY), size); } - int style = font.getStyle(); + int style = (font != null) ? font.getStyle() : Font.PLAIN; if (dictionary.containsKey(BOLD_KEY)) { boolean bold = dictionary.getBoolean(BOLD_KEY); @@ -338,7 +340,7 @@ public abstract class Theme { } } - return new Font(name, style, size); + return FontUtilities.getFont(name, style, size); } }