1 /*
2  * JasperReports - Free Java Reporting Library.
3  * Copyright (C) 2001 - 2019 TIBCO Software Inc. All rights reserved.
4  * http://www.jaspersoft.com
5  *
6  * Unless you have purchased a commercial license agreement from Jaspersoft,
7  * the following license terms apply:
8  *
9  * This program is part of JasperReports.
10  *
11  * JasperReports is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * JasperReports is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * along with JasperReports. If not, see <http://www.gnu.org/licenses/>.
23  */

24 package net.sf.jasperreports.engine.fonts;
25
26 import java.util.List;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30
31 import net.sf.jasperreports.engine.DefaultJasperReportsContext;
32 import net.sf.jasperreports.engine.JRRuntimeException;
33 import net.sf.jasperreports.extensions.ExtensionsRegistry;
34
35 /**
36  * @author Teodor Danciu (teodord@users.sourceforge.net)
37  */

38 public class FontExtensionsRegistry implements ExtensionsRegistry
39 {
40     
41     private static final Log log = LogFactory.getLog(FontExtensionsRegistry.class);
42
43     private final List<String> fontFamiliesLocations;
44     private List<FontFamily> fontFamilies;
45     private List<FontSet> fontSets;
46     
47     public FontExtensionsRegistry(List<String> fontFamiliesLocations)
48     {
49         this.fontFamiliesLocations = fontFamiliesLocations;
50     }
51     
52     @Override
53     public <T> List<T> getExtensions(Class<T> extensionType)
54     {
55         if (FontFamily.class.equals(extensionType)) 
56         {
57             ensureFontExtensions();
58             
59             @SuppressWarnings("unchecked")
60             List<T> extensions = (List<T>) fontFamilies;
61             return extensions;
62         }
63         
64         if (FontSet.class.equals(extensionType)) 
65         {
66             ensureFontExtensions();
67             
68             @SuppressWarnings("unchecked")
69             List<T> extensions = (List<T>) fontSets;
70             return extensions;
71         }
72         
73         return null;
74     }
75
76     protected void ensureFontExtensions()
77     {
78         if ((fontFamilies == null || fontSets == null) && fontFamiliesLocations != null)
79         {
80             SimpleFontExtensionHelper fontExtensionHelper = SimpleFontExtensionHelper.getInstance();
81             DefaultJasperReportsContext context = DefaultJasperReportsContext.getInstance();
82             
83             FontExtensionsCollector extensionsCollector = new FontExtensionsCollector();
84             for (String location : fontFamiliesLocations)
85             {
86                 if (log.isDebugEnabled())
87                 {
88                     log.debug("Loading font extensions from " + location);
89                 }
90                 
91                 try
92                 {
93                     fontExtensionHelper.loadFontExtensions(context, location, extensionsCollector);
94                 }
95                 catch (JRRuntimeException e)//only catching JRRuntimeException for now
96                 {
97                     log.error("Error loading font extensions from " + location, e);
98                     //keeping any font extensions collected so far, though it's a little weird
99                 }
100             }
101             
102             fontFamilies = extensionsCollector.getFontFamilies();
103             fontSets = extensionsCollector.getFontSets();
104         }
105     }
106
107 }
108