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.util;
25
26 import java.awt.font.TextAttribute;
27 import java.text.AttributedCharacterIterator.Attribute;
28 import java.util.HashMap;
29 import java.util.Map;
30
31 import javax.swing.text.AttributeSet;
32 import javax.swing.text.StyleConstants;
33
34
35 /**
36  * @author Teodor Danciu (teodord@users.sourceforge.net)
37  */

38 public abstract class JEditorPaneMarkupProcessor implements MarkupProcessor
39 {
40
41     /**
42      * 
43      */

44     public static final class RtfFactory implements MarkupProcessorFactory
45     { 
46         @Override
47         public MarkupProcessor createMarkupProcessor()
48         {
49             return JEditorPaneRtfMarkupProcessor.getInstance();
50         }
51     }
52
53     /**
54      * 
55      */

56     public static final class HtmlFactory implements MarkupProcessorFactory
57     { 
58         @Override
59         public MarkupProcessor createMarkupProcessor()
60         {
61             return JEditorPaneHtmlMarkupProcessor.getInstance();
62         }
63     }
64
65     /**
66      * 
67      */

68     protected Map<Attribute,Object> getAttributes(AttributeSet attrSet) 
69     {
70         Map<Attribute,Object> attrMap = new HashMap<Attribute,Object>();
71         if (attrSet.isDefined(StyleConstants.FontFamily))
72         {
73             attrMap.put(
74                 TextAttribute.FAMILY,
75                 StyleConstants.getFontFamily(attrSet)
76                 );
77         }
78                     
79         if (attrSet.isDefined(StyleConstants.Bold))
80         {
81             attrMap.put(
82                 TextAttribute.WEIGHT,
83                 StyleConstants.isBold(attrSet) ? TextAttribute.WEIGHT_BOLD : TextAttribute.WEIGHT_REGULAR
84                 );
85         }
86                     
87         if (attrSet.isDefined(StyleConstants.Italic))
88         {
89             attrMap.put(
90                 TextAttribute.POSTURE,
91                 StyleConstants.isItalic(attrSet) ? TextAttribute.POSTURE_OBLIQUE : TextAttribute.POSTURE_REGULAR
92                 );
93         }
94                     
95         if (attrSet.isDefined(StyleConstants.Underline))
96         {
97             attrMap.put(
98                 TextAttribute.UNDERLINE,
99                 StyleConstants.isUnderline(attrSet) ? TextAttribute.UNDERLINE_ON : null
100                 );
101         }
102                     
103         if (attrSet.isDefined(StyleConstants.StrikeThrough))
104         {
105             attrMap.put(
106                 TextAttribute.STRIKETHROUGH,
107                 StyleConstants.isStrikeThrough(attrSet) ? TextAttribute.STRIKETHROUGH_ON : null
108                 );
109         }
110                     
111         if (attrSet.isDefined(StyleConstants.FontSize))
112         {
113             attrMap.put(
114                 TextAttribute.SIZE,
115                 StyleConstants.getFontSize(attrSet)
116                 );
117         }
118                     
119         if (attrSet.isDefined(StyleConstants.Foreground))
120         {
121             attrMap.put(
122                 TextAttribute.FOREGROUND,
123                 StyleConstants.getForeground(attrSet)
124                 );
125         }
126                     
127         if (attrSet.isDefined(StyleConstants.Background))
128         {
129             attrMap.put(
130                 TextAttribute.BACKGROUND,
131                 StyleConstants.getBackground(attrSet)
132                 );
133         }
134         
135         //FIXME: why StyleConstants.isSuperscript(attrSet) does return false
136         if (attrSet.isDefined(StyleConstants.Superscript) && !StyleConstants.isSubscript(attrSet))
137         {
138             attrMap.put(
139                 TextAttribute.SUPERSCRIPT,
140                 TextAttribute.SUPERSCRIPT_SUPER
141                 );
142         }
143                     
144         if (attrSet.isDefined(StyleConstants.Subscript) && StyleConstants.isSubscript(attrSet))
145         {
146             attrMap.put(
147                 TextAttribute.SUPERSCRIPT,
148                 TextAttribute.SUPERSCRIPT_SUB
149                 );
150         }
151                     
152         return attrMap;
153     }
154
155     @Override
156     public abstract String convert(String srcText);
157     
158 }
159