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