Пример #1
0
        /// <summary>
        /// This will parse the kern data.
        /// </summary>
        /// <param name="fontMetrics">The metrics class to put the parsed data into.</param>
        /// <exception cref="IOException">If there is an error parsing the data.</exception>
        private void ParseKernData(FontMetrics fontMetrics)
        {
            string nextCommand;

            while (!(nextCommand = ReadString()).Equals(END_KERN_DATA))
            {
                if (START_TRACK_KERN.Equals(nextCommand))
                {
                    int count = ReadInt();
                    for (int i = 0; i < count; i++)
                    {
                        fontMetrics.AddTrackKern(new TrackKern {
                            Degree       = ReadInt(),
                            MinPointSize = ReadFloat(),
                            MinKern      = ReadFloat(),
                            MaxPointSize = ReadFloat(),
                            MaxKern      = ReadFloat()
                        });
                    }
                    string end = ReadString();
                    if (!end.Equals(END_TRACK_KERN))
                    {
                        throw new IOException("Error: Expected '" + END_TRACK_KERN + "' actual '" + end + "'");
                    }
                }
                else if (START_KERN_PAIRS.Equals(nextCommand))
                {
                    int count = ReadInt();
                    for (int i = 0; i < count; i++)
                    {
                        KernPair pair = ParseKernPair();
                        fontMetrics.AddKernPair(pair);
                    }
                    String end = ReadString();
                    if (!end.Equals(END_KERN_PAIRS))
                    {
                        throw new IOException("Error: Expected '" + END_KERN_PAIRS + "' actual '" + end + "'");
                    }
                }
                else if (START_KERN_PAIRS0.Equals(nextCommand))
                {
                    int count = ReadInt();
                    for (int i = 0; i < count; i++)
                    {
                        KernPair pair = ParseKernPair();
                        fontMetrics.AddKernPair0(pair);
                    }
                    string end = ReadString();
                    if (!end.Equals(END_KERN_PAIRS))
                    {
                        throw new IOException("Error: Expected '" + END_KERN_PAIRS + "' actual '" + end + "'");
                    }
                }
                else if (START_KERN_PAIRS1.Equals(nextCommand))
                {
                    int count = ReadInt();
                    for (int i = 0; i < count; i++)
                    {
                        KernPair pair = ParseKernPair();
                        fontMetrics.AddKernPair1(pair);
                    }
                    string end = ReadString();
                    if (!end.Equals(END_KERN_PAIRS))
                    {
                        throw new IOException("Error: Expected '" + END_KERN_PAIRS + "' actual '" + end + "'");
                    }
                }
                else
                {
                    throw new IOException("Unknown kerning data type '" + nextCommand + "'");
                }
            }
        }
