org.squale.welcom.outils.pdf.PDFFontUtil.java Source code

Java tutorial

Introduction

Here is the source code for org.squale.welcom.outils.pdf.PDFFontUtil.java

Source

/**
 * Copyright (C) 2008-2010, Squale Project - http://www.squale.org
 *
 * This file is part of Squale.
 *
 * Squale is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or any later version.
 *
 * Squale 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 General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Squale.  If not, see <http://www.gnu.org/licenses/>.
 */
/*
 * Cr le 18 mars 05
 *
 * Pour changer le modle de ce fichier gnr, allez  :
 * Fentre&gt;Prfrences&gt;Java&gt;Gnration de code&gt;Code et commentaires
 */
package org.squale.welcom.outils.pdf;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.net.URL;
import java.util.Iterator;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionServlet;
import org.squale.welcom.outils.WelcomConfigurator;

import com.lowagie.text.FontFactory;

/**
 * @author M327836 Pour changer le modle de ce commentaire de type gnr, allez  :
 *         Fentre&gt;Prfrences&gt;Java&gt;Gnration de code&gt;Code et commentaires
 */
public class PDFFontUtil {
    /**
     * Commons Logger for this class
     */
    private static Log logger = LogFactory.getLog(PDFFontUtil.class);

    /** Singleton */
    private static PDFFontUtil pdfFontUtil;

    /** L'actionServlet pour la conf */
    private ActionServlet servlet;

    /**
     * Contructer priv
     * 
     * @param s Action Servlet pour la conf
     */
    private PDFFontUtil(final ActionServlet s) {
        servlet = s;
    }

    /**
     * Initolisation du repertoire de font pour l'impression
     * 
     * @param s Action Servlet pour la fonc
     * @return True si TVB
     */
    public static boolean init(final ActionServlet s) {
        pdfFontUtil = new PDFFontUtil(s);
        boolean resultSR = true;
        boolean resultIT = true;

        /*
         * TODO FAB : XReport doit tre sorti car il s'agit d'une lib non OSS try {
         * Class.forName("inetsoft.report.ReportEnv"); inetsoft.report.ReportEnv.setProperty("font.metrics.source",
         * "truetype"); inetsoft.report.ReportEnv.setProperty("font.truetype.path",
         * PDFFontUtil.getRealFontPath().trim()); resultSR = true; } catch (final ClassNotFoundException e) { resultSR =
         * false; }
         */

        try {
            Class.forName("com.lowagie.text.Document");
            final String path = PDFFontUtil.getRealFontPath().trim();
            final File fontDir = new File(path);
            final File[] listFile = fontDir.listFiles(new FilenameFilter() {
                public boolean accept(File dir, String name) {
                    return name.endsWith(".ttf");
                }
            });
            if (listFile != null) {

                for (int i = 0; i < listFile.length; i++) {

                    FontFactory.register(listFile[i].getCanonicalPath());
                }
                if (logger.isDebugEnabled()) {
                    for (final Iterator iter = FontFactory.getRegisteredFamilies().iterator(); iter.hasNext();) {
                        final String font = (String) iter.next();

                        logger.debug("registering " + font);

                    }
                }
            }

            resultIT = true;
        } catch (final ClassNotFoundException e) {
            if (logger.isDebugEnabled()) {
                logger.debug("Erreur lors du chargement des fonts pour itext", e);
            }
            resultIT = false;
        } catch (final IOException ie) {
            if (logger.isDebugEnabled()) {
                logger.debug("Erreur lors du chargement des fonts pour itext", ie);
            }

            resultIT = false;
        }

        return resultSR || resultIT;
    }

    /**
     * @return Retourne le chemin absolu
     */
    private String getInternalRealFontPath() {

        final String inConf = WelcomConfigurator.getMessage(WelcomConfigurator.FONT_PATH);
        URL url;
        try {
            url = this.getClass().getClassLoader().getResource(inConf);
            if (url != null) {
                return url.getFile();
            }
        } catch (final Exception e) {
            e.printStackTrace();

        }
        return servlet.getServletContext().getRealPath(inConf);
    }

    /**
     * @return Retourne le chemin absolu
     */
    public static String getRealFontPath() {
        return pdfFontUtil.getInternalRealFontPath();
    }

    /**
     * @return Verifie que le chemin est trouv
     */
    public static boolean isSpecificRealFontPath() {
        final File f = new File(getRealFontPath());

        return (f.isDirectory());
    }
}