当前位置: 首页>>代码示例>>C#>>正文


C# Font.GetTextWidth方法代码示例

本文整理汇总了C#中Font.GetTextWidth方法的典型用法代码示例。如果您正苦于以下问题:C# Font.GetTextWidth方法的具体用法?C# Font.GetTextWidth怎么用?C# Font.GetTextWidth使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Font的用法示例。


在下文中一共展示了Font.GetTextWidth方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: DrawSprite

        /// <summary>
        /// Draws the sprite. and Add the scene.
        /// 文字列(のsprite)を作成するメソッド
        /// </summary>
        /// <returns>
        /// The sprite.
        /// </returns>
        /// <param name='drawStr'>
        /// Draw string.
        /// </param>
        /// <param name='positionX'>
        /// Position x.
        /// </param>
        /// <param name='positionY'>
        /// Position y.
        /// </param>
        /// <param name='fontSize'>
        /// Font size.
        /// </param>
        /// <param name='fontColor'>
        /// Font color.
        /// </param>
        public static SpriteUV DrawSprite(String drawStr,float positionX, float positionY,
		                                   int fontSize,ImageColor fontColor)
        {
            //Director.Initialize(); //重要なんで最初にinitializeしようね。GameEngine2Dのために必要(たぶんほかにも)
            //Scene  //Sce.Pss.HighLevel.GameEngine2DのScene()
            //scene.Camera.SetViewFromViewport(); //CameraをDisplayで使う たぶん画面サイズ -> 3DCGソフトでいうところのカメラ。それの移してる範囲=画面サイズ
            positionX = Const.DISPLAY_WIDTH - Const.DISPLAY_WIDTH*(Const.GRID_NUM_X-positionX)/Const.GRID_NUM_X;
            positionY = Const.DISPLAY_HEIGHT - Const.DISPLAY_HEIGHT*(Const.GRID_NUM_Y-positionY)/Const.GRID_NUM_Y;
            //the Director's OpenGL context たぶん画面サイズの幅
            //var width = (int)Const.getVITA_DISPLAY_WIDTH();
            //var height = (int)Const.getVITA_DISPLAY_HEIGHT();

            //Font font = new Font(FontAlias.System, fontSize, FontStyle.Regular);
            Font font = new Font(Const.DEFAULT_FONT, fontSize, FontStyle.Regular); //日本語使う
            var width = font.GetTextWidth(drawStr);
            var height = font.GetTextWidth("oo");
            //Console.WriteLine(width + " : " + height);

            //Image classはimageデータを保つよ~。 PNG or JPGなど
            //今回はblankのnew Image でも、これ作らないと img.DrawTextが使えない
            Image img = new Image(ImageMode.Rgba,
                                  new ImageSize(width,height), //固定で。たぶん描画範囲
                                  new ImageColor(255,0,0,0));//blankなので変更しても意味なし
            //ここで文字を作ってる
            img.DrawText(drawStr,
                         fontColor,
                         font,
                         new ImagePosition(0, 0)); //画面の位置

            //textureを作る
            Texture2D texture = new Texture2D(width,
                                              height,
                                              false,
                                              PixelFormat.Rgba);

            //Texture2D を使うためにimageからpixelデータに変換(img.ToBuffer())
            //それをtexture.SetPixelsでtextureにPixcelデータを貼り付け
            texture.SetPixels(0, img.ToBuffer());
            //で一覧のimgの処理を実行
            img.Dispose();

            //新しいTexture作るときはTextureInfoも作らないとね
            TextureInfo ti = new TextureInfo();
            ti.Texture = texture;

            //スプライト(イメージパターン)を作る。まぁ、テクスチャだし
            SpriteUV sprite = new SpriteUV();
            sprite.TextureInfo = ti;

            //テクスチャーの貼り付けられた長方形(quad.S)を作成。サイズはImage img = new Imageのとこ。たぶん
            sprite.Quad.S = ti.TextureSizef;
            sprite.CenterSprite();
            //sprite.Position = scene.Camera.CalcBounds().Center;

            positionX = positionX + sprite.Quad.S.X/2;
            positionY = Const.DISPLAY_HEIGHT- positionY - sprite.Quad.S.Y;

            sprite.Position = new Vector2(positionX,positionY);

            return sprite;
        }
开发者ID:k2wanko,项目名称:PSMGameJamWinter,代码行数:83,代码来源:WriteString.cs

示例2: CreateTextureFromText

        /// <summary>
        /// 文字列からテクスチャ作成
        /// </summary>
        /// <returns>
        /// テクスチャ
        /// </returns>
        /// <param name='text'>
        /// 文字列
        /// </param>
        /// <param name='font'>
        /// フォント
        /// </param>
        /// <param name='argb'>
        /// ARGB
        /// </param>
        public static Texture2D CreateTextureFromText(string text, Font font, uint argb)
        {
            int width = font.GetTextWidth(text, 0, text.Length);
            int height = font.Metrics.Height;

            var image = new Image(ImageMode.Rgba, new ImageSize(width,height),new ImageColor(0,0,0,0));

            image.DrawText(text,new ImageColor((int)((argb >> 16) & 0xff),
                                               (int)((argb >> 8) & 0xff),
                                               (int)((argb >> 0) & 0xff),
                                               (int)((argb >> 24) & 0xff)),font,new ImagePosition(0,0));

            var texture = new Texture2D(width,height,false, PixelFormat.Rgba);
            texture.SetPixels(0, image.ToBuffer());

            image.Dispose();
            font.Dispose();
            return texture;
        }
