private void UpdateMaxItemWidth(Graphics gfx, int index) {
			Size txtSize = Size.Empty;
			if (gfx == null) txtSize = TextMeasurer.MeasureText(Items[index].ToString(), Font, Size.Empty, StringFormat.GenericDefault);
			else txtSize = TextMeasurer.MeasureText(gfx, Items[index].ToString(), Font, Size.Empty, StringFormat.GenericDefault);
			//Size txtSize = TextRenderer.MeasureText(Items[index].ToString(), Font);
			if (txtSize.Width > maxItemTextWidth) maxItemTextWidth = txtSize.Width;
		}
Exemple #2
0
        /// <summary>
        /// Gets the preferred size of a MenuItem.
        /// </summary>
        public virtual Size GetPreferredItemSize(Menu control, MenuItem item, Size proposedSize)
        {
            if (item is MenuSeparatorItem msi)
            {
                return(GetPreferredSeparatorItemSize(control, msi, proposedSize));
            }

            var padding   = control.LogicalToDeviceUnits(item.Padding.Horizontal);
            var font_size = control.LogicalToDeviceUnits(Theme.FontSize);
            var text_size = (int)Math.Round(TextMeasurer.MeasureText(item.Text, Theme.UIFont, font_size).Width);

            return(new Size(text_size + padding, item.Bounds.Height));
        }
Exemple #3
0
        /// <summary>
        /// Renders the specified text to an image
        /// </summary>
        /// <param name="text">Text to render</param>
        /// <returns>PNG image</returns>
        private ActionResult RenderText(string text)
        {
            var family   = FontCollection.SystemFonts.Find("Segoe UI");
            var font     = new Font(family, 13);
            var measurer = new TextMeasurer();
            var size     = measurer.MeasureText(text, font, 72);

            using (var image = new Image((int)Math.Ceiling(size.Width) + PADDING, (int)Math.Ceiling(size.Height) + PADDING))
            {
                image.DrawText(text, font, Rgba32.Black, new Vector2(0, 0));
                return(image.ToActionResult());
            }
        }
        /// <summary>
        /// Gets the preferred size of a MenuItem.
        /// </summary>
        public virtual Size GetPreferredItemSize(MenuDropDown control, MenuItem item, Size proposedSize)
        {
            if (item is MenuSeparatorItem msi)
            {
                return(GetPreferredSeparatorItemSize(control, msi, proposedSize));
            }

            var padding   = control.LogicalToDeviceUnits(item.Padding);
            var font_size = control.LogicalToDeviceUnits(Theme.FontSize);
            var text_size = TextMeasurer.MeasureText(item.Text, Theme.UIFont, font_size);

            return(new Size((int)Math.Round(text_size.Width, 0, MidpointRounding.AwayFromZero) + padding.Horizontal + control.LogicalToDeviceUnits(70), (int)Math.Round(text_size.Height, 0, MidpointRounding.AwayFromZero) + control.LogicalToDeviceUnits(8)));
        }
        /// <summary>
        /// Gets the preferred size of a MenuItem.
        /// </summary>
        public virtual Size GetPreferredItemSize(Ribbon control, MenuItem item, Size proposedSize)
        {
            if (item is MenuSeparatorItem msi)
            {
                return(GetPreferredSeparatorItemSize(control, msi, proposedSize));
            }

            var padding       = control.LogicalToDeviceUnits(6);
            var font_size     = control.LogicalToDeviceUnits(Theme.ItemFontSize);
            var proposed_size = control.LogicalToDeviceUnits(new Size(40, 30));
            var text_size     = (int)Math.Round(TextMeasurer.MeasureText(item.Text ?? string.Empty, Theme.UIFont, font_size, proposed_size).Width);

            return(new Size(Math.Max(text_size + padding, control.LogicalToDeviceUnits(MINIMUM_ITEM_SIZE)), 0));
        }
Exemple #6
0
        private Size MeasureText(TextRun text)
        {
            if (string.IsNullOrWhiteSpace(text.Text))
            {
                return(new Size(0, 0));
            }

            // TODO: Support text.Formatting.FormattingType
            var family = FontCollection.SystemFonts.Find("Arial");
            var font   = new Font(family, (float)text.Formatting.Size, FontStyle.Regular);
            var size   = textMeasurer.MeasureText(text.Text, font, 72);

            return(new Size(size.Width, size.Height));
        }
