示例#1
0
 /// <summary>
 /// Merges any explictly declared styles on this component, onto the cacluated applied style
 /// </summary>
 /// <param name="applied"></param>
 protected virtual void MergeDeclaredStyles(Style applied)
 {
     if (this is IPDFStyledComponent)
     {
         IPDFStyledComponent styledComponent = this as IPDFStyledComponent;
         if (styledComponent.HasStyle)
         {
             styledComponent.Style.MergeInto(applied, Style.DirectStylePriority);
         }
     }
 }
示例#2
0
        private void ApplySizeStyle(IPDFStyledComponent found)
        {
            StyleValue <PDFUnit> val;

            if (this.HasX)
            {
                if (found.HasStyle && found.Style.TryGetValue(StyleKeys.PositionXKey, out val))
                {
                    found.Style.SetValue(StyleKeys.PositionXKey, val.Value + this.X);
                }
                else
                {
                    found.Style.SetValue(StyleKeys.PositionXKey, this.X);
                }
            }

            if (this.HasY)
            {
                if (found.HasStyle && found.Style.TryGetValue(StyleKeys.PositionYKey, out val))
                {
                    found.Style.SetValue(StyleKeys.PositionYKey, val.Value + this.Y);
                }
                else
                {
                    found.Style.SetValue(StyleKeys.PositionYKey, this.Y);
                }
            }

            if (this.HasWidth)
            {
                if (found.HasStyle && found.Style.TryGetValue(StyleKeys.SizeWidthKey, out val))
                {
                    found.Style.SetValue(StyleKeys.SizeWidthKey, val.Value + this.Width);
                }
                else
                {
                    found.Style.SetValue(StyleKeys.SizeWidthKey, this.Width);
                }
            }

            if (this.HasHeight)
            {
                if (found.HasStyle && found.Style.TryGetValue(StyleKeys.SizeHeightKey, out val))
                {
                    found.Style.SetValue(StyleKeys.SizeHeightKey, val.Value + this.Height);
                }
                else
                {
                    found.Style.SetValue(StyleKeys.SizeHeightKey, this.Height);
                }
            }
        }
示例#3
0
        public bool SetStyleValue(IHtmlContentParser parser, IPDFStyledComponent component, CSSStyleItemReader reader)
        {
            IParserStyleFactory found;

            if (string.IsNullOrEmpty(reader.CurrentAttribute) && reader.ReadNextAttributeName() == false)
            {
                return(false);
            }

            if (_knownStyles.TryGetValue(reader.CurrentAttribute, out found))
            {
                return(found.SetStyleValue(parser, component, reader));
            }
            else
            {
                if (null != parser && parser.IsLogging)
                {
                    parser.Context.TraceLog.Add(TraceLevel.Warning, "CSS", "Could not set the style value on attribute '" + reader.CurrentAttribute + "' as it is not a known style attribute.");
                }
            }
            return(false);
        }
        public bool IsMatchedTo(IPDFStyledComponent component, ComponentState state, out int priority)
        {
            priority = 0;
            // check everything
            // return false if it's not a match
            //otherwise return true at the end.

            // fastest first - ID, Element, Class, Type

            if (string.IsNullOrEmpty(this.AppliedID) == false)
            {
                if (string.IsNullOrEmpty(component.ID))
                {
                    return(false);
                }
                else if (!this.AppliedID.Equals(component.ID))
                {
                    return(false);
                }
            }

            if (string.IsNullOrEmpty(this.AppliedElement) == false)
            {
                if (string.IsNullOrEmpty(component.ElementName))
                {
                    return(false);
                }
                else if (!this.AppliedElement.Equals(component.ElementName))
                {
                    return(false);
                }
            }

            if (null != this.AppliedClass)
            {
                if (string.IsNullOrEmpty(component.StyleClass))
                {
                    return(false);
                }
                else if (!this.AppliedClass.IsMatchedTo(component, state))
                {
                    return(false);
                }
            }

            if (null != this.AppliedType)
            {
                if ((this.AppliedType.IsInstanceOfType(component)) == false)
                {
                    return(false);
                }
            }

            if (this.HasAncestor)
            {
                var parentPriority = 0;
                var parent         = component.Parent;
                if (this.Ancestor.Placement == StylePlacement.DirectParent)
                {
                    while (null != parent && !(parent is IPDFStyledComponent)) // select the closest styled component
                    {
                        parent = parent.Parent;
                    }

                    if (null == parent)
                    {
                        return(false);
                    }

                    if (this.Ancestor.IsMatchedTo(parent as IPDFStyledComponent, state, out parentPriority))
                    {
                        priority = this.Priority;
                        return(true);
                    }
                }
                else
                {
                    while (null != parent)
                    {
                        if (parent is IPDFStyledComponent)
                        {
                            if (this.Ancestor.IsMatchedTo(parent as IPDFStyledComponent, state, out parentPriority))
                            {
                                priority = this.Priority;
                                return(true);
                            }
                        }
                        parent = parent.Parent;
                    }
                }
                return(false);
            }
            else
            {
                priority = this.Priority;
                return(true);
            }
        }
        /// <summary>
        /// Reads each of the atributes in turn (if any) in the current Source and sets them onto the parsed compent.
        /// Style attributes will be split again and set individually. Resets the buffer
        /// </summary>
        /// <param name="parsed"></param>
        /// <param name="tagname"></param>
        private void ReadAttributes(IPDFComponent parsed, string tagname)
        {
            //Read to the start of each attribute in turn
            //Then check the current name aginst the Style and Class attribute values (including the equals)
            //If they are a match then read the values, otherwise skip onto the next white space.

            while (!this.Source.EOS)
            {
                while (!this.Source.EOS && IsWhiteSpace(this.Source.Current))
                {
                    if (!this.Source.MoveNext())
                    {
                        return;
                    }
                }

                Buffer.Clear();
                bool foundAttrName = false;

                char cur = this.Source.Current;
                if (cur == HTMLCloseTag)
                {
                    break;
                }
                else if (cur == HTMLEndMarker)
                {
                    if (!this.Source.MoveNext() || this.Source.Current == HTMLCloseTag)
                    {
                        break;
                    }
                    Buffer.Append(cur);
                }
                else
                {
                    Buffer.Append(cur);


                    while (this.Source.MoveNext())
                    {
                        cur = this.Source.Current;

                        if (this.Source.Current == HTMLAttributeKVSeparator)
                        {
                            foundAttrName = true;
                            break;
                        }
                        Buffer.Append(cur);
                    }
                }

                if (foundAttrName && Buffer.Length > 0)
                {
                    string attrName = Buffer.ToString();
                    Buffer.Clear();

                    if (!this.Source.MoveNext())
                    {
                        return;
                    }

                    string attrValue = ReadAttributeValue();

                    if (!string.IsNullOrEmpty(attrValue))
                    {
                        if (string.Equals(attrName, HTMLStyleAttribute, StringComparison.OrdinalIgnoreCase))
                        {
                            if (this.Parser.Settings.SkipStyles == false && parsed is IPDFStyledComponent)
                            {
                                IPDFStyledComponent parsedStyled = parsed as IPDFStyledComponent;

                                CSSStyleItemReader reader = new CSSStyleItemReader(attrValue);
                                while (reader.ReadNextAttributeName())
                                {
                                    this.Parser.StyleFactory.SetStyleValue(this.Parser, parsedStyled, reader);
                                }
                            }
                        }
                        else if (string.Equals(attrName, HTMLCssClassName, StringComparison.OrdinalIgnoreCase))
                        {
                            if (this.Parser.Settings.SkipCssClasses == false)
                            {
                                attrValue = HTMLUnEntities(attrValue);
                                this.Parser.ComponentFactory.SetAttribute(this.Parser, parsed, tagname, attrName, attrValue);
                            }
                        }
                        else
                        {
                            attrValue = HTMLUnEntities(attrValue);
                            this.Parser.ComponentFactory.SetAttribute(this.Parser, parsed, tagname, attrName, attrValue);
                        }
                    }

                    if (this.Source.Current == '\'' || this.Source.Current == '"')
                    {
                        this.Source.MoveNext(); //push past the end of the closing quotes;
                    }
                }
                else
                {
                    while (!this.Source.EOS && !IsWhiteSpace(this.Source.Current))
                    {
                        if (!this.Source.MoveNext())
                        {
                            return;
                        }
                        if (this.Source.Current == HTMLCloseTag) //safe because > is reserved and should not be used in attributes.
                        {
                            return;
                        }
                    }
                }
            }
        }
