Exemple #1
0
    public void InitializeFont()
    {
        if (fontConfig != null && fontMaterials.Length > 0)
        {
            _fontFile = FontLoader.LoadFromString(fontConfig.text);
            if (_fontFile.Pages.Count != fontMaterials.Length)
            {
                Debug.LogError("Materials count don't match!");
                return;
            }

            _indexLists = new List <List <int> >();
            for (int i = 0; i < _fontFile.Pages.Count; ++i)
            {
                _indexLists.Add(new List <int>());
            }

            _textureSize = new Vector2(_fontFile.Common.ScaleW, _fontFile.Common.ScaleH);
            _charMap     = new Dictionary <int, SpriteChar>();
            for (int i = 0; i < _fontFile.Chars.Count; ++i)
            {
                SpriteChar sc   = new SpriteChar();
                Rect       rect = new Rect(_fontFile.Chars[i].X, _fontFile.Chars[i].Y, _fontFile.Chars[i].Width, _fontFile.Chars[i].Height);
                sc.fontChar = _fontFile.Chars[i];
                sc.rect     = rect;
                _charMap.Add(_fontFile.Chars[i].ID, sc);
            }

            Debug.Log("Font Initialized");
        }
    }
Exemple #2
0
        /// <summary>
        /// Constrcutor
        /// </summary>
        /// <param name="device"></param>
        /// <param name="fileName"></param>
        public SpriteFont(RenderSystem rs, Stream stream)
        {
            this.rs = rs.Device;

            using (var br = new BinaryReader(stream)) {
                var      xml   = br.ReadString();
                FontFile input = FontLoader.LoadFromString(xml);

                int numGlyphs = input.Chars.Max(ch => ch.ID);

                //	create charInfo and kernings :
                fontInfo.kernings = new Dictionary <Tuple <char, char>, float>();
                fontInfo.charInfo = new SpriteFontInfo.CharInfo[numGlyphs + 1];

                //	check one-page bitmap fonts :
                if (input.Pages.Count != 1)
                {
                    throw new GraphicsException("Only one page of font image is supported");
                }

                //	create path for font-image :
                string fontImagePath = input.Pages[0].File;

                //	skip two bytes :
                var texData = stream.ReadAllBytes();
                fontTexture = new UserTexture(rs.Game.RenderSystem, texData, false);

                //	Fill structure :
                fontInfo.fontFace    = input.Info.Face;
                fontInfo.baseLine    = input.Common.Base;
                fontInfo.lineHeight  = input.Common.LineHeight;
                fontInfo.scaleWidth  = input.Common.ScaleW;
                fontInfo.scaleHeight = input.Common.ScaleH;

                float scaleWidth  = fontInfo.scaleWidth;
                float scaleHeight = fontInfo.scaleHeight;

                //	process character info :
                for (int i = 0; i < input.Chars.Count; i++)
                {
                    FontChar ch = input.Chars[i];

                    int id = ch.ID;

                    if (id < 0)
                    {
                        continue;
                    }

                    int x     = ch.X;
                    int y     = ch.Y;
                    int xoffs = ch.XOffset;
                    int yoffs = ch.YOffset;
                    int w     = ch.Width;
                    int h     = ch.Height;

                    fontInfo.charInfo[ch.ID].validChar = true;
                    fontInfo.charInfo[ch.ID].xAdvance  = ch.XAdvance;
                    fontInfo.charInfo[ch.ID].srcRect   = new RectangleF(x, y, w, h);
                    fontInfo.charInfo[ch.ID].dstRect   = new RectangleF(xoffs, yoffs, w, h);
                }


                var letterHeights = input.Chars
                                    .Where(ch1 => char.IsUpper((char)(ch1.ID)))
                                    .Select(ch2 => ch2.Height)
                                    .OrderBy(h => h)
                                    .ToList();
                CapHeight = letterHeights[letterHeights.Count / 2];



                //	process kerning info :
                for (int i = 0; i < input.Kernings.Count; i++)
                {
                    var pair    = new Tuple <char, char>((char)input.Kernings[i].First, (char)input.Kernings[i].Second);
                    int kerning = input.Kernings[i].Amount;
                    fontInfo.kernings.Add(pair, kerning);
                }

                SpaceWidth = MeasureString(" ").Width;
                LineHeight = MeasureString(" ").Height;
            }
        }