Ejemplo n.º 1
0
        public static CSSValue ParseValue(Rule rule, DOMElement element)
        {
            CSSValue cssValue = new CSSValue();

            if (rule.Value.StartsWith("calc("))
            {
                cssValue.Unit      = Unit.CalcFunc;
                rule.ComputedValue = cssValue;

                return(cssValue);
            }
            else
            {
                List <string> array = Regex.Split(rule.Value, @"[^0-9\.]+").Where(c => c != "." && c.Trim() != "").ToList();

                if (array.Count > 0 && rule.Value.Length > 2)
                {
                    float value      = float.Parse(array[0], CultureInfo.InvariantCulture.NumberFormat);
                    int   startIndex = value.ToString().Length;

                    string unitType = rule.Value.Substring(startIndex, rule.Value.Length - startIndex).ToLower();
                    cssValue.Value = value;

                    foreach (Unit unit in allUnits)
                    {
                        string abbreviation = GetUnitAbbreviation(unit);

                        if (unitType == abbreviation)
                        {
                            cssValue.Unit = unit;
                        }
                    }

                    rule.ComputedValue = cssValue;

                    cssValue.Value = ConvertAnyUnitToPixels(rule, element);
                    cssValue.ValueBeforeComputing = value;

                    return(cssValue);
                }
            }

            return(null);
        }
Ejemplo n.º 2
0
        public static float ConvertAnyUnitToPixels(Rule rule, DOMElement element)
        {
            CSSValue cssValue = rule.ComputedValue;

            if (cssValue.Unit == Unit.Px)
            {
                return(cssValue.Value);
            }
            else if (cssValue.Unit == Unit.Cm)
            {
                return(CSSUnitsConverter.CmToPX(cssValue.Value));
            }
            else if (cssValue.Unit == Unit.Mm)
            {
                return(CSSUnitsConverter.MmToPX(cssValue.Value));
            }
            else if (cssValue.Unit == Unit.In)
            {
                return(CSSUnitsConverter.InToPX(cssValue.Value));
            }
            else if (cssValue.Unit == Unit.Percent)
            {
                return(CSSUnitsConverter.PercentToPx(rule, element));
            }
            else if (cssValue.Unit == Unit.Pt)
            {
                return(CSSUnitsConverter.PtToPX(cssValue.Value));
            }
            else if (cssValue.Unit == Unit.Em)
            {
                return(CSSUnitsConverter.EmToPx(rule, element));
            }
            else if (cssValue.Unit == Unit.Vh)
            {
                return(CSSUnitsConverter.VhToPx(rule, element));
            }
            else if (cssValue.Unit == Unit.Vw)
            {
                return(CSSUnitsConverter.VwToPx(rule, element));
            }

            return(-1f);
        }
