CMap.java

1
/**
2
 * Copyright (c) 2005, www.fontbox.org
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions are met:
7
 *
8
 * 1. Redistributions of source code must retain the above copyright notice,
9
 *    this list of conditions and the following disclaimer.
10
 * 2. Redistributions in binary form must reproduce the above copyright notice,
11
 *    this list of conditions and the following disclaimer in the documentation
12
 *    and/or other materials provided with the distribution.
13
 * 3. Neither the name of fontbox; nor the names of its
14
 *    contributors may be used to endorse or promote products derived from this
15
 *    software without specific prior written permission.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20
 * DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
21
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 *
28
 * http://www.fontbox.org
29
 *
30
 */
31
package com.lowagie.text.pdf.fonts.cmaps;
32
33
import java.io.IOException;
34
import com.lowagie.text.error_messages.MessageLocalization;
35
36
import java.util.ArrayList;
37
import java.util.HashMap;
38
import java.util.List;
39
import java.util.Map;
40
41
/**
42
 * This class represents a CMap file.
43
 *
44
 * @author Ben Litchfield (ben@benlitchfield.com)
45
 * @since    2.1.4
46
 */
47
public class CMap
48
{
49
    private List codeSpaceRanges = new ArrayList();
50
    private Map singleByteMappings = new HashMap();
51
    private Map doubleByteMappings = new HashMap();
52
53
    /**
54
     * Creates a new instance of CMap.
55
     */
56
    public CMap()
57
    {
58
        //default constructor
59
    }
60
    
61
    /**
62
     * This will tell if this cmap has any one byte mappings.
63
     * 
64
     * @return true If there are any one byte mappings, false otherwise.
65
     */
66
    public boolean hasOneByteMappings()
67
    {
68 2 1. hasOneByteMappings : negated conditional → NO_COVERAGE
2. hasOneByteMappings : replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE
        return !singleByteMappings.isEmpty();
69
    }
70
    
71
    /**
72
     * This will tell if this cmap has any two byte mappings.
73
     * 
74
     * @return true If there are any two byte mappings, false otherwise.
75
     */
76
    public boolean hasTwoByteMappings()
77
    {
78 2 1. hasTwoByteMappings : negated conditional → NO_COVERAGE
2. hasTwoByteMappings : replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE
        return !doubleByteMappings.isEmpty();
79
    }
80
81
    /**
82
     * This will perform a lookup into the map.
83
     * 
84
     * Some characters (e.g. ligatures) decode to character sequences.
85
     *
86
     * @param code The code used to lookup.
87
     * @return The string that matches the lookup.
88
     */
89
    public String lookup(char code)
90
    {
91
        String result = null;
92 1 1. lookup : negated conditional → NO_COVERAGE
        if (hasTwoByteMappings()) {
93
            result = (String) doubleByteMappings.get((int) code);
94
        }
95 4 1. lookup : changed conditional boundary → NO_COVERAGE
2. lookup : negated conditional → NO_COVERAGE
3. lookup : negated conditional → NO_COVERAGE
4. lookup : negated conditional → NO_COVERAGE
        if (result == null && code <= 0xff && hasOneByteMappings()) {
96 1 1. lookup : Replaced bitwise AND with OR → NO_COVERAGE
            result = (String) singleByteMappings.get(code & 0xff);
97
        }
98 1 1. lookup : mutated return of Object value for com/lowagie/text/pdf/fonts/cmaps/CMap::lookup to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
        return result;
99
    }
100
101
    /**
102
     * This will perform a lookup into the map.
103
     *
104
     * @param code The code used to lookup.
105
     * @param offset The offset into the byte array.
106
     * @param length The length of the data we are getting.
107
     *
108
     * @return The string that matches the lookup.
109
     */
110
    public String lookup( byte[] code, int offset, int length )
111
    {
112
113
        String result = null;
114
        Integer key = null;
115 1 1. lookup : negated conditional → NO_COVERAGE
        if( length == 1 )
116
        {
117
            
118 1 1. lookup : Replaced bitwise AND with OR → NO_COVERAGE
            key = code[offset] & 0xff;
119
            result = (String)singleByteMappings.get( key );
120
        }
121 1 1. lookup : negated conditional → NO_COVERAGE
        else if( length == 2 )
122
        {
123 1 1. lookup : Replaced bitwise AND with OR → NO_COVERAGE
            int intKey = code[offset] & 0xff;
124 1 1. lookup : Replaced Shift Left with Shift Right → NO_COVERAGE
            intKey <<= 8;
125 3 1. lookup : Replaced integer addition with subtraction → NO_COVERAGE
2. lookup : Replaced bitwise AND with OR → NO_COVERAGE
3. lookup : Replaced integer addition with subtraction → NO_COVERAGE
            intKey += code[offset+1] & 0xff;
126
            key = intKey;
127
128
            result = (String)doubleByteMappings.get( key );
129
        }
130
131 1 1. lookup : mutated return of Object value for com/lowagie/text/pdf/fonts/cmaps/CMap::lookup to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
        return result;
132
    }
133
134
    /**
135
     * This will add a mapping.
136
     *
137
     * @param src The src to the mapping.
138
     * @param dest The dest to the mapping.
139
     *
140
     * @throws IOException if the src is invalid.
141
     */
142
    public void addMapping( byte[] src, String dest ) throws IOException
143
    {
144 1 1. addMapping : negated conditional → NO_COVERAGE
        if( src.length == 1 )
145
        {
146 1 1. addMapping : Replaced bitwise AND with OR → NO_COVERAGE
            singleByteMappings.put(src[0] & 0xff, dest );
147
        }
148 1 1. addMapping : negated conditional → NO_COVERAGE
        else if( src.length == 2 )
149
        {
150 1 1. addMapping : Replaced bitwise AND with OR → NO_COVERAGE
            int intSrc = src[0]&0xFF;
151 1 1. addMapping : Replaced Shift Left with Shift Right → NO_COVERAGE
            intSrc <<= 8;
152 2 1. addMapping : Replaced bitwise AND with OR → NO_COVERAGE
2. addMapping : Replaced bitwise OR with AND → NO_COVERAGE
            intSrc |= (src[1]&0xFF);
153
            doubleByteMappings.put(intSrc, dest );
154
        }
155
        else
156
        {
157
            throw new IOException(MessageLocalization.getComposedMessage("mapping.code.should.be.1.or.two.bytes.and.not.1", src.length));
158
        }
159
    }
160
161
162
    /**
163
     * This will add a codespace range.
164
     *
165
     * @param range A single codespace range.
166
     */
167
    public void addCodespaceRange( CodespaceRange range )
168
    {
169
        codeSpaceRanges.add( range );
170
    }
171
172
    /**
173
     * Getter for property codeSpaceRanges.
174
     *
175
     * @return Value of property codeSpaceRanges.
176
     */
177
    public List getCodeSpaceRanges()
178
    {
179
        return codeSpaceRanges;
180
    }
181
182
}

