Ejemplo n.º 1
0
        /// <summary>
        /// Gets the font attribute and combine with the style, size and family.
        /// </summary>
        public HtmlFont GetAsFont(String name)
        {
            HtmlFont font      = HtmlFont.Parse(this[name]);
            string   attrValue = this[name + "-style"];

            if (attrValue != null)
            {
                var style = Converter.ToFontStyle(attrValue);
                if (style.HasValue)
                {
                    font.Style = style.Value;
                }
            }
            attrValue = this[name + "-variant"];
            if (attrValue != null)
            {
                var variant = Converter.ToFontVariant(attrValue);
                if (variant.HasValue)
                {
                    font.Variant = variant.Value;
                }
            }
            attrValue = this[name + "-weight"];
            if (attrValue != null)
            {
                var weight = Converter.ToFontWeight(attrValue);
                if (weight.HasValue)
                {
                    font.Weight = weight.Value;
                }
            }
            attrValue = this[name + "-family"];
            if (attrValue != null)
            {
                font.Family = Converter.ToFontFamily(attrValue);
            }
            Unit unit = this.GetAsUnit(name + "-size");

            if (unit.IsValid)
            {
                font.Size = unit;
            }
            return(font);
        }
Ejemplo n.º 2
0
        public static HtmlFont Parse(String str)
        {
            if (str == null)
            {
                return(HtmlFont.Empty);
            }

            // The font shorthand property sets all the font properties in one declaration.
            // The properties that can be set, are (in order):
            // "font-style font-variant font-weight font-size/line-height font-family"
            // The font-size and font-family values are required.
            // If one of the other values are missing, the default values will be inserted, if any.
            // http://www.w3schools.com/cssref/pr_font_font.asp


            // in order to split by white spaces, we remove any white spaces between 2 family names (ex: Verdana, Arial -> Verdana,Arial)
            str = System.Text.RegularExpressions.Regex.Replace(str, @",\s+?", ",");

            String[] fontParts = str.Split(HttpUtility.WhiteSpaces, StringSplitOptions.RemoveEmptyEntries);
            if (fontParts.Length < 2)
            {
                return(HtmlFont.Empty);
            }
            HtmlFont font = HtmlFont.Empty;

            if (fontParts.Length == 2)             // 2=the minimal set of required parameters
            {
                // should be the size and the family (in that order). Others are set to their default values
                font.size = ReadFontSize(fontParts[0]);
                if (!font.size.IsValid)
                {
                    return(HtmlFont.Empty);
                }
                font.family = Converter.ToFontFamily(fontParts[1]);
                return(font);
            }

            int index = 0;

            FontStyle?style = Converter.ToFontStyle(fontParts[index]);

            if (style.HasValue)
            {
                font.style = style.Value; index++;
            }

            if (index + 2 > fontParts.Length)
            {
                return(HtmlFont.Empty);
            }
            FontVariant?variant = Converter.ToFontVariant(fontParts[index]);

            if (variant.HasValue)
            {
                font.variant = variant.Value; index++;
            }

            if (index + 2 > fontParts.Length)
            {
                return(HtmlFont.Empty);
            }
            FontWeight?weight = Converter.ToFontWeight(fontParts[index]);

            if (weight.HasValue)
            {
                font.weight = weight.Value; index++;
            }

            if (fontParts.Length - index < 2)
            {
                return(HtmlFont.Empty);
            }
            font.size = ReadFontSize(fontParts[fontParts.Length - 2]);
            if (!font.size.IsValid)
            {
                return(HtmlFont.Empty);
            }

            font.family = Converter.ToFontFamily(fontParts[fontParts.Length - 1]);

            return(font);
        }