public static void HtmlNodeToFormatedString(XmlNode node, FormattedString fs, FontAttributes attributes = FontAttributes.None)
 {
     if (node.NodeType == XmlNodeType.Text)
     {
         var resname = "content";
         if (attributes.HasFlag(FontAttributes.Bold))
         {
             resname += "bold";
         }
         if (attributes.HasFlag(FontAttributes.Italic))
         {
             resname += "ital";
         }
         var fd = FontData.FromResource(resname);
         fs.Spans.Add(new Span()
         {
             FontFamily = fd.FontFamily, FontAttributes = attributes | fd.FontAttributes, FontSize = fd.FontSize, ForegroundColor = fd.TextColor, Text = node.InnerText
         });
     }
     else if (node.NodeType == XmlNodeType.Element && node.Name == "br")
     {
         fs.Spans.Add(new Span()
         {
             Text = "\n"
         });
     }
     else if (node.NodeType == XmlNodeType.Element && node.Name == "strong")
     {
         HtmlNodesToFormatedString(node.ChildNodes, fs, attributes | FontAttributes.Bold);
     }
     else if (node.NodeType == XmlNodeType.Element && node.Name == "em")
     {
         HtmlNodesToFormatedString(node.ChildNodes, fs, attributes | FontAttributes.Italic);
     }
     else if (node.NodeType == XmlNodeType.Element && node.Name == "p")
     {
         HtmlNodesToFormatedString(node.ChildNodes, fs, attributes);
         fs.Spans.Add(new Span()
         {
             Text = "\n"
         });
         fs.Spans.Add(new Span()
         {
             Text = "\n"
         });
     }
     else if (node.NodeType == XmlNodeType.Element)
     {
         HtmlNodesToFormatedString(node.ChildNodes, fs, attributes);
     }
     else if (node.NodeType == XmlNodeType.Document)
     {
         HtmlNodesToFormatedString(node.ChildNodes, fs, attributes);
     }
 }
Beispiel #2
0
        private static string GetFontName(string fontFamily, FontAttributes fontAttributes)
        {
            if (fontAttributes.HasFlag(FontAttributes.Bold))
            {
                return(fontAttributes.HasFlag(FontAttributes.Italic)
                    ? $"{fontFamily}-BoldItalic"
                    : $"{fontFamily}-Bold");
            }

            return(fontAttributes.HasFlag(FontAttributes.Italic)
                ? $"{fontFamily}-Italic"
                : $"{fontFamily}");
        }
Beispiel #3
0
        public static Size MeasureSize(this string text, string fontFamily, double fontSize, FontAttributes fontAttrs)
        {
            if (string.IsNullOrEmpty(text))
            {
                return(Size.Zero);
            }

            var fontHeight = fontSize;

            var isBold = fontAttrs.HasFlag(FontAttributes.Bold);

            var props = isBold ? BoldCharacterProportions : CharacterProportions;
            var avgp  = isBold ? BoldAverageCharProportion : AverageCharProportion;

            var pwidth = 1.0e-6; // Tiny little padding to account for sampling errors

            for (var i = 0; i < text.Length; i++)
            {
                var c = (int)text[i];
                if (c < 128)
                {
                    pwidth += props[c];
                }
                else
                {
                    pwidth += avgp;
                }
            }
            var width = fontSize * pwidth;

            return(new Size(width, fontHeight));
        }
        public static void ApplyFontAttributes(this Control self, FontAttributes fontAttributes)
        {
            if (fontAttributes.HasFlag(FontAttributes.Italic))
            {
                self.FontStyle = FontStyles.Italic;
            }
            else
            {
                self.FontStyle = FontStyles.Normal;
            }

            if (fontAttributes.HasFlag(FontAttributes.Bold))
            {
                self.FontWeight = FontWeights.Bold;
            }
            else
            {
                self.FontWeight = FontWeights.Normal;
            }
        }
Beispiel #5
0
        private static string GetFontName(string fontFamily, FontAttributes fontAttributes)
        {
            var postfix = "Regular";
            var bold    = fontAttributes.HasFlag(FontAttributes.Bold);
            var italic  = fontAttributes.HasFlag(FontAttributes.Italic);

            if (bold && italic)
            {
                postfix = "BoldItalic";
            }
            else if (bold)
            {
                postfix = "Bold";
            }
            else if (italic)
            {
                postfix = "Italic";
            }

            return($"{fontFamily}-{postfix}.ttf");
        }
Beispiel #6
0
        private static string GetFontName(string fontFamily, FontAttributes fontAttributes)
        {
            var postfix = "";
            var bold    = fontAttributes.HasFlag(FontAttributes.Bold);
            var italic  = fontAttributes.HasFlag(FontAttributes.Italic);

            if (bold && italic)
            {
                postfix = "-BoldItalic";
            }
            else if (bold)
            {
                postfix = "-Bold";
            }
            else if (italic)
            {
                postfix = "-Italic";
            }

            return(fontFamily + postfix);
        }
