예제 #1
0
        protected internal virtual void Process()
        {
            RandomAccessFileOrArray raf = fontParser.GetMetricsFile();
            String line;
            bool   startKernPairs = false;

            while (!startKernPairs && (line = raf.ReadLine()) != null)
            {
                StringTokenizer tok = new StringTokenizer(line, " ,\n\r\t\f");
                if (!tok.HasMoreTokens())
                {
                    continue;
                }
                String ident = tok.NextToken();
                switch (ident)
                {
                case "FontName": {
                    fontNames.SetFontName(tok.NextToken("\u00ff").Substring(1));
                    break;
                }

                case "FullName": {
                    String fullName = tok.NextToken("\u00ff").Substring(1);
                    fontNames.SetFullName(new String[][] { new String[] { "", "", "", fullName } });
                    break;
                }

                case "FamilyName": {
                    String familyName = tok.NextToken("\u00ff").Substring(1);
                    fontNames.SetFamilyName(new String[][] { new String[] { "", "", "", familyName } });
                    break;
                }

                case "Weight": {
                    fontNames.SetFontWeight(FontWeights.FromType1FontWeight(tok.NextToken("\u00ff").Substring(1)));
                    break;
                }

                case "ItalicAngle": {
                    fontMetrics.SetItalicAngle(float.Parse(tok.NextToken(), System.Globalization.CultureInfo.InvariantCulture)
                                               );
                    break;
                }

                case "IsFixedPitch": {
                    fontMetrics.SetIsFixedPitch(tok.NextToken().Equals("true"));
                    break;
                }

                case "CharacterSet": {
                    characterSet = tok.NextToken("\u00ff").Substring(1);
                    break;
                }

                case "FontBBox": {
                    int llx = (int)float.Parse(tok.NextToken(), System.Globalization.CultureInfo.InvariantCulture);
                    int lly = (int)float.Parse(tok.NextToken(), System.Globalization.CultureInfo.InvariantCulture);
                    int urx = (int)float.Parse(tok.NextToken(), System.Globalization.CultureInfo.InvariantCulture);
                    int ury = (int)float.Parse(tok.NextToken(), System.Globalization.CultureInfo.InvariantCulture);
                    fontMetrics.SetBbox(llx, lly, urx, ury);
                    break;
                }

                case "UnderlinePosition": {
                    fontMetrics.SetUnderlinePosition((int)float.Parse(tok.NextToken(), System.Globalization.CultureInfo.InvariantCulture
                                                                      ));
                    break;
                }

                case "UnderlineThickness": {
                    fontMetrics.SetUnderlineThickness((int)float.Parse(tok.NextToken(), System.Globalization.CultureInfo.InvariantCulture
                                                                       ));
                    break;
                }

                case "EncodingScheme": {
                    encodingScheme = tok.NextToken("\u00ff").Substring(1).Trim();
                    break;
                }

                case "CapHeight": {
                    fontMetrics.SetCapHeight((int)float.Parse(tok.NextToken(), System.Globalization.CultureInfo.InvariantCulture
                                                              ));
                    break;
                }

                case "XHeight": {
                    fontMetrics.SetXHeight((int)float.Parse(tok.NextToken(), System.Globalization.CultureInfo.InvariantCulture
                                                            ));
                    break;
                }

                case "Ascender": {
                    fontMetrics.SetTypoAscender((int)float.Parse(tok.NextToken(), System.Globalization.CultureInfo.InvariantCulture
                                                                 ));
                    break;
                }

                case "Descender": {
                    fontMetrics.SetTypoDescender((int)float.Parse(tok.NextToken(), System.Globalization.CultureInfo.InvariantCulture
                                                                  ));
                    break;
                }

                case "StdHW": {
                    fontMetrics.SetStemH((int)float.Parse(tok.NextToken(), System.Globalization.CultureInfo.InvariantCulture));
                    break;
                }

                case "StdVW": {
                    fontMetrics.SetStemV((int)float.Parse(tok.NextToken(), System.Globalization.CultureInfo.InvariantCulture));
                    break;
                }

                case "StartCharMetrics": {
                    startKernPairs = true;
                    break;
                }
                }
            }
            if (!startKernPairs)
            {
                String metricsPath = fontParser.GetAfmPath();
                if (metricsPath != null)
                {
                    throw new iText.IO.IOException("startcharmetrics is missing in {0}.").SetMessageParams(metricsPath);
                }
                else
                {
                    throw new iText.IO.IOException("startcharmetrics is missing in the metrics file.");
                }
            }
            avgWidth = 0;
            int widthCount = 0;

            while ((line = raf.ReadLine()) != null)
            {
                StringTokenizer tok = new StringTokenizer(line);
                if (!tok.HasMoreTokens())
                {
                    continue;
                }
                String ident = tok.NextToken();
                if (ident.Equals("EndCharMetrics"))
                {
                    startKernPairs = false;
                    break;
                }
                int    C  = -1;
                int    WX = 250;
                String N  = "";
                int[]  B  = null;
                tok = new StringTokenizer(line, ";");
                while (tok.HasMoreTokens())
                {
                    StringTokenizer tokc = new StringTokenizer(tok.NextToken());
                    if (!tokc.HasMoreTokens())
                    {
                        continue;
                    }
                    ident = tokc.NextToken();
                    switch (ident)
                    {
                    case "C": {
                        C = Convert.ToInt32(tokc.NextToken());
                        break;
                    }

                    case "WX": {
                        WX = (int)float.Parse(tokc.NextToken(), System.Globalization.CultureInfo.InvariantCulture);
                        break;
                    }

                    case "N": {
                        N = tokc.NextToken();
                        break;
                    }

                    case "B": {
                        B = new int[] { Convert.ToInt32(tokc.NextToken()), Convert.ToInt32(tokc.NextToken()), Convert.ToInt32(tokc
                                                                                                                              .NextToken()), Convert.ToInt32(tokc.NextToken()) };
                        break;
                    }
                    }
                }
                int   unicode = AdobeGlyphList.NameToUnicode(N);
                Glyph glyph   = new Glyph(C, WX, unicode, B);
                if (C >= 0)
                {
                    codeToGlyph.Put(C, glyph);
                }
                if (unicode != -1)
                {
                    unicodeToGlyph.Put(unicode, glyph);
                }
                avgWidth += WX;
                widthCount++;
            }
            if (widthCount != 0)
            {
                avgWidth /= widthCount;
            }
            if (startKernPairs)
            {
                String metricsPath = fontParser.GetAfmPath();
                if (metricsPath != null)
                {
                    throw new iText.IO.IOException("endcharmetrics is missing in {0}.").SetMessageParams(metricsPath);
                }
                else
                {
                    throw new iText.IO.IOException("endcharmetrics is missing in the metrics file.");
                }
            }
            // From AdobeGlyphList:
            // nonbreakingspace;00A0
            // space;0020
            if (!unicodeToGlyph.ContainsKey(0x00A0))
            {
                Glyph space = unicodeToGlyph.Get(0x0020);
                if (space != null)
                {
                    unicodeToGlyph.Put(0x00A0, new Glyph(space.GetCode(), space.GetWidth(), 0x00A0, space.GetBbox()));
                }
            }
            bool endOfMetrics = false;

            while ((line = raf.ReadLine()) != null)
            {
                StringTokenizer tok = new StringTokenizer(line);
                if (!tok.HasMoreTokens())
                {
                    continue;
                }
                String ident = tok.NextToken();
                if (ident.Equals("EndFontMetrics"))
                {
                    endOfMetrics = true;
                    break;
                }
                else
                {
                    if (ident.Equals("StartKernPairs"))
                    {
                        startKernPairs = true;
                        break;
                    }
                }
            }
            if (startKernPairs)
            {
                while ((line = raf.ReadLine()) != null)
                {
                    StringTokenizer tok = new StringTokenizer(line);
                    if (!tok.HasMoreTokens())
                    {
                        continue;
                    }
                    String ident = tok.NextToken();
                    if (ident.Equals("KPX"))
                    {
                        String first     = tok.NextToken();
                        String second    = tok.NextToken();
                        int?   width     = (int)float.Parse(tok.NextToken(), System.Globalization.CultureInfo.InvariantCulture);
                        int    firstUni  = AdobeGlyphList.NameToUnicode(first);
                        int    secondUni = AdobeGlyphList.NameToUnicode(second);
                        if (firstUni != -1 && secondUni != -1)
                        {
                            long record = ((long)firstUni << 32) + secondUni;
                            kernPairs.Put(record, width);
                        }
                    }
                    else
                    {
                        if (ident.Equals("EndKernPairs"))
                        {
                            startKernPairs = false;
                            break;
                        }
                    }
                }
            }
            else
            {
                if (!endOfMetrics)
                {
                    String metricsPath = fontParser.GetAfmPath();
                    if (metricsPath != null)
                    {
                        throw new iText.IO.IOException("endfontmetrics is missing in {0}.").SetMessageParams(metricsPath);
                    }
                    else
                    {
                        throw new iText.IO.IOException("endfontmetrics is missing in the metrics file.");
                    }
                }
            }
            if (startKernPairs)
            {
                String metricsPath = fontParser.GetAfmPath();
                if (metricsPath != null)
                {
                    throw new iText.IO.IOException("endkernpairs is missing in {0}.").SetMessageParams(metricsPath);
                }
                else
                {
                    throw new iText.IO.IOException("endkernpairs is missing in the metrics file.");
                }
            }
            raf.Close();
            isFontSpecific = !(encodingScheme.Equals("AdobeStandardEncoding") || encodingScheme.Equals("StandardEncoding"
                                                                                                       ));
        }