Ejemplo n.º 1
0
        public BitmapFont LoadFont(string fontName)
        {
            string bitmapFileName = Path.Combine(_fontsFolder, String.Format(@"{0}.png", fontName));
            string bitmapAlphaFileName = Path.Combine(_fontsFolder, String.Format(@"_{0}.png", fontName));
            string definitionFileName = Path.Combine(_fontsFolder, String.Format(@"{0}.txt", fontName));

            _logger.Debug(
                "Loading font - fontname={0}, filename={1}, alphafilename={2}, definitionfilename={3}",
                fontName,
                bitmapFileName,
                bitmapAlphaFileName,
                definitionFileName);

            if (!File.Exists(bitmapFileName))
            {
                throw new FileNotFoundException(bitmapFileName);
            }

            if (!File.Exists(definitionFileName))
            {
                throw new FileNotFoundException(definitionFileName);
            }

            var bitmap = new Image<Bgra, byte>(bitmapFileName);
            var alphaBitmap = new Image<Bgra, byte>(bitmapAlphaFileName);
            var font = new BitmapFont { Bitmap = bitmap.CombineAlpha(alphaBitmap) };

            var definition = File.ReadAllText(definitionFileName, Encoding.GetEncoding(1252));

            var definesRegex = new Regex(@"Define.*?\);", RegexOptions.Singleline);
            var matches = definesRegex.Matches(definition).OfType<Match>().ToList();
            var charList = matches.First(x => x.Value.Contains("CharList")).Value;
            var widthList = matches.First(x => x.Value.Contains("WidthList")).Value;
            var rectList = matches.First(x => x.Value.Contains("RectList")).Value;
            var offsetList = matches.First(x => x.Value.Contains("OffsetList")).Value;

            AddCharList(font, charList);
            AddWidthList(font, widthList);
            AddRectList(font, rectList);
            AddOffsetList(font, offsetList);

            font.Ascent = Int32.Parse(new Regex(@"LayerSetAscent .*?(\d+?);").Match(definition).Groups[1].Value);

            int spaceWidth = Int32.Parse(new Regex(@"LayerSetCharWidths\s+Main\s\(' '\) \((\d+?)\);").Match(definition).Groups[1].Value);
            font.Characters.Add(new FontCharacter { Character = ' ', Width = spaceWidth, Rectangle = new Rectangle(0, 0, spaceWidth, 0) });

            return font;
        }