com.github.exerrk.engine.fonts.FontExtensionsRegistry Maven / Gradle / Ivy
The newest version!
/*
* JasperReports - Free Java Reporting Library.
* Copyright (C) 2001 - 2016 TIBCO Software Inc. All rights reserved.
* http://www.jaspersoft.com
*
* Unless you have purchased a commercial license agreement from Jaspersoft,
* the following license terms apply:
*
* This program is part of JasperReports.
*
* JasperReports 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
* (at your option) any later version.
*
* JasperReports 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with JasperReports. If not, see .
*/
package com.github.exerrk.engine.fonts;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.github.exerrk.engine.DefaultJasperReportsContext;
import com.github.exerrk.engine.JRRuntimeException;
import com.github.exerrk.extensions.ExtensionsRegistry;
/**
* @author Teodor Danciu ([email protected])
*/
public class FontExtensionsRegistry implements ExtensionsRegistry
{
private static final Log log = LogFactory.getLog(FontExtensionsRegistry.class);
private final List fontFamiliesLocations;
private List fontFamilies;
private List fontSets;
public FontExtensionsRegistry(List fontFamiliesLocations)
{
this.fontFamiliesLocations = fontFamiliesLocations;
}
@Override
public List getExtensions(Class extensionType)
{
if (FontFamily.class.equals(extensionType))
{
ensureFontExtensions();
@SuppressWarnings("unchecked")
List extensions = (List) fontFamilies;
return extensions;
}
if (FontSet.class.equals(extensionType))
{
ensureFontExtensions();
@SuppressWarnings("unchecked")
List extensions = (List) fontSets;
return extensions;
}
return null;
}
protected void ensureFontExtensions()
{
if ((fontFamilies == null || fontSets == null) && fontFamiliesLocations != null)
{
SimpleFontExtensionHelper fontExtensionHelper = SimpleFontExtensionHelper.getInstance();
DefaultJasperReportsContext context = DefaultJasperReportsContext.getInstance();
FontExtensionsCollector extensionsCollector = new FontExtensionsCollector();
for (String location : fontFamiliesLocations)
{
if (log.isDebugEnabled())
{
log.debug("Loading font extensions from " + location);
}
try
{
fontExtensionHelper.loadFontExtensions(context, location, extensionsCollector);
}
catch (JRRuntimeException e)//only catching JRRuntimeException for now
{
log.error("Error loading font extensions from " + location, e);
//keeping any font extensions collected so far, though it's a little weird
}
}
fontFamilies = extensionsCollector.getFontFamilies();
fontSets = extensionsCollector.getFontSets();
}
}
}