fbpx
维基百科

自动换行

显示文字時, 换行(line wrap)是指文字在一行已满後转到新行,使每行都可在視窗范围看到,不需水平滚动。

自动换行(word wrap)是大多数文字編輯器文書處理器、和网页浏览器的附加功能。用于在行间或一行里的单词间隔处分行,不考虑單一单词超过一行长度的情况。

它通常是在看文档或打印的时候实时完成,所以没有储存或人手插入换行代码[來源請求]。如果改变文档边缘,编辑器就会自动重排换行符的位置,保证全部文字都处于可见状态,或者给打字员提供一些便捷的方式重排换行符。

软回车是由于自动换行形成的回车;硬回车则用于另起一段。

单词边界、断字和硬空格 编辑

软回车通常放在完整单词后面,或者紧接完整单词的标点符号后,但也可能在连字号后。

连字号后可用非断字连字号代替一般连字号避免自动换行,也可在单词中插入隱形连字号(软连字号)使文字处理软件在该处自动换行。

单词间可用硬空格避免自动换行。

中日韩文字的自动换行 编辑

中文日文韩文中,汉字常认为是一个词,因此自动换行可以在汉字之前或之后发生。

而在某些情况下,是不需换行。例如:

  • 人名处不宜换行
  • 复合词处不宜换行

大多数现有的文字处理和排版软件不能处理上述任何情况。

中日韩标点符号可能不遵守上述特殊情况,这些规则常称为避頭尾日语禁則処理(意思是“禁止规则处理”)。

避頭尾英语Line breaking rules in East Asian languages有一个总是成立的特例:自动换行永远不能在中日韩越统一表意文字符集中的破折号省略号之间发生。虽然由于现存字符编码的关系,这些符号需要用两个字符表示,但这些字符实际上是一个两字符宽的符号,而不是两个单字宽的符号对。

算法 编辑

自动换行是最优化问题。因应不同的需求,有不同的解决办法。

最小长度 编辑

自动换行可用贪心算法简单实现:尽可能将单词放进一行,直到所有单词都放进去。这是很多现代文字处理软件的做法,如Microsoft WordOpen Office 。这算法在追求最少行数的目标上能够达到最优化。下面是伪代码:

SpaceLeft := LineWidth for each Word in Text if Width(Word) > SpaceLeft insert line break before Word in Text SpaceLeft := LineWidth - Width(Word) else SpaceLeft := SpaceLeft - (Width(Word) + SpaceWidth) 

LineWidth指行宽,SpaceLeft指一行中剩余的空格,SpaceWidth是空格宽度,Text是文字,Word是文字中的单词。

最小破损度 编辑

TeX用的则是另一條算法,旨在将行尾空格数的平方和最小化,使结果更美观。以上算法不能完成这目标,如:

aaa bb cc ddddd

如果惩罚函数定义为行尾剩余空格数的平方,则贪婪算法会得到一个次优解(为了简化起见,不妨假设采用定宽字体):

------ 一行的宽度为6 aaa bb 剩余的空格数:0,平方=0 cc 剩余的空格数:4,平方=16 ddddd 剩余的空格数:1,平方=1 

总计代价17,而最佳的解决方案是这样的:

------ 一行的宽度为6 aaa 剩余空格数:3 平方=9 bb cc 剩余空格数:1 平方=1 ddddd 剩余空格数:1 平方=1 


请注意,第一行在bb前断开了,相对于在bb后断开的解法,可以得到更好的右边界和更低的代价11。

解决这问题需定义惩罚函数 ,用于计算包含单词 到单词 的一行的代价:

 

其中 通常为  。另外,有一些特殊的情况值得考虑:如果结果为负(即单词串不能全部放在一行里),惩罚函数需要反映跟踪或压缩文字以适应一行的代价;如果这是不可能的,则返回 

最优解的代价可以用以下的递归式定义:

 

这可以利用动态规划来高效地实现,时间和空间复杂度均为 

参见 编辑

外部链接 编辑

