- Timestamp:
- 01/09/14 11:59:53 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/expeditee/io/WebParser.java
r662 r672 9 9 import java.net.MalformedURLException; 10 10 import java.net.URL; 11 import java.util.ArrayList; 11 12 import java.util.Arrays; 13 import java.util.regex.Matcher; 14 import java.util.regex.Pattern; 12 15 13 16 import javax.imageio.ImageIO; … … 153 156 + "function addToSpan(text) {" 154 157 + " span = document.createElement('wordSpan');" 155 + " span.textContent = text + ' ';"158 + " span.textContent = text;" 156 159 + " textNode.parentElement.insertBefore(span, textNode);" 157 160 + " if (prevSpan !== null && span.getBoundingClientRect().top > prevSpan.getBoundingClientRect().top) {" … … 175 178 // Getting an array of HTML elements from the page that will be checked for 'content' (i.e. will be modified to be 176 179 // properly wrapped in Expeditee) 177 Object contentElements = JavaFX.WebEngineExecuteScript.invoke(webEngine, "document.querySelectorAll(' p,li');");180 Object contentElements = JavaFX.WebEngineExecuteScript.invoke(webEngine, "document.querySelectorAll('body *');"); 178 181 int contentElementsLength = (Integer) JavaFX.JSObjectGetMember.invoke(contentElements, "length"); 179 182 … … 197 200 // Looping through all the text nodes in the current paragraph 198 201 while ((textNode = (Node) JavaFX.WebEngineExecuteScript.invoke(webEngine, "walker.nextNode()")) != null) { 199 // Making the current node acces able in JavaScript202 // Making the current node accessible in JavaScript 200 203 JavaFX.JSObjectSetMember.invoke(window, "textNode", textNode); 201 204 … … 207 210 208 211 // Splitting the text node's content into individual words 209 Object words = JavaFX.WebEngineExecuteScript.invoke(webEngine, "textNode.textContent.split(/\\s+/);");210 int wordsLength = (Integer) JavaFX.JSObjectGetMember.invoke(words, "length");212 String textContent = (String) JavaFX.WebEngineExecuteScript.invoke(webEngine, "textNode.textContent"); 213 String[] words = splitIntoWords(textContent); 211 214 212 215 // Clearing all text from the current text node (but not removing it, as it is needed as a reference … … 215 218 216 219 // Adding each word back to the page 217 for (int j = 0; j < words Length; j++) {218 Object currentWord = JavaFX.JSObjectGetSlot.invoke(words, j);220 for (int j = 0; j < words.length; j++) { 221 Object currentWord = words[j]; 219 222 JavaFX.JSObjectCall.invoke(window, "addToSpan", new Object[] { currentWord }); 220 223 } … … 270 273 + Float.valueOf(JavaFX.WebEngineExecuteScript.invoke(webEngine, "window.pageYOffset").toString()); 271 274 272 float width = Float.valueOf(JavaFX.JSObjectGetMember.invoke(bounds, "width").toString()) + 5;275 float width = Float.valueOf(JavaFX.JSObjectGetMember.invoke(bounds, "width").toString()); 273 276 float height = Float.valueOf(JavaFX.JSObjectGetMember.invoke(bounds, "height").toString()); 274 277 … … 368 371 } 369 372 370 float line SpacingInt = -1;373 float lineHeightInt = -1; 371 374 372 375 try { 373 line SpacingInt = (Float.parseFloat(lineHeight.substring(0, lineHeight.length() - 2)));376 lineHeightInt = (Float.parseFloat(lineHeight.substring(0, lineHeight.length() - 2))); 374 377 } catch (NumberFormatException nfe) { 375 378 // Use default value as set above … … 377 380 378 381 Text t; 379 String textContent = currentNode.getTextContent() .replaceAll("[^\\S\\n]+", " ").trim();382 String textContent = currentNode.getTextContent(); // .replaceAll("[^\\S\\n]+", " ").trim(); 380 383 381 384 if (textTransform.equals("uppercase")) { … … 385 388 } 386 389 387 t = frame.addText( (int) x, (int) y, textContent, null);390 t = frame.addText(Math.round(x), Math.round(y), textContent, null); 388 391 389 392 t.setColor(rgbStringToColor(color)); … … 394 397 t.setLetterSpacing(letterSpacingFloat); 395 398 399 // Removing any spacing between lines allowing t.getLineHeight() to be used to get the actual height 400 // of just the characters (i.e. distance from ascenders to descenders) 396 401 t.setSpacing(0); 397 t.setSpacing(lineSpacingInt - t.getLineHeight()); 402 403 t.setSpacing(lineHeightInt - t.getLineHeight()); 398 404 399 405 if (align.equals("left")) { … … 407 413 } 408 414 409 // Font size divided by 2is added to the item width to give a little breathing room410 t.setWidth(Math.round(width + (t.getSize() / 2)));415 // Font size is added to the item width to give a little breathing room 416 t.setWidth(Math.round(width + (t.getSize()))); 411 417 412 418 } else if (currentNode.getNodeType() == Node.ELEMENT_NODE) { … … 679 685 pic.getSource().anchor(); 680 686 } 687 688 private static String[] splitIntoWords(String toSplit) { 689 ArrayList<String> words = new ArrayList<String>(); 690 Pattern regex = Pattern.compile("\\s+"); 691 Matcher matcher = regex.matcher(toSplit); 692 693 // The index at which the previous word ended 694 int prevEndIndex = 0; 695 696 String prev = null; 697 698 while (matcher.find()) { 699 String w = toSplit.substring(prevEndIndex, matcher.start()); 700 System.out.println(toSplit.substring(0, 0)); 701 if (prev != null) { 702 words.add(prev + " "); 703 } 704 705 prev = w; 706 prevEndIndex = matcher.end(); 707 } 708 709 // Adding the final two words 710 if (prev != null) { 711 words.add(prev + " "); 712 } 713 714 words.add(toSplit.substring(prevEndIndex)); 715 716 return words.toArray(new String[words.size()]); 717 } 681 718 }
Note:
See TracChangeset
for help on using the changeset viewer.