Emit a sentence of text, defined as a chunk of text without any newlines. @param stop non-inclusive, the end of the text in question @return false if it cannot fit
(char[] buffer, int start, int stop,
float boxWidth)
| 4948 | * @return false if it cannot fit |
| 4949 | */ |
| 4950 | protected boolean textSentence(char[] buffer, int start, int stop, |
| 4951 | float boxWidth) { |
| 4952 | float runningX = 0; |
| 4953 | |
| 4954 | // Keep track of this separately from index, since we'll need to back up |
| 4955 | // from index when breaking words that are too long to fit. |
| 4956 | int lineStart = start; |
| 4957 | int wordStart = start; |
| 4958 | int index = start; |
| 4959 | while (index <= stop) { |
| 4960 | // boundary of a word or end of this sentence |
| 4961 | if ((buffer[index] == ' ') || (index == stop)) { |
| 4962 | // System.out.println((index == stop) + " " + wordStart + " " + index); |
| 4963 | float wordWidth = 0; |
| 4964 | if (index > wordStart) { |
| 4965 | // we have a non-empty word, measure it |
| 4966 | wordWidth = textWidthImpl(buffer, wordStart, index); |
| 4967 | } |
| 4968 | |
| 4969 | if (runningX + wordWidth >= boxWidth) { |
| 4970 | if (runningX != 0) { |
| 4971 | // Next word is too big, output the current line and advance |
| 4972 | index = wordStart; |
| 4973 | textSentenceBreak(lineStart, index); |
| 4974 | // Eat whitespace before the first word on the next line. |
| 4975 | while ((index < stop) && (buffer[index] == ' ')) { |
| 4976 | index++; |
| 4977 | } |
| 4978 | } else { // (runningX == 0) |
| 4979 | // If this is the first word on the line, and its width is greater |
| 4980 | // than the width of the text box, then break the word where at the |
| 4981 | // max width, and send the rest of the word to the next line. |
| 4982 | if (index - wordStart < 25) { |
| 4983 | do { |
| 4984 | index--; |
| 4985 | if (index == wordStart) { |
| 4986 | // Not a single char will fit on this line. screw 'em. |
| 4987 | return false; |
| 4988 | } |
| 4989 | wordWidth = textWidthImpl(buffer, wordStart, index); |
| 4990 | } while (wordWidth > boxWidth); |
| 4991 | } else { |
| 4992 | // This word is more than 25 characters long, might be faster to |
| 4993 | // start from the beginning of the text rather than shaving from |
| 4994 | // the end of it, which is super slow if it's 1000s of letters. |
| 4995 | // https://github.com/processing/processing/issues/211 |
| 4996 | int lastIndex = index; |
| 4997 | index = wordStart + 1; |
| 4998 | // walk to the right while things fit |
| 4999 | // while ((wordWidth = textWidthImpl(buffer, wordStart, index)) < boxWidth) { |
| 5000 | while (textWidthImpl(buffer, wordStart, index) < boxWidth) { |
| 5001 | index++; |
| 5002 | if (index > lastIndex) { // Unreachable? |
| 5003 | break; |
| 5004 | } |
| 5005 | } |
| 5006 | index--; |
| 5007 | if (index == wordStart) { |
no test coverage detected