开发者ID:noradium,项目名称:Black-Rins-ambition,代码行数:34,代码来源:Convert.cs

示例3: Sprite

        /// コンストラクタ(文字)
        public Sprite(string text, uint argb, Font font, int positionX, int positionY)
        {
            int width = font.GetTextWidth(text, 0, text.Length);
            int height = font.Metrics.Height;

            var image = new Image(ImageMode.Rgba,
                              new ImageSize(width, height),
                              new ImageColor(0, 0, 0, 0));

            image.DrawText(text,
                       new ImageColor((int)((argb >> 16) & 0xff),
                                      (int)((argb >> 8) & 0xff),
                                      (int)((argb >> 0) & 0xff),
                                      (int)((argb >> 24) & 0xff)),
                       font, new ImagePosition(0, 0));

            var texture = new Texture2D(width, height, false, PixelFormat.Rgba);
            texture.SetPixels(0, image.ToBuffer());
            image.Dispose();

            SetTexture(texture);
            SetDrawRect(0, 0, texture.Width, texture.Height);

            PositionX = positionX;
            PositionY = positionY;

            CenterX = texture.Width / 2;
            CenterY = texture.Height / 2;
            Degree = 0.0f;

            ScaleX = 1.0f;
            ScaleY = 1.0f;
            Visible = true;
            Alpha	= 1.0f;
        }
开发者ID:hatano0x06,项目名称:Coroppoxus,代码行数:36,代码来源:Sprite.cs

示例4: Initialize

        /// <summary>
        /// </summary>
        /// <param name="font">The font to use to render characters. Note that FontMap disposes of this Font object.</param>
        /// <param name="charset">A string containing all the characters you will ever need when drawing text with this FontMap.</param>
        /// <param name="fontmap_width">The internal with used by the texture (height is adjusted automatically).</param>
        public void Initialize( Font font, string charset, int fontmap_width = 512 )
        {
            CharSet = new Dictionary< char, CharData >();

            CharPixelHeight = font.Metrics.Height;

            Image image = null;
            Vector2i totalsize = new Vector2i( 0, 0 );

            for ( int k=0; k < 2; ++k )
            {
                Vector2i turtle = new Vector2i( 0, 0 );	// turtle is in Sce.PlayStation.Core.Imaging.Font's coordinate system
                int max_height = 0;

                for ( int i=0; i < charset.Length; ++i )
                {
                    if ( CharSet.ContainsKey( charset[i] ) )
                        continue; // this character is already in the map

                    Vector2i char_size = new Vector2i(
                        font.GetTextWidth( charset[i].ToString(), 0, 1 ),
                        font.Metrics.Height
                        );

                    max_height = Common.Max( max_height, char_size.Y );

                    if ( turtle.X + char_size.X > fontmap_width )
                    {
                        // hit the right side, go to next line
                        turtle.X = 0;
                        turtle.Y += max_height;	// Sce.PlayStation.Core.Imaging.Font's coordinate system: top is 0, so we += to move down
                        max_height = 0;

                        // make sure we are noit going to newline forever due to lack of fontmap_width
                        Common.Assert( char_size.Y <= fontmap_width );
                    }

                    if ( k > 0 )
                    {
                        // that starts from top left
                        image.DrawText( charset[i].ToString(), new ImageColor(255,255,255,255), font
                                        , new ImagePosition( turtle.X, turtle.Y ) );

                        var uv = new Bounds2( turtle.Vector2() / totalsize.Vector2()
                                             , ( turtle + char_size ).Vector2() / totalsize.Vector2() );

                        // now fix the UV to be in GameEngine2D's UV coordinate system, where 0,0 is bottom left
                        uv = uv.OutrageousYVCoordFlip().OutrageousYTopBottomSwap();

                        CharSet.Add( charset[i], new CharData(){ UV = uv, PixelSize = char_size.Vector2()} );
                    }

                    turtle.X += char_size.X;

                    if ( k == 0 )
                    {
                        totalsize.X = Common.Max( totalsize.X, turtle.X );
                        totalsize.Y = Common.Max( totalsize.Y, turtle.Y + max_height );
                    }
                }

                if ( k == 0 )
                {
            //					System.Console.WriteLine( "FontMap.Initialize: totalsize " + totalsize );
                    image = new Image( ImageMode.A, new ImageSize( totalsize.X, totalsize.Y ), new ImageColor(0,0,0,0) );

                    CharSet.Clear(); // we want to go through the same add logic on second pass, so clear
                }
            }

            Texture = new Texture2D( image.Size.Width, image.Size.Height, false, PixelFormat.Luminance );
            Texture.SetPixels( 0, image.ToBuffer() );
            //			image.Export("uh?","hey.png");
            image.Dispose();

            {
                // cache ascii entries so we can skip TryGetValue logic for those
                m_ascii_char_data = new CharData[ AsciiCharSet.Length ];
                m_ascii_char_data_valid = new bool[ AsciiCharSet.Length ];
                for ( int i=0; i < AsciiCharSet.Length; ++i )
                {
                    CharData cdata;
                    m_ascii_char_data_valid[i] = CharSet.TryGetValue( AsciiCharSet[i], out cdata );
                    m_ascii_char_data[i] = cdata;
                }
            }

            // dispose of the font by default
            font.Dispose();
        }
开发者ID:artron33,项目名称:PsmFramework,代码行数:95,代码来源:FontMap.cs


注:本文中的Font.GetTextWidth方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。