    001/* =====================================================================
    002 * JFreePDF : a fast, light-weight PDF library for the Java(tm) platform
    003 * =====================================================================
    004 *
    005 * (C)opyright 2013-2022, by David Gilbert.  All rights reserved.
    006 *
    007 * https://github.com/jfree/orsonpdf
    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 * runtime license is available to JFree sponsors:
    027 *
    028 * https://github.com/sponsors/jfree
    029 *
    030 */
    031
    032package org.jfree.pdf;
    033
    034import java.awt.Font;
    035import java.util.HashMap;
    036import java.util.Map;
    037import org.jfree.pdf.internal.FontKey;
    038import org.jfree.pdf.internal.PDFFont;
    039
    040/**
    041 * A class that can be used to map AWT/Java2D fonts to PDF built-in font names.
    042 * This is a very minimal way to support fonts in this {@link PDFGraphics2D}
    043 * implementation.
    044 * <br><br>
    045 * Note that there is an option to draw text as vector graphics which you can
    046 * specify using the rendering hint {@link PDFHints#KEY_DRAW_STRING_TYPE}.
    047 * This can be useful, for example, if you need to display characters (such
    048 * as the euro symbol) that are not supported by the PDF built-in fonts.
    049 *
    050 */
    051public class DefaultFontMapper implements FontMapper {
    052
    053    private final Map<FontKey, String> map;
    054
    055    /**
    056     * Creates a new instance with default mappings.
    057     */
    058    public DefaultFontMapper() {
    059        this.map = new HashMap<>();
    060        this.map.put(new FontKey("Dialog", false, false), PDFFont.HELVETICA);
    061        this.map.put(new FontKey("Dialog", true, false), PDFFont.HELVETICA_BOLD);
    062        this.map.put(new FontKey("Dialog", false, true), PDFFont.HELVETICA_OBLIQUE);
    063        this.map.put(new FontKey("Dialog", true, true), PDFFont.HELVETICA_BOLDOBLIQUE);
    064        this.map.put(new FontKey("Arial", false, false), PDFFont.HELVETICA);
    065        this.map.put(new FontKey("Arial", true, false), PDFFont.HELVETICA_BOLD);
    066        this.map.put(new FontKey("Arial", false, true), PDFFont.HELVETICA_OBLIQUE);
    067        this.map.put(new FontKey("Arial", true, true), PDFFont.HELVETICA_BOLDOBLIQUE);
    068        this.map.put(new FontKey("Courier", false, false), PDFFont.COURIER);
    069        this.map.put(new FontKey("Courier", true, false), PDFFont.COURIER_BOLD);
    070        this.map.put(new FontKey("Courier", false, true), PDFFont.COURIER_ITALIC);
    071        this.map.put(new FontKey("Courier", true, true), PDFFont.COURIER_BOLDITALIC);
    072        this.map.put(new FontKey("Courier_New", false, false), PDFFont.COURIER);
    073        this.map.put(new FontKey("Courier_New", true, false), PDFFont.COURIER_BOLD);
    074        this.map.put(new FontKey("Courier_New", false, true), PDFFont.COURIER_ITALIC);
    075        this.map.put(new FontKey("Courier_New", true, true), PDFFont.COURIER_BOLDITALIC);
    076        this.map.put(new FontKey("DialogInput", false, false), PDFFont.HELVETICA);
    077        this.map.put(new FontKey("DialogInput", true, false), PDFFont.HELVETICA_BOLD);
    078        this.map.put(new FontKey("DialogInput", false, true), PDFFont.HELVETICA_OBLIQUE);
    079        this.map.put(new FontKey("DialogInput", true, true), PDFFont.HELVETICA_BOLDOBLIQUE);
    080        this.map.put(new FontKey("MgOpen_Cosmetica", false, false), PDFFont.TIMES_ROMAN);
    081        this.map.put(new FontKey("MgOpen_Cosmetica", true, false), PDFFont.TIMES_BOLD);
    082        this.map.put(new FontKey("MgOpen_Cosmetica", false, true), PDFFont.TIMES_ITALIC);
    083        this.map.put(new FontKey("MgOpen_Cosmetica", true, true), PDFFont.TIMES_BOLDITALIC);
    084        this.map.put(new FontKey("Monospaced", false, false), PDFFont.COURIER);
    085        this.map.put(new FontKey("Monospaced", true, false), PDFFont.COURIER_BOLD);
    086        this.map.put(new FontKey("Monospaced", false, true), PDFFont.COURIER_ITALIC);
    087        this.map.put(new FontKey("Monospaced", true, true), PDFFont.COURIER_BOLDITALIC);
    088        this.map.put(new FontKey("Palatino", false, false), PDFFont.TIMES_ROMAN);
    089        this.map.put(new FontKey("Palatino", true, false), PDFFont.TIMES_BOLD);
    090        this.map.put(new FontKey("Palatino", false, true), PDFFont.TIMES_ITALIC);
    091        this.map.put(new FontKey("Palatino", true, true), PDFFont.TIMES_BOLDITALIC);
    092        this.map.put(new FontKey("SansSerif", false, false), PDFFont.HELVETICA);
    093        this.map.put(new FontKey("SansSerif", true, false), PDFFont.HELVETICA_BOLD);
    094        this.map.put(new FontKey("SansSerif", false, true), PDFFont.HELVETICA_OBLIQUE);
    095        this.map.put(new FontKey("SansSerif", true, true), PDFFont.HELVETICA_BOLDOBLIQUE);
    096        this.map.put(new FontKey("Serif", false, false), PDFFont.TIMES_ROMAN);
    097        this.map.put(new FontKey("Serif", true, false), PDFFont.TIMES_BOLD);
    098        this.map.put(new FontKey("Serif", false, true), PDFFont.TIMES_ITALIC);
    099        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}