/// <summary>
        /// Gets the rotated alignments given the specified angle.
        /// </summary>
        /// <param name="angle">The angle.</param>
        /// <param name="defaultHorizontalAlignment">The default horizontal alignment.</param>
        /// <param name="defaultVerticalAlignment">The default vertical alignment.</param>
        /// <param name="ha">The rotated horizontal alignment.</param>
        /// <param name="va">The rotated vertical alignment.</param>
        private static void GetRotatedAlignments(double angle, HorizontalTextAlign defaultHorizontalAlignment, VerticalTextAlign defaultVerticalAlignment,
                                                 out HorizontalTextAlign ha, out VerticalTextAlign va)
        {
            ha = defaultHorizontalAlignment;
            va = defaultVerticalAlignment;

            Debug.Assert(angle <= 180 && angle >= -180, "Axis angle should be in the interval [-180,180] degrees.");

            if (angle > -45 && angle < 45)
            {
                return;
            }
            if (angle > 135 || angle < -135)
            {
                ha = (HorizontalTextAlign)(-(int)defaultHorizontalAlignment);
                va = (VerticalTextAlign)(-(int)defaultVerticalAlignment);
                return;
            }
            if (angle > 45)
            {
                ha = (HorizontalTextAlign)((int)defaultVerticalAlignment);
                va = (VerticalTextAlign)(-(int)defaultHorizontalAlignment);
                return;
            }
            if (angle < -45)
            {
                ha = (HorizontalTextAlign)(-(int)defaultVerticalAlignment);
                va = (VerticalTextAlign)((int)defaultHorizontalAlignment);
                return;
            }
        }
        public void SetTextAlign(HorizontalTextAlign align)
        {
            var textAlign = "";

            switch (align)
            {
            case HorizontalTextAlign.Center:
                textAlign = "center";
                break;

            case HorizontalTextAlign.Left:
                textAlign = "left";
                break;

            case HorizontalTextAlign.Right:
                textAlign = "right";
                break;

            default:
                throw new Exception("Invalid HorizontalTextAlign");
            }
            _jsRuntime.InvokeVoid(
                _p5InvokeFunction,
                "textAlign",
                textAlign
                );
        }
Esempio n. 3
0
        public override void RenderIcon(int icon, int x, int y, Color color, HorizontalTextAlign hAlign = HorizontalTextAlign.Center, VerticalTextAlign vAlign = VerticalTextAlign.Center)
        {
            var fnt = FontEngine.IconFont;

            if (fnt != null)
            {
                x += transX;
                y += transY;
                var    pos  = new Microsoft.Xna.Framework.Vector2(x, y);
                var    col  = color.GetColor();
                string txt  = "" + (char)icon;
                var    ft   = fnt.GetFont();
                var    size = ft.MeasureString(txt);
                if (hAlign == HorizontalTextAlign.Center)
                {
                    pos.X -= size.Width / 2;
                }
                else if (hAlign == HorizontalTextAlign.Left)
                {
                }
                else if (hAlign == HorizontalTextAlign.Right)
                {
                    pos.X -= size.Width;
                }
                if (vAlign == VerticalTextAlign.Center)
                {
                    pos.Y -= size.Height / 2;
                }
                else if (vAlign == VerticalTextAlign.Bottom)
                {
                    pos.Y -= size.Height;
                }
                batch.DrawString(fnt.GetFont(), txt, pos, col);
            }
        }
        /// <summary>
        /// Gets the coordinates of the (rotated) background rectangle.
        /// </summary>
        /// <param name="position">
        /// The position.
        /// </param>
        /// <param name="size">
        /// The size.
        /// </param>
        /// <param name="padding">
        /// The padding.
        /// </param>
        /// <param name="rotation">
        /// The rotation.
        /// </param>
        /// <param name="horizontalAlignment">
        /// The horizontal alignment.
        /// </param>
        /// <param name="verticalAlignment">
        /// The vertical alignment.
        /// </param>
        /// <returns>
        /// The background rectangle coordinates.
        /// </returns>
        private static IList <ScreenPoint> GetTextBounds(
            ScreenPoint position,
            OxySize size,
            OxyThickness padding,
            double rotation,
            HorizontalTextAlign horizontalAlignment,
            VerticalTextAlign verticalAlignment)
        {
            double left, right, top, bottom;

            switch (horizontalAlignment)
            {
            case HorizontalTextAlign.Center:
                left  = -size.Width * 0.5;
                right = -left;
                break;

            case HorizontalTextAlign.Right:
                left  = -size.Width;
                right = 0;
                break;

            default:
                left  = 0;
                right = size.Width;
                break;
            }

            switch (verticalAlignment)
            {
            case VerticalTextAlign.Middle:
                top    = -size.Height * 0.5;
                bottom = -top;
                break;

            case VerticalTextAlign.Bottom:
                top    = -size.Height;
                bottom = 0;
                break;

            default:
                top    = 0;
                bottom = size.Height;
                break;
            }

            double cost    = Math.Cos(rotation / 180 * Math.PI);
            double sint    = Math.Sin(rotation / 180 * Math.PI);
            var    u       = new ScreenVector(cost, sint);
            var    v       = new ScreenVector(-sint, cost);
            var    polygon = new ScreenPoint[4];

            polygon[0] = position + u * (left - padding.Left) + v * (top - padding.Top);
            polygon[1] = position + u * (right + padding.Right) + v * (top - padding.Top);
            polygon[2] = position + u * (right + padding.Right) + v * (bottom + padding.Bottom);
            polygon[3] = position + u * (left - padding.Left) + v * (bottom + padding.Bottom);
            return(polygon);
        }
Esempio n. 5
0
 /// <summary>
 /// Called when this instance starts
 /// </summary>
 void Start()
 {
     mOldText             = Text;
     mOldScale            = Scale;
     mOldLineSpacingScale = LineSpacingScale;
     mOldVerticalAlign    = VerticalAlign;
     mOldHorizontalAlign  = HorizontalAlign;
     mOldTextColor        = TextColor;
 }
Esempio n. 6
0
 /// <summary>
 /// Draws the text.
 /// </summary>
 /// <param name="p">
 /// The p.
 /// </param>
 /// <param name="text">
 /// The text.
 /// </param>
 /// <param name="fill">
 /// The fill color.
 /// </param>
 /// <param name="fontFamily">
 /// The font family.
 /// </param>
 /// <param name="fontSize">
 /// Size of the font.
 /// </param>
 /// <param name="fontWeight">
 /// The font weight.
 /// </param>
 /// <param name="rotate">
 /// The rotatation angle.
 /// </param>
 /// <param name="halign">
 /// The horizontal alignment.
 /// </param>
 /// <param name="valign">
 /// The vertical alignment.
 /// </param>
 /// <param name="maxSize">
 /// The maximum size of the text.
 /// </param>
 public abstract void DrawText(
     ScreenPoint p,
     string text,
     OxyColor fill,
     string fontFamily,
     double fontSize,
     double fontWeight,
     double rotate,
     HorizontalTextAlign halign,
     VerticalTextAlign valign,
     OxySize?maxSize);
        public void SetTextAlign(HorizontalTextAlign horizontalAlign, VerticalTextAlign verticalAlign)
        {
            var horizontalTextAlign = "";

            switch (horizontalAlign)
            {
            case HorizontalTextAlign.Center:
                horizontalTextAlign = "center";
                break;

            case HorizontalTextAlign.Left:
                horizontalTextAlign = "left";
                break;

            case HorizontalTextAlign.Right:
                horizontalTextAlign = "right";
                break;

            default:
                throw new Exception("Invalid HorizontalTextAlign");
            }
            var verticalTextAlign = "";

            switch (verticalAlign)
            {
            case VerticalTextAlign.Baseline:
                verticalTextAlign = "alphabetic";
                break;

            case VerticalTextAlign.Bottom:
                verticalTextAlign = "bottom";
                break;

            case VerticalTextAlign.Center:
                verticalTextAlign = "center";
                break;

            case VerticalTextAlign.Top:
                verticalTextAlign = "top";
                break;

            default:
                throw new Exception("Invalid VerticalTextAlign");
            }
            _jsRuntime.InvokeVoid(
                _p5InvokeFunction,
                "textAlign",
                horizontalTextAlign,
                verticalTextAlign
                );
        }
 /// <summary>
 /// Draws text containing sub- and superscript.
 /// </summary>
 /// <param name="rc">The render context.</param>
 /// <param name="pt">The point.</param>
 /// <param name="text">The text.</param>
 /// <param name="textColor">Color of the text.</param>
 /// <param name="fontFamily">The font family.</param>
 /// <param name="fontSize">The font size.</param>
 /// <param name="fontWeight">The font weight.</param>
 /// <param name="angle">The angle.</param>
 /// <param name="ha">The horizontal alignment.</param>
 /// <param name="va">The vertical alignment.</param>
 /// <param name="maxsize">The maximum size of the text.</param>
 /// <example>
 /// Subscript: H_{2}O
 /// Superscript: E=mc^{2}
 /// Both: A^{2}_{i,j}
 /// </example>
 public static void DrawMathText(
     this IRenderContext rc,
     ScreenPoint pt,
     string text,
     OxyColor textColor,
     string fontFamily,
     double fontSize,
     double fontWeight,
     double angle,
     HorizontalTextAlign ha,
     VerticalTextAlign va,
     OxySize?maxsize = null)
 {
     DrawMathText(rc, pt, text, textColor, fontFamily, fontSize, fontWeight, angle, ha, va, maxsize, false);
 }
