Ejemplo n.º 1
0
        internal PdfTrueTypeFont(PdfDictionary fontDictionary)
            : base(fontDictionary)
        {
            newFont      = false;
            subset       = false;
            fontEncoding = DocFontEncoding.CreateDocFontEncoding(fontDictionary.Get(PdfName.Encoding), toUnicode);
            PdfName baseFontName = fontDictionary.GetAsName(PdfName.BaseFont);

            // Section 9.6.3 (ISO-32000-1): A TrueType font dictionary may contain the same entries as a Type 1 font
            // dictionary (see Table 111), with these differences...
            // Section 9.6.2.2. (ISO-32000-1) associate standard fonts with Type1 fonts but there does not
            // seem to be a strict requirement on the subtype
            // Cases when a font with /TrueType subtype has base font which is one of the Standard 14 fonts
            // does not seem to be forbidden and it's handled by many PDF tools, so we handle it here as well
            if (baseFontName != null && StandardFonts.IsStandardFont(baseFontName.GetValue()) && !fontDictionary.ContainsKey
                    (PdfName.FontDescriptor) && !fontDictionary.ContainsKey(PdfName.Widths))
            {
                try {
                    fontProgram = FontProgramFactory.CreateFont(baseFontName.GetValue(), true);
                }
                catch (System.IO.IOException e) {
                    throw new PdfException(PdfException.IoExceptionWhileCreatingFont, e);
                }
            }
            else
            {
                fontProgram = DocTrueTypeFont.CreateFontProgram(fontDictionary, fontEncoding, toUnicode);
            }
            embedded = fontProgram is IDocFontProgram && ((IDocFontProgram)fontProgram).GetFontFile() != null;
        }
Ejemplo n.º 2
0
 internal PdfTrueTypeFont(PdfDictionary fontDictionary)
     : base(fontDictionary)
 {
     newFont      = false;
     fontEncoding = DocFontEncoding.CreateDocFontEncoding(fontDictionary.Get(PdfName.Encoding), toUnicode);
     fontProgram  = DocTrueTypeFont.CreateFontProgram(fontDictionary, fontEncoding, toUnicode);
     embedded     = ((IDocFontProgram)fontProgram).GetFontFile() != null;
     subset       = false;
 }
Ejemplo n.º 3
0
        internal PdfTrueTypeFont(PdfDictionary fontDictionary)
            : base(fontDictionary)
        {
            newFont = false;
            CheckFontDictionary(fontDictionary, PdfName.TrueType);
            CMapToUnicode toUni = FontUtil.ProcessToUnicode(fontDictionary.Get(PdfName.ToUnicode));

            fontEncoding = DocFontEncoding.CreateDocFontEncoding(fontDictionary.Get(PdfName.Encoding), toUni, false);
            fontProgram  = DocTrueTypeFont.CreateFontProgram(fontDictionary, fontEncoding);
            embedded     = ((IDocFontProgram)fontProgram).GetFontFile() != null;
            subset       = false;
        }
Ejemplo n.º 4
0
 internal PdfType1Font(PdfDictionary fontDictionary)
     : base(fontDictionary)
 {
     newFont = false;
     // if there is no FontDescriptor, it is most likely one of the Standard Font with StandardEncoding as base encoding.
     // unused variable.
     // boolean fillStandardEncoding = !fontDictionary.containsKey(PdfName.FontDescriptor);
     fontEncoding = DocFontEncoding.CreateDocFontEncoding(fontDictionary.Get(PdfName.Encoding), toUnicode);
     fontProgram  = DocType1Font.CreateFontProgram(fontDictionary, fontEncoding, toUnicode);
     if (fontProgram is IDocFontProgram)
     {
         embedded = ((IDocFontProgram)fontProgram).GetFontFile() != null;
     }
     subset = false;
 }
