001: /*
002: * Copyright (c) 2007, intarsys consulting GmbH
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * - Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * - 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: *
014: * - Neither the name of intarsys nor the names of its contributors may be used
015: * to endorse or promote products derived from this software without specific
016: * prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
022: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
024: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
025: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
026: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
027: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
028: * POSSIBILITY OF SUCH DAMAGE.
029: */
030: package de.intarsys.pdf.font;
031:
032: import java.io.FileOutputStream;
033: import de.intarsys.pdf.cos.COSName;
034: import de.intarsys.pdf.cos.COSObject;
035: import de.intarsys.pdf.cos.COSStream;
036:
037: /**
038: * A TrueType based CID font.
039: *
040: */
041: public class CIDFontType2 extends CIDFont {
042: /**
043: * The meta class implementation
044: */
045: static public class MetaClass extends CIDFont.MetaClass {
046: protected MetaClass(Class instanceClass) {
047: super (instanceClass);
048: }
049: }
050:
051: /** The meta class instance */
052: static public final MetaClass META = new MetaClass(MetaClass.class
053: .getDeclaringClass());
054:
055: public static final COSName DK_CIDToGIDMap = COSName
056: .constant("CIDToGIDMap");
057:
058: /**
059: * @param object
060: */
061: public CIDFontType2(COSObject object) {
062: super (object);
063: }
064:
065: /*
066: * (non-Javadoc)
067: *
068: * @see de.intarsys.pdf.font.PDFont#getNextCID(byte[], int)
069: */
070: public CID getNextCID(byte[] bytes, int offset) {
071: return new CIDSelectorCID(bytes, offset, 2);
072: }
073:
074: /*
075: * (non-Javadoc)
076: *
077: * @see de.intarsys.pdf.font.PDFont#dump()
078: */
079: protected void dump() {
080: PDFontDescriptorEmbedded fd = (PDFontDescriptorEmbedded) getFontDescriptor();
081: if (fd == null) {
082: return;
083: }
084: byte[] data = fd.getFontFile2();
085: if (data == null) {
086: return;
087: }
088: try {
089: String filename = getBaseFont().stringValue();
090: filename = filename.replace('+', '_');
091: filename = filename.replace(',', '_');
092: FileOutputStream os = new FileOutputStream("fontdump_"
093: + filename + ".ttf");
094: os.write(data);
095: os.close();
096: } catch (Exception e) {
097: // ignore and use fallback
098: }
099: }
100:
101: public COSObject cosGetCIDToGIDMap() {
102: return cosGetField(DK_CIDToGIDMap);
103: }
104:
105: private byte[] mappingTable;
106:
107: public int getGlyphIndex(CID cid) {
108: int cidValue = cid.getValue();
109: if (mappingTable == null) {
110: COSObject map = cosGetCIDToGIDMap();
111: if (map.isNull() || map instanceof COSName) {
112: return cidValue;
113: }
114: mappingTable = ((COSStream) map).getDecodedBytes();
115: }
116: int result = 0;
117: int index = cidValue * 2;
118: result = result + (mappingTable[index] & 0xff);
119: result = (result << 8) + (mappingTable[index + 1] & 0xff);
120: return result;
121: }
122: }
|