[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Center string

Subject: Re: Center string
From: Frédéric Schwebel <Fred.Schwebel@xxxxxxxxxxx>
Date: Wed, 17 Jun 2009 17:46:56 +0200
Re:  Center string
Thanks for your code, Michael, I'll stick to mine, it's less compact
and less "rest-of-the-sheet" independant but it's a bit better with
balancing words between lines. Here it is (sorry most of the names are
french). The first "xsl:when" is dealing with the case where a word is
longer than the max line length.

<!-- fonction de mise en ligne des titres avant centrage -->
<xsl:function name="doc:centreTitre" as="xs:string*">
	<xsl:param name="mots" as="xs:string*" />
	<xsl:param name="longueurIdeale" as="xs:integer" />
	<xsl:param name="longueurMax" as="xs:integer" />
	<xsl:param name="dejaFait" as="xs:string?" />

	<xsl:variable name="derniereLigne" as="xs:string"
select='functx:substring-after-last($dejaFait,$sautAGenerer)' />
	<xsl:variable name="motCourant" as="xs:string"
select="translate($mots[1],$carcoup,'')" />
	<xsl:variable name="longueurIdealeCourante" as="xs:integer">
		<xsl:choose>
			<xsl:when test="($longueurIdeale + string-length($motCourant) + 1)
&lt;= $longueurMax">
				<xsl:value-of select="xs:integer($longueurIdeale +
round(string-length($motCourant) div 2))" />
			</xsl:when>
			<xsl:otherwise><xsl:value-of select="$longueurIdeale" />
			</xsl:otherwise>
		</xsl:choose>
	</xsl:variable>
	<xsl:variable name="derniereLigneAvecMot" as="xs:string"
select="concat($derniereLigne,$espace,$motCourant)" />
	<xsl:variable name="finAvecMot" as="xs:string"
select="translate(string-join($mots,$espace),$carcoup,'')" />

	<xsl:variable name="motMisEnPage" as="xs:string">
		<xsl:choose>
			<!-- cas 0 : un mot plus long que la longueur max -->
			<xsl:when test="string-length($motCourant) > $longueurMax">
				<xsl:choose>
					<!-- le mot rentre dans la longueur max, on le coupe pas -->
					<xsl:when test="string-length($motCourant) &lt;= $longueur">
						<xsl:variable name="motSansCoupe">
							<xsl:if test="string-length($derniereLigne) > 0"><xsl:value-of
select="$sautAGenerer" /></xsl:if>
							<xsl:value-of select="$motCourant" />
						</xsl:variable>
						<xsl:value-of select="string-join($motSansCoupe,'')" />
					</xsl:when>
					<!-- sinon mode sagouin et zut -->
					<xsl:otherwise>
						<xsl:variable name="motSansCoupe" select="substring($motCourant,1,
								$longueurMax -
string-length($derniereLigne)-1-xs:integer(string-length($derniereLigne)
> 0))" />
						<!-- <xsl:message select="$motSansCoupe" />
						<xsl:message select="$longueurMax -
string-length($derniereLigne)-1" /> -->
						<xsl:variable name="motCoupeSagouin">
							<xsl:if test="string-length($derniereLigne) > 0">
								<xsl:value-of select="$espace" />
							</xsl:if>
							<xsl:value-of select="$motSansCoupe" />
							<xsl:if test="string-length($motSansCoupe) > 0">
								<xsl:choose>
									<xsl:when
test="starts-with($mots[1],$debMath)"><xsl:text>&pt5;</xsl:text></xsl:when>
									<xsl:otherwise><xsl:text>&pt36;</xsl:text></xsl:otherwise>
								</xsl:choose>
							</xsl:if>
							<xsl:value-of select="$sautAGenerer" />
						</xsl:variable>
						<xsl:variable name="motCoupeSagouin2">
							<xsl:value-of select="$motCoupeSagouin" />
							<xsl:value-of
select="doc:centreTitre(substring-after($motCourant,$motSansCoupe),
								$longueurIdeale, $longueurMax,
concat(functx:trim($dejaFait),functx:trim(string-join($motCoupeSagouin,''))))
"
/>
						</xsl:variable>
						<xsl:value-of select="string-join($motCoupeSagouin2,'')" />
					</xsl:otherwise>
				</xsl:choose>
			</xsl:when>
			<!-- cas 1 : on est en dibut de ligne -->
			<xsl:when test="string-length($derniereLigne)=0">
				<xsl:value-of select="$motCourant" />
			</xsl:when>
			<!-- cas 2 : le mot rentre dans la ligne de (longueur ideale +
moitie longueur mot courant) -->
			<xsl:when test="string-length($derniereLigneAvecMot) &lt;=
$longueurIdealeCourante">
				<xsl:value-of select="concat($espace,$motCourant)" />
			</xsl:when>
			<!-- cas 3 : faut passer ` la ligne -->
			<xsl:otherwise>
				<xsl:value-of select="concat($sautAGenerer,$motCourant)" />
			</xsl:otherwise>
		</xsl:choose>
	</xsl:variable>

	<xsl:value-of select="functx:trim($motMisEnPage)" />

	<xsl:if test="(count($mots) > 1) and not($motMisEnPage =
concat($espace,$finAvecMot))">
		<xsl:value-of select="doc:centreTitre($mots[position() > 1],
$longueurIdeale,
$longueurMax,	concat(functx:trim($dejaFait),functx:trim($motMisEnPage)))"
/>
	</xsl:if>
</xsl:function>


2009/6/12 Michael Ludwig <milu71@xxxxxx>:
> Emmanuel Bigui schrieb am 12.06.2009 um 01:57:04 (+0200):
>>
>> Dealing with a minimum / maximum line length can be done with
>> regexp: they help produce, I think, more legible stylesheets
>
> Of course, regular expression! That's certainly an improvement
> over the low-level code that I produced after some battling in
> the trenches to determine how exactly to pass the parameters.
>
>> -- although it may be a question of taste.
>
> Regular expressions have a tendency to be pretty compact, but I
> wouldn't say that's a disadvantage.
>
> Michael Ludwig

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2007 All Rights Reserved.