计算html表的rowspan和colspan(算法)

时间:2010-12-18 05:26:31

标签: c# algorithm html-table

我已经工作了大约18个小时而且我变成了僵尸..如果没有你的帮助我无法弄清楚这个问题..

项目

我正在开发一个pdf到epub转换器应用程序,它将pdf文件转换为epub格式。

问题

由于EPub限制,EPub xhtml不允许绝对定位。所以,我决定对页面上的所有元素(文本和图像)使用表格布局(html)。

但我无法想象如何计算细胞的rowspan和colspan值。

我做了什么

    class TableItem
    {
        public IRenderable Item;
        public Rectangle Bounds;
        public int RowSpan, ColSpan;
        public TableItem(Rectangle bounds, IRenderable item)
        {
            this.Bounds = bounds;
            this.Item = item;
        }
    }

    class Table
    {
        Rectangle bounds;
        List<TableItem> items;

        public Table(int width, int height)
        {
            bounds = new Rectangle(0, 0, width, height);
            items = new List<TableItem>();
        }

        public void Add(IRenderable item)
        {
            Rectangle rect = Rectangle.Intersect(bounds, item.GetBounds());
            if (rect.IsEmpty)
                return;
            items.Add(new TableItem(rect, item));
        }

        List<TableItem> Slice(List<TableItem> list, int startIndex, int endIndex)
        {
            TableItem[] tmp = new TableItem[endIndex - startIndex];
            list.CopyTo(startIndex, tmp, 0, tmp.Length);
            return new List<TableItem>(tmp);
        }

        List<TableItem>[] Analyze(out int maxCol)
        {
            maxCol = 0;
            if (items.Count < 1)
                return new List<TableItem>[0];

            /*
            items.Sort
                (
                    delegate(TableItem x, TableItem y)
                    {
                        if (x.Bounds.Y == y.Bounds.Y)
                            return x.Bounds.X - y.Bounds.X;
                        return x.Bounds.Y - y.Bounds.Y;
                    }
                );
             */ 

            List<Rectangle> freeAreas = new List<Rectangle>();
            freeAreas.Add(bounds);

            foreach (TableItem item in items)
            {
                for (int i = 0; i < freeAreas.Count; i++)
                {
                    Rectangle area;
                    if ((area = freeAreas[i]).Contains(item.Bounds))
                    {
                        freeAreas.RemoveAt(i);
                        freeAreas.InsertRange(i, SplitRect(area, item.Bounds));
                    }
                }
            }

            foreach (Rectangle rt in freeAreas)
                items.Add(new TableItem(rt, null));

            items.Sort
                (
                    delegate(TableItem x, TableItem y)
                    {
                        if (x.Bounds.Y == y.Bounds.Y)
                            return x.Bounds.X - y.Bounds.X;
                        return x.Bounds.Y - y.Bounds.Y;
                    }
                );

            List<List<TableItem>> xlist = new List<List<TableItem>>();
            int lasty = items[0].Bounds.Y;
            int startIndex = 0;
            for(int i=0;i<items.Count;i++)
                if (lasty < items[i].Bounds.Y)
                {
                    lasty = items[i].Bounds.Y;
                    List<TableItem> xl = Slice(items, startIndex, i);
                    xlist.Add(xl);
                    startIndex = i;
                    if (maxCol < xl.Count)
                        maxCol = xl.Count;
                }

            if (startIndex < items.Count - 1)
            {
                xlist.Add(Slice(items, startIndex, items.Count));
                int t = xlist[xlist.Count - 1].Count;
                if (maxCol < t)
                    t = maxCol;
            }

            return xlist.ToArray();
        }

        private Rectangle[] SplitRect(Rectangle a, Rectangle r)
        {
            if (a == r)
                return new Rectangle[0];

            if (!Rectangle.Inflate(a, -2, -2).Contains(r))
                return new Rectangle[0];

            #region MyRegion
            if (r.X == a.X)
            {
                if (r.Height == a.Height)
                {
                    #region MyRegion
                    return new Rectangle[]
                    {
                        new Rectangle(r.Right, a.Y, a.Width - r.Width, a.Height)
                    };
                    #endregion
                }
                else if (r.Y == a.Y)
                {
                    #region MyRegion
                    if (r.Width == a.Width)
                    {
                        return new Rectangle[]
                        {
                            new Rectangle(a.X, r.Bottom, a.Width, a.Height - r.Height)
                        };
                    }
                    return new Rectangle[]
                    {
                        new Rectangle(r.Right, a.Y, a.Width - r.Width, r.Height),
                        new Rectangle(a.X, r.Bottom, a.Width, a.Height - r.Height)
                    };
                    #endregion
                }
                else if (r.Bottom == a.Bottom)
                {
                    #region MyRegion
                    if (r.Width == a.Width)
                    {
                        return new Rectangle[]
                        {
                            new Rectangle(a.X, a.Y, a.Width, r.Y - a.Y)
                        };
                    }
                    return new Rectangle[]
                    {
                        new Rectangle(a.X, a.Y, a.Width, r.Y - a.Y),
                        new Rectangle(r.Right, r.Y, a.Width - r.Width, r.Height)
                    };
                    #endregion
                }
                else
                {
                    #region MyRegion
                    if (a.Width == r.Width)
                    {
                        return new Rectangle[]
                        {
                            new Rectangle(a.X, a.Y, a.Width, a.Height - (r.Y - a.Y)),
                            new Rectangle(a.X, r.Bottom, a.Width, a.Height - (r.Bottom - a.Y))
                        };
                    }
                    return new Rectangle[]
                    {
                        new Rectangle(a.X, a.Y, a.Width, a.Height - (r.Y - a.Y)),
                        new Rectangle(r.Right, r.Y, a.Width - r.Right, r.Height),
                        new Rectangle(a.X, r.Bottom, a.Width, a.Height - (r.Bottom - a.Y))
                    };
                    #endregion
                }
            }
            else if (r.Y == a.Y)
            {
                if (r.Height == a.Height)
                {
                    #region MyRegion
                    if (r.Right == a.Right)
                    {
                        return new Rectangle[]
                        {
                            new Rectangle(a.X, a.Y, a.Width - r.Width, a.Height)
                        };
                    }
                    return new Rectangle[]
                    {
                        new Rectangle(a.X, a.Y, r.X - a.X, a.Height),
                        new Rectangle(r.Right, a.Y, a.Right - r.Right, a.Height)
                    };
                    #endregion
                }
                else
                {
                    #region MyRegion
                    if (r.Right == a.Right)
                    {
                        return new Rectangle[]
                        {
                            new Rectangle(a.X, a.Y, a.Width, r.Y - a.Y),
                            new Rectangle(a.X, r.Y, r.X - a.X, r.Height),
                            new Rectangle(a.X, r.Bottom, a.Width, a.Bottom - r.Bottom)
                        };
                    }
                    return new Rectangle[]
                    {
                        new Rectangle(a.X, a.Y, r.X - a.X, r.Height),
                        Rectangle.FromLTRB(r.Right, a.Y, a.Right, r.Bottom),
                        Rectangle.FromLTRB(a.X, r.Bottom, a.Right, a.Bottom)
                    };
                    #endregion
                }
            }
            else if (r.Right == a.Right)
            {
                #region MyRegion
                if (a.Bottom == r.Bottom)
                {
                    return new Rectangle[]
                    {
                        Rectangle.FromLTRB(a.X, a.Y, a.Right, r.Y),
                        Rectangle.FromLTRB(a.X, r.Y, r.X, r.Bottom)
                    };
                }
                return new Rectangle[]
                {
                    Rectangle.FromLTRB(a.X, a.Y, a.Right, r.Y),
                    Rectangle.FromLTRB(a.X, r.Y, r.X, r.Bottom),
                    Rectangle.FromLTRB(a.X, r.Bottom, a.Right, a.Bottom)
                };
                #endregion
            }
            else if (r.Bottom == a.Bottom)
            {
                #region MyRegion
                return new Rectangle[]
                {
                    Rectangle.FromLTRB(a.X, a.Y, a.Right, r.Y),
                    Rectangle.FromLTRB(a.X, r.Y, r.X, r.Bottom),
                    Rectangle.FromLTRB(r.Right, r.Y, a.Right, a.Bottom)
                };
                #endregion
            } 
            #endregion

            return new Rectangle[]
            {
                Rectangle.FromLTRB(a.X, a.Y, a.Right, r.Y),
                Rectangle.FromLTRB(a.X, r.Y, r.X, r.Bottom),
                Rectangle.FromLTRB(r.Right, r.Y, a.Right, r.Bottom),
                Rectangle.FromLTRB(a.X, r.Bottom, a.Right, a.Bottom)
            };
        }

        public void Compose(EPubWriter writer)
        {
            int mc;
            List<TableItem>[] all = Analyze(out mc);
            if (all.Length < 1)
                return;
            foreach (List<TableItem> item in all)
            {
                writer.BeginRow();

                for (int i = 0; i < item.Count; i++)
                {
                    TableItem ti = item[i];

                    ti.RowSpan = 0; //???? Should Calculate
                    ti.ColSpan = 0; //???? Should Calculate

                    writer.Render(ti.Item, ti.RowSpan, ti.ColSpan);
                }

                writer.EndRow();
            }
        }
    }

我需要什么

    public void Compose(EPubWriter writer)
    {
        int mc;
        // Analyze: Calculates item positions, creates
        // empty TableItem s if needs, sorts by y ASC,x ASC
        // Returns array of rows (TR) what contains cells (TD)
        List<TableItem>[] all = Analyze(out mc);
        if (all.Length < 1)
            return;
        foreach (List<TableItem> item in all)
        {
            writer.BeginRow();

            for (int i = 0; i < item.Count; i++)
            {
                TableItem ti = item[i];

                ti.RowSpan = 0; //???? Should Calculate
                ti.ColSpan = 0; //???? Should Calculate

                writer.Render(ti.Item, ti.RowSpan, ti.ColSpan);
            }

            writer.EndRow();
        }
    }
                ti.RowSpan = 0; //???? Should Calculate
                ti.ColSpan = 0; //???? Should Calculate

谢谢..

1 个答案:

答案 0 :(得分:0)

好的..我找不到表布局的任何好解决方案..所以我用div改变了布局样式..

我为所有空白区域使用了空div,并为它们提供了固定的宽度和高度以及所需的空白区域..并将其float属性设置为left。

最后我将所有div,spans和图像放入容器div中,其宽度属性设置为页面宽度..