1 /* -*- Mode: Java; tab-width: 4; c-basic-offset: 4 -*- */
2 /*
3  * Copyright 2002 Paulo Soares
4  *
5  *
6  * The Original Code is 'iText, a free JAVA-PDF library'.
7  *
8  * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
9  * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
10  * All Rights Reserved.
11  * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
12  * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
13  *
14  * Contributor(s): all the names of the contributors are added in the source code
15  * where applicable.
16  *
17  *
18  * This library is free software; you can redistribute it and/or
19  * modify it under the terms of the GNU Library General Public
20  * License as published by the Free Software Foundation; either
21  * version 2 of the License, or (at your option) any later version.
22  *
23  * This library is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
26  * Library General Public License for more details.
27  *
28  * You should have received a copy of the GNU Library General Public
29  * License along with this library; if not, write to the
30  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
31  * Boston, MA  02110-1301, USA.
32  *
33  *
34  * This library is free software; you can redistribute it and/or
35  * modify it under the terms of the GNU Library General Public
36  * License as published by the Free Software Foundation; either
37  * version 2 of the License, or (at your option) any later version.
38  *
39  * This library is distributed in the hope that it will be useful,
40  * but WITHOUT ANY WARRANTY; without even the implied warranty of
41  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
42  * Library General Public License for more details.
43  *
44  * You should have received a copy of the GNU Library General Public
45  * License along with this library; if not, write to the
46  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
47  * Boston, MA  02110-1301, USA.
48  *
49  *
50  * If you didn't download this code from the following link, you should check if
51  * you aren't using an obsolete version:
52  * http://www.lowagie.com/iText/
53  */
54 
55 package com.gitlab.pdftk_java.com.lowagie.text.pdf;
56 import com.gitlab.pdftk_java.com.lowagie.text.Rectangle;
57 import java.util.HashMap;
58 
59 /**
60  * Implements the appearance stream to be used with form fields..
61  */
62 
63 public class PdfAppearance extends PdfTemplate {
64 
65     public static final HashMap stdFieldFontNames = new HashMap();
66     static {
67         stdFieldFontNames.put("Courier-BoldOblique", new PdfName("CoBO"));
68         stdFieldFontNames.put("Courier-Bold", new PdfName("CoBo"));
69         stdFieldFontNames.put("Courier-Oblique", new PdfName("CoOb"));
70         stdFieldFontNames.put("Courier", new PdfName("Cour"));
71         stdFieldFontNames.put("Helvetica-BoldOblique", new PdfName("HeBO"));
72         stdFieldFontNames.put("Helvetica-Bold", new PdfName("HeBo"));
73         stdFieldFontNames.put("Helvetica-Oblique", new PdfName("HeOb"));
74         stdFieldFontNames.put("Helvetica", new PdfName("Helv"));
75         stdFieldFontNames.put("Symbol", new PdfName("Symb"));
76         stdFieldFontNames.put("Times-BoldItalic", new PdfName("TiBI"));
77         stdFieldFontNames.put("Times-Bold", new PdfName("TiBo"));
78         stdFieldFontNames.put("Times-Italic", new PdfName("TiIt"));
79         stdFieldFontNames.put("Times-Roman", new PdfName("TiRo"));
80         stdFieldFontNames.put("ZapfDingbats", new PdfName("ZaDb"));
81         stdFieldFontNames.put("HYSMyeongJo-Medium", new PdfName("HySm"));
82         stdFieldFontNames.put("HYGoThic-Medium", new PdfName("HyGo"));
83         stdFieldFontNames.put("HeiseiKakuGo-W5", new PdfName("KaGo"));
84         stdFieldFontNames.put("HeiseiMin-W3", new PdfName("KaMi"));
85         stdFieldFontNames.put("MHei-Medium", new PdfName("MHei"));
86         stdFieldFontNames.put("MSung-Light", new PdfName("MSun"));
87         stdFieldFontNames.put("STSong-Light", new PdfName("STSo"));
88         stdFieldFontNames.put("MSungStd-Light", new PdfName("MSun"));
89         stdFieldFontNames.put("STSongStd-Light", new PdfName("STSo"));
90         stdFieldFontNames.put("HYSMyeongJoStd-Medium", new PdfName("HySm"));
91         stdFieldFontNames.put("KozMinPro-Regular", new PdfName("KaMi"));
92     }
93 
94     /**
95      *Creates a <CODE>PdfAppearance</CODE>.
96      */
97 
PdfAppearance()98     PdfAppearance() {
99         super();
100         separator = ' ';
101     }
102 
PdfAppearance(PdfIndirectReference iref)103     PdfAppearance(PdfIndirectReference iref) {
104         thisReference = iref;
105     }
106 
107     /**
108      * Creates new PdfTemplate
109      *
110      * @param wr the <CODE>PdfWriter</CODE>
111      */
112 
PdfAppearance(PdfWriter wr)113     PdfAppearance(PdfWriter wr) {
114         super(wr);
115         separator = ' ';
116     }
117 
118     /**
119      * Set the font and the size for the subsequent text writing.
120      *
121      * @param bf the font
122      * @param size the font size in points
123      */
setFontAndSize(BaseFont bf, float size)124     public void setFontAndSize(BaseFont bf, float size) {
125         checkWriter();
126         state.size = size;
127         if (bf.getFontType() == BaseFont.FONT_TYPE_DOCUMENT) {
128             state.fontDetails = new FontDetails(null, ((DocumentFont)bf).getIndirectReference(), bf);
129         }
130         else
131             state.fontDetails = writer.addSimple(bf);
132         PdfName psn = (PdfName)stdFieldFontNames.get(bf.getPostscriptFontName());
133         if (psn == null) {
134             psn = new PdfName(bf.getPostscriptFontName());
135             bf.setSubset(false);
136         }
137         PageResources prs = getPageResources();
138 //        PdfName name = state.fontDetails.getFontName();
139         prs.addFont(psn, state.fontDetails.getIndirectReference());
140         content.append(psn.getBytes()).append(' ').append(size).append(" Tf").append_i(separator);
141     }
142 
getDuplicate()143     public PdfContentByte getDuplicate() {
144         PdfAppearance tpl = new PdfAppearance();
145         tpl.writer = writer;
146         tpl.pdf = pdf;
147         tpl.thisReference = thisReference;
148         tpl.pageResources = pageResources;
149         tpl.bBox = new Rectangle(bBox);
150         tpl.group = group;
151         tpl.layer = layer;
152         if (matrix != null) {
153             tpl.matrix = new PdfArray(matrix);
154         }
155         tpl.separator = separator;
156         return tpl;
157     }
158 }