001/* =====================================================================
002 * JFreePDF : a fast, light-weight PDF library for the Java(tm) platform
003 * =====================================================================
004 * 
005 * (C)opyright 2013-2020, by Object Refinery Limited.  All rights reserved.
006 * 
007 * https://github.com/jfree/jfreepdf
008 * 
009 * This program is free software: you can redistribute it and/or modify
010 * it under the terms of the GNU General Public License as published by
011 * the Free Software Foundation, either version 3 of the License, or
012 * (at your option) any later version.
013 *
014 * This program is distributed in the hope that it will be useful,
015 * but WITHOUT ANY WARRANTY; without even the implied warranty of
016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
017 * GNU General Public License for more details.
018 *
019 * You should have received a copy of the GNU General Public License
020 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
021 * 
022 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 
023 * Other names may be trademarks of their respective owners.]
024 * 
025 * If you do not wish to be bound by the terms of the GPL, an alternative
026 * commercial license can be purchased.  For details, please see the
027 * Orson PDF home page:
028 * 
029 * http://www.object-refinery.com/orsonpdf/index.html
030 * 
031 */
032
033package org.jfree.pdf;
034
035import java.awt.Font;
036import java.util.HashMap;
037import java.util.Map;
038import org.jfree.pdf.internal.FontKey;
039import org.jfree.pdf.internal.PDFFont;
040
041/**
042 * A class that can be used to map AWT/Java2D fonts to PDF built-in font names.
043 * This is a very minimal way to support fonts in this {@link PDFGraphics2D} 
044 * implementation.
045 * <br><br>
046 * Note that there is an option to draw text as vector graphics which you can
047 * specify using the rendering hint {@link PDFHints#KEY_DRAW_STRING_TYPE}.
048 * This can be useful, for example, if you need to display characters (such
049 * as the euro symbol) that are not supported by the PDF built-in fonts.
050 * 
051 */
052public class DefaultFontMapper implements FontMapper {
053
054    private final Map<FontKey, String> map;
055    
056    /**
057     * Creates a new instance with default mappings.
058     */
059    public DefaultFontMapper() {
060        this.map = new HashMap<>();
061        this.map.put(new FontKey("Dialog", false, false), PDFFont.HELVETICA);
062        this.map.put(new FontKey("Dialog", true, false), PDFFont.HELVETICA_BOLD);
063        this.map.put(new FontKey("Dialog", false, true), PDFFont.HELVETICA_OBLIQUE);
064        this.map.put(new FontKey("Dialog", true, true), PDFFont.HELVETICA_BOLDOBLIQUE);
065        this.map.put(new FontKey("Arial", false, false), PDFFont.HELVETICA);
066        this.map.put(new FontKey("Arial", true, false), PDFFont.HELVETICA_BOLD);
067        this.map.put(new FontKey("Arial", false, true), PDFFont.HELVETICA_OBLIQUE);
068        this.map.put(new FontKey("Arial", true, true), PDFFont.HELVETICA_BOLDOBLIQUE);
069        this.map.put(new FontKey("Courier", false, false), PDFFont.COURIER);
070        this.map.put(new FontKey("Courier", true, false), PDFFont.COURIER_BOLD);
071        this.map.put(new FontKey("Courier", false, true), PDFFont.COURIER_ITALIC);
072        this.map.put(new FontKey("Courier", true, true), PDFFont.COURIER_BOLDITALIC);
073        this.map.put(new FontKey("Courier_New", false, false), PDFFont.COURIER);
074        this.map.put(new FontKey("Courier_New", true, false), PDFFont.COURIER_BOLD);
075        this.map.put(new FontKey("Courier_New", false, true), PDFFont.COURIER_ITALIC);
076        this.map.put(new FontKey("Courier_New", true, true), PDFFont.COURIER_BOLDITALIC);
077        this.map.put(new FontKey("DialogInput", false, false), PDFFont.HELVETICA);
078        this.map.put(new FontKey("DialogInput", true, false), PDFFont.HELVETICA_BOLD);
079        this.map.put(new FontKey("DialogInput", false, true), PDFFont.HELVETICA_OBLIQUE);
080        this.map.put(new FontKey("DialogInput", true, true), PDFFont.HELVETICA_BOLDOBLIQUE);
081        this.map.put(new FontKey("MgOpen_Cosmetica", false, false), PDFFont.TIMES_ROMAN);
082        this.map.put(new FontKey("MgOpen_Cosmetica", true, false), PDFFont.TIMES_BOLD);
083        this.map.put(new FontKey("MgOpen_Cosmetica", false, true), PDFFont.TIMES_ITALIC);
084        this.map.put(new FontKey("MgOpen_Cosmetica", true, true), PDFFont.TIMES_BOLDITALIC);
085        this.map.put(new FontKey("Monospaced", false, false), PDFFont.COURIER);
086        this.map.put(new FontKey("Monospaced", true, false), PDFFont.COURIER_BOLD);
087        this.map.put(new FontKey("Monospaced", false, true), PDFFont.COURIER_ITALIC);
088        this.map.put(new FontKey("Monospaced", true, true), PDFFont.COURIER_BOLDITALIC);
089        this.map.put(new FontKey("Palatino", false, false), PDFFont.TIMES_ROMAN);
090        this.map.put(new FontKey("Palatino", true, false), PDFFont.TIMES_BOLD);
091        this.map.put(new FontKey("Palatino", false, true), PDFFont.TIMES_ITALIC);
092        this.map.put(new FontKey("Palatino", true, true), PDFFont.TIMES_BOLDITALIC);
093        this.map.put(new FontKey("SansSerif", false, false), PDFFont.HELVETICA);
094        this.map.put(new FontKey("SansSerif", true, false), PDFFont.HELVETICA_BOLD);
095        this.map.put(new FontKey("SansSerif", false, true), PDFFont.HELVETICA_OBLIQUE);
096        this.map.put(new FontKey("SansSerif", true, true), PDFFont.HELVETICA_BOLDOBLIQUE);
097        this.map.put(new FontKey("Serif", false, false), PDFFont.TIMES_ROMAN);
098        this.map.put(new FontKey("Serif", true, false), PDFFont.TIMES_BOLD);
099        this.map.put(new FontKey("Serif", false, true), PDFFont.TIMES_ITALIC);
100        this.map.put(new FontKey("Serif", true, true), PDFFont.TIMES_BOLDITALIC);
101        this.map.put(new FontKey("Tahoma", false, false), PDFFont.TIMES_ROMAN);
102        this.map.put(new FontKey("Tahoma", true, false), PDFFont.TIMES_BOLD);
103        this.map.put(new FontKey("Tahoma", false, true), PDFFont.TIMES_ITALIC);
104        this.map.put(new FontKey("Tahoma", true, true), PDFFont.TIMES_BOLDITALIC);
105        this.map.put(new FontKey("Times_New_Roman", false, false), PDFFont.TIMES_ROMAN);
106        this.map.put(new FontKey("Times_New_Roman", true, false), PDFFont.TIMES_BOLD);
107        this.map.put(new FontKey("Times_New_Roman", false, true), PDFFont.TIMES_ITALIC);
108        this.map.put(new FontKey("Times_New_Roman", true, true), PDFFont.TIMES_BOLDITALIC);
109    }
110    
111    @Override
112    public String mapToBaseFont(Font f) {
113        String result = this.map.get(FontKey.createFontKey(f));
114        if (result == null) {
115            result = "Courier";
116        }
117        return result;
118    }
119    
120}