TinyButStrong - the PHP Template Engine
Categories > OpenTBS general LibreOffice >

merging in multiple spaces

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs

By: william
Date: 2011-04-01
Time: 20:23

merging in multiple spaces

In a merge I am doing I need to merge in a variable containing multiple leading spaces but when I look at the output and the opentbs_debug_output I see that they have been collapsed into 1 space. 
How can I preserve multiple leading spaces ?  I am constructing the field to be merged in so I could use another character, if it looked like a space on the output.
By: william
Date: 2011-04-02
Time: 13:56

Re: merging in multiple spaces

I tried using non-printing ascii chars as the first char in the string and OO complained that there was a format error.
I tried using a printing character and TBS was fine with that and debug_output shows the appropriate spaces, but OO then condenses them to 1 even though I have all autocorrect options turned off.  There must be a way to coding a non-breaking space in php to build my string.
By: Skrol29
Date: 2011-04-02
Time: 23:52

Re: merging in multiple spaces

Hi Willima,

It is an XML specification to display only one space when they are several in the source. You have the same behavior in HTML.
So, in order to display several spaces, OpenOffice inserts a special entity:
<text:s text:c="3"/>
This entity displays 3 collapsed spaces in a text.

Here is an "onformat" TBS function that should replace multiple spaces in a text with the <text:s> entity
Use it like this: [my_text_field;onformat=f_frm_spaces]

The function is not tested yet.
function f_frm_spaces ($FieldName, &$CurrVal, &$CurrPrm, &$TBS) {

  // prepare
  $CurrPrm['htmlconv'] = 'no'; // disable the TBS string conversion because it would happen after inserting the XML <text:s> entity
  $TBS->meth_Conv_Str($CurrVal, true); // apply the TBS string conversion now

  // search multiple spaces
  $p = 0; // search double spaces
  while ( ($p=strpos($CurrVal, '  ', $p)) !==false) {
    $len = strlen($CurrVal);
    $nbr = 1;
    $i = $p+2;
    while ( ($i<$len) && ($CurrVal[$i]===' ') ) { // count the number of successive spaces
      $i++;
      $nbr++;
    }
    $xml = '<text:s text:c="'.$nbr.'"/>';
    $CurrVal = substr_replace($CurrVal, $xml, $p+1, $nbr);
    $p = $p + strlen($xml) - $nbr; // new position to continue the search
  }

}
By: william
Date: 2011-04-03
Time: 16:25

Re: merging in multiple spaces

works like a charm.  The last line needs to be changed to:
    $p = $p + strlen($xml) ; // new position to continue the search
because the blanks represented by $nbr have been removed, so do not need to be counted.


If you have a string like (spaces are represented by dots)
...X....q
then the 2nd replacement leaves a space after the "X", eg:
<text:s text:c="3"/>.<text:s text:c="3"/> // note the one space remaining after the X. 
This is not a problem as the single space is printed.