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 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);
        }
Example #3
0
 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));
     }
 }
Example #4
0
 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));
     }
 }
Example #5
0
 public static void SetIsVisible(this HtmlStyleDictionary style, bool isVisible)
 {
     if (isVisible)
     {
         style.ClearValue("display");
     }
     else
     {
         style.SetValue("display", "none");
     }
 }
Example #6
0
 public static void SetBackgroundImage(this HtmlStyleDictionary style, ImageSource imageSource, IHtmlValueConverter converter)
 {
     if (imageSource == null)
     {
         style.ClearValue("background-image");
     }
     else
     {
         style.SetValue("background-image", converter.ToUrlString(((RenderImageSource)imageSource.RenderImageSource).Url));
     }
 }
Example #7
0
 public static void SetOpacity(this HtmlStyleDictionary style, double opacity, IHtmlValueConverter converter)
 {
     if (opacity == 1.0)
     {
         style.ClearValue("opacity");
     }
     else
     {
         style.SetValue("opacity", converter.ToImplicitValueString(opacity));
     }
 }
Example #8
0
 public static void SetTransform(this HtmlStyleDictionary style, Transform transform, IHtmlValueConverter converter)
 {
     if (transform == Transform.Identity)
     {
         style.ClearValue("transform");
     }
     else
     {
         style.SetValue("transform", transform.Value.ToString());
     }
 }
Example #9
0
 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));
     }
 }
Example #10
0
 public static void SetFontStyle(this HtmlStyleDictionary style, FontStyle fontStyle, IHtmlValueConverter converter)
 {
     if (fontStyle == FontStyle.Normal)
     {
         style.ClearValue("font-style");
     }
     else
     {
         style.SetValue("font-style", converter.ToFontStyleString(fontStyle));
     }
 }
Example #11
0
 public static void SetFontWeight(this HtmlStyleDictionary style, FontWeight fontWeight, IHtmlValueConverter converter)
 {
     if (fontWeight == FontWeight.Normal)
     {
         style.ClearValue("font-weight");
     }
     else
     {
         style.SetValue("font-weight", converter.ToFontWeightString(fontWeight));
     }
 }
Example #12
0
 public static void SetFontStretch(this HtmlStyleDictionary style, FontStretch fontStretch, IHtmlValueConverter converter)
 {
     if (fontStretch == FontStretch.Normal)
     {
         style.ClearValue("font-stretch");
     }
     else
     {
         style.SetValue("font-stretch", converter.ToFontStretchString(fontStretch));
     }
 }
Example #13
0
 public static void SetTextTrimming(this HtmlStyleDictionary style, TextTrimming textTrimming)
 {
     if (textTrimming == TextTrimming.None)
     {
         style.ClearValue("text-overflow");
     }
     else
     {
         style.SetValue("text-overflow", "ellipsis");
     }
 }
Example #14
0
 public static void SetFontFamily(this HtmlStyleDictionary style, FontFamily fontFamily, IHtmlValueConverter converter)
 {
     if (!fontFamily.FamilyNames.Any())
     {
         style.ClearValue("font-family");
     }
     else
     {
         style.SetValue("font-family", converter.ToFontFamilyNamesString(fontFamily));
     }
 }
Example #15
0
 public static void SetTransform(this HtmlStyleDictionary style, Matrix transform, IHtmlValueConverter converter)
 {
     if (transform == Matrix.Identity)
     {
         style.ClearValue("transform");
         style.ClearValue("transform-origin");
     }
     else
     {
         style.SetValue("transform", converter.ToTransformString(transform));
         style.SetValue("transform-origin", "0 0");
     }
 }
Example #16
0
        public static void SetBackground(this HtmlStyleDictionary style, Brush background, Rect targetRect, IHtmlValueConverter converter)
        {
            style.ClearValue("background-color");
            style.ClearValue("background-image");

            if (background is SolidColorBrush)
            {
                style.SetValue("background-color", converter.ToColorString((SolidColorBrush)background));
            }
            else if (background != null)
            {
                style.SetValue("background-image", converter.ToImageString(background, targetRect));
            }
        }
Example #17
0
        public static void SetBorderBrush(this HtmlStyleDictionary style, Brush borderBrush, Size targetSize, IHtmlValueConverter converter)
        {
            style.ClearValue("border-color");
            style.ClearValue("border-image-source");

            if (borderBrush is SolidColorBrush)
            {
                style.SetValue("border-color", converter.ToColorString((SolidColorBrush)borderBrush));
            }
            else if (borderBrush != null)
            {
                style.SetValue("border-image-source", converter.ToImageString(borderBrush, new Rect(targetSize)));
            }
        }
Example #18
0
 public static void SetForeground(this HtmlStyleDictionary style, Brush foreground, IHtmlValueConverter converter)
 {
     if (foreground == null)
     {
         style.ClearValue("color");
     }
     else if (foreground is SolidColorBrush)
     {
         style.SetValue("color", converter.ToColorString((SolidColorBrush)foreground));
     }
     else
     {
         throw new Granular.Exception("A \"{0}\" foreground brush is not supported", foreground.GetType());
     }
 }
Example #19
0
 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));
     }
 }
Example #20
0
        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));
            }
        }
Example #21
0
        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));
                }
            }
        }
Example #22
0
 public static void SetBackgroundBounds(this HtmlStyleDictionary style, Rect bounds, IHtmlValueConverter converter)
 {
     style.SetBackgroundLocation(bounds.Location, converter);
     style.SetBackgroundSize(bounds.Size, converter);
 }
Example #23
0
 public static void SetVerticalScrollBarVisibility(this HtmlStyleDictionary style, ScrollBarVisibility scrollBarVisibility, IHtmlValueConverter converter)
 {
     style.SetValue("overflow-y", converter.ToOverflowString(scrollBarVisibility));
 }
Example #24
0
 public static void SetTextWrapping(this HtmlStyleDictionary style, TextWrapping textWrapping, IHtmlValueConverter converter)
 {
     style.SetValue("white-space", converter.ToWhiteSpaceString(textWrapping));
 }
Example #25
0
 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));
 }
Example #26
0
 public static void SetTextAlignment(this HtmlStyleDictionary style, TextAlignment textAlignment, IHtmlValueConverter converter)
 {
     style.SetValue("text-align", converter.ToTextAlignmentString(textAlignment));
 }
Example #27
0
 public static void SetClipToBounds(this HtmlStyleDictionary style, bool clipToBounds)
 {
     style.SetValue("overflow", clipToBounds ? "hidden" : "visible");
 }
Example #28
0
 public static void SetIsHitTestVisible(this HtmlStyleDictionary style, bool isHitTestVisible)
 {
     style.SetValue("pointer-events", isHitTestVisible ? "auto" : "none");
 }
Example #29
0
 public static void SetBounds(this HtmlStyleDictionary style, Rect bounds, IHtmlValueConverter converter)
 {
     SetLocation(style, bounds.Location, converter);
     SetSize(style, bounds.Size, converter);
 }