aboutsummaryrefslogtreecommitdiffstats
path: root/src/org/apache/fop/layout/FontInfo.java
blob: 4f145e3eab62023b21376eb5a9c5c2eb1fd9bf71 (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
/*
 * $Id$
 * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
 * For details on use and redistribution please refer to the
 * LICENSE file included with these sources.
 */

package org.apache.fop.layout;

import java.util.HashMap;
import org.apache.fop.messaging.MessageHandler;
import java.util.Enumeration;

import org.apache.fop.apps.FOPException;

public class FontInfo {
    HashMap usedFonts;
    HashMap triplets;    // look up a font-triplet to find a font-name
    HashMap fonts;    // look up a font-name to get a font (that implements FontMetric at least)

    public FontInfo() {
        this.triplets = new HashMap();
        this.fonts = new HashMap();
        this.usedFonts = new HashMap();
    }

    public void addFontProperties(String name, String family, String style,
                                  String weight) {
        /*
         * add the given family, style and weight as a lookup for the font
         * with the given name
         */

        String key = createFontKey(family, style, weight);
        this.triplets.put(key, name);
    }

    public void addMetrics(String name, FontMetric metrics) {
        // add the given metrics as a font with the given name

        this.fonts.put(name, metrics);
    }

    public String fontLookup(String family, String style,
                             String weight) throws FOPException {
        return fontLookup(createFontKey(family, style, weight));
    }

    public String fontLookup(String key) throws FOPException {

        String f = (String)this.triplets.get(key);
        if (f == null) {
            int i = key.indexOf(',');
            String s = "any" + key.substring(i);
            f = (String)this.triplets.get(s);
            if (f == null) {
                f = (String)this.triplets.get("any,normal,normal");
                if (f == null) {
                    throw new FOPException("no default font defined by OutputConverter");
                }
                MessageHandler.errorln("defaulted font to any,normal,normal");
            }
            MessageHandler.errorln("unknown font " + key
                                   + " so defaulted font to any");
        }

        usedFonts.put(f, fonts.get(f));
        return f;
    }

    public boolean hasFont(String family, String style, String weight) {
        String key = createFontKey(family, style, weight);
        return this.triplets.get(key) != null;
    }

    /**
     * Creates a key from the given strings
     */
    public static String createFontKey(String family, String style,
                                       String weight) {
        int i;

        try {
            i = Integer.parseInt(weight);
        } catch (NumberFormatException e) {
            i = 0;
        }

        if (i > 600)
            weight = "bold";
        else if (i > 0)
            weight = "normal";

        return family + "," + style + "," + weight;
    }

    public HashMap getFonts() {
        return this.fonts;
    }

    public HashMap getUsedFonts() {
        return this.usedFonts;
    }

    public FontMetric getMetricsFor(String fontName) throws FOPException {
        usedFonts.put(fontName, fonts.get(fontName));
        return (FontMetric)fonts.get(fontName);
    }

}