Ejemplo n.º 5
0
        /// <summary>Creates a Type 3 font based on an existing font dictionary, which must be an indirect object.</summary>
        /// <param name="fontDictionary">a dictionary of type <code>/Font</code>, must have an indirect reference.</param>
        internal PdfType3Font(PdfDictionary fontDictionary)
            : base(fontDictionary)
        {
            subset      = true;
            embedded    = true;
            fontProgram = new Type3Font(false);
            CMapToUnicode toUni = FontUtil.ProcessToUnicode(fontDictionary.Get(PdfName.ToUnicode));

            fontEncoding = DocFontEncoding.CreateDocFontEncoding(fontDictionary.Get(PdfName.Encoding), toUni);
            PdfDictionary charProcsDic    = GetPdfObject().GetAsDictionary(PdfName.CharProcs);
            PdfArray      fontMatrixArray = GetPdfObject().GetAsArray(PdfName.FontMatrix);

            if (GetPdfObject().ContainsKey(PdfName.FontBBox))
            {
                PdfArray fontBBox = GetPdfObject().GetAsArray(PdfName.FontBBox);
                fontProgram.GetFontMetrics().SetBbox(fontBBox.GetAsNumber(0).IntValue(), fontBBox.GetAsNumber(1).IntValue(
                                                         ), fontBBox.GetAsNumber(2).IntValue(), fontBBox.GetAsNumber(3).IntValue());
            }
            else
            {
                fontProgram.GetFontMetrics().SetBbox(0, 0, 0, 0);
            }
            int firstChar = NormalizeFirstLastChar(fontDictionary.GetAsNumber(PdfName.FirstChar), 0);
            int lastChar  = NormalizeFirstLastChar(fontDictionary.GetAsNumber(PdfName.LastChar), 255);

            for (int i = firstChar; i <= lastChar; i++)
            {
                shortTag[i] = 1;
            }
            int[]    widths     = FontUtil.ConvertSimpleWidthsArray(fontDictionary.GetAsArray(PdfName.Widths), firstChar, 0);
            double[] fontMatrix = new double[6];
            for (int i = 0; i < fontMatrixArray.Size(); i++)
            {
                fontMatrix[i] = ((PdfNumber)fontMatrixArray.Get(i)).GetValue();
            }
            SetFontMatrix(fontMatrix);
            foreach (PdfName glyphName in charProcsDic.KeySet())
            {
                int unicode = AdobeGlyphList.NameToUnicode(glyphName.GetValue());
                if (unicode != -1 && fontEncoding.CanEncode(unicode))
                {
                    int code = fontEncoding.ConvertToByte(unicode);
                    ((Type3Font)GetFontProgram()).AddGlyph(code, unicode, widths[code], null, new Type3Glyph(charProcsDic.GetAsStream
                                                                                                                 (glyphName), GetDocument()));
                }
            }
            FillFontDescriptor(fontDictionary.GetAsDictionary(PdfName.FontDescriptor));
        }
Ejemplo n.º 6
0
        internal PdfType1Font(PdfDictionary fontDictionary)
            : base(fontDictionary)
        {
            newFont = false;
            CheckFontDictionary(fontDictionary, PdfName.Type1);
            CMapToUnicode toUni = FontUtil.ProcessToUnicode(fontDictionary.Get(PdfName.ToUnicode));
            //if there is no FontDescriptor, it is most likely one of the Standard Font with StandardEncoding as base encoding.
            bool fillStandardEncoding = !fontDictionary.ContainsKey(PdfName.FontDescriptor);

            fontEncoding = DocFontEncoding.CreateDocFontEncoding(fontDictionary.Get(PdfName.Encoding), toUni, fillStandardEncoding
                                                                 );
            fontProgram = DocType1Font.CreateFontProgram(fontDictionary, fontEncoding, toUni);
            if (fontProgram is IDocFontProgram)
            {
                embedded = ((IDocFontProgram)fontProgram).GetFontFile() != null;
            }
            subset = false;
        }
Ejemplo n.º 7
0
        /// <summary>Creates a Type3 font based on an existing font dictionary, which must be an indirect object.</summary>
        /// <param name="fontDictionary">a dictionary of type <code>/Font</code>, must have an indirect reference.</param>
        internal PdfType3Font(PdfDictionary fontDictionary)
            : base(fontDictionary)
        {
            EnsureObjectIsAddedToDocument(fontDictionary);
            CheckFontDictionary(fontDictionary, PdfName.Type3);
            subset       = true;
            embedded     = true;
            fontProgram  = new Type3FontProgram(false);
            fontEncoding = DocFontEncoding.CreateDocFontEncoding(fontDictionary.Get(PdfName.Encoding), null, false);
            PdfDictionary charProcsDic    = GetPdfObject().GetAsDictionary(PdfName.CharProcs);
            PdfArray      fontMatrixArray = GetPdfObject().GetAsArray(PdfName.FontMatrix);

            if (GetPdfObject().ContainsKey(PdfName.FontBBox))
            {
                PdfArray fontBBox = GetPdfObject().GetAsArray(PdfName.FontBBox);
                fontProgram.GetFontMetrics().SetBbox(fontBBox.GetAsNumber(0).IntValue(), fontBBox.GetAsNumber(1).IntValue(
                                                         ), fontBBox.GetAsNumber(2).IntValue(), fontBBox.GetAsNumber(3).IntValue());
            }
            else
            {
                fontProgram.GetFontMetrics().SetBbox(0, 0, 0, 0);
            }
            PdfNumber firstCharNumber = fontDictionary.GetAsNumber(PdfName.FirstChar);
            int       firstChar       = firstCharNumber != null?Math.Max(firstCharNumber.IntValue(), 0) : 0;

            int[]    widths     = FontUtil.ConvertSimpleWidthsArray(fontDictionary.GetAsArray(PdfName.Widths), firstChar);
            double[] fontMatrix = new double[6];
            for (int i = 0; i < fontMatrixArray.Size(); i++)
            {
                fontMatrix[i] = ((PdfNumber)fontMatrixArray.Get(i)).GetValue();
            }
            SetFontMatrix(fontMatrix);
            foreach (PdfName glyphName in charProcsDic.KeySet())
            {
                int unicode = (int)AdobeGlyphList.NameToUnicode(glyphName.GetValue());
                if (unicode != -1 && fontEncoding.CanEncode(unicode))
                {
                    int code = fontEncoding.ConvertToByte(unicode);
                    ((Type3FontProgram)GetFontProgram()).AddGlyph(code, unicode, widths[code], null, new Type3Glyph(charProcsDic
                                                                                                                    .GetAsStream(glyphName), GetDocument()));
                }
            }
        }
