Path: blob/master/src/java.desktop/share/classes/sun/font/FontResolver.java
41154 views
/*1* Copyright (c) 1999, 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*24*/2526/*27* (C) Copyright IBM Corp. 1999, All rights reserved.28*/2930package sun.font;3132import java.awt.Font;33import java.awt.GraphicsEnvironment;34import java.awt.font.TextAttribute;35import java.text.AttributedCharacterIterator;36import java.util.ArrayList;37import java.util.Map;3839/**40* This class maps an individual character to a Font family which can41* display it. The character-to-Font mapping does not depend on the42* character's context, so a particular character will be mapped to the43* same font family each time.44* <p>45* Typically, clients will call getIndexFor(char) for each character46* in a style run. When getIndexFor() returns a different value from47* ones seen previously, the characters up to that point will be assigned48* a font obtained from getFont().49*/50public final class FontResolver {5152// An array of all fonts available to the runtime. The fonts53// will be searched in order.54private Font[] allFonts;55private Font[] supplementaryFonts;56private int[] supplementaryIndices;5758// Default size of Fonts (if created from an empty Map, for instance).59private static final int DEFAULT_SIZE = 12; // from Font6061private Font defaultFont = new Font(Font.DIALOG, Font.PLAIN, DEFAULT_SIZE);6263// The results of previous lookups are cached in a two-level64// table. The value for a character c is found in:65// blocks[c>>SHIFT][c&MASK]66// although the second array is only allocated when needed.67// A 0 value means the character's font has not been looked up.68// A positive value means the character's font is in the allFonts69// array at index (value-1).70private static final int SHIFT = 9;71private static final int BLOCKSIZE = 1<<(16-SHIFT);72private static final int MASK = BLOCKSIZE-1;73private int[][] blocks = new int[1<<SHIFT][];7475private FontResolver() {76}7778private Font[] getAllFonts() {79if (allFonts == null) {80allFonts =81GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();82for (int i=0; i < allFonts.length; i++) {83allFonts[i] = allFonts[i].deriveFont((float)DEFAULT_SIZE);84}85}86return allFonts;87}8889/**90* Search fonts in order, and return "1" to indicate its in the default91* font, (or not found at all), or the index of the first font92* which can display the given character, plus 2, if it is not93* in the default font.94*/95private int getIndexFor(char c) {9697if (defaultFont.canDisplay(c)) {98return 1;99}100for (int i=0; i < getAllFonts().length; i++) {101if (allFonts[i].canDisplay(c)) {102return i+2;103}104}105return 1;106}107108private Font [] getAllSCFonts() {109110if (supplementaryFonts == null) {111ArrayList<Font> fonts = new ArrayList<Font>();112ArrayList<Integer> indices = new ArrayList<Integer>();113114for (int i=0; i<getAllFonts().length; i++) {115Font font = allFonts[i];116Font2D font2D = FontUtilities.getFont2D(font);117if (font2D.hasSupplementaryChars()) {118fonts.add(font);119indices.add(Integer.valueOf(i));120}121}122123int len = fonts.size();124supplementaryIndices = new int[len];125for (int i=0; i<len; i++) {126supplementaryIndices[i] = indices.get(i);127}128supplementaryFonts = fonts.toArray(new Font[len]);129}130return supplementaryFonts;131}132133/* This method is called only for character codes >= 0x10000 - which134* are assumed to be legal supplementary characters.135* It looks first at the default font (to avoid calling getAllFonts if at136* all possible) and if that doesn't map the code point, it scans137* just the fonts that may contain supplementary characters.138* The index that is returned is into the "allFonts" array so that139* callers see the same value for both supplementary and base chars.140*/141private int getIndexFor(int cp) {142143if (defaultFont.canDisplay(cp)) {144return 1;145}146147for (int i = 0; i < getAllSCFonts().length; i++) {148if (supplementaryFonts[i].canDisplay(cp)) {149return supplementaryIndices[i]+2;150}151}152return 1;153}154155/**156* Return an index for the given character. The index identifies a157* font family to getFont(), and has no other inherent meaning.158* @param c the character to map159* @return a value for consumption by getFont()160* @see #getFont161*/162public int getFontIndex(char c) {163164int blockIndex = c>>SHIFT;165int[] block = blocks[blockIndex];166if (block == null) {167block = new int[BLOCKSIZE];168blocks[blockIndex] = block;169}170171int index = c & MASK;172if (block[index] == 0) {173block[index] = getIndexFor(c);174}175return block[index];176}177178public int getFontIndex(int cp) {179if (cp < 0x10000) {180return getFontIndex((char)cp);181}182return getIndexFor(cp);183}184185/**186* Determines the font index for the code point at the current position in the187* iterator, then advances the iterator to the first code point that has188* a different index or until the iterator is DONE, and returns the font index.189* @param iter a code point iterator, this will be advanced past any code190* points that have the same font index191* @return the font index for the initial code point found, or 1 if the iterator192* was empty.193*/194public int nextFontRunIndex(CodePointIterator iter) {195int cp = iter.next();196int fontIndex = 1;197if (cp != CodePointIterator.DONE) {198fontIndex = getFontIndex(cp);199200while ((cp = iter.next()) != CodePointIterator.DONE) {201if (getFontIndex(cp) != fontIndex) {202iter.prev();203break;204}205}206}207return fontIndex;208}209210/**211* Return a Font from a given font index with properties212* from attributes. The font index, which should have been produced213* by getFontIndex(), determines a font family. The size and style214* of the Font reflect the properties in attributes. Any Font or215* font family specifications in attributes are ignored, on the216* assumption that clients have already handled them.217* @param index an index from getFontIndex() which determines the218* font family219* @param attributes a Map from which the size and style of the Font220* are determined. The default size is 12 and the default style221* is Font.PLAIN222* @see #getFontIndex223*/224public Font getFont(int index,225Map<? extends AttributedCharacterIterator.Attribute, ?> attributes) {226Font font = defaultFont;227228if (index >= 2) {229font = allFonts[index-2];230}231232return font.deriveFont(attributes);233}234235private static FontResolver INSTANCE;236237/**238* Return a shared instance of FontResolver.239*/240public static FontResolver getInstance() {241if (INSTANCE == null) {242INSTANCE = new FontResolver();243}244return INSTANCE;245}246}247248249