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.scene.layout.region;
  27 
  28 import com.sun.javafx.css.Size;
  29 import com.sun.javafx.css.SizeUnits;
  30 import com.sun.javafx.css.StyleConverterImpl;
  31 import javafx.css.ParsedValue;
  32 import javafx.scene.layout.CornerRadii;
  33 import javafx.scene.text.Font;
  34 
  35 /**
  36  * Convert parsed value of <size>{1,4} [ '/' <size>{1,4}]? [',' <size>{1,4} [ '/' <size>{1,4}]?]? to CornerRadii
  37  */
  38 public final class CornerRadiiConverter extends StyleConverterImpl<ParsedValue<ParsedValue<?,Size>[][],CornerRadii>[], CornerRadii[]> {
  39 
  40     private static final CornerRadiiConverter INSTANCE =
  41             new CornerRadiiConverter();
  42 
  43     public static CornerRadiiConverter getInstance() {
  44         return INSTANCE;
  45     }
  46 
  47     // Disallow instantiation
  48     private CornerRadiiConverter() { }
  49 
  50 
  51     @Override
  52     public CornerRadii[] convert(ParsedValue<ParsedValue<ParsedValue<?,Size>[][], CornerRadii>[], CornerRadii[]> value, Font font) {
  53 
  54         ParsedValue<ParsedValue<?,Size>[][], CornerRadii>[] values = value.getValue();
  55         CornerRadii[] cornerRadiiValues = new CornerRadii[values.length];
  56 
  57         // parser gives us 2x4 array normalized to the rules in http://www.w3.org/TR/css3-background/#the-border-radius
  58         for(int n=0; n<values.length; n++) {
  59 
  60             ParsedValue<?,Size>[][] sizes = values[n].getValue();
  61 
  62             Size topLeftHorizontalRadius = sizes[0][0].convert(font);
  63             Size topRightHorizontalRadius = sizes[0][1].convert(font);
  64             Size bottomRightHorizontalRadius = sizes[0][2].convert(font);
  65             Size bottomLeftHorizontalRadius = sizes[0][3].convert(font);
  66             Size topLeftVerticalRadius = sizes[1][0].convert(font);
  67             Size topRightVerticalRadius = sizes[1][1].convert(font);
  68             Size bottomRightVerticalRadius = sizes[1][2].convert(font);
  69             Size bottomLeftVerticalRadius = sizes[1][3].convert(font);
  70 
  71             cornerRadiiValues[n] = new CornerRadii(
  72                     topLeftHorizontalRadius.pixels(),     topLeftVerticalRadius.pixels(),
  73                     topRightVerticalRadius.pixels(),      topRightHorizontalRadius.pixels(),
  74                     bottomRightHorizontalRadius.pixels(), bottomRightVerticalRadius.pixels(),
  75                     bottomLeftVerticalRadius.pixels(),    bottomLeftHorizontalRadius.pixels(),
  76                     topLeftHorizontalRadius.getUnits()     == SizeUnits.PERCENT, topLeftVerticalRadius.getUnits()      == SizeUnits.PERCENT,
  77                     topRightVerticalRadius.getUnits()      == SizeUnits.PERCENT, topRightHorizontalRadius.getUnits()   == SizeUnits.PERCENT,
  78                     bottomRightHorizontalRadius.getUnits() == SizeUnits.PERCENT, bottomRightVerticalRadius.getUnits()  == SizeUnits.PERCENT,
  79                     bottomRightVerticalRadius.getUnits()   == SizeUnits.PERCENT, bottomLeftHorizontalRadius.getUnits() == SizeUnits.PERCENT
  80             );
  81 
  82         }
  83 
  84         return cornerRadiiValues;
  85 
  86     }
  87 }