001/*
002 * $Id: PdfFont.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;
045
046import com.itextpdf.text.ExceptionConverter;
047import com.itextpdf.text.Image;
048
049/**
050 * <CODE>PdfFont</CODE> is the Pdf Font object.
051 * <P>
052 * Limitation: in this class only base 14 Type 1 fonts (courier, courier bold, courier oblique,
053 * courier boldoblique, helvetica, helvetica bold, helvetica oblique, helvetica boldoblique,
054 * symbol, times roman, times bold, times italic, times bolditalic, zapfdingbats) and their
055 * standard encoding (standard, MacRoman, (MacExpert,) WinAnsi) are supported.<BR>
056 * This object is described in the 'Portable Document Format Reference Manual version 1.3'
057 * section 7.7 (page 198-203).
058 *
059 * @see         PdfName
060 * @see         PdfDictionary
061 * @see         BadPdfFormatException
062 */
063
064class PdfFont implements Comparable<PdfFont> {
065
066
067    /** the font metrics. */
068    private BaseFont font;
069
070    /** the size. */
071    private float size;
072
073    /** an image. */
074    protected Image image;
075
076    protected float hScale = 1;
077
078    // constructors
079
080    PdfFont(BaseFont bf, float size) {
081        this.size = size;
082        font = bf;
083    }
084
085    // methods
086
087    /**
088     * Compares this <CODE>PdfFont</CODE> with another
089     *
090     * @param   pdfFont the other <CODE>PdfFont</CODE>
091     * @return  a value
092     */
093
094    public int compareTo(PdfFont pdfFont) {
095        if (image != null)
096            return 0;
097        if (pdfFont == null) {
098            return -1;
099        }
100        try {
101            if (font != pdfFont.font) {
102                return 1;
103            }
104            if (this.size() != pdfFont.size()) {
105                return 2;
106            }
107            return 0;
108        }
109        catch(ClassCastException cce) {
110            return -2;
111        }
112    }
113
114    /**
115     * Returns the size of this font.
116     *
117     * @return          a size
118     */
119
120    float size() {
121        if (image == null)
122            return size;
123        else {
124            return image.getScaledHeight();
125        }
126    }
127
128    /**
129     * Returns the approximative width of 1 character of this font.
130     *
131     * @return          a width in Text Space
132     */
133
134    float width() {
135        return width(' ');
136    }
137
138    /**
139     * Returns the width of a certain character of this font.
140     *
141     * @param           character       a certain character
142     * @return          a width in Text Space
143     */
144
145    float width(int character) {
146        if (image == null)
147            return font.getWidthPoint(character, size) * hScale;
148        else
149            return image.getScaledWidth();
150    }
151
152    float width(String s) {
153        if (image == null)
154            return font.getWidthPoint(s, size) * hScale;
155        else
156            return image.getScaledWidth();
157    }
158
159    BaseFont getFont() {
160        return font;
161    }
162
163    void setImage(Image image) {
164        this.image = image;
165    }
166
167    static PdfFont getDefaultFont() {
168        try {
169            BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, false);
170            return new PdfFont(bf, 12);
171        }
172        catch (Exception ee) {
173            throw new ExceptionConverter(ee);
174        }
175    }
176    void setHorizontalScaling(float hScale) {
177        this.hScale = hScale;
178    }
179    /**
180     * Getter for the horizontal scaling.
181     * @since iText 5.1.0
182     */
183    float getHorizontalScaling() {
184        return hScale;
185    }
186}