2016-04-23 26 views
0

我想在使用AH格式化程序生成的PDF中的字符串長度爲14個字符後強制換行。因此,這是我的xsl代碼,沒有任何換行嘗試:在字符串長度後強制換行

<xsl:attribute-set name="big" use-attribute-sets="bold"> 
    <xsl:attribute name="font-size">38pt</xsl:attribute> 
    <xsl:attribute name="line-height">28.84pt</xsl:attribute> 
    <xsl:attribute name="text-align">center</xsl:attribute> 
    <xsl:attribute name="letter-spacing">1mm</xsl:attribute> 
</xsl:attribute-set> 

<xsl:attribute-set name="small" use-attribute-sets="bold"> 
    <xsl:attribute name="font-size">27pt</xsl:attribute> 
    <xsl:attribute name="line-height">27pt</xsl:attribute> 
    <xsl:attribute name="text-align">center</xsl:attribute> 
    <xsl:attribute name="letter-spacing">1mm</xsl:attribute> 
</xsl:attribute-set> 

<xsl:choose> 
    <xsl:when test="string-length($count_cover)>=14"> 
     <fo:block xsl:use-attribute-sets="small"> 
     <xsl:apply-templates/> 
     </fo:block> 
    </xsl:when> 
    <xsl:otherwise>   
     <fo:block xsl:use-attribute-sets="big"> 
     <xsl:apply-templates/> 
     </fo:block> 
    </xsl:otherwise> 
</xsl:choose> 

是否可以強制使用XSL-FO換行?

回答

1

如果標題可以轉換爲字符串,則可以插入<fo:block/>作爲換行符。

<xsl:variable name="cover_title" as="xs:string" select="'Very Long Cover Title! Very Long Cover Title! Very Long Cover Title! '"/> 
<xsl:variable name="count_cover" as="xs:integer" select="string-length($cover_title)"/> 
<xsl:variable name="lf_position" as="xs:integer" select="14"/> 

<xsl:template match="/"> 
    <xsl:choose> 
     <xsl:when test="$count_cover gt $lf_position"> 
      <fo:block xsl:use-attribute-sets="small"> 
       <xsl:analyze-string select="$cover_title" regex=".{{1}}"> 
        <xsl:matching-substring> 
         <xsl:value-of select="."/> 
         <xsl:if test="position() eq $lf_position"> 
          <fo:block/> 
         </xsl:if> 
        </xsl:matching-substring> 
        <xsl:non-matching-substring/> 
       </xsl:analyze-string> 
      </fo:block> 
     </xsl:when> 
     <xsl:otherwise> 
      <fo:block xsl:use-attribute-sets="big"> 
       <xsl:value-of select="$cover_title"/> 
      </fo:block> 
     </xsl:otherwise> 
    </xsl:choose> 
</xsl:template> 

其結果是:

<fo:block font-weight="bold" font-size="27pt" line-height="27pt" text-align="center" letter-spacing="1mm">Very Long Cove<fo:block/>r Title! Very Long Cover Title! Very Long Cover Title! </fo:block> 

然而,該方法忽略字邊界和連字控制。如果您打算製作封面標題,最好使用fo:block-container來引入AH Formatter擴展。

  1. 在封面頁中使用fo:block-container作爲標題的固定位置和大小。
  2. 設定屬性@溢出=「凝結」與@axf:溢流冷凝=」字體大小」 https://www.antennahouse.com/product/ahf60/docs/ahf-ext.html#axf.overflow-condense
  3. 的FO內部:嵌段 - 容器,地點FO:塊,其存儲內容標題
  4. 就可以得到所希望的結果,因爲AH格式化根據內容音量自動調整字體大小。

[實施例]

<fo:block-container position="absolute" top="..." left="..." width="..." height="..." overflow="condense" axf:overflow-condense="font-size" font-size="27pt" text-align="center"> 
    <fo:block> 
     <fo:inline>Very Long Cover Title! Very Long Cover Title! Very Long Cover Title!</fo:inline> 
    </fo:block> 
</fo:block-container> 
0

您不能在FO中強制換行,但可以將字符串拆分爲單獨的FO塊。

<xsl:choose> 
    <xsl:when test="string-length($count_cover) &gt;= 14"> 
    <fo:block><xsl:value-of select="substring($count_cover, 1, 13)"/></fo:block> 
    <fo:block><xsl:value-of select="substring($count_cover, 14)"/></fo:block> 
    </when> 
    <xsl:otherwise> 
    <fo:block> 
     <xsl:value-of select="$count_cover"/> 
    </fo:block> 
    </xsl:otherwise> 
</xsl:choose> 
1
  1. 如果您試圖打破單詞(而不是例如零件號碼),那麼啓用連字符可能會比打破固定數量的字符之後獲得更好的結果。

  2. 您可以使用linefeed-treatment="preserve"並插入&#xA;而不是fo:block,作爲Inserting a line break in a PDF generated from XSL FO using <xsl:value-of>筆記的回答。您可以與<xsl:value-of select="replace(., '(.{14})', '$1&#xA;')" />

  3. 做可以代替插入一個零寬度的空間,&#x200B;,每14字符後,讓在零寬度空間AH格式化突破:

    <xsl:template match="text()"> <xsl:value-of select="replace(replace(., '(\P{Zs}{14})', '$1&#x200B;'), '&#x200B;(\p{Zs})', '$1')" /> </xsl:template>

    replace()在每14個非空格字符之後插入字符,如果第15個字符是空格字符,則外部replace()將修復該字符。

    如果您使用的是比例寬度的字體,則某些14個字符的序列(不包括例如14個固定寬度的襯裏編號)的寬度將比其他寬度更多或更少,因此您可能需要在&#x200B;之間插入更多字符,以便AH格式化程序可以在破解之前盡最大努力填充該行。

  4. 即使在單詞中也可以使用axf:word-break="break-all"啓用換行。見https://www.antennahouse.com/product/ahf63/ahf-ext.html#axf.word-break