Ejemplo n.º 8
0
        /// <summary>Creates a Type 3 font based on an existing font dictionary, which must be an indirect object.</summary>
        /// <param name="fontDictionary">a dictionary of type <c>/Font</c>, must have an indirect reference.</param>
        internal PdfType3Font(PdfDictionary fontDictionary)
            : base(fontDictionary)
        {
            subset       = true;
            embedded     = true;
            fontProgram  = new Type3Font(false);
            fontEncoding = DocFontEncoding.CreateDocFontEncoding(fontDictionary.Get(PdfName.Encoding), toUnicode);
            PdfDictionary charProcsDic    = GetPdfObject().GetAsDictionary(PdfName.CharProcs);
            PdfArray      fontMatrixArray = GetPdfObject().GetAsArray(PdfName.FontMatrix);

            if (GetPdfObject().ContainsKey(PdfName.FontBBox))
            {
                PdfArray fontBBox = GetPdfObject().GetAsArray(PdfName.FontBBox);
                fontProgram.GetFontMetrics().SetBbox(fontBBox.GetAsNumber(0).IntValue(), fontBBox.GetAsNumber(1).IntValue(
                                                         ), fontBBox.GetAsNumber(2).IntValue(), fontBBox.GetAsNumber(3).IntValue());
            }
            else
            {
                fontProgram.GetFontMetrics().SetBbox(0, 0, 0, 0);
            }
            int firstChar = NormalizeFirstLastChar(fontDictionary.GetAsNumber(PdfName.FirstChar), 0);
            int lastChar  = NormalizeFirstLastChar(fontDictionary.GetAsNumber(PdfName.LastChar), 255);

            for (int i = firstChar; i <= lastChar; i++)
            {
                shortTag[i] = 1;
            }
            int[]    widths     = FontUtil.ConvertSimpleWidthsArray(fontDictionary.GetAsArray(PdfName.Widths), firstChar, 0);
            double[] fontMatrix = new double[6];
            for (int i = 0; i < fontMatrixArray.Size(); i++)
            {
                fontMatrix[i] = ((PdfNumber)fontMatrixArray.Get(i)).GetValue();
            }
            SetFontMatrix(fontMatrix);
            if (toUnicode != null && toUnicode.HasByteMappings() && fontEncoding.HasDifferences())
            {
                for (int i = 0; i < 256; i++)
                {
                    int     unicode   = fontEncoding.GetUnicode(i);
                    PdfName glyphName = new PdfName(fontEncoding.GetDifference(i));
                    if (unicode != -1 && !FontEncoding.NOTDEF.Equals(glyphName.GetValue()) && charProcsDic.ContainsKey(glyphName
                                                                                                                       ))
                    {
                        ((Type3Font)GetFontProgram()).AddGlyph(i, unicode, widths[i], null, new Type3Glyph(charProcsDic.GetAsStream
                                                                                                               (glyphName), GetDocument()));
                    }
                }
            }
            IDictionary <int, int?> unicodeToCode = null;

            if (toUnicode != null)
            {
                try {
                    unicodeToCode = toUnicode.CreateReverseMapping();
                }
                catch (Exception) {
                }
            }
            foreach (PdfName glyphName in charProcsDic.KeySet())
            {
                int unicode = AdobeGlyphList.NameToUnicode(glyphName.GetValue());
                int code    = -1;
                if (fontEncoding.CanEncode(unicode))
                {
                    code = fontEncoding.ConvertToByte(unicode);
                }
                else
                {
                    if (unicodeToCode != null && unicodeToCode.ContainsKey(unicode))
                    {
                        code = (int)unicodeToCode.Get(unicode);
                    }
                }
                if (code != -1 && GetFontProgram().GetGlyphByCode(code) == null)
                {
                    ((Type3Font)GetFontProgram()).AddGlyph(code, unicode, widths[code], null, new Type3Glyph(charProcsDic.GetAsStream
                                                                                                                 (glyphName), GetDocument()));
                }
            }
            FillFontDescriptor(fontDictionary.GetAsDictionary(PdfName.FontDescriptor));
        }