1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 /* $Id: AFPBase12FontCollection.java 1356646 2012-07-03 09:46:41Z mehdi $ */
19 
20 package org.apache.fop.afp.fonts;
21 
22 import org.apache.fop.afp.AFPEventProducer;
23 import org.apache.fop.fonts.Base14Font;
24 import org.apache.fop.fonts.Font;
25 import org.apache.fop.fonts.FontCollection;
26 import org.apache.fop.fonts.FontInfo;
27 import org.apache.fop.fonts.base14.Courier;
28 import org.apache.fop.fonts.base14.CourierBold;
29 import org.apache.fop.fonts.base14.CourierBoldOblique;
30 import org.apache.fop.fonts.base14.CourierOblique;
31 import org.apache.fop.fonts.base14.Helvetica;
32 import org.apache.fop.fonts.base14.HelveticaBold;
33 import org.apache.fop.fonts.base14.HelveticaOblique;
34 import org.apache.fop.fonts.base14.TimesBold;
35 import org.apache.fop.fonts.base14.TimesBoldItalic;
36 import org.apache.fop.fonts.base14.TimesItalic;
37 import org.apache.fop.fonts.base14.TimesRoman;
38 
39 /**
40  * Sets up a typical Base 12 font configuration for AFP
41  */
42 public class AFPBase12FontCollection implements FontCollection {
43 
44     private final AFPEventProducer eventProducer;
45 
46     /**
47      * @param eventProducer the AFP-specific event producer
48      */
AFPBase12FontCollection(AFPEventProducer eventProducer)49     public AFPBase12FontCollection(AFPEventProducer eventProducer) {
50         this.eventProducer = eventProducer;
51     }
52 
53     /** standard raster font sizes */
54     private static final int[] RASTER_SIZES = {6, 7, 8, 9, 10, 11, 12, 14, 16, 18, 20, 24, 30, 36};
55 
56     /** standard raster font charset references */
57     private static final String[] CHARSET_REF = {
58         "60", "70", "80", "90", "00", "A0", "B0", "D0", "F0", "H0", "J0", "N0", "T0", "Z0"};
59 
addCharacterSet(RasterFont font, String charsetName, Base14Font base14)60     private void addCharacterSet(RasterFont font, String charsetName, Base14Font base14) {
61         for (int i = 0; i < RASTER_SIZES.length; i++) {
62             int size = RASTER_SIZES[i] * 1000;
63             FopCharacterSet characterSet = new FopCharacterSet(
64                     CharacterSet.DEFAULT_CODEPAGE, CharacterSet.DEFAULT_ENCODING,
65                     charsetName + CHARSET_REF[i], base14, eventProducer);
66             font.addCharacterSet(size, characterSet);
67         }
68     }
69 
addFontProperties(FontInfo fontInfo, AFPFont font, String[] names, String style, int weight, int num)70     private int addFontProperties(FontInfo fontInfo, AFPFont font,
71             String[] names, String style, int weight, int num) {
72         String internalFontKey = "F" + num;
73         fontInfo.addMetrics(internalFontKey, font);
74         fontInfo.addFontProperties(internalFontKey, names, style, weight);
75         num++;
76         return num;
77     }
78 
79     /** {@inheritDoc} */
setup(int start, FontInfo fontInfo)80     public int setup(int start, FontInfo fontInfo) {
81 
82         /**
83          * Add the base 12 fonts (Helvetica, Times and Courier)
84          *
85          * Note: this default font configuration may not be available
86          * on your AFP environment.
87          */
88         int num = start;
89         RasterFont font = null;
90 
91         /** standard font family reference names for Helvetica font */
92         final String[] helveticaNames = {"Helvetica", "Arial", "sans-serif"};
93         font = createReferencedRasterFont("Helvetica");
94         addCharacterSet(font, "C0H200", new Helvetica());
95         num = addFontProperties(fontInfo, font, helveticaNames,
96                 Font.STYLE_NORMAL, Font.WEIGHT_NORMAL, num);
97 
98         font = createReferencedRasterFont("Helvetica Italic");
99         addCharacterSet(font, "C0H300", new HelveticaOblique());
100         num = addFontProperties(fontInfo, font, helveticaNames,
101                 Font.STYLE_ITALIC, Font.WEIGHT_NORMAL, num);
102 
103         font = createReferencedRasterFont("Helvetica (Semi) Bold");
104         addCharacterSet(font, "C0H400", new HelveticaBold());
105         num = addFontProperties(fontInfo, font, helveticaNames,
106                 Font.STYLE_NORMAL, Font.WEIGHT_BOLD, num);
107 
108         font = createReferencedRasterFont("Helvetica Italic (Semi) Bold");
109         addCharacterSet(font, "C0H500", new HelveticaOblique());
110         num = addFontProperties(fontInfo, font, helveticaNames,
111                 Font.STYLE_ITALIC, Font.WEIGHT_BOLD, num);
112 
113 
114         /** standard font family reference names for Times font */
115 
116         /** any is treated as serif */
117         final String[] timesNames = {"Times", "TimesRoman", "Times Roman", "Times-Roman",
118                 "Times New Roman", "TimesNewRoman", "serif", "any"};
119 
120         font = createReferencedRasterFont("Times Roman");
121         addCharacterSet(font, "C0N200", new TimesRoman());
122         num = addFontProperties(fontInfo, font, timesNames,
123                 Font.STYLE_NORMAL, Font.WEIGHT_NORMAL, num);
124 
125         font = createReferencedRasterFont("Times Roman Italic");
126         addCharacterSet(font, "C0N300", new TimesItalic());
127         num = addFontProperties(fontInfo, font, timesNames,
128                 Font.STYLE_ITALIC, Font.WEIGHT_NORMAL, num);
129 
130         font = createReferencedRasterFont("Times Roman Bold");
131         addCharacterSet(font, "C0N400", new TimesBold());
132         num = addFontProperties(fontInfo, font, timesNames,
133                 Font.STYLE_NORMAL, Font.WEIGHT_BOLD, num);
134 
135         font = createReferencedRasterFont("Times Roman Italic Bold");
136         addCharacterSet(font, "C0N500", new TimesBoldItalic());
137         num = addFontProperties(fontInfo, font, timesNames,
138                 Font.STYLE_ITALIC, Font.WEIGHT_BOLD, num);
139 
140 
141         /** standard font family reference names for Courier font */
142         final String[] courierNames = {"Courier", "monospace"};
143 
144         font = createReferencedRasterFont("Courier");
145         addCharacterSet(font, "C04200", new Courier());
146         num = addFontProperties(fontInfo, font, courierNames,
147                 Font.STYLE_NORMAL, Font.WEIGHT_NORMAL, num);
148 
149         font = createReferencedRasterFont("Courier Italic");
150         addCharacterSet(font, "C04300", new CourierOblique());
151         num = addFontProperties(fontInfo, font, courierNames,
152                 Font.STYLE_ITALIC, Font.WEIGHT_NORMAL, num);
153 
154         font = createReferencedRasterFont("Courier Bold");
155         addCharacterSet(font, "C04400", new CourierBold());
156         num = addFontProperties(fontInfo, font, courierNames,
157                 Font.STYLE_NORMAL, Font.WEIGHT_BOLD, num);
158 
159         font = createReferencedRasterFont("Courier Italic Bold");
160         addCharacterSet(font, "C04500", new CourierBoldOblique());
161         num = addFontProperties(fontInfo, font, courierNames,
162                 Font.STYLE_ITALIC, Font.WEIGHT_BOLD, num);
163 
164         return num;
165     }
166 
createReferencedRasterFont(String fontFamily)167     private RasterFont createReferencedRasterFont(String fontFamily) {
168         boolean embeddable = false; //Font is assumed to be available on the target platform
169         return new RasterFont(fontFamily, embeddable);
170     }
171 
172 }
173