예제 #1
0
        /// <inheritdoc cref="ICanvas"/>
        public ICanvas WriteText(string text, float x, float y)
        {
            ValidationUtil.RequireNonNull(text);
            ValidationUtil.RequireBetween(x, 0, 1, $"x({x}) must be in range [0, 1].");
            ValidationUtil.RequireBetween(y, 0, 1, $"y({y}) must be in range [0, 1].");

            var textWidth  = GetTextWidth(text);
            var textHeight = GetTextHeight(text);

            var textRect = new RectangleF(
                (currentDrawingSpace.Width * x) + currentDrawingSpace.X,
                (currentDrawingSpace.Height * y) + currentDrawingSpace.Y,
                textWidth * currentDrawingSpace.Width,
                textHeight * currentDrawingSpace.Height);


            /*
             * var f = GetFont();
             * if (!currentDrawingSpace.Contains(textRect))
             * {
             *  throw new ArgumentException("The text does not fit in the current drawing space.");
             * }
             */

            //currentGfx.DrawString(text, GetFont(), GetBrush(), currentDrawingSpace.Width * x, currentDrawingSpace.Height * y);
            currentGfx.DrawString(text, GetFont(), GetBrush(), new XPoint(textRect.X, textRect.Y));
            return(this);
        }
예제 #2
0
            /// <summary>
            /// Returns a new Builder instance.
            /// </summary>
            /// <param name="lineWidth">the width of the line</param>
            /// <param name="subtitle">the subtitle below</param>
            /// <returns>new Builder instance</returns>
            public static Builder NewInstance(float lineWidth, string subtitle)
            {
                ValidationUtil.RequireBetween(lineWidth, 0, 1, $"lineWidth({lineWidth}) must be in the interval [0, 1]");
                ValidationUtil.RequirePositive(lineWidth, $"lineWidth({lineWidth}) must be greater than 0.");
                ValidationUtil.RequireNonNull(subtitle);

                return(new Builder(lineWidth, subtitle));
            }
예제 #3
0
            /// <summary>
            /// Starts the top left corner of the text at the specified coordinate.
            /// </summary>
            /// <param name="x">the x position</param>
            /// <param name="y">the y position</param>
            /// <returns>this Builder for chaining</returns>
            public Builder SetStartAt(float x, float y)
            {
                ValidationUtil.RequireBetween(x, 0f, 1f, $"x({x}) has to be in the interval [0, 1].");
                ValidationUtil.RequireBetween(y, 0f, 1f, $"y({y}) has to be in the interval [0, 1].");

                StartAt = new Tuple <float, float>(x, y);
                return(this);
            }
예제 #4
0
        public void Test_RequireBetween_Outside()
        {
            double val  = 2;
            double from = -0.01;
            double to   = 1.99;

            ValidationUtil.RequireBetween(val, from, to, "");
        }
예제 #5
0
        public void Test_RequireBetween_Inside()
        {
            double val  = 1;
            double from = -2;
            double to   = 5;

            ValidationUtil.RequireBetween(val, from, to, "");
        }
예제 #6
0
            /// <summary>
            /// Sets the start position of the signature line by the upper left corner.
            /// <para>
            /// Defaults to 0, 0.
            /// </para>
            /// </summary>
            /// <param name="x">the x position of the upper left corner</param>
            /// <param name="y">the y position of the upper left corner</param>
            /// <returns></returns>
            public Builder SetStartAt(float x, float y)
            {
                ValidationUtil.RequireBetween(x, 0, 1, $"x({x}) must be in the interval [0, 1]");
                ValidationUtil.RequireBetween(y, 0, 1, $"y({y}) must be in the interval [0, 1]");

                StartAt = Tuple.Create(x, y);

                return(this);
            }
예제 #7
0
        /// <summary>
        /// Private constructor.
        /// </summary>
        /// <param name="builder">the Builder instance</param>
        private SignaturePlottable(Builder builder)
        {
            LineWidth = builder.LineWidth;
            Subtitle  = builder.Subtitle;
            LineInfo  = builder.LineInfo;
            TextInfo  = builder.TextInfo;
            StartAt   = builder.StartAt;

            ValidationUtil.RequireBetween(LineWidth + StartAt.Item1, 0, 1, $"LineWidth({LineWidth}) + StartAt.Item1({StartAt.Item1}) must be in the interval [0, 1]");
        }
예제 #8
0
            /// <summary>
            /// Returns a new Builder instance with the title to the left of the comment lines. One lineSpacing will be
            /// added under the title before the first line.
            /// </summary>
            /// <param name="lineSpacing">the space between the lines</param>
            /// <param name="title">the title; if empty no title is used</param>
            /// <param name="textInfo">the font information of the title</param>
            /// <returns>new Builder instance</returns>
            public static Builder NewInstance(float lineSpacing, string title, TextInfo textInfo)
            {
                ValidationUtil.RequireBetween(lineSpacing, 0, 1, $"lineSpacing({lineSpacing}) must be in the interval [0, 1]");
                ValidationUtil.RequirePositive(lineSpacing, $"lineSpacing({lineSpacing}) must be greater than 0.");

                ValidationUtil.RequireNonNull(title);
                ValidationUtil.RequireNonNull(textInfo);

                return(new Builder(lineSpacing, title, textInfo));
            }
