[Gambas-user] Text-Wrapping

jose.rodriguez at cenpalab.cu jose.rodriguez at cenpalab.cu
Fri Nov 13 00:57:22 CET 2020


November 11, 2020 7:54 AM, "Hans Lehmann" <hans at gambas-buch.de (mailto:hans at gambas-buch.de?to=%22Hans%20Lehmann%22%20<hans at gambas-buch.de>)> wrote:
 Hello,

since there are true string artists among the readers, I just ask: I'm looking for a function like LikeWrap(arg1Text, arg2Length) with which you can wrap a text. At most arg2Length characters should be in one line.
Line breaks in the original text should be preserved. A hyphenation is not used. The text must be separated by a hyphen in the last word. The length rule also applies in this case.

Does anybody have a finished function in his source code box?
Hope I'm not too late to the party and I do apologize because I had these java functions floating around in my utils library from ages ago.They were for word-wrapping a string for a POS printer (WordWrap()) and also the print preview (WordWrapHTML()). I would have converted them to Gambas if I wasn't up to my ears in work right now, but they should be somewhat self-explanatory (I hope...).
 public static String WordWrap(String WrapString, int WrapPos) {
 //Prepare variables
 String rsm = WrapString;
 boolean gotspace;
 boolean gotfeed;
 //Jump to characters to add line feeds
 int pos = WrapPos;
 while (pos < rsm.length()) {
 //Progressivly go backwards until next space
 int bf = pos - WrapPos; //What is the stop point
 gotspace = false;
 gotfeed = false;
 //Find space, slash or linefeed just before to avoid cutting words
 for (int ap = pos; ap > bf; ap--) {
 if (String.valueOf(rsm.charAt(ap)).equals(" ") == true && gotspace == false) {
 //Is it a space?
 //Insert line feed and compute position variable
 gotspace = true;
 pos = ap; //Go to position
 } else if (String.valueOf(rsm.charAt(ap)).equals("/") == true && gotspace == false) {
 //Is it a slash?
 gotspace = true;
 pos = ap; //Go to position
 } else if (String.valueOf(rsm.charAt(ap)).equals("n") == true && gotfeed == false) {
 //If it is a line feed, go to it
 pos = ap; //Go to position
 gotfeed = true;
 }
 }
 //Got no feed? Append a line feed to the appropriate place
 if (gotfeed == false) {
 if (gotspace == false) {
 rsm = new StringBuffer(rsm).insert(pos, "n").toString();
 } else {
 rsm = new StringBuffer(rsm).insert(pos + 1, "n").toString();
 }
 }
 //Increment position by WrapPos and restart loop
 pos += (WrapPos + 1);
 }
 //Return the result
 return (rsm);
 }
 public static String WordWrapHTML(String WrapString, int WrapPos) {
 //Prepare variables
 String rsm = WrapString;
 boolean gotspace;
 boolean gotfeed;
 //Jump to characters to add line feeds
 int pos = WrapPos;
 while (pos < rsm.length()) {
 //Progressivly go backwards until next space
 int bf = pos - WrapPos; //What is the stop point
 gotspace = false;
 gotfeed = false;
 //Find space just before to avoid cutting words
 for (int ap = pos; ap > bf; ap--) {
 if (String.valueOf(rsm.charAt(ap)).equals(" ") == true && gotspace == false) {
 //Is it a space?
 //Insert line feed and compute position variable
 gotspace = true;
 pos = ap; //Go to position
 } else if (String.valueOf(rsm.charAt(ap)).equals("/") == true && gotspace == false) {
 //Is it a slash?
 gotspace = true;
 pos = ap; //Go to position
 } else if (String.valueOf(rsm.charAt(ap)).equals("n") == true && gotfeed == false) {
 //If it is a line feed, go to it
 pos = ap; //Go to position
 gotfeed = true;
 }
 }
 //Got no feed? Append a line feed to the appropriate place
 if (gotfeed == false) {
 if (gotspace == false) {
 rsm = new StringBuffer(rsm).insert(pos, "<br>").toString();
 } else {
 rsm = new StringBuffer(rsm).insert(pos + 1, "<br>").toString();
 }
 }
 //Increment position by WrapPos and restart loop
 pos += (WrapPos + 1);
 }
 //Return the result
 return (rsm);
 }
Regards,
Joe1962
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.gambas-basic.org/pipermail/user/attachments/20201112/e592de10/attachment.htm>


More information about the User mailing list