Esempio n. 9
0
        public override void RenderIcon(int icon, int x, int y, Color color, HorizontalTextAlign hAlign = HorizontalTextAlign.Center, VerticalTextAlign vAlign = VerticalTextAlign.Center)
        {
            var fnt = FontEngine.IconFont;

            if (fnt != null)
            {
                x += transX;
                y += transY;
                SDL.SDL_Color col = new SDL.SDL_Color();
                col.a = color.Alpha;
                col.r = color.R;
                col.g = color.G;
                col.b = color.B;
                IntPtr txt  = SDL_ttf.TTF_RenderGlyph_Blended(fnt.GetFont(), (ushort)icon, col);
                IntPtr txt2 = SDL.SDL_CreateTextureFromSurface(game.ren, txt);
                SDL.SDL_QueryTexture(txt2, out uint format, out int access, out int w, out int h);
                int tx = x;
                int ty = y;
                switch (hAlign)
                {
                case HorizontalTextAlign.Center:
                    tx -= w / 2;
                    break;

                case HorizontalTextAlign.Right:
                    tx -= w;
                    break;
                }
                switch (vAlign)
                {
                case VerticalTextAlign.Center:
                    ty -= h / 2;
                    break;

                case VerticalTextAlign.Bottom:
                    ty -= h;
                    break;
                }
                SDL.SDL_Rect rect = new SDL.SDL_Rect();
                rect.x = tx;
                rect.y = ty;
                rect.w = w;
                rect.h = h;
                SDL.SDL_RenderCopy(game.ren, txt2, IntPtr.Zero, ref rect);
                SDL.SDL_DestroyTexture(txt2);
                SDL.SDL_FreeSurface(txt);
            }
        }
Esempio n. 10
0
        public GuiButton(Rectangle area, string text, VerticalTextAlign verticalAlign = VerticalTextAlign.Center, HorizontalTextAlign horizontalAlign = HorizontalTextAlign.Center)
        {
            if (buttonFont == null)
            {
                buttonFont = ContentHelper.GetFont("KenneySpace");
            }

            this.area            = area;
            this.text            = text;
            this.verticalAlign   = verticalAlign;
            this.horizontalAlign = horizontalAlign;

            AssignDrawPositions();

            InputHelper.ButtonPressed += OnButtonPressed;
            InputHelper.MouseMoved    += OnMouseMoved;
        }
 /// <summary>
 /// Draws the clipped text.
 /// </summary>
 /// <param name="rc">The rendering context.</param>
 /// <param name="clippingRectangle">The clipping rectangle.</param>
 /// <param name="p">The position.</param>
 /// <param name="text">The text.</param>
 /// <param name="fill">The fill color.</param>
 /// <param name="fontFamily">The font family.</param>
 /// <param name="fontSize">Size of the font.</param>
 /// <param name="fontWeight">The font weight.</param>
 /// <param name="rotate">The rotation angle.</param>
 /// <param name="horizontalAlignment">The horizontal align.</param>
 /// <param name="verticalAlignment">The vertical align.</param>
 /// <param name="maxSize">Size of the max.</param>
 public static void DrawClippedText(
     this IRenderContext rc,
     OxyRect clippingRectangle,
     ScreenPoint p,
     string text,
     OxyColor fill,
     string fontFamily = null,
     double fontSize   = 10,
     double fontWeight = 500,
     double rotate     = 0,
     HorizontalTextAlign horizontalAlignment = HorizontalTextAlign.Left,
     VerticalTextAlign verticalAlignment     = VerticalTextAlign.Top,
     OxySize?maxSize = null)
 {
     if (clippingRectangle.Contains(p.X, p.Y))
     {
         rc.DrawText(p, text, fill, fontFamily, fontSize, fontWeight, rotate, horizontalAlignment, verticalAlignment, maxSize);
     }
 }
        /// <summary>
        /// Draws the text.
        /// </summary>
        /// <param name="p">The p.</param>
        /// <param name="text">The text.</param>
        /// <param name="c">The c.</param>
        /// <param name="fontFamily">The font family.</param>
        /// <param name="fontSize">Size of the font.</param>
        /// <param name="fontWeight">The font weight.</param>
        /// <param name="rotate">The rotate.</param>
        /// <param name="halign">The halign.</param>
        /// <param name="valign">The valign.</param>
        /// <param name="maxSize">Size of the max.</param>
        public override void DrawText(
            ScreenPoint p,
            string text,
            OxyColor c,
            string fontFamily,
            double fontSize,
            double fontWeight,
            double rotate,
            HorizontalTextAlign halign,
            VerticalTextAlign valign,
            OxySize?maxSize)
        {
            if (string.IsNullOrEmpty(text))
            {
                return;
            }

            var lines = Regex.Split(text, "\r\n");

            if (valign == VerticalTextAlign.Bottom)
            {
                for (var i = lines.Length - 1; i >= 0; i--)
                {
                    var line = lines[i];
                    var size = this.MeasureText(line, fontFamily, fontSize, fontWeight);
                    this.w.WriteText(p, line, c, fontFamily, fontSize, fontWeight, rotate, halign, valign);

                    p.X += Math.Sin(rotate / 180.0 * Math.PI) * size.Height;
                    p.Y -= Math.Cos(rotate / 180.0 * Math.PI) * size.Height;
                }
            }
            else
            {
                foreach (var line in lines)
                {
                    var size = this.MeasureText(line, fontFamily, fontSize, fontWeight);
                    this.w.WriteText(p, line, c, fontFamily, fontSize, fontWeight, rotate, halign, valign);

                    p.X -= Math.Sin(rotate / 180.0 * Math.PI) * size.Height;
                    p.Y += Math.Cos(rotate / 180.0 * Math.PI) * size.Height;
                }
            }
        }
        /// <summary>
        /// Gets the axis title position, rotation and alignment.
        /// </summary>
        /// <param name="axis">
        /// The axis.
        /// </param>
        /// <param name="titlePosition">
        /// The title position.
        /// </param>
        /// <param name="angle">
        /// The angle.
        /// </param>
        /// <param name="halign">
        /// The horizontal alignment.
        /// </param>
        /// <param name="valign">
        /// The vertical alignment.
        /// </param>
        /// <returns>
        /// The <see cref="ScreenPoint"/>.
        /// </returns>
        protected virtual ScreenPoint GetAxisTitlePositionAndAlignment(
            Axis axis,
            double titlePosition,
            ref double angle,
            ref HorizontalTextAlign halign,
            ref VerticalTextAlign valign)
        {
            double middle = axis.IsHorizontal()
                                ? Lerp(axis.ScreenMin.X, axis.ScreenMax.X, axis.TitlePosition)
                                : Lerp(axis.ScreenMax.Y, axis.ScreenMin.Y, axis.TitlePosition);

            if (axis.PositionAtZeroCrossing)
            {
                var perpendicularAxis = axis.IsHorizontal() ? this.Plot.DefaultYAxis : this.Plot.DefaultXAxis;
                middle = perpendicularAxis.Transform(perpendicularAxis.ActualMaximum);
            }

            switch (axis.Position)
            {
            case AxisPosition.Left:
                return(new ScreenPoint(titlePosition, middle));

            case AxisPosition.Right:
                valign = VerticalTextAlign.Bottom;
                return(new ScreenPoint(titlePosition, middle));

            case AxisPosition.Top:
                halign = HorizontalTextAlign.Center;
                valign = VerticalTextAlign.Top;
                angle  = 0;
                return(new ScreenPoint(middle, titlePosition));

            case AxisPosition.Bottom:
                halign = HorizontalTextAlign.Center;
                valign = VerticalTextAlign.Bottom;
                angle  = 0;
                return(new ScreenPoint(middle, titlePosition));

            default:
                throw new ArgumentOutOfRangeException("axis");
            }
        }
