Re: [xsl] Calculating the size of a string

Subject: Re: [xsl] Calculating the size of a string
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Wed, 15 Aug 2012 08:15:37 -0400
At 2012-08-15 07:13 -0400, Philip Vallone wrote:
I have an xml input that will be processed into a FO file, via Saxon and Apache's FOP (1.0). The resulting string will eventually be output to a fo:table-cell. FOP does a decent job of hyphenating, however it is not perfect. If I have a string input like this:

<pnr>PN-THIS.IS.A.LONG.PN12.23.56</pnr>

The output on PDF looks like this:

PN-
THIS.IS.A.LONG.PN12.23.56

the problems is that my cell is very small. I would like to have it look like this:

PN-THIS.IS.A.
LONG.PN12.
23.56

I found a workaround, where if I include spaces in the string, I get better results:

<pnr>PN-THIS.IS.A.LONG. PN12.23. 56</pnr>

However, this requires changing the input, which I don't want to do. I can not use string-length because the size of the space can vary depending on the characters used. For example, 'WWWWW', is bigger than 'iiiii' in actual size. Both return a string-length of 5, but are not the same size. So, I've written a java extension that will return the size of the Rectangle2D width. I have an idea of where the 'line' break should be (140 the width of the framing rectangle), but I am not sure how to translate the width into a function in xslt to add the spaces.

Any ideas?

Yes, if you look on page 317 of my XSL-FO book (you can download it in full for free) at:


  http://www.CraneSoftwrights.com/training/#pfux
  (use the "Try and Buy free trial A4/US download" links)

... you'll see an example of this where I recommend to students to inject the string with Unicode zero-width spaces: &#x200b

That way if the string fits, you see no visual difference, but if the string does not fit, it breaks more likely where you have injected the zero-width spaces.

I needed to solve this problem for URIs.

Of course your space method and my zero-width-space method still presents the problem that if the user uses copy and paste from the printed material, those characters are embedded in the copied content. In my case I typically make the URI linkable so that users will be more tempted to simply click on it than use copy and paste.

I hope this helps.

. . . . . . . . . . . Ken

--
Public XSLT, XSL-FO, UBL and code list classes in Europe -- Oct 2012
Contact us for world-wide XML consulting and instructor-led training
Free 5-hour lecture: http://www.CraneSoftwrights.com/links/udemy.htm
Crane Softwrights Ltd.            http://www.CraneSoftwrights.com/s/
G. Ken Holman                   mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
Google+ profile: https://plus.google.com/116832879756988317389/about
Legal business disclaimers:    http://www.CraneSoftwrights.com/legal

Current Thread