Exemple #7
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);
            }
        }
        /// <summary>
        /// Gets the preferred size of a MenuItem.
        /// </summary>
        public virtual Size GetPreferredItemSize(ToolBar control, MenuItem item, Size proposedSize)
        {
            if (item is MenuSeparatorItem msi)
            {
                return(GetPreferredSeparatorItemSize(control, msi, proposedSize));
            }

            var width     = control.LogicalToDeviceUnits(item.Padding.Horizontal);
            var font_size = control.LogicalToDeviceUnits(Theme.FontSize);

            width += (int)Math.Round(TextMeasurer.MeasureText(item.Text, Theme.UIFont, font_size).Width);

            if (!(item.Image is null))
            {
                width += control.LogicalToDeviceUnits(20);
            }

            if (item.HasItems)
            {
                width += control.LogicalToDeviceUnits(14);
            }

            return(new Size(width, item.Bounds.Height));
        }
Exemple #9
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();
            });
        }
Exemple #10
0
        private void DoUpdateBounds()
        {
            Debug.Assert(shape != null);
            Debug.Assert(paragraphStyle != null);
            Debug.Assert(captionIndex >= 0);

            // Get rotated caption bounds from the shape
            Point tl = Point.Empty, tr = Point.Empty, bl = Point.Empty, br = Point.Empty;

            shape.GetCaptionBounds(captionIndex, out tl, out tr, out br, out bl);
            // Calculate unrotated caption bounds
            float angle;
            Point center = Geometry.VectorLinearInterpolation(tl, br, 0.5f);

            angle = Geometry.RadiansToDegrees(Geometry.Angle(tl.X, tl.Y, tr.X, tr.Y));
            tl    = Geometry.RotatePoint(center, -angle, tl);
            br    = Geometry.RotatePoint(center, -angle, br);

            // Calculate unrotated layout area for the text
            Rectangle layoutArea = Rectangle.Empty;

            layoutArea.Location = tl;
            layoutArea.Width    = br.X - tl.X;
            layoutArea.Height   = br.Y - tl.Y;
            // Measure the current size of the text
            Size textSize = TextMeasurer.MeasureText(Text, Font, layoutArea.Size, paragraphStyle);

            // Transform layout area and text size to control coordinates
            owner.DiagramToControl(layoutArea, out layoutArea);
            owner.DiagramToControl(textSize, out textSize);

            // Calculate new bounds for the text editor based on the shape's caption layout rectangle
            Rectangle newBounds = Rectangle.Empty;

            newBounds.X      = layoutArea.X;
            newBounds.Width  = layoutArea.Width;
            newBounds.Height = Math.Max(textSize.Height, lineHeight);
            // Move text editor depending on the vertical text alignment
            switch (paragraphStyle.Alignment)
            {
            case ContentAlignment.BottomCenter:
            case ContentAlignment.BottomLeft:
            case ContentAlignment.BottomRight:
                newBounds.Y = layoutArea.Bottom - newBounds.Height;
                break;

            case ContentAlignment.MiddleCenter:
            case ContentAlignment.MiddleLeft:
            case ContentAlignment.MiddleRight:
                newBounds.Y = layoutArea.Y + (int)Math.Round((layoutArea.Height - newBounds.Height) / 2f);
                break;

            case ContentAlignment.TopCenter:
            case ContentAlignment.TopLeft:
            case ContentAlignment.TopRight:
                newBounds.Y = layoutArea.Y;
                break;

            default:
                newBounds.Y = layoutArea.Y;
                Debug.Fail(string.Format("Unhandled {0} '{1}'.", paragraphStyle.Alignment.GetType().Name, paragraphStyle.Alignment));
                break;
            }
            // Correct textbox size by padding
            newBounds.X      += paragraphStyle.Padding.Left;
            newBounds.Y      += paragraphStyle.Padding.Top;
            newBounds.Width  -= paragraphStyle.Padding.Horizontal;
            newBounds.Height -= paragraphStyle.Padding.Vertical;
            newBounds.Inflate(1, 1);

            // Update control bounds
            // (No need to Invalidate here, this is done by OnResize())
            SuspendLayout();
            Bounds         = newBounds;
            defaultPadding = new Padding(paragraphStyle.Padding.Left, paragraphStyle.Padding.Top, paragraphStyle.Padding.Right,
                                         paragraphStyle.Padding.Bottom);
            ResumeLayout();
        }
Exemple #11
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);
            }
        }