/// <exception cref="System.IO.IOException"/>
        private void InitializeFontProperties()
        {
            // initialize sfnt tables
            OpenTypeParser.HeaderTable      head = fontParser.GetHeadTable();
            OpenTypeParser.HorizontalHeader hhea = fontParser.GetHheaTable();
            OpenTypeParser.WindowsMetrics   os_2 = fontParser.GetOs_2Table();
            OpenTypeParser.PostTable        post = fontParser.GetPostTable();
            isFontSpecific = fontParser.GetCmapTable().fontSpecific;
            kerning        = fontParser.ReadKerning(head.unitsPerEm);
            bBoxes         = fontParser.ReadBbox(head.unitsPerEm);
            // font names group
            fontNames.SetAllNames(fontParser.GetAllNameEntries());
            fontNames.SetFontName(fontParser.GetPsFontName());
            fontNames.SetFullName(fontNames.GetNames(4));
            String[][] otfFamilyName = fontNames.GetNames(16);
            if (otfFamilyName != null)
            {
                fontNames.SetFamilyName(otfFamilyName);
            }
            else
            {
                fontNames.SetFamilyName(fontNames.GetNames(1));
            }
            String[][] subfamily = fontNames.GetNames(2);
            if (subfamily != null)
            {
                fontNames.SetStyle(subfamily[0][3]);
            }
            String[][] otfSubFamily = fontNames.GetNames(17);
            if (otfFamilyName != null)
            {
                fontNames.SetSubfamily(otfSubFamily);
            }
            else
            {
                fontNames.SetSubfamily(subfamily);
            }
            String[][] cidName = fontNames.GetNames(20);
            if (cidName != null)
            {
                fontNames.SetCidFontName(cidName[0][3]);
            }
            fontNames.SetWeight(os_2.usWeightClass);
            fontNames.SetWidth(os_2.usWidthClass);
            fontNames.SetMacStyle(head.macStyle);
            fontNames.SetAllowEmbedding(os_2.fsType != 2);
            // font metrics group
            fontMetrics.SetUnitsPerEm(head.unitsPerEm);
            fontMetrics.UpdateBbox(head.xMin, head.yMin, head.xMax, head.yMax);
            fontMetrics.SetMaxGlyphId(fontParser.ReadMaxGlyphId());
            fontMetrics.SetGlyphWidths(fontParser.GetGlyphWidthsByIndex());
            fontMetrics.SetTypoAscender(os_2.sTypoAscender);
            fontMetrics.SetTypoDescender(os_2.sTypoDescender);
            fontMetrics.SetCapHeight(os_2.sCapHeight);
            fontMetrics.SetXHeight(os_2.sxHeight);
            fontMetrics.SetItalicAngle(post.italicAngle);
            fontMetrics.SetAscender(hhea.Ascender);
            fontMetrics.SetDescender(hhea.Descender);
            fontMetrics.SetLineGap(hhea.LineGap);
            fontMetrics.SetWinAscender(os_2.usWinAscent);
            fontMetrics.SetWinDescender(os_2.usWinDescent);
            fontMetrics.SetAdvanceWidthMax(hhea.advanceWidthMax);
            fontMetrics.SetUnderlinePosition((post.underlinePosition - post.underlineThickness) / 2);
            fontMetrics.SetUnderlineThickness(post.underlineThickness);
            fontMetrics.SetStrikeoutPosition(os_2.yStrikeoutPosition);
            fontMetrics.SetStrikeoutSize(os_2.yStrikeoutSize);
            fontMetrics.SetSubscriptOffset(-os_2.ySubscriptYOffset);
            fontMetrics.SetSubscriptSize(os_2.ySubscriptYSize);
            fontMetrics.SetSuperscriptOffset(os_2.ySuperscriptYOffset);
            fontMetrics.SetSuperscriptSize(os_2.ySuperscriptYSize);
            fontMetrics.SetIsFixedPitch(post.isFixedPitch);
            // font identification group
            String[][] ttfVersion = fontNames.GetNames(5);
            if (ttfVersion != null)
            {
                fontIdentification.SetTtfVersion(ttfVersion[0][3]);
            }
            String[][] ttfUniqueId = fontNames.GetNames(3);
            if (ttfUniqueId != null)
            {
                fontIdentification.SetTtfVersion(ttfUniqueId[0][3]);
            }
            fontIdentification.SetPanose(os_2.panose);
            IDictionary <int, int[]> cmap = GetActiveCmap();

            int[] glyphWidths = fontParser.GetGlyphWidthsByIndex();
            unicodeToGlyph = new LinkedDictionary <int, Glyph>(cmap.Count);
            codeToGlyph    = new LinkedDictionary <int, Glyph>(glyphWidths.Length);
            avgWidth       = 0;
            foreach (int charCode in cmap.Keys)
            {
                int index = cmap.Get(charCode)[0];
                if (index >= glyphWidths.Length)
                {
                    ILogger LOGGER = LoggerFactory.GetLogger(typeof(iText.IO.Font.TrueTypeFont));
                    LOGGER.Warn(String.Format(LogMessageConstant.FONT_HAS_INVALID_GLYPH, GetFontNames().GetFontName(), index));
                    continue;
                }
                Glyph glyph = new Glyph(index, glyphWidths[index], charCode, bBoxes != null ? bBoxes[index] : null);
                unicodeToGlyph[charCode] = glyph;
                codeToGlyph[index]       = glyph;
                avgWidth += glyph.GetWidth();
            }
            FixSpaceIssue();
            for (int index_1 = 0; index_1 < glyphWidths.Length; index_1++)
            {
                if (codeToGlyph.ContainsKey(index_1))
                {
                    continue;
                }
                Glyph glyph = new Glyph(index_1, glyphWidths[index_1], -1);
                codeToGlyph[index_1] = glyph;
                avgWidth            += glyph.GetWidth();
            }
            if (codeToGlyph.Count != 0)
            {
                avgWidth /= codeToGlyph.Count;
            }
            ReadGdefTable();
            ReadGsubTable();
            ReadGposTable();
            isVertical = false;
        }
