001/* ===================================================
002 * JFreeSVG : an SVG library for the Java(tm) platform
003 * ===================================================
004 * 
005 * (C)opyright 2013-2021, by Object Refinery Limited.  All rights reserved.
006 *
007 * Project Info:  http://www.jfree.org/jfreesvg/index.html
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 visit the
027 * JFreeSVG home page:
028 * 
029 * http://www.jfree.org/jfreesvg
030 * 
031 */
032
033package org.jfree.graphics2d.svg;
034
035import java.awt.Font;
036import java.util.HashMap;
037import java.util.Map;
038import org.jfree.graphics2d.Args;
039
040/**
041 * A default implementation of the {@link FontMapper} interface.  This 
042 * implementation will map the Java logical fonts to equivalent SVG generic
043 * fonts.  You can add your own mappings if you need to.
044 * 
045 * @since 1.5
046 */
047public class StandardFontMapper implements FontMapper {
048    
049    /** Storage for the alternates. */
050    private Map<String, String> alternates;
051    
052    /**
053     * Creates a new instance with mappings for the Java logical fonts.
054     */
055    public StandardFontMapper() {
056        this.alternates = new HashMap<String, String>();
057        this.alternates.put(Font.DIALOG, "sans-serif");
058        this.alternates.put(Font.DIALOG_INPUT, "monospace");
059        this.alternates.put(Font.SANS_SERIF, "sans-serif");
060        this.alternates.put(Font.SERIF, "serif");
061        this.alternates.put(Font.MONOSPACED, "monospace");
062    }
063
064    /**
065     * Returns the mapped (alternate) font family name.
066     * 
067     * @param family  the font family ({@code null} not permitted).
068     * 
069     * @return The alternate font family name (possibly {@code null}). 
070     */
071    public String get(String family) {
072        Args.nullNotPermitted(family, "family");
073        return this.alternates.get(family);
074    }
075    
076    /**
077     * Adds a font family mapping (if the specified alternate is 
078     * {@code null} it has the effect of clearing any existing mapping).
079     * 
080     * @param family  the font family name ({@code null} not permitted).
081     * @param alternate  the alternate ({@code null} permitted).
082     */
083    public void put(String family, String alternate) {
084        Args.nullNotPermitted(family, "family");
085        this.alternates.put(family, alternate);
086    }
087    
088    /**
089     * Maps the specified font family name to an alternative, or else returns
090     * the same family name.
091     * 
092     * @param family  the font family name ({@code null} not permitted).
093     * 
094     * @return The same font family name or an alternative (never {@code null}).
095     */
096    @Override
097    public String mapFont(String family) {
098        Args.nullNotPermitted(family, "family");
099        String alternate = this.alternates.get(family);
100        if (alternate != null) {
101            return alternate;
102        }
103        return family;
104    }
105
106}