001: /**
002: * Copyright (c) 2003, www.pdfbox.org
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions are met:
007: *
008: * 1. Redistributions of source code must retain the above copyright notice,
009: * this list of conditions and the following disclaimer.
010: * 2. Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: * 3. Neither the name of pdfbox; nor the names of its
014: * contributors may be used to endorse or promote products derived from this
015: * software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
018: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
019: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
020: * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
021: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
022: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
023: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
024: * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
026: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: *
028: * http://www.pdfbox.org
029: *
030: */package org.pdfbox.pdmodel.font;
031:
032: import java.io.IOException;
033: import java.util.Map;
034:
035: import org.pdfbox.cos.COSDictionary;
036: import org.pdfbox.cos.COSName;
037:
038: /**
039: * This will create the correct type of font based on information in the dictionary.
040: *
041: * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
042: * @version $Revision: 1.6 $
043: */
044: public class PDFontFactory {
045: /**
046: * private constructor, should only use static methods in this class.
047: */
048: private PDFontFactory() {
049: }
050:
051: /**
052: * Create a font from the dictionary. Use the fontCache to get the existing
053: * object instead of creating it.
054: *
055: * @param dic The font dictionary.
056: * @param fontCache The font cache.
057: * @return The PDModel object for the cos dictionary.
058: * @throws IOException If there is an error creating the font.
059: */
060: public static PDFont createFont(COSDictionary dic, Map fontCache)
061: throws IOException {
062: PDFont font = null;
063: if (fontCache != null) {
064: font = (PDFont) fontCache.get(dic);
065: }
066: if (font == null) {
067: font = createFont(dic);
068: if (fontCache != null) {
069: fontCache.put(dic, font);
070: }
071: }
072: return font;
073: }
074:
075: /**
076: * This will create the correct font based on information in the dictionary.
077: *
078: * @param dic The populated dictionary.
079: *
080: * @return The corrent implementation for the font.
081: *
082: * @throws IOException If the dictionary is not valid.
083: */
084: public static PDFont createFont(COSDictionary dic)
085: throws IOException {
086: PDFont retval = null;
087:
088: COSName type = (COSName) dic.getDictionaryObject(COSName.TYPE);
089: if (!type.equals(COSName.FONT)) {
090: throw new IOException(
091: "Cannot create font if /Type is not /Font. Actual="
092: + type);
093: }
094:
095: COSName subType = (COSName) dic
096: .getDictionaryObject(COSName.SUBTYPE);
097: if (subType.equals(COSName.getPDFName("Type1"))) {
098: retval = new PDType1Font(dic);
099: } else if (subType.equals(COSName.getPDFName("MMType1"))) {
100: retval = new PDMMType1Font(dic);
101: } else if (subType.equals(COSName.getPDFName("TrueType"))) {
102: retval = new PDTrueTypeFont(dic);
103: } else if (subType.equals(COSName.getPDFName("Type3"))) {
104: retval = new PDType3Font(dic);
105: } else if (subType.equals(COSName.getPDFName("Type0"))) {
106: retval = new PDType0Font(dic);
107: } else if (subType.equals(COSName.getPDFName("CIDFontType0"))) {
108: retval = new PDCIDFontType0Font(dic);
109: } else if (subType.equals(COSName.getPDFName("CIDFontType2"))) {
110: retval = new PDCIDFontType2Font(dic);
111: } else {
112: throw new IOException("Unknown font subtype=" + subType);
113: }
114:
115: return retval;
116: }
117: }
|