コード例 #1
0
ファイル: Font.cs プロジェクト: IedSoftworks/SMRendererV3
        /// <summary>
        /// (Re-)Generates the texture.
        /// </summary>
        public void RegenerateTexture()
        {
            Width = Height = 0;

            //Height = Math.Abs(_fontFace.BBox.Bottom) + _fontFace.BBox.Top;
            Positions = new Dictionary <char, CharParameter>();

            _fontFace.SetCharSize(0, FontSize, 0, 96);

            var pos = new Dictionary <char, float[]>();

            foreach (char c in CharSet)
            {
                _fontFace.LoadChar(c, LoadFlags.Render, LoadTarget.Normal);

                pos.Add(c, new [] { (float)_fontFace.Glyph.Bitmap.Width, Width });
                Width += (int)_fontFace.Glyph.Advance.X + 2;
                Height = Math.Max(_fontFace.Glyph.Bitmap.Rows, Height);
            }

            _fontFace.LoadChar('_', LoadFlags.Render, LoadTarget.Normal);
            SpaceWidth = _fontFace.Glyph.Advance.X.ToSingle();

            float bBoxHeight   = (Math.Abs(_fontFace.BBox.Bottom) + _fontFace.BBox.Top);
            float bBoxTopScale = _fontFace.BBox.Top / bBoxHeight;
            float baseline     = (Height * bBoxTopScale) + BaselineAdjust;

            Map = new Bitmap(Width, Height);
            using (Graphics g = Graphics.FromImage(Map))
            {
                g.Clear(Color.Transparent);


                foreach (var keyvalue in pos)
                {
                    _fontFace.LoadChar(keyvalue.Key, LoadFlags.Render, LoadTarget.Normal);

                    int y = ((int)baseline - (int)_fontFace.Glyph.Metrics.HorizontalBearingY);
                    g.DrawImageUnscaled(_fontFace.Glyph.Bitmap.ToGdipBitmap(Color.White), (int)keyvalue.Value[1], y);

                    Vector2 offset = new Vector2(keyvalue.Value[1] / Width, 0);
                    Vector2 scale  = new Vector2(keyvalue.Value[0] / Width, 1);
                    Positions.Add(keyvalue.Key, new CharParameter()
                    {
                        Advance  = (int)_fontFace.Glyph.LinearHorizontalAdvance,
                        BearingX = _fontFace.Glyph.BitmapLeft,

                        Width = keyvalue.Value[0],

                        TextureMatrix = TextureTransformation.CalculateMatrix(offset,
                                                                              scale, 0),
                    });
                }
            }
        }
コード例 #2
0
 /// <summary>
 /// Adjusts <see cref="Size"/> for the texture transform.
 /// <para>In this way you can make sure, the texture is not stretched.</para>
 /// </summary>
 /// <param name="transform"></param>
 public void AdjustSizeToTextureTransform(TextureTransformation transform)
 {
     Size.Set(transform.Scale * Size);
 }