Esempio n. 14
0
        public override void RenderIcon(int icon, int x, int y, Color color, HorizontalTextAlign hAlign = HorizontalTextAlign.Center, VerticalTextAlign vAlign = VerticalTextAlign.Center)
        {
            var fnt = FontEngine.IconFont;

            if (fnt != null)
            {
                x += transX;
                y += transY;
                string txt  = new string(new char[] { (char)icon });
                float  fx   = x;
                float  fy   = y;
                var    ft   = fnt.GetFont();
                var    size = gfx.MeasureString(txt, ft);
                if (hAlign == HorizontalTextAlign.Center)
                {
                    fx -= size.Width / 2;
                }
                else if (hAlign == HorizontalTextAlign.Left)
                {
                }
                else if (hAlign == HorizontalTextAlign.Right)
                {
                    fx -= size.Width;
                }
                if (vAlign == VerticalTextAlign.Center)
                {
                    fy -= size.Height / 2;
                }
                else if (vAlign == VerticalTextAlign.Top)
                {
                }
                else if (vAlign == VerticalTextAlign.Bottom)
                {
                    fy -= size.Height;
                }
                using (var brush = new System.Drawing.SolidBrush(color.GetColor()))
                {
                    gfx.DrawString(txt, fnt.GetFont(), brush, fx, fy);
                }
            }
        }
Esempio n. 15
0
    void Update()
    {
        // Runtime on devices
        if (Font == null)
        {
            return;
        }
        var parentFont = (BitmapFont)Font.GetComponent(typeof(BitmapFont));

        if (parentFont == null)
        {
            return;
        }
        if (mOldText != Text ||
            Scale != mOldScale ||
            HorizontalAlign != mOldHorizontalAlign ||
            VerticalAlign != mOldVerticalAlign ||
            LineSpacingScale != mOldLineSpacingScale ||
            mWasCreatedRetina != parentFont.UseRetinaFont)
        {
            mOldText             = Text;
            mOldScale            = Scale;
            mOldLineSpacingScale = LineSpacingScale;
            mOldVerticalAlign    = VerticalAlign;
            mOldHorizontalAlign  = HorizontalAlign;
            RecreateText(Text);
        }

        if (TextColor.r != mOldTextColor.r ||
            TextColor.g != mOldTextColor.g ||
            TextColor.b != mOldTextColor.b ||
            TextColor.a != mOldTextColor.a)
        {
            mOldTextColor.r = TextColor.r;
            mOldTextColor.g = TextColor.g;
            mOldTextColor.b = TextColor.b;
            mOldTextColor.a = TextColor.a;
            SetColor(TextColor);
        }
    }
 /// <summary>
 /// Draws the clipped text.
 /// </summary>
 /// <param name="rc">The rendering context.</param>
 /// <param name="clippingRectangle">The clipping rectangle.</param>
 /// <param name="p">The position.</param>
 /// <param name="text">The text.</param>
 /// <param name="fill">The fill color.</param>
 /// <param name="fontFamily">The font family.</param>
 /// <param name="fontSize">Size of the font.</param>
 /// <param name="fontWeight">The font weight.</param>
 /// <param name="rotate">The rotation angle.</param>
 /// <param name="horizontalAlignment">The horizontal align.</param>
 /// <param name="verticalAlignment">The vertical align.</param>
 /// <param name="maxSize">Size of the max.</param>
 public static void DrawClippedText(
     this IRenderContext rc,
     OxyRect clippingRectangle,
     ScreenPoint p,
     string text,
     OxyColor fill,
     string fontFamily = null,
     double fontSize = 10,
     double fontWeight = 500,
     double rotate = 0,
     HorizontalTextAlign horizontalAlignment = HorizontalTextAlign.Left,
     VerticalTextAlign verticalAlignment = VerticalTextAlign.Top,
     OxySize? maxSize = null)
 {
     if (clippingRectangle.Contains(p.X, p.Y))
     {
         rc.DrawText(p, text, fill, fontFamily, fontSize, fontWeight, rotate, horizontalAlignment, verticalAlignment, maxSize);
     }
 }
        public void DrawText(Point p, string text, Color fill, string fontFamily, double fontSize, double fontWeight, double rotate, HorizontalTextAlign halign, VerticalTextAlign valign)
        {
            var tb = new TextBlock
            {
                Text       = text,
                Foreground = new SolidColorBrush(fill.ToColor())
            };

            if (fontFamily != null)
            {
                tb.FontFamily = new FontFamily(fontFamily);
            }
            if (fontSize > 0)
            {
                tb.FontSize = fontSize;
            }
            if (fontWeight > 0)
            {
                tb.FontWeight = System.Windows.FontWeight.FromOpenTypeWeight((int)fontWeight);
            }

            tb.Measure(new System.Windows.Size(1000, 1000));

            double dx = 0;

            if (halign == HorizontalTextAlign.Center)
            {
                dx = -tb.DesiredSize.Width / 2;
            }
            if (halign == HorizontalTextAlign.Right)
            {
                dx = -tb.DesiredSize.Width;
            }

            double dy = 0;

            if (valign == VerticalTextAlign.Middle)
            {
                dy = -tb.DesiredSize.Height / 2;
            }
            if (valign == VerticalTextAlign.Bottom)
            {
                dy = -tb.DesiredSize.Height;
            }

            var transform = new TransformGroup();

            transform.Children.Add(new TranslateTransform(dx, dy));
            if (rotate != 0)
            {
                transform.Children.Add(new RotateTransform(rotate));
            }
            transform.Children.Add(new TranslateTransform(p.X, p.Y));
            tb.RenderTransform = transform;

            tb.SetValue(RenderOptions.ClearTypeHintProperty, ClearTypeHint.Enabled);

            canvas.Children.Add(tb);
        }
        /// <summary>
        /// Gets the axis title position, rotation and alignment.
        /// </summary>
        /// <param name="axis">
        /// The axis.
        /// </param>
        /// <param name="titlePosition">
        /// The title position.
        /// </param>
        /// <param name="angle">
        /// The angle.
        /// </param>
        /// <param name="halign">
        /// The horizontal alignment.
        /// </param>
        /// <param name="valign">
        /// The vertical alignment.
        /// </param>
        /// <returns>
        /// The <see cref="ScreenPoint"/>.
        /// </returns>
        protected virtual ScreenPoint GetAxisTitlePositionAndAlignment(
            Axis axis, 
            double titlePosition, 
            ref double angle, 
            ref HorizontalTextAlign halign, 
            ref VerticalTextAlign valign)
        {
            double middle = axis.IsHorizontal()
                                ? Lerp(axis.ScreenMin.X, axis.ScreenMax.X, axis.TitlePosition)
                                : Lerp(axis.ScreenMax.Y, axis.ScreenMin.Y, axis.TitlePosition);

            if (axis.PositionAtZeroCrossing)
            {
                var perpendicularAxis = axis.IsHorizontal() ? this.Plot.DefaultYAxis : this.Plot.DefaultXAxis;
                middle = perpendicularAxis.Transform(perpendicularAxis.ActualMaximum);
            }

            switch (axis.Position)
            {
                case AxisPosition.Left:
                    return new ScreenPoint(titlePosition, middle);
                case AxisPosition.Right:
                    valign = VerticalTextAlign.Bottom;
                    return new ScreenPoint(titlePosition, middle);
                case AxisPosition.Top:
                    halign = HorizontalTextAlign.Center;
                    valign = VerticalTextAlign.Top;
                    angle = 0;
                    return new ScreenPoint(middle, titlePosition);
                case AxisPosition.Bottom:
                    halign = HorizontalTextAlign.Center;
                    valign = VerticalTextAlign.Bottom;
                    angle = 0;
                    return new ScreenPoint(middle, titlePosition);
                default:
                    throw new ArgumentOutOfRangeException("axis");
            }
        }
 /// <summary>
 /// Draws the text.
 /// </summary>
 /// <param name="p">
 /// The p.
 /// </param>
 /// <param name="text">
 /// The text.
 /// </param>
 /// <param name="fill">
 /// The fill color.
 /// </param>
 /// <param name="fontFamily">
 /// The font family.
 /// </param>
 /// <param name="fontSize">
 /// Size of the font.
 /// </param>
 /// <param name="fontWeight">
 /// The font weight.
 /// </param>
 /// <param name="rotate">
 /// The rotatation angle.
 /// </param>
 /// <param name="halign">
 /// The horizontal alignment.
 /// </param>
 /// <param name="valign">
 /// The vertical alignment.
 /// </param>
 /// <param name="maxSize">
 /// The maximum size of the text.
 /// </param>
 public abstract void DrawText(
     ScreenPoint p,
     string text,
     OxyColor fill,
     string fontFamily,
     double fontSize,
     double fontWeight,
     double rotate,
     HorizontalTextAlign halign,
     VerticalTextAlign valign,
     OxySize? maxSize);
