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 test.com.sun.javafx.pgstub;
  27 
  28 import com.sun.javafx.font.FontResource;
  29 import com.sun.javafx.font.FontStrike;
  30 import com.sun.javafx.font.PGFont;
  31 import com.sun.javafx.geom.transform.BaseTransform;
  32 import com.sun.javafx.tk.FontLoader;
  33 import com.sun.javafx.tk.FontMetrics;
  34 import javafx.scene.text.Font;
  35 import javafx.scene.text.FontPosture;
  36 import javafx.scene.text.FontWeight;
  37 
  38 import java.io.InputStream;
  39 import java.util.Arrays;
  40 import java.util.List;
  41 import java.util.Locale;
  42 
  43 public class StubFontLoader extends FontLoader {
  44 
  45     @Override
  46     public void loadFont(Font font) {
  47         StubFont nativeFont = new StubFont();
  48         nativeFont.font = font;
  49         String name = font.getName().trim().toLowerCase(Locale.ROOT);
  50         if (name.equals("system") || name.equals("system regular")) {
  51             font.impl_setNativeFont(nativeFont, font.getName(), "System", "Regular");
  52         } else if (name.equals("amble regular")) {
  53             font.impl_setNativeFont(nativeFont, font.getName(), "Amble", "Regular");
  54         } else if (name.equals("amble bold")) {
  55             font.impl_setNativeFont(nativeFont, font.getName(), "Amble", "Bold");
  56         } else if (name.equals("amble italic")) {
  57             font.impl_setNativeFont(nativeFont, font.getName(), "Amble", "Italic");
  58         } else if (name.equals("amble bold italic")) {
  59             font.impl_setNativeFont(nativeFont, font.getName(), "Amble",
  60                     "Bold Italic");
  61         } else if (name.equals("amble condensed")) {
  62             font.impl_setNativeFont(nativeFont, font.getName(), "Amble Cn", "Regular");
  63         } else if (name.equals("amble bold condensed")) {
  64             font.impl_setNativeFont(nativeFont, font.getName(), "Amble Cn", "Bold");
  65         } else if (name.equals("amble condensed italic")) {
  66             font.impl_setNativeFont(nativeFont, font.getName(), "Amble Cn", "Italic");
  67         } else if (name.equals("amble bold condensed italic")) {
  68             font.impl_setNativeFont(nativeFont, font.getName(), "Amble Cn",
  69                     "Bold Italic");
  70         } else if (name.equals("amble light")) {
  71             font.impl_setNativeFont(nativeFont, font.getName(), "Amble Lt", "Regular");
  72         } else if (name.equals("amble light italic")) {
  73             font.impl_setNativeFont(nativeFont, font.getName(), "Amble Lt", "Italic");
  74         } else if (name.equals("amble light condensed")) {
  75             font.impl_setNativeFont(nativeFont, font.getName(), "Amble LtCn",
  76                     "Regular");
  77         } else if (name.equals("amble light condensed italic")) {
  78             font.impl_setNativeFont(nativeFont, font.getName(), "Amble LtCn",
  79                     "Italic");
  80         }
  81     }
  82 
  83     @Override
  84     public List<String> getFamilies() {
  85         return Arrays.asList("Amble", "Amble Cn", "Amble Lt", "Amble LtCn");
  86     }
  87 
  88     @Override
  89     public List<String> getFontNames() {
  90         return Arrays.asList("Amble Regular", "Amble Bold", "Amble Italic",
  91                 "Amble Bold Italic", "Amble Condensed", "Amble Bold Condensed",
  92                 "Amble Condensed Italic", "Amble Bold Condensed Italic",
  93                 "Amble Light", "Amble Light Italic", "Amble Light Condensed",
  94                 "Amble Light Condensed Italic");
  95     }
  96 
  97     @Override
  98     public List<String> getFontNames(String family) {
  99         String lower = family.trim().toLowerCase(Locale.ROOT);
 100         if ("amble".equals(lower)) {
 101             return Arrays.asList("Amble Regular", "Amble Bold", "Amble Italic",
 102                     "Amble Bold Italic");
 103         } else if ("amble cn".equals(lower)) {
 104             return Arrays.asList("Amble Condensed", "Amble Bold Condensed",
 105                     "Amble Condensed Italic", "Amble Bold Condensed Italic");
 106         } else if ("amble lt".equals(lower)) {
 107             return Arrays.asList("Amble Light", "Amble Light Italic");
 108         } else if ("amble ltcn".equals(lower)) {
 109             return Arrays.asList("Amble Light Condensed",
 110                     "Amble Light Condensed Italic");
 111         } else {
 112             return Arrays.asList();
 113         }
 114     }
 115 
 116     @Override
 117     public Font font(String family, FontWeight weight, FontPosture posture,
 118             float size) {
 119         family = family.trim();
 120         String fam = family.toLowerCase(Locale.ROOT);
 121         String name = "";
 122         if ("amble".equals(fam)) {
 123             if (weight != null
 124                     && weight.ordinal() < FontWeight.NORMAL.ordinal()) {
 125                 name = name + " Light";
 126             } else if (weight != null
 127                     && weight.ordinal() > FontWeight.NORMAL.ordinal()) {
 128                 name = name + " Bold";
 129             } else if (posture != FontPosture.ITALIC) {
 130                 name = name + " Regular";
 131             }
 132         } else if ("amble cn".equals(fam) || "amble condensed".equals(fam)) {
 133             if (weight != null
 134                     && weight.ordinal() < FontWeight.NORMAL.ordinal()) {
 135                 name = name + " Light";
 136             } else if (weight != null
 137                     && weight.ordinal() > FontWeight.NORMAL.ordinal()) {
 138                 name = name + " Bold";
 139             }
 140             name = name + " Condensed";
 141         } else if ("amble lt".equals(fam)) {
 142             if (weight.ordinal() <= FontWeight.NORMAL.ordinal()) {
 143                 name = name + " Light";
 144             } else if (weight != null
 145                     && weight.ordinal() < FontWeight.BOLD.ordinal()
 146                     && posture != FontPosture.ITALIC) {
 147                 name = name + " Regular";
 148             } else if (weight != null
 149                     && weight.ordinal() >= FontWeight.BOLD.ordinal()) {
 150                 name = name + " Bold";
 151             }
 152         } else if ("amble ltcn".equals(fam)) {
 153             if (weight.ordinal() <= FontWeight.NORMAL.ordinal()) {
 154                 name = name + " Light";
 155             } else if (weight != null
 156                     && weight.ordinal() >= FontWeight.BOLD.ordinal()) {
 157                 name = name + " Bold";
 158             }
 159             name = name + " Condensed";
 160         }
 161         if (posture == FontPosture.ITALIC) {
 162             name = name + " Italic";
 163         }
 164         String fn = "Amble" + name;
 165         return new Font(fn, size);
 166     }
 167 
 168     @Override
 169     public Font loadFont(InputStream in, double size) {
 170         return new Font("not implemented", size);
 171     }
 172 
 173     @Override
 174     public Font loadFont(String urlPath, double size) {
 175         return new Font("not implemented", size);
 176     }
 177 
 178     @Override
 179     public FontMetrics getFontMetrics(Font font) {
 180         float size = (float) font.getSize();
 181         return new FontMetrics(size, size, size, size/2, size/2, size/5, font);
 182     }
 183 
 184     @Override
 185     public float computeStringWidth(String s, Font f) {
 186         // Assume that the font glyph size == font point size, mono-spaced
 187         // TODO needs to make sense given getFontMetrics implementation
 188         return (float) (f.getSize() * s.length());
 189     }
 190 
 191     @Override
 192     public float getSystemFontSize() {
 193         return 12;
 194     }
 195 
 196     public static class StubFont implements PGFont {
 197         public Font font;
 198 
 199         @Override public String getFullName() {
 200             return font.getName();
 201         }
 202 
 203         @Override public String getFamilyName() {
 204             return font.getFamily();
 205         }
 206 
 207         @Override public String getStyleName() {
 208             return font.getStyle();
 209         }
 210 
 211         @Override public String getName() {
 212             return font.getName();
 213         }
 214 
 215         @Override public float getSize() {
 216             return (float) font.getSize();
 217         }
 218 
 219         @Override
 220         public FontResource getFontResource() {
 221             return null;
 222         }
 223 
 224         @Override
 225         public FontStrike getStrike(BaseTransform transform) {
 226             return null;
 227         }
 228 
 229         @Override
 230         public FontStrike getStrike(BaseTransform transform, int smoothingType) {
 231             return null;
 232         }
 233 
 234         @Override
 235         public int getFeatures() {
 236             return 0;
 237         }
 238     }
 239 }