예제 #9
0
        /// <summary>
        /// Private constructor.
        /// </summary>
        /// <param name="builder">the Builder instance</param>
        private CommentsPlottable(Builder builder)
        {
            LineSpacing = builder.LineSpacing;
            Title       = builder.Title;
            TextInfo    = builder.TextInfo;
            NumLines    = builder.NumLines;
            LineWidth   = builder.LineWidth;
            LineInfo    = builder.LineInfo;
            StartAt     = builder.StartAt;

            ValidationUtil.RequireBetween(LineWidth + StartAt.Item1, 0, 1, $"LineWidth({LineWidth}) + StartAt.Item1({StartAt.Item1}) must be in the interval [0, 1]");
        }
예제 #10
0
            /// <summary>
            /// Adds a legend to the graph.
            /// </summary>
            /// <param name="text">the text</param>
            /// <param name="info">the text display info</param>
            /// <param name="startX">the start x position of the top left corner</param>
            /// <param name="startY">the start y position of the top left corner</param>
            /// <returns>this Builder for chaining</returns>
            /// <exception cref="ArgumentException">if text or info is null or startX or startY not in 0,1 interval</exception>
            public Builder AddLegend(string text, TextInfo info, float startX, float startY)
            {
                ValidationUtil.RequireNonNull(text);
                ValidationUtil.RequireNonNull(info);
                ValidationUtil.RequireBetween(startX, 0f, 1f, $"startX({startX}) has to be in the interval [0, 1].");
                ValidationUtil.RequireBetween(startY, 0f, 1f, $"startY({startY}) has to be in the interval [0, 1].");


                legends.Add(Tuple.Create(text, info, startX, startY));

                return(this);
            }
예제 #11
0
            /// <summary>
            /// Sets the number of lines. This will result in crash if they cannot fit. Give -1 to mean as many as possible.
            /// <para>
            /// Defaults to -1 and a .5 thickness line with RGB(66, 66, 66) with width of 1f.
            /// </para>
            /// </summary>
            /// <param name="numLines">the number of lines</param>
            /// <param name="lineWidth">the width of the lines</param>
            /// <param name="lineInfo">the line info</param>
            /// <returns>this Builder for chaining</returns>
            public Builder SetLines(int numLines, float lineWidth, LineInfo lineInfo)
            {
                if (numLines != -1)
                {
                    ValidationUtil.RequireNonNegative(numLines, $"numLines({numLines}) must be greater or equal to -1");
                }
                ValidationUtil.RequireBetween(lineWidth, 0, 1, $"lineWidth({lineWidth}) must be in the interval [0, 1]");
                ValidationUtil.RequireNonNull(lineInfo);

                NumLines  = numLines;
                LineWidth = lineWidth;
                LineInfo  = lineInfo;

                return(this);
            }
예제 #12
0
        /// <inheritdoc cref="ICanvas"/>
        public ICanvas DrawLine(PointPair line, float thickness, float unitsOn)
        {
            ValidationUtil.RequireNonNull(line);
            ValidationUtil.RequirePositive(thickness, $"thickness({thickness}) must be positive (>0).");
            ValidationUtil.RequireBetween(unitsOn, 0, 1, $"unitsOn({unitsOn}) must be in the range [0, 1].");

            // Draws the line with the pen.
            var pen = GetPen(thickness);

            if (unitsOn.CompareTo(1f) == 0)
            {
                pen.DashStyle = XDashStyle.Solid;
            }
            else
            {
                pen.DashStyle = XDashStyle.Dash;
                var lineLength  = line.ConvertLineLength(currentDrawingSpace);
                var numUnits    = lineLength / thickness;
                var numOnUnits  = unitsOn * numUnits;
                var numOffUnits = numUnits - numOnUnits;
                //pen.DashPattern = new double[] {numOnUnits, numOffUnits};
                pen.DashPattern = new double[] { Math.Max(1, numOnUnits / numOffUnits), Math.Max(1, numOffUnits / numOnUnits) };
            }

            var fromX = line.ConvertFromX(currentDrawingSpace);
            var fromY = line.ConvertFromY(currentDrawingSpace);
            var toX   = line.ConvertToX(currentDrawingSpace);
            var toY   = line.ConvertToY(currentDrawingSpace);

            var from = BindPoint(
                fromX,
                fromY,
                thickness / 2,
                currentDrawingSpace);

            var to = BindPoint(
                toX,
                toY,
                thickness / 2,
                currentDrawingSpace);


            //currentGfx.DrawLine(pen, fromX, fromY, toX, toY);
            currentGfx.DrawLine(pen, from.Item1, from.Item2, to.Item1, to.Item2);

            return(this);
        }
