aboutsummaryrefslogtreecommitdiffstats
path: root/src/java/org/apache/fop/fonts/FontSelector.java
blob: a80a0e3686064d0304bce40ba91a9e3654936f13 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/* $Id$ */

package org.apache.fop.fonts;

import org.apache.fop.datatypes.PercentBaseContext;
import org.apache.fop.fo.FONode;
import org.apache.fop.fo.FOText;
import org.apache.fop.fo.flow.Character;
import org.apache.fop.fo.properties.CommonFont;

/**
 * Helper class for automatic font selection.
 * <p>
 * TODO: Check if this could be merged with another font class, such as
 * {@link FontManager}.
 */
public final class FontSelector {
    private FontSelector() {
        // Static since this is an utility class.
    }

    private static Font selectFontForCharacter(char c, FONode fonode,
            CommonFont commonFont, PercentBaseContext context) {
        FontInfo fi = fonode.getFOEventHandler().getFontInfo();
        FontTriplet[] fontkeys = commonFont.getFontState(fi);
        for (int i = 0; i < fontkeys.length; i++) {
            Font font = fi.getFontInstance(fontkeys[i], commonFont.fontSize
                    .getValue(context));
            if (font.hasChar(c)) {
                return font;
            }
        }
        return fi.getFontInstance(fontkeys[0], commonFont.fontSize
                .getValue(context));

    }

    /**
     * Selects a font which is able to display the given character.
     *
     * @param fobj
     *            a Character object containing the character and its
     *            attributes.
     * @param context
     *            the Percent-based context needed for creating the actual font.
     * @return a Font object.
     */
    public static Font selectFontForCharacter(Character fobj,
            PercentBaseContext context) {
        return FontSelector.selectFontForCharacter(fobj.getCharacter(), fobj,
                fobj.getCommonFont(), context);
    }

    /**
     * Selects a font which is able to display the given character.
     *
     * @param c
     *            character to find.
     * @param text
     *            the text object which contains the character
     * @param context
     *            the Percent-based context needed for creating the actual font.
     * @return a Font object.
     */
    public static Font selectFontForCharacterInText(char c, FOText text,
            PercentBaseContext context) {
        return FontSelector.selectFontForCharacter(c, text, text
                .getCommonFont(), context);
    }

    /**
     * Selects a font which is able to display the most of the given characters.
     *
     * @param charSeq
     *            Text to go through
     * @param firstIndex
     *            first index within text.
     * @param breakIndex
     *            last index +1 within text.
     * @param text
     *            the text object which contains the character
     * @param context
     *            the Percent-based context needed for creating the actual font.
     * @return a Font object.
     */
    public static Font selectFontForCharactersInText(CharSequence charSeq,
            int firstIndex, int breakIndex, FOText text,
            PercentBaseContext context) {

        final FontInfo fi = text.getFOEventHandler().getFontInfo();
        final CommonFont commonFont = text.getCommonFont();
        final FontTriplet[] fontkeys = commonFont.getFontState(fi);
        final int numFonts = fontkeys.length;
        final Font[] fonts = new Font[numFonts];
        final int[] fontCount = new int[numFonts];

        for (int fontnum = 0; fontnum < numFonts; fontnum++) {
            final Font font = fi.getFontInstance(fontkeys[fontnum],
                    commonFont.fontSize.getValue(context));
            fonts[fontnum] = font;
            for (int pos = firstIndex; pos < breakIndex; pos++) {
                if (font.hasChar(charSeq.charAt(pos))) {
                    fontCount[fontnum]++;
                }
            }

            // quick fall through if all characters can be displayed
            if (fontCount[fontnum] == (breakIndex - firstIndex)) {
                return font;
            }
        }

        Font font = fonts[0];
        int max = fontCount[0];

        for (int fontnum = 1; fontnum < numFonts; fontnum++) {
            final int curCount = fontCount[fontnum];
            if (curCount > max) {
                font = fonts[fontnum];
                max = curCount;
            }
        }
        return font;
    }

}