001/*
002 * $Id: PdfAppearance.java 4784 2011-03-15 08:33:00Z blowagie $
003 *
004 * This file is part of the iText (R) project.
005 * Copyright (c) 1998-2011 1T3XT BVBA
006 * Authors: Bruno Lowagie, Paulo Soares, et al.
007 *
008 * This program is free software; you can redistribute it and/or modify
009 * it under the terms of the GNU Affero General Public License version 3
010 * as published by the Free Software Foundation with the addition of the
011 * following permission added to Section 15 as permitted in Section 7(a):
012 * FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY 1T3XT,
013 * 1T3XT DISCLAIMS THE WARRANTY OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
014 *
015 * This program is distributed in the hope that it will be useful, but
016 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
017 * or FITNESS FOR A PARTICULAR PURPOSE.
018 * See the GNU Affero General Public License for more details.
019 * You should have received a copy of the GNU Affero General Public License
020 * along with this program; if not, see http://www.gnu.org/licenses or write to
021 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
022 * Boston, MA, 02110-1301 USA, or download the license from the following URL:
023 * http://itextpdf.com/terms-of-use/
024 *
025 * The interactive user interfaces in modified source and object code versions
026 * of this program must display Appropriate Legal Notices, as required under
027 * Section 5 of the GNU Affero General Public License.
028 *
029 * In accordance with Section 7(b) of the GNU Affero General Public License,
030 * a covered work must retain the producer line in every PDF that is created
031 * or manipulated using iText.
032 *
033 * You can be released from the requirements of the license by purchasing
034 * a commercial license. Buying such a license is mandatory as soon as you
035 * develop commercial activities involving the iText software without
036 * disclosing the source code of your own applications.
037 * These activities include: offering paid services to customers as an ASP,
038 * serving PDFs on the fly in a web application, shipping iText with a closed
039 * source product.
040 *
041 * For more information, please contact iText Software Corp. at this
042 * address: sales@itextpdf.com
043 */
044package com.itextpdf.text.pdf;
045import java.util.HashMap;
046
047import com.itextpdf.text.Rectangle;
048
049/**
050 * Implements the appearance stream to be used with form fields..
051 */
052
053public class PdfAppearance extends PdfTemplate {
054
055    public static final HashMap<String, PdfName> stdFieldFontNames = new HashMap<String, PdfName>();
056    static {
057        stdFieldFontNames.put("Courier-BoldOblique", new PdfName("CoBO"));
058        stdFieldFontNames.put("Courier-Bold", new PdfName("CoBo"));
059        stdFieldFontNames.put("Courier-Oblique", new PdfName("CoOb"));
060        stdFieldFontNames.put("Courier", new PdfName("Cour"));
061        stdFieldFontNames.put("Helvetica-BoldOblique", new PdfName("HeBO"));
062        stdFieldFontNames.put("Helvetica-Bold", new PdfName("HeBo"));
063        stdFieldFontNames.put("Helvetica-Oblique", new PdfName("HeOb"));
064        stdFieldFontNames.put("Helvetica", PdfName.HELV);
065        stdFieldFontNames.put("Symbol", new PdfName("Symb"));
066        stdFieldFontNames.put("Times-BoldItalic", new PdfName("TiBI"));
067        stdFieldFontNames.put("Times-Bold", new PdfName("TiBo"));
068        stdFieldFontNames.put("Times-Italic", new PdfName("TiIt"));
069        stdFieldFontNames.put("Times-Roman", new PdfName("TiRo"));
070        stdFieldFontNames.put("ZapfDingbats", PdfName.ZADB);
071        stdFieldFontNames.put("HYSMyeongJo-Medium", new PdfName("HySm"));
072        stdFieldFontNames.put("HYGoThic-Medium", new PdfName("HyGo"));
073        stdFieldFontNames.put("HeiseiKakuGo-W5", new PdfName("KaGo"));
074        stdFieldFontNames.put("HeiseiMin-W3", new PdfName("KaMi"));
075        stdFieldFontNames.put("MHei-Medium", new PdfName("MHei"));
076        stdFieldFontNames.put("MSung-Light", new PdfName("MSun"));
077        stdFieldFontNames.put("STSong-Light", new PdfName("STSo"));
078        stdFieldFontNames.put("MSungStd-Light", new PdfName("MSun"));
079        stdFieldFontNames.put("STSongStd-Light", new PdfName("STSo"));
080        stdFieldFontNames.put("HYSMyeongJoStd-Medium", new PdfName("HySm"));
081        stdFieldFontNames.put("KozMinPro-Regular", new PdfName("KaMi"));
082    }
083
084    /**
085     *Creates a <CODE>PdfAppearance</CODE>.
086     */
087
088    PdfAppearance() {
089        super();
090        separator = ' ';
091    }
092
093    PdfAppearance(PdfIndirectReference iref) {
094        thisReference = iref;
095    }
096
097    /**
098     * Creates new PdfTemplate
099     *
100     * @param wr the <CODE>PdfWriter</CODE>
101     */
102
103    PdfAppearance(PdfWriter wr) {
104        super(wr);
105        separator = ' ';
106    }
107
108    /**
109     * Creates a new appearance to be used with form fields.
110     *
111     * @param writer the PdfWriter to use
112     * @param width the bounding box width
113     * @param height the bounding box height
114     * @return the appearance created
115     */
116    public static PdfAppearance createAppearance(PdfWriter writer, float width, float height) {
117        return createAppearance(writer, width, height, null);
118    }
119
120    static PdfAppearance createAppearance(PdfWriter writer, float width, float height, PdfName forcedName) {
121        PdfAppearance template = new PdfAppearance(writer);
122        template.setWidth(width);
123        template.setHeight(height);
124        writer.addDirectTemplateSimple(template, forcedName);
125        return template;
126    }
127
128    /**
129     * Set the font and the size for the subsequent text writing.
130     *
131     * @param bf the font
132     * @param size the font size in points
133     */
134    @Override
135    public void setFontAndSize(BaseFont bf, float size) {
136        checkWriter();
137        state.size = size;
138        if (bf.getFontType() == BaseFont.FONT_TYPE_DOCUMENT) {
139            state.fontDetails = new FontDetails(null, ((DocumentFont)bf).getIndirectReference(), bf);
140        }
141        else
142            state.fontDetails = writer.addSimple(bf);
143        PdfName psn = stdFieldFontNames.get(bf.getPostscriptFontName());
144        if (psn == null) {
145            if (bf.isSubset() && bf.getFontType() == BaseFont.FONT_TYPE_TTUNI)
146                psn = state.fontDetails.getFontName();
147            else {
148                psn = new PdfName(bf.getPostscriptFontName());
149                state.fontDetails.setSubset(false);
150            }
151        }
152        PageResources prs = getPageResources();
153//        PdfName name = state.fontDetails.getFontName();
154        prs.addFont(psn, state.fontDetails.getIndirectReference());
155        content.append(psn.getBytes()).append(' ').append(size).append(" Tf").append_i(separator);
156    }
157
158    @Override
159    public PdfContentByte getDuplicate() {
160        PdfAppearance tpl = new PdfAppearance();
161        tpl.writer = writer;
162        tpl.pdf = pdf;
163        tpl.thisReference = thisReference;
164        tpl.pageResources = pageResources;
165        tpl.bBox = new Rectangle(bBox);
166        tpl.group = group;
167        tpl.layer = layer;
168        if (matrix != null) {
169            tpl.matrix = new PdfArray(matrix);
170        }
171        tpl.separator = separator;
172        return tpl;
173    }
174}