FontSelector.java

1
/*
2
 * Copyright 2003 by Paulo Soares.
3
 *
4
 * The contents of this file are subject to the Mozilla Public License Version 1.1
5
 * (the "License"); you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at http://www.mozilla.org/MPL/
7
 *
8
 * Software distributed under the License is distributed on an "AS IS" basis,
9
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10
 * for the specific language governing rights and limitations under the License.
11
 *
12
 * The Original Code is 'iText, a free JAVA-PDF library'.
13
 *
14
 * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
15
 * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
16
 * All Rights Reserved.
17
 * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
18
 * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
19
 *
20
 * Contributor(s): all the names of the contributors are added in the source code
21
 * where applicable.
22
 *
23
 * Alternatively, the contents of this file may be used under the terms of the
24
 * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
25
 * provisions of LGPL are applicable instead of those above.  If you wish to
26
 * allow use of your version of this file only under the terms of the LGPL
27
 * License and not to allow others to use your version of this file under
28
 * the MPL, indicate your decision by deleting the provisions above and
29
 * replace them with the notice and other provisions required by the LGPL.
30
 * If you do not delete the provisions above, a recipient may use your version
31
 * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
32
 *
33
 * This library is free software; you can redistribute it and/or modify it
34
 * under the terms of the MPL as stated above or under the terms of the GNU
35
 * Library General Public License as published by the Free Software Foundation;
36
 * either version 2 of the License, or any later version.
37
 *
38
 * This library is distributed in the hope that it will be useful, but WITHOUT
39
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
40
 * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
41
 * details.
42
 *
43
 * If you didn't download this code from the following link, you should check if
44
 * you aren't using an obsolete version:
45
 * http://www.lowagie.com/iText/
46
 */
47
package com.lowagie.text.pdf;
48
49
import java.util.ArrayList;
50
import com.lowagie.text.error_messages.MessageLocalization;
51
52
import com.lowagie.text.Chunk;
53
import com.lowagie.text.Font;
54
import com.lowagie.text.Phrase;
55
import com.lowagie.text.Utilities;
56
57
/** Selects the appropriate fonts that contain the glyphs needed to
58
 * render text correctly. The fonts are checked in order until the 
59
 * character is found.
60
 * <p>
61
 * The built in fonts "Symbol" and "ZapfDingbats", if used, have a special encoding
62
 * to allow the characters to be referred by Unicode.
63
 * @author Paulo Soares (psoares@consiste.pt)
64
 */