示例#6
0
 bool IParserStyleFactory.SetStyleValue(IHtmlContentParser parser, IPDFStyledComponent onComponent, CSSStyleItemReader reader)
 {
     return(this.SetStyleValue(onComponent.Style, reader, parser.Context));
 }
示例#7
0
 protected virtual void SetStyleAttribute(IPDFStyledComponent comp, string name, string value)
 {
     //TODO: Implement style parsing.
 }
        public bool IsMatchedTo(IPDFStyledComponent component, ComponentState state)
        {
            var equals = component.StyleClass.Equals(this.ClassName);

            if (equals)
            {
                if (null != this.AndClass)
                {
                    return(AndClass.IsMatchedTo(component, state));
                }
                else
                {
                    return(true);
                }
            }
            else
            {
                int foundIndex = 0;

                do
                {
                    if (ContainsClassName(component.StyleClass, foundIndex, out foundIndex))
                    {
                        if (null != this.AndClass)
                        {
                            return(AndClass.IsMatchedTo(component, state));
                        }
                        else
                        {
                            return(true);
                        }
                    }
                    else if (foundIndex > -1)
                    {
                        foundIndex++;
                    }
                }while (foundIndex >= 0);

                return(false);
            }

            //We are not equal, so if we are shorted or the same length return false
            if (component.StyleClass.Length <= this.ClassName.Length)
            {
                return(false);
            }

            //Now we know we are longer

            var starts = component.StyleClass.StartsWith(this.ClassName);

            if (starts && Char.IsWhiteSpace(component.StyleClass, this.ClassName.Length))
            {
                if (null != this.AndClass)
                {
                    return(AndClass.IsMatchedTo(component, state));
                }
                else
                {
                    return(true);
                }
            }

            var ends  = component.StyleClass.EndsWith(this.ClassName);
            int index = component.StyleClass.Length - this.ClassName.Length;

            if (ends && Char.IsWhiteSpace(component.StyleClass, index))
            {
                if (null != this.AndClass)
                {
                    return(this.AndClass.IsMatchedTo(component, state));
                }
                else
                {
                    return(true);
                }
            }
        }