コード例 #1
0
ファイル: BaseFont.cs プロジェクト: aakkssqq/CreatePDFCSharp
        /** Creates a new font. This font can be one of the 14 built in types,
         * a Type1 font referred by an AFM file, a TrueType font (simple or collection) or a CJK font from the
         * Adobe Asian Font Pack. TrueType fonts and CJK fonts can have an optional style modifier
         * appended to the name. These modifiers are: Bold, Italic and BoldItalic. An
         * example would be "STSong-Light,Bold". Note that this modifiers do not work if
         * the font is embedded. Fonts in TrueType collections are addressed by index such as "msgothic.ttc,1".
         * This would get the second font (indexes start at 0), in this case "MS PGothic".
         * <P>
         * The fonts may or may not be cached depending on the flag <CODE>cached</CODE>.
         * If the <CODE>byte</CODE> arrays are present the font will be
         * read from them instead of the name. The name is still required to identify
         * the font type.
         * @param name the name of the font or it's location on file
         * @param encoding the encoding to be applied to this font
         * @param embedded true if the font is to be embedded in the PDF
         * @param cached true if the font comes from the cache or is added to
         * the cache if new. false if the font is always created new
         * @param ttfAfm the true type font or the afm in a byte array
         * @param pfb the pfb in a byte array
         * @return returns a new font. This font may come from the cache but only if cached
         * is true, otherwise it will always be created new
         * @throws DocumentException the font is invalid
         * @throws IOException the font file could not be read
         */
        public static BaseFont createFont(string name, string encoding, bool embedded, bool cached, byte[] ttfAfm, byte[] pfb)
        {
            string nameBase = getBaseName(name);

            encoding = normalizeEncoding(encoding);
            bool isBuiltinFonts14 = BuiltinFonts14.ContainsKey(name);
            bool isCJKFont        = isBuiltinFonts14 ? false : CJKFont.isCJKFont(nameBase, encoding);

            if (isBuiltinFonts14 || isCJKFont)
            {
                embedded = false;
            }
            else if (encoding.Equals(IDENTITY_H) || encoding.Equals(IDENTITY_V))
            {
                embedded = true;
            }
            BaseFont fontFound = null;
            BaseFont fontBuilt = null;
            string   key       = name + "\n" + encoding + "\n" + embedded;

            if (cached)
            {
                lock (fontCache) {
                    fontFound = (BaseFont)fontCache[key];
                }
                if (fontFound != null)
                {
                    return(fontFound);
                }
            }
            if (isBuiltinFonts14 || name.ToLower().EndsWith(".afm"))
            {
                fontBuilt             = new Type1Font(name, encoding, embedded, ttfAfm, pfb);
                fontBuilt.fastWinansi = encoding.Equals(CP1252);
            }
            else if (nameBase.ToLower().EndsWith(".ttf") || nameBase.ToLower().IndexOf(".ttc,") > 0)
            {
                if (encoding.Equals(IDENTITY_H) || encoding.Equals(IDENTITY_V))
                {
                    fontBuilt = new TrueTypeFontUnicode(name, encoding, embedded, ttfAfm);
                }
                else
                {
                    fontBuilt             = new TrueTypeFont(name, encoding, embedded, ttfAfm);
                    fontBuilt.fastWinansi = encoding.Equals(CP1252);
                }
            }
            else if (nameBase.ToLower().EndsWith(".otf"))
            {
                fontBuilt             = new TrueTypeFont(name, encoding, embedded, ttfAfm);
                fontBuilt.fastWinansi = encoding.Equals(CP1252);
            }
            else if (isCJKFont)
            {
                fontBuilt = new CJKFont(name, encoding, embedded);
            }
            else
            {
                throw new DocumentException("Font '" + name + "' with '" + encoding + "' is not recognized.");
            }
            if (cached)
            {
                lock (fontCache) {
                    fontFound = (BaseFont)fontCache[key];
                    if (fontFound != null)
                    {
                        return(fontFound);
                    }
                    fontCache.Add(key, fontBuilt);
                }
            }
            return(fontBuilt);
        }