在固定大小页面(多列)上布局目录的算法

发布于 2024-12-21 13:43:14 字数 550 浏览 2 评论 0原文

我正在将目录写入标准 8.5 英寸 x 11 英寸页面。方向(横向与纵向)是一个变量。当 x 英寸边距应用于页面时(其中 x 是可变的),我可以将目录写入内部区域。

原始数据是一个包含两列的表格:主题和页面(即 { "Animals" , 1 } , { "Big Plants" , 2 } , { "Small Plants" , 2 } )。这不是嵌套目录 - 没有“子主题”。所有主题都具有相同的重要性级别,并且所有文本的字体大小都是固定的。

我希望每页允许 1 列或多列目录,并且如果需要,我允许多页目录。布局完全取决于主题栏中的文本。如果主题很短,您可以想象将 2 列纵向放置,或将 3 列横向放置。如果主题名称很长,那么可能只适合 1 列(如果很长,那么可以为一个主题使用多行)。如果主题很多,那么我可能会扩展到多个页面。目标是在每个页面上放置尽可能多的目录信息。

我意识到这是一个难题。有许多细节我还没有探索过(即所有页面都必须具有相同的列数吗?)。我只是在寻找一个开始......一些足够简单的东西,可以在一两个小时内完成这项工作。任何半智能的东西都比强制使用带有字符计数的 1 列 TOC 来确定页面上放置多少行要好。

I'm writing a table of contents to a standard 8.5in x 11in page. Orientation (landscape versus portrait) is a variable. I can write my TOC to an inner region when x-inch margins are applied to the page (where x is variable).

The raw data is a table with two columns: Topic and Page (i.e. { "Animals" , 1 } , { "Big Plants" , 2 } , { "Small Plants" , 2 } ). This is not a nested TOC - there are no "subtopics". All topics are at the same level of importance and font size is fixed for all text.

I want to allow for 1 or more columns of TOC per page and I'm allowing multiple pages of TOC if needed. The layout is completely dependent on text in the Topic column. If the topics are short, you can imagine putting 2 columns in portrait orientation, or 3 columns in landscape orentation. If there are long topic names, then maybe only 1 column will fit (if very long, then its ok to use multiple lines for a topic). If there are many topics, then I might spill over to multiple pages. The goal is to put as much TOC info as possible on each page.

I realize this is a hard problem. There's a number of details that I haven't explored (i.e. do all pages have to have the same number of columns?). I'm just looking for a start...something simple enough to implement in an hour or two that does the job. Anything semi-intelligent is better than forcing a 1-column TOC with character counts to determine how many rows to place on a page.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小姐丶请自重 2024-12-28 13:43:14

首先,您需要一些变量:

  • Line_hight(每 TOC 行的英寸数)
  • Max_TOC_width(最长 TOC 名称的英寸数)
  • Max_pagenum_width(打印时最大页码的宽度英寸数)
  • Left、Right、Top、Bottom _border(TOC 周围边框的英寸数)页)

然后就很容易计算了。

Lines_Per_Page = Floor( (Page_Height - Top_Border - Bottom_Border) / Line_Height )

Columns_Per_Page = Floor( (Page_Width - Left_Border - Right_Border) / (Max_TOC_Width + Max_PageNum_Width) )

Total_TOC_Per_Page = Lines_Per_Page * Columns_Per_Page

仅供参考:Floor( ) 表示向下舍入到最接近的整数。楼层(5.9) = 5, 楼层(0.1) = 0

First you need a few variables:

  • Line_hight (inches per TOC line)
  • Max_TOC_width (inches of the longest TOC name)
  • Max_pagenum_width (inches width of the largest page number when printed)
  • Left, Right, Top, Bottom _border (inches of the border around the page)

Then it's pretty easy to calculated.

Lines_Per_Page = Floor( (Page_Height - Top_Border - Bottom_Border) / Line_Height )

Columns_Per_Page = Floor( (Page_Width - Left_Border - Right_Border) / (Max_TOC_Width + Max_PageNum_Width) )

Total_TOC_Per_Page = Lines_Per_Page * Columns_Per_Page

FYI: Floor( ) means round down to the nearest integer. Floor(5.9) = 5, Floor(0.1) = 0

智商已欠费 2024-12-28 13:43:14

假设您希望所有列的宽度相同:

  1. 遍历表格查找最长 TOC 条目的(打印)宽度。
  2. 将页面宽度减去边距,然后计算出可以容纳多少列。
  3. 将页面高度减去边距除以行的高度,然后计算出每列有多少行。
  4. 在其他页面方向(例如横向)重复步骤 2 和 3。
  5. 选择提供最多槽位(行乘列)的那个。

数学稍微复杂一些,因为您需要考虑列之间的“排水沟”。通过填充最宽的值可以轻松实现这一点。

Assuming you want all columns to be the same width:

  1. Do one pass through the table finding the (printed) width of the longest TOC entry.
  2. Divide the page width minus margins and take the floor to figure out how many columns you can fit.
  3. Divide the page height minus margins by the height of a line and take the floor to figure out how many lines per column.
  4. Repeat steps 2 and 3 in the other page orientation (e.g., landscape).
  5. Choose the one that gives the most slots (rows times columns).

The math is slightly more complicated, since you need to account for a "gutter" between the columns. This is easily accomplished by padding your widest value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文