public Size Measure(string text, double fontSize, Typeface typeface, double maxWidth)
        {
            if (htmlElement == null)
            {
                htmlElement = Document.CreateElement("div");
                style       = new HtmlStyleDictionary(htmlElement);

                Document.Body.AppendChild(htmlElement);
            }

            style.SetValue("position", "absolute");
            style.SetValue("visibility", "hidden");
            style.SetFontSize(fontSize, converter);
            style.SetFontFamily(typeface.FontFamily, converter);
            style.SetFontStretch(typeface.Stretch, converter);
            style.SetFontStyle(typeface.Style, converter);
            style.SetFontWeight(typeface.Weight, converter);

            if (maxWidth.IsNaN() || !Double.IsFinite(maxWidth))
            {
                style.SetTextWrapping(TextWrapping.NoWrap, converter);
                style.ClearValue("max-width");
            }
            else
            {
                style.SetTextWrapping(TextWrapping.Wrap, converter);
                style.SetValue("max-width", converter.ToPixelString(maxWidth));
            }

            style.Apply();

            htmlElement.InnerHTML = converter.ToHtmlContentString(text.DefaultIfNullOrEmpty("A"));

            return(new Size(text.IsNullOrEmpty() ? 0 : htmlElement.OffsetWidth + 2, htmlElement.OffsetHeight));
        }
 public static void SetFontSize(this HtmlStyleDictionary style, double fontSize, IHtmlValueConverter converter)
 {
     if (fontSize.IsNaN())
     {
         style.ClearValue("font-size");
     }
     else
     {
         style.SetValue("font-size", converter.ToPixelString(fontSize));
     }
 }
        public static void SetSize(this HtmlStyleDictionary style, Size size, IHtmlValueConverter converter)
        {
            if (size.Width.IsNaN())
            {
                style.ClearValue("width");
            }
            else
            {
                style.SetValue("width", converter.ToPixelString(size.Width));
            }

            if (size.Height.IsNaN())
            {
                style.ClearValue("height");
            }
            else
            {
                style.SetValue("height", converter.ToPixelString(size.Height));
            }
        }
 public static void SetBackgroundSize(this HtmlStyleDictionary style, Size size, IHtmlValueConverter converter)
 {
     if (Size.IsNullOrEmpty(size))
     {
         style.ClearValue("background-size");
     }
     else
     {
         style.SetValue("background-size", converter.ToPixelString(size));
     }
 }
 public static void SetBackgroundLocation(this HtmlStyleDictionary style, Point location, IHtmlValueConverter converter)
 {
     if (Point.IsNullOrEmpty(location))
     {
         style.ClearValue("background-position");
     }
     else
     {
         style.SetValue("background-position", converter.ToPixelString(location));
     }
 }
        public void HtmlValueConverterBasicTest()
        {
            IHtmlValueConverter converter = HtmlValueConverter.Default;

            Assert.AreEqual("1.23px", converter.ToPixelString(1.234));
            Assert.AreEqual("123.46%", converter.ToPercentString(1.23456));
            Assert.AreEqual("1.23deg", converter.ToDegreesString(1.234));
            Assert.AreEqual("1.23px 5.68px", converter.ToPixelString(new Point(1.234, 5.678)));
            Assert.AreEqual("123.46% 567.89%", converter.ToPercentString(new Point(1.23456, 5.6789)));
            Assert.AreEqual("rgba(12, 34, 56, 0.25)", converter.ToColorString(Color.FromArgb(64, 12, 34, 56)));
            Assert.AreEqual("#0c2238", converter.ToColorString(Color.FromRgb(12, 34, 56)));
            Assert.AreEqual("1.23px", converter.ToPixelString(new Thickness(1.234)));
            Assert.AreEqual("2.35px 3.46px 4.57px 1.23px", converter.ToPixelString(new Thickness(1.234, 2.345, 3.456, 4.567)));
            Assert.AreEqual("2.35 3.46 4.57 1.23", converter.ToImplicitValueString(new Thickness(1.234, 2.345, 3.456, 4.567)));
            Assert.AreEqual("url(image-source)", converter.ToUrlString("image-source"));
            Assert.AreEqual("linear-gradient(135deg, #ff0000 0%, #0000ff 100%)", converter.ToLinearGradientString(new LinearGradientBrush(45, Colors.Red, Colors.Blue), new Rect(100, 100)));
            Assert.AreEqual("repeating-linear-gradient(135deg, #ff0000 0%, #0000ff 100%)", converter.ToLinearGradientString(new LinearGradientBrush(45, Colors.Red, Colors.Blue)
            {
                SpreadMethod = GradientSpreadMethod.Repeat
            }, new Rect(100, 100)));
            Assert.AreEqual("linear-gradient(135deg, #ff0000 0%, #0000ff 50%, #0000ff 50%, #ff0000 100%)", converter.ToLinearGradientString(new LinearGradientBrush(45, Colors.Red, Colors.Blue)
            {
                SpreadMethod = GradientSpreadMethod.Reflect
            }, new Rect(100, 100)));
            Assert.AreEqual("radial-gradient(ellipse 50% 50% at 50% 50%, #ff0000 0%, #0000ff 100%)", converter.ToRadialGradientString(new RadialGradientBrush(Colors.Red, Colors.Blue)));
            Assert.AreEqual("repeating-radial-gradient(ellipse 50% 50% at 50% 50%, #ff0000 0%, #0000ff 100%)", converter.ToRadialGradientString(new RadialGradientBrush(Colors.Red, Colors.Blue)
            {
                SpreadMethod = GradientSpreadMethod.Repeat
            }));
            Assert.AreEqual("#ff0000 0%, #0000ff 100%", converter.ToColorStopsString(new GradientStop[] { new GradientStop(Colors.Red, 0), new GradientStop(Colors.Blue, 1) }));
            Assert.AreEqual("#ff0000", converter.ToColorString(new SolidColorBrush(Colors.Red)));
            Assert.AreEqual("linear-gradient(135deg, #ff0000 0%, #0000ff 100%)", converter.ToImageString(new LinearGradientBrush(45, Colors.Red, Colors.Blue), new Rect(100, 100)));
            Assert.AreEqual("radial-gradient(ellipse 50% 50% at 50% 50%, #ff0000 0%, #0000ff 100%)", converter.ToImageString(new RadialGradientBrush(Colors.Red, Colors.Blue)));
            Assert.AreEqual("url(image-source)", converter.ToImageString(new ImageBrush()
            {
                ImageSource = "image-source"
            }));
        }
        public static void SetCornerRadius(this HtmlStyleDictionary style, CornerRadius cornerRadius, IHtmlValueConverter converter)
        {
            style.ClearValue("border-radius");
            style.ClearValue("border-top-left-radius");
            style.ClearValue("border-top-right-radius");
            style.ClearValue("border-bottom-left-radius");
            style.ClearValue("border-bottom-right-radius");

            if (cornerRadius != CornerRadius.Zero)
            {
                if (cornerRadius.IsUniform)
                {
                    style.SetValue("border-radius", converter.ToPixelString(cornerRadius.TopLeft));
                }
                else
                {
                    style.SetValue("border-top-left-radius", converter.ToPixelString(cornerRadius.TopLeft));
                    style.SetValue("border-top-right-radius", converter.ToPixelString(cornerRadius.TopRight));
                    style.SetValue("border-bottom-left-radius", converter.ToPixelString(cornerRadius.BottomLeft));
                    style.SetValue("border-bottom-right-radius", converter.ToPixelString(cornerRadius.BottomRight));
                }
            }
        }
 public static void SetBorderThickness(this HtmlStyleDictionary style, Thickness borderThickness, IHtmlValueConverter converter)
 {
     if (borderThickness == Thickness.Zero)
     {
         style.ClearValue("border-style");
         style.ClearValue("border-width");
         style.ClearValue("border-image-slice");
     }
     else
     {
         style.SetValue("border-style", "solid");
         style.SetValue("border-width", converter.ToPixelString(borderThickness));
         style.SetValue("border-image-slice", converter.ToImplicitValueString(borderThickness));
     }
 }
 public static void SetLocation(this HtmlStyleDictionary style, Point location, IHtmlValueConverter converter)
 {
     style.SetValue("position", "absolute");
     style.SetValue("left", converter.ToPixelString(location.X));
     style.SetValue("top", converter.ToPixelString(location.Y));
 }
 public static void SetLocation(this HtmlStyleDictionary style, Point location, IHtmlValueConverter converter)
 {
     style.SetValue("position", "absolute");
     style.SetValue("left", converter.ToPixelString(location.X));
     style.SetValue("top", converter.ToPixelString(location.Y));
 }
        public static void SetSize(this HtmlStyleDictionary style, Size size, IHtmlValueConverter converter)
        {
            if (size.Width.IsNaN())
            {
                style.ClearValue("width");
            }
            else
            {
                style.SetValue("width", converter.ToPixelString(size.Width));
            }

            if (size.Height.IsNaN())
            {
                style.ClearValue("height");
            }
            else
            {
                style.SetValue("height", converter.ToPixelString(size.Height));
            }
        }
 public static void SetFontSize(this HtmlStyleDictionary style, double fontSize, IHtmlValueConverter converter)
 {
     if (fontSize.IsNaN())
     {
         style.ClearValue("font-size");
     }
     else
     {
         style.SetValue("font-size", converter.ToPixelString(fontSize));
     }
 }
        public static void SetCornerRadius(this HtmlStyleDictionary style, CornerRadius cornerRadius, IHtmlValueConverter converter)
        {
            style.ClearValue("border-radius");
            style.ClearValue("border-top-left-radius");
            style.ClearValue("border-top-right-radius");
            style.ClearValue("border-bottom-left-radius");
            style.ClearValue("border-bottom-right-radius");

            if (cornerRadius != CornerRadius.Zero)
            {
                if (cornerRadius.IsUniform)
                {
                    style.SetValue("border-radius", converter.ToPixelString(cornerRadius.TopLeft));
                }
                else
                {
                    style.SetValue("border-top-left-radius", converter.ToPixelString(cornerRadius.TopLeft));
                    style.SetValue("border-top-right-radius", converter.ToPixelString(cornerRadius.TopRight));
                    style.SetValue("border-bottom-left-radius", converter.ToPixelString(cornerRadius.BottomLeft));
                    style.SetValue("border-bottom-right-radius", converter.ToPixelString(cornerRadius.BottomRight));
                }
            }
        }
 public static void SetBorderThickness(this HtmlStyleDictionary style, Thickness borderThickness, IHtmlValueConverter converter)
 {
     if (borderThickness == Thickness.Zero)
     {
         style.ClearValue("border-style");
         style.ClearValue("border-width");
         style.ClearValue("border-image-slice");
     }
     else
     {
         style.SetValue("border-style", "solid");
         style.SetValue("border-width", converter.ToPixelString(borderThickness));
         style.SetValue("border-image-slice", converter.ToImplicitValueString(borderThickness));
     }
 }
 public static void SetBackgroundSize(this HtmlStyleDictionary style, Size size, IHtmlValueConverter converter)
 {
     if (Size.IsNullOrEmpty(size))
     {
         style.ClearValue("background-size");
     }
     else
     {
         style.SetValue("background-size", converter.ToPixelString(size));
     }
 }
 public static void SetBackgroundLocation(this HtmlStyleDictionary style, Point location, IHtmlValueConverter converter)
 {
     if (Point.IsNullOrEmpty(location))
     {
         style.ClearValue("background-position");
     }
     else
     {
         style.SetValue("background-position", converter.ToPixelString(location));
     }
 }