1 /*
   2  * Copyright (c) 1999, 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 
  27 /*
  28  * (C) Copyright IBM Corp. 1999,  All rights reserved.
  29  */
  30 
  31 package sun.font;
  32 
  33 import java.awt.Font;
  34 import java.awt.GraphicsEnvironment;
  35 import java.awt.font.TextAttribute;
  36 import java.text.AttributedCharacterIterator;
  37 import java.util.ArrayList;
  38 import java.util.Map;
  39 import sun.text.CodePointIterator;
  40 
  41 /**
  42  * This class maps an individual character to a Font family which can
  43  * display it.  The character-to-Font mapping does not depend on the
  44  * character's context, so a particular character will be mapped to the
  45  * same font family each time.
  46  * <p>
  47  * Typically, clients will call getIndexFor(char) for each character
  48  * in a style run.  When getIndexFor() returns a different value from
  49  * ones seen previously, the characters up to that point will be assigned
  50  * a font obtained from getFont().
  51  */
  52 public final class FontResolver {
  53 
  54     // An array of all fonts available to the runtime.  The fonts
  55     // will be searched in order.
  56     private Font[] allFonts;
  57     private Font[] supplementaryFonts;
  58     private int[]  supplementaryIndices;
  59 
  60     // Default size of Fonts (if created from an empty Map, for instance).
  61     private static final int DEFAULT_SIZE = 12; // from Font
  62 
  63     private Font defaultFont = new Font(Font.DIALOG, Font.PLAIN, DEFAULT_SIZE);
  64 
  65     // The results of previous lookups are cached in a two-level
  66     // table.  The value for a character c is found in:
  67     //     blocks[c>>SHIFT][c&MASK]
  68     // although the second array is only allocated when needed.
  69     // A 0 value means the character's font has not been looked up.
  70     // A positive value means the character's font is in the allFonts
  71     // array at index (value-1).
  72     private static final int SHIFT = 9;
  73     private static final int BLOCKSIZE = 1<<(16-SHIFT);
  74     private static final int MASK = BLOCKSIZE-1;
  75     private int[][] blocks = new int[1<<SHIFT][];
  76 
  77     private FontResolver() {
  78     }
  79 
  80     private Font[] getAllFonts() {
  81         if (allFonts == null) {
  82             allFonts =
  83             GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
  84             for (int i=0; i < allFonts.length; i++) {
  85                 allFonts[i] = allFonts[i].deriveFont((float)DEFAULT_SIZE);
  86             }
  87         }
  88         return allFonts;
  89     }
  90 
  91     /**
  92      * Search fonts in order, and return "1" to indicate its in the default
  93      * font, (or not found at all),  or the index of the first font
  94      * which can display the given character, plus 2, if it is not
  95      * in the default font.
  96      */
  97     private int getIndexFor(char c) {
  98 
  99         if (defaultFont.canDisplay(c)) {
 100             return 1;
 101         }
 102         for (int i=0; i < getAllFonts().length; i++) {
 103             if (allFonts[i].canDisplay(c)) {
 104                 return i+2;
 105             }
 106         }
 107         return 1;
 108     }
 109 
 110     private Font [] getAllSCFonts() {
 111 
 112         if (supplementaryFonts == null) {
 113             ArrayList<Font> fonts = new ArrayList<Font>();
 114             ArrayList<Integer> indices = new ArrayList<Integer>();
 115 
 116             for (int i=0; i<getAllFonts().length; i++) {
 117                 Font font = allFonts[i];
 118                 Font2D font2D = FontUtilities.getFont2D(font);
 119                 if (font2D.hasSupplementaryChars()) {
 120                     fonts.add(font);
 121                     indices.add(Integer.valueOf(i));
 122                 }
 123             }
 124 
 125             int len = fonts.size();
 126             supplementaryIndices = new int[len];
 127             for (int i=0; i<len; i++) {
 128                 supplementaryIndices[i] = indices.get(i);
 129             }
 130             supplementaryFonts = fonts.toArray(new Font[len]);
 131         }
 132         return supplementaryFonts;
 133     }
 134 
 135     /* This method is called only for character codes >= 0x10000 - which
 136      * are assumed to be legal supplementary characters.
 137      * It looks first at the default font (to avoid calling getAllFonts if at
 138      * all possible) and if that doesn't map the code point, it scans
 139      * just the fonts that may contain supplementary characters.
 140      * The index that is returned is into the "allFonts" array so that
 141      * callers see the same value for both supplementary and base chars.
 142      */
 143     private int getIndexFor(int cp) {
 144 
 145         if (defaultFont.canDisplay(cp)) {
 146             return 1;
 147         }
 148 
 149         for (int i = 0; i < getAllSCFonts().length; i++) {
 150             if (supplementaryFonts[i].canDisplay(cp)) {
 151                 return supplementaryIndices[i]+2;
 152             }
 153         }
 154         return 1;
 155     }
 156 
 157     /**
 158      * Return an index for the given character.  The index identifies a
 159      * font family to getFont(), and has no other inherent meaning.
 160      * @param c the character to map
 161      * @return a value for consumption by getFont()
 162      * @see #getFont
 163      */
 164     public int getFontIndex(char c) {
 165 
 166         int blockIndex = c>>SHIFT;
 167         int[] block = blocks[blockIndex];
 168         if (block == null) {
 169             block = new int[BLOCKSIZE];
 170             blocks[blockIndex] = block;
 171         }
 172 
 173         int index = c & MASK;
 174         if (block[index] == 0) {
 175             block[index] = getIndexFor(c);
 176         }
 177         return block[index];
 178     }
 179 
 180     public int getFontIndex(int cp) {
 181         if (cp < 0x10000) {
 182             return getFontIndex((char)cp);
 183         }
 184         return getIndexFor(cp);
 185     }
 186 
 187     /**
 188      * Determines the font index for the code point at the current position in the
 189      * iterator, then advances the iterator to the first code point that has
 190      * a different index or until the iterator is DONE, and returns the font index.
 191      * @param iter a code point iterator, this will be advanced past any code
 192      *             points that have the same font index
 193      * @return the font index for the initial code point found, or 1 if the iterator
 194      * was empty.
 195      */
 196     public int nextFontRunIndex(CodePointIterator iter) {
 197         int cp = iter.next();
 198         int fontIndex = 1;
 199         if (cp != CodePointIterator.DONE) {
 200             fontIndex = getFontIndex(cp);
 201 
 202             while ((cp = iter.next()) != CodePointIterator.DONE) {
 203                 if (getFontIndex(cp) != fontIndex) {
 204                     iter.prev();
 205                     break;
 206                 }
 207             }
 208         }
 209         return fontIndex;
 210     }
 211 
 212     /**
 213      * Return a Font from a given font index with properties
 214      * from attributes.  The font index, which should have been produced
 215      * by getFontIndex(), determines a font family.  The size and style
 216      * of the Font reflect the properties in attributes.  Any Font or
 217      * font family specifications in attributes are ignored, on the
 218      * assumption that clients have already handled them.
 219      * @param index an index from getFontIndex() which determines the
 220      *        font family
 221      * @param attributes a Map from which the size and style of the Font
 222      *        are determined.  The default size is 12 and the default style
 223      *        is Font.PLAIN
 224      * @see #getFontIndex
 225      */
 226     public Font getFont(int index,
 227                         Map<? extends AttributedCharacterIterator.Attribute, ?> attributes) {
 228         Font font = defaultFont;
 229 
 230         if (index >= 2) {
 231             font = allFonts[index-2];
 232         }
 233 
 234         return font.deriveFont(attributes);
 235     }
 236 
 237     private static FontResolver INSTANCE;
 238 
 239     /**
 240      * Return a shared instance of FontResolver.
 241      */
 242     public static FontResolver getInstance() {
 243         if (INSTANCE == null) {
 244             INSTANCE = new FontResolver();
 245         }
 246         return INSTANCE;
 247     }
 248 }