aboutsummaryrefslogtreecommitdiffstats
path: root/src/org/apache/fop/layout/FontState.java
blob: 147e1198d50e563392b8356cf66cf1c4653e7151 (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
/*
 * $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.apps.FOPException;
import org.apache.fop.fo.properties.FontVariant;
import org.apache.fop.render.pdf.CodePointMapping;

public class FontState {

    private FontInfo _fontInfo;
    private String _fontName;
    private int _fontSize;
    private String _fontFamily;
    private String _fontStyle;
    private String _fontWeight;
    private int _fontVariant;

    private FontMetric _metric;

    private static HashMap EMPTY_HASHMAP = new HashMap();


    public FontState(FontInfo fontInfo, String fontFamily, String fontStyle,
                     String fontWeight, int fontSize,
                     int fontVariant) throws FOPException {
        _fontInfo = fontInfo;
        _fontFamily = fontFamily;
        _fontStyle = fontStyle;
        _fontWeight = fontWeight;
        _fontSize = fontSize;
        _fontName = fontInfo.fontLookup(fontFamily, fontStyle, fontWeight);
        _metric = fontInfo.getMetricsFor(_fontName);
        _fontVariant = fontVariant;
    }

    public int getAscender() {
        return _metric.getAscender(_fontSize) / 1000;
    }

    public int getCapHeight() {
        return _metric.getCapHeight(_fontSize) / 1000;
    }

    public int getDescender() {
        return _metric.getDescender(_fontSize) / 1000;
    }

    public String getFontName() {
        return _fontName;
    }

    public int getFontSize() {
        return _fontSize;
    }

    public String getFontWeight() {
        return _fontWeight;
    }

    public String getFontFamily() {
        return _fontFamily;
    }

    public String getFontStyle() {
        return _fontStyle;
    }

    public int getFontVariant() {
        return _fontVariant;
    }

    public FontInfo getFontInfo() {
        return _fontInfo;
    }

    public int getXHeight() {
        return _metric.getXHeight(_fontSize) / 1000;
    }

    public HashMap getKerning() {
        if (_metric instanceof FontDescriptor) {
            HashMap ret = ((FontDescriptor)_metric).getKerningInfo();
            if (ret != null)
                return ret;
        }
        return EMPTY_HASHMAP;
    }

    public int width(int charnum) {
        // returns width of given character number in millipoints
        return (_metric.width(charnum, _fontSize) / 1000);
    }

    /**
     * Map a java character (unicode) to a font character
     * Default uses CodePointMapping
     */
    public char mapChar(char c) {

        if (_metric instanceof org.apache.fop.render.pdf.Font) {
            return ((org.apache.fop.render.pdf.Font)_metric).mapChar(c);
        }

        // Use default CodePointMapping
	char d = CodePointMapping.getMapping("WinAnsiEncoding").mapChar(c);
	if (d != 0) {
	    c = d;
	} else {
	    c = '#';
	}

        return c;
    }

    public String toString() {
	StringBuffer sbuf = new StringBuffer();
	sbuf.append('(');
	sbuf.append(_fontFamily);
	sbuf.append(',');
	sbuf.append(_fontName);
	sbuf.append(',');
	sbuf.append(_fontSize);
	sbuf.append(',');
	sbuf.append(_fontStyle);
	sbuf.append(',');
	sbuf.append(_fontWeight);
	sbuf.append(')');
	return sbuf.toString();
    }
}