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.svg;
034
035import java.awt.Font;
036import java.util.HashMap;
037import java.util.Map;
038import java.util.function.Function;
039import org.jfree.svg.util.Args;
040
041/**
042 * The standard function used in JFreeSVG to create font references for the
043 * SVG output.  This implementation will substitute SVG generic font names
044 * for the Java logical fonts.  Methods are provided for adding more font
045 * substitutions if you require them.  This function will also surround the
046 * font name with single-quotes, which addresses issue #27.
047 * 
048 * @since 5.0
049 */
050public class StandardFontFunction implements Function<String, String> {
051    
052    /** A map of substitute font family names. */
053    private final Map<String, String> alternates;
054    
055    /**
056     * Creates a new instance with mappings for the Java logical fonts.
057     */
058    public StandardFontFunction() {
059        this.alternates = new HashMap<>();
060        this.alternates.put(Font.DIALOG, "sans-serif");
061        this.alternates.put(Font.DIALOG_INPUT, "monospace");
062        this.alternates.put(Font.SANS_SERIF, "sans-serif");
063        this.alternates.put(Font.SERIF, "serif");
064        this.alternates.put(Font.MONOSPACED, "monospace");
065    }
066
067    /**
068     * Returns the (substitute) SVG font family name for the specified Java font
069     * family name, or {@code null} if there is no substitute.
070     * 
071     * @param family  the Java font family name ({@code null} not permitted).
072     * 
073     * @return The substitute SVG font family name, or {@code null}. 
074     */
075    public String get(String family) {
076        Args.nullNotPermitted(family, "family");
077        return this.alternates.get(family);
078    }
079    
080    /**
081     * Adds a substitute font family name (to be used in the SVG output) for 
082     * the given Java font family name.  If the specified alternate is 
083     * {@code null} it has the effect of clearing any existing substitution.
084     * 
085     * @param family  the Java font family name ({@code null} not permitted).
086     * @param substitute  the font family name to substitute in the SVG 
087     *     output ({@code null} permitted).
088     */
089    public void put(String family, String substitute) {
090        Args.nullNotPermitted(family, "family");
091        this.alternates.put(family, substitute);
092    }
093    
094    /**
095     * Returns the SVG font reference for the supplied (Java) font family 
096     * name.  This implementation provides substitute names for the Java
097     * logical fonts.  Other Java font family names are not changed, but all
098     * font family names are surrounded in single quotes for the SVG output.
099     * 
100     * @param family  the font family name ({@code null} not permitted).
101     * 
102     * @return The SVG font reference (never {@code null}).
103     */
104    @Override
105    public String apply(String family) {
106        Args.nullNotPermitted(family, "family");
107        String alternate = this.alternates.get(family);
108        if (alternate == null) {
109            alternate = "\"" + family + "\"";
110        }
111        return alternate;
112    }
113
114}