public void CalculateSpacingFor(Size size, string text,
                                        ref DivergenceArgs divArgs, int?left, int?top, int?right, int?bottom,
                                        int?line)
        {
            if (left.HasValue && right.HasValue)
            {
                throw new ArgumentException("Left and Right spacing cannot both be " +
                                            "specified!");
            }
            else if (top.HasValue && bottom.HasValue)
            {
                throw new ArgumentException("Top and Bottom spacing cannot both be " +
                                            "specified!");
            }
            else if (text == null)
            {
                throw new ArgumentNullException(text);
            }
            text = Format(text, divArgs);

            DrawArgs args = new DrawArgs(text, divArgs)
            {
                Spacing = new DivergenceSpacing(
                    left ?? 0, top ?? 0, right ?? 0, bottom ?? 0, line ?? 0),
            };

            Size difference           = size - CalculateSize(args);
            DivergenceSpacing spacing = args.Spacing;

            if (left.HasValue)
            {
                spacing.Right = difference.Width;
            }
            else if (right.HasValue)
            {
                spacing.Left = difference.Width;
            }
            else
            {
                spacing.Left  = difference.Width / 2;
                spacing.Right = difference.Width - spacing.Left;
            }
            if (top.HasValue)
            {
                spacing.Bottom = difference.Height;
            }
            else if (bottom.HasValue)
            {
                spacing.Top = difference.Height;
            }
            else
            {
                spacing.Top    = difference.Height / 2;
                spacing.Bottom = difference.Height - spacing.Top;
            }

            divArgs.Spacing = spacing;
        }
 public DrawArgs(string text, DivergenceArgs args)
 {
     Text       = text;
     Lines      = text.Split('\n');
     Spacing    = args.Spacing;
     Background = args.Background;
     Alignment  = args.Alignment;
     UsePadding = args.UsePadding;
     AlignTubes = args.AlignTubes;
     Authentic  = args.Authenticity != DivergenceAuthenticity.None;
     Bitmap     = null;
 }
        public Size CalculateSize(string text, DivergenceArgs divArgs)
        {
            if (text == null)
            {
                throw new ArgumentNullException(text);
            }
            text = Format(text, divArgs);

            DrawArgs args = new DrawArgs(text, divArgs);

            return(CalculateSize(args));
        }
Exemple #4
0
        static void Main(string[] cmdArgs)
        {
            // Draw Figure A
            var args = new DivergenceArgs {
                Scale      = DivergenceScale.Medium,
                Spacing    = new DivergenceSpacing(8, 8),
                Background = Color.FromArgb(224, 224, 224),
            };
            string text = "Oslo II";

            using (Bitmap bmp = Divergence.Draw(text, args))
                bmp.Save("OsloII.png");

            // Draw Figure B
            args.Scale   = DivergenceScale.Small;
            args.Spacing = new DivergenceSpacing(5, 5);
            DateTime date = DateTime.Now;

            text = $"{date:MM\\/dd\\/yy}\n{date.TimeOfDay:hh\\:mm\\:ss}";
            using (Bitmap bmp = Divergence.Draw(text, args))
                bmp.Save("DateTime.png");

            // Draw Figure C
            args = new DivergenceArgs {
                Scale      = DivergenceScale.Small,
                Background = "EV_Z02A.PNG",                 // The CG background
            };
            text = "1.130426";
            Divergence.CalculateSpacingFor(1920 / 2, 1080 / 2, text, ref args, left: 5, top: 2);
            using (Bitmap bmp = Divergence.Draw(text, args))
                bmp.Save("Original Worldline.png");

            // Draw Figure D
            args = new DivergenceArgs {
                Scale      = DivergenceScale.Small,
                Background = Color.Black,
                Escape     = DivergenceEscape.NewLines,
            };
            text = @"#1\n#2";
            using (Bitmap bmp = Divergence.Draw(text, args))
                bmp.Save("Command Line Example.png");
        }
        public Bitmap Draw(string text, DivergenceArgs divArgs)
        {
            if (text == null)
            {
                throw new ArgumentNullException(text);
            }
            text = Format(text, divArgs);

            if (divArgs.Authenticity == DivergenceAuthenticity.Decide)
            {
                if (IsAuthentic(text))
                {
                    divArgs.Authenticity = DivergenceAuthenticity.Strict;
                }
                else if (IsSemiAuthentic(text))
                {
                    divArgs.Authenticity = DivergenceAuthenticity.Lax;
                }
                else
                {
                    divArgs.Authenticity = DivergenceAuthenticity.None;
                }
            }
            else if (divArgs.Authenticity == DivergenceAuthenticity.Strict &&
                     !IsAuthentic(text))
            {
                throw new NotAuthenticDivergenceException(text);
            }

            DrawArgs args = new DrawArgs(text, divArgs);

            if (args.LineCount > MaxLines && EnableLimits)
            {
                throw new ArgumentException($"{nameof(text)} has {args.LineCount} " +
                                            $"lines, which is greater than {nameof(MaxLines)} ({MaxLines})!");
            }
            for (int i = 0; i < args.LineCount && EnableLimits; i++)
            {
                string line = args.Lines[i];
                if (line.Length > MaxLength)
                {
                    throw new ArgumentException($"Line {i + 1} is " +
                                                $"longer than {nameof(MaxLength)} ({MaxLength})!");
                }
            }

            Size size = CalculateSize(args);

            args.Bitmap = new Bitmap(size.Width, size.Height,
                                     PixelFormat.Format32bppArgb);
            try {
                using (Graphics g = Graphics.FromImage(args.Bitmap))
                    using (Resources res = GetResources(text, args.Authentic)) {
                        g.InterpolationMode = InterpolationMode.NearestNeighbor;
                        DrawBackground(g, args);
                        for (int i = 0; i < args.LineCount; i++)
                        {
                            DrawLine(g, i, res, args);
                        }
                    }
            }
            catch {
                args.Bitmap.Dispose();
                throw;
            }
            return(args.Bitmap);
        }
