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.awt.Font;
27 import java.awt.font.TextAttribute;
28 import java.text.AttributedCharacterIterator.Attribute;
29 import java.util.Map;
30
31 import net.sf.jasperreports.engine.util.JRTextAttribute;
32
33 /**
34  * @author Lucian Chirita (lucianc@users.sourceforge.net)
35  */

36 public class AwtFontAttribute
37 {
38
39     public static AwtFontAttribute fromAttributes(Map<Attribute,Object> attributes)
40     {
41         String family = (String) attributes.get(TextAttribute.FAMILY);
42         FontInfo fontInfo = (FontInfo) attributes.get(JRTextAttribute.FONT_INFO);
43         return new AwtFontAttribute(family, fontInfo);
44     }
45     
46     private final String family;
47     private final FontInfo fontInfo;
48     
49     public AwtFontAttribute(String family, FontInfo fontInfo)
50     {
51         this.family = family;
52         this.fontInfo = fontInfo;
53     }
54
55     public boolean hasAttribute()
56     {
57         return family != null || fontInfo != null;
58     }
59     
60     public String getFamily()
61     {
62         return family;
63     }
64
65     public FontInfo getFontInfo()
66     {
67         return fontInfo;
68     }
69     
70     public void putAttributes(Map<Attribute,Object> attributes)
71     {
72         attributes.put(TextAttribute.FAMILY, family);
73         if (fontInfo != null)
74         {
75             attributes.put(JRTextAttribute.FONT_INFO, fontInfo);
76         }
77     }
78
79     @Override
80     public int hashCode()
81     {
82         int hash = 43;
83         hash = hash*29 + family.hashCode();
84         if (fontInfo != null)
85         {
86             hash = hash*29 + fontInfo.getStyle();
87             if (fontInfo.getFontFace() != null && fontInfo.getFontFace().getFont() != null)
88             {
89                 hash = hash*29 + fontInfo.getFontFace().getFont().hashCode();
90             }
91         }
92         return hash;
93     }
94
95     @Override
96     public boolean equals(Object obj)
97     {
98         if (!(obj instanceof AwtFontAttribute))
99         {
100             return false;
101         }
102         
103         AwtFontAttribute other = (AwtFontAttribute) obj;
104         if (!family.equals(other.family))
105         {
106             return false;
107         }
108         
109         if (fontInfo == null)
110         {
111             return other.fontInfo == null;
112         }
113         
114         if (other.fontInfo == null)
115         {
116             return false;
117         }
118         
119         if (fontInfo.getStyle() != other.fontInfo.getStyle())
120         {
121             return false;
122         }
123         
124         Font font = fontInfo.getFontFace() == null ? null : fontInfo.getFontFace().getFont();
125         Font otherFont = other.fontInfo.getFontFace() == null ? null : other.fontInfo.getFontFace().getFont();
126         return font == null ? otherFont == null : (otherFont != null && font.equals(otherFont));
127     }
128
129     @Override
130     public String toString()
131     {
132         StringBuilder string = new StringBuilder();
133         string.append("{family: ");
134         string.append(family);
135         if (fontInfo != null)
136         {
137             string.append(", style: ");
138             string.append(fontInfo.getStyle());
139             
140             if (fontInfo.getFontFace() != null && fontInfo.getFontFace().getFont() != null)
141             {
142                 string.append(", font: ");
143                 string.append(fontInfo.getFontFace().getFont());
144             }
145         }
146         string.append("}");
147         return string.toString();
148     }
149     
150 }
151