Esempio n. 20
0
        //
        // Constructors
        //

        public GuiButton(Point position, string text, VerticalTextAlign verticalAlign = VerticalTextAlign.Center, HorizontalTextAlign horizontalAlign = HorizontalTextAlign.Center)
            : this(new Rectangle(position, new Point(DefaultWidth, DefaultHeight)), text, verticalAlign, horizontalAlign)
        {
        }
Esempio n. 21
0
 public abstract void RenderText(Font font, string text, int x, int y, Color color, Color bg, HorizontalTextAlign hAlign = HorizontalTextAlign.Center, VerticalTextAlign vAlign = VerticalTextAlign.Center);
        /// <summary>
        /// Gets the coordinates of the (rotated) background rectangle.
        /// </summary>
        /// <param name="position">
        /// The position.
        /// </param>
        /// <param name="size">
        /// The size.
        /// </param>
        /// <param name="padding">
        /// The padding.
        /// </param>
        /// <param name="rotation">
        /// The rotation.
        /// </param>
        /// <param name="horizontalAlignment">
        /// The horizontal alignment.
        /// </param>
        /// <param name="verticalAlignment">
        /// The vertical alignment.
        /// </param>
        /// <returns>
        /// The background rectangle coordinates.
        /// </returns>
        private static IList<ScreenPoint> GetTextBounds(
            ScreenPoint position,
            OxySize size,
            OxyThickness padding,
            double rotation,
            HorizontalTextAlign horizontalAlignment,
            VerticalTextAlign verticalAlignment)
        {
            double left, right, top, bottom;
            switch (horizontalAlignment)
            {
                case HorizontalTextAlign.Center:
                    left = -size.Width * 0.5;
                    right = -left;
                    break;
                case HorizontalTextAlign.Right:
                    left = -size.Width;
                    right = 0;
                    break;
                default:
                    left = 0;
                    right = size.Width;
                    break;
            }

            switch (verticalAlignment)
            {
                case VerticalTextAlign.Middle:
                    top = -size.Height * 0.5;
                    bottom = -top;
                    break;
                case VerticalTextAlign.Bottom:
                    top = -size.Height;
                    bottom = 0;
                    break;
                default:
                    top = 0;
                    bottom = size.Height;
                    break;
            }

            double cost = Math.Cos(rotation / 180 * Math.PI);
            double sint = Math.Sin(rotation / 180 * Math.PI);
            var u = new ScreenVector(cost, sint);
            var v = new ScreenVector(-sint, cost);
            var polygon = new ScreenPoint[4];
            polygon[0] = position + u * (left - padding.Left) + v * (top - padding.Top);
            polygon[1] = position + u * (right + padding.Right) + v * (top - padding.Top);
            polygon[2] = position + u * (right + padding.Right) + v * (bottom + padding.Bottom);
            polygon[3] = position + u * (left - padding.Left) + v * (bottom + padding.Bottom);
            return polygon;
        }
        public override void DrawText(ScreenPoint p, string text, OxyColor fill, string fontFamily, double fontSize, double fontWeight, double rotate, HorizontalTextAlign halign, VerticalTextAlign valign, OxySize?maxSize)
        {
            //This method needs work not 100% around vertical alignment.
            if (string.IsNullOrEmpty(text))
            {
                return;
            }

            fontFamily = GetDefaultFont(fontFamily);

            if (fontWeight >= 700)
            {
                //fs = FontStyle.Bold;
            }

            //var textSize = MeasureText(text, fontFamily, fontSize, fontWeight);

            if (maxSize != null)
            {
//                if (size.Width > maxSize.Value.Width)
//                {
//                    size.Width = (float)maxSize.Value.Width;
//                }
//
//                if (size.Height > maxSize.Value.Height)
//                {
//                    size.Height = (float)maxSize.Value.Height;
//                }
            }

            gctx.SaveState();

            gctx.SelectFont(fontFamily, (float)fontSize, CGTextEncoding.MacRoman);
            ToColor(fill).SetFill();

            gctx.SetTextDrawingMode(CGTextDrawingMode.Fill);

            var      tfont = UIFont.FromName(fontFamily, (float)fontSize);
            NSString nsstr = new NSString(text);
            SizeF    sz    = nsstr.StringSize(tfont);

            float y = (float)(p.Y);
            float x = (float)(p.X);

            switch (halign)
            {
            case HorizontalTextAlign.Left:
                x = (float)(p.X);
                break;

            case HorizontalTextAlign.Right:
                x = (float)(p.X - sz.Width);
                break;

            case HorizontalTextAlign.Center:
                x = (float)(p.X - (sz.Width / 2));
                break;
            }

            switch (valign)
            {
            case VerticalTextAlign.Bottom:
                y -= (float)fontSize;
                break;

            case VerticalTextAlign.Top:
//				y += (float)fontSize;
                break;

            case VerticalTextAlign.Middle:
                y -= (float)(fontSize / 2);
                break;
            }


            RectangleF rect = new RectangleF(x, y, sz.Width, sz.Height);

            nsstr.DrawString(rect, tfont);

            gctx.RestoreState();
            //Console.WriteLine("X:{0:###} Y:{1:###} HA:{2}:{3:###} VA:{4}:{5:###} TW:{6:###} - {7}", p.X, p.Y, halign, x, valign, y, textSize.Width, text);
        }
Esempio n. 24
0
 public abstract void RenderIcon(int icon, int x, int y, Color color, HorizontalTextAlign hAlign = HorizontalTextAlign.Center, VerticalTextAlign vAlign = VerticalTextAlign.Center);