Exemple #6
0
 static void Save(string text, DivergenceArgs args, string path)
 {
     Divergence.Draw(text, args).SaveAndDispose(path);
 }
Exemple #7
0
 static void Test(string text, DivergenceArgs args)
 {
     Divergence.Draw(text, args).OpenInMSPaint();
 }
        static void Main(string[] args)
        {
            Console.Title           = "Divergence Meter Input";
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("======== Divergence Input ========");
            Console.ResetColor();
            Console.Write("Enter ");
            Console.ForegroundColor = ConsoleColor.Magenta;
            Console.Write("back");
            Console.ResetColor();
            Console.Write(" during any input to go to the previous input. Enter ");
            Console.ForegroundColor = ConsoleColor.Magenta;
            Console.Write("exit");
            Console.ResetColor();
            Console.WriteLine(" to exit. (case-sensitive)");

            string input = null;

            Divergence.EnableLimits = false;
            char[]         invalidNameChars = Path.GetInvalidFileNameChars();
            int            inputIndex       = 0;
            DivergenceArgs dargs            = new DivergenceArgs {
                Alignment    = System.Drawing.StringAlignment.Near,
                Authenticity = DivergenceAuthenticity.Lax,
                Background   = DivergenceBackground.None,
                Escape       = DivergenceEscape.NewLines,
                Scale        = DivergenceScale.Small,
                AlignTubes   = false,
                UsePadding   = false,
            };
            string text = null;
            string file = null;

            WriteHeader();
            while (true)
            {
                try {
                    switch (inputIndex)
                    {
                    case 0:
                        do
                        {
                            input = GetInput("Size (s/m/l)", ConsoleColor.Cyan, "l");
                        } while (input != "s" && input != "m" && input != "l");
                        if (input == "s")
                        {
                            dargs.Scale = DivergenceScale.Small;
                        }
                        if (input == "m")
                        {
                            dargs.Scale = DivergenceScale.Medium;
                        }
                        if (input == "l")
                        {
                            dargs.Scale = DivergenceScale.Large;
                        }
                        break;

                    case 1:
                        do
                        {
                            text = GetInput("  Meter Text", ConsoleColor.Yellow);
                        } while (text.Length == 0);
                        break;

                    case 2:
                        do
                        {
                            file = GetInput("Save to File", ConsoleColor.White, GetNextFile());
                            if (file.Any(c => Array.IndexOf(invalidNameChars, c) != -1))
                            {
                                WriteError("  Invalid path characters!");
                                file = null;
                            }
                        } while (file == null);

                        using (var bitmap = Divergence.Draw(text, dargs))
                            bitmap.Save(file, ImageFormat.Png);
                        Console.ForegroundColor = ConsoleColor.Green;
                        Console.WriteLine($"  Saved text \"{text}\" to file \"{file}\"!");
                        Console.ResetColor();

                        do
                        {
                            input = GetInput(" Open? (y/n)", ConsoleColor.White, "n");
                        } while (input != "y" && input != "yes" && input != "n" && input != "no");
                        if (input == "yes" || input == "y")
                        {
                            ProcessStartInfo startInfo = new ProcessStartInfo {
                                FileName        = file,
                                Verb            = "open",
                                UseShellExecute = true,
                            };
                            Process.Start(startInfo)?.Dispose();
                        }

                        WriteHeader();

                        break;
                    }
                    inputIndex = (inputIndex + 1) % 3;
                } catch (BackException) {
                    inputIndex = Math.Max(0, inputIndex - 1);
                } catch (ExitException) {
                    return;
                } catch (Exception ex) {
                    WriteError("  An error occurred!");
                    WriteError(ex);
                    File.WriteAllText("error.txt", ex.ToString());

                    WriteHeader();
                }
            }
        }