Пример #2
0
        /// <summary>
        /// This will parse a font metrics item.
        /// </summary>
        /// <returns>The parse font metrics item.<returns>
        /// <exception cref="IOException">If there is an error reading the AFM file.</exception>
        private FontMetrics ParseFontMetric(bool reducedDataset)
        {
            FontMetrics fontMetrics      = new FontMetrics();
            string      startFontMetrics = ReadString();

            if (!START_FONT_METRICS.Equals(startFontMetrics))
            {
                throw new IOException("Error: The AFM file should start with " + START_FONT_METRICS +
                                      " and not '" + startFontMetrics + "'");
            }
            fontMetrics.AfmVersion = ReadFloat();
            string nextCommand;
            bool   charMetricsRead = false;

            while (!END_FONT_METRICS.Equals((nextCommand = ReadString())))
            {
                if (FONT_NAME.Equals(nextCommand))
                {
                    fontMetrics.FontName = ReadLine();
                }
                else if (FULL_NAME.Equals(nextCommand))
                {
                    fontMetrics.FullName = ReadLine();
                }
                else if (FAMILY_NAME.Equals(nextCommand))
                {
                    fontMetrics.FamilyName = ReadLine();
                }
                else if (WEIGHT.Equals(nextCommand))
                {
                    fontMetrics.Weight = ReadLine();
                }
                else if (FONT_BBOX.Equals(nextCommand))
                {
                    fontMetrics.FontBBox = new BoundingBox
                    {
                        LowerLeftX  = ReadFloat(),
                        LowerLeftY  = ReadFloat(),
                        UpperRightX = ReadFloat(),
                        UpperRightY = ReadFloat()
                    };
                }
                else if (VERSION.Equals(nextCommand))
                {
                    fontMetrics.FontVersion = ReadLine();
                }
                else if (NOTICE.Equals(nextCommand))
                {
                    fontMetrics.Notice = ReadLine();
                }
                else if (ENCODING_SCHEME.Equals(nextCommand))
                {
                    fontMetrics.EncodingScheme = ReadLine();
                }
                else if (MAPPING_SCHEME.Equals(nextCommand))
                {
                    fontMetrics.MappingScheme = ReadInt();
                }
                else if (ESC_CHAR.Equals(nextCommand))
                {
                    fontMetrics.EscChar = ReadInt();
                }
                else if (CHARACTER_SET.Equals(nextCommand))
                {
                    fontMetrics.CharacterSet = ReadLine();
                }
                else if (CHARACTERS.Equals(nextCommand))
                {
                    fontMetrics.Characters = ReadInt();
                }
                else if (IS_BASE_FONT.Equals(nextCommand))
                {
                    fontMetrics.IsBaseFont = Readbool();
                }
                else if (V_VECTOR.Equals(nextCommand))
                {
                    fontMetrics.VVector = new float[2] {
                        ReadFloat(), ReadFloat()
                    };
                }
                else if (IS_FIXED_V.Equals(nextCommand))
                {
                    fontMetrics.IsFixedV = Readbool();
                }
                else if (CAP_HEIGHT.Equals(nextCommand))
                {
                    fontMetrics.CapHeight = ReadFloat();
                }
                else if (X_HEIGHT.Equals(nextCommand))
                {
                    fontMetrics.XHeight = ReadFloat();
                }
                else if (ASCENDER.Equals(nextCommand))
                {
                    fontMetrics.Ascender = ReadFloat();
                }
                else if (DESCENDER.Equals(nextCommand))
                {
                    fontMetrics.Descender = ReadFloat();
                }
                else if (STD_HW.Equals(nextCommand))
                {
                    fontMetrics.StandardHorizontalWidth = ReadFloat();
                }
                else if (STD_VW.Equals(nextCommand))
                {
                    fontMetrics.StandardVerticalWidth = ReadFloat();
                }
                else if (COMMENT.Equals(nextCommand))
                {
                    fontMetrics.AddComment(ReadLine());
                }
                else if (UNDERLINE_POSITION.Equals(nextCommand))
                {
                    fontMetrics.UnderlinePosition = ReadFloat();
                }
                else if (UNDERLINE_THICKNESS.Equals(nextCommand))
                {
                    fontMetrics.UnderlineThickness = ReadFloat();
                }
                else if (ITALIC_ANGLE.Equals(nextCommand))
                {
                    fontMetrics.ItalicAngle = ReadFloat();
                }
                else if (CHAR_WIDTH.Equals(nextCommand))
                {
                    fontMetrics.CharWidth = new float[2] {
                        ReadFloat(), ReadFloat()
                    };
                }
                else if (IS_FIXED_PITCH.Equals(nextCommand))
                {
                    fontMetrics.FixedPitch = Readbool();
                }
                else if (START_CHAR_METRICS.Equals(nextCommand))
                {
                    int count = ReadInt();
                    List <CharMetric> charMetrics = new List <CharMetric>(count);
                    for (int i = 0; i < count; i++)
                    {
                        CharMetric charMetric = ParseCharMetric();
                        charMetrics.Add(charMetric);
                    }
                    string end = ReadString();
                    if (!end.Equals(END_CHAR_METRICS))
                    {
                        throw new IOException("Error: Expected '" + END_CHAR_METRICS + "' actual '" + end + "'");
                    }
                    charMetricsRead         = true;
                    fontMetrics.CharMetrics = charMetrics.AsReadOnly();
                }
                else if (!reducedDataset && START_COMPOSITES.Equals(nextCommand))
                {
                    int count = ReadInt();
                    for (int i = 0; i < count; i++)
                    {
                        Composite part = ParseComposite();
                        fontMetrics.AddComposite(part);
                    }
                    string end = ReadString();
                    if (!end.Equals(END_COMPOSITES))
                    {
                        throw new IOException("Error: Expected '" + END_COMPOSITES + "' actual '" + end + "'");
                    }
                }
                else if (!reducedDataset && START_KERN_DATA.Equals(nextCommand))
                {
                    ParseKernData(fontMetrics);
                }
                else
                {
                    if (reducedDataset && charMetricsRead)
                    {
                        break;
                    }
                    throw new IOException("Unknown AFM key '" + nextCommand + "'");
                }
            }
            return(fontMetrics);
        }