Esempio n. 25
0
        public override void RenderText(Font font, string text, int x, int y, Color color, Color bg, HorizontalTextAlign hAlign = HorizontalTextAlign.Center, VerticalTextAlign vAlign = VerticalTextAlign.Center)
        {
            if (font == null)
            {
                return;
            }
            x += transX;
            y += transY;
            var c    = color.GetColor();
            var fnt  = font.GetFont();
            var size = fnt.MeasureString(text);
            var pos  = new Microsoft.Xna.Framework.Vector2(x, y);

            if (hAlign == HorizontalTextAlign.Center)
            {
                pos.X -= size.Width / 2;
            }
            else if (hAlign == HorizontalTextAlign.Left)
            {
            }
            else if (hAlign == HorizontalTextAlign.Right)
            {
                pos.X -= size.Width;
            }
            if (vAlign == VerticalTextAlign.Center)
            {
                pos.Y -= size.Height / 2;
            }
            else if (vAlign == VerticalTextAlign.Bottom)
            {
                pos.Y -= size.Height;
            }
            batch.DrawString(fnt, text, pos, c);
        }
        /// <summary>
        /// Draws the text.
        /// </summary>
        /// <param name="p">The p.</param>
        /// <param name="text">The text.</param>
        /// <param name="c">The c.</param>
        /// <param name="fontFamily">The font family.</param>
        /// <param name="fontSize">Size of the font.</param>
        /// <param name="fontWeight">The font weight.</param>
        /// <param name="rotate">The rotate.</param>
        /// <param name="halign">The halign.</param>
        /// <param name="valign">The valign.</param>
        /// <param name="maxSize">Size of the max.</param>
        public override void DrawText(
            ScreenPoint p,
            string text,
            OxyColor c,
            string fontFamily,
            double fontSize,
            double fontWeight,
            double rotate,
            HorizontalTextAlign halign,
            VerticalTextAlign valign,
            OxySize? maxSize)
        {
            if (string.IsNullOrEmpty(text))
            {
                return;
            }

            var lines = Regex.Split(text, "\r\n");
            if (valign == VerticalTextAlign.Bottom)
            {
                for (var i = lines.Length - 1; i >= 0; i--)
                {
                    var line = lines[i];
                    var size = this.MeasureText(line, fontFamily, fontSize, fontWeight);
                    this.w.WriteText(p, line, c, fontFamily, fontSize, fontWeight, rotate, halign, valign);

                    p.X += Math.Sin(rotate / 180.0 * Math.PI) * size.Height;
                    p.Y -= Math.Cos(rotate / 180.0 * Math.PI) * size.Height;
                }
            }
            else
            {
                foreach (var line in lines)
                {
                    var size = this.MeasureText(line, fontFamily, fontSize, fontWeight);
                    this.w.WriteText(p, line, c, fontFamily, fontSize, fontWeight, rotate, halign, valign);

                    p.X -= Math.Sin(rotate / 180.0 * Math.PI) * size.Height;
                    p.Y += Math.Cos(rotate / 180.0 * Math.PI) * size.Height;
                }
            }
        }
        /// <summary>
        /// Gets the alignments given the specified rotation angle.
        /// </summary>
        /// <param name="angle">
        /// The angle.
        /// </param>
        /// <param name="defaultHorizontalAlignment">
        /// The default horizontal alignment.
        /// </param>
        /// <param name="defaultVerticalAlignment">
        /// The default vertical alignment.
        /// </param>
        /// <param name="ha">
        /// The rotated horizontal alignment.
        /// </param>
        /// <param name="va">
        /// The rotated vertical alignment.
        /// </param>
        protected virtual void GetRotatedAlignments(
            double angle, 
            HorizontalTextAlign defaultHorizontalAlignment, 
            VerticalTextAlign defaultVerticalAlignment, 
            out HorizontalTextAlign ha, 
            out VerticalTextAlign va)
        {
            ha = defaultHorizontalAlignment;
            va = defaultVerticalAlignment;

            Debug.Assert(angle <= 180 && angle >= -180, "Axis angle should be in the interval [-180,180] degrees.");

            if (angle > -45 && angle < 45)
            {
                return;
            }

            if (angle > 135 || angle < -135)
            {
                ha = (HorizontalTextAlign)(-(int)defaultHorizontalAlignment);
                va = (VerticalTextAlign)(-(int)defaultVerticalAlignment);
                return;
            }

            if (angle > 45)
            {
                ha = (HorizontalTextAlign)((int)defaultVerticalAlignment);
                va = (VerticalTextAlign)(-(int)defaultHorizontalAlignment);
                return;
            }

            if (angle < -45)
            {
                ha = (HorizontalTextAlign)(-(int)defaultVerticalAlignment);
                va = (VerticalTextAlign)((int)defaultHorizontalAlignment);
            }
        }
Esempio n. 28
0
        public override void RenderText(Font font, string text, int x, int y, Color color, Color bg, HorizontalTextAlign hAlign = HorizontalTextAlign.Center, VerticalTextAlign vAlign = VerticalTextAlign.Center)
        {
            if (font == null)
            {
                return;
            }
            x += transX;
            y += transY;
            SDL.SDL_Color col = new SDL.SDL_Color();
            col.a = color.Alpha;
            col.r = color.R;
            col.g = color.G;
            col.b = color.B;
            SDL.SDL_Color bgcol = new SDL.SDL_Color();
            bgcol.a = bg.Alpha;
            bgcol.r = bg.R;
            bgcol.g = bg.G;
            bgcol.b = bg.B;

            IntPtr txt  = SDL_ttf.TTF_RenderText_Shaded(font.GetFont(), text, col, bgcol);
            IntPtr txt2 = SDL.SDL_CreateTextureFromSurface(game.ren, txt);

            SDL.SDL_QueryTexture(txt2, out uint format, out int access, out int w, out int h);
            int tx = x;
            int ty = y;

            switch (hAlign)
            {
            case HorizontalTextAlign.Center:
                tx -= w / 2;
                break;

            case HorizontalTextAlign.Right:
                tx -= w;
                break;
            }
            switch (vAlign)
            {
            case VerticalTextAlign.Center:
                ty -= h / 2;
                break;

            case VerticalTextAlign.Bottom:
                ty -= h;
                break;
            }
            SDL.SDL_Rect rect = new SDL.SDL_Rect();
            rect.x = tx;
            rect.y = ty;
            rect.w = w;
            rect.h = h;
            SDL.SDL_SetRenderDrawBlendMode(game.ren, SDL.SDL_BlendMode.SDL_BLENDMODE_BLEND);
            SDL.SDL_RenderCopy(game.ren, txt2, IntPtr.Zero, ref rect);
            SDL.SDL_DestroyTexture(txt2);
            SDL.SDL_FreeSurface(txt);
        }
        /// <summary>
        /// Writes text.
        /// </summary>
        /// <param name="position">
        /// The position.
        /// </param>
        /// <param name="text">
        /// The text.
        /// </param>
        /// <param name="fill">
        /// The text color.
        /// </param>
        /// <param name="fontFamily">
        /// The font family.
        /// </param>
        /// <param name="fontSize">
        /// The font size.
        /// </param>
        /// <param name="fontWeight">
        /// The font weight.
        /// </param>
        /// <param name="rotate">
        /// The rotation angle.
        /// </param>
        /// <param name="halign">
        /// The horizontal alignment.
        /// </param>
        /// <param name="valign">
        /// The vertical alignment.
        /// </param>
        public void WriteText(
            ScreenPoint position,
            string text,
            OxyColor fill,
            string fontFamily          = null,
            double fontSize            = 10,
            double fontWeight          = FontWeights.Normal,
            double rotate              = 0,
            HorizontalTextAlign halign = HorizontalTextAlign.Left,
            VerticalTextAlign valign   = VerticalTextAlign.Top)
        {
            // http://www.w3.org/TR/SVG/text.html
            this.WriteStartElement("text");

            // WriteAttributeString("x", position.X);
            // WriteAttributeString("y", position.Y);
            string baselineAlignment = "hanging";

            if (valign == VerticalTextAlign.Middle)
            {
                baselineAlignment = "middle";
            }

            if (valign == VerticalTextAlign.Bottom)
            {
                baselineAlignment = "baseline";
            }

            this.WriteAttributeString("dominant-baseline", baselineAlignment);

            string textAnchor = "start";

            if (halign == HorizontalTextAlign.Center)
            {
                textAnchor = "middle";
            }

            if (halign == HorizontalTextAlign.Right)
            {
                textAnchor = "end";
            }

            this.WriteAttributeString("text-anchor", textAnchor);

            string fmt       = "translate({0:" + this.NumberFormat + "},{1:" + this.NumberFormat + "})";
            string transform = string.Format(CultureInfo.InvariantCulture, fmt, position.X, position.Y);

            if (Math.Abs(rotate) > 0)
            {
                transform += string.Format(CultureInfo.InvariantCulture, " rotate({0})", rotate);
            }

            this.WriteAttributeString("transform", transform);

            if (fontFamily != null)
            {
                this.WriteAttributeString("font-family", fontFamily);
            }

            if (fontSize > 0)
            {
                this.WriteAttributeString("font-size", fontSize);
            }

            if (fontWeight > 0)
            {
                this.WriteAttributeString("font-weight", fontWeight);
            }

            this.WriteAttributeString("fill", this.ColorToString(fill));

            // WriteAttributeString("style", style);
            this.WriteString(text);
            this.WriteEndElement();
        }
        public void RenderLegends()
        {
            double maxWidth    = 0;
            double maxHeight   = 0;
            double totalHeight = 0;

            // Measure
            foreach (var s in plot.Series)
            {
                if (String.IsNullOrEmpty(s.Title))
                {
                    continue;
                }
                var oxySize = rc.MeasureText(s.Title, plot.LegendFont, plot.LegendFontSize);
                if (oxySize.Width > maxWidth)
                {
                    maxWidth = oxySize.Width;
                }
                if (oxySize.Height > maxHeight)
                {
                    maxHeight = oxySize.Height;
                }
                totalHeight += oxySize.Height;
            }

            double lineLength = plot.LegendSymbolLength;

            // Arrange
            double x0 = double.NaN, x1 = double.NaN, y0 = double.NaN;

            //   padding          padding
            //          lineLength
            // y0       -----o----       seriesName
            //          x0               x1

            double sign = 1;

            if (plot.IsLegendOutsidePlotArea)
            {
                sign = -1;
            }

            // Horizontal alignment
            HorizontalTextAlign ha = HorizontalTextAlign.Left;

            switch (plot.LegendPosition)
            {
            case LegendPosition.TopRight:
            case LegendPosition.BottomRight:
                x0 = plot.Bounds.Right - LEGEND_PADDING * sign;
                x1 = x0 - lineLength * sign - LEGEND_PADDING * sign;
                ha = sign == 1 ? HorizontalTextAlign.Right : HorizontalTextAlign.Left;
                break;

            case LegendPosition.TopLeft:
            case LegendPosition.BottomLeft:
                x0 = plot.Bounds.Left + LEGEND_PADDING * sign;
                x1 = x0 + lineLength * sign + LEGEND_PADDING * sign;
                ha = sign == 1 ? HorizontalTextAlign.Left : HorizontalTextAlign.Right;
                break;
            }

            // Vertical alignment
            VerticalTextAlign va = VerticalTextAlign.Middle;

            switch (plot.LegendPosition)
            {
            case LegendPosition.TopRight:
            case LegendPosition.TopLeft:
                y0 = plot.Bounds.Top + LEGEND_PADDING + maxHeight / 2;
                break;

            case LegendPosition.BottomRight:
            case LegendPosition.BottomLeft:
                y0 = plot.Bounds.Bottom - maxHeight + LEGEND_PADDING;
                break;
            }

            foreach (var s in plot.Series)
            {
                if (String.IsNullOrEmpty(s.Title))
                {
                    continue;
                }
                rc.DrawText(new ScreenPoint(x1, y0),
                            s.Title, plot.TextColor,
                            plot.LegendFont, plot.LegendFontSize, 500, 0,
                            ha, va);
                OxyRect rect = new OxyRect(x0 - lineLength, y0 - maxHeight / 2, lineLength, maxHeight);
                if (ha == HorizontalTextAlign.Left)
                {
                    rect = new OxyRect(x0, y0 - maxHeight / 2, lineLength, maxHeight);
                }

                s.RenderLegend(rc, rect);
                if (plot.LegendPosition == LegendPosition.TopLeft || plot.LegendPosition == LegendPosition.TopRight)
                {
                    y0 += maxHeight;
                }
                else
                {
                    y0 -= maxHeight;
                }
            }
        }
