Changeset 672


Ignore:
Timestamp:
01/09/14 11:59:53 (10 years ago)
Author:
ngw8
Message:

Webparser improvements regarding how whitespace is dealt with

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/expeditee/io/WebParser.java

    r662 r672  
    99import java.net.MalformedURLException;
    1010import java.net.URL;
     11import java.util.ArrayList;
    1112import java.util.Arrays;
     13import java.util.regex.Matcher;
     14import java.util.regex.Pattern;
    1215
    1316import javax.imageio.ImageIO;
     
    153156                                                                                + "function addToSpan(text) {"
    154157                                                                                + "             span = document.createElement('wordSpan');"
    155                                                                                 + "             span.textContent = text + ' ';"
     158                                                                                + "             span.textContent = text;"
    156159                                                                                + "             textNode.parentElement.insertBefore(span, textNode);"
    157160                                                                                + "             if (prevSpan !== null && span.getBoundingClientRect().top > prevSpan.getBoundingClientRect().top) {"
     
    175178                                                // Getting an array of HTML elements from the page that will be checked for 'content' (i.e. will be modified to be
    176179                                                // 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 *');");
    178181                                                int contentElementsLength = (Integer) JavaFX.JSObjectGetMember.invoke(contentElements, "length");
    179182
     
    197200                                                                // Looping through all the text nodes in the current paragraph
    198201                                                                while ((textNode = (Node) JavaFX.WebEngineExecuteScript.invoke(webEngine, "walker.nextNode()")) != null) {
    199                                                                         // Making the current node accesable in JavaScript
     202                                                                        // Making the current node accessible in JavaScript
    200203                                                                        JavaFX.JSObjectSetMember.invoke(window, "textNode", textNode);
    201204                                                                       
     
    207210
    208211                                                                        // 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);
    211214                                                                       
    212215                                                                        // Clearing all text from the current text node (but not removing it, as it is needed as a reference
     
    215218                                                                       
    216219                                                                        // Adding each word back to the page
    217                                                                         for (int j = 0; j < wordsLength; j++) {
    218                                                                                 Object currentWord = JavaFX.JSObjectGetSlot.invoke(words, j);
     220                                                                        for (int j = 0; j < words.length; j++) {
     221                                                                                Object currentWord = words[j];
    219222                                                                                JavaFX.JSObjectCall.invoke(window, "addToSpan", new Object[] { currentWord });
    220223                                                                        }
     
    270273                                                                                + Float.valueOf(JavaFX.WebEngineExecuteScript.invoke(webEngine, "window.pageYOffset").toString());
    271274
    272                                                                 float width = Float.valueOf(JavaFX.JSObjectGetMember.invoke(bounds, "width").toString()) + 5;
     275                                                                float width = Float.valueOf(JavaFX.JSObjectGetMember.invoke(bounds, "width").toString());
    273276                                                                float height = Float.valueOf(JavaFX.JSObjectGetMember.invoke(bounds, "height").toString());
    274277
     
    368371                                                                                }
    369372
    370                                                                                 float lineSpacingInt = -1;
     373                                                                                float lineHeightInt = -1;
    371374                                                                               
    372375                                                                                try {
    373                                                                                         lineSpacingInt = (Float.parseFloat(lineHeight.substring(0, lineHeight.length() - 2)));
     376                                                                                        lineHeightInt = (Float.parseFloat(lineHeight.substring(0, lineHeight.length() - 2)));
    374377                                                                                } catch (NumberFormatException nfe) {
    375378                                                                                        // Use default value as set above
     
    377380
    378381                                                                                Text t;
    379                                                                                 String textContent = currentNode.getTextContent().replaceAll("[^\\S\\n]+", " ").trim();
     382                                                                                String textContent = currentNode.getTextContent(); // .replaceAll("[^\\S\\n]+", " ").trim();
    380383
    381384                                                                                if (textTransform.equals("uppercase")) {
     
    385388                                                                                }
    386389
    387                                                                                 t = frame.addText((int) x, (int) y, textContent, null);
     390                                                                                t = frame.addText(Math.round(x), Math.round(y), textContent, null);
    388391
    389392                                                                                t.setColor(rgbStringToColor(color));
     
    394397                                                                                t.setLetterSpacing(letterSpacingFloat);
    395398
     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)
    396401                                                                                t.setSpacing(0);
    397                                                                                 t.setSpacing(lineSpacingInt - t.getLineHeight());
     402
     403                                                                                t.setSpacing(lineHeightInt - t.getLineHeight());
    398404
    399405                                                                                if (align.equals("left")) {
     
    407413                                                                                }
    408414
    409                                                                                 // Font size divided by 2 is added to the item width to give a little breathing room
    410                                                                                 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())));
    411417
    412418                                                                        } else if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
     
    679685                pic.getSource().anchor();
    680686        }
     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         }
    681718}
Note: See TracChangeset for help on using the changeset viewer.