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 cannot fit
(char[] buffer, int start, int stop,
float boxWidth, float spaceWidth)
| 4879 | * @return false if cannot fit |
| 4880 | */ |
| 4881 | protected boolean textSentence(char[] buffer, int start, int stop, |
| 4882 | float boxWidth, float spaceWidth) { |
| 4883 | float runningX = 0; |
| 4884 | |
| 4885 | // Keep track of this separately from index, since we'll need to back up |
| 4886 | // from index when breaking words that are too long to fit. |
| 4887 | int lineStart = start; |
| 4888 | int wordStart = start; |
| 4889 | int index = start; |
| 4890 | while (index <= stop) { |
| 4891 | // boundary of a word or end of this sentence |
| 4892 | if ((buffer[index] == ' ') || (index == stop)) { |
| 4893 | // System.out.println((index == stop) + " " + wordStart + " " + index); |
| 4894 | float wordWidth = 0; |
| 4895 | if (index > wordStart) { |
| 4896 | // we have a non-empty word, measure it |
| 4897 | wordWidth = textWidthImpl(buffer, wordStart, index); |
| 4898 | } |
| 4899 | |
| 4900 | if (runningX + wordWidth >= boxWidth) { |
| 4901 | if (runningX != 0) { |
| 4902 | // Next word is too big, output the current line and advance |
| 4903 | index = wordStart; |
| 4904 | textSentenceBreak(lineStart, index); |
| 4905 | // Eat whitespace before the first word on the next line. |
| 4906 | while ((index < stop) && (buffer[index] == ' ')) { |
| 4907 | index++; |
| 4908 | } |
| 4909 | } else { // (runningX == 0) |
| 4910 | // If this is the first word on the line, and its width is greater |
| 4911 | // than the width of the text box, then break the word where at the |
| 4912 | // max width, and send the rest of the word to the next line. |
| 4913 | if (index - wordStart < 25) { |
| 4914 | do { |
| 4915 | index--; |
| 4916 | if (index == wordStart) { |
| 4917 | // Not a single char will fit on this line. screw 'em. |
| 4918 | return false; |
| 4919 | } |
| 4920 | wordWidth = textWidthImpl(buffer, wordStart, index); |
| 4921 | } while (wordWidth > boxWidth); |
| 4922 | } else { |
| 4923 | // This word is more than 25 characters long, might be faster to |
| 4924 | // start from the beginning of the text rather than shaving from |
| 4925 | // the end of it, which is super slow if it's 1000s of letters. |
| 4926 | // https://github.com/processing/processing/issues/211 |
| 4927 | int lastIndex = index; |
| 4928 | index = wordStart + 1; |
| 4929 | // walk to the right while things fit |
| 4930 | while ((wordWidth = textWidthImpl(buffer, wordStart, index)) < boxWidth) { |
| 4931 | index++; |
| 4932 | if (index > lastIndex) { // Unreachable? |
| 4933 | break; |
| 4934 | } |
| 4935 | } |
| 4936 | index--; |
| 4937 | if (index == wordStart) { |
| 4938 | return false; // nothing fits |
no test coverage detected