Esempio n. 31
0
        private void RenderHorizontalAxis(Axis axis, Axis perpendicularAxis)
        {
            double y = Plot.Bounds.Bottom;

            switch (axis.Position)
            {
            case AxisPosition.Top:
                y = Plot.Bounds.Top;
                break;

            case AxisPosition.Bottom:
                y = Plot.Bounds.Bottom;
                break;
            }
            if (axis.PositionAtZeroCrossing)
            {
                y = perpendicularAxis.TransformX(0);
            }

            double y0, y1;

            if (axis.ShowMinorTicks)
            {
                GetVerticalTickPositions(axis, axis.TickStyle, axis.MinorTickSize, out y0, out y1);

                foreach (double xValue in minorTickValues)
                {
                    if (xValue < axis.ActualMinimum || xValue > axis.ActualMaximum)
                    {
                        continue;
                    }

                    if (majorTickValues.Contains(xValue))
                    {
                        continue;
                    }

                    double x = axis.TransformX(xValue);
                    if (minorPen != null)
                    {
                        RenderLine(x, Plot.Bounds.Top, x, Plot.Bounds.Bottom, minorPen);
                    }
                    RenderLine(x, y + y0, x, y + y1, minorTickPen);
                }
            }

            GetVerticalTickPositions(axis, axis.TickStyle, axis.MajorTickSize, out y0, out y1);

            double maxh  = 0;
            bool   istop = axis.Position == AxisPosition.Top;

            foreach (double xValue in majorTickValues)
            {
                if (xValue < axis.ActualMinimum || xValue > axis.ActualMaximum)
                {
                    continue;
                }

                double x = axis.TransformX(xValue);

                if (majorPen != null)
                {
                    RenderLine(x, Plot.Bounds.Top, x, Plot.Bounds.Bottom, majorPen);
                }
                RenderLine(x, y + y0, x, y + y1, majorTickPen);

                if (xValue == 0 && axis.PositionAtZeroCrossing)
                {
                    continue;
                }

                var    pt   = new ScreenPoint(x, istop ? y + y1 - TICK_DIST : y + y1 + TICK_DIST);
                string text = axis.FormatValue(xValue);
                double h    = rc.MeasureText(text, axis.FontFamily, axis.FontSize, axis.FontWeight).Height;

                rc.DrawText(pt, text, Plot.TextColor,
                            axis.FontFamily, axis.FontSize, axis.FontWeight,
                            axis.Angle,
                            HorizontalTextAlign.Center, istop ? VerticalTextAlign.Bottom : VerticalTextAlign.Top);

                maxh = Math.Max(maxh, h);
            }

            if (axis.PositionAtZeroCrossing)
            {
                double x = axis.TransformX(0);
                RenderLine(x, Plot.Bounds.Top, x, Plot.Bounds.Bottom, zeroPen);
            }

            if (axis.ExtraGridlines != null)
            {
                foreach (double x in axis.ExtraGridlines)
                {
                    if (!IsWithin(x, axis.ActualMinimum, axis.ActualMaximum))
                    {
                        continue;
                    }
                    double sx = axis.TransformX(x);
                    RenderLine(sx, Plot.Bounds.Top, sx, Plot.Bounds.Bottom, extraPen);
                }
            }

            // The horizontal axis line
            RenderLine(Plot.Bounds.Left, y, Plot.Bounds.Right, y, majorPen);

            // The horizontal axis legend (centered horizontally)
            double legendX             = axis.TransformX((axis.ActualMinimum + axis.ActualMaximum) / 2);
            HorizontalTextAlign halign = HorizontalTextAlign.Center;
            VerticalTextAlign   valign = VerticalTextAlign.Bottom;

            if (axis.PositionAtZeroCrossing)
            {
                legendX = perpendicularAxis.TransformX(perpendicularAxis.ActualMaximum);
            }

            double legendY = rc.Height - AXIS_LEGEND_DIST;

            if (istop)
            {
                legendY = AXIS_LEGEND_DIST;
                valign  = VerticalTextAlign.Top;
            }
            rc.DrawText(new ScreenPoint(legendX, legendY),
                        axis.Title, Plot.TextColor,
                        axis.FontFamily, axis.FontSize, axis.FontWeight, 0, halign, valign);
        }
        /// <summary>
        /// Draws or measures text containing sub- and superscript.
        /// </summary>
        /// <param name="rc">The render context.</param>
        /// <param name="pt">The point.</param>
        /// <param name="text">The text.</param>
        /// <param name="textColor">Color of the text.</param>
        /// <param name="fontFamily">The font family.</param>
        /// <param name="fontSize">The font size.</param>
        /// <param name="fontWeight">The font weight.</param>
        /// <param name="angle">The angle.</param>
        /// <param name="ha">The horizontal alignment.</param>
        /// <param name="va">The vertical alignment.</param>
        /// <param name="maxsize">The maximum size of the text.</param>
        /// <param name="measure">Measure the size of the text if set to <c>true</c>.</param>
        /// <returns>The size of the text.</returns>
        /// <example>
        /// Subscript: H_{2}O
        /// Superscript: E=mc^{2}
        /// Both: A^{2}_{i,j}
        /// </example>
        public static OxySize DrawMathText(
            this IRenderContext rc,
            ScreenPoint pt,
            string text,
            OxyColor textColor,
            string fontFamily,
            double fontSize,
            double fontWeight,
            double angle,
            HorizontalTextAlign ha,
            VerticalTextAlign va,
            OxySize?maxsize,
            bool measure)
        {
            if (string.IsNullOrEmpty(text))
            {
                return(OxySize.Empty);
            }

            if (angle.Equals(0) && (text.Contains("^{") || text.Contains("_{")))
            {
                double x = pt.X;
                double y = pt.Y;

                // Measure
                var size = InternalDrawMathText(rc, x, y, text, textColor, fontFamily, fontSize, fontWeight, true);

                switch (ha)
                {
                case HorizontalTextAlign.Right:
                    x -= size.Width;
                    break;

                case HorizontalTextAlign.Center:
                    x -= size.Width * 0.5;
                    break;
                }

                switch (va)
                {
                case VerticalTextAlign.Bottom:
                    y -= size.Height;
                    break;

                case VerticalTextAlign.Middle:
                    y -= size.Height * 0.5;
                    break;
                }

                InternalDrawMathText(rc, x, y, text, textColor, fontFamily, fontSize, fontWeight, false);
                return(measure ? size : OxySize.Empty);
            }

            rc.DrawText(pt, text, textColor, fontFamily, fontSize, fontWeight, angle, ha, va, maxsize);
            if (measure)
            {
                return(rc.MeasureText(text, fontFamily, fontSize, fontWeight));
            }

            return(OxySize.Empty);
        }
