示例#1
0
        public static Altsoft.PDFO.Font EmbedFont(Document doc, Stream fontstream, string encoding, bool embedFontFile, bool isVertical)
        {
            int      num2;
            PDFArray array2;
            int      num10;

            Altsoft.FTMB.Font font1 = Altsoft.FTMB.Font.OpenFont(fontstream);
            if ((font1.FontType != FontType.TrueTypeFontT) || (encoding != "Unicode"))
            {
                return(null);
            }
            TrueTypeFont font2 = (font1 as TrueTypeFont);
            double       num1  = ((double)font2.UnitsPerEm);
            PDFDict      dict1 = Library.CreateDict();

            dict1["Type"]     = Library.CreateName("Font");
            dict1["Subtype"]  = Library.CreateName("CIDFontType2");
            dict1["BaseFont"] = Library.CreateName(font2.FontName);
            PDFDict dict2 = Library.CreateDict();

            dict2["Registry"]      = Library.CreateString("Adobe");
            dict2["Ordering"]      = Library.CreateString("Identity");
            dict2["Supplement"]    = Library.CreateInteger(((long)0));
            dict1["CIDSystemInfo"] = dict2;
            TTEncoding encoding1 = null;

            for (num2 = 0; ((num2 < font2.Encodings.Count) && (encoding1 == null)); num2 += 1)
            {
                encoding1 = ((TTEncoding)font2.Encodings[num2]);
                if ((encoding1.platformID != 3) || ((encoding1.specificID != 1) && (encoding1.specificID != 0)))
                {
                    encoding1 = null;
                }
            }
            if (encoding1 == null)
            {
                return(null);
            }
            dict1["DW"] = Library.CreateInteger(((long)((((double)font2.GetWidth(encoding1.GetGID(0))) * 1000f) / num1)));
            int      num3   = 0;
            int      num4   = encoding1.GetLastCharCode(ref num3);
            int      num5   = encoding1.GetFirstCharCode(ref num3);
            PDFArray array1 = Library.CreateArray(0);

            if (num5 != 0)
            {
                array1.Add(Library.CreateInteger(((long)0)));
                array2 = Library.CreateArray(0);
                array2.Add(Library.CreateInteger(((long)((((double)font2.GetWidth(0, 3, 1)) * 1000f) / num1))));
                array1.Add(array2);
            }
            array1.Add(Library.CreateInteger(((long)num5)));
            array2 = Library.CreateArray(0);
            array2.Add(Library.CreateInteger(((long)((((double)font2.GetWidth(num3)) * 1000f) / num1))));
            int num6 = num5;

            while ((num5 != num4))
            {
                num5 = encoding1.GetNextCharCode(num5, ref num3);
                if (num5 != (num6 + 1))
                {
                    array1.Add(array2);
                    array1.Add(Library.CreateInteger(((long)num5)));
                    array2 = Library.CreateArray(0);
                }
                num6 = num5;
                array2.Add(Library.CreateInteger(((long)((((double)font2.GetWidth(num3)) * 1000f) / num1))));
            }
            array1.Add(array2);
            dict1["W"] = array1;
            Stream stream1 = new MemoryStream();
            int    num7    = encoding1.GetFirstCharCode(ref num3);
            int    num8    = 0;

            while ((num8 < num7))
            {
                stream1.WriteByte(0);
                stream1.WriteByte(0);
                num8 += 1;
            }
            stream1.WriteByte(((byte)(num3 / 256)));
            stream1.WriteByte(((byte)(num3 % 256)));
            num8 += 1;
            int num9 = encoding1.GetLastCharCode(ref num3);

            do
            {
                num7 = encoding1.GetNextCharCode(num7, ref num3);
                while ((num8 < num7))
                {
                    stream1.WriteByte(0);
                    stream1.WriteByte(0);
                    num8 += 1;
                }
                stream1.WriteByte(((byte)(num3 / 256)));
                stream1.WriteByte(((byte)(num3 % 256)));
                num8 += 1;
            }while ((num7 != num9));
            stream1.Position = ((long)0);
            PDFDict dict3 = Library.CreateDict();

            dict3["Filter"] = Library.CreateName("FlateDecode");
            PDFStream stream2 = Library.CreateStream(stream1, dict3);

            dict1["CIDToGIDMap"] = doc.Indirects.New(stream2);
            PDFDict dict4 = Library.CreateDict();

            dict4["Type"]     = Library.CreateName("FontDescriptor");
            dict4["FontName"] = Library.CreateName(font2.FontName);
            dict4["Flags"]    = Library.CreateInteger(((long)4));
            PDFArray array3 = Library.CreateArray(4);

            for (num10 = 0; (num10 < 4); num10 += 1)
            {
                array3[num10] = Library.CreateInteger(((long)font2.FontBBox[num10]));
            }
            dict4["FontBBox"]    = array3;
            dict4["ItalicAngle"] = Library.CreateFixed(font2.ItalicAngle);
            dict4["Ascent"]      = Library.CreateInteger(((long)font2.FontBBox[3]));
            dict4["Descent"]     = Library.CreateInteger(((long)font2.FontBBox[1]));
            dict4["CapHeight"]   = Library.CreateInteger(((long)font2.FontBBox[3]));
            dict4["StemV"]       = Library.CreateInteger(((long)0));
            if (((font2.EmbeddingRights == 0) || ((font2.EmbeddingRights & 8) != 0)) && embedFontFile)
            {
                font2.FontStream.Position = ((long)0);
                dict3              = Library.CreateDict();
                dict3["Filter"]    = Library.CreateName("FlateDecode");
                dict3["Length1"]   = Library.CreateInteger(font2.FontStream.Length);
                dict4["FontFile2"] = doc.Indirects.New(Library.CreateStream(font2.FontStream, dict3));
            }
            dict1["FontDescriptor"] = doc.Indirects.New(dict4);
            PDFDict dict5 = Library.CreateDict();

            dict5["Type"]     = Library.CreateName("Font");
            dict5["Subtype"]  = Library.CreateName("Type0");
            dict5["BaseFont"] = Library.CreateName(font2.FontName);
            if (!isVertical)
            {
                dict5["Encoding"] = Library.CreateName("Identity-H");
            }
            else
            {
                dict5["Encoding"] = Library.CreateName("Identity-V");
            }
            PDFArray array4 = Library.CreateArray(1);

            array4[0] = doc.Indirects.New(dict1);
            dict5["DescendantFonts"] = array4;
            return(new FontType0(dict5));
        }