Model.MyFontSelector.java Source code

Java tutorial

Introduction

Here is the source code for Model.MyFontSelector.java

Source

/**
 * MicroUnits - Pauses Analysis of XML files generated by Translog II software.
 * For Translog II details See <http://bridge.cbs.dk/platform/?q=Translog-II>
 *
 * Copyright (C) 2014 Gabriel Ed. da Silva
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Affero General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package Model;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.FontSelector;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author Gabriel Ed
 */
public class MyFontSelector {

    private final Map<String, FontSelector> fontSelectors;

    /**
     * MyFontSelector Constructor
     * Construtor da Classe MyFontSelector
     */
    private MyFontSelector() {
        this.fontSelectors = new HashMap<>();
        initBaseFonts();
    }

    /**
     * Gets the FontSelector with the type choosed
     * Recupera o seletor de fonte para o tipo escolhido
     *
     * @param st The font type
     * @return FontSelector Object
     */
    public FontSelector getFontSelector(String st) {
        return this.fontSelectors.get(st);
    }

    /**
     * Inits the BaseFonts of External Fonts and gets of the internals
     * Faz a inicializao das BaseFonts das fontes externas e recupera das
     * fontes internas.
     */
    private void initBaseFonts() {
        try {
            ArrayList<BaseFont> baseFontList = new ArrayList<>();
            ArrayList<Font> fontList = new ArrayList<>();

            /**
             * Gets Internals BaseFonts
             * Recupera as BaseFonts das fontes internas
             */
            fontList.add(FontFactory.getFont(BaseFont.TIMES_ROMAN));
            fontList.add(FontFactory.getFont(BaseFont.COURIER));
            fontList.add(FontFactory.getFont(BaseFont.SYMBOL));

            for (Font f : fontList) {
                baseFontList.add(f.getBaseFont());
            }

            /**
             * Gets external chinese traditional BaseFonts
             * Recupera as BaseFonts da fonte externa do Chins tradicional
             */

            baseFontList.add(BaseFont.createFont("MHei-Medium", "UniCNS-UCS2-H", BaseFont.EMBEDDED));
            baseFontList.add(BaseFont.createFont("MSung-Light", "UniCNS-UCS2-H", BaseFont.EMBEDDED));

            /**
             * Gets external chinese simplified BaseFonts
             * Recupera o BaseFont da fonte externa do Chins simplificado
             */
            baseFontList.add(BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED));

            initFonts(baseFontList);
        } catch (DocumentException | IOException ex) {
            System.out.println("Error - BaseFont generation");
        }
    }

    /**
     * Inicializa as fontes
     * Initialize the fonts
     * @param baseFontList A List with the BaseFonts
     */
    private void initFonts(ArrayList<BaseFont> baseFontList) {
        FontSelector fontSelectorTitle = new FontSelector();
        FontSelector fontSelectorBlue = new FontSelector();
        FontSelector fontSelectorGreen = new FontSelector();
        FontSelector fontSelectorRed = new FontSelector();
        FontSelector fontSelectorBlack = new FontSelector();

        for (BaseFont bf : baseFontList) {
            Font TITLE = new Font(bf, 16, Font.BOLD, BaseColor.BLACK);
            Font BLUE = new Font(bf, 14, Font.NORMAL, BaseColor.BLUE);
            Font GREEN = new Font(bf, 14, Font.NORMAL, new BaseColor(8, 138, 75));
            Font RED = new Font(bf, 14, Font.NORMAL, BaseColor.RED);
            Font BLACK = new Font(bf, 14, Font.NORMAL, BaseColor.BLACK);

            fontSelectorTitle.addFont(TITLE);
            fontSelectorBlue.addFont(BLUE);
            fontSelectorGreen.addFont(GREEN);
            fontSelectorRed.addFont(RED);
            fontSelectorBlack.addFont(BLACK);
        }

        fontSelectors.put("TITLE", fontSelectorTitle);
        fontSelectors.put("BLUE", fontSelectorBlue);
        fontSelectors.put("GREEN", fontSelectorGreen);
        fontSelectors.put("RED", fontSelectorRed);
        fontSelectors.put("BLACK", fontSelectorBlack);
    }

    /**
     * Gets a instance
     * Recupera uma instncia
     *
     * @return MyFontSelector Instance
     */
    public static MyFontSelector getInstance() {
        return MyFontSelectorHolder.INSTANCE;
    }

    /**
     * Singleton initiator class
     * Classe responsvel pela instnciao do Singleton.
     */
    private static class MyFontSelectorHolder {

        private static final MyFontSelector INSTANCE = new MyFontSelector();
    }
}