Mutations

68

1.1
Location : hasOneByteMappings
Killed by : none
negated conditional → NO_COVERAGE

2.2
Location : hasOneByteMappings
Killed by : none
replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE

78

1.1
Location : hasTwoByteMappings
Killed by : none
negated conditional → NO_COVERAGE

2.2
Location : hasTwoByteMappings
Killed by : none
replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE

92

1.1
Location : lookup
Killed by : none
negated conditional → NO_COVERAGE

95

1.1
Location : lookup
Killed by : none
changed conditional boundary → NO_COVERAGE

2.2
Location : lookup
Killed by : none
negated conditional → NO_COVERAGE

3.3
Location : lookup
Killed by : none
negated conditional → NO_COVERAGE

4.4
Location : lookup
Killed by : none
negated conditional → NO_COVERAGE

96

1.1
Location : lookup
Killed by : none
Replaced bitwise AND with OR → NO_COVERAGE

98

1.1
Location : lookup
Killed by : none
mutated return of Object value for com/lowagie/text/pdf/fonts/cmaps/CMap::lookup to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

115

1.1
Location : lookup
Killed by : none
negated conditional → NO_COVERAGE

118

1.1
Location : lookup
Killed by : none
Replaced bitwise AND with OR → NO_COVERAGE

121

1.1
Location : lookup
Killed by : none
negated conditional → NO_COVERAGE

123

1.1
Location : lookup
Killed by : none
Replaced bitwise AND with OR → NO_COVERAGE

124

1.1
Location : lookup
Killed by : none
Replaced Shift Left with Shift Right → NO_COVERAGE

125

1.1
Location : lookup
Killed by : none
Replaced integer addition with subtraction → NO_COVERAGE

2.2
Location : lookup
Killed by : none
Replaced bitwise AND with OR → NO_COVERAGE

3.3
Location : lookup
Killed by : none
Replaced integer addition with subtraction → NO_COVERAGE

131

1.1
Location : lookup
Killed by : none
mutated return of Object value for com/lowagie/text/pdf/fonts/cmaps/CMap::lookup to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

144

1.1
Location : addMapping
Killed by : none
negated conditional → NO_COVERAGE

146

1.1
Location : addMapping
Killed by : none
Replaced bitwise AND with OR → NO_COVERAGE

148

1.1
Location : addMapping
Killed by : none
negated conditional → NO_COVERAGE

150

1.1
Location : addMapping
Killed by : none
Replaced bitwise AND with OR → NO_COVERAGE

151

1.1
Location : addMapping
Killed by : none
Replaced Shift Left with Shift Right → NO_COVERAGE

152

1.1
Location : addMapping
Killed by : none
Replaced bitwise AND with OR → NO_COVERAGE

2.2
Location : addMapping
Killed by : none
Replaced bitwise OR with AND → NO_COVERAGE

Active mutators

Tests examined


Report generated by PIT 1.4.2