Guest User

Signage

a guest
Mar 25th, 2011
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. <cffunction name="getSignInfo" returntype="Struct">
  3.     <cfargument name="message" type="string" required="true">
  4.     <cfargument name="width" type="numeric" required="true">
  5.     <cfargument name="includeSpaces" type="boolean" required="false" default="false"/>
  6.     <cfargument name="fixWidthIssues" type="boolean" required="false" default="true">
  7.    
  8.     <cfset var signDetails = {}/>
  9.     <cfset var adjustedMessage = trim(arguments.message)/>
  10.     <cfset var charArr = listtoARray(adjustedMessage,'')/>
  11.     <cfset var wordArr = listtoARray(adjustedMessage,' ')/>
  12.     <cfset var i = ""/>
  13.     <cfset var wordLength = ""/>
  14.     <cfset var currentRow = 1/>
  15.     <cfset var slotsFilled = 0/>
  16.    
  17.     <!---
  18.         Examine each word in the message.
  19.         If a word is too long for the width of the sign, we'll add spaces to adjust the message (Basically creating a "word wrap" feature)
  20.         Optionally, we can throw an error when this happens.
  21.      --->  
  22.     <cfloop from="1" to="#arraylen(wordArr)#" index="i">
  23.         <cfset wordLength = len(wordarr[i])/>
  24.         <cfif wordLength gt arguments.width>
  25.             <cfif arguments.fixWidthIssues>
  26.                 <cfset wordArr[i] = left(wordArr[i],arguments.width) & " " & mid(wordArr[i],arguments.width,wordLength)/>
  27.             <cfelse>
  28.                 <cfthrow message="Oh noes! One of your words is too long for the sign!"/>              
  29.             </cfif>
  30.         </cfif>
  31.     </cfloop>
  32.        
  33.     <cfset adjustedMessage = arrayToList(wordArr," ")/>
  34.    
  35.     <!--- Loop and count each letter.  We can also return the number of spaces if desired --->
  36.     <cfset signDetails.letterData = {}/>
  37.     <cfloop array="#charArr#" index="i">
  38.         <cfif i eq " " && !arguments.includeSpaces>
  39.             <cfcontinue/>
  40.         </cfif>
  41.         <cfif !structkeyexists(signDetails.letterData,i)>
  42.             <cfset signDetails.letterData[i] = 0/>
  43.         </cfif>
  44.         <cfset signDetails.letterData[i] += 1/>
  45.     </cfloop>
  46.    
  47.     <cfset signDetails.rowData = {}/>
  48.    
  49.     <cfset signDetails.rowData["row1"] = ""/>
  50.     <!--- Let's fill those rows --->   
  51.     <cfloop list="#adjustedMessage#" index="i" delimiters=" ">     
  52.         <cfif (wordlength + slotsFilled) gt arguments.width> <!--- No more room, next row please. --->
  53.             <cfset currentRow++/>
  54.             <cfset signDetails.rowData["row" & currentRow] = ""/>
  55.             <cfset slotsFilled = 0/>
  56.         </cfif>
  57.        
  58.         <cfset signDetails.rowData["row" & currentRow] &= i/>
  59.         <cfset slotsFilled += wordLength/>
  60.         <cfif slotsFilled lt arguments.width>
  61.             <cfset signDetails.rowData["row" & currentRow] &= " "/> <!--- Add spaces between wods on the same row --->
  62.         </cfif>
  63.     </cfloop>
  64.    
  65.     <cfreturn signDetails/>
  66. </cffunction>
Advertisement
Add Comment
Please, Sign In to add comment