コード例 #1
0
ファイル: DomImageElement.cs プロジェクト: ywscr/CSharpWriter
 /// <summary>
 /// 从指定的资源地址加载图片数据
 /// </summary>
 /// <param name="strUrl">图片资源地址</param>
 /// <param name="setSize">是否设置对象大小</param>
 /// <returns>操作是否成功</returns>
 public bool LoadImage(string strUrl, bool setSize)
 {
     if (myImage.Load(strUrl) > 0)
     {
         if (setSize)
         {
             UpdateSize();
         }
         return(true);
     }
     return(false);
 }
コード例 #2
0
ファイル: HtmlLoader.cs プロジェクト: ywscr/CSharpWriter
        private void CreateElements(
            HTMLElement rootHtmlElement,
            DomElement rootDomElement,
            DocumentContentStyle currentStyle)
        {
            if (rootHtmlElement.ChildNodes == null)
            {
                return;
            }
            foreach (HTMLElement element in rootHtmlElement.ChildNodes)
            {
                if (element is HTMLCommentElement)
                {
                    // 忽略注释
                    continue;
                }
                string tagName = element.FixTagName;
                switch (tagName)
                {
                case "div":
                {
                    CreateElements(element, rootDomElement, currentStyle);
                    DomElement p = this.DomDocument.CreateElement(typeof(DomParagraphFlagElement));
                    p.StyleIndex = this.DomDocument.ContentStyles.GetStyleIndex(currentStyle);
                    rootDomElement.Elements.Add(p);
                }
                break;

                case "ul":    // 原点式列表
                case "ol":    // 数值式列表
                    foreach (HTMLElement li in element.ChildNodes)
                    {
                        if (li.FixTagName == "li")
                        {
                            CreateElements(li, rootDomElement, currentStyle);
                            DomParagraphFlagElement flag = (DomParagraphFlagElement)this.DomDocument.CreateElement(typeof(DomParagraphFlagElement));
                            DocumentContentStyle    s    = (DocumentContentStyle)currentStyle.Clone();
                            if (tagName == "ul")
                            {
                                s.BulletedList = true;
                            }
                            else
                            {
                                s.NumberedList = true;
                            }
                            flag.StyleIndex = this.DomDocument.ContentStyles.GetStyleIndex(s);
                            rootDomElement.Elements.Add(flag);
                        } //if
                    }     //foreach
                    break;

                case "pre":
                {
                    // 预览文本
                    DocumentContentStyle s    = CreateStyle(element, currentStyle);
                    DomElementList       list = this.DomDocument.CreateTextElements(element.InnerText, null, s);
                    rootDomElement.Elements.AddRange(list);
                    DomElement p = this.DomDocument.CreateElement(typeof(DomParagraphFlagElement));
                    p.StyleIndex = this.DomDocument.ContentStyles.GetStyleIndex(currentStyle);
                    rootDomElement.Elements.Add(p);
                    break;
                }

                case "img":
                {
                    // 图片
                    DocumentContentStyle s = CreateStyle(element, currentStyle);
                    s.BorderColor  = Color.Black;
                    s.BorderLeft   = true;
                    s.BorderTop    = true;
                    s.BorderRight  = true;
                    s.BorderBottom = true;
                    s.BorderWidth  = 0;
                    if (element.HasAttribute("border"))
                    {
                        s.BorderWidth = ToInt32(element.GetAttribute("border"));
                    }
                    DomImageElement img = (DomImageElement)this.DomDocument.CreateElement(typeof(DomImageElement));

                    img.StyleIndex = this.DomDocument.ContentStyles.GetStyleIndex(s);
                    if (element.HasAttribute("width"))
                    {
                        img.Width = (float )ToLength(element.GetAttribute("width"));
                    }
                    if (element.HasAttribute("height"))
                    {
                        img.Height = ( float )ToLength(element.GetAttribute("height"));
                    }
                    if (element.HasAttribute("id"))
                    {
                        img.ID = element.GetAttribute("id");
                    }
                    if (element.HasAttribute("alt"))
                    {
                        img.Alt = element.GetAttribute("alt");
                    }
                    if (element.HasAttribute("title"))
                    {
                        img.Title = element.GetAttribute("title");
                    }
                    if (element.HasAttribute("src"))
                    {
                        XImageValue v   = new XImageValue();
                        string      url = this.HtmlDocument.GetAbsoluteURL(element.GetAttribute("src"));
                        try
                        {
                            string msg = string.Format(WriterStrings.Downloading_URL, url);
                            if (this.DomDocument.EditorControl != null)
                            {
                                this.DomDocument.EditorControl.SetStatusText(msg);
                            }
                            if (this.DomDocument.Options.BehaviorOptions.DebugMode)
                            {
                                System.Diagnostics.Debug.Write(msg);
                            }
                            int len = v.Load(url);
                            if (this.DomDocument.Options.BehaviorOptions.DebugMode)
                            {
                                System.Diagnostics.Debug.WriteLine(WriterUtils.FormatByteSize(len));
                            }
                        }
                        catch (Exception ext)
                        {
                            img.Alt = url + ":" + ext.Message;
                            if (this.DomDocument.Options.BehaviorOptions.DebugMode)
                            {
                                System.Diagnostics.Debug.WriteLine(WriterStrings.Fail);
                            }
                        }
                        if (this.DomDocument.EditorControl != null)
                        {
                            this.DomDocument.EditorControl.SetStatusText(null);
                        }
                        if (v.HasContent == false)
                        {
                            img.Alt = url;
                            if (img.Width == 0)
                            {
                                img.Width = 300;
                            }
                            if (img.Height == 0)
                            {
                                img.Height = 150;
                            }
                        }
                        img.Image = v;
                    }

                    if (img.Width == 0 || img.Height == 0)
                    {
                        img.UpdateSize();
                    }
                    rootDomElement.Elements.Add(img);
                }
                break;

                case "#text":
                {
                    // 纯文本片段
                    string text = DCSoft.Common.StringFormatHelper.NormalizeSpace(element.Text);
                    text = System.Web.HttpUtility.HtmlDecode(text);
                    if (string.IsNullOrEmpty(text) == false)
                    {
                        DomElementList cs = this._DomDocument.CreateTextElements(
                            text,
                            currentStyle,
                            currentStyle);
                        if (cs != null && cs.Count > 0)
                        {
                            rootDomElement.Elements.AddRange(cs);
                        }
                    }
                }
                break;

                case "p":
                {
                    // 段落
                    DocumentContentStyle ps = CreateStyle(element, currentStyle);
                    CreateElements(element, rootDomElement, ps);
                    DomParagraphFlagElement flag = ( DomParagraphFlagElement )_DomDocument.CreateElement(typeof(DomParagraphFlagElement));
                    flag.StyleIndex = _DomDocument.ContentStyles.GetStyleIndex(ps);
                    rootDomElement.Elements.Add(flag);
                }
                break;

                case "br":
                {
                    // 软回车
                    DomLineBreakElement lb = (DomLineBreakElement)_DomDocument.CreateElement(typeof(DomLineBreakElement));
                    rootDomElement.Elements.Add(lb);
                }
                break;

                case "sup":
                {
                    // 上标
                    DocumentContentStyle ss = ( DocumentContentStyle )currentStyle.Clone();
                    ss.Superscript = true;
                    CreateElements(element, rootDomElement, ss);
                }
                break;

                case "sub":
                {
                    // 下标
                    DocumentContentStyle ss = (DocumentContentStyle)currentStyle.Clone();
                    ss.Subscript = true;
                    CreateElements(element, rootDomElement, ss);
                }
                break;

                case "strong":
                case "b":
                {
                    // 粗体
                    DocumentContentStyle ss = (DocumentContentStyle)currentStyle.Clone();
                    ss.Bold = true;
                    CreateElements(element, rootDomElement, ss);
                }
                break;

                case "i":
                {
                    // 斜体
                    DocumentContentStyle ss = (DocumentContentStyle)currentStyle.Clone();
                    ss.Italic = true;
                    CreateElements(element, rootDomElement, ss);
                }
                break;

                case "strike":
                {
                    // 删除线
                    DocumentContentStyle ss = (DocumentContentStyle)currentStyle.Clone();
                    ss.Strikeout = true;
                    CreateElements(element, rootDomElement, ss);
                }
                break;

                case "a":
                {
                    // 超链接
                    DocumentContentStyle ss = (DocumentContentStyle)currentStyle.Clone();
                    ss.Color = Color.Blue;
                    CreateElements(element, rootDomElement, ss);
                }
                break;

                case "font":
                {
                    // 字体
                    DocumentContentStyle ss = CreateStyle(element, currentStyle);
                    if (element.HasAttribute("color"))
                    {
                        // 文字颜色
                        ss.Color = ToColor(element.GetAttribute("color"), Color.Black);
                    }
                    if (element.HasAttribute("face"))
                    {
                        // 字体名称
                        ss.FontName = GetFontName(element.GetAttribute("face"));
                    }
                    if (element.HasAttribute("size"))
                    {
                        // 文字大小
                        int size = ToInt32(element.GetAttribute("size"));
                        switch (size)
                        {
                        case 1: ss.FontSize = 7; break;

                        case 2: ss.FontSize = 10; break;

                        case 3: ss.FontSize = 12; break;

                        case 4: ss.FontSize = 14; break;

                        case 5: ss.FontSize = 18; break;

                        case 6: ss.FontSize = 24; break;

                        case 7: ss.FontSize = 35; break;
                        }
                    }
                    CreateElements(element, rootDomElement, ss);
                }
                break;

                case "h1":
                case "h2":
                case "h3":
                case "h4":
                case "h5":
                case "h6":
                {
                    // 标题
                    rootDomElement.Elements.Add(this.DomDocument.CreateElement(typeof(DomParagraphFlagElement)));
                    float fz = 9;
                    switch (tagName)
                    {
                    case "h1": fz = 24; break;

                    case "h2": fz = 18; break;

                    case "h3": fz = 13; break;

                    case "h4": fz = 12; break;

                    case "h5": fz = 10; break;

                    case "h6": fz = 8; break;
                    }
                    DocumentContentStyle ss = CreateStyle(element, currentStyle);
                    if (XDependencyObject.HasPropertyValue(ss, "FontSize"))
                    {
                        ss.FontSize = fz;
                    }
                    CreateElements(element, rootDomElement, ss);
                    rootDomElement.Elements.Add(this.DomDocument.CreateElement(typeof(DomParagraphFlagElement)));
                }
                break;

                default:
                {
                    DocumentContentStyle ds = CreateStyle(element, (DocumentContentStyle)currentStyle);
                    CreateElements(element, rootDomElement, ds);
                }
                break;
                } //switch
            }     //foreach
        }
