aboutsummaryrefslogtreecommitdiffstats
path: root/src/java/org/apache/fop/render/afp/AFPFontConfig.java
blob: 4b2ece440e92e6587003022680d8b360dfffb278 (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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/*
 * 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.render.afp;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.ConfigurationException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.apache.fop.afp.AFPEventProducer;
import org.apache.fop.afp.fonts.AFPFont;
import org.apache.fop.afp.fonts.AFPFontInfo;
import org.apache.fop.afp.fonts.CharacterSet;
import org.apache.fop.afp.fonts.CharacterSetBuilder;
import org.apache.fop.afp.fonts.CharacterSetType;
import org.apache.fop.afp.fonts.DoubleByteFont;
import org.apache.fop.afp.fonts.OutlineFont;
import org.apache.fop.afp.fonts.RasterFont;
import org.apache.fop.afp.util.AFPResourceAccessor;
import org.apache.fop.apps.FOPException;
import org.apache.fop.apps.io.InternalResourceResolver;
import org.apache.fop.events.EventProducer;
import org.apache.fop.fonts.FontConfig;
import org.apache.fop.fonts.FontManager;
import org.apache.fop.fonts.FontManagerConfigurator;
import org.apache.fop.fonts.FontTriplet;
import org.apache.fop.fonts.FontTriplet.Matcher;
import org.apache.fop.fonts.FontUtil;
import org.apache.fop.fonts.Typeface;

/**
 * The config object for AFP fonts, these differ from the the more generic fonts (TTF and Type1).
 */
public final class AFPFontConfig implements FontConfig {

    private static final Log LOG = LogFactory.getLog(AFPFontConfig.class);

    private final List<AFPFontConfigData> fontsConfig;

    private AFPFontConfig() {
        fontsConfig = new ArrayList<AFPFontConfigData>();
    }

    /**
     * Returns a list of AFP font configuration data.
     * @return the AFP font config data
     */
    public List<AFPFontConfigData> getFontConfig() {
        return fontsConfig;
    }

    /**
     * The parser for AFP font data.
     */
    static final class AFPFontInfoConfigParser implements FontConfigParser {

        /** {@inheritDoc}} */
        public AFPFontConfig parse(Configuration cfg, FontManager fontManager, boolean strict,
                EventProducer eventProducer) throws FOPException {
            try {
                return new ParserHelper(cfg, fontManager, strict,
                        (AFPEventProducer) eventProducer).fontConfig;
            } catch (ConfigurationException ce) {
                throw new FOPException(ce);
            }
        }

        AFPFontConfig getEmptyConfig() {
            return new AFPFontConfig();
        }
    }

    private static final class AggregateMatcher implements Matcher {

        private final List<Matcher> matchers;

        private AggregateMatcher(Matcher... matchers) {
            this.matchers = new ArrayList<Matcher>();
            for (Matcher matcher : matchers) {
                if (matcher != null) {
                    this.matchers.add(matcher);
                }
            }
        }

        public boolean matches(FontTriplet triplet) {
            for (Matcher matcher : matchers) {
                if (matcher.matches(triplet)) {
                    return true;
                }
            }
            return false;
        }

    }

    private static final class ParserHelper {

        private static final Log LOG = LogFactory.getLog(ParserHelper.class);

        private final AFPFontConfig fontConfig;

        private final Matcher matcher;

        private ParserHelper(Configuration cfg, FontManager fontManager, boolean strict,
                AFPEventProducer eventProducer) throws FOPException, ConfigurationException {
            Configuration fonts = cfg.getChild("fonts");
            Matcher localMatcher = null;
            Configuration referencedFontsCfg = fonts.getChild("referenced-fonts", false);
            if (referencedFontsCfg != null) {
                localMatcher = FontManagerConfigurator.createFontsMatcher(referencedFontsCfg, strict);
            }
            matcher = new AggregateMatcher(fontManager.getReferencedFontsMatcher(), localMatcher);
            fontConfig = new AFPFontConfig();
            for (Configuration font : fonts.getChildren("font")) {
                buildFont(font, eventProducer);
            }
        }

        private void buildFont(Configuration fontCfg, AFPEventProducer eventProducer)
                throws ConfigurationException {
            //FontManager fontManager = this.userAgent.getFontManager();
            Configuration[] triplets = fontCfg.getChildren("font-triplet");
            List<FontTriplet> tripletList = new ArrayList<FontTriplet>();
            if (triplets.length == 0) {
                eventProducer.fontConfigMissing(this, "<font-triplet...", fontCfg.getLocation());
                return;
            }
            for (Configuration triplet : triplets) {
                int weight = FontUtil.parseCSS2FontWeight(triplet.getAttribute("weight"));
                FontTriplet fontTriplet = new FontTriplet(triplet.getAttribute("name"),
                        triplet.getAttribute("style"), weight);
                tripletList.add(fontTriplet);
            }
            //build the fonts
            Configuration[] config = fontCfg.getChildren("afp-font");
            if (config.length == 0) {
                eventProducer.fontConfigMissing(this, "<afp-font...", fontCfg.getLocation());
                return;
            }
            Configuration afpFontCfg = config[0];
            String uri = afpFontCfg.getAttribute("base-uri", null);
            try {
                String type = afpFontCfg.getAttribute("type");
                if (type == null) {
                    eventProducer.fontConfigMissing(this, "type attribute", fontCfg.getLocation());
                    return;
                }
                String codepage = afpFontCfg.getAttribute("codepage");
                if (codepage == null) {
                    eventProducer.fontConfigMissing(this, "codepage attribute",
                            fontCfg.getLocation());
                    return;
                }
                String encoding = afpFontCfg.getAttribute("encoding");
                if (encoding == null) {
                    eventProducer.fontConfigMissing(this, "encoding attribute",
                            fontCfg.getLocation());
                    return;
                }

                fontFromType(tripletList, type, codepage, encoding, afpFontCfg, eventProducer, uri);
            } catch (ConfigurationException ce) {
                eventProducer.invalidConfiguration(this, ce);
            }
        }

        private void fontFromType(List<FontTriplet> fontTriplets, String type, String codepage,
                String encoding, Configuration cfg, AFPEventProducer eventProducer, String embedURI)
                throws ConfigurationException {
            AFPFontConfigData config = null;
            if ("raster".equalsIgnoreCase(type)) {
                config = getRasterFont(fontTriplets, type, codepage, encoding, cfg, eventProducer,
                        embedURI);
            } else if ("outline".equalsIgnoreCase(type)) {
                config = getOutlineFont(fontTriplets, type, codepage, encoding, cfg, eventProducer,
                        embedURI);
            } else if ("CIDKeyed".equalsIgnoreCase(type)) {
                config = getCIDKeyedFont(fontTriplets, type, codepage, encoding, cfg,
                        eventProducer,
                        embedURI);
            } else {
                LOG.error("No or incorrect type attribute: " + type);
            }
            if (config != null) {
                fontConfig.fontsConfig.add(config);
            }
        }

        private CIDKeyedFontConfig getCIDKeyedFont(List<FontTriplet> fontTriplets, String type,
                String codepage, String encoding, Configuration cfg, AFPEventProducer eventProducer,
                String uri) throws ConfigurationException {
            String characterset = cfg.getAttribute("characterset");
            if (characterset == null) {
                eventProducer.fontConfigMissing(this, "characterset attribute",
                        cfg.getLocation());
                return null;
            }
            String name = cfg.getAttribute("name", characterset);
            CharacterSetType charsetType = cfg.getAttributeAsBoolean("ebcdic-dbcs", false)
                    ? CharacterSetType.DOUBLE_BYTE_LINE_DATA : CharacterSetType.DOUBLE_BYTE;
            return new CIDKeyedFontConfig(fontTriplets, type, codepage, encoding, characterset,
                    name, charsetType, isEmbbedable(fontTriplets), uri);
        }

        private OutlineFontConfig getOutlineFont(List<FontTriplet> fontTriplets, String type,
                String codepage, String encoding, Configuration cfg,
                AFPEventProducer eventProducer, String uri) throws ConfigurationException {
            String characterset = cfg.getAttribute("characterset");
            if (characterset == null) {
                eventProducer.fontConfigMissing(this, "characterset attribute",
                        cfg.getLocation());
                return null;
            }
            String name = cfg.getAttribute("name", characterset);
            String base14 = cfg.getAttribute("base14-font", null);
            return new OutlineFontConfig(fontTriplets, type, codepage, encoding, characterset,
                    name, base14, isEmbbedable(fontTriplets), uri);
        }

        private RasterFontConfig getRasterFont(List<FontTriplet> triplets, String type,
                String codepage, String encoding, Configuration cfg,
                AFPEventProducer eventProducer, String uri)
                throws ConfigurationException {
            String name = cfg.getAttribute("name", "Unknown");
            // Create a new font object
            Configuration[] rasters = cfg.getChildren("afp-raster-font");
            if (rasters.length == 0) {
                eventProducer.fontConfigMissing(this, "<afp-raster-font...",
                        cfg.getLocation());
                return null;
            }
            List<RasterCharactersetData> charsetData = new ArrayList<RasterCharactersetData>();
            for (Configuration rasterCfg : rasters) {
                String characterset = rasterCfg.getAttribute("characterset");
                if (characterset == null) {
                    eventProducer.fontConfigMissing(this, "characterset attribute",
                            cfg.getLocation());
                    return null;
                }
                float size = rasterCfg.getAttributeAsFloat("size");
                int sizeMpt = (int) (size * 1000);
                String base14 = rasterCfg.getAttribute("base14-font", null);
                charsetData.add(new RasterCharactersetData(characterset, sizeMpt, base14));
            }
            return new RasterFontConfig(triplets, type, codepage, encoding, null, name, uri, charsetData,
                    isEmbbedable(triplets));
        }

        private boolean isEmbbedable(List<FontTriplet> triplets) {
            for (FontTriplet triplet : triplets) {
                if (matcher.matches(triplet)) {
                    return false;
                }
            }
            return true;
        }

    }

    abstract static class AFPFontConfigData {
        private final List<FontTriplet> triplets;
        private final String codePage;
        private final String encoding;
        private final String name;
        private final boolean embeddable;
        private final String uri;

        AFPFontConfigData(List<FontTriplet> triplets, String type, String codePage,
                String encoding, String name, boolean embeddable, String uri) {
            this.triplets = Collections.unmodifiableList(triplets);
            this.codePage = codePage;
            this.encoding = encoding;
            this.name = name;
            this.embeddable = embeddable;
            this.uri = uri;
        }

        static AFPFontInfo getFontInfo(AFPFont font, AFPFontConfigData config) {
            return font != null ? new AFPFontInfo(font, config.triplets) : null;
        }

        abstract AFPFontInfo getFontInfo(InternalResourceResolver resourceResolver,
                AFPEventProducer eventProducer) throws IOException;

        AFPResourceAccessor getAccessor(InternalResourceResolver resourceResolver) {
            return new AFPResourceAccessor(resourceResolver, uri);
        }
    }

    static final class CIDKeyedFontConfig extends AFPFontConfigData {

        private final CharacterSetType charsetType;

        private final String characterset;

        private CIDKeyedFontConfig(List<FontTriplet> triplets, String type, String codePage,
                String encoding, String characterset, String name, CharacterSetType charsetType, boolean embeddable, String uri) {
            super(triplets, type, codePage, encoding, name, embeddable, uri);
            this.characterset = characterset;
            this.charsetType = charsetType;
        }

        @Override
        AFPFontInfo getFontInfo(InternalResourceResolver resourceResolver, AFPEventProducer eventProducer)
                throws IOException {
            AFPResourceAccessor accessor = getAccessor(resourceResolver);
            CharacterSet characterSet = CharacterSetBuilder.getDoubleByteInstance().buildDBCS(
                    characterset, super.codePage, super.encoding, charsetType, accessor, eventProducer);
            return getFontInfo(new DoubleByteFont(super.codePage, super.embeddable, characterSet),
                    this);
        }
    }

    static final class OutlineFontConfig extends AFPFontConfigData {
        private final String base14;
        private final String characterset;

        private OutlineFontConfig(List<FontTriplet> triplets, String type, String codePage,
                String encoding, String characterset, String name, String base14, boolean embeddable, String uri) {
            super(triplets, type, codePage, encoding, name, embeddable, uri);
            this.characterset = characterset;
            this.base14 = base14;
        }

        @Override
        AFPFontInfo getFontInfo(InternalResourceResolver resourceResolver, AFPEventProducer eventProducer)
                throws IOException {
            CharacterSet characterSet = null;
            if (base14 != null) {
                try {
                    Class<? extends Typeface> clazz = Class.forName(
                            "org.apache.fop.fonts.base14." + base14).asSubclass(Typeface.class);
                    try {
                        Typeface tf = clazz.newInstance();
                        characterSet = CharacterSetBuilder.getSingleByteInstance()
                                                          .build(characterset, super.codePage,
                                                                  super.encoding,
                                                                  tf, eventProducer);
                    } catch (Exception ie) {
                        String msg = "The base 14 font class " + clazz.getName()
                                + " could not be instantiated";
                        LOG.error(msg);
                    }
                } catch (ClassNotFoundException cnfe) {
                    String msg = "The base 14 font class for " + characterset
                            + " could not be found";
                    LOG.error(msg);
                }
            } else {
                AFPResourceAccessor accessor = getAccessor(resourceResolver);
                characterSet = CharacterSetBuilder.getSingleByteInstance().buildSBCS(
                        characterset, super.codePage, super.encoding, accessor, eventProducer);
            }
            return getFontInfo(new OutlineFont(super.name, super.embeddable, characterSet), this);
        }
    }

    static final class RasterFontConfig extends AFPFontConfigData {
        private final List<RasterCharactersetData> charsets;

        private RasterFontConfig(List<FontTriplet> triplets, String type, String codePage,
                String encoding, String characterset, String name, String uri,
                List<RasterCharactersetData> csetData, boolean embeddable) {
            super(triplets, type, codePage, encoding, name, embeddable, uri);
            this.charsets = Collections.unmodifiableList(csetData);
        }

        @Override
        AFPFontInfo getFontInfo(InternalResourceResolver resourceResolver, AFPEventProducer eventProducer)
                throws IOException {
            RasterFont rasterFont = new RasterFont(super.name, super.embeddable);
            for (RasterCharactersetData charset : charsets) {
                if (charset.base14 != null) {
                    try {
                        Class<? extends Typeface> clazz = Class.forName(
                                "org.apache.fop.fonts.base14." + charset.base14).asSubclass(
                                Typeface.class);
                        try {
                            Typeface tf = clazz.newInstance();
                            rasterFont.addCharacterSet(charset.size,
                                    CharacterSetBuilder.getSingleByteInstance().build(
                                            charset.characterset, super.codePage, super.encoding,
                                            tf, eventProducer));
                        } catch (Exception ie) {
                            String msg = "The base 14 font class " + clazz.getName()
                                    + " could not be instantiated";
                            LOG.error(msg);
                        }
                    } catch (ClassNotFoundException cnfe) {
                        String msg = "The base 14 font class for " + charset.characterset
                                + " could not be found";
                        LOG.error(msg);
                    }
                } else {
                    AFPResourceAccessor accessor = getAccessor(resourceResolver);
                    rasterFont.addCharacterSet(charset.size,
                            CharacterSetBuilder.getSingleByteInstance().buildSBCS(charset.characterset,
                                    super.codePage, super.encoding, accessor, eventProducer));
                }
            }
            return getFontInfo(rasterFont, this);
        }
    }

    static final class RasterCharactersetData {
        private final String characterset;
        private final int size;
        private final String base14;

        private RasterCharactersetData(String characterset, int size, String base14) {
            this.characterset = characterset;
            this.size = size;
            this.base14 = base14;
        }
    }
}