Esempio n. 33
0
        private void RenderVerticalAxis(Axis axis, Axis perpendicularAxis)
        {
            double x = Plot.Bounds.Left;

            switch (axis.Position)
            {
            case AxisPosition.Left:
                x = Plot.Bounds.Left;
                break;

            case AxisPosition.Right:
                x = Plot.Bounds.Right;
                break;
            }
            if (axis.PositionAtZeroCrossing)
            {
                x = perpendicularAxis.TransformX(0);
            }

            double x0, x1;

            if (axis.ShowMinorTicks)
            {
                GetHorizontalTickPositions(axis, axis.TickStyle, axis.MinorTickSize, out x0, out x1);
                foreach (double yValue in minorTickValues)
                {
                    if (yValue < axis.ActualMinimum || yValue > axis.ActualMaximum)
                    {
                        continue;
                    }

                    if (majorTickValues.Contains(yValue))
                    {
                        continue;
                    }
                    double y = axis.TransformX(yValue);

                    if (minorPen != null)
                    {
                        RenderLine(Plot.Bounds.Left, y, Plot.Bounds.Right, y, minorPen);
                    }

                    RenderLine(x + x0, y, x + x1, y, minorTickPen);
                }
            }

            GetHorizontalTickPositions(axis, axis.TickStyle, axis.MajorTickSize, out x0, out x1);
            double maxw = 0;

            bool isleft = axis.Position == AxisPosition.Left;

            foreach (double yValue in majorTickValues)
            {
                if (yValue < axis.ActualMinimum || yValue > axis.ActualMaximum)
                {
                    continue;
                }

                double y = axis.TransformX(yValue);

                if (majorPen != null)
                {
                    RenderLine(Plot.Bounds.Left, y, Plot.Bounds.Right, y, majorPen);
                }

                RenderLine(x + x0, y, x + x1, y, majorTickPen);

                if (yValue == 0 && axis.PositionAtZeroCrossing)
                {
                    continue;
                }

                var    pt   = new ScreenPoint(isleft ? x + x1 - TICK_DIST : x + x1 + TICK_DIST, y);
                string text = axis.FormatValue(yValue);
                double w    = rc.MeasureText(text, axis.FontFamily, axis.FontSize, axis.FontWeight).Height;
                rc.DrawText(pt, text, Plot.TextColor,
                            axis.FontFamily, axis.FontSize, axis.FontWeight,
                            axis.Angle,
                            isleft ? HorizontalTextAlign.Right : HorizontalTextAlign.Left, VerticalTextAlign.Middle);
                maxw = Math.Max(maxw, w);
            }

            if (axis.PositionAtZeroCrossing)
            {
                double y = axis.TransformX(0);
                RenderLine(Plot.Bounds.Left, y, Plot.Bounds.Right, y, zeroPen);
            }

            if (axis.ExtraGridlines != null)
            {
                foreach (double y in axis.ExtraGridlines)
                {
                    if (!IsWithin(y, axis.ActualMinimum, axis.ActualMaximum))
                    {
                        continue;
                    }
                    double sy = axis.TransformX(y);
                    RenderLine(Plot.Bounds.Left, sy, Plot.Bounds.Right, sy, extraPen);
                }
            }

            RenderLine(x, Plot.Bounds.Top, x, Plot.Bounds.Bottom, majorPen);

            double ymid = axis.TransformX((axis.ActualMinimum + axis.ActualMaximum) / 2);

            HorizontalTextAlign halign = HorizontalTextAlign.Center;
            VerticalTextAlign   valign = VerticalTextAlign.Top;

            if (axis.PositionAtZeroCrossing)
            {
                ymid = perpendicularAxis.TransformX(perpendicularAxis.ActualMaximum);
                // valign = axis.IsReversed ? VerticalTextAlign.Top : VerticalTextAlign.Bottom;
            }

            if (isleft)
            {
                x = AXIS_LEGEND_DIST;
            }
            else
            {
                x      = rc.Width - AXIS_LEGEND_DIST;
                valign = VerticalTextAlign.Bottom;
            }

            rc.DrawText(new ScreenPoint(x, ymid), axis.Title, Plot.TextColor,
                        axis.FontFamily, axis.FontSize, axis.FontWeight,
                        -90, halign, valign);
        }
Esempio n. 34
0
 public GuiButton(Point position, Point size, string text, VerticalTextAlign verticalAlign = VerticalTextAlign.Center, HorizontalTextAlign horizontalAlign = HorizontalTextAlign.Center)
     : this(new Rectangle(position, new Point(size.X, size.Y)), text, verticalAlign, horizontalAlign)
 {
 }
Esempio n. 35
0
        public override void RenderText(Font font, string text, int x, int y, Color color, Color bg, HorizontalTextAlign hAlign = HorizontalTextAlign.Center, VerticalTextAlign vAlign = VerticalTextAlign.Center)
        {
            if (font == null)
            {
                return;
            }
            x += transX;
            y += transY;
            var   fnt  = font.GetFont();
            float fx   = x;
            float fy   = y;
            var   size = gfx.MeasureString(text, fnt);

            if (hAlign == HorizontalTextAlign.Center)
            {
                fx -= size.Width / 2;
            }
            else if (hAlign == HorizontalTextAlign.Left)
            {
            }
            else if (hAlign == HorizontalTextAlign.Right)
            {
                fx -= size.Width;
            }
            if (vAlign == VerticalTextAlign.Center)
            {
                fy -= size.Height / 2;
            }
            else if (vAlign == VerticalTextAlign.Top)
            {
            }
            else if (vAlign == VerticalTextAlign.Bottom)
            {
                fy -= size.Height;
            }
            using (var brush = new System.Drawing.SolidBrush(color.GetColor()))
            {
                gfx.DrawString(text, fnt, brush, fx, fy);
            }
        }
        /// <summary>
        /// The draw text.
        /// </summary>
        /// <param name="p">
        /// The p.
        /// </param>
        /// <param name="text">
        /// The text.
        /// </param>
        /// <param name="fill">
        /// The fill.
        /// </param>
        /// <param name="fontFamily">
        /// The font family.
        /// </param>
        /// <param name="fontSize">
        /// The font size.
        /// </param>
        /// <param name="fontWeight">
        /// The font weight.
        /// </param>
        /// <param name="rotate">
        /// The rotate.
        /// </param>
        /// <param name="halign">
        /// The halign.
        /// </param>
        /// <param name="valign">
        /// The valign.
        /// </param>
        /// <param name="maxSize">
        /// The maximum size of the text.
        /// </param>
        public override void DrawText(
            ScreenPoint p,
            string text,
            OxyColor fill,
            string fontFamily,
            double fontSize,
            double fontWeight,
            double rotate,
            HorizontalTextAlign halign,
            VerticalTextAlign valign,
            OxySize? maxSize)
        {
            FontStyle fs = FontStyle.Regular;
            if (fontWeight >= 700)
            {
                fs = FontStyle.Bold;
            }

            var font = new Font(fontFamily, (float)fontSize * FontsizeFactor, fs);

            var sf = new StringFormat { Alignment = StringAlignment.Near };

            SizeF size = this.g.MeasureString(text, font);
            if (maxSize != null)
            {
                if (size.Width > maxSize.Value.Width)
                {
                    size.Width = (float)maxSize.Value.Width;
                }

                if (size.Height > maxSize.Value.Height)
                {
                    size.Height = (float)maxSize.Value.Height;
                }
            }

            float dx = 0;
            if (halign == HorizontalTextAlign.Center)
            {
                dx = -size.Width / 2;

                // sf.Alignment = StringAlignment.Center;
            }

            if (halign == HorizontalTextAlign.Right)
            {
                dx = -size.Width;

                // sf.Alignment = StringAlignment.Far;
            }

            float dy = 0;
            sf.LineAlignment = StringAlignment.Near;
            if (valign == VerticalTextAlign.Middle)
            {
                // sf.LineAlignment = StringAlignment.Center;
                dy = -size.Height / 2;
            }

            if (valign == VerticalTextAlign.Bottom)
            {
                // sf.LineAlignment = StringAlignment.Far;
                dy = -size.Height;
            }

            this.g.TranslateTransform((float)p.X, (float)p.Y);
            if (Math.Abs(rotate) > double.Epsilon)
            {
                this.g.RotateTransform((float)rotate);
            }

            this.g.TranslateTransform(dx, dy);

            var layoutRectangle = new RectangleF(0, 0, size.Width, size.Height);
            this.g.DrawString(text, font, this.ToBrush(fill), layoutRectangle, sf);

            this.g.ResetTransform();
        }
