Changeset 630


Ignore:
Timestamp:
12/17/13 15:31:24 (11 years ago)
Author:
ngw8
Message:

WebParser bug fixes related to image repetition, default/unspecified fonts and CSS text-transform

File:
1 edited

Legend:

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

    r628 r630  
    159159                                                                .invoke(webEngine,
    160160                                                                                "function isContent(el) { "
    161                                                                                 + "             if (el.tagName === 'li' && window.getComputedStyle(el).getPropertyValue('list-style-type') !== 'none') { "
    162                                                                                 + "                     return true; "
    163                                                                                 + "             } "
    164                                                                                 + "             var text = el.textContent.replace(/(^\\s*)|(\\s*$)/, ''); "
    165                                                                                 + "             var w = text.split(/\\S\\s\\S/).length; var punct = text.split(/\\.|\\,|\\!/).length; "
    166                                                                                 + "             if (punct < 2 || w < 10) { "
     161                                                                                + "             if (el.tagName === 'li' && window.getComputedStyle(el).getPropertyValue('list-style-type') !== 'none') {"
     162                                                                                + "                     return true;"
     163                                                                                + "             }"
     164                                                                                + "             var text = el.textContent.replace(/(^\\s*)|(\\s*$)/, '');"
     165                                                                                + "             var w = text.split(/\\S\\s\\S/).length; var punct = text.split(/\\.|\\,|\\!/).length;"
     166                                                                                + "             if (punct < 2 || w < 10) {"
    167167                                                                                + "                     return false;"
    168168                                                                                + "             } else { "
    169169                                                                                + "                     return true;"
    170                                                                                 + "             } "
    171                                                                                 + "} "
     170                                                                                + "             }"
     171                                                                                + "}"
     172
    172173                                                                                + "function addToSpan(text) {"
    173174                                                                                + "             span = document.createElement('wordSpan');"
    174                                                                                 + "             span.textContent = text + ' '; "
    175                                                                                 + "             para.appendChild(span); "
    176                                                                                 + "             if (prevSpan !== null && span.getBoundingClientRect().top > prevSpan.getBoundingClientRect().top) { "
     175                                                                                + "             span.textContent = text + ' ';"
     176                                                                                + "             para.appendChild(span);"
     177                                                                                + "             if (prevSpan !== null && span.getBoundingClientRect().top > prevSpan.getBoundingClientRect().top) {"
    177178                                                                                + "                     span.textContent = '\\n' + span.textContent;"
    178179                                                                                + "                     if (prevPrevSpan !== null && prevPrevSpan.getBoundingClientRect().left == prevSpan.getBoundingClientRect().left) {"
     
    183184                                                                                + "                     }"
    184185                                                                                + "                     prevSpan = span;"
    185                                                                                
    186186                                                                                + "             } else {"
    187187                                                                                + "                     prevSpan.textContent = prevSpan.textContent + span.textContent;"
     
    196196                                                for (int i = 0; i < contentElementsLength; i++) {
    197197                                                        Element currentNode = (Element) JavaFX.JSObjectGetSlot.invoke(contentElements, i);
    198                                                         // JavaFX.JSObjectCall.invoke(window, "addBreaks", new Object[] { currentNode });
     198
    199199                                                        JavaFX.JSObjectSetMember.invoke(window, "para", currentNode);
    200200                                                       
    201201                                                        JavaFX.WebEngineExecuteScript.invoke(webEngine, ""
    202                                                                         + "var exp = {};"
    203202                                                                        + "var span = document.createElement('wordSpan');"
    204203                                                                        + "var prevSpan = span;"
     
    293292                                                                                // Returns comma-separated list of typefaces
    294293                                                                                String typeface = (String) JavaFX.JSObjectCall.invoke(style, "getPropertyValue", new Object[] { "font-family" });
     294
    295295                                                                                String[] typefaces = typeface.split(", |,");
    296296                                                                               
     
    302302                                                                                String letterSpacing = (String) JavaFX.JSObjectCall.invoke(style, "getPropertyValue", new Object[] { "letter-spacing" });
    303303
     304                                                                                String textTransform = (String) JavaFX.JSObjectCall.invoke(style, "getPropertyValue", new Object[] { "text-transform" });
     305
    304306                                                                                Boolean fontFound = false;
    305307                                                                                Font font = new Font(null);
     
    308310                                                                                // found, or the end of the list is reached, in which case the default font is used
    309311                                                                                for (int j = 0; j < typefaces.length && !fontFound; j++) {
    310 
     312                                                                                        if (typefaces[j].toLowerCase().equals("sans-serif")) {
     313                                                                                                typefaces[j] = "Arial";
     314                                                                                        } else if (typefaces[j].toLowerCase().equals("serif")) {
     315                                                                                                typefaces[j] = "Times New Roman";
     316                                                                                        }
     317                                                                                       
    311318                                                                                        // Regex will remove any inverted commas surrounding multi-word typeface names
    312319                                                                                        font = new Font(typefaces[j].replaceAll("^'|'$", ""), Font.PLAIN, 12);
    313 
     320                                                                                       
     321                                                                                        // If the font isn't found, Java just uses Font.DIALOG, so this check checks whether the font was found
    314322                                                                                        if (!(font.getFamily().toLowerCase().equals(Font.DIALOG.toLowerCase()))) {
    315323                                                                                                fontFound = true;
    316324                                                                                        }
     325                                                                                }
     326
     327                                                                                if (font.getFamily().toLowerCase().equals(Font.DIALOG.toLowerCase())) {
     328                                                                                        font = new Font("Times New Roman", Font.PLAIN, 12);
    317329                                                                                }
    318330
     
    344356                                                                                }
    345357
    346                                                                                 Node siblingNode;
    347                                                                                 NodeList siblings = currentNode.getParentNode().getChildNodes();
    348 
    349                                                                                 String combined = currentNode.getTextContent();
    350 
    351                                                                                 for (int j = 0; j < siblings.getLength(); j++) {
    352                                                                                         siblingNode = siblings.item(j);
    353                                                                                         if (siblingNode.getNodeType() == Node.TEXT_NODE) {
    354                                                                                                 System.out.println("sibling is text");
    355                                                                                                 combined = combined + siblingNode.getTextContent();
    356                                                                                         }
    357                                                                                 }
    358 
    359                                                                                 System.out.println("Combined: " + combined);
    360 
    361 
    362358                                                                                Text t;
    363 
    364                                                                                 t = frame.addText((int) x, (int) y, currentNode.getTextContent().replaceAll("[^\\S\\n]+", " ").trim(), null);
     359                                                                                String textContent = currentNode.getTextContent().replaceAll("[^\\S\\n]+", " ").trim();
     360
     361                                                                                if (textTransform.equals("uppercase")) {
     362                                                                                        textContent = textContent.toUpperCase();
     363                                                                                } else if (textTransform.equals("lowercase")) {
     364                                                                                        textContent = textContent.toUpperCase();
     365                                                                                }
     366
     367                                                                                t = frame.addText((int) x, (int) y, textContent, null);
    365368
    366369                                                                                t.setColor(rgbStringToColor(color));
     
    396399
    397400                                                                                        String bgSize = ((String) JavaFX.JSObjectCall.invoke(style, "getPropertyValue", new Object[] { "background-size" })).toLowerCase();
     401                                                                                        String bgRepeat = ((String) JavaFX.JSObjectCall.invoke(style, "getPropertyValue", new Object[] { "background-repeat" })).toLowerCase();
    398402
    399403                                                                                        // Returns "[x]px [y]px", "[x]% [y]%", "[x]px [y]%" or "[x]% [y]px"
     
    446450
    447451                                                                                        try {
    448                                                                                                 WebParser.addImageFromUrl(bgImage, frame, x, y, bgWidth, cropStartX, cropStartY, cropEndX, cropEndY);
     452                                                                                                System.out.println("START-Y: " + cropStartY + " END-Y: " + cropEndY);
     453                                                                                                WebParser.addImageFromUrl(bgImage, frame, x, y, bgWidth, cropStartX, cropStartY, cropEndX, cropEndY, bgRepeat);
    449454                                                                                        } catch (MalformedURLException mue) {
    450455                                                                                                // probably a 'data:' url, not supported yet
     
    457462                                                                                if (currentNode.getNodeName().toLowerCase().equals("img") && (imgSrc = JavaFX.JSObjectGetMember.invoke(currentNode, "src").toString()) != null) {
    458463                                                                                        try {
    459                                                                                                 WebParser.addImageFromUrl(imgSrc, frame, x, y, (int) width, null, null, null, null);
     464                                                                                                WebParser.addImageFromUrl(imgSrc, frame, x, y, (int) width, null, null, null, null, null);
    460465                                                                                        } catch (MalformedURLException mue) {
    461466                                                                                                // probably a 'data:' url, not supported yet
     
    547552         * @throws IOException
    548553         */
    549         private static void addImageFromUrl(String imgSrc, final Frame frame, float x, float y, int width, Integer cropStartX, Integer cropStartY, Integer cropEndX, Integer cropEndY)
     554        private static void addImageFromUrl(String imgSrc, final Frame frame, float x, float y, int width, Integer cropStartX, Integer cropStartY, Integer cropEndX, Integer cropEndY, String repeat)
    550555                        throws MalformedURLException,
    551556                        IOException {
     
    564569                out.mkdirs();
    565570                ImageIO.write(img, "png", out);
    566                
    567                
     571
    568572                if (cropEndX == null || cropStartX == null || cropEndY == null || cropStartY == null) {
    569573                        cropStartX = 0;
     
    571575                        cropEndX = img.getWidth();
    572576                        cropEndY = img.getHeight();
     577                } else if (cropStartX < 0) {
     578                        cropEndX = cropEndX - cropStartX;
     579                        x = x + Math.abs(cropStartX);
     580                        cropStartX = 0;
     581                }
     582
     583                if (cropStartY < 0) {
     584                        cropEndY = cropEndY - cropStartY;
     585                        y = y + Math.abs(cropStartY);
     586                        cropStartY = 0;
     587
    573588                }
    574589
     
    576591                        width = img.getWidth();
    577592                }
    578                
     593
     594                if (repeat != null) {
     595                        if (repeat.equals("no-repeat")) {
     596                                int tmpCropEndY = (int) (cropStartY + ((float) width / img.getWidth()) * img.getHeight());
     597                                int tmpCropEndX = cropStartX + width;
     598
     599                                cropEndX = (cropEndX < tmpCropEndX) ? cropEndX : tmpCropEndX;
     600                                cropEndY = (cropEndY < tmpCropEndY) ? cropEndY : tmpCropEndY;
     601                        } else if (repeat.equals("repeat-x")) {
     602                                int tmpCropEndY = (int) (cropStartY + ((float) width / img.getWidth()) * img.getHeight());
     603                                cropEndY = (cropEndY < tmpCropEndY) ? cropEndY : tmpCropEndY;
     604                        } else if (repeat.equals("repeat-y")) {
     605                                int tmpCropEndX = cropStartX + width;
     606                                cropEndX = (cropEndX < tmpCropEndX) ? cropEndX : tmpCropEndX;
     607                        }
     608                }
     609
    579610                Text text = new Text("@i: " + out.getName() + " " + width);
    580611                text.setPosition(x, y);
     
    583614               
    584615                float invScale = 1 / pic.getScale();
    585                
    586                 // System.out.println("unscaled crop: " + cropStartX + " " + cropStartY + " " + cropEndX + " " + cropEndY);
    587                 // System.out.println("crop: " + cropStartX * invScale + " " + cropStartY * invScale + " " + cropEndX * invScale + " " + cropEndY * invScale);
    588                
     616
    589617                pic.setCrop((int)(cropStartX * invScale), (int)(cropStartY * invScale), (int)(cropEndX * invScale), (int)(cropEndY * invScale));
    590                
    591                 // System.out.println("source = " + pic.getSource().getText());
    592                
     618
    593619                frame.addItem(pic);
    594620                pic.anchor();
Note: See TracChangeset for help on using the changeset viewer.