DefaultFontMapper.java

1
/*
2
 * Copyright 2004 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 com.lowagie.text.ExceptionConverter;
50
51
import java.awt.Font;
52
import java.io.File;
53
import java.util.HashMap;
54
55
56
/**
57
 * Default class to map awt fonts to BaseFont.
58
 *
59
 * @author Paulo Soares (psoares@consiste.pt)
60
 */
61
62
public class DefaultFontMapper implements FontMapper {
63
64
  /**
65
   * A representation of BaseFont parameters.
66
   */
67
  public static class BaseFontParameters {
68
69
    /**
70
     * The font name.
71
     */
72
    public String fontName;
73
    /**
74
     * The encoding for that font.
75
     */
76
    public String encoding;
77
    /**
78
     * The embedding for that font.
79
     */
80
    public boolean embedded;
81
    /**
82
     * Whether the font is cached of not.
83
     */
84
    public boolean cached;
85
    /**
86
     * The font bytes for ttf and afm.
87
     */
88
    public byte[] ttfAfm;
89
    /**
90
     * The font bytes for pfb.
91
     */
92
    public byte[] pfb;
93
94
    /**
95
     * Constructs default BaseFont parameters.
96
     *
97
     * @param fontName the font name or location
98
     */
99
    public BaseFontParameters(String fontName) {
100
      this.fontName = fontName;
101
      encoding = BaseFont.CP1252;
102
      embedded = BaseFont.EMBEDDED;
103
      cached = BaseFont.CACHED;
104
    }
105
  }
106
107
  /**
108
   * Maps aliases to names.
109
   */
110
  private HashMap<String, String> aliases = new HashMap<>();
111
  /**
112
   * Maps names to BaseFont parameters.
113
   */
114
  private HashMap<String, BaseFontParameters> mapper = new HashMap<>();
115
116
  /**
117
   * Returns a BaseFont which can be used to represent the given AWT Font
118
   *
119
   * @param  font    the font to be converted
120
   * @return a BaseFont which has similar properties to the provided Font
121
   */
122
123
  public BaseFont awtToPdf(Font font) {
124
    try {
125
      BaseFontParameters p = getBaseFontParameters(font.getFontName());
126 1 1. awtToPdf : negated conditional → NO_COVERAGE
      if (p != null) {
127 1 1. awtToPdf : mutated return of Object value for com/lowagie/text/pdf/DefaultFontMapper::awtToPdf to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
        return BaseFont.createFont(p.fontName, p.encoding, p.embedded, p.cached, p.ttfAfm, p.pfb);
128
      }
129
      final String fontKey;
130
      final String logicalName = font.getName();
131
132 2 1. awtToPdf : negated conditional → NO_COVERAGE
2. awtToPdf : negated conditional → NO_COVERAGE
      if (logicalName.equalsIgnoreCase("DialogInput") || logicalName.equalsIgnoreCase("Monospaced") || logicalName
133 1 1. awtToPdf : negated conditional → NO_COVERAGE
          .equalsIgnoreCase("Courier")) {
134
135 1 1. awtToPdf : negated conditional → NO_COVERAGE
        if (font.isItalic()) {
136 1 1. awtToPdf : negated conditional → NO_COVERAGE
          if (font.isBold()) {
137
            fontKey = BaseFont.COURIER_BOLDOBLIQUE;
138
139
          } else {
140
            fontKey = BaseFont.COURIER_OBLIQUE;
141
          }
142
143
        } else {
144 1 1. awtToPdf : negated conditional → NO_COVERAGE
          if (font.isBold()) {
145
            fontKey = BaseFont.COURIER_BOLD;
146
147
          } else {
148
            fontKey = BaseFont.COURIER;
149
          }
150
        }
151
152 2 1. awtToPdf : negated conditional → NO_COVERAGE
2. awtToPdf : negated conditional → NO_COVERAGE
      } else if (logicalName.equalsIgnoreCase("Serif") || logicalName.equalsIgnoreCase("TimesRoman")) {
153
154 1 1. awtToPdf : negated conditional → NO_COVERAGE
        if (font.isItalic()) {
155 1 1. awtToPdf : negated conditional → NO_COVERAGE
          if (font.isBold()) {
156
            fontKey = BaseFont.TIMES_BOLDITALIC;
157
158
          } else {
159
            fontKey = BaseFont.TIMES_ITALIC;
160
          }
161
162
        } else {
163 1 1. awtToPdf : negated conditional → NO_COVERAGE
          if (font.isBold()) {
164
            fontKey = BaseFont.TIMES_BOLD;
165
166
          } else {
167
            fontKey = BaseFont.TIMES_ROMAN;
168
          }
169
        }
170
171
      } else {  // default, this catches Dialog and SansSerif
172
173 1 1. awtToPdf : negated conditional → NO_COVERAGE
        if (font.isItalic()) {
174 1 1. awtToPdf : negated conditional → NO_COVERAGE
          if (font.isBold()) {
175
            fontKey = BaseFont.HELVETICA_BOLDOBLIQUE;
176
177
          } else {
178
            fontKey = BaseFont.HELVETICA_OBLIQUE;
179
          }
180
181
        } else {
182 1 1. awtToPdf : negated conditional → NO_COVERAGE
          if (font.isBold()) {
183
            fontKey = BaseFont.HELVETICA_BOLD;
184
          } else {
185
            fontKey = BaseFont.HELVETICA;
186
          }
187
        }
188
      }
189 1 1. awtToPdf : mutated return of Object value for com/lowagie/text/pdf/DefaultFontMapper::awtToPdf to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
      return BaseFont.createFont(fontKey, BaseFont.CP1252, false);
190
    } catch (Exception e) {
191
      throw new ExceptionConverter(e);
192
    }
193
  }
194
195
  /**
196
   * Returns an AWT Font which can be used to represent the given BaseFont
197
   *
198
   * @param  font    the font to be converted
199
   * @param  size    the desired point size of the resulting font
200
   * @return a Font which has similar properties to the provided BaseFont
201
   */
202
203
  public Font pdfToAwt(BaseFont font, int size) {
204
    String[][] names = font.getFullFontName();
205 1 1. pdfToAwt : negated conditional → NO_COVERAGE
    if (names.length == 1) {
206 1 1. pdfToAwt : mutated return of Object value for com/lowagie/text/pdf/DefaultFontMapper::pdfToAwt to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
      return new Font(names[0][3], Font.PLAIN, size);
207
    }
208
    String name10 = null;
209
    String name3x = null;
210
    for (String[] name : names) {
211 2 1. pdfToAwt : negated conditional → NO_COVERAGE
2. pdfToAwt : negated conditional → NO_COVERAGE
      if (name[0].equals("1") && name[1].equals("0")) {
212
        name10 = name[3];
213 1 1. pdfToAwt : negated conditional → NO_COVERAGE
      } else if (name[2].equals("1033")) {
214
        name3x = name[3];
215
        break;
216
      }
217
    }
218
    String finalName = name3x;
219 1 1. pdfToAwt : negated conditional → NO_COVERAGE
    if (finalName == null) {
220
      finalName = name10;
221
    }
222 1 1. pdfToAwt : negated conditional → NO_COVERAGE
    if (finalName == null) {
223
      finalName = names[0][3];
224
    }
225 1 1. pdfToAwt : mutated return of Object value for com/lowagie/text/pdf/DefaultFontMapper::pdfToAwt to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
    return new Font(finalName, Font.PLAIN, size);
226
  }
227
228
  /**
229
   * Maps a name to a BaseFont parameter.
230
   *
231
   * @param awtName the name
232
   * @param parameters the BaseFont parameter
233
   */
234
  public void putName(String awtName, BaseFontParameters parameters) {
235
    mapper.put(awtName, parameters);
236
  }
237
238
  /**
239
   * Maps an alias to a name.
240
   *
241
   * @param alias the alias
242
   * @param awtName the name
243
   */
244
  public void putAlias(String alias, String awtName) {
245
    aliases.put(alias, awtName);
246
  }
247
248
  /**
249
   * Looks for a BaseFont parameter associated with a name.
250
   *
251
   * @param name the name
252
   * @return the BaseFont parameter or <CODE>null</CODE> if not found.
253
   */
254
  public BaseFontParameters getBaseFontParameters(String name) {
255
    String alias = aliases.get(name);
256 1 1. getBaseFontParameters : negated conditional → NO_COVERAGE
    if (alias == null) {
257 1 1. getBaseFontParameters : mutated return of Object value for com/lowagie/text/pdf/DefaultFontMapper::getBaseFontParameters to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
      return mapper.get(name);
258
    }
259
    BaseFontParameters p = mapper.get(alias);
260 1 1. getBaseFontParameters : negated conditional → NO_COVERAGE
    if (p == null) {
261 1 1. getBaseFontParameters : mutated return of Object value for com/lowagie/text/pdf/DefaultFontMapper::getBaseFontParameters to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
      return mapper.get(name);
262
    } else {
263 1 1. getBaseFontParameters : mutated return of Object value for com/lowagie/text/pdf/DefaultFontMapper::getBaseFontParameters to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
      return p;
264
    }
265
  }
266
267
  /**
268
   * Inserts the names in this map.
269
   *
270
   * @param allNames the returned value of calling {@link BaseFont#getAllFontNames(String, String, byte[])}
271
   * @param path the full path to the font
272
   */
273
  public void insertNames(Object[] allNames, String path) {
274
    String[][] names = (String[][]) allNames[2];
275
    String main = null;
276
    for (String[] name : names) {
277 1 1. insertNames : negated conditional → NO_COVERAGE
      if (name[2].equals("1033")) {
278
        main = name[3];
279
        break;
280
      }
281
    }
282 1 1. insertNames : negated conditional → NO_COVERAGE
    if (main == null) {
283
      main = names[0][3];
284
    }
285
    BaseFontParameters p = new BaseFontParameters(path);
286
    mapper.put(main, p);
287
      for (String[] name : names) {
288
          aliases.put(name[3], main);
289
      }
290
    aliases.put((String) allNames[0], main);
291
  }
292
293
  /**
294
   * Inserts all the fonts recognized by iText in the
295
   * <CODE>directory</CODE> into the map. The encoding
296
   * will be <CODE>BaseFont.CP1252</CODE> but can be changed later.
297
   *
298
   * @param dir the directory to scan
299
   * @return the number of files processed
300
   */
301
  public int insertDirectory(String dir) {
302
    File file = new File(dir);
303 2 1. insertDirectory : negated conditional → NO_COVERAGE
2. insertDirectory : negated conditional → NO_COVERAGE
    if (!file.exists() || !file.isDirectory()) {
304 1 1. insertDirectory : replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE
      return 0;
305
    }
306
    File[] files = file.listFiles();
307 1 1. insertDirectory : negated conditional → NO_COVERAGE
    if (files == null) {
308 1 1. insertDirectory : replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE
      return 0;
309
    }
310
    int count = 0;
311
    for (File file1 : files) {
312
      file = file1;
313
      String name = file.getPath().toLowerCase();
314
      try {
315 3 1. insertDirectory : negated conditional → NO_COVERAGE
2. insertDirectory : negated conditional → NO_COVERAGE
3. insertDirectory : negated conditional → NO_COVERAGE
        if (name.endsWith(".ttf") || name.endsWith(".otf") || name.endsWith(".afm")) {
316
          Object[] allNames = BaseFont.getAllFontNames(file.getPath(), BaseFont.CP1252, null);
317 1 1. insertDirectory : removed call to com/lowagie/text/pdf/DefaultFontMapper::insertNames → NO_COVERAGE
          insertNames(allNames, file.getPath());
318 1 1. insertDirectory : Changed increment from 1 to -1 → NO_COVERAGE
          ++count;
319 1 1. insertDirectory : negated conditional → NO_COVERAGE
        } else if (name.endsWith(".ttc")) {
320
          String[] ttcs = BaseFont.enumerateTTCNames(file.getPath());
321 2 1. insertDirectory : changed conditional boundary → NO_COVERAGE
2. insertDirectory : negated conditional → NO_COVERAGE
          for (int j = 0; j < ttcs.length; ++j) {
322
            String nt = file.getPath() + "," + j;
323
            Object[] allNames = BaseFont.getAllFontNames(nt, BaseFont.CP1252, null);
324 1 1. insertDirectory : removed call to com/lowagie/text/pdf/DefaultFontMapper::insertNames → NO_COVERAGE
            insertNames(allNames, nt);
325
          }
326 1 1. insertDirectory : Changed increment from 1 to -1 → NO_COVERAGE
          ++count;
327
        }
328
      } catch (Exception e) {
329
      }
330
    }
331 1 1. insertDirectory : replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE
    return count;
332
  }
333
334
  public HashMap<String, BaseFontParameters> getMapper() {
335
    return mapper;
336
  }
337
338
  public HashMap<String, String> getAliases() {
339
    return aliases;
340
  }
341
}

Mutations

126

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

127

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

132

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

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

133

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

135

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

136

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

144

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

152

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

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

154

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

155

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

163

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

173

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

174

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

182

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

189

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

205

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

206

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

211

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

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

213

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

219

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

222

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

225

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

256

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

257

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

260

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

261

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

263

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

277

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

282

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

303

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

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

304

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

307

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

308

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

315

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

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

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

317

1.1
Location : insertDirectory
Killed by : none
removed call to com/lowagie/text/pdf/DefaultFontMapper::insertNames → NO_COVERAGE

318

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

319

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

321

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

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

324

1.1
Location : insertDirectory
Killed by : none
removed call to com/lowagie/text/pdf/DefaultFontMapper::insertNames → NO_COVERAGE

326

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

331

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

Active mutators

Tests examined


Report generated by PIT 1.4.2