1 /*
   2  * Copyright (c) 2011, 2014, 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.font;
  27 
  28 import java.security.AccessController;
  29 import java.security.PrivilegedAction;
  30 import java.util.ArrayList;
  31 import java.util.HashMap;
  32 import java.util.Locale;
  33 
  34 import com.sun.glass.utils.NativeLibLoader;
  35 
  36 class MacFontFinder {
  37 
  38     static {
  39         AccessController.doPrivileged(
  40                 (PrivilegedAction<Void>) () -> {
  41                     NativeLibLoader.loadLibrary("javafx_font");
  42                     return null;
  43                 }
  44         );
  45     }
  46 
  47     private static final int SystemFontType = 2; /*kCTFontSystemFontType*/
  48     private static final int MonospacedFontType = 1; /*kCTFontUserFixedPitchFontType*/
  49     private native static String getFont(int type);
  50     public static String getSystemFont() {
  51         return getFont(SystemFontType);
  52     }
  53 
  54     public static String getMonospacedFont() {
  55         return getFont(MonospacedFontType);
  56     }
  57 
  58     native static float getSystemFontSize();
  59 
  60     public static boolean populateFontFileNameMap(
  61             HashMap<String,String> fontToFileMap,
  62             HashMap<String,String> fontToFamilyNameMap,
  63             HashMap<String,ArrayList<String>> familyToFontListMap,
  64             Locale locale) {
  65 
  66         if (fontToFileMap == null ||
  67             fontToFamilyNameMap == null ||
  68             familyToFontListMap == null) {
  69             return false;
  70         }
  71         if (locale == null) {
  72             locale = Locale.ENGLISH;
  73         }
  74         String[] fontData = getFontData();
  75         if (fontData == null) return false;
  76 
  77         int i = 0;
  78         while (i < fontData.length) {
  79             String name = fontData[i++];
  80             String family = fontData[i++];
  81             String file = fontData[i++];
  82 
  83             if (PrismFontFactory.debugFonts) {
  84                 System.err.println("[MacFontFinder] Name=" + name);
  85                 System.err.println("\tFamily=" + family);
  86                 System.err.println("\tFile=" + file);
  87             }
  88 
  89             String lcName = name.toLowerCase(locale);
  90             String lcFamily = family.toLowerCase(locale);
  91             fontToFileMap.put(lcName, file);
  92             fontToFamilyNameMap.put(lcName, family);
  93             ArrayList<String> list = familyToFontListMap.get(lcFamily);
  94             if (list == null) {
  95                 list = new ArrayList<String>();
  96                 familyToFontListMap.put(lcFamily, list);
  97             }
  98             list.add(name);
  99         }
 100         return true;
 101     }
 102     /*
 103      *
 104      * @param familyName
 105      * @return array of post-script font names
 106      */
 107     private native static String[] getFontData();
 108 }
 109