Beispiel #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 = ConverterUtility.ConvertToFontStyle(attrValue);
                if (style.HasValue)
                {
                    font.Style = style.Value;
                }
            }
            attrValue = this[name + "-variant"];
            if (attrValue != null)
            {
                var variant = ConverterUtility.ConvertToFontVariant(attrValue);
                if (variant.HasValue)
                {
                    font.Variant = variant.Value;
                }
            }
            attrValue = this[name + "-weight"];
            if (attrValue != null)
            {
                var weight = ConverterUtility.ConvertToFontWeight(attrValue);
                if (weight.HasValue)
                {
                    font.Weight = weight.Value;
                }
            }
            attrValue = this[name + "-family"];
            if (attrValue != null)
            {
                font.Family = ConverterUtility.ConvertToFontFamily(attrValue);
            }
            Unit unit = this.GetAsUnit(name + "-size");

            if (unit.IsValid)
            {
                font.Size = unit;
            }
            return(font);
        }
Beispiel #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 = ConverterUtility.ConvertToFontFamily(fontParts[1]);
                return(font);
            }

            int index = 0;

            FontStyle?style = ConverterUtility.ConvertToFontStyle(fontParts[index]);

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

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

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

            if (index + 2 > fontParts.Length)
            {
                return(HtmlFont.Empty);
            }
            FontWeight?weight = ConverterUtility.ConvertToFontWeight(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 = ConverterUtility.ConvertToFontFamily(fontParts[fontParts.Length - 1]);

            return(font);
        }
        /// <summary>
        /// Converts some common styling attributes to their OpenXml equivalence.
        /// </summary>
        /// <param name="styleAttributes">The collection of attributes where to store new discovered attributes.</param>
        public void ProcessCommonAttributes(HtmlEnumerator en, IList <OpenXmlElement> styleAttributes)
        {
            if (en.Attributes.Count == 0)
            {
                return;
            }

            var colorValue = en.StyleAttributes.GetAsColor("color");

            if (colorValue.IsEmpty)
            {
                colorValue = en.Attributes.GetAsColor("color");
            }
            if (!colorValue.IsEmpty)
            {
                styleAttributes.Add(new Color {
                    Val = colorValue.ToHexString()
                });
            }

            colorValue = en.StyleAttributes.GetAsColor("background-color");
            if (!colorValue.IsEmpty)
            {
                // change the way the background-color renders. It now uses Shading instead of Highlight.
                // Changes brought by Wude on http://notesforhtml2openxml.codeplex.com/discussions/277570
                styleAttributes.Add(new Shading {
                    Val = ShadingPatternValues.Clear, Fill = colorValue.ToHexString()
                });
            }

            string attrValue = en.StyleAttributes["text-decoration"];

            if (attrValue == "underline")
            {
                styleAttributes.Add(new Underline {
                    Val = UnderlineValues.Single
                });
            }
            else if (attrValue == "line-through")
            {
                styleAttributes.Add(new Strike());
            }

            String[] classes = en.Attributes.GetAsClass();
            if (classes != null)
            {
                for (int i = 0; i < classes.Length; i++)
                {
                    string className = documentStyle.GetStyle(classes[i], StyleValues.Character, ignoreCase: true);
                    if (className != null)                     // only one Style can be applied in OpenXml and dealing with inheritance is out of scope
                    {
                        styleAttributes.Add(new RunStyle()
                        {
                            Val = className
                        });
                        break;
                    }
                }
            }

            HtmlFont font = en.StyleAttributes.GetAsFont("font");

            if (!font.IsEmpty)
            {
                if (font.Style == FontStyle.Italic)
                {
                    styleAttributes.Add(new Italic());
                }

                if (font.Weight == FontWeight.Bold || font.Weight == FontWeight.Bolder)
                {
                    styleAttributes.Add(new Bold());
                }

                if (font.Variant == FontVariant.SmallCaps)
                {
                    styleAttributes.Add(new SmallCaps());
                }

                if (font.Family != null)
                {
                    styleAttributes.Add(new RunFonts()
                    {
                        Ascii = font.Family.Name, HighAnsi = font.Family.Name
                    });
                }

                // size are half-point font size
                if (font.Size.IsFixed)
                {
                    styleAttributes.Add(new FontSize()
                    {
                        Val = (font.Size.ValueInPoint * 2).ToString(CultureInfo.InvariantCulture)
                    });
                }
            }
        }