コード例 #3
0
ファイル: HtmlLoader.cs プロジェクト: ywscr/CSharpWriter
        private void ParseStyleItem(DocumentContentStyle cstyle, string styleName, string styleValue)
        {
            switch (styleName)
            {
            case "width":
            {
                float v = 0;
                if (TryParseLength(styleValue, out v))
                {
                    cstyle.WidthValue = v;
                }
            }
            break;

            case "height":
            {
                float v = 0;
                if (TryParseLength(styleValue, out v))
                {
                    cstyle.HeightValue = v;
                }
            }
            break;

            case "background":
            {
                // 解析背景设置
                string[] items = SplitItems(styleValue);
                if (items != null)
                {
                    foreach (string item in items)
                    {
                        Color  c   = Color.Empty;
                        string url = GetAttributeUrl(item);
                        if (url != null)
                        {
                            url = this.HtmlDocument.GetAbsoluteURL(url);
                            XImageValue img = new XImageValue();
                            if (img.Load(url) <= 0)
                            {
                                cstyle.BackgroundImage = img;
                            }
                        }
                        else if (TryParseColor(item, out c))
                        {
                            cstyle.BackgroundColor = c;
                        }
                        else if (string.Equals(item, "repeat", StringComparison.CurrentCultureIgnoreCase))
                        {
                            cstyle.BackgroundRepeat = true;
                        }
                        else if (string.Equals(item, "no-repeat", StringComparison.CurrentCultureIgnoreCase))
                        {
                            cstyle.BackgroundRepeat = false;
                        }
                    }        //foreach
                }
            }
            break;

            case "background-color":
            {
                Color c = Color.Empty;
                if (TryParseColor(styleValue, out c))
                {
                    cstyle.BackgroundColor = c;
                }
            }
            break;

            case "background-repeat":
            {
                string text = Trim(styleValue);
                if (string.Equals(text, "repeat", StringComparison.CurrentCultureIgnoreCase))
                {
                    cstyle.BackgroundRepeat = true;
                }
                else if (string.Equals(text, "no-repeat", StringComparison.CurrentCultureIgnoreCase))
                {
                    cstyle.BackgroundRepeat = false;
                }
                else
                {
                    cstyle.BackgroundRepeat = true;
                }
            }
            break;

            case "border":
            {
                // 解析边框设置
                string[] items = SplitItems(styleValue);
                if (items != null)
                {
                    foreach (string item in items)
                    {
                        DashStyle ds = DashStyle.Custom;
                        Color     c  = Color.Empty;
                        float     w  = 0;
                        if (TryParseBorderStyle(item, out ds))
                        {
                            cstyle.BorderStyle = ds;
                            if (ds == DashStyle.Custom)
                            {
                                cstyle.BorderStyle = DashStyle.Solid;
                                cstyle.BorderWidth = 0;
                                break;
                            }
                        }
                        else if (TryParseColor(item, out c))
                        {
                            cstyle.BorderColor = c;
                        }
                        else if (TryParseBorderWidth(item, ref w))
                        {
                            cstyle.BorderWidth = w;
                        }
                    }        //foreach
                }
            }
            break;

            case "border-color":
            {
                Color c = Color.Empty;
                if (TryParseColor(styleValue, out c))
                {
                    cstyle.BorderColor = c;
                }
            }
            break;

            case "border-width":
            {
                float w = 0;
                if (TryParseBorderWidth(styleValue, ref w))
                {
                    cstyle.BorderWidth = w;
                }
            }
            break;

            case "border-style":
            {
                DashStyle bs = DashStyle.Solid;
                if (TryParseBorderStyle(styleValue, out bs))
                {
                    cstyle.BorderStyle = bs;
                    if (bs == DashStyle.Custom)
                    {
                        cstyle.BorderStyle = DashStyle.Solid;
                        cstyle.BorderWidth = 0;
                    }
                }
            }
            break;

            case "color":
            {
                Color c = Color.Empty;
                if (TryParseColor(styleValue, out c))
                {
                    cstyle.Color = c;
                }
            }
            break;

            case "font":
            {
                string[] items = SplitItems(styleValue);
                if (items != null)
                {
                    for (int iCount = 0; iCount < items.Length; iCount++)
                    {
                        string item = items[iCount];
                        float  size = 0;
                        if (string.Equals(item, "italic", StringComparison.CurrentCultureIgnoreCase))
                        {
                            cstyle.Italic = true;
                        }
                        else if (string.Equals(item, "oblique", StringComparison.CurrentCultureIgnoreCase))
                        {
                            cstyle.Italic = true;
                        }
                        else if (string.Equals(item, "bold", StringComparison.CurrentCultureIgnoreCase))
                        {
                            cstyle.Bold = true;
                        }
                        else if (string.Equals(item, "bolder", StringComparison.CurrentCultureIgnoreCase))
                        {
                            cstyle.Bold = true;
                        }
                        else if (TryParseLength(item, out size))
                        {
                            cstyle.FontSize = GraphicsUnitConvert.Convert(size, this.DomDocument.DocumentGraphicsUnit, GraphicsUnit.Point);
                        }
                        else
                        {
                            string name2 = GetFontName(item);
                            if (name2 != null)
                            {
                                cstyle.FontName = name2;
                            }
                        }
                    }
                }
            }
            break;

            case "font-family":
            {
                string fn = GetFontName(styleValue);
                if (fn != null)
                {
                    cstyle.FontName = fn;
                }
            }
            break;

            case "font-size":
            {
                float size = 0;
                if (TryParseLength(styleValue, out size))
                {
                    cstyle.FontSize = GraphicsUnitConvert.Convert(
                        size,
                        this.DomDocument.DocumentGraphicsUnit,
                        GraphicsUnit.Point);
                }
            }
            break;

            case "font-style":
            {
                if (Contains(styleValue, "italic") ||
                    Contains(styleValue, "oblique"))
                {
                    cstyle.Italic = true;
                }
            }
            break;

            case "font-weight":
                if (Contains(styleValue, "bold"))
                {
                    cstyle.Bold = true;
                }
                else if (Contains(styleValue, "700"))
                {
                    cstyle.Bold = true;
                }
                break;

            case "line-height":
                break;

            case "text-align":
                if (Contains(styleValue, "left"))
                {
                    cstyle.Align = DocumentContentAlignment.Left;
                }
                else if (Contains(styleValue, "right"))
                {
                    cstyle.Align = DocumentContentAlignment.Right;
                }
                else if (Contains(styleValue, "center"))
                {
                    cstyle.Align = DocumentContentAlignment.Center;
                }
                else if (Contains(styleValue, "justify"))
                {
                    cstyle.Align = DocumentContentAlignment.Justify;
                }
                break;

            case "text-indent":
            {
                float v = 0;
                if (TryParseLength(styleValue, out v))
                {
                    cstyle.FirstLineIndent = v;
                }
            }
            break;

            case "padding":
            {
                // 读取内边距
                float[] values = ParseLengths(styleValue);
                if (values != null)
                {
                    if (values.Length >= 4)
                    {
                        cstyle.PaddingTop    = values[0];
                        cstyle.PaddingRight  = values[1];
                        cstyle.PaddingBottom = values[2];
                        cstyle.PaddingLeft   = values[3];
                    }
                    else if (values.Length == 1)
                    {
                        cstyle.PaddingLeft   = values[0];
                        cstyle.PaddingTop    = values[0];
                        cstyle.PaddingRight  = values[0];
                        cstyle.PaddingBottom = values[0];
                    }
                    else if (values.Length == 2)
                    {
                        cstyle.PaddingTop    = values[0];
                        cstyle.PaddingBottom = values[0];
                        cstyle.PaddingLeft   = values[1];
                        cstyle.PaddingRight  = values[1];
                    }
                    else if (values.Length == 3)
                    {
                        cstyle.PaddingTop    = values[0];
                        cstyle.PaddingRight  = values[1];
                        cstyle.PaddingLeft   = values[1];
                        cstyle.PaddingBottom = values[2];
                    }
                }
            }
            break;

            case "padding-left":
            {
                float v = 0;
                if (TryParseLength(styleValue, out v))
                {
                    cstyle.PaddingLeft = v;
                }
            }
            break;

            case "padding-top":
            {
                float v = 0;
                if (TryParseLength(styleValue, out v))
                {
                    cstyle.PaddingTop = v;
                }
            }
            break;

            case "padding-right":
            {
                float v = 0;
                if (TryParseLength(styleValue, out v))
                {
                    cstyle.PaddingRight = v;
                }
            }
            break;

            case "padding-bottom":
            {
                float v = 0;
                if (TryParseLength(styleValue, out v))
                {
                    cstyle.PaddingBottom = v;
                }
            }
            break;
            }//switch
        }