public WordArtGlyphBuilder(IPath path, SixLabors.Fonts.Size size)
        {
            this.yOffset = size.Height;

            this.builder = new PathBuilder();
            this.path    = path;
            this.builder.SetTransform(Matrix3x2.Identity);
        }
Beispiel #2
0
        static void ApplyWaterMarkScaled(Font font, string text, string inputPath, string outputPath)
        {
            using (Image img = Image.Load(inputPath))
            {
                TextMeasurer measurer = new TextMeasurer();

                // we can now get the dimensions of the bounding box of a piece of text
                SixLabors.Fonts.Size size = measurer.MeasureText(text, font, 72);

                //  calculate the scaling factor we need to change the fontsize by to fit the image
                float scalingFactor = Math.Min(img.Width / size.Width, img.Height / size.Height);

                Font  scaledFont = new Font(font, scalingFactor * font.Size);
                Color fill       = new Color(128, 128, 128, 200);

                img.DrawText(text, scaledFont
                             , fill, new Vector2(0, 0));

                img.Save(outputPath);
            }
        }
Beispiel #3
0
        public async Task MemeAsync(int memeNumber, [Remainder] string text)
        {
            var task = Task.Run(async() =>
            {
                if (memeNumber > memeImage.Length)
                {
                    await ReplyAsync($"`ERROR: INCORRECT INTERGER VALUE DETECTED, THE NUMBER MUST BE {memeImage.Length} OR LESS`");
                    return;
                }
                await ReplyAsync("");
                string top    = null;
                string bottom = null;
                int charsIn   = 0;
                var botStart  = await ReplyAsync($"*BLEEP BLOOP*\n`GENERATING DANK MEME FOR USER: {Context.User.Username}`");
                foreach (char x in text)
                {
                    if (x != '|')
                    {
                        top     += x;
                        charsIn += 1;
                    }
                    else
                    {
                        for (int i = charsIn + 1; i < text.Length; i++)
                        {
                            bottom += text.ElementAt(i);
                        }
                        break;
                    }
                }
                top    = top.Trim();
                bottom = bottom.Trim();
                ImageSharp.Image image       = null;
                HttpClient httpClient        = new HttpClient();
                HttpResponseMessage response = await httpClient.GetAsync(memeImage[memeNumber - 1]);
                Stream inputStream           = await response.Content.ReadAsStreamAsync();
                image                     = ImageSharp.Image.Load(inputStream);
                Rgba32 white              = new Rgba32(255, 255, 255, 255);
                Rgba32 black              = new Rgba32(0, 0, 0, 255);
                FontCollection fonts      = new FontCollection();
                Font font1                = fonts.Install("Images/impact.ttf");
                Font font2                = new Font(font1, 50f, FontStyle.Regular);
                TextMeasurer measurer     = new TextMeasurer();
                SixLabors.Fonts.Size size = measurer.MeasureText(top, font2, 72);
                float scalingFactor       = Math.Min((image.Width - 10) / size.Width, ((image.Height - 10) / 4) / (size.Height));
                Font scaledFont           = new Font(font2, scalingFactor * font2.Size);

                SixLabors.Fonts.Size size2 = measurer.MeasureText(bottom, font2, 72);
                float scalingFactor2       = Math.Min((image.Width - 10) / size2.Width, ((image.Height - 10) / 4) / (size2.Height));
                Font scaledFont2           = new Font(font2, scalingFactor2 * font2.Size);

                Vector2 posTop    = new Vector2(5, 5);
                Vector2 posBottom = new Vector2(5, (image.Height * (float)(.65)));

                var pen      = new Pen(black, scalingFactor);
                var pen2     = new Pen(black, scalingFactor2);
                var brush    = new SolidBrush(white);
                var topWidth = measurer.MeasureText(top, scaledFont, 72).Width;
                var botWidth = measurer.MeasureText(bottom, scaledFont2, 72).Width;
                if (topWidth < image.Width - 15)
                {
                    posTop.X += ((image.Width - topWidth) / 2);
                }
                if (botWidth < image.Width - 15)
                {
                    posBottom.X += ((image.Width - botWidth) / 2);
                }
                image.DrawText(top, scaledFont, brush, pen, posTop);
                image.DrawText(bottom, scaledFont2, brush, pen2, posBottom);
                Stream outputStream = new MemoryStream();
                image.SaveAsPng(outputStream);
                outputStream.Position = 0;
                string input          = "abcdefghijklmnopqrstuvwxyz0123456789";
                char ch;
                string randomString = "";
                Random rand         = new Random();
                for (int i = 0; i < 8; i++)
                {
                    ch            = input[rand.Next(0, input.Length)];
                    randomString += ch;
                }
                var file = File.Create($"Images/{randomString}.png");
                await outputStream.CopyToAsync(file);
                file.Dispose();
                await Context.Channel.SendFileAsync(file.Name);
                var botDone = await ReplyAsync($"`IMAGE HES BEEN GENERATED FOR USER: {Context.User.Username}\nENJOY!!`\n*MURP*\n`BOT MESSAGES WILL DELETE IN 10 SECONDS.`");
                File.Delete(file.Name);
                await Task.Delay(10000);
                await botStart.DeleteAsync();
                await botDone.DeleteAsync();
            });
        }
Beispiel #4
0
        static void ApplyWaterMarkScaledWordWrap(Font font, string text, string inputPath, string outputPath)
        {
            using (Image img = Image.Load(inputPath))
            {
                float padding = 20; // margin in 20px

                float targetWidth  = img.Width - (padding * 2);
                float targetHeight = img.Height - (padding * 2);

                float targetMinHeight = img.Height - (padding * 3); // must be with in a margin width of the target height

                TextMeasurer measurer = new TextMeasurer();

                // now we are working i 2 dimensions at once and cant just scale because it will cause the text to
                // reflow we need to just try multiple times

                var f = font;
                SixLabors.Fonts.Size s = new SixLabors.Fonts.Size(float.MaxValue, float.MaxValue);

                float scaleFactor = (f.Size / 2);// everytime we change direction we half this size
                int   trapCount   = (int)f.Size * 2;
                if (trapCount < 10)
                {
                    trapCount = 10;
                }

                bool isTooSmall = false;

                while ((s.Height > targetHeight || s.Height < targetMinHeight) && trapCount > 0)
                {
                    if (s.Height > targetHeight)
                    {
                        if (isTooSmall)
                        {
                            scaleFactor = scaleFactor / 2;
                        }

                        f          = new Font(f, f.Size - scaleFactor);
                        isTooSmall = false;
                    }

                    if (s.Height < targetMinHeight)
                    {
                        if (!isTooSmall)
                        {
                            scaleFactor = scaleFactor / 2;
                        }
                        f          = new Font(f, f.Size + scaleFactor);
                        isTooSmall = true;
                    }
                    trapCount--;

                    var style = new FontSpan(f, 72)
                    {
                        WrappingWidth = targetWidth
                    };

                    s = measurer.MeasureText(text, style);
                }

                Color fill = new Color(128, 128, 128, 200);

                img.DrawText(text, f
                             , fill, new Vector2(padding), new ImageSharp.Drawing.TextGraphicsOptions {
                    WrapTextWidth = targetWidth
                });

                img.Save(outputPath);
            }
        }