       1 /*
       2  * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
       3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       4  *
       5  * This code is free software; you can redistribute it and/or modify it
       6  * under the terms of the GNU General Public License version 2 only, as
       7  * published by the Free Software Foundation.  Oracle designates this
       8  * particular file as subject to the "Classpath" exception as provided
       9  * by Oracle in the LICENSE file that accompanied this code.
      10  *
      11  * This code is distributed in the hope that it will be useful, but WITHOUT
      12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
      13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
      14  * version 2 for more details (a copy is included in the LICENSE file that
      15  * accompanied this code).
      16  *
      17  * You should have received a copy of the GNU General Public License version
      18  * 2 along with this work; if not, write to the Free Software Foundation,
      19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
      20  *
      21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
      22  * or visit www.oracle.com if you need additional information or have any
      23  * questions.
      24  */
      25
      26 package com.sun.javafx.css.converters;
      27
      28 import com.sun.javafx.Utils;
      29 import com.sun.javafx.css.Size;
      30 import com.sun.javafx.css.StyleConverterImpl;
      31 import java.util.Map;
      32 import java.util.Map.Entry;
      33 import javafx.css.CssMetaData;
      34 import javafx.css.ParsedValue;
      35 import javafx.css.StyleConverter;
      36 import javafx.css.Styleable;
      37 import javafx.scene.text.Font;
      38 import javafx.scene.text.FontPosture;
      39 import javafx.scene.text.FontWeight;
      40
      41 public final class FontConverter extends StyleConverterImpl<ParsedValue[], Font> {
      42
      43     // lazy, thread-safe instatiation
      44     private static class Holder {
      45         static FontConverter INSTANCE = new FontConverter();
      46     }
      47
      48     public static StyleConverter<ParsedValue[], Font> getInstance() {
      49         return Holder.INSTANCE;
      50     }
      51
      52     private FontConverter() {
      53         super();
      54     }
      55
      56     @Override
      57     public Font convert(ParsedValue<ParsedValue[], Font> value, Font font) {
      58         ParsedValue[] values = value.getValue();
      59         Font aFont = (font != null) ? font : Font.getDefault();
      60         String family = (values[0] != null) ? Utils.stripQuotes((String) values[0].convert(aFont)) : aFont.getFamily();
      61         // if font size is given in terms of percent, then we have to call
      62         // pixels directly in order to pass the multiplier.
      63         double fsize = aFont.getSize();
      64         if (values[1] != null) {
      65             ParsedValue<?, ?> pv = (ParsedValue<?, ?>) values[1].getValue();
      66             Size size = (Size) pv.convert(aFont);
      67             fsize = size.pixels(aFont.getSize(), aFont);
      68         }
      69         FontWeight weight = (values[2] != null) ? (FontWeight) values[2].convert(aFont) : FontWeight.NORMAL;
      70         FontPosture style = (values[3] != null) ? (FontPosture) values[3].convert(aFont) : FontPosture.REGULAR;
      71         Font f = Font.font(family, weight, style, fsize);
      72         return f;
      73     }
      74
      75     @Override
      76     public Font convert(Map<CssMetaData<? extends Styleable, ?>, Object> convertedValues) {
      77         Font font = Font.getDefault();
      78         double size = font.getSize();
      79         String family = font.getFamily();
      80         FontWeight weight = FontWeight.NORMAL;
      81         FontPosture style = FontPosture.REGULAR;
      82
      83         for (Entry<CssMetaData<? extends Styleable, ?>, Object> entry : convertedValues.entrySet()) {
      84
      85             Object value = entry.getValue();
      86             if (value == null) {
      87                 continue;
      88             }
      89             final String prop = entry.getKey().getProperty();
      90             if (prop.endsWith("font-size")) {
      91                 size = ((Number) value).doubleValue();
      92             } else if (prop.endsWith("font-family")) {
      93                 family = Utils.stripQuotes((String) value);
      94             } else if (prop.endsWith("font-weight")) {
      95                 weight = (FontWeight) value;
      96             } else if (prop.endsWith("font-style")) {
      97                 style = (FontPosture) value;
      98             }
      99         }
     100         final Font f = Font.font(family, weight, style, size);
     101         return f;
     102     }
     103
     104     @Override
     105     public String toString() {
     106         return "FontConverter";
     107     }
     108
     109     public static final class FontStyleConverter extends StyleConverterImpl<String, FontPosture> {
     110
     111         // lazy, thread-safe instatiation
     112         private static class Holder {
     113             static FontStyleConverter INSTANCE = new FontStyleConverter();
     114         }
     115
     116         public static FontStyleConverter getInstance() {
     117             return Holder.INSTANCE;
     118         }
     119
     120         private FontStyleConverter() {
     121             super();
     122         }
     123
     124         @Override
     125         public FontPosture convert(ParsedValue<String, FontPosture> value, Font font) {
     126
     127             // Testing for RT-31022 exposed a ClassCastException where value
     128             // wraps a String (e.g., "ITALIC", not a FontUnits.Style).
     129             final Object val = value.getValue();
     130
     131             FontPosture style = null;
     132
     133             if (val instanceof String) {
     134                 try {
     135                     String sval = ((String)val).toUpperCase();
     136                     style = Enum.valueOf(FontPosture.class, sval);
     137                 } catch (IllegalArgumentException iae) {
     138                     style =  FontPosture.REGULAR;
     139                 } catch (NullPointerException npe) {
     140                     style =  FontPosture.REGULAR;
     141                 }
     142
     143             } else if (val instanceof FontPosture) {
     144                 style = (FontPosture)val;
     145             }
     146             return style;
     147         }
     148
     149         @Override
     150         public String toString() {
     151             return "FontConverter.StyleConverter";
     152         }
     153     }
     154
     155     public static final class FontWeightConverter extends StyleConverterImpl<String, FontWeight> {
     156
     157         // lazy, thread-safe instatiation
     158         private static class Holder {
     159             static FontWeightConverter INSTANCE = new FontWeightConverter();
     160         }
     161
     162         public static FontWeightConverter getInstance() {
     163             return Holder.INSTANCE;
     164         }
     165
     166         private FontWeightConverter() {
     167             super();
     168         }
     169
     170         @Override
     171         public FontWeight convert(ParsedValue<String, FontWeight> value, Font font) {
     172
     173             // Testing for RT-31022 exposed a ClassCastException where value
     174             // wraps a String (e.g., "ITALIC", not a FontUnits.Style).
     175             final Object val = value.getValue();
     176
     177             FontWeight weight = null;
     178
     179             if (val instanceof String) {
     180                 try {
     181                     String sval = ((String)val).toUpperCase();
     182                     weight = Enum.valueOf(FontWeight.class, sval);
     183                 } catch (IllegalArgumentException iae) {
     184                     weight =  FontWeight.NORMAL;
     185                 } catch (NullPointerException npe) {
     186                     weight =  FontWeight.NORMAL;
     187                 }
     188
     189             } else if (val instanceof FontWeight) {
     190                 weight = (FontWeight)val;
     191             }
     192
     193             return weight;
     194         }
     195
     196         @Override
     197         public String toString() {
     198             return "FontConverter.WeightConverter";
     199         }
     200     }
     201
     202     public static final class FontSizeConverter extends StyleConverterImpl<ParsedValue<?, Size>, Number> {
     203
     204         // lazy, thread-safe instatiation
     205         private static class Holder {
     206             static FontSizeConverter INSTANCE = new FontSizeConverter();
     207         }
     208
     209         public static FontSizeConverter getInstance() {
     210             return Holder.INSTANCE;
     211         }
     212
     213         private FontSizeConverter() {
     214             super();
     215         }
     216
     217         @Override
     218         public Number convert(ParsedValue<ParsedValue<?, Size>, Number> value, Font font) {
     219             final ParsedValue<?, Size> size = value.getValue();
     220             return size.convert(font).pixels(font.getSize(), font);
     221         }
     222
     223         @Override
     224         public String toString() {
     225             return "FontConverter.FontSizeConverter";
     226         }
     227     }
     228
     229 }