001: /*
002: * $Id: PDFCMap.java,v 1.2 2007/12/20 18:33:31 rbair Exp $
003: *
004: * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
005: * Santa Clara, California 95054, U.S.A. All rights reserved.
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
020: */
021: package com.sun.pdfview.font;
022:
023: import java.io.IOException;
024: import java.util.HashMap;
025:
026: import com.sun.pdfview.PDFObject;
027:
028: /**
029: * A CMap maps from a character in a composite font to a font/glyph number
030: * pair in a CID font.
031: *
032: * @author jkaplan
033: */
034: public abstract class PDFCMap {
035: /**
036: * A cache of known CMaps by name
037: */
038: private static HashMap cache;
039:
040: /** Creates a new instance of CMap */
041: protected PDFCMap() {
042: }
043:
044: /**
045: * Get a CMap, given a PDF object containing one of the following:
046: * a string name of a known CMap
047: * a stream containing a CMap definition
048: */
049: public static PDFCMap getCMap(PDFObject map) throws IOException {
050: if (map.getType() == PDFObject.NAME) {
051: return getCMap(map.getStringValue());
052: } else if (map.getType() == PDFObject.STREAM) {
053: return parseCMap(map);
054: } else {
055: throw new IOException("CMap type not Name or Stream!");
056: }
057: }
058:
059: /**
060: * Get a CMap, given a string name
061: */
062: public static PDFCMap getCMap(String mapName) throws IOException {
063: if (cache == null) {
064: populateCache();
065: }
066:
067: if (!cache.containsKey(mapName)) {
068: throw new IOException("Unknown CMap: " + mapName);
069: }
070:
071: return (PDFCMap) cache.get(mapName);
072: }
073:
074: /**
075: * Populate the cache with well-known types
076: */
077: protected static void populateCache() {
078: cache = new HashMap();
079:
080: // add the Identity-H map
081: cache.put("Identity-H", new PDFCMap() {
082: public char map(char src) {
083: return src;
084: }
085: });
086: }
087:
088: /**
089: * Parse a CMap from a CMap stream
090: */
091: protected static PDFCMap parseCMap(PDFObject map)
092: throws IOException {
093: throw new IOException("Parsing CMap Files Unsupported!");
094: }
095:
096: /**
097: * Map a given source character to a destination character
098: */
099: public abstract char map(char src);
100:
101: /**
102: * Get the font number assoicated with a given source character
103: */
104: public int getFontID(char src) {
105: return 0;
106: }
107:
108: }
|