001/**
002 * Copyright (c) 2005, www.fontbox.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 fontbox; 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.fontbox.org
029 *
030 */
031package com.itextpdf.text.pdf.fonts.cmaps;
032
033import java.io.IOException;
034import java.util.ArrayList;
035import java.util.HashMap;
036import java.util.List;
037import java.util.Map;
038
039import com.itextpdf.text.error_messages.MessageLocalization;
040
041/**
042 * This class represents a CMap file.
043 *
044 * @author Ben Litchfield (ben@benlitchfield.com)
045 * @since       2.1.4
046 */
047public class CMap
048{
049    private List<CodespaceRange> codeSpaceRanges = new ArrayList<CodespaceRange>();
050    private Map<Integer, String> singleByteMappings = new HashMap<Integer, String>();
051    private Map<Integer, String> doubleByteMappings = new HashMap<Integer, String>();
052
053    /**
054     * Creates a new instance of CMap.
055     */
056    public CMap()
057    {
058        //default constructor
059    }
060
061    /**
062     * This will tell if this cmap has any one byte mappings.
063     *
064     * @return true If there are any one byte mappings, false otherwise.
065     */
066    public boolean hasOneByteMappings()
067    {
068        return !singleByteMappings.isEmpty();
069    }
070
071    /**
072     * This will tell if this cmap has any two byte mappings.
073     *
074     * @return true If there are any two byte mappings, false otherwise.
075     */
076    public boolean hasTwoByteMappings()
077    {
078        return !doubleByteMappings.isEmpty();
079    }
080
081    /**
082     * This will perform a lookup into the map.
083     *
084     * @param code The code used to lookup.
085     * @param offset The offset into the byte array.
086     * @param length The length of the data we are getting.
087     *
088     * @return The string that matches the lookup.
089     */
090    public String lookup( byte[] code, int offset, int length )
091    {
092
093        String result = null;
094        Integer key = null;
095        if( length == 1 )
096        {
097
098            key = Integer.valueOf( code[offset] & 0xff );
099            result = singleByteMappings.get( key );
100        }
101        else if( length == 2 )
102        {
103            int intKey = code[offset] & 0xff;
104            intKey <<= 8;
105            intKey += code[offset+1] & 0xff;
106            key = Integer.valueOf( intKey );
107
108            result = doubleByteMappings.get( key );
109        }
110
111        return result;
112    }
113
114    /**
115     * This will add a mapping.
116     *
117     * @param src The src to the mapping.
118     * @param dest The dest to the mapping.
119     *
120     * @throws IOException if the src is invalid.
121     */
122    public void addMapping( byte[] src, String dest ) throws IOException
123    {
124        if( src.length == 1 )
125        {
126            singleByteMappings.put( Integer.valueOf( src[0] & 0xff ), dest );
127        }
128        else if( src.length == 2 )
129        {
130            int intSrc = src[0]&0xFF;
131            intSrc <<= 8;
132            intSrc |= src[1]&0xFF;
133            doubleByteMappings.put( Integer.valueOf( intSrc), dest );
134        }
135        else
136        {
137            throw new IOException(MessageLocalization.getComposedMessage("mapping.code.should.be.1.or.two.bytes.and.not.1", src.length));
138        }
139    }
140
141
142    /**
143     * This will add a codespace range.
144     *
145     * @param range A single codespace range.
146     */
147    public void addCodespaceRange( CodespaceRange range )
148    {
149        codeSpaceRanges.add( range );
150    }
151
152    /**
153     * Getter for property codeSpaceRanges.
154     *
155     * @return Value of property codeSpaceRanges.
156     */
157    public List<CodespaceRange> getCodeSpaceRanges()
158    {
159        return codeSpaceRanges;
160    }
161
162}