﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<?application progid="SMath Studio Desktop" version="1.2.9018.0"?>
<worksheet xmlns="http://smath.info/schemas/worksheet/1.0">
  <settings ppi="96">
    <identity>
      <id>8847c54e-2e0f-4b71-bd90-dd857cf897f1</id>
      <revision>242</revision>
    </identity>
    <metadata lang="eng">
      <author>francesco rapuano</author>
    </metadata>
    <calculation>
      <precision>4</precision>
      <exponentialThreshold>5</exponentialThreshold>
      <trailingZeros>false</trailingZeros>
      <significantDigitsMode>false</significantDigitsMode>
      <mixedNumbers>false</mixedNumbers>
      <roundingMode>0</roundingMode>
      <approximateEqualAccuracy>0</approximateEqualAccuracy>
      <fractions>decimal</fractions>
    </calculation>
    <pageModel active="false" viewMode="2" printGrid="false" printAreas="true" simpleEqualsOnly="false" printBackgroundImages="true" hideElementsHighlightings="false">
      <paper id="9" orientation="Portrait" width="827" height="1169" />
      <margins left="15" right="19" top="19" bottom="19" />
      <header alignment="Center" color="#a9a9a9">&amp;[DATE] &amp;[TIME] - &amp;[FILENAME]</header>
      <footer alignment="Center" color="#a9a9a9">&amp;[PAGENUM] / &amp;[COUNT]</footer>
      <backgrounds />
    </pageModel>
    <dependencies>
      <assembly name="SMath Studio Desktop" version="1.2.9018.0" guid="a37cba83-b69c-4c71-9992-55ff666763bd" />
      <assembly name="MathRegion" version="1.11.9018.0" guid="02f1ab51-215b-466e-a74d-5d8b1cf85e8d" />
      <assembly name="Custom Functions" version="1.1.8726.29023" guid="18dadffd-79a3-4cf9-aee1-d66deb0ea720" />
      <assembly name="SpecialFunctions" version="1.12.9018.0" guid="2814e667-4e12-48b1-8d51-194e480eabc5" />
      <assembly name="TextRegion" version="1.11.9018.0" guid="485d28c5-349a-48b6-93be-12a35a1c1e39" />
    </dependencies>
  </settings>
  <regions type="content">
    <region left="0" top="0" width="791" height="113" color="#000000" fontSize="10">
      <text lang="eng" width="791" fontFamily="Arial" fontSize="10">
        <content>
          <p>Word Wrapping routine <br /><br />Purpose of the user defined function  :  split  an input  text string in rows of a defined length<br />Inputs: ts -  string of text to wrap <br />            tw - maximum length of each row of wrapped  text <br /><br />Output: an array of row(s) of text wrapped at the maximum length tw</p>
        </content>
      </text>
    </region>
    <region left="0" top="126" width="771" height="554" color="#000000" fontSize="10">
      <math>
        <input>
          <e type="operand">ts</e>
          <e type="operand">tw</e>
          <e type="function" args="2">WordWrap</e>
          <e type="operand">TextString</e>
          <e type="operand">ts</e>
          <e type="operator" args="2">:</e>
          <e type="operand">TextWidth</e>
          <e type="operand">tw</e>
          <e type="operator" args="2">:</e>
          <e type="operand">StrtPos</e>
          <e type="operand">1</e>
          <e type="operator" args="2">:</e>
          <e type="operand">EndPos</e>
          <e type="operand">StrtPos</e>
          <e type="operand">TextWidth</e>
          <e type="operator" args="2">+</e>
          <e type="operand">1</e>
          <e type="operator" args="2">-</e>
          <e type="operator" args="2">:</e>
          <e type="operand">TS_Length</e>
          <e type="operand">TextString</e>
          <e type="function" args="1">strlen</e>
          <e type="operator" args="2">:</e>
          <e type="operand">i_row</e>
          <e type="operand">1</e>
          <e type="operator" args="2">:</e>
          <e type="operand">EndPos</e>
          <e type="operand">TS_Length</e>
          <e type="operator" args="2">≤</e>
          <e type="operand">Letter_EndPos</e>
          <e type="operand">TextString</e>
          <e type="operand">EndPos</e>
          <e type="operand">1</e>
          <e type="function" args="3">substr</e>
          <e type="operator" args="2">:</e>
          <e type="operand">Letter_EndPos</e>
          <e type="operand" style="string"> </e>
          <e type="operator" args="2">≠</e>
          <e type="operand">TextString</e>
          <e type="operand">EndPos</e>
          <e type="operand">1</e>
          <e type="operator" args="2">+</e>
          <e type="operand">1</e>
          <e type="function" args="3">substr</e>
          <e type="operand" style="string"> </e>
          <e type="operator" args="2">≠</e>
          <e type="operand">TextRow</e>
          <e type="operand">TextString</e>
          <e type="operand">StrtPos</e>
          <e type="operand">TextWidth</e>
          <e type="function" args="3">substr</e>
          <e type="operator" args="2">:</e>
          <e type="operand">LastSpacePosition</e>
          <e type="operand">TextRow</e>
          <e type="operand" style="string"> </e>
          <e type="function" args="2">findstr</e>
          <e type="function" args="1">Max</e>
          <e type="operator" args="2">:</e>
          <e type="operand">TextWrapped</e>
          <e type="operand">i_row</e>
          <e type="function" args="2">el</e>
          <e type="operand">TextString</e>
          <e type="operand">StrtPos</e>
          <e type="operand">LastSpacePosition</e>
          <e type="operand">1</e>
          <e type="operator" args="2">-</e>
          <e type="function" args="3">substr</e>
          <e type="operator" args="2">:</e>
          <e type="operand">StrtPos</e>
          <e type="operand">StrtPos</e>
          <e type="operand">LastSpacePosition</e>
          <e type="operator" args="2">+</e>
          <e type="operator" args="2">:</e>
          <e type="operand">EndPos</e>
          <e type="operand">StrtPos</e>
          <e type="operand">TextWidth</e>
          <e type="operator" args="2">+</e>
          <e type="operand">1</e>
          <e type="operator" args="2">-</e>
          <e type="operator" args="2">:</e>
          <e type="operand">5</e>
          <e type="operand">1</e>
          <e type="function" args="7">line</e>
          <e type="operand">TextWrapped</e>
          <e type="operand">i_row</e>
          <e type="function" args="2">el</e>
          <e type="operand">TextString</e>
          <e type="operand">StrtPos</e>
          <e type="operand">TextWidth</e>
          <e type="function" args="3">substr</e>
          <e type="operator" args="2">:</e>
          <e type="operand">StrtPos</e>
          <e type="operand">EndPos</e>
          <e type="operand">2</e>
          <e type="operator" args="2">+</e>
          <e type="operator" args="2">:</e>
          <e type="operand">EndPos</e>
          <e type="operand">StrtPos</e>
          <e type="operand">TextWidth</e>
          <e type="operator" args="2">+</e>
          <e type="operand">1</e>
          <e type="operator" args="2">-</e>
          <e type="operator" args="2">:</e>
          <e type="operand">3</e>
          <e type="operand">1</e>
          <e type="function" args="5">line</e>
          <e type="function" args="3">if</e>
          <e type="operand">1</e>
          <e type="operand">1</e>
          <e type="function" args="3">line</e>
          <e type="operand">TextWrapped</e>
          <e type="operand">i_row</e>
          <e type="function" args="2">el</e>
          <e type="operand">TextString</e>
          <e type="operand">StrtPos</e>
          <e type="operand">TextWidth</e>
          <e type="function" args="3">substr</e>
          <e type="operator" args="2">:</e>
          <e type="operand">StrtPos</e>
          <e type="operand">EndPos</e>
          <e type="operand">1</e>
          <e type="operator" args="2">+</e>
          <e type="operator" args="2">:</e>
          <e type="operand">EndPos</e>
          <e type="operand">StrtPos</e>
          <e type="operand">TextWidth</e>
          <e type="operator" args="2">+</e>
          <e type="operand">1</e>
          <e type="operator" args="2">-</e>
          <e type="operator" args="2">:</e>
          <e type="operand">3</e>
          <e type="operand">1</e>
          <e type="function" args="5">line</e>
          <e type="function" args="3">if</e>
          <e type="operand">i_row</e>
          <e type="operand">i_row</e>
          <e type="operand">1</e>
          <e type="operator" args="2">+</e>
          <e type="operator" args="2">:</e>
          <e type="operand">3</e>
          <e type="operand">1</e>
          <e type="function" args="5">line</e>
          <e type="function" args="2">while</e>
          <e type="operand">LastRowLength</e>
          <e type="operand">TS_Length</e>
          <e type="operand">StrtPos</e>
          <e type="operator" args="2">-</e>
          <e type="operand">1</e>
          <e type="operator" args="2">+</e>
          <e type="operator" args="2">:</e>
          <e type="operand">TextWrapped</e>
          <e type="operand">i_row</e>
          <e type="function" args="2">el</e>
          <e type="operand">TextString</e>
          <e type="operand">StrtPos</e>
          <e type="operand">LastRowLength</e>
          <e type="function" args="3">substr</e>
          <e type="operator" args="2">:</e>
          <e type="operand">TextWrapped</e>
          <e type="operand">10</e>
          <e type="operand">1</e>
          <e type="function" args="12">line</e>
          <e type="operator" args="2">:</e>
        </input>
      </math>
    </region>
    <region left="765" top="126" width="157" height="98" color="#000000" fontSize="10">
      <text lang="eng" fontFamily="Arial" fontSize="10">
        <content>
          <p>\<br />|  Variables Inizialization<br />|<br />|<br />|<br />/ </p>
        </content>
      </text>
    </region>
    <region left="783" top="162" width="542" height="23" color="#000000" fontSize="10">
      <text lang="eng" fontFamily="Arial" fontSize="10">
        <content>
          <p>StrPos is the starting  position of a character in the Input text string of the text row to wrap</p>
        </content>
      </text>
    </region>
    <region left="783" top="180" width="525" height="23" color="#000000" fontSize="10" isBreakable="false">
      <text lang="eng" fontFamily="Arial" fontSize="10">
        <content>
          <p>EndPos is the last  position of a character in the Input text string of the text row to wrap</p>
        </content>
      </text>
    </region>
    <region left="783" top="198" width="278" height="23" color="#000000" fontSize="10">
      <text lang="eng" fontFamily="Arial" fontSize="10">
        <content>
          <p>this is the total length of the Input Text String</p>
        </content>
      </text>
    </region>
    <region left="783" top="216" width="408" height="23" color="#000000" fontSize="10" isBreakable="false">
      <text lang="eng" fontFamily="Arial" fontSize="10">
        <content>
          <p>i_row is the index of the Array that stores the wrapped rows of text </p>
        </content>
      </text>
    </region>
    <region left="765" top="243" width="646" height="38" color="#000000" fontSize="10">
      <text lang="eng" width="646" fontFamily="Arial" fontSize="10">
        <content>
          <p>till the position of the cursor/character EndPos is lower or equal to the length of the Input Text String executes the line istructions</p>
        </content>
      </text>
    </region>
    <region left="792" top="279" width="646" height="23" color="#000000" fontSize="10" isBreakable="false">
      <text lang="eng" width="646" fontFamily="Arial" fontSize="10">
        <content>
          <p>check if the character at  EndPos (so at the end of a substring of tw characters) is not a space</p>
        </content>
      </text>
    </region>
    <region left="828" top="297" width="428" height="23" color="#000000" fontSize="10" isBreakable="false">
      <text lang="eng" fontFamily="Arial" fontSize="10">
        <content>
          <p>if this is true check if at the  next character position there isn't a space </p>
        </content>
      </text>
    </region>
    <region left="864" top="324" width="508" height="68" color="#000000" fontSize="10" isBreakable="false">
      <text lang="eng" fontFamily="Arial" fontSize="10">
        <content>
          <p>if this is true this means that we will split a word if we break the text string  at StrPos<br />so we need to find the position of the last word in the TextRow string and update accordingly the positions/values of the variables StrPos and EndPos and extracts a substring and store it in the array TextWrapped , is length is less then tw</p>
        </content>
      </text>
    </region>
    <region left="828" top="423" width="78" height="23" color="#000000" fontSize="10">
      <text lang="eng" fontFamily="Arial" fontSize="10">
        <content>
          <p>otherwise  </p>
        </content>
      </text>
    </region>
    <region left="864" top="441" width="451" height="23" color="#000000" fontSize="10">
      <text lang="eng" fontFamily="Arial" fontSize="10">
        <content>
          <p>extracts a substring of tw characters and store it in the array TextWrapped</p>
        </content>
      </text>
    </region>
    <region left="864" top="459" width="547" height="23" color="#000000" fontSize="10">
      <text lang="eng" fontFamily="Arial" fontSize="10">
        <content>
          <p>and updates the starting and ending position to use on the next  substring in the while loop</p>
        </content>
      </text>
    </region>
    <region left="864" top="477" width="479" height="38" color="#000000" fontSize="10">
      <text lang="eng" fontFamily="Arial" fontSize="10">
        <content>
          <p>to avoid that that this substring start with a space it is added a + 2 and not a +1<br />in the StrtPos expression </p>
        </content>
      </text>
    </region>
    <region left="792" top="504" width="75" height="23" color="#000000" fontSize="10" isBreakable="false">
      <text lang="eng" fontFamily="Arial" fontSize="10">
        <content>
          <p>otherwise </p>
        </content>
      </text>
    </region>
    <region left="828" top="531" width="451" height="23" color="#000000" fontSize="10" isBreakable="false">
      <text lang="eng" fontFamily="Arial" fontSize="10">
        <content>
          <p>extracts a substring of tw characters and store it in the array TextWrapped</p>
        </content>
      </text>
    </region>
    <region left="828" top="549" width="491" height="23" color="#000000" fontSize="10">
      <text lang="eng" fontFamily="Arial" fontSize="10">
        <content>
          <p>and update accordingly the positions/values of the variables StrPos and EndPos </p>
        </content>
      </text>
    </region>
    <region left="765" top="594" width="166" height="23" color="#000000" fontSize="10" isBreakable="false">
      <text lang="eng" fontFamily="Arial" fontSize="10">
        <content>
          <p>increment the i_row index</p>
        </content>
      </text>
    </region>
    <region left="765" top="621" width="478" height="53" color="#000000" fontSize="10" isBreakable="false">
      <text lang="eng" fontFamily="Arial" fontSize="10">
        <content>
          <p>when the while do loop ends we must manage the extraction of the last part of <br />TextString so assign to the variable LastRowLength the width of the last substring to extract starting at the position StrPos</p>
        </content>
      </text>
    </region>
    <region left="0" top="720" width="1974" height="24" color="#000000" fontSize="10">
      <math>
        <input>
          <e type="operand">Text_</e>
          <e type="operand" style="string">c'era una volta un testo parecchio lungo e ci si chiedeva se riusciva a stare in una variabile ora ci proviamo e vediamo cosa succede per fare un callout, non e' finita si riesce a scrivere un testo ancora piu' lungo ma fino a quando ??</e>
          <e type="operator" args="2">:</e>
        </input>
      </math>
    </region>
    <region left="0" top="747" width="613" height="118" color="#000000" fontSize="10">
      <math>
        <input>
          <e type="operand">TextRows</e>
          <e type="operand">Text_</e>
          <e type="operand">40</e>
          <e type="function" args="2">WordWrap</e>
          <e type="operator" args="2">:</e>
        </input>
        <result action="numeric">
          <e type="operand" style="string">c'era una volta un testo parecchio lungo</e>
          <e type="operand" style="string">e ci si chiedeva se riusciva a stare in </e>
          <e type="operand" style="string">una variabile ora ci proviamo e vediamo </e>
          <e type="operand" style="string">cosa succede per fare un callout, non e'</e>
          <e type="operand" style="string">finita si riesce a scrivere un testo</e>
          <e type="operand" style="string">ancora piu' lungo ma fino a quando ??</e>
          <e type="operand">6</e>
          <e type="operand">1</e>
          <e type="function" args="8">mat</e>
        </result>
      </math>
    </region>
    <region left="630" top="747" width="287" height="118" color="#000000" fontSize="10">
      <math>
        <input>
          <e type="operand">TS_Lengths</e>
          <e type="operand">TextRows</e>
          <e type="function" args="1">strlen</e>
          <e type="function" args="1">vectorize</e>
          <e type="operator" args="2">:</e>
        </input>
        <result action="numeric">
          <e type="operand">40</e>
          <e type="operand">40</e>
          <e type="operand">40</e>
          <e type="operand">40</e>
          <e type="operand">36</e>
          <e type="operand">37</e>
          <e type="operand">6</e>
          <e type="operand">1</e>
          <e type="function" args="8">mat</e>
        </result>
      </math>
    </region>
    <region left="0" top="882" width="693" height="100" color="#000000" fontSize="10">
      <math>
        <input>
          <e type="operand">TextRows</e>
          <e type="operand">Text_</e>
          <e type="operand">50</e>
          <e type="function" args="2">WordWrap</e>
          <e type="operator" args="2">:</e>
        </input>
        <result action="numeric">
          <e type="operand" style="string">c'era una volta un testo parecchio lungo e ci si</e>
          <e type="operand" style="string">chiedeva se riusciva a stare in una variabile ora </e>
          <e type="operand" style="string">ci proviamo e vediamo cosa succede per fare un</e>
          <e type="operand" style="string">callout, non e' finita si riesce a scrivere un</e>
          <e type="operand" style="string">testo ancora piu' lungo ma fino a quando ??</e>
          <e type="operand">5</e>
          <e type="operand">1</e>
          <e type="function" args="7">mat</e>
        </result>
      </math>
    </region>
    <region left="702" top="882" width="287" height="100" color="#000000" fontSize="10">
      <math>
        <input>
          <e type="operand">TS_Lengths</e>
          <e type="operand">TextRows</e>
          <e type="function" args="1">strlen</e>
          <e type="function" args="1">vectorize</e>
          <e type="operator" args="2">:</e>
        </input>
        <result action="numeric">
          <e type="operand">48</e>
          <e type="operand">50</e>
          <e type="operand">46</e>
          <e type="operand">46</e>
          <e type="operand">43</e>
          <e type="operand">5</e>
          <e type="operand">1</e>
          <e type="function" args="7">mat</e>
        </result>
      </math>
    </region>
    <region left="0" top="981" width="653" height="118" color="#000000" fontSize="10">
      <math>
        <input>
          <e type="operand">TextRows</e>
          <e type="operand">Text_</e>
          <e type="operand">45</e>
          <e type="function" args="2">WordWrap</e>
          <e type="operator" args="2">:</e>
        </input>
        <result action="numeric">
          <e type="operand" style="string">c'era una volta un testo parecchio lungo e ci</e>
          <e type="operand" style="string">si chiedeva se riusciva a stare in una</e>
          <e type="operand" style="string">variabile ora ci proviamo e vediamo cosa</e>
          <e type="operand" style="string">succede per fare un callout, non e' finita si</e>
          <e type="operand" style="string">riesce a scrivere un testo ancora piu' lungo </e>
          <e type="operand" style="string">ma fino a quando ??</e>
          <e type="operand">6</e>
          <e type="operand">1</e>
          <e type="function" args="8">mat</e>
        </result>
      </math>
    </region>
    <region left="630" top="981" width="287" height="118" color="#000000" fontSize="10">
      <math>
        <input>
          <e type="operand">TS_Lengths</e>
          <e type="operand">TextRows</e>
          <e type="function" args="1">strlen</e>
          <e type="function" args="1">vectorize</e>
          <e type="operator" args="2">:</e>
        </input>
        <result action="numeric">
          <e type="operand">45</e>
          <e type="operand">38</e>
          <e type="operand">40</e>
          <e type="operand">45</e>
          <e type="operand">45</e>
          <e type="operand">19</e>
          <e type="operand">6</e>
          <e type="operand">1</e>
          <e type="function" args="8">mat</e>
        </result>
      </math>
    </region>
  </regions>
</worksheet>