LineBreakMeasurer.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. var AttributedStringIterator = function(text){
  2. //this.text = this.rtrim(this.ltrim(text));
  3. text = text.replace(/(\s)+/, " ");
  4. this.text = this.rtrim(text);
  5. /*
  6. if (beginIndex < 0 || beginIndex > endIndex || endIndex > length()) {
  7. throw new IllegalArgumentException("Invalid substring range");
  8. }
  9. */
  10. this.beginIndex = 0;
  11. this.endIndex = this.text.length;
  12. this.currentIndex = this.beginIndex;
  13. //console.group("[AttributedStringIterator]");
  14. var i = 0;
  15. var string = this.text;
  16. var fullPos = 0;
  17. //console.log("string: \"" + string + "\", length: " + string.length);
  18. this.startWordOffsets = [];
  19. this.startWordOffsets.push(fullPos);
  20. // TODO: remove i 1000
  21. while (i<1000) {
  22. var pos = string.search(/[ \t\n\f-\.\,]/);
  23. if (pos == -1)
  24. break;
  25. // whitespace start
  26. fullPos += pos;
  27. string = string.substr(pos);
  28. ////console.log("fullPos: " + fullPos + ", pos: " + pos + ", string: ", string);
  29. // remove whitespaces
  30. var pos = string.search(/[^ \t\n\f-\.\,]/);
  31. if (pos == -1)
  32. break;
  33. // whitespace end
  34. fullPos += pos;
  35. string = string.substr(pos);
  36. ////console.log("fullPos: " + fullPos);
  37. this.startWordOffsets.push(fullPos);
  38. i++;
  39. }
  40. //console.log("startWordOffsets: ", this.startWordOffsets);
  41. //console.groupEnd();
  42. };
  43. AttributedStringIterator.prototype = {
  44. getEndIndex: function(pos){
  45. if (typeof(pos) == "undefined")
  46. return this.endIndex;
  47. var string = this.text.substr(pos, this.endIndex - pos);
  48. var posEndOfLine = string.search(/[\n]/);
  49. if (posEndOfLine == -1)
  50. return this.endIndex;
  51. else
  52. return pos + posEndOfLine;
  53. },
  54. getBeginIndex: function(){
  55. return this.beginIndex;
  56. },
  57. isWhitespace: function(pos){
  58. var str = this.text[pos];
  59. var whitespaceChars = " \t\n\f";
  60. return (whitespaceChars.indexOf(str) != -1);
  61. },
  62. isNewLine: function(pos){
  63. var str = this.text[pos];
  64. var whitespaceChars = "\n";
  65. return (whitespaceChars.indexOf(str) != -1);
  66. },
  67. preceding: function(pos){
  68. //console.group("[AttributedStringIterator.preceding]");
  69. for(var i in this.startWordOffsets) {
  70. var startWordOffset = this.startWordOffsets[i];
  71. if (pos < startWordOffset && i>0) {
  72. //console.log("startWordOffset: " + this.startWordOffsets[i-1]);
  73. //console.groupEnd();
  74. return this.startWordOffsets[i-1];
  75. }
  76. }
  77. //console.log("pos: " + pos);
  78. //console.groupEnd();
  79. return this.startWordOffsets[i];
  80. },
  81. following: function(pos){
  82. //console.group("[AttributedStringIterator.following]");
  83. for(var i in this.startWordOffsets) {
  84. var startWordOffset = this.startWordOffsets[i];
  85. if (pos < startWordOffset && i>0) {
  86. //console.log("startWordOffset: " + this.startWordOffsets[i]);
  87. //console.groupEnd();
  88. return this.startWordOffsets[i];
  89. }
  90. }
  91. //console.log("pos: " + pos);
  92. //console.groupEnd();
  93. return this.startWordOffsets[i];
  94. },
  95. ltrim: function(str){
  96. var patt2=/^\s+/g;
  97. return str.replace(patt2, "");
  98. },
  99. rtrim: function(str){
  100. var patt2=/\s+$/g;
  101. return str.replace(patt2, "");
  102. },
  103. getLayout: function(start, limit){
  104. return this.text.substr(start, limit - start);
  105. },
  106. getCharAtPos: function(pos) {
  107. return this.text[pos];
  108. }
  109. };
  110. var LineBreakMeasurer = function(paper, x, y, text, fontAttrs){
  111. this.paper = paper;
  112. this.text = new AttributedStringIterator(text);
  113. this.fontAttrs = fontAttrs;
  114. if (this.text.getEndIndex() - this.text.getBeginIndex() < 1) {
  115. throw {message: "Text must contain at least one character.", code: "IllegalArgumentException"};
  116. }
  117. //this.measurer = new TextMeasurer(paper, this.text, this.fontAttrs);
  118. this.limit = this.text.getEndIndex();
  119. this.pos = this.start = this.text.getBeginIndex();
  120. this.rafaelTextObject = this.paper.text(x, y, this.text.text).attr(fontAttrs).attr("text-anchor", "start");
  121. this.svgTextObject = this.rafaelTextObject[0];
  122. };
  123. LineBreakMeasurer.prototype = {
  124. nextOffset: function(wrappingWidth, offsetLimit, requireNextWord) {
  125. //console.group("[nextOffset]");
  126. var nextOffset = this.pos;
  127. if (this.pos < this.limit) {
  128. if (offsetLimit <= this.pos) {
  129. throw {message: "offsetLimit must be after current position", code: "IllegalArgumentException"};
  130. }
  131. var charAtMaxAdvance = this.getLineBreakIndex(this.pos, wrappingWidth);
  132. //charAtMaxAdvance --;
  133. //console.log("charAtMaxAdvance:", charAtMaxAdvance, ", [" + this.text.getCharAtPos(charAtMaxAdvance) + "]");
  134. if (charAtMaxAdvance == this.limit) {
  135. nextOffset = this.limit;
  136. //console.log("charAtMaxAdvance == this.limit");
  137. } else if (this.text.isNewLine(charAtMaxAdvance)) {
  138. //console.log("isNewLine");
  139. nextOffset = charAtMaxAdvance+1;
  140. } else if (this.text.isWhitespace(charAtMaxAdvance)) {
  141. // TODO: find next noSpaceChar
  142. //return nextOffset;
  143. nextOffset = this.text.following(charAtMaxAdvance);
  144. } else {
  145. // Break is in a word; back up to previous break.
  146. /*
  147. var testPos = charAtMaxAdvance + 1;
  148. if (testPos == this.limit) {
  149. console.error("hbz...");
  150. } else {
  151. nextOffset = this.text.preceding(charAtMaxAdvance);
  152. }
  153. */
  154. nextOffset = this.text.preceding(charAtMaxAdvance);
  155. if (nextOffset <= this.pos) {
  156. nextOffset = Math.max(this.pos+1, charAtMaxAdvance);
  157. }
  158. }
  159. }
  160. if (nextOffset > offsetLimit) {
  161. nextOffset = offsetLimit;
  162. }
  163. //console.log("nextOffset: " + nextOffset);
  164. //console.groupEnd();
  165. return nextOffset;
  166. },
  167. nextLayout: function(wrappingWidth) {
  168. //console.groupCollapsed("[nextLayout]");
  169. if (this.pos < this.limit) {
  170. var requireNextWord = false;
  171. var layoutLimit = this.nextOffset(wrappingWidth, this.limit, requireNextWord);
  172. //console.log("layoutLimit:", layoutLimit);
  173. if (layoutLimit == this.pos) {
  174. //console.groupEnd();
  175. return null;
  176. }
  177. var result = this.text.getLayout(this.pos, layoutLimit);
  178. //console.log("layout: \"" + result + "\"");
  179. // remove end of line
  180. //var posEndOfLine = this.text.getEndIndex(this.pos);
  181. //if (posEndOfLine < result.length)
  182. // result = result.substr(0, posEndOfLine);
  183. this.pos = layoutLimit;
  184. //console.groupEnd();
  185. return result;
  186. } else {
  187. //console.groupEnd();
  188. return null;
  189. }
  190. },
  191. getLineBreakIndex: function(pos, wrappingWidth) {
  192. //console.group("[getLineBreakIndex]");
  193. //console.log("pos:"+pos + ", text: \""+ this.text.text.replace(/\n/g, "_").substr(pos, 1) + "\"");
  194. var bb = this.rafaelTextObject.getBBox();
  195. var charNum = -1;
  196. try {
  197. var svgPoint = this.svgTextObject.getStartPositionOfChar(pos);
  198. //var dot = this.paper.ellipse(svgPoint.x, svgPoint.y, 1, 1).attr({"stroke-width": 0, fill: Color.blue});
  199. svgPoint.x = svgPoint.x + wrappingWidth;
  200. //svgPoint.y = bb.y;
  201. //console.log("svgPoint:", svgPoint);
  202. //var dot = this.paper.ellipse(svgPoint.x, svgPoint.y, 1, 1).attr({"stroke-width": 0, fill: Color.red});
  203. charNum = this.svgTextObject.getCharNumAtPosition(svgPoint);
  204. } catch (e){
  205. console.warn("getStartPositionOfChar error, pos:" + pos);
  206. /*
  207. var testPos = pos + 1;
  208. if (testPos < this.limit) {
  209. return testPos
  210. }
  211. */
  212. }
  213. //console.log("charNum:", charNum);
  214. if (charNum == -1) {
  215. //console.groupEnd();
  216. return this.text.getEndIndex(pos);
  217. } else {
  218. // When case there is new line between pos and charnum then use this new line
  219. var newLineIndex = this.text.getEndIndex(pos);
  220. if (newLineIndex < charNum ) {
  221. console.log("newLineIndex <= charNum, newLineIndex:"+newLineIndex+", charNum:"+charNum, "\"" + this.text.text.substr(newLineIndex+1).replace(/\n/g, "?") + "\"");
  222. //console.groupEnd();
  223. return newLineIndex;
  224. }
  225. //var charAtMaxAdvance = this.text.text.substring(charNum, charNum + 1);
  226. var charAtMaxAdvance = this.text.getCharAtPos(charNum);
  227. //console.log("!!charAtMaxAdvance: " + charAtMaxAdvance);
  228. //console.groupEnd();
  229. return charNum;
  230. }
  231. },
  232. getPosition: function() {
  233. return this.pos;
  234. }
  235. };