Ejemplo n.º 1
0
        /// <summary>
        /// Renders the specified HTML source on the specified area clipping if specified
        /// </summary>
        /// <param name="g">Device to draw</param>
        /// <param name="html">HTML source</param>
        /// <param name="area">Area where HTML should be drawn</param>
        /// <param name="clip">If true, it will only paint on the specified area</param>
        public static void Render(IGraphics g, string html, RectangleF area, bool clip)
        {
            InitialContainer container = new InitialContainer(html);
            Region           prevClip  = g.Clip;

            if (clip)
            {
                g.SetClip(area);
            }

            /////////////////////////////////////////
            //this is new
            RectangleF htmlBox = area;

            htmlBox.Inflate(-HTML_GAP, -HTML_GAP);

            g.TranslateTransform(0, htmlBox.Y);
            /////////////////////////////////////////

            container.SetBounds(area);
            container.MeasureBounds(g);
            container.Paint(g);

            // workaround that top position is not used
            g.TranslateTransform(0, -htmlBox.Y); //NEW

            if (clip)
            {
                g.SetClip(prevClip, System.Drawing.Drawing2D.CombineMode.Replace);
            }
        }
        /// <summary>
        /// Parses the document
        /// </summary>
        private void ParseDocument()
        {
            InitialContainer root = this;
            MatchCollection  tags = Parser.Match(Parser.HtmlTag, DocumentSource);
            Box curBox            = root;
            int lastEnd           = -1;

            foreach (Match tagmatch in tags)
            {
                string text = tagmatch.Index > 0 ? DocumentSource.Substring(lastEnd + 1, tagmatch.Index - lastEnd - 1) : string.Empty;

                if (!string.IsNullOrEmpty(text.Trim()))
                {
                    AnonymousBox abox = new AnonymousBox(curBox);
                    abox.Text = text;
                }
                else if (text != null && text.Length > 0)
                {
                    CssAnonymousSpaceBox sbox = new CssAnonymousSpaceBox(curBox);
                    sbox.Text = text;
                }

                HtmlTag tag = new HtmlTag(tagmatch.Value);

                if (tag.IsClosing)
                {
                    curBox = FindParent(tag.TagName, curBox);
                }
                else if (tag.IsSingle)
                {
                    Box foo = new Box(curBox, tag);
                }
                else
                {
                    curBox = new Box(curBox, tag);
                }



                lastEnd = tagmatch.Index + tagmatch.Length - 1;
            }

            string finaltext = DocumentSource.Substring((lastEnd > 0 ? lastEnd + 1 : 0), DocumentSource.Length - lastEnd - 1 + (lastEnd == 0 ? 1 : 0));

            if (!string.IsNullOrEmpty(finaltext))
            {
                AnonymousBox abox = new AnonymousBox(curBox);
                abox.Text = finaltext;
            }
        }