最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【部分解决】docbook的programlisting中,单行的连续字符太长,超出pdf边界

Docbook crifan 2548浏览 0评论

【问题】

docbook中,之前已经遇到过programlisting内,单行代码太长超过边界的问题,但是已经解决掉了:

【已解决】Docbook生成PDF中,程序代码programlisting中单行代码太长,超出页面宽度,无法显示

效果如下:

本来很长的一行的代码 是可以自动换行的

但是现在遇到了新问题,个别连续的字符和个别的数字,超出边界了:

连续太长的字符:连续字符太长 超出边界

个别的数字:

个别部分内容 也超出边界

【解决过程】

1.参考Formatting listings去把配置改为:

<!-- http://www.sagehill.net/docbookxsl/FittingText.html#BreakLongLines -->
<xsl:attribute-set name="monospace.verbatim.properties">
    <xsl:attribute name="wrap-option">wrap</xsl:attribute>
    <!-- http://www.sagehill.net/docbookxsl/ProgramListings.html -->
    <xsl:attribute name="keep-together.within-column">always</xsl:attribute>
</xsl:attribute-set>

结果问题依旧。

而且,后来在:Keep-together processing instruction看到解释,说是keep-together只适用于formal,即包括:

tables, examples, figures, and equations (and their informal versions too

所以,此处的programlisting,也不起效果的,所以还是删除了上述配置。

2.又去试了添加hyphenation-character,变成:

<!-- http://www.sagehill.net/docbookxsl/FittingText.html#BreakLongLines -->
<xsl:attribute-set name="monospace.verbatim.properties">
    <xsl:attribute name="wrap-option">wrap</xsl:attribute>
    <xsl:attribute name="hyphenation-character">\</xsl:attribute>
</xsl:attribute-set>

结果是:

问题依旧,然后又试了试多添加一些关于hypernate字符的支持:

<!-- http://www.sagehill.net/docbookxsl/FittingText.html#BreakLongLines -->
<xsl:attribute-set name="monospace.verbatim.properties">
    <xsl:attribute name="wrap-option">wrap</xsl:attribute>
    <xsl:attribute name="hyphenation-character">\/&amp;?-_=</xsl:attribute>
</xsl:attribute-set>

问题依旧。

3.原先fo中的内容是:

&lt;!--============================================================================
font setting
=============================================================================--&gt;

现在尝试,把注释内容去掉:

============================================================================
font setting
=============================================================================

看看是否可以自动换行,结果问题依旧。

4.手动将fo中的hyphenate从false:

            <fo:block id="id36191341" wrap-option="wrap" hyphenation-character="\/&amp;?-_=" text-align="start" space-before.minimum="0.8em" space-before.optimum="1em" space-before.maximum="1.2em" space-after.minimum="0.8em" space-after.optimum="1em" space-after.maximum="1.2em" hyphenate="false" white-space-collapse="false" white-space-treatment="preserve" linefeed-treatment="preserve" font-family="Microsoft YaHei" background-color="Lavender" padding="1pt">
&lt;?xml version='1.0'?&gt;

&lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version='1.0'&gt;

&lt;xsl:import href="E:/DevRoot/docbook<fo:inline><fo:basic-link internal-destination="co.note.xsl_parent_path" id="co.xsl_parent_path"><fo:external-graphic content-width="7pt" width="7pt" src="url(D:/tmp/tmp_dev_root/cgwin/home/CLi/develop/docbook/config/images/system/callouts/1.svg)"/></fo:basic-link></fo:inline>/tools/<fo:inline><fo:basic-link internal-destination="co.note.slash" id="co.slash"><fo:external-graphic content-width="7pt" width="7pt" src="url(D:/tmp/tmp_dev_root/cgwin/home/CLi/develop/docbook/config/images/system/callouts/2.svg)"/></fo:basic-link></fo:inline>docbook-xsl-ns-1.77.1/html/docbook.xsl"/&gt;

&lt;!--============================================================================
font setting
=============================================================================--&gt;
......

&lt;/xsl:stylesheet&gt;
                    </fo:block>

改为true:

            <fo:block id="id36191341" wrap-option="wrap" hyphenation-character="\/&amp;?-_=" text-align="start" space-before.minimum="0.8em" space-before.optimum="1em" space-before.maximum="1.2em" space-after.minimum="0.8em" space-after.optimum="1em" space-after.maximum="1.2em" hyphenate="true" white-space-collapse="false" white-space-treatment="preserve" linefeed-treatment="preserve" font-family="Microsoft YaHei" background-color="Lavender" padding="1pt">
&lt;?xml version='1.0'?&gt;

&lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version='1.0'&gt;

&lt;xsl:import href="E:/DevRoot/docbook<fo:inline><fo:basic-link internal-destination="co.note.xsl_parent_path" id="co.xsl_parent_path"><fo:external-graphic content-width="7pt" width="7pt" src="url(D:/tmp/tmp_dev_root/cgwin/home/CLi/develop/docbook/config/images/system/callouts/1.svg)"/></fo:basic-link></fo:inline>/tools/<fo:inline><fo:basic-link internal-destination="co.note.slash" id="co.slash"><fo:external-graphic content-width="7pt" width="7pt" src="url(D:/tmp/tmp_dev_root/cgwin/home/CLi/develop/docbook/config/images/system/callouts/2.svg)"/></fo:basic-link></fo:inline>docbook-xsl-ns-1.77.1/html/docbook.xsl"/&gt;

&lt;!--============================================================================
font setting
=============================================================================--&gt;
......

&lt;/xsl:stylesheet&gt;
                    </fo:block>

结果问题依旧。

5.关于line-wrap,也找到了官网的解释:7.16.13 "wrap-option",但是此处的确已经是设置为:

wrap-option="wrap"

但是那个长串的=号,还是超出边界了。

6.另外在fo源码中看到了white-space-collapse,然后找到了FOP官网的解释:

Preformatting Content,然后找到white-space-collapse,结果发现是关于是否处理空格方面的内容的,和此处的wrap,也没啥关系,所以不去管此参数了。

7.才发现,原来之前也遇到过

“WARNING: Line 2 of a paragraph overflows the available area by more than 50 points. (See position 2711:836)”

的问题:

【基本解决】Docbook中,用fop将fo转换为pdf过程中出现警告:Line xxx of a paragraph overflows the available area by xxx millipoints. (See position xxx:xxx)

最后的workaround,是插入零宽度字符来临时解决的,也是没根本性的解决此问题。

8.参考:

FOP warning:line 1 of a paragraph overflows the available area by more than 50 points

也只是找到了官网中关于7.21.2 "overflow"等的解释,还是无助于解决此问题。

9.看到I tried it with FOP Trunk,所以去尝试一下,下载truck的fop试试,看看是否能解决此问题。

Apache FOP: Nightly Snapshots中下载了:fop-20120610-bin.tar.gz ,然后用最新的fop去编译,结果是问题依旧,而且还额外出现了很多很多的waring,所以,此法放弃。

10.后来手动给那个一长串的连续的等号中间添加了一些空格:

&lt;!--========= =============== ============ ==== =========== ======== ====== ===========
font setting
===== === ========= ======= ===== ======= ======== ====== ============ ===== ==========--&gt;
&lt;xsl:param name="title.font.family"&gt;Microsoft YaHei&lt;/xsl:param&gt;
&lt;xsl:param name="body.font.family"&gt;Microsoft YaHei&lt;/xsl:param&gt;
&lt;xsl:param name="monospace.font.family"&gt;Microsoft YaHei&lt;/xsl:param&gt;
&lt;xsl:param name="symbol.font.family"&gt;Cambria Math&lt;/xsl:param&gt;

希望可以这样能fop能检测到空格,可以自动换行,

然后编译出来的pdf的效果,真的是可以实现自动换行的了:

手动添加了空格后 就可以自动换行了

所以,说明那个wrap是根据中间有空格,实现wrap换行的。

突然也明白了,之前那个url中,url地址很长,中间也是没有空格,而此处的连续的等号,也是中间没有空格,所以都无法实现自动wrap换行,导致超出边界。

对应的换行控制,好像是通过hypernate去实现的。

11.后来找到了这里:

6.8. Hyphenation

参考其解释,去试试在fo:block中,hyphenate="true"之后,添加了hyphenation-character="=-"

结果生成的效果,连加了空格的那两行等号,都不能自动wrap了…

12.后来试了各种组合:

language="en"

hyphenate="true"

hyphenation-character="="

hyphenation-push-character-count="8"

等等,结果都还是没有效果,等号还是会超出边界.

13.截止目前,唯一的workaround方面就是,

手动在某个位置,添加零宽度字符:

&#x200B;

这样就可以自动换行了。

否则都还是会超出边界。

【后记1】

关于上述的hyphenation-character,后来看到:

Re: [docbook-apps] hyphenation-character not inserted when there are line break

Re: [docbook-apps] hyphenation-character not inserted when thereare line break

中看到解释说是fop不支持插入此hyphenation-character字符,而xep等其他工具,倒是支持的。

所以,我此处用xsltproc+fop,生成pdf中,也的确没有看到之前所设置的backslash字符,即’\’。

所以,现在突然想起来了,不知道,是不是由于本身fop的问题,导致虽然设置都正确了,但是结果还是不对,连续的等号,还是无法换行,搞不好是fop本身的原因。

看来以后有机会是需要折腾折腾saxon,xep等工具了,或许,可以解决此问题。

【后记2】

刚看到,这人也遇到和我一样的问题:

Subject: Break long programlisting lines without spaces?

但是看了该贴的讨论和其他一些帖子:

Re: Inserting line-break symbols?

目前的结论就是:

除了自己去看代码搞懂后,让xsl去添加soft-hypen之外,貌似真的没有办法让fop支持此种情况的自动换行了。

这么常见的问题,都没有一个(除了手动添加之外的)自动处理掉此问题的解决办法。真是垃圾啊。。。太阳啊!!!

转载请注明:在路上 » 【部分解决】docbook的programlisting中,单行的连续字符太长,超出pdf边界

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.156 seconds, using 22.38MB memory