001/*
002 * $Id: FontSelector.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 java.util.ArrayList;
047
048import com.itextpdf.text.Chunk;
049import com.itextpdf.text.Font;
050import com.itextpdf.text.Phrase;
051import com.itextpdf.text.Utilities;
052import com.itextpdf.text.error_messages.MessageLocalization;
053
054/** Selects the appropriate fonts that contain the glyphs needed to
055 * render text correctly. The fonts are checked in order until the
056 * character is found.
057 * <p>
058 * The built in fonts "Symbol" and "ZapfDingbats", if used, have a special encoding
059 * to allow the characters to be referred by Unicode.
060 * @author Paulo Soares
061 */
062public class FontSelector {
063
064    protected ArrayList<Font> fonts = new ArrayList<Font>();
065
066    /**
067     * Adds a <CODE>Font</CODE> to be searched for valid characters.
068     * @param font the <CODE>Font</CODE>
069     */
070    public void addFont(Font font) {
071        if (font.getBaseFont() != null) {
072            fonts.add(font);
073            return;
074        }
075        BaseFont bf = font.getCalculatedBaseFont(true);
076        Font f2 = new Font(bf, font.getSize(), font.getCalculatedStyle(), font.getColor());
077        fonts.add(f2);
078    }
079
080    /**
081     * Process the text so that it will render with a combination of fonts
082     * if needed.
083     * @param text the text
084     * @return a <CODE>Phrase</CODE> with one or more chunks
085     */
086    public Phrase process(String text) {
087        int fsize = fonts.size();
088        if (fsize == 0)
089            throw new IndexOutOfBoundsException(MessageLocalization.getComposedMessage("no.font.is.defined"));
090        char cc[] = text.toCharArray();
091        int len = cc.length;
092        StringBuffer sb = new StringBuffer();
093        Font font = null;
094        int lastidx = -1;
095        Phrase ret = new Phrase();
096        for (int k = 0; k < len; ++k) {
097            char c = cc[k];
098            if (c == '\n' || c == '\r') {
099                sb.append(c);
100                continue;
101            }
102            if (Utilities.isSurrogatePair(cc, k)) {
103                int u = Utilities.convertToUtf32(cc, k);
104                for (int f = 0; f < fsize; ++f) {
105                    font = fonts.get(f);
106                    if (font.getBaseFont().charExists(u)) {
107                        if (lastidx != f) {
108                            if (sb.length() > 0 && lastidx != -1) {
109                                Chunk ck = new Chunk(sb.toString(), fonts.get(lastidx));
110                                ret.add(ck);
111                                sb.setLength(0);
112                            }
113                            lastidx = f;
114                        }
115                        sb.append(c);
116                        sb.append(cc[++k]);
117                        break;
118                    }
119                }
120            }
121            else {
122                for (int f = 0; f < fsize; ++f) {
123                    font = fonts.get(f);
124                    if (font.getBaseFont().charExists(c)) {
125                        if (lastidx != f) {
126                            if (sb.length() > 0 && lastidx != -1) {
127                                Chunk ck = new Chunk(sb.toString(), fonts.get(lastidx));
128                                ret.add(ck);
129                                sb.setLength(0);
130                            }
131                            lastidx = f;
132                        }
133                        sb.append(c);
134                        break;
135                    }
136                }
137            }
138        }
139        if (sb.length() > 0) {
140            Chunk ck = new Chunk(sb.toString(), fonts.get(lastidx == -1 ? 0 : lastidx));
141            ret.add(ck);
142        }
143        return ret;
144    }
145}