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;
25
26 import java.awt.font.TextAttribute;
27 import java.text.AttributedCharacterIterator.Attribute;
28 import java.util.HashMap;
29 import java.util.Locale;
30 import java.util.Map;
31
32 import net.sf.jasperreports.engine.fonts.FontUtil;
33 import net.sf.jasperreports.engine.type.ModeEnum;
34 import net.sf.jasperreports.engine.util.JRDataUtils;
35 import net.sf.jasperreports.engine.util.JRStyledTextParser;
36 import net.sf.jasperreports.engine.util.JRStyledTextUtil;
37
38 /**
39  * Selector of element-level styled text attributes for print text objects.
40  * 
41  * @author Lucian Chirita (lucianc@users.sourceforge.net)
42  * @see JRStyledTextUtil#getStyledText(JRPrintText, JRStyledTextAttributeSelector)
43  * @see JRPrintText#getFullStyledText(JRStyledTextAttributeSelector)
44  */

45 public abstract class JRStyledTextAttributeSelector
46 {
47     protected final JasperReportsContext jasperReportsContext;
48     
49     /**
50      * 
51      */

52     protected JRStyledTextAttributeSelector(JasperReportsContext jasperReportsContext)
53     {
54         this.jasperReportsContext = jasperReportsContext;
55     }
56     
57     /**
58      * 
59      */

60     private static Locale getLocale()
61     {
62         return JRStyledTextParser.getLocale();
63     }
64     
65     /**
66      * 
67      */

68     public static Locale getTextLocale(JRPrintText printText)
69     {
70         String localeCode = printText.getLocaleCode();
71         if (localeCode == null)
72         {
73             return getLocale();
74         }
75         return JRDataUtils.getLocale(localeCode);
76     }
77     
78     /**
79      * Construct a map containing the selected element-level styled text attributes
80      * for a print text element.
81      * 
82      * @param printText the print text object
83      * @return a map containing styled text attributes
84      */

85     public abstract Map<Attribute,Object> getStyledTextAttributes(JRPrintText printText);
86     
87
88     /**
89      * Selects all styled text attributes, i.e. font attributes plus forecolor
90      * and backcolor.
91      */

92     public static JRStyledTextAttributeSelector getAllSelector(JasperReportsContext jasperReportsContext)
93     {
94         return new AllSelector(jasperReportsContext);
95     }
96     
97
98     /**
99      * Selects all styled text attributes, i.e. font attributes plus forecolor
100      * and backcolor.
101      */

102     private static class AllSelector extends JRStyledTextAttributeSelector
103     {
104         public AllSelector(JasperReportsContext jasperReportsContext)
105         {
106             super(jasperReportsContext);
107         }
108         
109         @Override
110         public Map<Attribute,Object> getStyledTextAttributes(JRPrintText printText)
111         {
112             Map<Attribute,Object> attributes = new HashMap<Attribute,Object>(); 
113             //JRFontUtil.getAttributes(attributes, printText, getTextLocale(printText));
114             FontUtil.getInstance(jasperReportsContext).getAttributesWithoutAwtFont(attributes, printText);
115             attributes.put(TextAttribute.FOREGROUND, printText.getForecolor());
116             if (printText.getModeValue() == ModeEnum.OPAQUE)
117             {
118                 attributes.put(TextAttribute.BACKGROUND, printText.getBackcolor());
119             }
120             return attributes;
121         }
122     }
123
124
125     /**
126      * Selects all styled text attribute except backcolor, i.e. font attributes
127      * plus forecolor.
128      */

129     public static JRStyledTextAttributeSelector getNoBackcolorSelector(JasperReportsContext jasperReportsContext)
130     {
131         return new NoBackcolorSelector(jasperReportsContext);
132     }
133     
134
135     /**
136      * Selects all styled text attribute except backcolor, i.e. font attributes
137      * plus forecolor.
138      */

139     private static class NoBackcolorSelector extends JRStyledTextAttributeSelector
140     {
141         public NoBackcolorSelector(JasperReportsContext jasperReportsContext)
142         {
143             super(jasperReportsContext);
144         }
145         
146         @Override
147         public Map<Attribute,Object> getStyledTextAttributes(JRPrintText printText)
148         {
149             Map<Attribute,Object> attributes = new HashMap<Attribute,Object>(); 
150             //JRFontUtil.getAttributes(attributes, printText, getTextLocale(printText));
151             FontUtil.getInstance(jasperReportsContext).getAttributesWithoutAwtFont(attributes, printText);
152             attributes.put(TextAttribute.FOREGROUND, printText.getForecolor());
153             return attributes;
154         }
155     }
156     
157
158     /**
159      * Doesn't select any styled text attribute.
160      */

161     public static JRStyledTextAttributeSelector getNoneSelector(JasperReportsContext jasperReportsContext)
162     {
163         return new NoneSelector(jasperReportsContext);
164     }
165     
166
167     /**
168      * Doesn't select any styled text attribute.
169      */

170     private static class NoneSelector extends JRStyledTextAttributeSelector
171     {
172         public NoneSelector(JasperReportsContext jasperReportsContext)
173         {
174             super(jasperReportsContext);
175         }
176         
177         @Override
178         public Map<Attribute,Object> getStyledTextAttributes(JRPrintText printText)
179         {
180             return null;
181         }
182     }
183     
184 }
185
186