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 com.lowagie.text.DocumentException;
27 import com.lowagie.text.Phrase;
28 import com.lowagie.text.pdf.ColumnText;
29 import com.lowagie.text.pdf.PdfWriter;
30
31 import net.sf.jasperreports.engine.JRRuntimeException;
32 import net.sf.jasperreports.export.pdf.PdfChunk;
33 import net.sf.jasperreports.export.pdf.PdfPhrase;
34 import net.sf.jasperreports.export.pdf.PdfTextAlignment;
35 import net.sf.jasperreports.export.pdf.TextDirection;
36
37 /**
38  * 
39  * @author Lucian Chirita (lucianc@users.sourceforge.net)
40  */

41 public class ClassicPhrase implements PdfPhrase
42 {
43
44     private ClassicPdfProducer pdfProducer;
45     private Phrase phrase;
46
47     public ClassicPhrase(ClassicPdfProducer pdfProducer, Phrase phrase)
48     {
49         this.pdfProducer = pdfProducer;
50         this.phrase = phrase;
51     }
52
53     @Override
54     public void add(PdfChunk chunk)
55     {
56         phrase.add(((ClassicChunk) chunk).getChunk());
57     }
58
59     @Override
60     public float go(float llx, float lly, float urx, float ury, 
61             float fixedLeading, float multipliedLeading, 
62             PdfTextAlignment alignment, TextDirection runDirection)
63     {
64         ColumnText colText = new ColumnText(pdfProducer.getPdfContentByte());
65         colText.setSimpleColumn(phrase, 
66                 llx, lly, urx, ury, 
67                 fixedLeading, 
68                 ClassicPdfUtils.toITextAlignment(alignment));
69         if (multipliedLeading != 0f)
70         {
71             colText.setLeading(fixedLeading, multipliedLeading);
72         }
73         colText.setRunDirection(toITextRunDirection(runDirection));
74         try
75         {
76             colText.go();
77         }
78         catch (DocumentException e)
79         {
80             throw new JRRuntimeException(e);
81         }
82         return colText.getYLine();
83     }
84     
85     protected static int toITextRunDirection(TextDirection direction)
86     {
87         int iTextDirection;
88         switch (direction)
89         {
90         case DEFAULT:
91             iTextDirection = PdfWriter.RUN_DIRECTION_DEFAULT;
92             break;
93         case LTR:
94             iTextDirection = PdfWriter.RUN_DIRECTION_LTR;
95             break;
96         case RTL:
97             iTextDirection = PdfWriter.RUN_DIRECTION_RTL;
98             break;
99         default:
100             throw new JRRuntimeException("Unknown text direction " + direction);
101         }
102         return iTextDirection;
103     }
104
105 }
106