1
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
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