Exemple #2
0
        /// <exception cref="System.IO.IOException"/>
        private void InitializeFontProperties()
        {
            // initialize sfnt tables
            OpenTypeParser.HeaderTable      head = fontParser.GetHeadTable();
            OpenTypeParser.HorizontalHeader hhea = fontParser.GetHheaTable();
            OpenTypeParser.WindowsMetrics   os_2 = fontParser.GetOs_2Table();
            OpenTypeParser.PostTable        post = fontParser.GetPostTable();
            isFontSpecific = fontParser.GetCmapTable().fontSpecific;
            kerning        = fontParser.ReadKerning(head.unitsPerEm);
            bBoxes         = fontParser.ReadBbox(head.unitsPerEm);
            // font names group
            fontNames = fontParser.GetFontNames();
            // font metrics group
            fontMetrics.SetUnitsPerEm(head.unitsPerEm);
            fontMetrics.UpdateBbox(head.xMin, head.yMin, head.xMax, head.yMax);
            fontMetrics.SetNumberOfGlyphs(fontParser.ReadNumGlyphs());
            fontMetrics.SetGlyphWidths(fontParser.GetGlyphWidthsByIndex());
            fontMetrics.SetTypoAscender(os_2.sTypoAscender);
            fontMetrics.SetTypoDescender(os_2.sTypoDescender);
            fontMetrics.SetCapHeight(os_2.sCapHeight);
            fontMetrics.SetXHeight(os_2.sxHeight);
            fontMetrics.SetItalicAngle(post.italicAngle);
            fontMetrics.SetAscender(hhea.Ascender);
            fontMetrics.SetDescender(hhea.Descender);
            fontMetrics.SetLineGap(hhea.LineGap);
            fontMetrics.SetWinAscender(os_2.usWinAscent);
            fontMetrics.SetWinDescender(os_2.usWinDescent);
            fontMetrics.SetAdvanceWidthMax(hhea.advanceWidthMax);
            fontMetrics.SetUnderlinePosition((post.underlinePosition - post.underlineThickness) / 2);
            fontMetrics.SetUnderlineThickness(post.underlineThickness);
            fontMetrics.SetStrikeoutPosition(os_2.yStrikeoutPosition);
            fontMetrics.SetStrikeoutSize(os_2.yStrikeoutSize);
            fontMetrics.SetSubscriptOffset(-os_2.ySubscriptYOffset);
            fontMetrics.SetSubscriptSize(os_2.ySubscriptYSize);
            fontMetrics.SetSuperscriptOffset(os_2.ySuperscriptYOffset);
            fontMetrics.SetSuperscriptSize(os_2.ySuperscriptYSize);
            fontMetrics.SetIsFixedPitch(post.isFixedPitch);
            // font identification group
            String[][] ttfVersion = fontNames.GetNames(5);
            if (ttfVersion != null)
            {
                fontIdentification.SetTtfVersion(ttfVersion[0][3]);
            }
            String[][] ttfUniqueId = fontNames.GetNames(3);
            if (ttfUniqueId != null)
            {
                fontIdentification.SetTtfVersion(ttfUniqueId[0][3]);
            }
            byte[] pdfPanose = new byte[12];
            pdfPanose[1] = (byte)(os_2.sFamilyClass);
            pdfPanose[0] = (byte)(os_2.sFamilyClass >> 8);
            Array.Copy(os_2.panose, 0, pdfPanose, 2, 10);
            fontIdentification.SetPanose(pdfPanose);
            IDictionary <int, int[]> cmap = GetActiveCmap();

            int[] glyphWidths = fontParser.GetGlyphWidthsByIndex();
            int   numOfGlyphs = fontMetrics.GetNumberOfGlyphs();

            unicodeToGlyph = new LinkedDictionary <int, Glyph>(cmap.Count);
            codeToGlyph    = new LinkedDictionary <int, Glyph>(numOfGlyphs);
            avgWidth       = 0;
            foreach (int charCode in cmap.Keys)
            {
                int index = cmap.Get(charCode)[0];
                if (index >= numOfGlyphs)
                {
                    ILog LOGGER = LogManager.GetLogger(typeof(iText.IO.Font.TrueTypeFont));
                    LOGGER.Warn(MessageFormatUtil.Format(iText.IO.LogMessageConstant.FONT_HAS_INVALID_GLYPH, GetFontNames().GetFontName
                                                             (), index));
                    continue;
                }
                Glyph glyph = new Glyph(index, glyphWidths[index], charCode, bBoxes != null ? bBoxes[index] : null);
                unicodeToGlyph.Put(charCode, glyph);
                // This is done on purpose to keep the mapping to glyphs with smaller unicode values, in contrast with
                // larger values which often represent different forms of other characters.
                if (!codeToGlyph.ContainsKey(index))
                {
                    codeToGlyph.Put(index, glyph);
                }
                avgWidth += glyph.GetWidth();
            }
            FixSpaceIssue();
            for (int index = 0; index < glyphWidths.Length; index++)
            {
                if (codeToGlyph.ContainsKey(index))
                {
                    continue;
                }
                Glyph glyph = new Glyph(index, glyphWidths[index], -1);
                codeToGlyph.Put(index, glyph);
                avgWidth += glyph.GetWidth();
            }
            if (codeToGlyph.Count != 0)
            {
                avgWidth /= codeToGlyph.Count;
            }
            ReadGdefTable();
            ReadGsubTable();
            ReadGposTable();
            isVertical = false;
        }