Knuth's algorithm 编辑

  • "Knuth & Plass line-breaking Revisited" (页面存档备份,存于互联网档案馆
  • "tex_wrap": "Implements TeX's algorithm for breaking paragraphs into lines." (页面存档备份,存于互联网档案馆) Reference: "Breaking Paragraphs into Lines", D.E. Knuth and M.F. Plass, chapter 3 of _Digital Typography_, CSLI Lecture Notes #78.
  • Text::Reflow - Perl module for reflowing text files using Knuth's paragraphing algorithm. "The reflow algorithm tries to keep the lines the same length but also tries to break at punctuation, and avoid breaking within a proper name or after certain connectives ("a", "the", etc.). The result is a file with a more "ragged" right margin than is produced by fmt or Text::Wrap but it is easier to read since fewer phrases are broken across line breaks."
  • to recognize the "soft hyphen".
  • Knuth's breaking algorithm. (页面存档备份,存于互联网档案馆) "The detailed description of the model and the algorithm can be found on the paper "Breaking Paragraphs into Lines" by Donald E. Knuth, published in the book "Digital Typography" (Stanford, California: Center for the Study of Language and Information, 1999), (CSLI Lecture Notes, no. 78.)" ; part of
  • "Bridging the Algorithm Gap: A Linear-time Functional Program for Paragraph Formatting" (页面存档备份,存于互联网档案馆) by Oege de Moor, Jeremy Gibbons, 1999

其他自动换行有关的连接 编辑

  • the reverse problem -- picking columns just wide enough to fit (wrapped) text Archive.is的存檔,存档日期2007-09-27
  • KWordWrap Class Reference (页面存档备份,存于互联网档案馆) used in the KDE GUI
  • by Simon Pepping 2006. Extends the Knuth model to handle a few enhancements.
  • "Page breaking strategies" (页面存档备份,存于互联网档案馆) Extends the Knuth model to handle a few enhancements.
  • "a Knuth-Plass-like linebreaking algorithm ... The *really* interesting thing is how Adobe's algorithm differs from the Knuth-Plass algorithm. It must differ, since Adobe has managed to patent its algorithm (6,510,441)."[1]
  • "Murray Sargent: Math in Office" (页面存档备份,存于互联网档案馆

自动换行, 此條目需要精通或熟悉相关主题的编者参与及协助编辑, 2011年1月16日, 請邀請適合的人士改善本条目, 更多的細節與詳情請參见討論頁, 此條目需要編修, 以確保文法, 用詞, 语气, 格式, 標點等使用恰当, 2010年4月22日, 請按照校對指引, 幫助编辑這個條目, 幫助, 討論, 显示文字時, 换行, line, wrap, 是指文字在一行已满後转到新行, 使每行都可在視窗范围看到, 不需水平滚动, word, wrap, 是大多数文字編輯器, 文書處理器, 和网页浏览器的附加功能, 用于在行间. 此條目需要精通或熟悉相关主题的编者参与及协助编辑 2011年1月16日 請邀請適合的人士改善本条目 更多的細節與詳情請參见討論頁 此條目需要編修 以確保文法 用詞 语气 格式 標點等使用恰当 2010年4月22日 請按照校對指引 幫助编辑這個條目 幫助 討論 显示文字時 换行 line wrap 是指文字在一行已满後转到新行 使每行都可在視窗范围看到 不需水平滚动 自动换行 word wrap 是大多数文字編輯器 文書處理器 和网页浏览器的附加功能 用于在行间或一行里的单词间隔处分行 不考虑單一单词超过一行长度的情况 它通常是在看文档或打印的时候实时完成 所以没有储存或人手插入换行代码 來源請求 如果改变文档边缘 编辑器就会自动重排换行符的位置 保证全部文字都处于可见状态 或者给打字员提供一些便捷的方式重排换行符 软回车是由于自动换行形成的回车 硬回车则用于另起一段 目录 1 单词边界 断字和硬空格 2 中日韩文字的自动换行 3 算法 3 1 最小长度 3 2 最小破损度 4 参见 5 外部链接 5 1 Knuth s algorithm 5 2 其他自动换行有关的连接单词边界 断字和硬空格 编辑软回车通常放在完整单词后面 或者紧接完整单词的标点符号后 但也可能在连字号后 连字号后可用非断字连字号代替一般连字号避免自动换行 也可在单词中插入隱形连字号 软连字号 使文字处理软件在该处自动换行 单词间可用硬空格避免自动换行 中日韩文字的自动换行 编辑在中文 日文和韩文中 汉字常认为是一个词 因此自动换行可以在汉字之前或之后发生 而在某些情况下 是不需换行 例如 人名处不宜换行 复合词处不宜换行大多数现有的文字处理和排版软件不能处理上述任何情况 中日韩标点符号可能不遵守上述特殊情况 这些规则常称为避頭尾 日语 禁則処理 意思是 禁止规则处理 避頭尾 英语 Line breaking rules in East Asian languages 有一个总是成立的特例 自动换行永远不能在中日韩越统一表意文字符集中的破折号和省略号之间发生 虽然由于现存字符编码的关系 这些符号需要用两个字符表示 但这些字符实际上是一个两字符宽的符号 而不是两个单字宽的符号对 算法 编辑自动换行是最优化问题 因应不同的需求 有不同的解决办法 最小长度 编辑 自动换行可用贪心算法简单实现 尽可能将单词放进一行 直到所有单词都放进去 这是很多现代文字处理软件的做法 如Microsoft Word和Open Office 这算法在追求最少行数的目标上能够达到最优化 下面是伪代码 SpaceLeft LineWidth for each Word in Text if Width Word gt SpaceLeft insert line break before Word in Text SpaceLeft LineWidth Width Word else SpaceLeft SpaceLeft Width Word SpaceWidth LineWidth指行宽 SpaceLeft指一行中剩余的空格 SpaceWidth是空格宽度 Text是文字 Word是文字中的单词 最小破损度 编辑 TeX用的则是另一條算法 旨在将行尾空格数的平方和最小化 使结果更美观 以上算法不能完成这目标 如 aaa bb cc ddddd如果惩罚函数定义为行尾剩余空格数的平方 则贪婪算法会得到一个次优解 为了简化起见 不妨假设采用定宽字体 一行的宽度为6 aaa bb 剩余的空格数 0 平方 0 cc 剩余的空格数 4 平方 16 ddddd 剩余的空格数 1 平方 1 总计代价17 而最佳的解决方案是这样的 一行的宽度为6 aaa 剩余空格数 3 平方 9 bb cc 剩余空格数 1 平方 1 ddddd 剩余空格数 1 平方 1 请注意 第一行在bb前断开了 相对于在bb后断开的解法 可以得到更好的右边界和更低的代价11 解决这问题需定义惩罚函数c i j displaystyle c i j nbsp 用于计算包含单词Word i displaystyle text Word i nbsp 到单词Word j displaystyle text Word j nbsp 的一行的代价 c i j LineWidth j i OneSpaceWidth k i j WidthOf Word k P displaystyle c i j left text LineWidth j i cdot text OneSpaceWidth sum k i j text WidthOf text Word k right P nbsp 其中P displaystyle P nbsp 通常为2 displaystyle 2 nbsp 或3 displaystyle 3 nbsp 另外 有一些特殊的情况值得考虑 如果结果为负 即单词串不能全部放在一行里 惩罚函数需要反映跟踪或压缩文字以适应一行的代价 如果这是不可能的 则返回 displaystyle infty nbsp 最优解的代价可以用以下的递归式定义 f j c 1 j if c 1 j lt min 1 k lt j f k c k 1 j if c 1 j displaystyle f j begin cases c 1 j amp text if c 1 j lt infty displaystyle min 1 leq k lt j big f k c k 1 j big amp text if c 1 j infty end cases nbsp 这可以利用动态规划来高效地实现 时间和空间复杂度均为O j 2 displaystyle O j 2 nbsp 参见 编辑分词 零宽度非换行空格外部链接 编辑Knuth s algorithm 编辑 Knuth amp Plass line breaking Revisited 页面存档备份 存于互联网档案馆 tex wrap Implements TeX s algorithm for breaking paragraphs into lines 页面存档备份 存于互联网档案馆 Reference Breaking Paragraphs into Lines D E Knuth and M F Plass chapter 3 of Digital Typography CSLI Lecture Notes 78 Text Reflow Perl module for reflowing text files using Knuth s paragraphing algorithm The reflow algorithm tries to keep the lines the same length but also tries to break at punctuation and avoid breaking within a proper name or after certain connectives a the etc The result is a file with a more ragged right margin than is produced by fmt or Text Wrap but it is easier to read since fewer phrases are broken across line breaks adjusting the Knuth algorithm to recognize the soft hyphen Knuth s breaking algorithm 页面存档备份 存于互联网档案馆 The detailed description of the model and the algorithm can be found on the paper Breaking Paragraphs into Lines by Donald E Knuth published in the book Digital Typography Stanford California Center for the Study of Language and Information 1999 CSLI Lecture Notes no 78 part of Google Summer Of Code 2006 Bridging the Algorithm Gap A Linear time Functional Program for Paragraph Formatting 页面存档备份 存于互联网档案馆 by Oege de Moor Jeremy Gibbons 1999其他自动换行有关的连接 编辑 the reverse problem picking columns just wide enough to fit wrapped text Archive is的存檔 存档日期2007 09 27 KWordWrap Class Reference 页面存档备份 存于互联网档案馆 used in the KDE GUI Knuth linebreaking elements for Formatting Objects by Simon Pepping 2006 Extends the Knuth model to handle a few enhancements Page breaking strategies 页面存档备份 存于互联网档案馆 Extends the Knuth model to handle a few enhancements a Knuth Plass like linebreaking algorithm The really interesting thing is how Adobe s algorithm differs from the Knuth Plass algorithm It must differ since Adobe has managed to patent its algorithm 6 510 441 1 Murray Sargent Math in Office 页面存档备份 存于互联网档案馆 取自 https zh wikipedia org w index php title 自动换行 amp oldid 78992837, 维基百科,wiki,书籍,书籍,图书馆,

文章

,阅读,下载,免费,免费下载,mp3,视频,mp4,3gp, jpg,jpeg,gif,png,图片,音乐,歌曲,电影,书籍,游戏,游戏。