Esempio n. 37
0
        /// <summary>
        /// The draw text.
        /// </summary>
        /// <param name="p">
        /// The p.
        /// </param>
        /// <param name="text">
        /// The text.
        /// </param>
        /// <param name="fill">
        /// The fill.
        /// </param>
        /// <param name="fontFamily">
        /// The font family.
        /// </param>
        /// <param name="fontSize">
        /// The font size.
        /// </param>
        /// <param name="fontWeight">
        /// The font weight.
        /// </param>
        /// <param name="rotate">
        /// The rotate.
        /// </param>
        /// <param name="halign">
        /// The halign.
        /// </param>
        /// <param name="valign">
        /// The valign.
        /// </param>
        /// <param name="maxSize">
        /// The maximum size of the text.
        /// </param>
        public override void DrawText(
            ScreenPoint p,
            string text,
            OxyColor fill,
            string fontFamily,
            double fontSize,
            double fontWeight,
            double rotate,
            HorizontalTextAlign halign,
            VerticalTextAlign valign,
            OxySize?maxSize)
        {
            FontStyle fs = FontStyle.Regular;

            if (fontWeight >= 700)
            {
                fs = FontStyle.Bold;
            }

            var font = new Font(fontFamily, (float)fontSize * FontsizeFactor, fs);

            var sf = new StringFormat {
                Alignment = StringAlignment.Near
            };

            SizeF size = this.g.MeasureString(text, font);

            if (maxSize != null)
            {
                if (size.Width > maxSize.Value.Width)
                {
                    size.Width = (float)maxSize.Value.Width;
                }

                if (size.Height > maxSize.Value.Height)
                {
                    size.Height = (float)maxSize.Value.Height;
                }
            }

            float dx = 0;

            if (halign == HorizontalTextAlign.Center)
            {
                dx = -size.Width / 2;

                // sf.Alignment = StringAlignment.Center;
            }

            if (halign == HorizontalTextAlign.Right)
            {
                dx = -size.Width;

                // sf.Alignment = StringAlignment.Far;
            }

            float dy = 0;

            sf.LineAlignment = StringAlignment.Near;
            if (valign == VerticalTextAlign.Middle)
            {
                // sf.LineAlignment = StringAlignment.Center;
                dy = -size.Height / 2;
            }

            if (valign == VerticalTextAlign.Bottom)
            {
                // sf.LineAlignment = StringAlignment.Far;
                dy = -size.Height;
            }

            this.g.TranslateTransform((float)p.X, (float)p.Y);
            if (Math.Abs(rotate) > double.Epsilon)
            {
                this.g.RotateTransform((float)rotate);
            }

            this.g.TranslateTransform(dx, dy);

            var layoutRectangle = new RectangleF(0, 0, size.Width, size.Height);

            this.g.DrawString(text, font, this.ToBrush(fill), layoutRectangle, sf);

            this.g.ResetTransform();
        }
		public override void DrawText (ScreenPoint p, string text, OxyColor fill, string fontFamily, double fontSize, double fontWeight, double rotate, HorizontalTextAlign halign, VerticalTextAlign valign, OxySize? maxSize)
		{


			//This method needs work not 100% around vertical alignment.
			if(string.IsNullOrEmpty(text))
			{
				return;
			}

			fontFamily = GetDefaultFont(fontFamily);

			if (fontWeight >= 700)
            {
                //fs = FontStyle.Bold;
            }

			//var textSize = MeasureText(text, fontFamily, fontSize, fontWeight);

			if (maxSize != null)
            {
//                if (size.Width > maxSize.Value.Width)
//                {
//                    size.Width = (float)maxSize.Value.Width;
//                }
//
//                if (size.Height > maxSize.Value.Height)
//                {
//                    size.Height = (float)maxSize.Value.Height;
//                }
            }

			gctx.SaveState();

			gctx.SelectFont(fontFamily, (float)fontSize, CGTextEncoding.MacRoman);
			ToColor(fill).SetFill();

			gctx.SetTextDrawingMode(CGTextDrawingMode.Fill);

			var tfont = UIFont.FromName (fontFamily,(float)fontSize);
			NSString nsstr = new NSString(text);
			SizeF sz = nsstr.StringSize(tfont);

			float y = (float)(p.Y);
			float x = (float)(p.X);

			switch(halign)
			{
			case HorizontalTextAlign.Left:
				x = (float)(p.X);
				break;
			case HorizontalTextAlign.Right:
				x = (float)(p.X - sz.Width);
				break;
			case HorizontalTextAlign.Center:
				x = (float)(p.X - (sz.Width / 2));
				break;
			}

			switch(valign)
			{
			case VerticalTextAlign.Bottom:
				y -= (float)fontSize;
				break;
			case VerticalTextAlign.Top:
//				y += (float)fontSize;
				break;
			case VerticalTextAlign.Middle:
				y -= (float)(fontSize / 2);
				break;
			}


			RectangleF rect = new RectangleF(x,y,sz.Width,sz.Height);
			nsstr.DrawString( rect, tfont);

			gctx.RestoreState();
			//Console.WriteLine("X:{0:###} Y:{1:###} HA:{2}:{3:###} VA:{4}:{5:###} TW:{6:###} - {7}", p.X, p.Y, halign, x, valign, y, textSize.Width, text);
		}
        public void DrawText(Point p, string text, Color fill, string fontFamily, double fontSize, double fontWeight, double rotate, HorizontalTextAlign halign, VerticalTextAlign valign)
        {
            var tb = new TextBlock
                         {
                             Text = text,
                             Foreground = new SolidColorBrush(fill.ToColor())
                         };
            if (fontFamily != null)
                tb.FontFamily = new FontFamily(fontFamily);
            if (fontSize > 0)
                tb.FontSize = fontSize;
            if (fontWeight > 0)
                tb.FontWeight = System.Windows.FontWeight.FromOpenTypeWeight((int)fontWeight);

            tb.Measure(new System.Windows.Size(1000, 1000));

            double dx = 0;
            if (halign == HorizontalTextAlign.Center)
                dx = -tb.DesiredSize.Width / 2;
            if (halign == HorizontalTextAlign.Right)
                dx = -tb.DesiredSize.Width;

            double dy = 0;
            if (valign == VerticalTextAlign.Middle)
                dy = -tb.DesiredSize.Height / 2;
            if (valign == VerticalTextAlign.Bottom)
                dy = -tb.DesiredSize.Height;

            var transform = new TransformGroup();
            transform.Children.Add(new TranslateTransform(dx, dy));
            if (rotate != 0)
                transform.Children.Add(new RotateTransform(rotate));
            transform.Children.Add(new TranslateTransform(p.X, p.Y));
            tb.RenderTransform = transform;

            tb.SetValue(RenderOptions.ClearTypeHintProperty, ClearTypeHint.Enabled);

            canvas.Children.Add(tb);
        }