예제 #13
0
        /// <inheritdoc cref="IPlottable"/>
        public void Plot(ICanvas canvas)
        {
            canvas.SetFont(TextInfo.Type, TextInfo.Style, TextInfo.Size);

            var textHeight = canvas.GetTextHeight(Text);
            var textWidth  = canvas.GetTextWidth(Text);

            ValidationUtil.RequireBetween(textWidth, 0f, 1f, $"the text is to wide with a relative width of {textWidth}.");
            ValidationUtil.RequireBetween(textHeight, 0f, 1f, $"the text is to high with a relative height of {textHeight}.");

            var textBox = PointPair.NewInstance(0.5f - textWidth / 2, 0.5f - textHeight / 2, 0.5f + textWidth / 2, 0.5f + textHeight / 2);
            var x       = 0.5f - textWidth / 2;
            var y       = 0.5f - textHeight / 2;

            if (StartAt != null)
            {
                ValidationUtil.RequireBetween(StartAt.Item1 + textWidth, 0f, 1f, $"x({StartAt.Item1}) + textWidth({textWidth}) must be less than or equal to 1.");
                ValidationUtil.RequireBetween(StartAt.Item2 + textHeight, 0f, 1f, $"y({StartAt.Item2}) + textHeight({textHeight}) must be less than or equal to 1.");
                x = StartAt.Item1;
                y = StartAt.Item2;

                textBox = PointPair.NewInstance(x, y, x + textWidth, y + textHeight);

                if (canvas.GetModeParam() == ModeParam.Calibration ||
                    canvas.GetModeParam() == ModeParam.BoxedCalibration)
                {
                    canvas.SetColor(Colors.Red);
                    canvas.DrawRectangle(textBox);
                }
            }
            else if (CenterIn != null)
            {
                ValidationUtil.RequireNonNegative(CenterIn.Width - textWidth, $"CenterIn.Width({CenterIn.Width}) cannot be less than textWidth({textWidth}).");
                ValidationUtil.RequireNonNegative(CenterIn.Height - textHeight, $"CenterIn.Height({CenterIn.Height}) cannot be less than textHeight({textHeight}).");
                x       = CenterIn.FromX + CenterIn.Width / 2 - textWidth / 2;
                y       = CenterIn.FromY + CenterIn.Height / 2 - textHeight / 2;
                textBox = CenterIn;


                if (canvas.GetModeParam() == ModeParam.Calibration ||
                    canvas.GetModeParam() == ModeParam.BoxedCalibration)
                {
                    canvas.SetColor(Colors.Red);
                    canvas.DrawRectangle(CenterIn);
                }
            }

            if (!IsGravity(GravityType.None))
            {
                if (IsGravity(GravityType.Top) && !IsGravity(GravityType.Bottom, GravityType.Top))
                {
                    textBox = PointPair.NewInstance(textBox.FromX, 0, textBox.ToX, textBox.Height);
                    y       = textBox.FromY + textBox.Height / 2 - textHeight / 2;
                }

                if (IsGravity(GravityType.Bottom) && !IsGravity(GravityType.Bottom, GravityType.Top))
                {
                    textBox = PointPair.NewInstance(textBox.FromX, 1 - textBox.Height, textBox.ToX, 1f);
                    y       = textBox.FromY + textBox.Height / 2 - textHeight / 2;
                }

                if (IsGravity(GravityType.Left) && !IsGravity(GravityType.Left, GravityType.Right))
                {
                    textBox = PointPair.NewInstance(0f, textBox.FromY, textBox.Width, textBox.ToY);
                    x       = textBox.FromX + textBox.Width / 2 - textWidth / 2;
                }

                if (IsGravity(GravityType.Right) && !IsGravity(GravityType.Left, GravityType.Right))
                {
                    textBox = PointPair.NewInstance(1 - textBox.Width, textBox.FromY, 1f, textBox.ToY);
                    x       = textBox.FromX + textBox.Width / 2 - textWidth / 2;
                }

                if (canvas.GetModeParam() == ModeParam.Calibration ||
                    canvas.GetModeParam() == ModeParam.BoxedCalibration)
                {
                    canvas.SetColor(Colors.Green);
                    canvas.DrawRectangle(textBox);
                }
            }

            if (x.CompareTo(0f) < 0)
            {
                x = 0;
            }

            if (y.CompareTo(0f) < 0)
            {
                y = 0;
            }

            canvas.SetColor(TextInfo.Color);

            if (canvas.GetModeParam() == ModeParam.Calibration ||
                canvas.GetModeParam() == ModeParam.BoxedCalibration)
            {
                canvas.SetColor(Color.Black);
            }

            canvas.SetFont(TextInfo.Type, TextInfo.Style, TextInfo.Size);
            canvas.WriteText(Text, x, y + textHeight * 0.85f);
        }