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.base;
25
26 import java.awt.font.TextAttribute;
27 import java.io.IOException;
28 import java.io.ObjectInputStream;
29 import java.io.Serializable;
30 import java.text.AttributedCharacterIterator.Attribute;
31 import java.util.Map;
32
33 import net.sf.jasperreports.engine.JRAbstractObjectFactory;
34 import net.sf.jasperreports.engine.JRCloneable;
35 import net.sf.jasperreports.engine.JRConstants;
36 import net.sf.jasperreports.engine.JRDefaultStyleProvider;
37 import net.sf.jasperreports.engine.JRFont;
38 import net.sf.jasperreports.engine.JRRuntimeException;
39 import net.sf.jasperreports.engine.JRStyle;
40 import net.sf.jasperreports.engine.JRStyleContainer;
41 import net.sf.jasperreports.engine.design.events.JRChangeEventsSupport;
42 import net.sf.jasperreports.engine.design.events.JRPropertyChangeSupport;
43 import net.sf.jasperreports.engine.util.JRTextAttribute;
44 import net.sf.jasperreports.engine.util.StyleResolver;
45
46
47 /**
48  * @author Teodor Danciu (teodord@users.sourceforge.net)
49  */

50 public class JRBaseFont implements JRFont, Serializable, JRChangeEventsSupport, JRCloneable
51 {
52
53
54     /**
55      *
56      */

57     private static final long serialVersionUID = JRConstants.SERIAL_VERSION_UID;
58     
59     public static final String PROPERTY_BOLD = "isBold";
60     
61     public static final String PROPERTY_FONT_NAME = "fontName";
62     
63     public static final String PROPERTY_FONT_SIZE = "fontSize";
64     
65     public static final String PROPERTY_ITALIC = "isItalic";
66     
67     public static final String PROPERTY_PDF_EMBEDDED = "isPdfEmbedded";
68     
69     public static final String PROPERTY_PDF_ENCODING = "pdfEncoding";
70     
71     public static final String PROPERTY_PDF_FONT_NAME = "pdfFontName";
72     
73     public static final String PROPERTY_REPORT_FONT = "reportFont";
74     
75     public static final String PROPERTY_STRIKE_THROUGH = "isStrikeThrough";
76     
77     public static final String PROPERTY_UNDERLINE = "isUnderline";
78
79     /**
80      *
81      */

82     protected JRStyleContainer styleContainer;
83     protected JRStyle style;
84     protected String styleNameReference;
85
86     protected String fontName;
87     protected Boolean isBold;
88     protected Boolean isItalic;
89     protected Boolean isUnderline;
90     protected Boolean isStrikeThrough;
91     protected Float fontsize;
92     protected String pdfFontName;
93     protected String pdfEncoding;
94     protected Boolean isPdfEmbedded;
95
96
97     /**
98      *
99      */

100     public JRBaseFont()
101     {
102     }
103         
104
105     /**
106      *
107      */

108     public JRBaseFont(Map<Attribute,Object> attributes)
109     {
110         String fontNameAttr = (String)attributes.get(TextAttribute.FAMILY);
111         if (fontNameAttr != null)
112         {
113             setFontName(fontNameAttr);
114         }
115         
116         Object bold = attributes.get(TextAttribute.WEIGHT);
117         if (bold != null)
118         {
119             setBold(TextAttribute.WEIGHT_BOLD.equals(bold));
120         }
121
122         Object italic = attributes.get(TextAttribute.POSTURE);
123         if (italic != null)
124         {
125             setItalic(TextAttribute.POSTURE_OBLIQUE.equals(italic));
126         }
127
128         Float sizeAttr = (Float)attributes.get(TextAttribute.SIZE);
129         if (sizeAttr != null)
130         {
131             setFontSize(sizeAttr);
132         }
133         
134         Object underline = attributes.get(TextAttribute.UNDERLINE);
135         if (underline != null)
136         {
137             setUnderline(TextAttribute.UNDERLINE_ON.equals(underline));
138         }
139
140         Object strikeThrough = attributes.get(TextAttribute.STRIKETHROUGH);
141         if (strikeThrough != null)
142         {
143             setStrikeThrough(TextAttribute.STRIKETHROUGH_ON.equals(strikeThrough));
144         }
145
146         String pdfFontNameAttr = (String)attributes.get(JRTextAttribute.PDF_FONT_NAME);
147         if (pdfFontNameAttr != null)
148         {
149             setPdfFontName(pdfFontNameAttr);
150         }
151         
152         String pdfEncodingAttr = (String)attributes.get(JRTextAttribute.PDF_ENCODING);
153         if (pdfEncodingAttr != null)
154         {
155             setPdfEncoding(pdfEncodingAttr);
156         }
157         
158         Boolean isPdfEmbeddedAttr = (Boolean)attributes.get(JRTextAttribute.IS_PDF_EMBEDDED);
159         if (isPdfEmbeddedAttr != null)
160         {
161             setPdfEmbedded(isPdfEmbeddedAttr);
162         }
163     }
164         
165
166     /**
167      * 
168      */

169     public JRBaseFont(JRStyleContainer styleContainer)
170     {
171         this.styleContainer = styleContainer;
172     }
173         
174
175     /**
176      *
177      */

178     public JRBaseFont(
179         JRStyleContainer styleContainer,
180         JRFont font
181         ) // constructor used in chart themes
182     {
183         this(styleContainer);
184         
185         if (font != null)
186         {
187             fontName = font.getOwnFontName();
188             isBold = font.isOwnBold();
189             isItalic = font.isOwnItalic();
190             isUnderline = font.isOwnUnderline();
191             isStrikeThrough = font.isOwnStrikeThrough();
192             fontsize = font.getOwnFontsize();
193             pdfFontName = font.getOwnPdfFontName();
194             pdfEncoding = font.getOwnPdfEncoding();
195             isPdfEmbedded = font.isOwnPdfEmbedded();
196         }
197     }
198         
199
200     /**
201      *
202      */

203     public JRBaseFont(JRStyleContainer styleContainer, JRFont font, JRAbstractObjectFactory factory)
204     {
205         factory.put(font, this);
206
207         this.styleContainer = styleContainer;
208
209         style = factory.getStyle(font.getStyle());
210         styleNameReference = font.getStyleNameReference();
211
212         fontName = font.getOwnFontName();
213         isBold = font.isOwnBold();
214         isItalic = font.isOwnItalic();
215         isUnderline = font.isOwnUnderline();
216         isStrikeThrough = font.isOwnStrikeThrough();
217         fontsize = font.getOwnFontsize();
218         pdfFontName = font.getOwnPdfFontName();
219         pdfEncoding = font.getOwnPdfEncoding();
220         isPdfEmbedded = font.isOwnPdfEmbedded();
221     }
222
223     
224     @Override
225     public JRDefaultStyleProvider getDefaultStyleProvider()
226     {
227         return styleContainer == null ? null : styleContainer.getDefaultStyleProvider();
228     }
229
230     /**
231      *
232      */

233     protected StyleResolver getStyleResolver() 
234     {
235         if (getDefaultStyleProvider() != null)
236         {
237             return getDefaultStyleProvider().getStyleResolver();
238         }
239         return StyleResolver.getInstance();
240     }
241     
242     @Override
243     public JRStyle getStyle()
244     {
245         return style == null ? (styleContainer == null ? null : styleContainer.getStyle()) : style;
246     }
247
248     @Override
249     public String getStyleNameReference()
250     {
251         return styleNameReference == null ? (styleContainer == null ? null : styleContainer.getStyleNameReference()) : styleNameReference;
252     }
253
254     @Override
255     public String getFontName()
256     {
257         return getStyleResolver().getFontName(this);
258     }
259     
260     @Override
261     public String getOwnFontName()
262     {
263         return fontName;
264     }
265     
266     @Override
267     public void setFontName(String fontName)
268     {
269         Object old = this.fontName;
270         this.fontName = fontName;
271         getEventSupport().firePropertyChange(PROPERTY_FONT_NAME, old, this.fontName);
272     }
273     
274
275     @Override
276     public boolean isBold()
277     {
278         return getStyleResolver().isBold(this);
279     }
280     
281     @Override
282     public Boolean isOwnBold()
283     {
284         return isBold;
285     }
286     
287     /**
288      * @deprecated Replaced by {@link #setBold(Boolean)}.
289      */

290     @Override
291     public void setBold(boolean isBold)
292     {
293         setBold((Boolean)isBold);
294     }
295
296     /**
297      * Alternative setBold method which allows also to reset
298      * the "own" isBold property.
299      */

300     @Override
301     public void setBold(Boolean isBold)
302     {
303         Object old = this.isBold;
304         this.isBold = isBold;
305         getEventSupport().firePropertyChange(PROPERTY_BOLD, old, this.isBold);
306     }
307
308     
309     @Override
310     public boolean isItalic()
311     {
312         return getStyleResolver().isItalic(this);
313     }
314     
315     @Override
316     public Boolean isOwnItalic()
317     {
318         return isItalic;
319     }
320     
321     /**
322      * @deprecated Replaced by {@link #setItalic(Boolean)}.
323      */

324     @Override
325     public void setItalic(boolean isItalic)
326     {
327         setItalic((Boolean)isItalic);
328     }
329     
330     /**
331      * Alternative setItalic method which allows also to reset
332      * the "own" isItalic property.
333      */

334     @Override
335     public void setItalic(Boolean isItalic) 
336     {
337         Object old = this.isItalic;
338         this.isItalic = isItalic;
339         getEventSupport().firePropertyChange(PROPERTY_ITALIC, old, this.isItalic);
340     }
341     
342     @Override
343     public boolean isUnderline()
344     {
345         return getStyleResolver().isUnderline(this);
346     }
347     
348     @Override
349     public Boolean isOwnUnderline()
350     {
351         return isUnderline;
352     }
353     
354     /**
355      * @deprecated Replaced by {@link #setUnderline(Boolean)}.
356      */

357     @Override
358     public void setUnderline(boolean isUnderline)
359     {
360         setUnderline((Boolean)isUnderline);
361     }
362     
363     /**
364      * Alternative setUnderline method which allows also to reset
365      * the "own" isUnderline property.
366      */

367     @Override
368     public void setUnderline(Boolean isUnderline) 
369     {
370         Object old = this.isUnderline;
371         this.isUnderline = isUnderline;
372         getEventSupport().firePropertyChange(PROPERTY_UNDERLINE, old, this.isUnderline);
373     }
374
375     @Override
376     public boolean isStrikeThrough()
377     {
378         return getStyleResolver().isStrikeThrough(this);
379     }
380     
381     @Override
382     public Boolean isOwnStrikeThrough()
383     {
384         return isStrikeThrough;
385     }
386     
387     /**
388      * @deprecated Replaced by {@link #setStrikeThrough(Boolean)}.
389      */

390     @Override
391     public void setStrikeThrough(boolean isStrikeThrough)
392     {
393         setStrikeThrough((Boolean)isStrikeThrough);
394     }
395
396     /**
397      * Alternative setStrikeThrough method which allows also to reset
398      * the "own" isStrikeThrough property.
399      */

400     @Override
401     public void setStrikeThrough(Boolean isStrikeThrough) 
402     {
403         Object old = this.isStrikeThrough;
404         this.isStrikeThrough = isStrikeThrough;
405         getEventSupport().firePropertyChange(PROPERTY_STRIKE_THROUGH, old, this.isStrikeThrough);
406     }
407
408     @Override
409     public float getFontsize()
410     {
411         return getStyleResolver().getFontsize(this);
412     }
413     
414     @Override
415     public Float getOwnFontsize()
416     {
417         return fontsize;
418     }
419     
420     /**
421      * Method which allows also to reset the "own" size property.
422      */

423     @Override
424     public void setFontSize(Float fontSize) 
425     {
426         Object old = this.fontsize;
427         this.fontsize = fontSize;
428         getEventSupport().firePropertyChange(PROPERTY_FONT_SIZE, old, this.fontsize);
429     }
430
431     @Override
432     public String getPdfFontName()
433     {
434         return getStyleResolver().getPdfFontName(this);
435     }
436
437     @Override
438     public String getOwnPdfFontName()
439     {
440         return pdfFontName;
441     }
442     
443     @Override
444     public void setPdfFontName(String pdfFontName)
445     {
446         Object old = this.pdfFontName;
447         this.pdfFontName = pdfFontName;
448         getEventSupport().firePropertyChange(PROPERTY_PDF_FONT_NAME, old, this.pdfFontName);
449     }
450
451     
452     @Override
453     public String getPdfEncoding()
454     {
455         return getStyleResolver().getPdfEncoding(this);
456     }
457     
458     @Override
459     public String getOwnPdfEncoding()
460     {
461         return pdfEncoding;
462     }
463     
464     @Override
465     public void setPdfEncoding(String pdfEncoding)
466     {
467         Object old = this.pdfEncoding;
468         this.pdfEncoding = pdfEncoding;
469         getEventSupport().firePropertyChange(PROPERTY_PDF_ENCODING, old, this.pdfEncoding);
470     }
471
472
473     @Override
474     public boolean isPdfEmbedded()
475     {
476         return getStyleResolver().isPdfEmbedded(this);
477     }
478
479     @Override
480     public Boolean isOwnPdfEmbedded()
481     {
482         return isPdfEmbedded;
483     }
484     
485     /**
486      * @deprecated Replaced by {@link #setPdfEmbedded(Boolean)}.
487      */

488     @Override
489     public void setPdfEmbedded(boolean isPdfEmbedded)
490     {
491         setPdfEmbedded((Boolean)isPdfEmbedded);
492     }
493     
494     /**
495      * Alternative setPdfEmbedded method which allows also to reset
496      * the "own" isPdfEmbedded property.
497      */

498     @Override
499     public void setPdfEmbedded(Boolean isPdfEmbedded) 
500     {
501         Object old = this.isPdfEmbedded;
502         this.isPdfEmbedded = isPdfEmbedded;
503         getEventSupport().firePropertyChange(PROPERTY_PDF_EMBEDDED, old, this.isPdfEmbedded);
504     }
505
506     @Override
507     public Object clone()
508     {
509         JRBaseFont clone = null;
510         
511         try
512         {
513             clone = (JRBaseFont)super.clone();
514         }
515         catch (CloneNotSupportedException e)
516         {
517             throw new JRRuntimeException(e);
518         }
519         
520         clone.eventSupport = null;
521         
522         return clone;
523     }
524     
525     private transient JRPropertyChangeSupport eventSupport;
526     
527     @Override
528     public JRPropertyChangeSupport getEventSupport()
529     {
530         synchronized (this)
531         {
532             if (eventSupport == null)
533             {
534                 eventSupport = new JRPropertyChangeSupport(this);
535             }
536         }
537         
538         return eventSupport;
539     }
540
541     
542     /*
543      * These fields are only for serialization backward compatibility.
544      */

545     private int PSEUDO_SERIAL_VERSION_UID = JRConstants.PSEUDO_SERIAL_VERSION_UID; //NOPMD
546     /**
547      * @deprecated
548      */

549     private Integer fontSize;
550     
551     private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException
552     {
553         in.defaultReadObject();
554         
555         if (PSEUDO_SERIAL_VERSION_UID < JRConstants.PSEUDO_SERIAL_VERSION_UID_5_5_2)
556         {
557             fontsize = fontSize == null ? null : fontSize.floatValue();
558             
559             fontSize = null;
560         }
561     }
562
563 }
564