Пример #1
0
        public static void DrawTextBoxes <TPixel>(Image <TPixel> image, Font font, IEnumerable <TextBox> boxes) where TPixel : struct, IPixel <TPixel>
        {
            image.Mutate(context =>
            {
                foreach (var box in boxes)
                {
                    var boxBounds = new RectangleF(
                        x: (float)(image.Width * box.X / 100),
                        y: (float)(image.Height * box.Y / 100),
                        width: (float)(image.Width * box.Width / 100),
                        height: (float)(image.Height * box.Height / 100)
                        );
                    var scaledFont = ScaleFont(new Font(font, image.Height / 8), box.Text, boxBounds.Width - 2 * outlineSize, boxBounds.Height - 2 * outlineSize);
                    var wrapWidth  = ScaleWidth(scaledFont, box.Text, boxBounds.Width - 2 * outlineSize);
                    var lineColor  = ColorBuilder <TPixel> .FromRGBA(box.LineColor.R, box.LineColor.G, box.LineColor.B, box.LineColor.A);
                    var fillColor  = ColorBuilder <TPixel> .FromRGBA(box.FillColor.R, box.FillColor.G, box.FillColor.B, box.FillColor.A);
                    var pen        = Pens.Solid(lineColor, 3f);
                    var brush      = Brushes.Solid(fillColor);

                    var textBounds = TextMeasurer.MeasureBounds(box.Text, new RendererOptions(scaledFont)
                    {
                        WrappingWidth       = wrapWidth,
                        HorizontalAlignment = ConvertHorizontalAlignment(box.Horizontal),
                    });

                    var drawLocation = GetLocation(box, boxBounds, textBounds);

                    // draw outline
                    context.DrawText(
                        new TextGraphicsOptions(true)
                    {
                        WrapTextWidth       = wrapWidth,
                        HorizontalAlignment = ConvertHorizontalAlignment(box.Horizontal),
                    },
                        box.Text,
                        scaledFont,
                        pen,
                        drawLocation);

                    // draw fill
                    context.DrawText(new TextGraphicsOptions(true)
                    {
                        WrapTextWidth       = wrapWidth,
                        HorizontalAlignment = ConvertHorizontalAlignment(box.Horizontal),
                    },
                                     box.Text,
                                     scaledFont,
                                     brush,
                                     drawLocation);
                }
            });
        }
Пример #2
0
        /// <inheritdoc/>
        public override object ConvertFrom(CultureInfo culture, string value, Type propertyType)
        {
            if (string.IsNullOrWhiteSpace(value))
            {
                return(base.ConvertFrom(culture, value, propertyType));
            }

            // Special case. HTML requires LightGrey, but NamedColors has LightGray to conform with System.Drawing
            // Check early on.
            if (value.Equals("LightGrey", StringComparison.OrdinalIgnoreCase))
            {
                return(Rgba32.LightGray);
            }

            // Numeric r,g,b - r,g,b,a
            char separator = culture.TextInfo.ListSeparator[0];

            if (value.Contains(separator.ToString()))
            {
                string[] components = value.Split(separator);

                bool convert = true;
                foreach (string component in components)
                {
                    if (!NumberRegex.IsMatch(component))
                    {
                        convert = false;
                    }
                }

                if (convert)
                {
                    List <byte> rgba = CommandParser.Instance.ParseValue <List <byte> >(value);

                    return(rgba.Count == 4
                        ? ColorBuilder <Rgba32> .FromRGBA(rgba[0], rgba[1], rgba[2], rgba[3])
                        : ColorBuilder <Rgba32> .FromRGB(rgba[0], rgba[1], rgba[2]));
                }
            }

            // Hex colors rgb, rrggbb, rrggbbaa
            if (HexColorRegex.IsMatch(value))
            {
                return(Rgba32.FromHex(value));
            }

            // Named colors
            IDictionary <string, Rgba32> table = ColorConstantsTable.Value;

            return(table.ContainsKey(value) ? table[value] : base.ConvertFrom(culture, value, propertyType));
        }