aboutsummaryrefslogtreecommitdiffstats
path: root/src/java/org/apache/fop/fo/properties/FontShorthandProperty.java
blob: 3d1fa6604a937f535a52193a01fdb2712e639f0e (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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
 * Copyright 2006 The Apache Software Foundation.
 * 
 * Licensed 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.fo.properties;

import org.apache.fop.fo.Constants;
import org.apache.fop.fo.FObj;
import org.apache.fop.fo.PropertyList;
import org.apache.fop.fo.expr.PropertyException;

/**
 * Property subclass for the font shorthand
 */
public class FontShorthandProperty extends ListProperty {

    /**
     * Inner class for creating instances of FontShorthandProperty
     */
    public static class Maker extends PropertyMaker {

        private static final int[] PROP_IDS = {
            Constants.PR_FONT_SIZE, Constants.PR_FONT_FAMILY,
            Constants.PR_LINE_HEIGHT, Constants.PR_FONT_STYLE,
            Constants.PR_FONT_VARIANT, Constants.PR_FONT_WEIGHT
        };
        
        /**
         * @param propId ID of the property for which Maker should be created
         */
        public Maker(int propId) {
            super(propId);
        }
        
        /**
         * @see PropertyMaker#make(PropertyList, String, FObj)
         */
        public Property make(PropertyList propertyList, 
                String value, FObj fo) throws PropertyException {
            
            FontShorthandProperty newProp = new FontShorthandProperty();
            newProp.setSpecifiedValue(value);
            
            String specVal = value;
            Property prop = null;
            if ("inherit".equals(specVal)) {
                for (int i = PROP_IDS.length; --i >= 0;) {
                    prop = propertyList.getFromParent(PROP_IDS[i]);
                    newProp.addProperty(prop, i);
                }
            } else {
                /* initialize list with nulls */
                for (int pos = 6; --pos >= 0;) {
                    newProp.addProperty(null, pos);
                }
                prop = checkEnumValues(specVal);
                if (prop == null) {
                    /* not an enum:
                     * value should consist at least of font-size and font-family
                     * separated by a space
                     * mind the possible spaces from quoted font-family names
                     */
                    int spaceIndex = value.indexOf(' ');
                    int quoteIndex = (value.indexOf('\'') == -1)
                        ? value.indexOf('\"') : value.indexOf('\'');
                    if (spaceIndex == -1 
                            || (quoteIndex != -1 && spaceIndex > quoteIndex)) {
                        /* no spaces or first space appears after the first
                         * single/double quote, so malformed value string
                         */
                        throw new PropertyException("Invalid property value: "
                                + "font=\"" + value + "\"");                        
                    } 
                    PropertyMaker m = null;
                    int fromIndex = spaceIndex + 1;
                    int toIndex = specVal.length();
                    /* at least one space that appears before the first
                     * single/double quote, so extract the individual properties
                     */
                    boolean fontFamilyParsed = false;
                    int commaIndex = value.indexOf(',');
                    while (!fontFamilyParsed) {
                        /* value contains a (list of) possibly quoted 
                         * font-family name(s) 
                         */
                        if (commaIndex == -1) {
                            /* no list, just a single name 
                             * (or first name in the list)
                             */
                            if (quoteIndex != -1) {
                                /* a single name, quoted
                                 */
                                fromIndex = quoteIndex;
                            }
                            m = FObj.getPropertyMakerFor(PROP_IDS[1]);
                            prop = m.make(propertyList, specVal.substring(fromIndex), fo);
                            newProp.addProperty(prop, 1);
                            fontFamilyParsed = true;                            
                        } else {
                            if (quoteIndex != -1 && quoteIndex < commaIndex) {
                                /* a quoted font-family name as first name
                                 * in the comma-separated list
                                 * fromIndex = index of the first quote
                                 */
                                fromIndex = quoteIndex;
                                quoteIndex = -1;
                            } else {
                                fromIndex = value.lastIndexOf(' ', commaIndex) + 1;
                            }
                            commaIndex = -1;
                        }
                    }
                    toIndex = fromIndex - 1;
                    fromIndex = value.lastIndexOf(' ', toIndex - 1) + 1;
                    value = specVal.substring(fromIndex, toIndex);
                    int slashIndex = value.indexOf('/');
                    String fontSize = value.substring(0, 
                            (slashIndex == -1) ? value.length() : slashIndex);
                    m = FObj.getPropertyMakerFor(PROP_IDS[0]);
                    prop = m.make(propertyList, fontSize, fo);
                    /* need to make sure subsequent call to LineHeightPropertyMaker.make()
                     * doesn't generate the default font-size property...
                     */
                    propertyList.putExplicit(PROP_IDS[0], prop);
                    newProp.addProperty(prop, 0);
                    if (slashIndex != -1) {
                        /* line-height */
                        String lineHeight = value.substring(slashIndex + 1);
                        m = FObj.getPropertyMakerFor(PROP_IDS[2]);
                        prop = m.make(propertyList, lineHeight, fo);
                        newProp.addProperty(prop, 2);
                    }
                    if (fromIndex != 0) {
                        toIndex = fromIndex - 1;
                        value = specVal.substring(0, toIndex);
                        fromIndex = 0;
                        spaceIndex = value.indexOf(' ');
                        do {
                            toIndex = (spaceIndex == -1) ? value.length() : spaceIndex;
                            String val = value.substring(fromIndex, toIndex);
                            for (int i = 6; --i >= 3;) {
                                if (newProp.list.get(i) == null) {
                                    /* not set */
                                    m = FObj.getPropertyMakerFor(PROP_IDS[i]);
                                    val = m.checkValueKeywords(val);
                                    prop = m.checkEnumValues(val);
                                    if (prop != null) {
                                        newProp.addProperty(prop, i);
                                    }
                                }
                            }
                            fromIndex = toIndex + 1;
                            spaceIndex = value.indexOf(' ', fromIndex);
                        } while (toIndex != value.length());
                    }
                } else {
                    //TODO: implement enum values
                    log.warn("Enum values other than \"inherit\""
                            + " not yet supported for the font shorthand.");
                    return null;
                }
            }
            if (newProp.list.get(0) == null || newProp.list.get(1) == null) {
                throw new PropertyException("Invalid property value: "
                        + "font-size and font-family are required for the font shorthand"
                        + "\nfont=" + value);
            }
            return newProp;
        }

    }
    
    private void addProperty(Property prop, int pos) {
        while (list.size() < (pos + 1)) {
            list.add(null);
        }
        list.set(pos, prop);
    }
}