示例#1
0
        /// <summary>
        /// Function to read the kerning pairs for the font, if they exist.
        /// </summary>
        /// <param name="fontFile">Font file to read.</param>
        private static Dictionary <GorgonKerningPair, int> ReadKernPairs(GorgonChunkFileReader fontFile)
        {
            var result = new Dictionary <GorgonKerningPair, int>();
            GorgonBinaryReader reader = fontFile.OpenChunk(KernDataChunk);

            // Read optional kerning information.
            int kernCount = reader.ReadInt32();

            for (int i = 0; i < kernCount; ++i)
            {
                var kernPair = new GorgonKerningPair(reader.ReadChar(), reader.ReadChar());
                result[kernPair] = reader.ReadInt32();
            }

            fontFile.CloseChunk();

            return(result);
        }
        /// <summary>
        /// Function to read in the kerning information.
        /// </summary>
        /// <param name="fontInfo">The font information to update.</param>
        /// <param name="reader">The reader that is reading the file data.</param>
        private static void ParseKerning(BmFontInfo fontInfo, StreamReader reader)
        {
            if (reader.EndOfStream)
            {
                fontInfo.UseKerningPairs = false;
                return;
            }

            string countLine = reader.ReadLine();

            if (string.IsNullOrWhiteSpace(countLine))
            {
                fontInfo.UseKerningPairs = false;
                return;
            }

            string[] lineItems = countLine.Split(new[]
            {
                ' '
            },
                                                 StringSplitOptions.RemoveEmptyEntries);

            Dictionary <string, string> keyValues = GetLineKeyValuePairs(lineItems);

            if (!keyValues.ContainsKey(KerningCountLine))
            {
                throw new GorgonException(GorgonResult.CannotRead, Resources.GORGFX_ERR_FONT_FILE_FORMAT_INVALID);
            }

            int count = Convert.ToInt32(keyValues[CountTag]);

            if (count < 1)
            {
                fontInfo.UseKerningPairs = false;
                return;
            }

            fontInfo.UseKerningPairs = true;

            // Iterate through the characters so we have enough info to build out glyph data.
            for (int i = 0; i < count; ++i)
            {
                string line = reader.ReadLine();

                if (string.IsNullOrWhiteSpace(line))
                {
                    throw new GorgonException(GorgonResult.CannotRead, Resources.GORGFX_ERR_FONT_FILE_FORMAT_INVALID);
                }

                lineItems = line.Split(new[]
                {
                    ' '
                },
                                       StringSplitOptions.RemoveEmptyEntries);

                keyValues = GetLineKeyValuePairs(lineItems);

                if (!keyValues.ContainsKey(KerningLine))
                {
                    throw new GorgonException(GorgonResult.CannotRead, Resources.GORGFX_ERR_FONT_FILE_FORMAT_INVALID);
                }

                var pair = new GorgonKerningPair(Convert.ToChar(Convert.ToInt32(keyValues[KernFirstTag])), Convert.ToChar(Convert.ToInt32(keyValues[KernSecondTag])));
                fontInfo.KerningPairs[pair] = Convert.ToInt32(keyValues[KernAmountTag]);
            }
        }