package com.srbenoit.font;

import java.awt.Font;

/**
 * A specification for a font that can be obtained from the <code>BundledFontManager</code>.
 */
public final class FontSpec {

    /** the name of the font, or one of "SANS", "SERIF" or "MONOSPACE" */
    private String fontName = null;

    /** the point size of the font */
    private float fontSize = 1.0f;

    /** a scaling factor to apply to the font size */
    private float fontScale = 1.0f;

    /** the style of the font */
    private int fontStyle = Font.PLAIN;

    /**
     * Gets the font name.
     *
     * @return  the font name
     */
    public String getFontName() {

        return this.fontName;
    }

    /**
     * Sets the font name.
     *
     * @param  theFontName  the font name
     */
    public void setFontName(final String theFontName) {

        this.fontName = theFontName;
    }

    /**
     * Gets the font size.
     *
     * @return  the font size
     */
    public float getFontSize() {

        return this.fontSize;
    }

    /**
     * Sets the font size.
     *
     * @param  theFontSize  the font size
     */
    public void setFontSize(final float theFontSize) {

        this.fontSize = theFontSize;
    }

    /**
     * Gets the font scale.
     *
     * @return  the font scale
     */
    public float getFontScale() {

        return this.fontScale;
    }

    /**
     * Sets the font scale.
     *
     * @param  theFontScale  the font scale
     */
    public void setFontScale(final float theFontScale) {

        this.fontScale = theFontScale;
    }

    /**
     * Gets the font style.
     *
     * @return  the font style
     */
    public int getFontStyle() {

        return this.fontStyle;
    }

    /**
     * Sets the font style.
     *
     * @param  theFontStyle  the font style
     */
    public void setFontStyle(final int theFontStyle) {

        this.fontStyle = theFontStyle;
    }

    /**
     * Prints the font specification in the format of attributes in an XML tag.
     *
     * @param  builder  the <code>StringBuilder</code> to which to print
     * @param  prefix   a prefix to use on attributes. For example, if the prefix is 'foo', the
     *                  printed attributes will be 'foo-font', 'foo-size' and 'foo-style'
     */
    public void printAsAttributes(final StringBuilder builder, final String prefix) {

        if (this.fontName != null) {
            builder.append(" ");
            builder.append(prefix);
            builder.append("-font='");
            builder.append(this.fontName);
            builder.append("'");
        }

        builder.append(" ");
        builder.append(prefix);
        builder.append("-size='");
        builder.append(Float.toString(this.fontSize));
        builder.append("'");

        if (this.fontScale != 1.0f) {
            builder.append(" ");
            builder.append(prefix);
            builder.append("-scale='");
            builder.append(Float.toString(this.fontScale));
            builder.append("'");
        }

        builder.append(" ");
        builder.append(prefix);
        builder.append("-style='");

        if (this.fontStyle == Font.PLAIN) {
            builder.append("PLAIN");
        } else if (this.fontStyle == Font.BOLD) {
            builder.append("BOLD");
        } else if (this.fontStyle == Font.ITALIC) {
            builder.append("ITALIC");
        } else if (this.fontStyle == (Font.BOLD | Font.ITALIC)) {
            builder.append("BOLD,ITALIC");
        }

        builder.append("'");
    }
}