Beispiel #7
0
        public static void SetStyleFont(this View view, string family, double size, FontAttributes attrs, Style style)
        {
#pragma warning disable RECS0018 // Comparison of floating point numbers with equality operator
            if (size == 14.0)
            {
                style.FontSize = null;
            }
            else
            {
                style.FontSize = size;
            }
#pragma warning restore RECS0018 // Comparison of floating point numbers with equality operator

            if (string.IsNullOrEmpty(family))
            {
                style.FontFamily = null;
            }
            else
            {
                style.FontFamily = family;
            }

            if (attrs.HasFlag(FontAttributes.Bold))
            {
                style.FontWeight = "bold";
            }
            else
            {
                style.FontWeight = null;
            }

            if (attrs.HasFlag(FontAttributes.Italic))
            {
                style.FontStyle = "italic";
            }
            else
            {
                style.FontStyle = null;
            }
        }
Beispiel #8
0
 public static FontStyle ToFontStyle(this FontAttributes fontAttributes) =>
 fontAttributes.HasFlag(FontAttributes.Italic) ? FontStyle.Italic : FontStyle.Normal;
Beispiel #9
0
 public static FontWeight ToFontWeight(this FontAttributes fontAttributes) =>
 fontAttributes.HasFlag(FontAttributes.Bold) ? FontWeights.Bold : FontWeights.Normal;
Beispiel #10
0
        public static Size MeasureSize(this string text, string fontFamily, double fontSize, FontAttributes fontAttrs, double widthConstraint, double heightConstraint)
        {
            if (string.IsNullOrEmpty(text))
            {
                return(Size.Zero);
            }

            var fontHeight = fontSize;
            var lineHeight = (int)(fontSize * 1.42857143); // Floor is intentional -- browsers round down

            var isBold = fontAttrs.HasFlag(FontAttributes.Bold);

            var props = isBold ? BoldCharacterProportions : CharacterProportions;
            var avgp  = isBold ? BoldAverageCharProportion : AverageCharProportion;

            var px               = 0.0;
            var lines            = 1;
            var maxPWidth        = 0.0;
            var pwidthConstraint = double.IsPositiveInfinity(widthConstraint) ? double.PositiveInfinity : widthConstraint / fontSize;
            var firstSpaceX      = -1.0;
            var lastSpaceIndex   = -1;
            var lineStartIndex   = 0;

            var n = text != null ? text.Length : 0;

            for (var i = 0; i < n; i++)
            {
                var c  = (int)text[i];
                var pw = (c < 128) ? props[c] : avgp;
                // Should we wrap?
                if (px + pw > pwidthConstraint && lastSpaceIndex > 0)
                {
                    lines++;
                    maxPWidth = Math.Max(maxPWidth, firstSpaceX);
                    i         = lastSpaceIndex;
                    while (i < n && text[i] == ' ')
                    {
                        i++;
                    }
                    i--;
                    px             = 0;
                    firstSpaceX    = -1;
                    lastSpaceIndex = -1;
                    lineStartIndex = i + 1;
                }
                else
                {
                    if (c == ' ')
                    {
                        if (i >= lineStartIndex && i > 0 && text[i - 1] != ' ')
                        {
                            firstSpaceX = px;
                        }
                        lastSpaceIndex = i;
                    }
                    px += pw;
                }
            }
            maxPWidth = Math.Max(maxPWidth, px);
            var width  = (int)Math.Ceiling(fontSize * maxPWidth);
            var height = lines * lineHeight;

            //Console.WriteLine ($"MEASURE TEXT SIZE {widthConstraint}x{heightConstraint} ==> {width}x{height} \"{text}\"");

            return(new Size(width, height));
        }
Beispiel #11
0
        public static Size MeasureSize(this string text, string fontFamily, double fontSize, FontAttributes fontAttrs, double widthConstraint, double heightConstraint)
        {
            if (string.IsNullOrEmpty(text))
            {
                return(Size.Zero);
            }

            var fontHeight = fontSize;
            var lineHeight = fontHeight * 1.4;

            var isBold = fontAttrs.HasFlag(FontAttributes.Bold);

            var props = isBold ? BoldCharacterProportions : CharacterProportions;
            var avgp  = isBold ? BoldAverageCharProportion : AverageCharProportion;

            var px               = 0.0;
            var lines            = 1;
            var maxPWidth        = 0.0;
            var pwidthConstraint = double.IsPositiveInfinity(widthConstraint) ? double.PositiveInfinity : widthConstraint / fontSize;
            var lastSpaceWidth   = -1.0;

            // Tiny little padding to account for sampling errors
            var pwidthHack = 1.0e-6;
            var plineHack  = 0.333;

            var n = text != null ? text.Length : 0;

            for (var i = 0; i < n; i++)
            {
                var c  = (int)text[i];
                var pw = (c < 128) ? props[c] : avgp;
                // Should we wrap?
                if (px + pw + plineHack > pwidthConstraint)
                {
                    lines++;
                    if (lastSpaceWidth > 0)
                    {
                        maxPWidth      = Math.Max(maxPWidth, lastSpaceWidth + pwidthHack);
                        px             = pw - lastSpaceWidth;
                        lastSpaceWidth = -1;
                    }
                    else
                    {
                        maxPWidth = Math.Max(maxPWidth, px + pwidthHack);
                        px        = 0;
                    }
                }
                if (c == ' ')
                {
                    lastSpaceWidth = pw;
                }
                px += pw;
            }
            maxPWidth = Math.Max(maxPWidth, px + pwidthHack);
            var width  = fontSize * maxPWidth;
            var height = lines * lineHeight;

            // Console.WriteLine ($"MEASURE TEXT SIZE {widthConstraint}x{heightConstraint} \"{text}\" == {width}x{height}");

            return(new Size(width, height));
        }