FileDocCategorySizeDatePackage
FontFinder.javaAPI DocAndroid 1.5 API4206Wed May 06 22:41:54 BST 2009org.apache.harmony.awt.gl.font

FontFinder

public class FontFinder extends Object
This class chooses the default font for the given text. If it finds the character which current font is unable to display it starts the next font run and looks for the font which is able to display the current character. It also caches the font mappings (index in the array containing all fonts) for the characters, using that fact that scripts are mainly contiguous in the UTF-16 encoding and there's a high probability that the upper byte will be the same for the next character as for the previous. This allows to save the space used for the cache.

Fields Summary
private static final float
DEFAULT_FONT_SIZE
private static final Font[]
fonts
private static final int
NUM_BLOCKS
private static final int
BLOCK_SIZE
private static final int
INDEX_MASK
private static final int
BLOCK_SHIFT
private static final int[]
blocks
Constructors Summary
Methods Summary
static java.awt.FontfindFontForChar(char c)
Finds the font which is able to display the given character and saves the font mapping for this character

param
c - character
return
font


                                  
        
        int blockNum = c >> BLOCK_SHIFT;
        int index = c & INDEX_MASK;

        if (blocks[blockNum] == null) {
            blocks[blockNum] = new int[BLOCK_SIZE];
        }

        if (blocks[blockNum][index] == 0) {
            blocks[blockNum][index] = 1;

            for (int i=0; i<fonts.length; i++) {
                if (fonts[i].canDisplay(c)) {
                    blocks[blockNum][index] = i+1;
                    break;
                }
            }
        }

        return getDefaultSizeFont(blocks[blockNum][index]-1);
    
static voidfindFonts(char[] text, int runStart, int runLimit, java.util.List runStarts, java.util.Map fonts)
Assigns default fonts for the given text run. First three parameters are input, last three are output.

param
text - given text
param
runStart - start of the text run
param
runLimit - end of the text run
param
runStarts - starts of the resulting font runs
param
fonts - mapping of the font run starts to the fonts

        Font prevFont = null;
        Font currFont;
        for (int i = runStart; i < runLimit; i++) {
            currFont = findFontForChar(text[i]);
            if (currFont != prevFont) {
                prevFont = currFont;
                Integer idx = new Integer(i);
                fonts.put(idx, currFont);
                if (i != runStart) {
                    runStarts.add(idx);
                }
            }
        }
    
static java.awt.FontgetDefaultSizeFont(int i)
Derives the default size font

param
i - index in the array of all fonts
return
derived font

        if (fonts[i].getSize() != DEFAULT_FONT_SIZE) {
            fonts[i] = fonts[i].deriveFont(DEFAULT_FONT_SIZE);
        }

        return fonts[i];