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.export.pdf.classic;
25
26 import java.awt.Color;
27 import java.io.IOException;
28
29 import com.lowagie.text.DocumentException;
30 import com.lowagie.text.Font;
31 import com.lowagie.text.FontFactory;
32 import com.lowagie.text.pdf.BaseFont;
33
34 import net.sf.jasperreports.engine.JRRuntimeException;
35 import net.sf.jasperreports.export.pdf.FontRecipient;
36 import net.sf.jasperreports.export.pdf.PdfFontStyle;
37
38 /**
39  * 
40  * @author Lucian Chirita (lucianc@users.sourceforge.net)
41  */

42 public class ClassicFontRecipient implements FontRecipient
43 {
44
45     private Font font;
46
47     public ClassicFontRecipient()
48     {
49     }
50     
51     @Override
52     public boolean hasFont()
53     {
54         return font != null;
55     }
56     
57     public Font getFont()
58     {
59         return font;
60     }
61
62     @Override
63     public void setFont(String pdfFontName, String pdfEncoding, boolean isPdfEmbedded, 
64             float size, PdfFontStyle pdfFontStyle, Color forecolor)
65     {
66         Font font = FontFactory.getFont(pdfFontName, pdfEncoding, isPdfEmbedded, 
67                 size, toITextFontStyle(pdfFontStyle), forecolor);
68         // check if FontFactory didn't find the font
69         if (font != null && font.getBaseFont() == null && font.getFamily() == Font.UNDEFINED)
70         {
71             font = null;
72         }
73         this.font = font;
74     }
75
76     @Override
77     public void setFont(String pdfFontName, String pdfEncoding, boolean isPdfEmbedded,
78             float size, PdfFontStyle pdfFontStyle, Color forecolor,
79             byte[] fontData)
80     {
81         BaseFont baseFont;
82         try
83         {
84             baseFont = BaseFont.createFont(pdfFontName, pdfEncoding, isPdfEmbedded,
85                     true, fontData, null);
86         }
87         catch(DocumentException e)
88         {
89             throw new JRRuntimeException(e);
90         }
91         catch(IOException e)
92         {
93             throw new JRRuntimeException(e);
94         }
95
96         font = new Font(baseFont, size, toITextFontStyle(pdfFontStyle), forecolor);
97     }
98     
99     protected static int toITextFontStyle(PdfFontStyle pdfFontStyle)
100     {
101         return (pdfFontStyle.isBold() ? Font.BOLD : 0)
102                 | (pdfFontStyle.isItalic() ? Font.ITALIC : 0)
103                 | (pdfFontStyle.isUnderline() ? Font.UNDERLINE : 0)
104                 | (pdfFontStyle.isStrikethrough() ? Font.STRIKETHRU : 0);
105     }
106     
107 }
108