Exemple #3
0
        /// <summary>Reads the windows metrics table.</summary>
        /// <remarks>
        /// Reads the windows metrics table. The metrics are extracted from the table 'OS/2'.
        /// Depends on
        /// <see cref="HeaderTable.unitsPerEm"/>
        /// property.
        /// </remarks>
        private void ReadOs_2Table()
        {
            int[] table_location = tables.Get("OS/2");
            if (table_location == null)
            {
                if (fileName != null)
                {
                    throw new iText.IO.IOException(iText.IO.IOException.TableDoesNotExistsIn).SetMessageParams("os/2", fileName
                                                                                                               );
                }
                else
                {
                    throw new iText.IO.IOException(iText.IO.IOException.TableDoesNotExist).SetMessageParams("os/2");
                }
            }
            os_2 = new OpenTypeParser.WindowsMetrics();
            raf.Seek(table_location[0]);
            int version = raf.ReadUnsignedShort();

            os_2.xAvgCharWidth       = raf.ReadShort();
            os_2.usWeightClass       = raf.ReadUnsignedShort();
            os_2.usWidthClass        = raf.ReadUnsignedShort();
            os_2.fsType              = raf.ReadShort();
            os_2.ySubscriptXSize     = raf.ReadShort();
            os_2.ySubscriptYSize     = raf.ReadShort();
            os_2.ySubscriptXOffset   = raf.ReadShort();
            os_2.ySubscriptYOffset   = raf.ReadShort();
            os_2.ySuperscriptXSize   = raf.ReadShort();
            os_2.ySuperscriptYSize   = raf.ReadShort();
            os_2.ySuperscriptXOffset = raf.ReadShort();
            os_2.ySuperscriptYOffset = raf.ReadShort();
            os_2.yStrikeoutSize      = raf.ReadShort();
            os_2.yStrikeoutPosition  = raf.ReadShort();
            os_2.sFamilyClass        = raf.ReadShort();
            raf.ReadFully(os_2.panose);
            raf.SkipBytes(16);
            raf.ReadFully(os_2.achVendID);
            os_2.fsSelection      = raf.ReadUnsignedShort();
            os_2.usFirstCharIndex = raf.ReadUnsignedShort();
            os_2.usLastCharIndex  = raf.ReadUnsignedShort();
            os_2.sTypoAscender    = raf.ReadShort();
            os_2.sTypoDescender   = raf.ReadShort();
            if (os_2.sTypoDescender > 0)
            {
                os_2.sTypoDescender = (short)-os_2.sTypoDescender;
            }
            os_2.sTypoLineGap = raf.ReadShort();
            os_2.usWinAscent  = raf.ReadUnsignedShort();
            os_2.usWinDescent = raf.ReadUnsignedShort();
            if (os_2.usWinDescent > 0)
            {
                os_2.usWinDescent = (short)-os_2.usWinDescent;
            }
            os_2.ulCodePageRange1 = 0;
            os_2.ulCodePageRange2 = 0;
            if (version > 0)
            {
                os_2.ulCodePageRange1 = raf.ReadInt();
                os_2.ulCodePageRange2 = raf.ReadInt();
            }
            if (version > 1)
            {
                // todo os_2.sxHeight = raf.readShort();
                raf.SkipBytes(2);
                os_2.sCapHeight = raf.ReadShort();
            }
            else
            {
                os_2.sCapHeight = (int)(0.7 * head.unitsPerEm);
            }
        }