ID:195099
 
//Title: Word Wrapping (Inefficient Version)
//Credit to: Jtgibson
//Contributed by: Jtgibson


//This is a monospace word-wrapper. BYOND lacks any utilities
// to determine the width of proportional fonts, so this feature
// must remain monospace.

//Eventually I might make a version that can read the data presented
// from Lummox JR's DMIFonts, such that it could intelligently use
// proportional fonts, but that's not high on my list of things to do.


//Inefficient Version -- use Hiead's optimised version instead


proc/wordwrap(string, wordwrap=80)
//List of substrings -- one per line
var/list/strings = list()
//List of characters that line breaks can occur at
var/list/breaks = list(" ","\t")

while(length(string) > wordwrap)
var/substring = copytext(string, 1, wordwrap+1)
var/pos = 0
var/break_type = ""

//Run through the whole substring and find the highest position of a line
// break.
for(var/i = 1, i <= lentext(substring), i++)
for(var/linebreak in breaks)
//Real newlines mean we stop right here!
if(cmptext(copytext(substring, i, i+lentext("\n")), "\n"))
pos = i; break_type = "\n"
break

//Otherwise, we need to check for places where newlines can happen...
else if(cmptext(copytext(substring, i, i+length(linebreak)), linebreak))
pos = i; break_type = linebreak
break

//Escape here -- if we encounter a real newline, we quit.
if(break_type == "\n") break


if(!pos)
//No break position found! ...automatically wrapping at wordwrap limit...
strings += substring
string = copytext(string, wordwrap+1)
else
//Line break position found, we'll break there.
strings += copytext(substring, 1, pos)
string = copytext(string, pos+lentext(break_type))

var/newstring = ""
for(var/addition in strings) newstring += addition + "\n"
newstring += string
return(newstring)


///*
//Testing code/sample implementation

mob/verb/test_wordwrap(message as message, wordwrap=80 as num)
usr << "<tt>[wordwrap(message, wordwrap)]</tt>"

//*/