library WordWrap /* v2.1.0.0
*********************************************************************
*
* Simulates word wrapping for multiboards and texttags.
* Word wrapping is where, if a text is too long, it will
* cut to the next line rather than cutting off the text.
*
* How it works:
*
* - Specify a source string and a size margin. The margin
* determines what the size of the line can be before it
* splits the string into fragments. The value is more or
* less arbritrary, so just experiment with values.
* - Explodes the string by spaces into words.
* - Checks the size of each word.
* - Once the size margin is reached, it will cut the string.
* - The first line is stored in the array under the index 0.
* - Rinse and repeat until it fragments the entire string.
* - You can retrieve the count to know the max index reached.
*
*********************************************************************
*
* */uses/*
*
* */ StringSize /* [url]http://wc3jass.com/5014/snippet-stringsize/msg38482/topicseen/#new[/url]
*
* This library does not require Ascii, but StringSize requires
* the Ascii library:
*
* Ascii [url]http://www.hiveworkshop.com/forums/jass-functions-413/snippet-ascii-190746/[/url]
*
*********************************************************************
*
* function WordWrapString takes string source, real sizeMargin, boolean preserveColors returns nothing
*
* - Breaks the string into several fragments.
* - Size margin determines the size the string can reach
* before it fragments the string and breaks to the next
* line.
* - Preserve colors is a boolean to check if a color
* code is being broken during the word wrap. If you
* are not using color codes (ex: |cffffcc00text|r)
* then you should set this to false to save performance.
*
* function GetWrappedStringFragment takes integer index returns string
*
* - Retrieves the fragmented strings. The indices start at 0
* and will go up to the "count" index - 1. (see below) The last
* line will be:
*
* GetWrappedStringFragment(GetWrappedStringCount() - 1)
*
* function GetWrappedStringCount takes nothing returns integer
*
* - Returns the total number of fragments. Indices start at 0,
* and the final line will be this number minus 1.
*
*********************************************************************
*
* struct WordWrap
*
* static method getLine takes integer index returns string
*
* static method getCount takes nothing returns integer
*
* static method create takes string source, real sizeMargin, boolean preserveColors returns thistype
*
*********************************************************************
*
* Credits
*
* - cleeezzz & tooltiperror for the idea.
*
* Bugs
*
* - If there is a single word that is longer than the
* "sizeMargin" input, then this system will fail.
* It requires extra checks. I may update this later
* to fix that problem, but for now this should be fine.
* PM or message me if you have any problems.
*
*********************************************************************/
struct WordWrap extends array
private static string array line
private static integer count = 0
static method getLine takes integer index returns string
return line[index]
endmethod
static method getCount takes nothing returns integer
return count
endmethod
private static method preserveColor takes nothing returns nothing
local integer i = 0
local integer k = 0
local integer l = 0
local string temp = ""
local string hex = ""
local boolean carryOn = false
loop
exitwhen i == count
set carryOn = false
set hex = ""
set k = 0
loop
exitwhen k == l
if temp == "|" then
if temp == "c" then
set carryOn = true
elseif temp == "r" then
set carryOn = false
endif
endif
set k = k + 1
endloop
set i = i + 1
if carryOn then
set line[i] = hex + line[i]
endif
endloop
endmethod
static method create takes string source, real size, boolean preserveColors returns thistype
local integer i = 0
local integer k = 0
local string s
local string word
local real result = 0
local real ssize = 0
loop
exitwhen count == 0
set count = count - 1
set line[count] = ""
endloop
loop
exitwhen i == l
if s == " " or i == (l-1) then
set ssize = MeasureString(word)
set result = result + ssize
if result <= size then
set line[count] = line[count] + word
else
set count = count + 1
set line[count] = word
set result = ssize
endif
set k = i + 1
endif
set i = i + 1
endloop
set count = count + 1
if preserveColors then
call ForForce(bj_FORCE_PLAYER[0],
function thistype.preserveColor)
endif
return 0
endmethod
endstruct
function WordWrapString takes string source, real sizeMargin, boolean preserveColors returns nothing
call WordWrap.create(source, sizeMargin, preserveColors)
endfunction
function GetWrappedStringFragment takes integer i returns string
return WordWrap.getLine(i)
endfunction
function GetWrappedStringCount takes nothing returns integer
return WordWrap.getCount()
endfunction
endlibrary