65
public class FontSelector {
66
    
67
    protected ArrayList fonts = new ArrayList();
68
69
    /**
70
     * Adds a <CODE>Font</CODE> to be searched for valid characters.
71
     * @param font the <CODE>Font</CODE>
72
     */    
73
    public void addFont(Font font) {
74 1 1. addFont : negated conditional → NO_COVERAGE
        if (font.getBaseFont() != null) {
75
            fonts.add(font);
76
            return;
77
        }
78
        BaseFont bf = font.getCalculatedBaseFont(true);
79
        Font f2 = new Font(bf, font.getSize(), font.getCalculatedStyle(), font.getColor());
80
        fonts.add(f2);
81
    }
82
    
83
    /**
84
     * Process the text so that it will render with a combination of fonts
85
     * if needed.
86
     * @param text the text
87
     * @return a <CODE>Phrase</CODE> with one or more chunks
88
     */    
89
    public Phrase process(String text) {
90
        int fsize = fonts.size();
91 1 1. process : negated conditional → NO_COVERAGE
        if (fsize == 0)
92
            throw new IndexOutOfBoundsException(MessageLocalization.getComposedMessage("no.font.is.defined"));
93
        char[] cc = text.toCharArray();
94
        int len = cc.length;
95
        StringBuilder sb = new StringBuilder();
96
        Font font = null;
97
        int lastidx = -1;
98
        Phrase ret = new Phrase();
99 3 1. process : changed conditional boundary → NO_COVERAGE
2. process : Changed increment from 1 to -1 → NO_COVERAGE
3. process : negated conditional → NO_COVERAGE
        for (int k = 0; k < len; ++k) {
100
            char c = cc[k];
101 2 1. process : negated conditional → NO_COVERAGE
2. process : negated conditional → NO_COVERAGE
            if (c == '\n' || c == '\r') {
102
                sb.append(c);
103
                continue;
104
            }
105 1 1. process : negated conditional → NO_COVERAGE
            if (Utilities.isSurrogatePair(cc, k)) {
106
                int u = Utilities.convertToUtf32(cc, k);
107 3 1. process : changed conditional boundary → NO_COVERAGE
2. process : Changed increment from 1 to -1 → NO_COVERAGE
3. process : negated conditional → NO_COVERAGE
                for (int f = 0; f < fsize; ++f) {
108
                    font = (Font)fonts.get(f);
109 1 1. process : negated conditional → NO_COVERAGE
                    if (font.getBaseFont().charExists(u)) {
110 1 1. process : negated conditional → NO_COVERAGE
                        if (lastidx != f) {
111 3 1. process : changed conditional boundary → NO_COVERAGE
2. process : negated conditional → NO_COVERAGE
3. process : negated conditional → NO_COVERAGE
                            if (sb.length() > 0 && lastidx != -1) {
112
                                Chunk ck = new Chunk(sb.toString(), (Font)fonts.get(lastidx));
113
                                ret.add(ck);
114 1 1. process : removed call to java/lang/StringBuilder::setLength → NO_COVERAGE
                                sb.setLength(0);
115
                            }
116
                            lastidx = f;
117
                        }
118
                        sb.append(c);
119 1 1. process : Changed increment from 1 to -1 → NO_COVERAGE
                        sb.append(cc[++k]);
120
                        break;
121
                    }
122
                }
123
            }
124
            else {
125 3 1. process : changed conditional boundary → NO_COVERAGE
2. process : Changed increment from 1 to -1 → NO_COVERAGE
3. process : negated conditional → NO_COVERAGE
                for (int f = 0; f < fsize; ++f) {
126
                    font = (Font)fonts.get(f);
127 1 1. process : negated conditional → NO_COVERAGE
                    if (font.getBaseFont().charExists(c)) {
128 1 1. process : negated conditional → NO_COVERAGE
                        if (lastidx != f) {
129 3 1. process : changed conditional boundary → NO_COVERAGE
2. process : negated conditional → NO_COVERAGE
3. process : negated conditional → NO_COVERAGE
                            if (sb.length() > 0 && lastidx != -1) {
130
                                Chunk ck = new Chunk(sb.toString(), (Font)fonts.get(lastidx));
131
                                ret.add(ck);
132 1 1. process : removed call to java/lang/StringBuilder::setLength → NO_COVERAGE
                                sb.setLength(0);
133
                            }
134
                            lastidx = f;
135
                        }
136
                        sb.append(c);
137
                        break;
138
                    }
139
                }
140
            }
141
        }
142 2 1. process : changed conditional boundary → NO_COVERAGE
2. process : negated conditional → NO_COVERAGE
        if (sb.length() > 0) {
143 1 1. process : negated conditional → NO_COVERAGE
            Chunk ck = new Chunk(sb.toString(), (Font)fonts.get(lastidx == -1 ? 0 : lastidx));
144
            ret.add(ck);
145
        }
146 1 1. process : mutated return of Object value for com/lowagie/text/pdf/FontSelector::process to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
        return ret;
147
    }
148
}

Mutations

74

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

91

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

99

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

2.2
Location : process
Killed by : none
Changed increment from 1 to -1 → NO_COVERAGE

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

101

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

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

105

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

107

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

2.2
Location : process
Killed by : none
Changed increment from 1 to -1 → NO_COVERAGE

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

109

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

110

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

111

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

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

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

114

1.1
Location : process
Killed by : none
removed call to java/lang/StringBuilder::setLength → NO_COVERAGE

119

1.1
Location : process
Killed by : none
Changed increment from 1 to -1 → NO_COVERAGE

125

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

2.2
Location : process
Killed by : none
Changed increment from 1 to -1 → NO_COVERAGE

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

127

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

128

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

129

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

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

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

132

1.1
Location : process
Killed by : none
removed call to java/lang/StringBuilder::setLength → NO_COVERAGE

142

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

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

143

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

146

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

Active mutators

Tests examined


Report generated by PIT 1.4.2