Ejemplo n.º 3
0
        private static void SetFonts(List <DOMElement> elements, List <RuleSet> ruleSets, List <Rule> inheritedStyles = null)
        {
            if (inheritedStyles == null)
            {
                inheritedStyles = new List <Rule>();
            }

            foreach (DOMElement element in elements)
            {
                List <Rule> newInheritedStyles = new List <Rule>();

                string fontFamily = "Times New Roman";

                List <FontStyle>      fontStyles      = new List <FontStyle>();
                List <FontWeight>     fontWeights     = new List <FontWeight>();
                List <TextDecoration> textDecorations = new List <TextDecoration>();

                float fontSize = 16;
                Color color    = Color.Black;

                List <Rule> inheritedStylesCopy = new List <Rule>();
                inheritedStylesCopy.AddRange(inheritedStyles);

                // Set styles for current element used by its parents.
                // For example if a div is in b and the b is in i,
                // the div's text will be bold and italic.

                foreach (Rule rule in inheritedStyles)
                {
                    if (rule.Property == "font-weight")
                    {
                        if (rule.Value == "bold")
                        {
                            fontWeights.Add(FontWeight.Bold);
                        }
                        else if (rule.Value == "normal")
                        {
                            fontWeights.Add(FontWeight.Normal);
                        }
                    }
                    else if (rule.Property == "font-style")
                    {
                        if (rule.Value == "italic")
                        {
                            fontStyles.Add(FontStyle.Italic);
                        }
                        else if (rule.Value == "normal")
                        {
                            fontStyles.Add(FontStyle.Normal);
                        }
                    }
                    else if (rule.Property == "font-family")
                    {
                        fontFamily = rule.Value;
                    }
                    else if (rule.Property == "color")
                    {
                        color = ColorTranslator.FromHtml(rule.Value);
                        element.Style.Color = color;
                    }
                    else if (rule.Property == "text-decoration")
                    {
                        if (rule.Value == "underline")
                        {
                            textDecorations.Add(TextDecoration.Underline);
                        }
                    }
                    else if (rule.Property == "font-size")
                    {
                        CSSValue parsedValue = CSSUnits.ParseValue(rule, element);

                        fontSize = parsedValue.Value;

                        inheritedStylesCopy.Remove(rule);
                    }
                }

                // Set styles for current element.
                if (element.Type == DOMElementType.Normal)
                {
                    foreach (RuleSet ruleSet in ruleSets)
                    {
                        // Check if the selector in the rule set is matching current element's selector.
                        if (element.HasSelector(ruleSet.Selector))
                        {
                            element.RuleSet = ruleSet;

                            foreach (Rule rule in ruleSet.Rules)
                            {
                                if (rule.Property == "font-weight")
                                {
                                    if (rule.Value == "bold")
                                    {
                                        fontWeights.Add(FontWeight.Bold);
                                    }
                                    else if (rule.Value == "normal")
                                    {
                                        fontWeights.Add(FontWeight.Normal);
                                    }

                                    newInheritedStyles.Add(rule);
                                }
                                else if (rule.Property == "font-style")
                                {
                                    if (rule.Value == "italic")
                                    {
                                        fontStyles.Add(FontStyle.Italic);
                                    }
                                    else if (rule.Value == "normal")
                                    {
                                        fontStyles.Add(FontStyle.Normal);
                                    }

                                    newInheritedStyles.Add(rule);
                                }
                                else if (rule.Property == "font-size")
                                {
                                    CSSValue parsedValue = CSSUnits.ParseValue(rule, element);

                                    fontSize = parsedValue.Value;

                                    newInheritedStyles.Add(rule);
                                }
                                else if (rule.Property == "font-family")
                                {
                                    fontFamily = rule.Value;

                                    newInheritedStyles.Add(rule);
                                }
                                else if (rule.Property == "color")
                                {
                                    color = ColorTranslator.FromHtml(rule.Value);
                                    element.Style.Color = color;

                                    newInheritedStyles.Add(rule);
                                }
                                else if (rule.Property == "text-decoration")
                                {
                                    if (rule.Value == "underline")
                                    {
                                        textDecorations.Add(TextDecoration.Underline);
                                    }

                                    newInheritedStyles.Add(rule);
                                }
                            }
                        }
                    }
                }

                // Apply all the font styles added before.
                Font font = new Font(new FontFamily(fontFamily), fontSize, System.Drawing.FontStyle.Regular, GraphicsUnit.Pixel);

                foreach (FontStyle fontStyle in fontStyles)
                {
                    if (fontStyle == FontStyle.Italic)
                    {
                        font = new Font(font, font.Style ^ System.Drawing.FontStyle.Italic);
                    }
                }

                foreach (FontWeight fontWeight in fontWeights)
                {
                    if (fontWeight == FontWeight.Bold)
                    {
                        font = new Font(font, font.Style ^ System.Drawing.FontStyle.Bold);
                    }
                    else if (fontWeight == FontWeight.Normal)
                    {
                        font = new Font(font, font.Style ^ System.Drawing.FontStyle.Regular);
                    }
                }

                foreach (TextDecoration textDecoration in textDecorations)
                {
                    if (textDecoration == TextDecoration.Underline)
                    {
                        font = new Font(font, font.Style ^ System.Drawing.FontStyle.Underline);
                    }
                }

                element.Style.Font = font;

                // Pass used styles by the current element and its parents, to its children.
                foreach (Rule rule in inheritedStylesCopy)
                {
                    newInheritedStyles.Add(rule);
                }

                if (element.Children.Count > 0)
                {
                    SetFonts(element.Children, ruleSets, newInheritedStyles);
                }
            }
        }