| 1 |
/* ===================================================================== |
| 2 |
* JFreePDF : a fast, light-weight PDF library for the Java(tm) platform |
| 3 |
* ===================================================================== |
| 4 |
* |
| 5 |
* (C)opyright 2013-2022, by David Gilbert. All rights reserved. |
| 6 |
* |
| 7 |
* https://github.com/jfree/orsonpdf |
| 8 |
* |
| 9 |
* This program is free software: you can redistribute it and/or modify |
| 10 |
* it under the terms of the GNU General Public License as published by |
| 11 |
* the Free Software Foundation, either version 3 of the License, or |
| 12 |
* (at your option) any later version. |
| 13 |
* |
| 14 |
* This program is distributed in the hope that it will be useful, |
| 15 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 |
* GNU General Public License for more details. |
| 18 |
* |
| 19 |
* You should have received a copy of the GNU General Public License |
| 20 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 21 |
* |
| 22 |
* [Oracle and Java are registered trademarks of Oracle and/or its affiliates. |
| 23 |
* Other names may be trademarks of their respective owners.] |
| 24 |
* |
| 25 |
* If you do not wish to be bound by the terms of the GPL, an alternative |
| 26 |
* runtime license is available to JFree sponsors: |
| 27 |
* |
| 28 |
* https://github.com/sponsors/jfree |
| 29 |
* |
| 30 |
*/ |
| 31 |
|
| 32 |
package org.jfree.pdf; |
| 33 |
|
| 34 |
import java.awt.Font; |
| 35 |
import java.util.HashMap; |
| 36 |
import java.util.Map; |
| 37 |
import org.jfree.pdf.internal.FontKey; |
| 38 |
import org.jfree.pdf.internal.PDFFont; |
| 39 |
|
| 40 |
/** |
| 41 |
* A class that can be used to map AWT/Java2D fonts to PDF built-in font names. |
| 42 |
* This is a very minimal way to support fonts in this {@link PDFGraphics2D} |
| 43 |
* implementation. |
| 44 |
* <br><br> |
| 45 |
* Note that there is an option to draw text as vector graphics which you can |
| 46 |
* specify using the rendering hint {@link PDFHints#KEY_DRAW_STRING_TYPE}. |
| 47 |
* This can be useful, for example, if you need to display characters (such |
| 48 |
* as the euro symbol) that are not supported by the PDF built-in fonts. |
| 49 |
* |
| 50 |
*/ |
| 51 |
public class DefaultFontMapper implements FontMapper { |
| 52 |
|
| 53 |
private final Map<FontKey, String> map; |
| 54 |
|
| 55 |
/** |
| 56 |
* Creates a new instance with default mappings. |
| 57 |
*/ |
| 58 |
public DefaultFontMapper() { |
| 59 |
this.map = new HashMap<>(); |
| 60 |
this.map.put(new FontKey("Dialog", false, false), PDFFont.HELVETICA); |
| 61 |
this.map.put(new FontKey("Dialog", true, false), PDFFont.HELVETICA_BOLD); |
| 62 |
this.map.put(new FontKey("Dialog", false, true), PDFFont.HELVETICA_OBLIQUE); |
| 63 |
this.map.put(new FontKey("Dialog", true, true), PDFFont.HELVETICA_BOLDOBLIQUE); |
| 64 |
this.map.put(new FontKey("Arial", false, false), PDFFont.HELVETICA); |
| 65 |
this.map.put(new FontKey("Arial", true, false), PDFFont.HELVETICA_BOLD); |
| 66 |
this.map.put(new FontKey("Arial", false, true), PDFFont.HELVETICA_OBLIQUE); |
| 67 |
this.map.put(new FontKey("Arial", true, true), PDFFont.HELVETICA_BOLDOBLIQUE); |
| 68 |
this.map.put(new FontKey("Courier", false, false), PDFFont.COURIER); |
| 69 |
this.map.put(new FontKey("Courier", true, false), PDFFont.COURIER_BOLD); |
| 70 |
this.map.put(new FontKey("Courier", false, true), PDFFont.COURIER_ITALIC); |
| 71 |
this.map.put(new FontKey("Courier", true, true), PDFFont.COURIER_BOLDITALIC); |
| 72 |
this.map.put(new FontKey("Courier_New", false, false), PDFFont.COURIER); |
| 73 |
this.map.put(new FontKey("Courier_New", true, false), PDFFont.COURIER_BOLD); |
| 74 |
this.map.put(new FontKey("Courier_New", false, true), PDFFont.COURIER_ITALIC); |
| 75 |
this.map.put(new FontKey("Courier_New", true, true), PDFFont.COURIER_BOLDITALIC); |
| 76 |
this.map.put(new FontKey("DialogInput", false, false), PDFFont.HELVETICA); |
| 77 |
this.map.put(new FontKey("DialogInput", true, false), PDFFont.HELVETICA_BOLD); |
| 78 |
this.map.put(new FontKey("DialogInput", false, true), PDFFont.HELVETICA_OBLIQUE); |
| 79 |
this.map.put(new FontKey("DialogInput", true, true), PDFFont.HELVETICA_BOLDOBLIQUE); |
| 80 |
this.map.put(new FontKey("MgOpen_Cosmetica", false, false), PDFFont.TIMES_ROMAN); |
| 81 |
this.map.put(new FontKey("MgOpen_Cosmetica", true, false), PDFFont.TIMES_BOLD); |
| 82 |
this.map.put(new FontKey("MgOpen_Cosmetica", false, true), PDFFont.TIMES_ITALIC); |
| 83 |
this.map.put(new FontKey("MgOpen_Cosmetica", true, true), PDFFont.TIMES_BOLDITALIC); |
| 84 |
this.map.put(new FontKey("Monospaced", false, false), PDFFont.COURIER); |
| 85 |
this.map.put(new FontKey("Monospaced", true, false), PDFFont.COURIER_BOLD); |
| 86 |
this.map.put(new FontKey("Monospaced", false, true), PDFFont.COURIER_ITALIC); |
| 87 |
this.map.put(new FontKey("Monospaced", true, true), PDFFont.COURIER_BOLDITALIC); |
| 88 |
this.map.put(new FontKey("Palatino", false, false), PDFFont.TIMES_ROMAN); |
| 89 |
this.map.put(new FontKey("Palatino", true, false), PDFFont.TIMES_BOLD); |
| 90 |
this.map.put(new FontKey("Palatino", false, true), PDFFont.TIMES_ITALIC); |
| 91 |
this.map.put(new FontKey("Palatino", true, true), PDFFont.TIMES_BOLDITALIC); |
| 92 |
this.map.put(new FontKey("SansSerif", false, false), PDFFont.HELVETICA); |
| 93 |
this.map.put(new FontKey("SansSerif", true, false), PDFFont.HELVETICA_BOLD); |
| 94 |
this.map.put(new FontKey("SansSerif", false, true), PDFFont.HELVETICA_OBLIQUE); |
| 95 |
this.map.put(new FontKey("SansSerif", true, true), PDFFont.HELVETICA_BOLDOBLIQUE); |
| 96 |
this.map.put(new FontKey("Serif", false, false), PDFFont.TIMES_ROMAN); |
| 97 |
this.map.put(new FontKey("Serif", true, false), PDFFont.TIMES_BOLD); |
| 98 |
this.map.put(new FontKey("Serif", false, true), PDFFont.TIMES_ITALIC); |
| 99 |
this.map.put(new FontKey("Serif", true, true), PDFFont.TIMES_BOLDITALIC); |
| 100 |
this.map.put(new FontKey("Tahoma", false, false), PDFFont.TIMES_ROMAN); |
| 101 |
this.map.put(new FontKey("Tahoma", true, false), PDFFont.TIMES_BOLD); |
| 102 |
this.map.put(new FontKey("Tahoma", false, true), PDFFont.TIMES_ITALIC); |
| 103 |
this.map.put(new FontKey("Tahoma", true, true), PDFFont.TIMES_BOLDITALIC); |
| 104 |
this.map.put(new FontKey("Times_New_Roman", false, false), PDFFont.TIMES_ROMAN); |
| 105 |
this.map.put(new FontKey("Times_New_Roman", true, false), PDFFont.TIMES_BOLD); |
| 106 |
this.map.put(new FontKey("Times_New_Roman", false, true), PDFFont.TIMES_ITALIC); |
| 107 |
this.map.put(new FontKey("Times_New_Roman", true, true), PDFFont.TIMES_BOLDITALIC); |
| 108 |
} |
| 109 |
|
| 110 |
@Override |
| 111 |
public String mapToBaseFont(Font f) { |
| 112 |
String result = this.map.get(FontKey.createFontKey(f)); |
| 113 |
if (result == null) { |
| 114 |
result = "Courier"; |
| 115 |
} |
| 116 |
return result; |
| 117 |
} |
| 118 |
|
| 119 |
} |