gpt4 book ai didi

c# - 计算 html 表的 rowspan 和 colspan(算法)

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:27:31 24 4
gpt4 key购买 nike

我已经工作了大约 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

谢谢你..

最佳答案

好的..我找不到任何好的表格布局解决方案..所以我通过divs改变了布局样式..

我对所有空白区域都使用了空 div,并根据需要的空白区域给它们固定的宽度和高度。并将它们的 float 属性设置为左侧。

最后我将所有的 div、span 和图像放入一个宽度属性设置为页面宽度的容器 div..

关于c# - 计算 html 表的 rowspan 和 colspan(算法),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4476800/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com